Clean up indentation.
[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.14 2001/12/08 21:03:41 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 "packet.h"
32 #include "timestamp.h"
33 #include "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
49 static gint ett_frame = -1;
50
51 static dissector_handle_t data_handle;
52
53 /* Preferences */
54 static gboolean show_file_off = FALSE;
55
56 static const value_string p2p_dirs[] = {
57         { P2P_DIR_SENT, "Sent" },
58         { P2P_DIR_RECV, "Received" },
59         { 0, NULL }
60 };
61
62 static dissector_table_t wtap_encap_dissector_table;
63         
64 void
65 dissect_frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
66 {
67         proto_tree      *fh_tree;
68         proto_item      *ti;
69         nstime_t        ts;
70         int             cap_len, pkt_len;
71
72         pinfo->current_proto = "Frame";
73
74         if (pinfo->fd->lnk_t == WTAP_ENCAP_LAPD ||
75                         pinfo->fd->lnk_t == WTAP_ENCAP_PPP_WITH_PHDR) {
76
77                 pinfo->p2p_dir = pinfo->pseudo_header->p2p.sent ? P2P_DIR_SENT : P2P_DIR_RECV;
78         }
79
80         /* Put in frame header information. */
81         if (tree) {
82
83           cap_len = tvb_length(tvb);
84           pkt_len = tvb_reported_length(tvb);
85
86           ti = proto_tree_add_protocol_format(tree, proto_frame, tvb, 0, tvb_length(tvb),
87             "Frame %u (%u on wire, %u captured)", pinfo->fd->num, pkt_len, cap_len);
88
89           fh_tree = proto_item_add_subtree(ti, ett_frame);
90
91           ts.secs = pinfo->fd->abs_secs;
92           ts.nsecs = pinfo->fd->abs_usecs*1000;
93
94           proto_tree_add_time(fh_tree, hf_frame_arrival_time, tvb,
95                 0, 0, &ts);
96
97           ts.secs = pinfo->fd->del_secs;
98           ts.nsecs = pinfo->fd->del_usecs*1000;
99
100           proto_tree_add_time(fh_tree, hf_frame_time_delta, tvb,
101                 0, 0, &ts);
102
103           ts.secs = pinfo->fd->rel_secs;
104           ts.nsecs = pinfo->fd->rel_usecs*1000;
105
106           proto_tree_add_time(fh_tree, hf_frame_time_relative, tvb,
107                 0, 0, &ts);
108
109           proto_tree_add_uint(fh_tree, hf_frame_number, tvb,
110                 0, 0, pinfo->fd->num);
111
112           proto_tree_add_uint_format(fh_tree, hf_frame_packet_len, tvb,
113                 0, 0, pkt_len, "Packet Length: %d byte%s", pkt_len,
114                 plurality(pkt_len, "", "s"));
115                 
116           proto_tree_add_uint_format(fh_tree, hf_frame_capture_len, tvb,
117                 0, 0, cap_len, "Capture Length: %d byte%s", cap_len,
118                 plurality(cap_len, "", "s"));
119
120           /* Check for existences of P2P pseudo header */
121           if (pinfo->p2p_dir != P2P_DIR_UNKNOWN) {
122                   proto_tree_add_uint(fh_tree, hf_frame_p2p_dir, tvb,
123                                   0, 0, pinfo->p2p_dir);
124           }
125
126           if (show_file_off) {
127                   proto_tree_add_int_format(fh_tree, hf_frame_file_off, tvb,
128                                   0, 0, pinfo->fd->file_off,
129                                   "File Offset: %ld (0x%lx)",
130                                   pinfo->fd->file_off, pinfo->fd->file_off);
131           }
132         }
133
134
135         TRY {
136                 if (!dissector_try_port(wtap_encap_dissector_table, pinfo->fd->lnk_t,
137                                         tvb, pinfo, tree)) {
138
139                         if (check_col(pinfo->fd, COL_PROTOCOL))
140                                 col_set_str(pinfo->fd, COL_PROTOCOL, "UNKNOWN");
141                         if (check_col(pinfo->fd, COL_INFO))
142                                 col_add_fstr(pinfo->fd, COL_INFO, "WTAP_ENCAP = %u", pinfo->fd->lnk_t);
143                         call_dissector(data_handle,tvb, pinfo, tree);
144                 }
145         }
146         CATCH(BoundsError) {
147                 if (check_col(pinfo->fd, COL_INFO))
148                         col_append_str(pinfo->fd, COL_INFO, "[Short Frame]");
149                 proto_tree_add_protocol_format(tree, proto_short, tvb, 0, 0,
150                                 "[Short Frame: %s]", pinfo->current_proto );
151         }
152         CATCH(ReportedBoundsError) {
153                 if (check_col(pinfo->fd, COL_INFO))
154                         col_append_str(pinfo->fd, COL_INFO, "[Malformed Frame]");
155                 proto_tree_add_protocol_format(tree, proto_malformed, tvb, 0, 0,
156                                 "[Malformed Frame: %s]", pinfo->current_proto );
157         }
158         ENDTRY;
159 }
160
161 void
162 proto_register_frame(void)
163 {
164         static hf_register_info hf[] = {
165                 { &hf_frame_arrival_time,
166                 { "Arrival Time",               "frame.time", FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
167                         "", HFILL }},
168
169                 { &hf_frame_time_delta,
170                 { "Time delta from previous packet",    "frame.time_delta", FT_RELATIVE_TIME, BASE_NONE, NULL,
171                         0x0,
172                         "", HFILL }},
173
174                 { &hf_frame_time_relative,
175                 { "Time relative to first packet",      "frame.time_relative", FT_RELATIVE_TIME, BASE_NONE, NULL,
176                         0x0,
177                         "", HFILL }},
178
179                 { &hf_frame_number,
180                 { "Frame Number",               "frame.number", FT_UINT32, BASE_DEC, NULL, 0x0,
181                         "", HFILL }},
182
183                 { &hf_frame_packet_len,
184                 { "Total Frame Length",         "frame.pkt_len", FT_UINT32, BASE_DEC, NULL, 0x0,
185                         "", HFILL }},
186
187                 { &hf_frame_capture_len,
188                 { "Capture Frame Length",       "frame.cap_len", FT_UINT32, BASE_DEC, NULL, 0x0,
189                         "", HFILL }},
190
191                 { &hf_frame_p2p_dir,
192                 { "Point-to-Point Direction",   "frame.p2p_dir", FT_UINT8, BASE_DEC, VALS(p2p_dirs), 0x0,
193                         "", HFILL }},
194
195                 { &hf_frame_file_off,
196                 { "File Offset",        "frame.file_off", FT_INT32, BASE_DEC, NULL, 0x0,
197                         "", HFILL }},
198
199         };
200         static gint *ett[] = {
201                 &ett_frame,
202         };
203         module_t *frame_module; 
204
205         wtap_encap_dissector_table = register_dissector_table("wtap_encap",
206             "Wiretap encapsulation type", FT_UINT32, BASE_DEC);
207
208         proto_frame = proto_register_protocol("Frame", "Frame", "frame");
209         proto_register_field_array(proto_frame, hf, array_length(hf));
210         proto_register_subtree_array(ett, array_length(ett));
211         register_dissector("frame",dissect_frame,proto_frame);
212
213         /* You can't disable dissection of "Frame", as that would be
214            tantamount to not doing any dissection whatsoever. */
215         proto_set_cant_disable(proto_frame);
216
217         proto_short = proto_register_protocol("Short Frame", "Short frame", "short");
218         proto_malformed = proto_register_protocol("Malformed Frame",
219             "Malformed frame", "malformed");
220
221         /* "Short Frame" and "Malformed Frame" aren't really protocols,
222            they're error indications; disabling them makes no sense. */
223         proto_set_cant_disable(proto_short);
224         proto_set_cant_disable(proto_malformed);
225
226         /* Our preferences */
227         frame_module = prefs_register_protocol(proto_frame, NULL);
228         prefs_register_bool_preference(frame_module, "show_file_off",
229             "Show File Offset", "Show File Offset", &show_file_off);
230 }
231
232 void
233 proto_reg_handoff_frame(void)
234 {
235         data_handle = find_dissector("data");
236 }