Init some variables to prevent scan-build "Function call argument is an uninitialized...
[metze/wireshark/wip.git] / plugins / profinet / packet-pn.c
1 /* packet-pn.c
2  * Common functions for other PROFINET protocols like IO, CBA, DCP, ...
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1999 Gerald Combs
9  *
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.
14  *
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.
19  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <string.h>
31
32 #include <glib.h>
33 #include <epan/packet.h>
34 #include <epan/dissectors/packet-dcerpc.h>
35 #include <epan/expert.h>
36
37 #include "packet-pn.h"
38
39
40
41 static int hf_pn_padding = -1;
42 static int hf_pn_undecoded_data = -1;
43 static int hf_pn_user_data = -1;
44 static int hf_pn_user_bytes = -1;
45 static int hf_pn_malformed = -1;
46
47
48
49 /* dissect an 8 bit unsigned integer */
50 int
51 dissect_pn_uint8(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_,
52                   proto_tree *tree, int hfindex, guint8 *pdata)
53 {
54     guint8 data;
55
56     data = tvb_get_guint8 (tvb, offset);
57     if (tree) {
58         proto_tree_add_uint(tree, hfindex, tvb, offset, 1, data);
59     }
60     if (pdata)
61         *pdata = data;
62     return offset + 1;
63 }
64
65 /* dissect a 16 bit unsigned integer */
66 int
67 dissect_pn_uint16(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_,
68                        proto_tree *tree, int hfindex, guint16 *pdata)
69 {
70     guint16 data;
71
72     data = tvb_get_ntohs (tvb, offset);
73
74     if (tree) {
75         proto_tree_add_uint(tree, hfindex, tvb, offset, 2, data);
76     }
77     if (pdata)
78         *pdata = data;
79     return offset + 2;
80 }
81
82 /* dissect a 32 bit unsigned integer */
83 int
84 dissect_pn_uint32(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_,
85                        proto_tree *tree, int hfindex, guint32 *pdata)
86 {
87     guint32 data;
88
89     data = tvb_get_ntohl (tvb, offset);
90
91     if (tree) {
92         proto_tree_add_uint(tree, hfindex, tvb, offset, 4, data);
93     }
94     if (pdata)
95         *pdata = data;
96     return offset+4;
97 }
98
99 /* dissect a 16 bit signed integer */
100 int
101 dissect_pn_int16(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_,
102                        proto_tree *tree, int hfindex, gint16 *pdata)
103 {
104     gint16 data;
105
106     data = tvb_get_ntohs (tvb, offset);
107
108     if (tree) {
109         proto_tree_add_int(tree, hfindex, tvb, offset, 2, data);
110     }
111     if (pdata)
112         *pdata = data;
113     return offset + 2;
114 }
115
116 /* dissect a 32 bit signed integer */
117 int
118 dissect_pn_int32(tvbuff_t *tvb, gint offset, packet_info *pinfo _U_,
119                        proto_tree *tree, int hfindex, gint32 *pdata)
120 {
121     gint32 data;
122
123     data = tvb_get_ntohl (tvb, offset);
124
125     if (tree) {
126         proto_tree_add_int(tree, hfindex, tvb, offset, 4, data);
127     }
128     if (pdata)
129         *pdata = data;
130     return offset + 4;
131 }
132
133 /* dissect a 24bit OUI (IEC organizational unique id) */
134 int
135 dissect_pn_oid(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
136                     proto_tree *tree, int hfindex, guint32 *pdata)
137 {
138     guint32 data;
139
140     data = tvb_get_ntoh24(tvb, offset);
141
142     if (tree) {
143         proto_tree_add_uint(tree, hfindex, tvb, offset, 3, data);
144     }
145     if (pdata)
146         *pdata = data;
147     return offset+3;
148 }
149
150 /* dissect a 6 byte MAC address */
151 int
152 dissect_pn_mac(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
153                     proto_tree *tree, int hfindex, guint8 *pdata)
154 {
155     guint8 data[6];
156
157     tvb_memcpy(tvb, data, offset, 6);
158     if(tree)
159         proto_tree_add_ether(tree, hfindex, tvb, offset, 6, data);
160
161     if (pdata)
162         memcpy(pdata, data, 6);
163
164     return offset + 6;
165 }
166
167 /* dissect an IPv4 address */
168 int
169 dissect_pn_ipv4(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
170                     proto_tree *tree, int hfindex, guint32 *pdata)
171 {
172     guint32 data;
173
174     data = tvb_get_ipv4(tvb, offset);
175     if(tree)
176         proto_tree_add_ipv4(tree, hfindex, tvb, offset, 4, data);
177
178     if (pdata)
179         *pdata = data;
180
181     return offset + 4;
182 }
183
184 /* dissect a 16 byte UUID address */
185 int
186 dissect_pn_uuid(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
187                     proto_tree *tree, int hfindex, e_uuid_t *uuid)
188 {
189     guint8 drep[2] = { 0,0 };
190
191     offset = dissect_dcerpc_uuid_t(tvb, offset, pinfo, tree, drep,
192                     hfindex, uuid);
193
194     return offset;
195 }
196
197 /* "dissect" some bytes still undecoded (with Expert warning) */
198 int
199 dissect_pn_undecoded(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
200                     proto_tree *tree, guint32 length)
201 {
202     proto_item *item;
203
204
205     item = proto_tree_add_string_format(tree, hf_pn_undecoded_data, tvb, offset, length, "data",
206         "Undecoded Data: %d bytes", length);
207
208     expert_add_info_format(pinfo, item, PI_UNDECODED, PI_WARN,
209                     "Undecoded Data, %u bytes", length);
210
211     return offset + length;
212 }
213
214 /* "dissect" some user bytes */
215 int
216 dissect_pn_user_data_bytes(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
217                     proto_tree *tree, guint32 length, const char *text _U_)
218 {
219     proto_tree_add_bytes(tree, hf_pn_user_bytes, tvb, offset, length, tvb_get_ptr(tvb,offset, length));
220
221     return offset + length;
222 }
223
224 int
225 dissect_pn_user_data(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
226                     proto_tree *tree, guint32 length, const char *text)
227 {
228     proto_tree_add_string_format(tree, hf_pn_user_data, tvb, offset, length, "data",
229         "%s: %d bytes", text, length);
230
231     return offset + length;
232 }
233
234 /* packet is malformed, mark it as such */
235 int
236 dissect_pn_malformed(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
237                     proto_tree *tree, guint32 length)
238 {
239     proto_tree_add_item(tree, hf_pn_malformed, tvb, 0, 10000, ENC_NA);
240
241     return offset + length;
242 }
243
244
245 /* dissect some padding data (with the given length) */
246 int
247 dissect_pn_padding(tvbuff_t *tvb, int offset, packet_info *pinfo _U_,
248                     proto_tree *tree, int length)
249 {
250     proto_tree_add_string_format(tree, hf_pn_padding, tvb, offset, length, "data",
251         "Padding: %u byte", length);
252
253     return offset + length;
254 }
255
256 /* align offset to 4 */
257 int
258 dissect_pn_align4(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
259 {
260     guint padding = 0;
261
262
263     if (offset % 4) {
264         padding = 4 - (offset % 4);
265
266         proto_tree_add_string_format(tree, hf_pn_padding, tvb, offset, padding, "data",
267             "Padding: %u byte", padding);
268     }
269
270     return offset + padding;
271 }
272
273 /* append the given info text to item and column */
274 void
275 pn_append_info(packet_info *pinfo, proto_item *dcp_item, const char *text)
276 {
277     col_append_str(pinfo->cinfo, COL_INFO, text);
278
279     proto_item_append_text(dcp_item, "%s", text);
280 }
281
282
283
284 void
285 init_pn (int proto)
286 {
287         static hf_register_info hf[] = {
288         { &hf_pn_padding,
289         { "Padding", "pn.padding", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
290         { &hf_pn_undecoded_data,
291         { "Undecoded Data", "pn.undecoded", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
292         { &hf_pn_user_data,
293         { "User Data", "pn.user_data", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL }},
294         { &hf_pn_user_bytes,
295         { "Substitute Data", "pn.user_bytes", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }},
296         { &hf_pn_malformed,
297         { "Malformed", "pn_rt.malformed", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL }}
298         };
299
300
301         /*static gint *ett[] = {
302         };*/
303
304         proto_register_field_array (proto, hf, array_length (hf));
305         /*proto_register_subtree_array (ett, array_length (ett));*/
306 }
307