added some options and enhancements to the print output:
[obnox/wireshark/wip.git] / packet-frame.c
1 /* packet-frame.c
2  *
3  * Top-most dissector. Decides dissector based on Wiretap Encapsulation Type.
4  *
5  * $Id: packet-frame.c,v 1.43 2004/01/03 18:40:07 sharpe Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 2000 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/timestamp.h>
33 #include <epan/tvbuff.h>
34 #include "packet-frame.h"
35 #include "prefs.h"
36 #include "tap.h"
37
38 int proto_frame = -1;
39 int hf_frame_arrival_time = -1;
40 static int hf_frame_time_delta = -1;
41 static int hf_frame_time_relative = -1;
42 int hf_frame_number = -1;
43 int hf_frame_packet_len = -1;
44 int hf_frame_capture_len = -1;
45 static int hf_frame_p2p_dir = -1;
46 static int hf_frame_file_off = -1;
47 static int hf_frame_marked = -1;
48 static int hf_frame_ref_time = -1;
49
50 static int proto_short = -1;
51 int proto_malformed = -1;
52 static int proto_unreassembled = -1;
53
54 static gint ett_frame = -1;
55
56 static int frame_tap = -1;
57
58 static dissector_handle_t data_handle;
59 static dissector_handle_t docsis_handle;
60
61 /* Preferences */
62 static gboolean show_file_off = FALSE;
63 static gboolean force_docsis_encap;
64
65 static const value_string p2p_dirs[] = {
66         { P2P_DIR_SENT, "Sent" },
67         { P2P_DIR_RECV, "Received" },
68         { 0, NULL }
69 };
70
71 static dissector_table_t wtap_encap_dissector_table;
72
73 static void
74 dissect_frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
75 {
76         proto_tree      *fh_tree;
77         proto_item      *ti;
78         nstime_t        ts;
79         int             cap_len, pkt_len;
80
81         pinfo->current_proto = "Frame";
82
83         if (pinfo->pseudo_header != NULL) {
84                 switch (pinfo->fd->lnk_t) {
85
86                 case WTAP_ENCAP_WFLEET_HDLC:
87                 case WTAP_ENCAP_CHDLC_WITH_PHDR:
88                 case WTAP_ENCAP_PPP_WITH_PHDR:
89                 case WTAP_ENCAP_SDLC:
90                         pinfo->p2p_dir = pinfo->pseudo_header->p2p.sent ?
91                             P2P_DIR_SENT : P2P_DIR_RECV;
92                         break;
93
94                 case WTAP_ENCAP_LAPB:
95                 case WTAP_ENCAP_FRELAY_WITH_PHDR:
96                         pinfo->p2p_dir =
97                             (pinfo->pseudo_header->x25.flags & FROM_DCE) ?
98                             P2P_DIR_RECV : P2P_DIR_SENT;
99                         break;
100
101                 case WTAP_ENCAP_ISDN:
102                         pinfo->p2p_dir = pinfo->pseudo_header->isdn.uton ?
103                             P2P_DIR_SENT : P2P_DIR_RECV;
104                         break;
105                 }
106         }
107
108         if ((force_docsis_encap) && (docsis_handle)) {
109                 /*
110                  * XXX - setting it here makes it impossible to
111                  * turn the "Treat all frames as DOCSIS frames"
112                  * option off.
113                  *
114                  * The TCP Graph code currently uses "fd->lnk_t";
115                  * it should eventually just get the information
116                  * it needs from a full-blown dissection, so that
117                  * can handle any link-layer type.
118                  */
119                 pinfo->fd->lnk_t = WTAP_ENCAP_DOCSIS;
120         }
121
122         /* Put in frame header information. */
123         if (tree) {
124
125           cap_len = tvb_length(tvb);
126           pkt_len = tvb_reported_length(tvb);
127
128           ti = proto_tree_add_protocol_format(tree, proto_frame, tvb, 0, -1,
129             "Frame %u (%u bytes on wire, %u bytes captured)", pinfo->fd->num, pkt_len, cap_len);
130
131           fh_tree = proto_item_add_subtree(ti, ett_frame);
132
133           proto_tree_add_boolean_hidden(fh_tree, hf_frame_marked, tvb, 0, 0,pinfo->fd->flags.marked);
134
135           if(pinfo->fd->flags.ref_time){
136                 proto_tree_add_item(fh_tree, hf_frame_ref_time, tvb, 0, 0, FALSE);
137           }
138
139           ts.secs = pinfo->fd->abs_secs;
140           ts.nsecs = pinfo->fd->abs_usecs*1000;
141
142           proto_tree_add_time(fh_tree, hf_frame_arrival_time, tvb,
143                 0, 0, &ts);
144
145           ts.secs = pinfo->fd->del_secs;
146           ts.nsecs = pinfo->fd->del_usecs*1000;
147
148           proto_tree_add_time(fh_tree, hf_frame_time_delta, tvb,
149                 0, 0, &ts);
150
151           ts.secs = pinfo->fd->rel_secs;
152           ts.nsecs = pinfo->fd->rel_usecs*1000;
153
154           proto_tree_add_time(fh_tree, hf_frame_time_relative, tvb,
155                 0, 0, &ts);
156
157           proto_tree_add_uint(fh_tree, hf_frame_number, tvb,
158                 0, 0, pinfo->fd->num);
159
160           proto_tree_add_uint_format(fh_tree, hf_frame_packet_len, tvb,
161                 0, 0, pkt_len, "Packet Length: %d byte%s", pkt_len,
162                 plurality(pkt_len, "", "s"));
163
164           proto_tree_add_uint_format(fh_tree, hf_frame_capture_len, tvb,
165                 0, 0, cap_len, "Capture Length: %d byte%s", cap_len,
166                 plurality(cap_len, "", "s"));
167
168           /* Check for existences of P2P pseudo header */
169           if (pinfo->p2p_dir != P2P_DIR_UNKNOWN) {
170                   proto_tree_add_uint(fh_tree, hf_frame_p2p_dir, tvb,
171                                   0, 0, pinfo->p2p_dir);
172           }
173
174           if (show_file_off) {
175                   proto_tree_add_int_format(fh_tree, hf_frame_file_off, tvb,
176                                   0, 0, pinfo->fd->file_off,
177                                   "File Offset: %ld (0x%lx)",
178                                   pinfo->fd->file_off, pinfo->fd->file_off);
179           }
180         }
181
182
183         TRY {
184                 if (!dissector_try_port(wtap_encap_dissector_table, pinfo->fd->lnk_t,
185                                         tvb, pinfo, tree)) {
186
187                         if (check_col(pinfo->cinfo, COL_PROTOCOL))
188                                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "UNKNOWN");
189                         if (check_col(pinfo->cinfo, COL_INFO))
190                                 col_add_fstr(pinfo->cinfo, COL_INFO, "WTAP_ENCAP = %u",
191                                     pinfo->fd->lnk_t);
192                         call_dissector(data_handle,tvb, pinfo, tree);
193                 }
194         }
195         CATCH_ALL {
196                 show_exception(tvb, pinfo, tree, EXCEPT_CODE);
197         }
198         ENDTRY;
199
200         tap_queue_packet(frame_tap, pinfo, NULL);
201 }
202
203 void
204 show_exception(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
205     unsigned long exception)
206 {
207         switch (exception) {
208
209         case BoundsError:
210                 if (check_col(pinfo->cinfo, COL_INFO))
211                         col_append_str(pinfo->cinfo, COL_INFO, "[Short Frame]");
212                 proto_tree_add_protocol_format(tree, proto_short, tvb, 0, 0,
213                                 "[Short Frame: %s]", pinfo->current_proto);
214                 break;
215
216         case ReportedBoundsError:
217                 show_reported_bounds_error(tvb, pinfo, tree);
218                 break;
219         }
220 }
221
222 void
223 show_reported_bounds_error(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
224 {
225         if (pinfo->fragmented) {
226                 /*
227                  * We were dissecting an unreassembled fragmented
228                  * packet when the exception was thrown, so the
229                  * problem isn't that the dissector expected
230                  * something but it wasn't in the packet, the
231                  * problem is that the dissector expected something
232                  * but it wasn't in the fragment we dissected.
233                  */
234                 if (check_col(pinfo->cinfo, COL_INFO))
235                         col_append_fstr(pinfo->cinfo, COL_INFO,
236                             "[Unreassembled Packet%s]",
237                             pinfo->noreassembly_reason);
238                 proto_tree_add_protocol_format(tree, proto_unreassembled,
239                     tvb, 0, 0, "[Unreassembled Packet%s: %s]",
240                     pinfo->noreassembly_reason, pinfo->current_proto);
241         } else {
242                 if (check_col(pinfo->cinfo, COL_INFO))
243                         col_append_str(pinfo->cinfo, COL_INFO,
244                             "[Malformed Packet]");
245                 proto_tree_add_protocol_format(tree, proto_malformed,
246                     tvb, 0, 0, "[Malformed Packet: %s]", pinfo->current_proto);
247         }
248 }
249
250 void
251 proto_register_frame(void)
252 {
253         static hf_register_info hf[] = {
254                 { &hf_frame_arrival_time,
255                 { "Arrival Time",               "frame.time", FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
256                         "Absolute time when this frame was captured", HFILL }},
257
258                 { &hf_frame_time_delta,
259                 { "Time delta from previous packet",    "frame.time_delta", FT_RELATIVE_TIME, BASE_NONE, NULL,
260                         0x0,
261                         "Time delta since previous diplayed frame", HFILL }},
262
263                 { &hf_frame_time_relative,
264                 { "Time since reference or first frame",        "frame.time_relative", FT_RELATIVE_TIME, BASE_NONE, NULL,
265                         0x0,
266                         "Time relative reference or first frame", HFILL }},
267
268                 { &hf_frame_number,
269                 { "Frame Number",               "frame.number", FT_UINT32, BASE_DEC, NULL, 0x0,
270                         "", HFILL }},
271
272                 { &hf_frame_packet_len,
273                 { "Total Frame Length",         "frame.pkt_len", FT_UINT32, BASE_DEC, NULL, 0x0,
274                         "", HFILL }},
275
276                 { &hf_frame_capture_len,
277                 { "Capture Frame Length",       "frame.cap_len", FT_UINT32, BASE_DEC, NULL, 0x0,
278                         "", HFILL }},
279
280                 { &hf_frame_p2p_dir,
281                 { "Point-to-Point Direction",   "frame.p2p_dir", FT_UINT8, BASE_DEC, VALS(p2p_dirs), 0x0,
282                         "", HFILL }},
283
284                 { &hf_frame_file_off,
285                 { "File Offset",        "frame.file_off", FT_INT32, BASE_DEC, NULL, 0x0,
286                         "", HFILL }},
287
288                 { &hf_frame_marked,
289                 { "Frame is marked",    "frame.marked", FT_BOOLEAN, 8, NULL, 0x0,
290                         "Frame is marked in the GUI", HFILL }},
291
292                 { &hf_frame_ref_time,
293                 { "This is a Ref Time frame",   "frame.ref_time", FT_NONE, 0, NULL, 0x0,
294                         "This frame is a Reference Time frame", HFILL }},
295         };
296         static gint *ett[] = {
297                 &ett_frame,
298         };
299         module_t *frame_module;
300
301         wtap_encap_dissector_table = register_dissector_table("wtap_encap",
302             "Wiretap encapsulation type", FT_UINT32, BASE_DEC);
303
304         proto_frame = proto_register_protocol("Frame", "Frame", "frame");
305         proto_register_field_array(proto_frame, hf, array_length(hf));
306         proto_register_subtree_array(ett, array_length(ett));
307         register_dissector("frame",dissect_frame,proto_frame);
308
309         /* You can't disable dissection of "Frame", as that would be
310            tantamount to not doing any dissection whatsoever. */
311         proto_set_cant_toggle(proto_frame);
312
313         proto_short = proto_register_protocol("Short Frame", "Short frame", "short");
314         proto_malformed = proto_register_protocol("Malformed Packet",
315             "Malformed packet", "malformed");
316         proto_unreassembled = proto_register_protocol(
317             "Unreassembled Fragmented Packet",
318             "Unreassembled fragmented packet", "unreassembled");
319
320         /* "Short Frame", "Malformed Packet", and "Unreassembled Fragmented
321            Packet" aren't really protocols, they're error indications;
322            disabling them makes no sense. */
323         proto_set_cant_toggle(proto_short);
324         proto_set_cant_toggle(proto_malformed);
325         proto_set_cant_toggle(proto_unreassembled);
326
327         /* Our preferences */
328         frame_module = prefs_register_protocol(proto_frame, NULL);
329         prefs_register_bool_preference(frame_module, "show_file_off",
330             "Show File Offset", "Show File Offset", &show_file_off);
331         prefs_register_bool_preference(frame_module, "force_docsis_encap",
332             "Treat all frames as DOCSIS frames", "Treat all frames as DOCSIS Frames", &force_docsis_encap);
333
334         frame_tap=register_tap("frame");
335 }
336
337 void
338 proto_reg_handoff_frame(void)
339 {
340         data_handle = find_dissector("data");
341         docsis_handle = find_dissector("docsis");
342 }