Latest "config.guess" and "config.sub" from
[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.12 2001/12/06 22:52:18 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@zing.org>
9  * Copyright 2000 Gerald Combs
10  *
11  * 
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <glib.h>
32 #include "packet.h"
33 #include "timestamp.h"
34 #include "tvbuff.h"
35 #include "packet-frame.h"
36 #include "prefs.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 proto_short = -1;
48 int proto_malformed = -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 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->fd->lnk_t == WTAP_ENCAP_LAPD ||
76                         pinfo->fd->lnk_t == WTAP_ENCAP_PPP_WITH_PHDR) {
77
78                 pinfo->p2p_dir = pinfo->pseudo_header->p2p.sent ? P2P_DIR_SENT : P2P_DIR_RECV;
79         }
80
81         /* Put in frame header information. */
82         if (tree) {
83
84           cap_len = tvb_length(tvb);
85           pkt_len = tvb_reported_length(tvb);
86
87           ti = proto_tree_add_protocol_format(tree, proto_frame, tvb, 0, tvb_length(tvb),
88             "Frame %u (%u on wire, %u captured)", pinfo->fd->num, pkt_len, cap_len);
89
90           fh_tree = proto_item_add_subtree(ti, ett_frame);
91
92           ts.secs = pinfo->fd->abs_secs;
93           ts.nsecs = pinfo->fd->abs_usecs*1000;
94
95           proto_tree_add_time(fh_tree, hf_frame_arrival_time, tvb,
96                 0, 0, &ts);
97
98           ts.secs = pinfo->fd->del_secs;
99           ts.nsecs = pinfo->fd->del_usecs*1000;
100
101           proto_tree_add_time(fh_tree, hf_frame_time_delta, tvb,
102                 0, 0, &ts);
103
104           ts.secs = pinfo->fd->rel_secs;
105           ts.nsecs = pinfo->fd->rel_usecs*1000;
106
107           proto_tree_add_time(fh_tree, hf_frame_time_relative, tvb,
108                 0, 0, &ts);
109
110           proto_tree_add_uint(fh_tree, hf_frame_number, tvb,
111                 0, 0, pinfo->fd->num);
112
113           proto_tree_add_uint_format(fh_tree, hf_frame_packet_len, tvb,
114                 0, 0, pkt_len, "Packet Length: %d byte%s", pkt_len,
115                 plurality(pkt_len, "", "s"));
116                 
117           proto_tree_add_uint_format(fh_tree, hf_frame_capture_len, tvb,
118                 0, 0, cap_len, "Capture Length: %d byte%s", cap_len,
119                 plurality(cap_len, "", "s"));
120
121           /* Check for existences of P2P pseudo header */
122           if (pinfo->p2p_dir != P2P_DIR_UNKNOWN) {
123                   proto_tree_add_uint(fh_tree, hf_frame_p2p_dir, tvb,
124                                   0, 0, pinfo->p2p_dir);
125           }
126
127       if (show_file_off) {
128         proto_tree_add_int_format(fh_tree, hf_frame_file_off, tvb,
129             0, 0, pinfo->fd->file_off, "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
207         proto_frame = proto_register_protocol("Frame", "Frame", "frame");
208         proto_register_field_array(proto_frame, hf, array_length(hf));
209         proto_register_subtree_array(ett, array_length(ett));
210         register_dissector("frame",dissect_frame,proto_frame);
211
212         /* You can't disable dissection of "Frame", as that would be
213            tantamount to not doing any dissection whatsoever. */
214         proto_set_cant_disable(proto_frame);
215
216         proto_short = proto_register_protocol("Short Frame", "Short frame", "short");
217         proto_malformed = proto_register_protocol("Malformed Frame",
218             "Malformed frame", "malformed");
219
220         /* "Short Frame" and "Malformed Frame" aren't really protocols,
221            they're error indications; disabling them makes no sense. */
222         proto_set_cant_disable(proto_short);
223         proto_set_cant_disable(proto_malformed);
224
225     /* Our preferences */
226     frame_module = prefs_register_protocol(proto_frame, NULL);
227     prefs_register_bool_preference(frame_module, "show_file_off",
228         "Show File Offset", "Show File Offset", &show_file_off);
229 }
230
231 void
232 proto_reg_handoff_frame(void){
233   data_handle = find_dissector("data");
234 }