Fixup: tvb_get_string(z) -> tvb_get_string(z)_enc
[metze/wireshark/wip.git] / epan / dissectors / packet-egd.c
1 /* packet-egd.c
2  * Routines for Ethernet Global Data dissection
3  * EGD Home: www.gefanuc.com
4  *
5  * Copyright 2008
6  * 29 July 2008 -- ryan wamsley
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include "config.h"
28
29 #include <string.h>
30 #include <epan/packet.h>
31
32 #define EGD_PORT 18246 /* 0x4746 */
33
34 #define EGD_ST_NONEW        0
35 #define EGD_ST_NOERROR      1
36 #define EGD_ST_CONSUMED     2
37 #define EGD_ST_SNTPERR      3
38 #define EGD_ST_SPECERR      4
39 #define EGD_ST_REFRESHERR   6
40 #define EGD_ST_REFEXDERR    7
41 #define EGD_ST_IPERR        10
42 #define EGD_ST_RESOURSEERR  12
43 #define EGD_ST_NAMERES      16
44 #define EGD_ST_ETHERR       18
45 #define EGD_ST_NOSUPPORT    22
46 #define EGD_ST_NORESP       26
47 #define EGD_ST_CREATEERR    28
48 #define EGD_ST_DELETED      30
49
50
51 void proto_register_egd(void);
52 void proto_reg_handoff_egd(void);
53
54 /* Translate status to string */
55 static const value_string egd_stat_vals[] = {
56   { EGD_ST_NONEW,                  "No new status event has occurred" },
57   { EGD_ST_NOERROR,                "No error currently exists" },
58   { EGD_ST_CONSUMED,               "No error, data consumed" },
59   { EGD_ST_SNTPERR,                "SNTP error"  },
60   { EGD_ST_SPECERR,                "Specification error" },
61   { EGD_ST_REFRESHERR,             "Data refresh error" },
62   { EGD_ST_REFEXDERR,              "Data refresh period exceeded" },
63   { EGD_ST_IPERR,                  "IP Layer not currently initialized" },
64   { EGD_ST_RESOURSEERR,            "Lack of resource error" },
65   { EGD_ST_NAMERES,                "Name Resolution in progress" },
66   { EGD_ST_ETHERR,                 "Loss of Ethernet Interface error" },
67   { EGD_ST_NOSUPPORT,              "Ethernet Interface does not support EGD" },
68   { EGD_ST_NORESP,                 "No Response from Ethernet Interface" },
69   { EGD_ST_CREATEERR,              "Failed to create an exchange." },
70   { EGD_ST_DELETED,                "Configured exchange deleted." },
71   { 0,                             NULL }
72 };
73
74 static int proto_egd = -1;
75
76 static dissector_handle_t data_handle;
77
78 static int hf_egd_ver = -1;
79 static int hf_egd_type = -1;
80 static int hf_egd_rid = -1;
81 static int hf_egd_pid = -1;
82 static int hf_egd_exid = -1;
83 static int hf_egd_time = -1;
84 static int hf_egd_notime = -1;
85 static int hf_egd_stat = -1;
86 static int hf_egd_csig = -1;
87 static int hf_egd_resv = -1;
88
89 static gint ett_egd = -1;
90 static gint ett_status_item = -1;
91
92 static void dissect_egd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
93 {
94   /* replace UDP with EGD in display */
95   col_set_str(pinfo->cinfo, COL_PROTOCOL, "EGD");
96
97   /* Clear out stuff in the info column */
98   col_clear(pinfo->cinfo, COL_INFO);
99   col_add_fstr(pinfo->cinfo, COL_INFO, "Data Msg: ExchangeID=0x%08X, RequestID=%05u",
100                  tvb_get_letohl(tvb, 8), tvb_get_letohs(tvb, 2));
101
102   if (tree)
103   {
104     proto_item *ti = NULL;
105     proto_item *notime = NULL;
106     proto_tree *egd_tree = NULL;
107     tvbuff_t *next_tvb = NULL;
108     gint offset, data_length;
109     guint32 sectime;
110     nstime_t egd_time;
111
112     memset(&egd_time, 0, sizeof(nstime_t));
113     offset = 0;
114
115     ti = proto_tree_add_item(tree, proto_egd, tvb, 0, -1, ENC_NA);
116     egd_tree = proto_item_add_subtree(ti, ett_egd);
117     proto_tree_add_item(egd_tree, hf_egd_type, tvb, offset, 1, ENC_LITTLE_ENDIAN);
118     offset++;
119     proto_tree_add_item(egd_tree, hf_egd_ver, tvb, offset, 1, ENC_LITTLE_ENDIAN);
120     offset++;
121     proto_tree_add_item(egd_tree, hf_egd_rid, tvb, offset, 2, ENC_LITTLE_ENDIAN);
122     offset += 2;
123     proto_tree_add_item(egd_tree, hf_egd_pid, tvb, offset, 4, ENC_BIG_ENDIAN);
124     offset += 4;
125     proto_tree_add_item(egd_tree, hf_egd_exid, tvb, offset, 4, ENC_LITTLE_ENDIAN);
126     offset += 4;
127
128     /* time */
129     sectime = tvb_get_letohl(tvb, offset);
130     if (0 == sectime)
131     {
132       notime = proto_tree_add_item(egd_tree, hf_egd_notime, tvb, offset, 8, ENC_LITTLE_ENDIAN);
133       proto_item_append_text(notime, "--No TimeStamp");
134     }
135     else
136     {
137       egd_time.secs  = tvb_get_letohl(tvb, offset);
138       egd_time.nsecs = tvb_get_letohl(tvb, offset+4);
139       proto_tree_add_time(egd_tree, hf_egd_time, tvb, offset, 8, &egd_time);
140     }
141     offset += 8;
142
143     proto_tree_add_item(egd_tree, hf_egd_stat, tvb, offset, 4, ENC_LITTLE_ENDIAN);
144     offset += 4;
145     proto_tree_add_item(egd_tree, hf_egd_csig, tvb, offset, 4, ENC_LITTLE_ENDIAN);
146     offset += 4;
147     proto_tree_add_item(egd_tree, hf_egd_resv, tvb, offset, 4, ENC_LITTLE_ENDIAN);
148     offset += 4;
149
150     data_length = tvb_length_remaining(tvb, offset);
151     if (data_length > 0)
152     {
153       next_tvb = tvb_new_subset_remaining(tvb, offset);
154       call_dissector(data_handle, next_tvb, pinfo, egd_tree);
155     }
156   }
157 }
158
159 void proto_register_egd(void)
160 {
161   static hf_register_info hf[] =
162     {
163       { &hf_egd_ver,
164         { "Version", "egd.ver",
165           FT_UINT8, BASE_DEC,
166           NULL, 0x0,
167           NULL, HFILL }
168       },
169       { &hf_egd_type,
170         { "Type", "egd.type",
171           FT_UINT8, BASE_DEC,
172           NULL, 0x0,
173           NULL, HFILL }
174       },
175       { &hf_egd_rid,
176         { "RequestID", "egd.rid",
177           FT_UINT16, BASE_DEC,
178           NULL, 0x0,
179           NULL, HFILL }
180       },
181       { &hf_egd_pid,
182         { "ProducerID", "egd.pid",
183           FT_IPv4, BASE_NONE,
184           NULL, 0x0,
185           NULL, HFILL }
186       },
187       { &hf_egd_exid,
188         { "ExchangeID", "egd.exid",
189           FT_UINT32, BASE_HEX,
190           NULL, 0x0,
191           NULL, HFILL }
192       },
193       { &hf_egd_time,
194         { "Timestamp", "egd.time",
195           FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL,
196           NULL, 0x0,
197           NULL, HFILL }
198       },
199       { &hf_egd_notime,
200         { "Timestamp", "egd.notime",
201           FT_UINT64, BASE_HEX,
202           NULL, 0x0,
203           NULL, HFILL }
204       },
205       { &hf_egd_stat,
206         { "Status", "egd.stat",
207           FT_UINT32, BASE_DEC,
208           VALS(egd_stat_vals), 0x0,
209           NULL, HFILL }
210       },
211       { &hf_egd_csig,
212         { "ConfigSignature", "egd.csig",
213           FT_UINT32, BASE_DEC,
214           NULL, 0x0,
215           NULL, HFILL }
216       },
217       { &hf_egd_resv,
218         { "Reserved", "egd.rsrv",
219           FT_UINT32, BASE_DEC,
220           NULL, 0x0,
221           NULL, HFILL }
222       }
223     };
224
225   static gint *ett[] =
226     {
227       &ett_egd,
228       &ett_status_item
229     };
230
231   proto_egd = proto_register_protocol (
232     "Ethernet Global Data",  /* name */
233     "EGD",                   /* short name */
234     "egd"                    /* abbrev */
235     );
236   proto_register_field_array(proto_egd, hf, array_length(hf));
237   proto_register_subtree_array(ett, array_length(ett));
238 }
239
240 void proto_reg_handoff_egd(void)
241 {
242   dissector_handle_t egd_handle;
243
244   egd_handle = create_dissector_handle(dissect_egd, proto_egd);
245   dissector_add_uint("udp.port", EGD_PORT, egd_handle);
246
247   /* find data dissector */
248   data_handle = find_dissector("data");
249 }
250