Give the code that computes protocol statistics a progress dialog box,
[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.20 2001/01/22 00:20:29 guy 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-q931.h"
40 #include "xdlc.h"
41
42 /* ISDN/LAPD references:
43  *
44  * http://www.cisco.com/univercd/cc/td/doc/cisintwk/ito_doc/isdn.htm
45  * http://www.ece.wpi.edu/courses/ee535/hwk11cd95/agrebe/agrebe.html
46  * http://www.acacia-net.com/Clarinet/Protocol/q9213o84.htm
47  */
48
49 static int proto_lapd = -1;
50 static int hf_lapd_address = -1;
51 static int hf_lapd_sapi = -1;
52 static int hf_lapd_cr = -1;
53 static int hf_lapd_ea1 = -1;
54 static int hf_lapd_tei = -1;
55 static int hf_lapd_ea2 = -1;
56 static int hf_lapd_control = -1;
57
58 static gint ett_lapd = -1;
59 static gint ett_lapd_address = -1;
60 static gint ett_lapd_control = -1;
61
62 /*
63  * Bits in the address field.
64  */
65 #define LAPD_SAPI       0xfc00  /* Service Access Point Identifier */
66 #define LAPD_SAPI_SHIFT 10
67 #define LAPD_CR         0x0200  /* Command/Response bit */
68 #define LAPD_EA1        0x0100  /* First Address Extension bit */
69 #define LAPD_TEI        0x00fe  /* Terminal Endpoint Identifier */
70 #define LAPD_EA2        0x0001  /* Second Address Extension bit */
71
72 #define LAPD_SAPI_Q931          0       /* Q.931 call control procedure */
73 #define LAPD_SAPI_PM_Q931       1       /* Packet mode Q.931 call control procedure */
74 #define LAPD_SAPI_X25           16      /* X.25 Level 3 procedures */
75 #define LAPD_SAPI_L2            63      /* Layer 2 management procedures */
76
77 static const value_string lapd_sapi_vals[] = {
78         { LAPD_SAPI_Q931,       "Q.931 Call control procedure" },
79         { LAPD_SAPI_PM_Q931,    "Packet mode Q.931 Call control procedure" },
80         { LAPD_SAPI_X25,        "X.25 Level 3 procedures" },
81         { LAPD_SAPI_L2,         "Layer 2 management procedures" },
82         { 0,                    NULL }
83 };
84
85 static void
86 dissect_lapd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
87 {
88         proto_tree      *lapd_tree, *addr_tree;
89         proto_item      *ti;
90         guint16         control;
91         int             lapd_header_len;
92         guint16         address, cr, sapi;
93         gboolean        is_response;
94         tvbuff_t        *next_tvb;
95
96         if (check_col(pinfo->fd, COL_PROTOCOL))
97                 col_set_str(pinfo->fd, COL_PROTOCOL, "LAPD");
98         if (check_col(pinfo->fd, COL_INFO))
99                 col_clear(pinfo->fd, COL_INFO);
100
101         address = tvb_get_ntohs(tvb, 0);
102         cr = address & LAPD_CR;
103         sapi = (address & LAPD_SAPI) >> LAPD_SAPI_SHIFT;
104         lapd_header_len = 2;    /* address */
105
106         if (pinfo->pseudo_header->p2p.sent) {
107                 is_response = cr ? TRUE : FALSE;
108                 if(check_col(pinfo->fd, COL_RES_DL_DST))
109                         col_set_str(pinfo->fd, COL_RES_DL_DST, "Network");
110                 if(check_col(pinfo->fd, COL_RES_DL_SRC))
111                         col_set_str(pinfo->fd, COL_RES_DL_SRC, "User");
112         }
113         else {
114                 is_response = cr ? FALSE : TRUE;
115                 if(check_col(pinfo->fd, COL_RES_DL_DST))
116                     col_set_str(pinfo->fd, COL_RES_DL_DST, "User");
117                 if(check_col(pinfo->fd, COL_RES_DL_SRC))
118                     col_set_str(pinfo->fd, COL_RES_DL_SRC, "Network");
119         }
120
121         if (tree) {
122                 ti = proto_tree_add_item(tree, proto_lapd, tvb, 0, 3, FALSE);
123                 lapd_tree = proto_item_add_subtree(ti, ett_lapd);
124
125                 ti = proto_tree_add_uint(lapd_tree, hf_lapd_address, tvb, 0, 2, address);
126                 addr_tree = proto_item_add_subtree(ti, ett_lapd_address);
127
128                 proto_tree_add_uint(addr_tree, hf_lapd_sapi,tvb, 0, 1, address);
129                 proto_tree_add_uint(addr_tree, hf_lapd_cr,  tvb, 0, 1, address);
130                 proto_tree_add_uint(addr_tree, hf_lapd_ea1, tvb, 0, 1, address);
131                 proto_tree_add_uint(addr_tree, hf_lapd_tei, tvb, 1, 1, address);
132                 proto_tree_add_uint(addr_tree, hf_lapd_ea2, tvb, 1, 1, address);
133         }
134         else {
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         next_tvb = tvb_new_subset(tvb, lapd_header_len, -1, -1);
143         if (XDLC_IS_INFORMATION(control)) {
144                 /* call next protocol */
145                 switch (sapi) {
146
147                 case LAPD_SAPI_Q931:
148                         dissect_q931(next_tvb, pinfo, tree);
149                         break;
150
151                 default:
152                         dissect_data(next_tvb, 0, pinfo, tree);
153                         break;
154                 }
155         } else
156                 dissect_data(next_tvb, 0, pinfo, tree);
157 }
158
159 void
160 proto_register_lapd(void)
161 {
162     static hf_register_info hf[] = {
163         { &hf_lapd_address,
164           { "Address Field", "lapd.address", FT_UINT16, BASE_HEX, NULL, 0x0, 
165                 "Address" }},
166
167         { &hf_lapd_sapi,
168           { "SAPI", "lapd.sapi", FT_UINT16, BASE_DEC, VALS(lapd_sapi_vals), LAPD_SAPI,
169                 "Service Access Point Identifier" }},
170
171         { &hf_lapd_cr,
172           { "C/R", "lapd.cr", FT_UINT16, BASE_DEC, NULL, LAPD_CR,
173                 "Command/Response bit" }},
174
175         { &hf_lapd_ea1,
176           { "EA1", "lapd.ea1", FT_UINT16, BASE_DEC, NULL, LAPD_EA1,
177                 "First Address Extension bit" }},
178
179         { &hf_lapd_tei,
180           { "TEI", "lapd.tei", FT_UINT16, BASE_DEC, NULL, LAPD_TEI,
181                 "Terminal Endpoint Identifier" }},
182
183         { &hf_lapd_ea2,
184           { "EA2", "lapd.ea2", FT_UINT16, BASE_DEC, NULL, LAPD_EA2,
185                 "Second Address Extension bit" }},
186
187         { &hf_lapd_control,
188           { "Control Field", "lapd.control", FT_UINT16, BASE_HEX, NULL, 0x0,
189                 "Control field" }},
190     };
191     static gint *ett[] = {
192         &ett_lapd,
193         &ett_lapd_address,
194         &ett_lapd_control,
195     };
196
197     proto_lapd = proto_register_protocol("Link Access Procedure, Channel D (LAPD)",
198                                          "LAPD", "lapd");
199     proto_register_field_array (proto_lapd, hf, array_length(hf));
200     proto_register_subtree_array(ett, array_length(ett));
201 }
202
203 void
204 proto_reg_handoff_lapd(void)
205 {
206         dissector_add("wtap_encap", WTAP_ENCAP_LAPD, dissect_lapd, proto_lapd);
207 }