Add compat_macros.h to the tarball
[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.32 2002/10/31 07:12:23 guy 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 static int proto_frame = -1;
39 static int hf_frame_arrival_time = -1;
40 static int hf_frame_time_delta = -1;
41 static int hf_frame_time_relative = -1;
42 static int hf_frame_number = -1;
43 static int hf_frame_packet_len = -1;
44 static 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
49 static int proto_short = -1;
50 int proto_malformed = -1;
51 static int proto_unreassembled = -1;
52
53 static gint ett_frame = -1;
54
55 static int frame_tap = -1;
56
57 static dissector_handle_t data_handle;
58 static dissector_handle_t docsis_handle;
59
60 /* Preferences */
61 static gboolean show_file_off = FALSE;
62 static gboolean force_docsis_encap;
63
64 static const value_string p2p_dirs[] = {
65         { P2P_DIR_SENT, "Sent" },
66         { P2P_DIR_RECV, "Received" },
67         { 0, NULL }
68 };
69
70 static dissector_table_t wtap_encap_dissector_table;
71
72 static void
73 dissect_frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
74 {
75         proto_tree      *fh_tree;
76         proto_item      *ti;
77         nstime_t        ts;
78         int             cap_len, pkt_len;
79
80         pinfo->current_proto = "Frame";
81
82         if (pinfo->pseudo_header != NULL) {
83                 switch (pinfo->fd->lnk_t) {
84
85                 case WTAP_ENCAP_CHDLC:
86                 case WTAP_ENCAP_PPP_WITH_PHDR:
87                         pinfo->p2p_dir = pinfo->pseudo_header->p2p.sent ?
88                             P2P_DIR_SENT : P2P_DIR_RECV;
89                         break;
90
91                 case WTAP_ENCAP_LAPB:
92                 case WTAP_ENCAP_FRELAY:
93                         pinfo->p2p_dir =
94                             (pinfo->pseudo_header->x25.flags & FROM_DCE) ?
95                             P2P_DIR_RECV : P2P_DIR_SENT;
96                         break;
97
98                 case WTAP_ENCAP_ISDN:
99                         pinfo->p2p_dir = pinfo->pseudo_header->isdn.uton ?
100                             P2P_DIR_SENT : P2P_DIR_RECV;
101                         break;
102                 }
103         }
104
105         if ((force_docsis_encap) && (docsis_handle)) {
106                 /*
107                  * XXX - setting it here makes it impossible to
108                  * turn the "Treat all frames as DOCSIS frames"
109                  * option off.
110                  *
111                  * The TCP Graph code currently uses "fd->lnk_t";
112                  * it should eventually just get the information
113                  * it needs from a full-blown dissection, so that
114                  * can handle any link-layer type.
115                  */
116                 pinfo->fd->lnk_t = WTAP_ENCAP_DOCSIS;
117         }
118
119         /* Put in frame header information. */
120         if (tree) {
121
122           cap_len = tvb_length(tvb);
123           pkt_len = tvb_reported_length(tvb);
124
125           ti = proto_tree_add_protocol_format(tree, proto_frame, tvb, 0, -1,
126             "Frame %u (%u bytes on wire, %u bytes captured)", pinfo->fd->num, pkt_len, cap_len);
127
128           fh_tree = proto_item_add_subtree(ti, ett_frame);
129
130           proto_tree_add_boolean_hidden(fh_tree, hf_frame_marked, tvb, 0, 0,pinfo->fd->flags.marked);
131
132           ts.secs = pinfo->fd->abs_secs;
133           ts.nsecs = pinfo->fd->abs_usecs*1000;
134
135           proto_tree_add_time(fh_tree, hf_frame_arrival_time, tvb,
136                 0, 0, &ts);
137
138           ts.secs = pinfo->fd->del_secs;
139           ts.nsecs = pinfo->fd->del_usecs*1000;
140
141           proto_tree_add_time(fh_tree, hf_frame_time_delta, tvb,
142                 0, 0, &ts);
143
144           ts.secs = pinfo->fd->rel_secs;
145           ts.nsecs = pinfo->fd->rel_usecs*1000;
146
147           proto_tree_add_time(fh_tree, hf_frame_time_relative, tvb,
148                 0, 0, &ts);
149
150           proto_tree_add_uint(fh_tree, hf_frame_number, tvb,
151                 0, 0, pinfo->fd->num);
152
153           proto_tree_add_uint_format(fh_tree, hf_frame_packet_len, tvb,
154                 0, 0, pkt_len, "Packet Length: %d byte%s", pkt_len,
155                 plurality(pkt_len, "", "s"));
156
157           proto_tree_add_uint_format(fh_tree, hf_frame_capture_len, tvb,
158                 0, 0, cap_len, "Capture Length: %d byte%s", cap_len,
159                 plurality(cap_len, "", "s"));
160
161           /* Check for existences of P2P pseudo header */
162           if (pinfo->p2p_dir != P2P_DIR_UNKNOWN) {
163                   proto_tree_add_uint(fh_tree, hf_frame_p2p_dir, tvb,
164                                   0, 0, pinfo->p2p_dir);
165           }
166
167           if (show_file_off) {
168                   proto_tree_add_int_format(fh_tree, hf_frame_file_off, tvb,
169                                   0, 0, pinfo->fd->file_off,
170                                   "File Offset: %ld (0x%lx)",
171                                   pinfo->fd->file_off, pinfo->fd->file_off);
172           }
173         }
174
175
176         TRY {
177                 if (!dissector_try_port(wtap_encap_dissector_table, pinfo->fd->lnk_t,
178                                         tvb, pinfo, tree)) {
179
180                         if (check_col(pinfo->cinfo, COL_PROTOCOL))
181                                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "UNKNOWN");
182                         if (check_col(pinfo->cinfo, COL_INFO))
183                                 col_add_fstr(pinfo->cinfo, COL_INFO, "WTAP_ENCAP = %u",
184                                     pinfo->fd->lnk_t);
185                         call_dissector(data_handle,tvb, pinfo, tree);
186                 }
187         }
188         CATCH(BoundsError) {
189                 if (check_col(pinfo->cinfo, COL_INFO))
190                         col_append_str(pinfo->cinfo, COL_INFO, "[Short Frame]");
191                 proto_tree_add_protocol_format(tree, proto_short, tvb, 0, 0,
192                                 "[Short Frame: %s]", pinfo->current_proto);
193         }
194         CATCH(ReportedBoundsError) {
195                 show_reported_bounds_error(tvb, pinfo, tree);
196         }
197         ENDTRY;
198
199         tap_queue_packet(frame_tap, pinfo, NULL);
200 }
201
202 void
203 show_reported_bounds_error(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
204 {
205         if (pinfo->fragmented) {
206                 /*
207                  * We were dissecting an unreassembled fragmented
208                  * packet when the exception was thrown, so the
209                  * problem isn't that the dissector expected
210                  * something but it wasn't in the packet, the
211                  * problem is that the dissector expected something
212                  * but it wasn't in the fragment we dissected.
213                  */
214                 if (check_col(pinfo->cinfo, COL_INFO))
215                         col_append_str(pinfo->cinfo, COL_INFO,
216                             "[Unreassembled Packet]");
217                 proto_tree_add_protocol_format(tree, proto_unreassembled,
218                     tvb, 0, 0, "[Unreassembled Packet: %s]",
219                     pinfo->current_proto);
220         } else {
221                 if (check_col(pinfo->cinfo, COL_INFO))
222                         col_append_str(pinfo->cinfo, COL_INFO,
223                             "[Malformed Packet]");
224                 proto_tree_add_protocol_format(tree, proto_malformed,
225                     tvb, 0, 0, "[Malformed Packet: %s]", pinfo->current_proto);
226         }
227 }
228
229 void
230 proto_register_frame(void)
231 {
232         static hf_register_info hf[] = {
233                 { &hf_frame_arrival_time,
234                 { "Arrival Time",               "frame.time", FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
235                         "", HFILL }},
236
237                 { &hf_frame_time_delta,
238                 { "Time delta from previous packet",    "frame.time_delta", FT_RELATIVE_TIME, BASE_NONE, NULL,
239                         0x0,
240                         "", HFILL }},
241
242                 { &hf_frame_time_relative,
243                 { "Time relative to first packet",      "frame.time_relative", FT_RELATIVE_TIME, BASE_NONE, NULL,
244                         0x0,
245                         "", HFILL }},
246
247                 { &hf_frame_number,
248                 { "Frame Number",               "frame.number", FT_UINT32, BASE_DEC, NULL, 0x0,
249                         "", HFILL }},
250
251                 { &hf_frame_packet_len,
252                 { "Total Frame Length",         "frame.pkt_len", FT_UINT32, BASE_DEC, NULL, 0x0,
253                         "", HFILL }},
254
255                 { &hf_frame_capture_len,
256                 { "Capture Frame Length",       "frame.cap_len", FT_UINT32, BASE_DEC, NULL, 0x0,
257                         "", HFILL }},
258
259                 { &hf_frame_p2p_dir,
260                 { "Point-to-Point Direction",   "frame.p2p_dir", FT_UINT8, BASE_DEC, VALS(p2p_dirs), 0x0,
261                         "", HFILL }},
262
263                 { &hf_frame_file_off,
264                 { "File Offset",        "frame.file_off", FT_INT32, BASE_DEC, NULL, 0x0,
265                         "", HFILL }},
266
267                 { &hf_frame_marked,
268                 { "Frame is marked",    "frame.marked", FT_BOOLEAN, 8, NULL, 0x0,
269                         "Frame is marked in the GUI", HFILL }},
270         };
271         static gint *ett[] = {
272                 &ett_frame,
273         };
274         module_t *frame_module;
275
276         wtap_encap_dissector_table = register_dissector_table("wtap_encap",
277             "Wiretap encapsulation type", FT_UINT32, BASE_DEC);
278
279         proto_frame = proto_register_protocol("Frame", "Frame", "frame");
280         proto_register_field_array(proto_frame, hf, array_length(hf));
281         proto_register_subtree_array(ett, array_length(ett));
282         register_dissector("frame",dissect_frame,proto_frame);
283
284         /* You can't disable dissection of "Frame", as that would be
285            tantamount to not doing any dissection whatsoever. */
286         proto_set_cant_disable(proto_frame);
287
288         proto_short = proto_register_protocol("Short Frame", "Short frame", "short");
289         proto_malformed = proto_register_protocol("Malformed Packet",
290             "Malformed packet", "malformed");
291         proto_unreassembled = proto_register_protocol(
292             "Unreassembled Fragmented Packet",
293             "Unreassembled fragmented packet", "unreassembled");
294
295         /* "Short Frame", "Malformed Packet", and "Unreassembled Fragmented
296            Packet" aren't really protocols, they're error indications;
297            disabling them makes no sense. */
298         proto_set_cant_disable(proto_short);
299         proto_set_cant_disable(proto_malformed);
300         proto_set_cant_disable(proto_unreassembled);
301
302         /* Our preferences */
303         frame_module = prefs_register_protocol(proto_frame, NULL);
304         prefs_register_bool_preference(frame_module, "show_file_off",
305             "Show File Offset", "Show File Offset", &show_file_off);
306         prefs_register_bool_preference(frame_module, "force_docsis_encap",
307             "Treat all frames as DOCSIS frames", "Treat all frames as DOCSIS Frames", &force_docsis_encap);
308
309         frame_tap=register_tap("frame");
310 }
311
312 void
313 proto_reg_handoff_frame(void)
314 {
315         data_handle = find_dissector("data");
316         docsis_handle = find_dissector("docsis");
317 }