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