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