Put "WTP" into the Info column for segmented invoke/result PDUs on which
[obnox/wireshark/wip.git] / packet-sdlc.c
1 /* packet-sdlc.c
2  * Routines for SDLC frame disassembly
3  *
4  * $Id: packet-sdlc.c,v 1.1 2003/01/03 22:31:24 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 "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
45 static gint ett_sdlc = -1;
46 static gint ett_sdlc_control = -1;
47
48 static dissector_handle_t sna_handle;
49 static dissector_handle_t data_handle;
50
51 static void
52 dissect_sdlc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
53 {
54         proto_tree      *sdlc_tree;
55         proto_item      *sdlc_ti;
56         guint8          address;
57         guint16         control;
58         int             sdlc_header_len;
59         gboolean        is_response;
60         tvbuff_t        *next_tvb;
61
62         if (check_col(pinfo->cinfo, COL_PROTOCOL))
63                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "SDLC");
64         if (check_col(pinfo->cinfo, COL_INFO))
65                 col_clear(pinfo->cinfo, COL_INFO);
66
67         address = tvb_get_guint8(tvb, 0);
68         sdlc_header_len = 1;    /* address */
69
70         /*
71          * XXX - is there something in the SDLC header that indicates
72          * how to interpret "command vs. response" based on the
73          * direction?
74          */
75         if (pinfo->p2p_dir == P2P_DIR_SENT) {
76                 is_response = FALSE;
77                 if (check_col(pinfo->cinfo, COL_RES_DL_DST))
78                         col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DCE");
79                 if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
80                         col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DTE");
81         }
82         else {
83                 /* XXX - what if the direction is unknown? */
84                 is_response = TRUE;
85                 if (check_col(pinfo->cinfo, COL_RES_DL_DST))
86                     col_set_str(pinfo->cinfo, COL_RES_DL_DST, "DTE");
87                 if (check_col(pinfo->cinfo, COL_RES_DL_SRC))
88                     col_set_str(pinfo->cinfo, COL_RES_DL_SRC, "DCE");
89         }
90
91         if (tree) {
92                 sdlc_ti = proto_tree_add_item(tree, proto_sdlc, tvb, 0, -1,
93                     FALSE);
94                 sdlc_tree = proto_item_add_subtree(sdlc_ti, ett_sdlc);
95
96                 proto_tree_add_uint(sdlc_tree, hf_sdlc_address, tvb, 0, 1,
97                     address);
98         } else {
99                 sdlc_ti = NULL;
100                 sdlc_tree = NULL;
101         }
102
103         /*
104          * XXX - SDLC has a mod-128 mode as well as a mod-7 mode.
105          * We can infer the mode from an SNRM/SRME frame, but if
106          * we don't see one of them, we may have to have a preference
107          * to control what to use.
108          */
109         control = dissect_xdlc_control(tvb, 1, pinfo, sdlc_tree, hf_sdlc_control,
110             ett_sdlc_control, is_response, FALSE);
111         sdlc_header_len += XDLC_CONTROL_LEN(control, FALSE);
112
113         if (tree)
114                 proto_item_set_len(sdlc_ti, sdlc_header_len);
115
116         /*
117          * XXX - is there an FCS at the end, at least in Sniffer
118          * captures?  (There doesn't appear to be.)
119          */
120         next_tvb = tvb_new_subset(tvb, sdlc_header_len, -1, -1);
121         if (XDLC_IS_INFORMATION(control)) {
122                 /* call the SNA dissector */
123                 call_dissector(sna_handle, next_tvb, pinfo, tree);
124         } else
125                 call_dissector(data_handle, next_tvb, pinfo, tree);
126 }
127
128 void
129 proto_register_sdlc(void)
130 {
131         static hf_register_info hf[] = {
132                 { &hf_sdlc_address,
133                   { "Address Field", "sdlc.address", FT_UINT8, BASE_HEX,
134                      NULL, 0x0, "Address", HFILL }},
135
136                 { &hf_sdlc_control,
137                   { "Control Field", "sdlc.control", FT_UINT16, BASE_HEX,
138                     NULL, 0x0, "Control field", HFILL }},
139         };
140         static gint *ett[] = {
141                 &ett_sdlc,
142                 &ett_sdlc_control,
143         };
144
145         proto_sdlc = proto_register_protocol(
146             "Synchronous Data Link Control (SDLC)", "SDLC", "sdlc");
147         proto_register_field_array(proto_sdlc, hf, array_length(hf));
148         proto_register_subtree_array(ett, array_length(ett));
149 }
150
151 void
152 proto_reg_handoff_sdlc(void)
153 {
154         dissector_handle_t sdlc_handle;
155
156         /*
157          * Get handle for the SNA dissector.
158          */
159         sna_handle = find_dissector("sna");
160         data_handle = find_dissector("data");
161
162         sdlc_handle = create_dissector_handle(dissect_sdlc, proto_sdlc);
163         dissector_add("wtap_encap", WTAP_ENCAP_SDLC, sdlc_handle);
164 }