Removed trailing whitespaces from .h and .c files using the
[metze/wireshark/wip.git] / packet-lapd.c
1 /* packet-lapd.c
2  * Routines for LAPD frame disassembly
3  * Gilbert Ramirez <gram@alumni.rice.edu>
4  *
5  * $Id: packet-lapd.c,v 1.31 2002/08/28 21:00:19 jmayer Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <glib.h>
32 #include <string.h>
33 #include <epan/packet.h>
34 #include "xdlc.h"
35
36 /* ISDN/LAPD references:
37  *
38  * http://www.cisco.com/univercd/cc/td/doc/cisintwk/ito_doc/isdn.htm
39  * http://www.ece.wpi.edu/courses/ee535/hwk11cd95/agrebe/agrebe.html
40  * http://www.acacia-net.com/Clarinet/Protocol/q9213o84.htm
41  */
42
43 static int proto_lapd = -1;
44 static int hf_lapd_address = -1;
45 static int hf_lapd_sapi = -1;
46 static int hf_lapd_cr = -1;
47 static int hf_lapd_ea1 = -1;
48 static int hf_lapd_tei = -1;
49 static int hf_lapd_ea2 = -1;
50 static int hf_lapd_control = -1;
51
52 static gint ett_lapd = -1;
53 static gint ett_lapd_address = -1;
54 static gint ett_lapd_control = -1;
55
56 static dissector_handle_t q931_handle;
57 static dissector_handle_t data_handle;
58
59 /*
60  * Bits in the address field.
61  */
62 #define LAPD_SAPI       0xfc00  /* Service Access Point Identifier */
63 #define LAPD_SAPI_SHIFT 10
64 #define LAPD_CR         0x0200  /* Command/Response bit */
65 #define LAPD_EA1        0x0100  /* First Address Extension bit */
66 #define LAPD_TEI        0x00fe  /* Terminal Endpoint Identifier */
67 #define LAPD_EA2        0x0001  /* Second Address Extension bit */
68
69 #define LAPD_SAPI_Q931          0       /* Q.931 call control procedure */
70 #define LAPD_SAPI_PM_Q931       1       /* Packet mode Q.931 call control procedure */
71 #define LAPD_SAPI_X25           16      /* X.25 Level 3 procedures */
72 #define LAPD_SAPI_L2            63      /* Layer 2 management procedures */
73
74 static const value_string lapd_sapi_vals[] = {
75         { LAPD_SAPI_Q931,       "Q.931 Call control procedure" },
76         { LAPD_SAPI_PM_Q931,    "Packet mode Q.931 Call control procedure" },
77         { LAPD_SAPI_X25,        "X.25 Level 3 procedures" },
78         { LAPD_SAPI_L2,         "Layer 2 management procedures" },
79         { 0,                    NULL }
80 };
81
82 static void
83 dissect_lapd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
84 {
85         proto_tree      *lapd_tree, *addr_tree;
86         proto_item      *lapd_ti, *addr_ti;
87         guint16         control;
88         int             lapd_header_len;
89         guint16         address, cr, sapi;
90         gboolean        is_response;
91         tvbuff_t        *next_tvb;
92
93         if (check_col(pinfo->cinfo, COL_PROTOCOL))
94                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "LAPD");
95         if (check_col(pinfo->cinfo, COL_INFO))
96                 col_clear(pinfo->cinfo, COL_INFO);
97
98         address = tvb_get_ntohs(tvb, 0);
99         cr = address & LAPD_CR;
100         sapi = (address & LAPD_SAPI) >> LAPD_SAPI_SHIFT;
101         lapd_header_len = 2;    /* address */
102
103         if (pinfo->pseudo_header->p2p.sent) {
104                 is_response = cr ? TRUE : FALSE;
105                 if(check_col(pinfo->cinfo, COL_RES_DL_DST))
106                         col_set_str(pinfo->cinfo, COL_RES_DL_DST, "Network");
107                 if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
108                         col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "User");
109         }
110         else {
111                 is_response = cr ? FALSE : TRUE;
112                 if(check_col(pinfo->cinfo, COL_RES_DL_DST))
113                     col_set_str(pinfo->cinfo, COL_RES_DL_DST, "User");
114                 if(check_col(pinfo->cinfo, COL_RES_DL_SRC))
115                     col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "Network");
116         }
117
118         if (tree) {
119                 lapd_ti = proto_tree_add_item(tree, proto_lapd, tvb, 0, -1,
120                     FALSE);
121                 lapd_tree = proto_item_add_subtree(lapd_ti, ett_lapd);
122
123                 addr_ti = proto_tree_add_uint(lapd_tree, hf_lapd_address, tvb,
124                     0, 2, address);
125                 addr_tree = proto_item_add_subtree(addr_ti, ett_lapd_address);
126
127                 proto_tree_add_uint(addr_tree, hf_lapd_sapi,tvb, 0, 1, address);
128                 proto_tree_add_uint(addr_tree, hf_lapd_cr,  tvb, 0, 1, address);
129                 proto_tree_add_uint(addr_tree, hf_lapd_ea1, tvb, 0, 1, address);
130                 proto_tree_add_uint(addr_tree, hf_lapd_tei, tvb, 1, 1, address);
131                 proto_tree_add_uint(addr_tree, hf_lapd_ea2, tvb, 1, 1, address);
132         }
133         else {
134                 lapd_ti = NULL;
135                 lapd_tree = NULL;
136         }
137
138         control = dissect_xdlc_control(tvb, 2, pinfo, lapd_tree, hf_lapd_control,
139             ett_lapd_control, is_response, TRUE);
140         lapd_header_len += XDLC_CONTROL_LEN(control, TRUE);
141
142         if (tree)
143                 proto_item_set_len(lapd_ti, lapd_header_len);
144
145         next_tvb = tvb_new_subset(tvb, lapd_header_len, -1, -1);
146         if (XDLC_IS_INFORMATION(control)) {
147                 /* call next protocol */
148                 switch (sapi) {
149
150                 case LAPD_SAPI_Q931:
151                         call_dissector(q931_handle, next_tvb, pinfo, tree);
152                         break;
153
154                 default:
155                         call_dissector(data_handle,next_tvb, pinfo, tree);
156                         break;
157                 }
158         } else
159                 call_dissector(data_handle,next_tvb, pinfo, tree);
160 }
161
162 void
163 proto_register_lapd(void)
164 {
165     static hf_register_info hf[] = {
166         { &hf_lapd_address,
167           { "Address Field", "lapd.address", FT_UINT16, BASE_HEX, NULL, 0x0,
168                 "Address", HFILL }},
169
170         { &hf_lapd_sapi,
171           { "SAPI", "lapd.sapi", FT_UINT16, BASE_DEC, VALS(lapd_sapi_vals), LAPD_SAPI,
172                 "Service Access Point Identifier", HFILL }},
173
174         { &hf_lapd_cr,
175           { "C/R", "lapd.cr", FT_UINT16, BASE_DEC, NULL, LAPD_CR,
176                 "Command/Response bit", HFILL }},
177
178         { &hf_lapd_ea1,
179           { "EA1", "lapd.ea1", FT_UINT16, BASE_DEC, NULL, LAPD_EA1,
180                 "First Address Extension bit", HFILL }},
181
182         { &hf_lapd_tei,
183           { "TEI", "lapd.tei", FT_UINT16, BASE_DEC, NULL, LAPD_TEI,
184                 "Terminal Endpoint Identifier", HFILL }},
185
186         { &hf_lapd_ea2,
187           { "EA2", "lapd.ea2", FT_UINT16, BASE_DEC, NULL, LAPD_EA2,
188                 "Second Address Extension bit", HFILL }},
189
190         { &hf_lapd_control,
191           { "Control Field", "lapd.control", FT_UINT16, BASE_HEX, NULL, 0x0,
192                 "Control field", HFILL }},
193     };
194     static gint *ett[] = {
195         &ett_lapd,
196         &ett_lapd_address,
197         &ett_lapd_control,
198     };
199
200     proto_lapd = proto_register_protocol("Link Access Procedure, Channel D (LAPD)",
201                                          "LAPD", "lapd");
202     proto_register_field_array (proto_lapd, hf, array_length(hf));
203     proto_register_subtree_array(ett, array_length(ett));
204 }
205
206 void
207 proto_reg_handoff_lapd(void)
208 {
209         dissector_handle_t lapd_handle;
210
211         /*
212          * Get handle for the Q.931 dissector.
213          */
214         q931_handle = find_dissector("q931");
215         data_handle = find_dissector("data");
216
217         lapd_handle = create_dissector_handle(dissect_lapd, proto_lapd);
218         dissector_add("wtap_encap", WTAP_ENCAP_LAPD, lapd_handle);
219 }