Final updates for 0.9.14.
[metze/wireshark/wip.git] / packet-ypserv.c
1 /* packet-ypserv.c
2  * Routines for ypserv dissection
3  *
4  * $Id: packet-ypserv.c,v 1.26 2002/10/24 20:59:21 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 all remaining dissectors for this 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
36 #include "packet-rpc.h"
37 #include "packet-ypserv.h"
38
39 static int proto_ypserv = -1;
40 static int hf_ypserv_procedure_v1 = -1;
41 static int hf_ypserv_procedure_v2 = -1;
42 static int hf_ypserv_domain = -1;
43 static int hf_ypserv_servesdomain = -1;
44 static int hf_ypserv_map = -1;
45 static int hf_ypserv_key = -1;
46 static int hf_ypserv_peer = -1;
47 static int hf_ypserv_more = -1;
48 static int hf_ypserv_ordernum = -1;
49 static int hf_ypserv_transid = -1;
50 static int hf_ypserv_prog = -1;
51 static int hf_ypserv_port = -1;
52 static int hf_ypserv_value = -1;
53 static int hf_ypserv_status = -1;
54 static int hf_ypserv_map_parms = -1;
55 static int hf_ypserv_xfrstat = -1;
56
57 static gint ett_ypserv = -1;
58 static gint ett_ypserv_map_parms = -1;
59
60 static const value_string ypstat[] =
61 {
62         {       1,      "YP_TRUE"       },
63         {       2,      "YP_NOMORE"     },
64         {       0,      "YP_FALSE"      },
65         {       -1,     "YP_NOMAP"      },
66         {       -2,     "YP_NODOM"      },
67         {       -3,     "YP_NOKEY"      },
68         {       -4,     "YP_BADOP"      },
69         {       -5,     "YP_BADDB"      },
70         {       -6,     "YP_YPERR"      },
71         {       -7,     "YP_BADARGS"    },
72         {       -8,     "YP_VERS"       },
73         {       0,      NULL    },
74 };
75
76 static const value_string xfrstat[] =
77 {
78         {       1,      "YPXFR_SUCC"    },
79         {       2,      "YPXFR_AGE"     },
80         {       -1,     "YPXFR_NOMAP"   },
81         {       -2,     "YPXFR_NODOM"   },
82         {       -3,     "YPXFR_RSRC"    },
83         {       -4,     "YPXFR_RPC"     },
84         {       -5,     "YPXFR_MADDR"   },
85         {       -6,     "YPXFR_YPERR"   },
86         {       -7,     "YPXFR_BADARGS" },
87         {       -8,     "YPXFR_DBM"     },
88         {       -9,     "YPXFR_FILE"    },
89         {       -10,    "YPXFR_SKEW"    },
90         {       -11,    "YPXFR_CLEAR"   },
91         {       -12,    "YPXFR_FORCE"   },
92         {       -13,    "YPXFR_XFRERR"  },
93         {       -14,    "YPXFR_REFUSED" },
94         {       0,      NULL    },
95 };
96
97 static int
98 dissect_domain_call(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
99 {
100         if ( tree )
101         {
102                 offset = dissect_rpc_string(tvb,tree,hf_ypserv_domain,offset,NULL);
103         }
104
105         return offset;
106 }
107
108 static int
109 dissect_domain_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
110 {
111         if ( tree )
112         {
113                 proto_tree_add_boolean(tree, hf_ypserv_servesdomain, tvb,
114                         offset, 4, tvb_get_ntohl(tvb,offset));
115         }
116
117         offset += 4;
118         return offset;
119 }
120
121 static int
122 dissect_match_call(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
123 {
124         if ( tree )
125         {
126                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_domain, offset, NULL);
127                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_map, offset, NULL);
128                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_key, offset, NULL);
129         }
130
131         return offset;
132 }
133
134 static int
135 dissect_match_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
136 {
137         if ( tree )
138         {
139                 offset = dissect_rpc_uint32(tvb, tree, hf_ypserv_status, offset);
140
141                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_value,offset, NULL);
142         }
143
144         return offset;
145 }
146
147
148 static int
149 dissect_first_call(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
150 {
151         /*
152          * XXX - does Sun's "yp.x" lie, and claim that the argument to a
153          * FIRST call is a "ypreq_key" rather than a "ypreq_nokey"?
154          * You presumably need the key for NEXT, as "next" is "next
155          * after some entry", and the key tells you which entry, but
156          * you don't need a key for FIRST, as there's only one entry that
157          * is the first entry.
158          *
159          * The NIS server originally used DBM, which has a "firstkey()"
160          * call, with no argument, and a "nextkey()" argument, with
161          * a key argument.  (Heck, it might *still* use DBM.)
162          *
163          * Given that, and given that at least one FIRST call from a Sun
164          * running Solaris 8 (the Sun on which I'm typing this, in fact)
165          * had a "ypreq_nokey" as the argument, I'm assuming that "yp.x"
166          * is buggy.
167          */
168
169         if ( tree )
170         {
171                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_domain, offset, NULL);
172                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_map, offset, NULL);
173         }
174
175         return offset;
176 }
177
178
179 static int
180 dissect_firstnext_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
181 {
182         if ( tree )
183         {
184                 offset = dissect_rpc_uint32(tvb, tree, hf_ypserv_status, offset);
185
186                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_value, offset, NULL);
187                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_key, offset, NULL);
188         }
189
190         return offset;
191 }
192
193
194 static int
195 dissect_next_call(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
196 {
197         if ( tree )
198         {
199                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_domain, offset, NULL);
200                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_map, offset, NULL);
201                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_key, offset, NULL);
202         }
203
204         return offset;
205 }
206
207 static int
208 dissect_xfr_call(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
209 {
210         proto_item *sub_item=NULL;
211         proto_tree *sub_tree=NULL;
212         int start_offset = offset;
213
214         if(tree){
215                 sub_item = proto_tree_add_item(tree, hf_ypserv_map_parms, tvb,
216                                 offset, -1, FALSE);
217                 if(sub_item)
218                         sub_tree = proto_item_add_subtree(sub_item, ett_ypserv_map_parms);
219         }
220
221         offset = dissect_rpc_string(tvb, sub_tree, hf_ypserv_domain, offset, NULL);
222
223         offset = dissect_rpc_string(tvb, sub_tree, hf_ypserv_map, offset, NULL);
224
225         offset = dissect_rpc_uint32(tvb, sub_tree, hf_ypserv_ordernum, offset);
226
227         offset = dissect_rpc_string(tvb, sub_tree, hf_ypserv_peer, offset, NULL);
228
229         proto_tree_add_item(tree, hf_ypserv_transid, tvb, offset, 4, FALSE);
230         offset += 4;
231
232         offset = dissect_rpc_uint32(tvb, tree, hf_ypserv_prog, offset);
233         offset = dissect_rpc_uint32(tvb, tree, hf_ypserv_port, offset);
234
235         if(sub_item)
236                 proto_item_set_len(sub_item, offset - start_offset);
237
238         return offset;
239 }
240
241 static int
242 dissect_xfr_reply(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
243 {
244         proto_tree_add_item(tree, hf_ypserv_transid, tvb, offset, 4, FALSE);
245         offset += 4;
246
247         offset = dissect_rpc_uint32(tvb, tree, hf_ypserv_xfrstat, offset);
248
249         return offset;
250 }
251
252 static int
253 dissect_ypreq_nokey(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
254 {
255
256         offset = dissect_rpc_string(tvb, tree, hf_ypserv_domain, offset, NULL);
257
258         offset = dissect_rpc_string(tvb, tree, hf_ypserv_map, offset, NULL);
259
260         return offset;
261 }
262
263 static int
264 dissect_ypresp_all(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
265 {
266         guint32 more;
267
268         for (;;) {
269                 more = tvb_get_ntohl(tvb, offset);
270
271                 offset = dissect_rpc_uint32(tvb, tree, hf_ypserv_more, offset);
272                 if (!more)
273                         break;
274                 offset = dissect_rpc_uint32(tvb, tree, hf_ypserv_status, offset);
275                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_value, offset, NULL);
276                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_key, offset, NULL);
277         }
278
279         return offset;
280 }
281
282 static int
283 dissect_ypresp_master(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
284 {
285
286         offset = dissect_rpc_uint32(tvb, tree, hf_ypserv_status, offset);
287
288         offset = dissect_rpc_string(tvb, tree, hf_ypserv_peer, offset, NULL);
289
290         return offset;
291 }
292
293
294 static int
295 dissect_ypresp_order(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
296 {
297
298         offset = dissect_rpc_uint32(tvb, tree, hf_ypserv_status, offset);
299
300         offset = dissect_rpc_uint32(tvb, tree, hf_ypserv_ordernum, offset);
301
302         return offset;
303 }
304
305
306 static int
307 dissect_ypresp_maplist(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, proto_tree *tree)
308 {
309         offset = dissect_rpc_uint32(tvb, tree, hf_ypserv_status, offset);
310         while(tvb_get_ntohl(tvb,offset)){
311                 offset = dissect_rpc_uint32(tvb, tree, hf_ypserv_more, offset);
312                 offset = dissect_rpc_string(tvb, tree, hf_ypserv_map, offset, NULL);
313
314         }
315         offset = dissect_rpc_uint32(tvb, tree, hf_ypserv_more, offset);
316         return offset;
317 }
318
319
320 /* proc number, "proc name", dissect_request, dissect_reply */
321 /* NULL as function pointer means: type of arguments is "void". */
322
323 /* someone please get me a version 1 trace */
324
325 static const vsff ypserv1_proc[] = {
326     { 0, "NULL", NULL, NULL },
327     { YPPROC_DOMAIN, "DOMAIN",
328                 NULL, NULL },
329     { YPPROC_DOMAIN_NONACK, "DOMAIN_NONACK",
330                 NULL, NULL },
331     { YPPROC_MATCH, "MATCH",
332                 NULL, NULL },
333     { YPPROC_FIRST, "FIRST",
334                 NULL, NULL },
335     { YPPROC_NEXT,  "NEXT",
336                 NULL, NULL },
337     { YPPROC_XFR,   "XFR",
338                 NULL, NULL },
339     { YPPROC_CLEAR, "CLEAR",
340                 NULL, NULL },
341     { YPPROC_ALL,   "ALL",
342                 NULL, NULL },
343     { YPPROC_MASTER,    "MASTER",
344                 NULL, NULL },
345     { YPPROC_ORDER, "ORDER",
346                 NULL, NULL },
347     { YPPROC_MAPLIST,   "MAPLIST",
348                 NULL, NULL },
349     { 0, NULL, NULL, NULL }
350 };
351
352 static const value_string ypserv1_proc_vals[] = {
353     { YPPROC_DOMAIN, "DOMAIN" },
354     { YPPROC_DOMAIN_NONACK, "DOMAIN_NONACK" },
355     { YPPROC_MATCH, "MATCH" },
356     { YPPROC_FIRST, "FIRST" },
357     { YPPROC_NEXT,  "NEXT" },
358     { YPPROC_XFR,   "XFR" },
359     { YPPROC_CLEAR, "CLEAR" },
360     { YPPROC_ALL,   "ALL" },
361     { YPPROC_MASTER,    "MASTER" },
362     { YPPROC_ORDER, "ORDER" },
363     { YPPROC_MAPLIST,   "MAPLIST" },
364     { 0, NULL }
365 };
366
367 /* end of YPServ version 1 */
368
369 /* proc number, "proc name", dissect_request, dissect_reply */
370 /* NULL as function pointer means: type of arguments is "void". */
371
372 static const vsff ypserv2_proc[] = {
373     { 0, "NULL", NULL, NULL },
374     { YPPROC_DOMAIN, "DOMAIN",
375                 dissect_domain_call, dissect_domain_reply },
376     { YPPROC_DOMAIN_NONACK, "DOMAIN_NONACK",
377                 dissect_domain_call, dissect_domain_reply },
378     { YPPROC_MATCH, "MATCH",
379                 dissect_match_call, dissect_match_reply },
380     { YPPROC_FIRST, "FIRST",
381                 dissect_first_call, dissect_firstnext_reply },
382     { YPPROC_NEXT,  "NEXT",
383                 dissect_next_call, dissect_firstnext_reply },
384     { YPPROC_XFR,   "XFR",
385                 dissect_xfr_call, dissect_xfr_reply },
386     { YPPROC_CLEAR, "CLEAR",
387                 NULL, NULL },
388     { YPPROC_ALL,   "ALL",
389                 dissect_ypreq_nokey, dissect_ypresp_all },
390     { YPPROC_MASTER,    "MASTER",
391                 dissect_ypreq_nokey, dissect_ypresp_master },
392     { YPPROC_ORDER, "ORDER",
393                 dissect_ypreq_nokey, dissect_ypresp_order },
394     { YPPROC_MAPLIST,   "MAPLIST",
395                 dissect_domain_call, dissect_ypresp_maplist },
396     { 0, NULL, NULL, NULL }
397 };
398
399 static const value_string ypserv2_proc_vals[] = {
400     { YPPROC_DOMAIN, "DOMAIN" },
401     { YPPROC_DOMAIN_NONACK, "DOMAIN_NONACK" },
402     { YPPROC_MATCH, "MATCH" },
403     { YPPROC_FIRST, "FIRST" },
404     { YPPROC_NEXT,  "NEXT" },
405     { YPPROC_XFR,   "XFR" },
406     { YPPROC_CLEAR, "CLEAR" },
407     { YPPROC_ALL,   "ALL" },
408     { YPPROC_MASTER,    "MASTER" },
409     { YPPROC_ORDER, "ORDER" },
410     { YPPROC_MAPLIST,   "MAPLIST" },
411     { 0, NULL }
412 };
413
414 /* end of YPServ version 2 */
415
416
417 void
418 proto_register_ypserv(void)
419 {
420         /*static struct true_false_string okfailed = { "Ok", "Failed" };*/
421         static struct true_false_string yesno = { "Yes", "No" };
422
423         static hf_register_info hf[] = {
424                 { &hf_ypserv_procedure_v1, {
425                         "V1 Procedure", "ypserv.procedure_v1", FT_UINT32, BASE_DEC,
426                         VALS(ypserv1_proc_vals), 0, "V1 Procedure", HFILL }},
427                 { &hf_ypserv_procedure_v2, {
428                         "V2 Procedure", "ypserv.procedure_v2", FT_UINT32, BASE_DEC,
429                         VALS(ypserv2_proc_vals), 0, "V2 Procedure", HFILL }},
430                 { &hf_ypserv_domain, {
431                         "Domain", "ypserv.domain", FT_STRING, BASE_DEC,
432                         NULL, 0, "Domain", HFILL }},
433                 { &hf_ypserv_servesdomain, {
434                         "Serves Domain", "ypserv.servesdomain", FT_BOOLEAN, BASE_DEC,
435                         &yesno, 0, "Serves Domain", HFILL }},
436                 { &hf_ypserv_map, {
437                         "Map Name", "ypserv.map", FT_STRING, BASE_DEC,
438                         NULL, 0, "Map Name", HFILL }},
439                 { &hf_ypserv_peer, {
440                         "Peer Name", "ypserv.peer", FT_STRING, BASE_DEC,
441                         NULL, 0, "Peer Name", HFILL }},
442                 { &hf_ypserv_more, {
443                         "More", "ypserv.more", FT_BOOLEAN, BASE_NONE,
444                         &yesno, 0, "More", HFILL }},
445                 { &hf_ypserv_ordernum, {
446                         "Order Number", "ypserv.ordernum", FT_UINT32, BASE_DEC,
447                         NULL, 0, "Order Number for XFR", HFILL }},
448                 { &hf_ypserv_transid, {
449                         "Host Transport ID", "ypserv.transid", FT_IPv4, BASE_DEC,
450                         NULL, 0, "Host Transport ID to use for XFR Callback", HFILL }},
451                 { &hf_ypserv_prog, {
452                         "Program Number", "ypserv.prog", FT_UINT32, BASE_DEC,
453                         NULL, 0, "Program Number to use for XFR Callback", HFILL }},
454                 { &hf_ypserv_port, {
455                         "Port", "ypserv.port", FT_UINT32, BASE_DEC,
456                         NULL, 0, "Port to use for XFR Callback", HFILL }},
457                 { &hf_ypserv_key, {
458                         "Key", "ypserv.key", FT_STRING, BASE_DEC,
459                         NULL, 0, "Key", HFILL }},
460                 { &hf_ypserv_value, {
461                         "Value", "ypserv.value", FT_STRING, BASE_DEC,
462                         NULL, 0, "Value", HFILL }},
463                 { &hf_ypserv_status, {
464                         "Status", "ypserv.status", FT_INT32, BASE_DEC,
465                         VALS(ypstat) , 0, "Status", HFILL }},
466                 { &hf_ypserv_map_parms, {
467                         "YP Map Parameters", "ypserv.map_parms", FT_NONE, BASE_DEC,
468                         NULL, 0, "YP Map Parameters", HFILL }},
469                 { &hf_ypserv_xfrstat, {
470                         "Xfrstat", "ypserv.xfrstat", FT_INT32, BASE_DEC,
471                         VALS(xfrstat), 0, "Xfrstat", HFILL }},
472         };
473         static gint *ett[] = {
474                 &ett_ypserv,
475                 &ett_ypserv_map_parms,
476         };
477
478         proto_ypserv = proto_register_protocol("Yellow Pages Service",
479             "YPSERV", "ypserv");
480         proto_register_field_array(proto_ypserv, hf, array_length(hf));
481         proto_register_subtree_array(ett, array_length(ett));
482 }
483
484 void
485 proto_reg_handoff_ypserv(void)
486 {
487         /* Register the protocol as RPC */
488         rpc_init_prog(proto_ypserv, YPSERV_PROGRAM, ett_ypserv);
489         /* Register the procedure tables */
490         rpc_init_proc_table(YPSERV_PROGRAM, 1, ypserv1_proc,
491             hf_ypserv_procedure_v1);
492         rpc_init_proc_table(YPSERV_PROGRAM, 2, ypserv2_proc,
493             hf_ypserv_procedure_v2);
494 }