HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / dissectors / packet-ixiatrailer.c
blobbebba40642a04024838f272f01476bf3f7aca307
1 /* packet-ixiatrailer.c
2 * Routines for Ixia trailer parsing
4 * Dissector for Ixia Network Visibility Solutions trailer
5 * Copyright Ixia 2012
7 * $Id$
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "config.h"
30 #include <epan/packet.h>
31 #include <wsutil/pint.h>
32 #include <epan/prefs.h>
33 #include <epan/in_cksum.h>
34 #include <epan/expert.h>
36 #define IXIA_PATTERN 0xAF12
38 /* TODO: which of these typestamp types are currently supported?
39 Should lose the rest!! */
40 #define IXIATRAILER_FTYPE_TIMESTAMP_LOCAL 3
41 #define IXIATRAILER_FTYPE_TIMESTAMP_NTP 4
42 #define IXIATRAILER_FTYPE_TIMESTAMP_GPS 5
43 #define IXIATRAILER_FTYPE_TIMESTAMP_1588 6 /* PTP */
44 #define IXIATRAILER_FTYPE_TIMESTAMP_HOLDOVER 7
46 static const value_string ixiatrailer_ftype_timestamp[] = {
47 { IXIATRAILER_FTYPE_TIMESTAMP_LOCAL, "Local" },
48 { IXIATRAILER_FTYPE_TIMESTAMP_NTP, "NTP" },
49 { IXIATRAILER_FTYPE_TIMESTAMP_GPS, "GPS" },
50 { IXIATRAILER_FTYPE_TIMESTAMP_1588, "PTP" },
51 { IXIATRAILER_FTYPE_TIMESTAMP_HOLDOVER, "Holdover" },
52 { 0, NULL }
55 /* Preference settings */
56 static gboolean ixiatrailer_summary_in_tree = TRUE;
58 static int proto_ixiatrailer = -1;
59 static gint ett_ixiatrailer = -1;
61 static int hf_ixiatrailer_timestamp = -1;
62 static int hf_ixiatrailer_generic = -1;
64 static expert_field ei_ixiatrailer_field_length_invalid = EI_INIT;
66 /* Format is as follows:
67 - Time Sync source (1 byte)
68 - Length of time (1 byte - value will always be 8)
69 - Timestamp (8 bytes)
70 - Trailer length -previous fields, always 10 (1 byte)
71 - Ixia signature - AF12 (2 bytes)
72 - FCS for IXIA timestamp - covers 13 bytes of all previous fields (2 bytes)
74 static int
75 dissect_ixiatrailer(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree, void *data _U_)
77 proto_tree *ti;
78 guint tvblen, trailer_length, time_length;
79 proto_tree *ixiatrailer_tree = NULL;
80 guint offset = 0;
81 guint16 cksum, comp_cksum;
82 vec_t vec;
83 guint8 source;
85 /* Need at least 5 bytes. TODO: should be 15? */
86 tvblen = tvb_length(tvb);
87 if (tvblen < 5) {
88 return 0;
91 /* Depending upon the ethernet preference "Assume packets have FCS", we
92 may be given those 4 bytes too. If we see 19 bytes, assume we are
93 getting them and only look at first 15. */
94 if (tvblen == 19) {
95 tvblen = 15;
98 /* 3rd & 4th bytes from the end must match our pattern */
99 if (tvb_get_ntohs(tvb, tvblen-4) != IXIA_PATTERN) {
100 return 0;
103 /* Read Trailer-length field */
104 trailer_length = tvb_get_guint8(tvb, tvblen-5);
105 /* Should match overall length of trailer */
106 if ((tvblen-5) != trailer_length) {
107 return 0;
110 /* Last 2 bytes are the checksum */
111 cksum = tvb_get_ntohs(tvb, tvblen-2);
113 /* Verify the checksum; if not valid, it means that the trailer is not valid */
114 vec.len = trailer_length + 3;
115 vec.ptr = tvb_get_ptr(tvb, offset, vec.len);
116 comp_cksum = in_cksum(&vec, 1);
117 if (pntohs(&comp_cksum) != cksum) {
118 return 0;
121 /* OK: We have our trailer - create protocol root */
122 ti = proto_tree_add_item(tree, proto_ixiatrailer, tvb, offset, trailer_length + 5, ENC_NA);
124 /* Append summary to item, if configured to */
125 if (ixiatrailer_summary_in_tree) {
126 proto_item_append_text(ti, ", Length: %u, Checksum: 0x%x", trailer_length, cksum);
129 /* Create subtree */
130 ixiatrailer_tree = proto_item_add_subtree(ti, ett_ixiatrailer);
132 source = tvb_get_guint8(tvb, offset++);
133 time_length = tvb_get_guint8(tvb, offset++);
135 switch (source) {
136 case IXIATRAILER_FTYPE_TIMESTAMP_LOCAL:
137 case IXIATRAILER_FTYPE_TIMESTAMP_NTP:
138 case IXIATRAILER_FTYPE_TIMESTAMP_GPS:
139 case IXIATRAILER_FTYPE_TIMESTAMP_1588:
140 case IXIATRAILER_FTYPE_TIMESTAMP_HOLDOVER:
141 if (time_length != 8) {
142 expert_add_info_format(pinfo, ti, &ei_ixiatrailer_field_length_invalid, "Field length %u invalid", time_length);
143 break;
145 /* Timestamp */
146 ti = proto_tree_add_item(ixiatrailer_tree, hf_ixiatrailer_timestamp, tvb, offset, time_length, ENC_TIME_TIMESPEC|ENC_BIG_ENDIAN);
147 proto_item_append_text(ti, "; Source: %s", val_to_str_const(source, ixiatrailer_ftype_timestamp, "Unknown"));
148 break;
150 default:
151 /* Not a recognised time format - just show as bytes */
152 ti = proto_tree_add_item(ixiatrailer_tree, hf_ixiatrailer_generic, tvb, offset, time_length, ENC_NA);
153 proto_item_append_text(ti, " [Id: %u, Length: %u]", source, time_length);
154 break;
157 /* We are claiming all of the bytes */
158 return tvblen;
161 void
162 proto_register_ixiatrailer(void)
165 static hf_register_info hf[] = {
166 { &hf_ixiatrailer_timestamp, {
167 "Time Stamp", "ixiatrailer.timestamp", FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL,
168 NULL, 0x0, NULL, HFILL }},
169 { &hf_ixiatrailer_generic, {
170 "Generic Field", "ixiatrailer.generic", FT_BYTES, BASE_NONE,
171 NULL, 0x0, NULL, HFILL }},
174 static gint *ixiatrailer_ett[] = {
175 &ett_ixiatrailer
178 static ei_register_info ei[] = {
179 { &ei_ixiatrailer_field_length_invalid, { "ixiatrailer.field_length_invalid", PI_MALFORMED, PI_ERROR, "Field length invalid", EXPFILL }},
182 module_t *ixiatrailer_module;
183 expert_module_t* expert_ixiatrailer;
185 proto_ixiatrailer = proto_register_protocol("Ixia Trailer", "IXIATRAILER", "ixiatrailer");
186 proto_register_field_array(proto_ixiatrailer, hf, array_length(hf));
187 proto_register_subtree_array(ixiatrailer_ett, array_length(ixiatrailer_ett));
188 expert_ixiatrailer = expert_register_protocol(proto_ixiatrailer);
189 expert_register_field_array(expert_ixiatrailer, ei, array_length(ei));
191 ixiatrailer_module = prefs_register_protocol(proto_ixiatrailer, NULL);
192 prefs_register_bool_preference(ixiatrailer_module, "summary_in_tree",
193 "Show trailer summary in protocol tree",
194 "Whether the trailer summary line should be shown in the protocol tree",
195 &ixiatrailer_summary_in_tree);
199 void
200 proto_reg_handoff_ixiatrailer(void)
202 /* Check for Ixia format in the ethernet trailer */
203 heur_dissector_add("eth.trailer", dissect_ixiatrailer, proto_ixiatrailer);