add WERR_INVALID_STATE
[wireshark-wip.git] / plugins / profinet / packet-pn.c
blobcb8fd2434d8bc95082e610f7ae28d8c961b319ea
1 /* packet-pn.c
2 * Common functions for other PROFINET protocols like IO, CBA, DCP, ...
4 * $Id$
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1999 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "config.h"
28 #include <string.h>
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/dissectors/packet-dcerpc.h>
33 #include <epan/expert.h>
35 #include "packet-pn.h"
39 static int hf_pn_padding = -1;
40 static int hf_pn_undecoded_data = -1;
41 static int hf_pn_user_data = -1;
42 static int hf_pn_user_bytes = -1;
43 static int hf_pn_frag_bytes = -1;
44 static int hf_pn_malformed = -1;
46 static expert_field ei_pn_undecoded_data = EI_INIT;
48 /* dissect an 8 bit unsigned integer */
49 int
50 dissect_pn_uint8(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_,
51 proto_tree *tree, int hfindex, guint8 *pdata)
53 guint8 data;
55 data = tvb_get_guint8 (tvb, offset);
56 if (tree) {
57 proto_tree_add_uint(tree, hfindex, tvb, offset, 1, data);
59 if (pdata)
60 *pdata = data;
61 return offset + 1;
64 /* dissect a 16 bit unsigned integer */
65 int
66 dissect_pn_uint16(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_,
67 proto_tree *tree, int hfindex, guint16 *pdata)
69 guint16 data;
71 data = tvb_get_ntohs (tvb, offset);
73 if (tree) {
74 proto_tree_add_uint(tree, hfindex, tvb, offset, 2, data);
76 if (pdata)
77 *pdata = data;
78 return offset + 2;
81 /* dissect a 32 bit unsigned integer */
82 int
83 dissect_pn_uint32(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_,
84 proto_tree *tree, int hfindex, guint32 *pdata)
86 guint32 data;
88 data = tvb_get_ntohl (tvb, offset);
90 if (tree) {
91 proto_tree_add_uint(tree, hfindex, tvb, offset, 4, data);
93 if (pdata)
94 *pdata = data;
95 return offset+4;
98 /* dissect a 16 bit signed integer */
99 int
100 dissect_pn_int16(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_,
101 proto_tree *tree, int hfindex, gint16 *pdata)
103 gint16 data;
105 data = tvb_get_ntohs (tvb, offset);
107 if (tree) {
108 proto_tree_add_int(tree, hfindex, tvb, offset, 2, data);
110 if (pdata)
111 *pdata = data;
112 return offset + 2;
115 /* dissect a 32 bit signed integer */
117 dissect_pn_int32(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_,
118 proto_tree *tree, int hfindex, gint32 *pdata)
120 gint32 data;
122 data = tvb_get_ntohl (tvb, offset);
124 if (tree) {
125 proto_tree_add_int(tree, hfindex, tvb, offset, 4, data);
127 if (pdata)
128 *pdata = data;
129 return offset + 4;
132 /* dissect a 24bit OUI (IEC organizational unique id) */
134 dissect_pn_oid(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
135 proto_tree *tree, int hfindex, guint32 *pdata)
137 guint32 data;
139 data = tvb_get_ntoh24(tvb, offset);
141 if (tree) {
142 proto_tree_add_uint(tree, hfindex, tvb, offset, 3, data);
144 if (pdata)
145 *pdata = data;
146 return offset + 3;
149 /* dissect a 6 byte MAC address */
151 dissect_pn_mac(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
152 proto_tree *tree, int hfindex, guint8 *pdata)
154 guint8 data[6];
156 tvb_memcpy(tvb, data, offset, 6);
157 if(tree)
158 proto_tree_add_ether(tree, hfindex, tvb, offset, 6, data);
160 if (pdata)
161 memcpy(pdata, data, 6);
163 return offset + 6;
166 /* dissect an IPv4 address */
168 dissect_pn_ipv4(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
169 proto_tree *tree, int hfindex, guint32 *pdata)
171 guint32 data;
173 data = tvb_get_ipv4(tvb, offset);
174 if(tree)
175 proto_tree_add_ipv4(tree, hfindex, tvb, offset, 4, data);
177 if (pdata)
178 *pdata = data;
180 return offset + 4;
183 /* dissect a 16 byte UUID address */
185 dissect_pn_uuid(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
186 proto_tree *tree, int hfindex, e_uuid_t *uuid)
188 guint8 drep[2] = { 0,0 };
190 offset = dissect_dcerpc_uuid_t(tvb, offset, pinfo, tree, drep,
191 hfindex, uuid);
193 return offset;
196 /* "dissect" some bytes still undecoded (with Expert warning) */
198 dissect_pn_undecoded(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
199 proto_tree *tree, guint32 length)
201 proto_item *item;
204 item = proto_tree_add_string_format(tree, hf_pn_undecoded_data, tvb, offset, length, "data",
205 "Undecoded Data: %d bytes", length);
207 expert_add_info_format(pinfo, item, &ei_pn_undecoded_data,
208 "Undecoded Data, %u bytes", length);
210 return offset + length;
213 /* "dissect" some user bytes */
215 dissect_pn_user_data_bytes(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
216 proto_tree *tree, guint32 length, int iSelect)
218 if (tree) {
219 if(iSelect == FRAG_DATA)
220 proto_tree_add_bytes(tree, hf_pn_frag_bytes, tvb, offset, length, tvb_get_ptr(tvb,offset, length));
221 else
222 proto_tree_add_bytes(tree, hf_pn_user_bytes, tvb, offset, length, tvb_get_ptr(tvb,offset, length));
224 return offset + length;
228 dissect_pn_user_data(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
229 proto_tree *tree, guint32 length, const char *text)
231 if (tree) {
232 proto_tree_add_string_format(tree, hf_pn_user_data, tvb, offset, length, "data",
233 "%s: %d bytes", text, length);
235 return offset + length;
238 /* packet is malformed, mark it as such */
240 dissect_pn_malformed(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
241 proto_tree *tree, guint32 length)
243 proto_tree_add_item(tree, hf_pn_malformed, tvb, 0, 10000, ENC_NA);
245 return offset + length;
249 /* dissect some padding data (with the given length) */
251 dissect_pn_padding(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
252 proto_tree *tree, int length)
254 proto_tree_add_string_format(tree, hf_pn_padding, tvb, offset, length, "data",
255 "Padding: %u byte", length);
257 return offset + length;
260 /* align offset to 4 */
262 dissect_pn_align4(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
264 guint padding = 0;
267 if (offset % 4) {
268 padding = 4 - (offset % 4);
270 proto_tree_add_string_format(tree, hf_pn_padding, tvb, offset, padding, "data",
271 "Padding: %u byte", padding);
274 return offset + padding;
277 /* append the given info text to item and column */
278 void
279 pn_append_info(packet_info *pinfo, proto_item *dcp_item, const char *text)
281 col_append_str(pinfo->cinfo, COL_INFO, text);
283 proto_item_append_text(dcp_item, "%s", text);
288 void
289 init_pn (int proto)
291 static hf_register_info hf[] = {
292 { &hf_pn_padding,
293 { "Padding", "pn.padding",
294 FT_STRING, BASE_NONE, NULL, 0x0,
295 NULL, HFILL }},
297 { &hf_pn_undecoded_data,
298 { "Undecoded Data", "pn.undecoded",
299 FT_STRING, BASE_NONE, NULL, 0x0,
300 NULL, HFILL }},
302 { &hf_pn_user_data,
303 { "User Data", "pn.user_data",
304 FT_STRING, BASE_NONE, NULL, 0x0,
305 NULL, HFILL }},
307 { &hf_pn_user_bytes,
308 { "Substitute Data", "pn.user_bytes",
309 FT_BYTES, BASE_NONE, NULL, 0x0,
310 NULL, HFILL }},
312 { &hf_pn_frag_bytes,
313 { "Fragment Data", "pn.frag_bytes",
314 FT_BYTES, BASE_NONE, NULL, 0x0,
315 NULL, HFILL }},
317 { &hf_pn_malformed,
318 { "Malformed", "pn_rt.malformed",
319 FT_BYTES, BASE_NONE, NULL, 0x0,
320 NULL, HFILL }}
324 /*static gint *ett[] = {
325 };*/
327 static ei_register_info ei[] = {
328 { &ei_pn_undecoded_data, { "pn.undecoded_data", PI_UNDECODED, PI_WARN, "Undecoded Data", EXPFILL }},
331 expert_module_t* expert_pn;
334 proto_register_field_array (proto, hf, array_length (hf));
335 /*proto_register_subtree_array (ett, array_length (ett));*/
336 expert_pn = expert_register_protocol(proto);
337 expert_register_field_array(expert_pn, ei, array_length(ei));