0eb02a262aa770c187f8ae7831a72d78a6e93929
[obnox/wireshark/wip.git] / packet-stat.c
1 /* packet-stat.c
2  * Routines for stat dissection
3  *
4  * $Id: packet-stat.c,v 1.18 2002/10/23 21:17:03 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * Copied from packet-smb.c
11  *
12  * 2001  Ronnie Sahlberg <See AUTHORS for email>
13  *     Added the dissectors for STAT protocol
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34
35 #include "packet-rpc.h"
36 #include "packet-stat.h"
37
38 static int proto_stat = -1;
39 static int hf_stat_mon_name = -1;
40 static int hf_stat_stat_res = -1;
41 static int hf_stat_stat_res_res = -1;
42 static int hf_stat_stat_res_state = -1;
43 static int hf_stat_state = -1;
44 static int hf_stat_mon = -1;
45 static int hf_stat_mon_id_name = -1;
46 static int hf_stat_my_id = -1;
47 static int hf_stat_my_id_hostname = -1;
48 static int hf_stat_my_id_prog = -1;
49 static int hf_stat_my_id_vers = -1;
50 static int hf_stat_my_id_proc = -1;
51 static int hf_stat_priv = -1;
52 static int hf_stat_stat_chge = -1;
53
54 static gint ett_stat = -1;
55 static gint ett_stat_stat_res = -1;
56 static gint ett_stat_mon = -1;
57 static gint ett_stat_my_id = -1;
58 static gint ett_stat_stat_chge = -1;
59
60 #define STAT_SUCC       0
61 #define STAT_FAIL       1
62
63 static const value_string stat_res[] =
64 {
65         {       0,      "STAT_SUCC" },
66         {       1,      "STAT_FAIL" },
67         {       0,      NULL }
68 };
69
70 /* Calculate length (including padding) of my_id structure.
71  * First read the length of the string and round it upwards to nearest
72  * multiple of 4, then add 16 (4*uint32)
73  */
74 static int
75 my_id_len(tvbuff_t *tvb, int offset)
76 {
77         int len;
78
79         len = tvb_get_ntohl(tvb, offset);
80         if(len&0x03)
81                 len = (len&0xfc)+4;
82
83         len += 16;
84
85         return len;
86 }
87
88 /* Calculate length (including padding) of my_id structure.
89  * First read the length of the string and round it upwards to nearest
90  * multiple of 4, then add 4 (string len) and size of my_id struct.
91  */
92 static int
93 mon_id_len(tvbuff_t *tvb, int offset)
94 {
95         int len;
96
97         len = tvb_get_ntohl(tvb, offset);
98         if(len&0x03){
99                 len = (len&0xfc)+4;
100         }
101
102         len += 4;
103
104         return len+my_id_len(tvb,offset+len);
105 }
106
107 static int
108 dissect_stat_stat(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
109 {
110         if (tree)
111         {
112                 offset = dissect_rpc_string(tvb,tree,hf_stat_mon_name,offset,NULL);
113         }
114
115         return offset;
116 }
117
118 static int
119 dissect_stat_stat_res(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
120 {
121         proto_item* sub_item = NULL;
122         proto_tree* sub_tree = NULL;
123         gint32 res;
124         gint32 state;
125
126         if (tree) {
127                 sub_item = proto_tree_add_item(tree, hf_stat_stat_res, tvb,
128                                 offset, -1, FALSE);
129                 if (sub_item)
130                         sub_tree = proto_item_add_subtree(sub_item, ett_stat_stat_res);
131         }
132
133         res = tvb_get_ntohl(tvb, offset);
134         offset = dissect_rpc_uint32(tvb,sub_tree,hf_stat_stat_res_res,offset);
135
136         if (res==STAT_SUCC) {
137                 state = tvb_get_ntohl(tvb, offset);
138                 offset = dissect_rpc_uint32(tvb,sub_tree,hf_stat_stat_res_state,offset);
139         } else {
140                 offset += 4;
141         }
142
143         return offset;
144 }
145
146 static int
147 dissect_stat_my_id(tvbuff_t *tvb, int offset, proto_tree *tree)
148 {
149         proto_item* sub_item = NULL;
150         proto_tree* sub_tree = NULL;
151
152         if (tree) {
153                 sub_item = proto_tree_add_item(tree, hf_stat_my_id, tvb,
154                                 offset, my_id_len(tvb,offset), FALSE);
155                 if (sub_item)
156                         sub_tree = proto_item_add_subtree(sub_item, ett_stat_my_id);
157         }
158
159         offset = dissect_rpc_string(tvb,sub_tree,hf_stat_my_id_hostname,offset,NULL);
160         offset = dissect_rpc_uint32(tvb,sub_tree,hf_stat_my_id_prog,offset);
161         offset = dissect_rpc_uint32(tvb,sub_tree,hf_stat_my_id_vers,offset);
162         offset = dissect_rpc_uint32(tvb,sub_tree,hf_stat_my_id_proc,offset);
163
164         return offset;
165 }
166
167 static int
168 dissect_stat_mon_id(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
169 {
170         proto_item* sub_item = NULL;
171         proto_tree* sub_tree = NULL;
172
173         if (tree) {
174                 sub_item = proto_tree_add_item(tree, hf_stat_mon, tvb,
175                                 offset, mon_id_len(tvb,offset), FALSE);
176                 if (sub_item)
177                         sub_tree = proto_item_add_subtree(sub_item, ett_stat_mon);
178         }
179
180
181         offset = dissect_rpc_string(tvb,sub_tree,hf_stat_mon_id_name,offset,NULL);
182
183         offset = dissect_stat_my_id(tvb,offset,sub_tree);
184
185         return offset;
186 }
187
188 static int
189 dissect_stat_priv(tvbuff_t *tvb, int offset, proto_tree *tree)
190 {
191         proto_tree_add_item(tree, hf_stat_priv, tvb, offset, 16, FALSE);
192         offset += 16;
193
194         return offset;
195 }
196
197 static int
198 dissect_stat_mon(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
199 {
200
201         offset = dissect_stat_mon_id(tvb,offset,pinfo,tree);
202
203         offset = dissect_stat_priv(tvb,offset,tree);
204         return offset;
205 }
206
207 static int
208 dissect_stat_state(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
209 {
210         offset = dissect_rpc_uint32(tvb,tree,hf_stat_state,offset);
211
212         return offset;
213 }
214
215 static int
216 dissect_stat_notify(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
217 {
218         proto_item* sub_item = NULL;
219         proto_tree* sub_tree = NULL;
220         int start_offset = offset;
221
222         if (tree) {
223                 sub_item = proto_tree_add_item(tree, hf_stat_stat_chge, tvb,
224                                 offset, -1, FALSE);
225                 if (sub_item)
226                         sub_tree = proto_item_add_subtree(sub_item, ett_stat_stat_chge);
227         }
228
229         offset = dissect_rpc_string(tvb,sub_tree,hf_stat_mon_id_name,offset,NULL);
230
231         offset = dissect_rpc_uint32(tvb,tree,hf_stat_state,offset);
232
233         if(sub_item)
234                 proto_item_set_len(sub_item, offset - start_offset);
235
236         return offset;
237 }
238
239 static int
240 dissect_stat_umon_all(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
241 {
242         offset = dissect_stat_my_id(tvb,offset,tree);
243
244         return offset;
245 }
246
247 /* proc number, "proc name", dissect_request, dissect_reply */
248 /* NULL as function pointer means: type of arguments is "void". */
249
250 static const vsff stat_proc[] = {
251     { 0, "NULL", NULL, NULL },
252     { STATPROC_STAT,   "STAT",
253                 dissect_stat_stat, dissect_stat_stat_res },
254     { STATPROC_MON,   "MON",
255                 dissect_stat_mon, dissect_stat_stat_res },
256     { STATPROC_UNMON, "UNMON",
257                 dissect_stat_mon_id, dissect_stat_state },
258     { STATPROC_UNMON_ALL, "UNMON_ALL",
259                 dissect_stat_umon_all, dissect_stat_state },
260     { STATPROC_SIMU_CRASH, "SIMU_CRASH",
261                 NULL, NULL },
262     { STATPROC_NOTIFY, "NOTIFY",
263                 dissect_stat_notify, NULL },
264     { 0, NULL, NULL, NULL }
265 };
266 /* end of stat version 1 */
267
268
269 void
270 proto_register_stat(void)
271 {
272         static hf_register_info hf[] = {
273                 { &hf_stat_mon_name, {
274                         "Name", "stat.name", FT_STRING, BASE_DEC,
275                         NULL, 0, "Name", HFILL }},
276                 { &hf_stat_stat_res, {
277                         "Status Result", "stat.stat_res", FT_NONE,0,
278                         NULL, 0, "Status Result", HFILL }},
279                 { &hf_stat_stat_res_res, {
280                         "Result", "stat.stat_res.res", FT_UINT32, BASE_DEC,
281                         VALS(stat_res), 0, "Result", HFILL }},
282                 { &hf_stat_stat_res_state, {
283                         "State", "stat.stat_res.state", FT_UINT32, BASE_DEC,
284                         NULL, 0, "State", HFILL }},
285                 { &hf_stat_mon, {
286                         "Monitor", "stat.mon", FT_NONE, 0,
287                         NULL, 0, "Monitor Host", HFILL }},
288                 { &hf_stat_mon_id_name, {
289                         "Monitor ID Name", "stat.mon_id.name", FT_STRING, BASE_DEC,
290                         NULL, 0, "Monitor ID Name", HFILL }},
291                 { &hf_stat_my_id, {
292                         "My ID", "stat.my_id", FT_NONE,0,
293                         NULL, 0, "My_ID structure", HFILL }},
294                 { &hf_stat_my_id_hostname, {
295                         "Hostname", "stat.my_id.hostname", FT_STRING, BASE_DEC,
296                         NULL, 0, "My_ID Host to callback", HFILL }},
297                 { &hf_stat_my_id_prog, {
298                         "Program", "stat.my_id.prog", FT_UINT32, BASE_DEC,
299                         NULL, 0, "My_ID Program to callback", HFILL }},
300                 { &hf_stat_my_id_vers, {
301                         "Version", "stat.my_id.vers", FT_UINT32, BASE_DEC,
302                         NULL, 0, "My_ID Version of callback", HFILL }},
303                 { &hf_stat_my_id_proc, {
304                         "Procedure", "stat.my_id.proc", FT_UINT32, BASE_DEC,
305                         NULL, 0, "My_ID Procedure to callback", HFILL }},
306                 { &hf_stat_priv, {
307                         "Priv", "stat.priv", FT_BYTES, BASE_HEX,
308                         NULL, 0, "Private client supplied opaque data", HFILL }},
309                 { &hf_stat_state, {
310                         "State", "stat.state", FT_UINT32, BASE_DEC,
311                         NULL, 0, "State of local NSM", HFILL }},
312                 { &hf_stat_stat_chge, {
313                         "Status Change", "stat.stat_chge", FT_NONE, 0,
314                         NULL, 0, "Status Change structure", HFILL }},
315         };
316
317         static gint *ett[] = {
318                 &ett_stat,
319                 &ett_stat_stat_res,
320                 &ett_stat_mon,
321                 &ett_stat_my_id,
322                 &ett_stat_stat_chge,
323         };
324
325         proto_stat = proto_register_protocol("Network Status Monitor Protocol", "STAT", "stat");
326         proto_register_field_array(proto_stat, hf, array_length(hf));
327         proto_register_subtree_array(ett, array_length(ett));
328 }
329
330 void
331 proto_reg_handoff_stat(void)
332 {
333         /* Register the protocol as RPC */
334         rpc_init_prog(proto_stat, STAT_PROGRAM, ett_stat);
335         /* Register the procedure tables */
336         rpc_init_proc_table(STAT_PROGRAM, 1, stat_proc, -1);
337 }