"hf_sna_rh_csi" is now an FT_UINT8 field, so add it with
[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.6 2001/01/03 06:55:28 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
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 proto_short = -1;
46 int proto_malformed = -1;
47
48 static gint ett_frame = -1;
49
50 static const value_string p2p_dirs[] = {
51         { P2P_DIR_SENT, "Sent" },
52         { P2P_DIR_RECV, "Received" },
53         { 0, NULL }
54 };
55
56 static dissector_table_t wtap_encap_dissector_table;
57         
58 void
59 dissect_frame(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
60 {
61         proto_tree      *fh_tree;
62         proto_item      *ti;
63         struct timeval  tv;
64         int             cap_len, pkt_len;
65
66         pinfo->current_proto = "Frame";
67
68         if (pinfo->fd->lnk_t == WTAP_ENCAP_LAPD ||
69                         pinfo->fd->lnk_t == WTAP_ENCAP_PPP_WITH_PHDR) {
70
71                 pinfo->p2p_dir = pinfo->pseudo_header->p2p.sent ? P2P_DIR_SENT : P2P_DIR_RECV;
72         }
73
74         /* Put in frame header information. */
75         if (tree) {
76
77           cap_len = tvb_length(tvb);
78           pkt_len = tvb_reported_length(tvb);
79
80           ti = proto_tree_add_protocol_format(tree, proto_frame, tvb, 0, tvb_length(tvb),
81             "Frame %u (%u on wire, %u captured)", pinfo->fd->num, pkt_len, cap_len);
82
83           fh_tree = proto_item_add_subtree(ti, ett_frame);
84
85           tv.tv_sec = pinfo->fd->abs_secs;
86           tv.tv_usec = pinfo->fd->abs_usecs;
87
88           proto_tree_add_time(fh_tree, hf_frame_arrival_time, tvb,
89                 0, 0, &tv);
90
91           tv.tv_sec = pinfo->fd->del_secs;
92           tv.tv_usec = pinfo->fd->del_usecs;
93
94           proto_tree_add_time(fh_tree, hf_frame_time_delta, tvb,
95                 0, 0, &tv);
96
97           tv.tv_sec = pinfo->fd->rel_secs;
98           tv.tv_usec = pinfo->fd->rel_usecs;
99
100           proto_tree_add_time(fh_tree, hf_frame_time_relative, tvb,
101                 0, 0, &tv);
102
103           proto_tree_add_uint(fh_tree, hf_frame_number, tvb,
104                 0, 0, pinfo->fd->num);
105
106           proto_tree_add_uint_format(fh_tree, hf_frame_packet_len, tvb,
107                 0, 0, pkt_len, "Packet Length: %d byte%s", pkt_len,
108                 plurality(pkt_len, "", "s"));
109                 
110           proto_tree_add_uint_format(fh_tree, hf_frame_capture_len, tvb,
111                 0, 0, cap_len, "Capture Length: %d byte%s", cap_len,
112                 plurality(cap_len, "", "s"));
113
114           /* Check for existences of P2P pseudo header */
115           if (pinfo->p2p_dir != P2P_DIR_UNKNOWN) {
116                   proto_tree_add_uint(fh_tree, hf_frame_p2p_dir, tvb,
117                                   0, 0, pinfo->p2p_dir);
118           }
119         }
120
121
122         TRY {
123                 if (!dissector_try_port(wtap_encap_dissector_table, pinfo->fd->lnk_t,
124                                         tvb, pinfo, tree)) {
125
126                         if (check_col(pinfo->fd, COL_PROTOCOL))
127                                 col_set_str(pinfo->fd, COL_PROTOCOL, "UNKNOWN");
128                         if (check_col(pinfo->fd, COL_INFO))
129                                 col_add_fstr(pinfo->fd, COL_INFO, "WTAP_ENCAP = 0x%x", pinfo->fd->lnk_t);
130                         dissect_data(tvb, 0, pinfo, tree);
131                 }
132         }
133         CATCH(BoundsError) {
134                 if (check_col(pinfo->fd, COL_INFO))
135                         col_append_str(pinfo->fd, COL_INFO, "[Short Frame]");
136                 proto_tree_add_protocol_format(tree, proto_short, tvb, 0, 0,
137                                 "[Short Frame: %s]", pinfo->current_proto );
138         }
139         CATCH(ReportedBoundsError) {
140                 if (check_col(pinfo->fd, COL_INFO))
141                         col_append_str(pinfo->fd, COL_INFO, "[Malformed Frame]");
142                 proto_tree_add_protocol_format(tree, proto_malformed, tvb, 0, 0,
143                                 "[Malformed Frame: %s]", pinfo->current_proto );
144         }
145         ENDTRY;
146 }
147
148 void
149 proto_register_frame(void)
150 {
151         static hf_register_info hf[] = {
152                 { &hf_frame_arrival_time,
153                 { "Arrival Time",               "frame.time", FT_ABSOLUTE_TIME, BASE_NONE, NULL, 0x0,
154                         ""}},
155
156                 { &hf_frame_time_delta,
157                 { "Time delta from previous packet",    "frame.time_delta", FT_RELATIVE_TIME, BASE_NONE, NULL,
158                         0x0,
159                         "" }},
160
161                 { &hf_frame_time_relative,
162                 { "Time relative to first packet",      "frame.time_relative", FT_RELATIVE_TIME, BASE_NONE, NULL,
163                         0x0,
164                         "" }},
165
166                 { &hf_frame_number,
167                 { "Frame Number",               "frame.number", FT_UINT32, BASE_DEC, NULL, 0x0,
168                         "" }},
169
170                 { &hf_frame_packet_len,
171                 { "Total Frame Length",         "frame.pkt_len", FT_UINT32, BASE_DEC, NULL, 0x0,
172                         "" }},
173
174                 { &hf_frame_capture_len,
175                 { "Capture Frame Length",       "frame.cap_len", FT_UINT32, BASE_DEC, NULL, 0x0,
176                         "" }},
177
178                 { &hf_frame_p2p_dir,
179                 { "Point-to-Point Direction",   "frame.p2p_dir", FT_UINT8, BASE_DEC, VALS(p2p_dirs), 0x0,
180                         "" }},
181         };
182         static gint *ett[] = {
183                 &ett_frame,
184         };
185
186         wtap_encap_dissector_table = register_dissector_table("wtap_encap");
187
188         proto_frame = proto_register_protocol("Frame", "Frame", "frame");
189         proto_register_field_array(proto_frame, hf, array_length(hf));
190         proto_register_subtree_array(ett, array_length(ett));
191
192         /* You can't disable dissection of "Frame", as that would be
193            tantamount to not doing any dissection whatsoever. */
194         proto_set_cant_disable(proto_frame);
195
196         proto_short = proto_register_protocol("Short Frame", "Short frame", "short");
197         proto_malformed = proto_register_protocol("Malformed Frame",
198             "Malformed frame", "malformed");
199
200         /* "Short Frame" and "Malformed Frame" aren't really protocols,
201            they're error indications; disabling them makes no sense. */
202         proto_set_cant_disable(proto_short);
203         proto_set_cant_disable(proto_malformed);
204 }