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