Small fix for dissect_ndr_nt_UNICODE_STRING_str() in packet-dcerpc-nt.c
[obnox/wireshark/wip.git] / packet-tpkt.c
1 /* packet-tpkt.c
2  *
3  * Routine to check for RFC 1006 TPKT header and to dissect TPKT header
4  * Copyright 2000, Philips Electronics N.V.
5  * Andreas Sikkema <andreas.sikkema@philips.com>
6  *
7  * Routine to dissect RFC 1006 TPKT packet containing OSI TP PDU
8  * Copyright 2001, Martin Thomas <Martin_A_Thomas@yahoo.com>
9  *
10  * $Id: packet-tpkt.c,v 1.17 2002/03/05 22:15:21 guy Exp $
11  *
12  * Ethereal - Network traffic analyzer
13  * By Gerald Combs <gerald@ethereal.com>
14  * Copyright 1998 Gerald Combs
15  * 
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * 
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <glib.h>
36 #include <epan/packet.h>
37
38 #ifdef HAVE_SYS_TYPES_H
39 #  include <sys/types.h>
40 #endif
41
42 #ifdef HAVE_NETINET_IN_H
43 #  include <netinet/in.h>
44 #endif
45
46 #include <stdio.h>
47 #include <string.h>
48
49 #include "packet-tpkt.h"
50 #include "packet-frame.h"
51 #include "prefs.h"
52
53 /* TPKT header fields             */
54 static int proto_tpkt          = -1;
55 static int hf_tpkt_version     = -1;
56 static int hf_tpkt_reserved    = -1;
57 static int hf_tpkt_length      = -1;
58
59 /* TPKT fields defining a sub tree */
60 static gint ett_tpkt           = -1;
61
62 /* desegmentation of OSI over TPKT over TCP */
63 static gboolean tpkt_desegment = TRUE;
64
65 #define TCP_PORT_TPKT   102
66
67 /* find the dissector for OSI TP (aka COTP) */
68 static dissector_handle_t osi_tp_handle; 
69
70 /*
71  * Check whether this could be a TPKT-encapsulated PDU.
72  * Returns -1 if it's not, and the PDU length from the TPKT header
73  * if it is.
74  */
75 int
76 is_tpkt(tvbuff_t *tvb)
77 {
78         /*
79          * If TPKT is disabled, don't dissect it, just return -1, meaning
80          * "this isn't TPKT".
81          */
82         if (!proto_is_protocol_enabled(proto_tpkt))
83                 return -1;
84
85         /* There should at least be 4 bytes left in the frame */
86         if (!tvb_bytes_exist(tvb, 0, 4))
87                 return -1;      /* there aren't */
88
89         /*
90          * The first octet should be 3 and the second one should be 0 
91          * The H.323 implementers guide suggests that this might not 
92          * always be the case....
93          */
94         if (!(tvb_get_guint8(tvb, 0) == 3 && tvb_get_guint8(tvb, 1) == 0))
95                 return -1;      /* They're not */
96
97         /*
98          * Return the length from the TPKT header.
99          */
100         return tvb_get_ntohs(tvb, 2);
101 }
102
103 /*
104  * Dissect TPKT-encapsulated data in a TCP stream.
105  */
106 void
107 dissect_tpkt_encap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
108     gboolean desegment, dissector_handle_t subdissector_handle)
109 {
110         proto_item *ti = NULL;
111         proto_tree *tpkt_tree = NULL;
112         volatile int offset = 0;
113         int length_remaining;
114         int data_len;
115         volatile int length;
116         tvbuff_t *next_tvb;
117         const char *saved_proto;
118
119         while (tvb_reported_length_remaining(tvb, offset) != 0) {
120                 length_remaining = tvb_length_remaining(tvb, offset);
121
122                 /*
123                  * Can we do reassembly?
124                  */
125                 if (desegment && pinfo->can_desegment) {
126                         /*
127                          * Yes - is the TPKT header split across segment
128                          * boundaries?
129                          */
130                         if (length_remaining < 4) {
131                                 /*
132                                  * Yes.  Tell the TCP dissector where
133                                  * the data for this message starts in
134                                  * the data it handed us, and how many
135                                  * more bytes we need, and return.
136                                  */
137                                 pinfo->desegment_offset = offset;
138                                 pinfo->desegment_len = 4 - length_remaining;
139                                 return;
140                         }
141                 }
142
143                 /*
144                  * Get the length from the TPKT header.
145                  */
146                 data_len = tvb_get_ntohs(tvb, offset + 2);
147
148                 /*
149                  * Can we do reassembly?
150                  */
151                 if (desegment && pinfo->can_desegment) {
152                         /*
153                          * Yes - is the payload split across segment
154                          * boundaries?
155                          */
156                         if (length_remaining < data_len) {
157                                 /*
158                                  * Yes.  Tell the TCP dissector where
159                                  * the data for this message starts in
160                                  * the data it handed us, and how many
161                                  * more bytes we need, and return.
162                                  */
163                                 pinfo->desegment_offset = offset;
164                                 pinfo->desegment_len =
165                                     data_len - length_remaining;
166                                 return;
167                         }
168                 }
169
170                 /*
171                  * Dissect the TPKT header.
172                  * Save and restore "pinfo->current_proto".
173                  */
174                 saved_proto = pinfo->current_proto;
175                 pinfo->current_proto = "TPKT";
176
177                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
178                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "TPKT");
179                 if (check_col(pinfo->cinfo, COL_INFO)) {
180                         col_add_fstr(pinfo->cinfo, COL_INFO,
181                             "TPKT Data length = %u", data_len);
182                 }
183
184                 if (tree) {
185                         ti = proto_tree_add_item(tree, proto_tpkt, tvb,
186                             offset, 4, FALSE);
187                         tpkt_tree = proto_item_add_subtree(ti, ett_tpkt);
188
189                         /* Version */
190                         proto_tree_add_item(tpkt_tree, hf_tpkt_version, tvb,
191                             offset, 1, FALSE);
192
193                         /* Reserved octet*/
194                         proto_tree_add_item(tpkt_tree, hf_tpkt_reserved, tvb,
195                             offset + 1, 1, FALSE);
196
197                         /* Length */
198                         proto_tree_add_uint(tpkt_tree, hf_tpkt_length, tvb,
199                             offset + 2, 2, data_len);
200                 }
201                 pinfo->current_proto = saved_proto;
202
203                 /* Skip the TPKT header. */
204                 offset += 4;
205                 data_len -= 4;
206
207                 /*
208                  * Construct a tvbuff containing the amount of the payload
209                  * we have available.  Make its reported length the
210                  * amount of data in this TPKT packet.
211                  *
212                  * XXX - if reassembly isn't enabled. the subdissector
213                  * will throw a BoundsError exception, rather than a
214                  * ReportedBoundsError exception.  We really want
215                  * a tvbuff where the length is "length", the reported
216                  * length is "plen + 2", and the "if the snapshot length
217                  * were infinite" length were the minimum of the
218                  * reported length of the tvbuff handed to us and "plen+2",
219                  * with a new type of exception thrown if the offset is
220                  * within the reported length but beyond that third length,
221                  * with that exception getting the "Unreassembled Packet"
222                  * error.
223                  */
224                 length = length_remaining - 4;
225                 if (length > data_len)
226                         length = data_len;
227                 next_tvb = tvb_new_subset(tvb, offset, length, data_len);
228
229                 /*
230                  * Call the subdissector.
231                  *
232                  * Catch the ReportedBoundsError exception; if this
233                  * particular message happens to get a ReportedBoundsError
234                  * exception, that doesn't mean that we should stop
235                  * dissecting TPKT messages within this frame or chunk
236                  * of reassembled data.
237                  *
238                  * If it gets a BoundsError, we can stop, as there's nothing
239                  * more to see, so we just re-throw it.
240                  */
241                 TRY {
242                         call_dissector(subdissector_handle, next_tvb, pinfo,
243                             tree);
244                 }
245                 CATCH(BoundsError) {
246                         RETHROW;
247                 }
248                 CATCH(ReportedBoundsError) {
249                         show_reported_bounds_error(tvb, pinfo, tree);
250                 }
251                 ENDTRY;
252
253                 /*
254                  * Skip the payload.
255                  */
256                 offset += length;
257         }
258 }
259
260 /*
261  * Dissect RFC 1006 TPKT, which wraps a TPKT header around an OSI TP
262  * PDU.
263  */
264 static void
265 dissect_tpkt(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
266 {
267         dissect_tpkt_encap(tvb, pinfo, tree, tpkt_desegment, osi_tp_handle);
268 }
269
270 void
271 proto_register_tpkt(void)
272 {
273         static hf_register_info hf[] = 
274         {
275                 { 
276                         &hf_tpkt_version,
277                         { 
278                                 "Version", 
279                                 "tpkt.version", 
280                                 FT_UINT8, 
281                                 BASE_DEC, 
282                                 NULL, 
283                                 0x0,
284                                 "", HFILL 
285                         }
286                 },
287                 { 
288                         &hf_tpkt_reserved,
289                         { 
290                                 "Reserved", 
291                                 "tpkt.reserved", 
292                                 FT_UINT8, 
293                                 BASE_DEC, 
294                                 NULL, 
295                                 0x0,
296                                 "", HFILL 
297                         }
298                 },
299                 { 
300                         &hf_tpkt_length,
301                         { 
302                                 "Length", 
303                                 "tpkt.length", 
304                                 FT_UINT16, 
305                                 BASE_DEC, 
306                                 NULL, 
307                                 0x0,
308                                 "", HFILL 
309                         }
310                 },
311         };
312         
313         static gint *ett[] = 
314         {
315                 &ett_tpkt,
316         };
317         module_t *tpkt_module;
318
319         proto_tpkt = proto_register_protocol("TPKT", "TPKT", "tpkt");
320         proto_register_field_array(proto_tpkt, hf, array_length(hf));
321         proto_register_subtree_array(ett, array_length(ett));
322
323         tpkt_module = prefs_register_protocol(proto_tpkt, NULL);
324         prefs_register_bool_preference(tpkt_module, "desegment",
325             "Desegment all TPKT messages spanning multiple TCP segments",
326             "Whether the TPKT dissector should desegment all messages spanning multiple TCP segments",
327             &tpkt_desegment);
328 }
329
330 void
331 proto_reg_handoff_tpkt(void)
332 {
333         dissector_handle_t tpkt_handle;
334
335         osi_tp_handle = find_dissector("ositp");
336         tpkt_handle = create_dissector_handle(dissect_tpkt, proto_tpkt);
337         dissector_add("tcp.port", TCP_PORT_TPKT, tpkt_handle);
338 }