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