From Kovarththanan Rajaratnam via bug 3548:
[obnox/wireshark/wip.git] / epan / dissectors / packet-sdlc.c
1 /* packet-sdlc.c
2  * Routines for SDLC frame disassembly
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30 #include <glib.h>
31 #include <string.h>
32 #include <epan/packet.h>
33 #include <epan/xdlc.h>
34
35 /*
36  * See
37  *
38  *      http://www.protocols.com/pbook/sna.htm
39  */
40
41 static int proto_sdlc = -1;
42 static int hf_sdlc_address = -1;
43 static int hf_sdlc_control = -1;
44 static int hf_sdlc_n_r = -1;
45 static int hf_sdlc_n_s = -1;
46 static int hf_sdlc_p = -1;
47 static int hf_sdlc_f = -1;
48 static int hf_sdlc_s_ftype = -1;
49 static int hf_sdlc_u_modifier_cmd = -1;
50 static int hf_sdlc_u_modifier_resp = -1;
51 static int hf_sdlc_ftype_i = -1;
52 static int hf_sdlc_ftype_s_u = -1;
53
54 static gint ett_sdlc = -1;
55 static gint ett_sdlc_control = -1;
56
57 static dissector_handle_t sna_handle;
58 static dissector_handle_t data_handle;
59
60 static const xdlc_cf_items sdlc_cf_items = {
61         &hf_sdlc_n_r,
62         &hf_sdlc_n_s,
63         &hf_sdlc_p,
64         &hf_sdlc_f,
65         &hf_sdlc_s_ftype,
66         &hf_sdlc_u_modifier_cmd,
67         &hf_sdlc_u_modifier_resp,
68         &hf_sdlc_ftype_i,
69         &hf_sdlc_ftype_s_u
70 };
71
72 static void
73 dissect_sdlc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
74 {
75         proto_tree      *sdlc_tree;
76         proto_item      *sdlc_ti;
77         guint8          address;
78         guint16         control;
79         int             sdlc_header_len;
80         gboolean        is_response;
81         tvbuff_t        *next_tvb;
82
83         if (check_col(pinfo->cinfo, COL_PROTOCOL))
84                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SDLC");
85         if (check_col(pinfo->cinfo, COL_INFO))
86                 col_clear(pinfo->cinfo, COL_INFO);
87
88         address = tvb_get_guint8(tvb, 0);
89         sdlc_header_len = 1;    /* address */
90
91         /*
92          * XXX - is there something in the SDLC header that indicates
93          * how to interpret "command vs. response" based on the
94          * direction?
95          */
96         if (pinfo->p2p_dir == P2P_DIR_SENT) {
97                 is_response = FALSE;
98                 if (check_col(pinfo->cinfo, COL_RES_DL_DST))
99                         col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DCE");
100                 if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
101                         col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DTE");
102         }
103         else {
104                 /* XXX - what if the direction is unknown? */
105                 is_response = TRUE;
106                 if (check_col(pinfo->cinfo, COL_RES_DL_DST))
107                     col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DTE");
108                 if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
109                     col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DCE");
110         }
111
112         if (tree) {
113                 sdlc_ti = proto_tree_add_item(tree, proto_sdlc, tvb, 0, -1,
114                     FALSE);
115                 sdlc_tree = proto_item_add_subtree(sdlc_ti, ett_sdlc);
116
117                 proto_tree_add_uint(sdlc_tree, hf_sdlc_address, tvb, 0, 1,
118                     address);
119         } else {
120                 sdlc_ti = NULL;
121                 sdlc_tree = NULL;
122         }
123
124         /*
125          * XXX - SDLC has a mod-128 mode as well as a mod-7 mode.
126          * We can infer the mode from an SNRM/SRME frame, but if
127          * we don't see one of them, we may have to have a preference
128          * to control what to use.
129          */
130         control = dissect_xdlc_control(tvb, 1, pinfo, sdlc_tree, hf_sdlc_control,
131             ett_sdlc_control, &sdlc_cf_items, NULL, NULL, NULL,
132             is_response, FALSE, FALSE);
133         sdlc_header_len += XDLC_CONTROL_LEN(control, FALSE);
134
135         if (tree)
136                 proto_item_set_len(sdlc_ti, sdlc_header_len);
137
138         /*
139          * XXX - is there an FCS at the end, at least in Sniffer
140          * captures?  (There doesn't appear to be.)
141          */
142         next_tvb = tvb_new_subset(tvb, sdlc_header_len, -1, -1);
143         if (XDLC_IS_INFORMATION(control)) {
144                 /* call the SNA dissector */
145                 call_dissector(sna_handle, next_tvb, pinfo, tree);
146         } else
147                 call_dissector(data_handle, next_tvb, pinfo, tree);
148 }
149
150 void
151 proto_register_sdlc(void)
152 {
153         static hf_register_info hf[] = {
154                 { &hf_sdlc_address,
155                   { "Address Field", "sdlc.address", FT_UINT8, BASE_HEX,
156                      NULL, 0x0, "Address", HFILL }},
157
158                 { &hf_sdlc_control,
159                   { "Control Field", "sdlc.control", FT_UINT16, BASE_HEX,
160                     NULL, 0x0, "Control field", HFILL }},
161
162                 { &hf_sdlc_n_r,
163                     { "N(R)", "sdlc.control.n_r", FT_UINT8, BASE_DEC,
164                       NULL, XDLC_N_R_MASK, NULL, HFILL }},
165
166                 { &hf_sdlc_n_s,
167                     { "N(S)", "sdlc.control.n_s", FT_UINT8, BASE_DEC,
168                       NULL, XDLC_N_S_MASK, NULL, HFILL }},
169
170                 { &hf_sdlc_p,
171                     { "Poll", "sdlc.control.p", FT_BOOLEAN, 8,
172                       TFS(&flags_set_truth), XDLC_P_F, NULL, HFILL }},
173
174                 { &hf_sdlc_f,
175                     { "Final", "sdlc.control.f", FT_BOOLEAN, 8,
176                       TFS(&flags_set_truth), XDLC_P_F, NULL, HFILL }},
177
178                 { &hf_sdlc_s_ftype,
179                     { "Supervisory frame type", "sdlc.control.s_ftype", FT_UINT8, BASE_HEX,
180                       VALS(stype_vals), XDLC_S_FTYPE_MASK, NULL, HFILL }},
181
182                 { &hf_sdlc_u_modifier_cmd,
183                     { "Command", "sdlc.control.u_modifier_cmd", FT_UINT8, BASE_HEX,
184                       VALS(modifier_vals_cmd), XDLC_U_MODIFIER_MASK, NULL, HFILL }},
185
186                 { &hf_sdlc_u_modifier_resp,
187                     { "Response", "sdlc.control.u_modifier_resp", FT_UINT8, BASE_HEX,
188                       VALS(modifier_vals_resp), XDLC_U_MODIFIER_MASK, NULL, HFILL }},
189
190                 { &hf_sdlc_ftype_i,
191                     { "Frame type", "sdlc.control.ftype", FT_UINT8, BASE_HEX,
192                       VALS(ftype_vals), XDLC_I_MASK, NULL, HFILL }},
193
194                 { &hf_sdlc_ftype_s_u,
195                     { "Frame type", "sdlc.control.ftype", FT_UINT8, BASE_HEX,
196                       VALS(ftype_vals), XDLC_S_U_MASK, NULL, HFILL }},
197         };
198         static gint *ett[] = {
199                 &ett_sdlc,
200                 &ett_sdlc_control,
201         };
202
203         proto_sdlc = proto_register_protocol(
204             "Synchronous Data Link Control (SDLC)", "SDLC", "sdlc");
205         proto_register_field_array(proto_sdlc, hf, array_length(hf));
206         proto_register_subtree_array(ett, array_length(ett));
207 }
208
209 void
210 proto_reg_handoff_sdlc(void)
211 {
212         dissector_handle_t sdlc_handle;
213
214         /*
215          * Get handle for the SNA dissector.
216          */
217         sna_handle = find_dissector("sna");
218         data_handle = find_dissector("data");
219
220         sdlc_handle = create_dissector_handle(dissect_sdlc, proto_sdlc);
221         dissector_add("wtap_encap", WTAP_ENCAP_SDLC, sdlc_handle);
222 }