Note that we could save some overhead if we require the name argument to
[obnox/wireshark/wip.git] / packet-ypbind.c
1 /* packet-ypbind.c
2  * Routines for ypbind dissection
3  *
4  * $Id: packet-ypbind.c,v 1.9 2002/01/05 21:49:36 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * Copied from packet-smb.c
11  *
12  *    2001  Ronnie Sahlberg, added dissectors for the commands
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37
38
39 #include "packet-rpc.h"
40 #include "packet-ypbind.h"
41
42 static int proto_ypbind = -1;
43 static int hf_ypbind_domain = -1;
44 static int hf_ypbind_resp_type = -1;
45 static int hf_ypbind_error = -1;
46 static int hf_ypbind_addr = -1;
47 static int hf_ypbind_port = -1;
48 static int hf_ypbind_setdom_version = -1;
49
50 static gint ett_ypbind = -1;
51
52
53 static int
54 dissect_ypbind_domain_v2_request(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
55 {
56         /* domain */
57         offset = dissect_rpc_string(tvb, pinfo, tree, 
58                         hf_ypbind_domain, offset, NULL);
59
60         return offset;
61 }
62
63 #define YPBIND_RESP_TYPE_SUCC_VAL       1
64 #define YPBIND_RESP_TYPE_FAIL_VAL       2
65 static const value_string resp_type_vals[] = {
66         {YPBIND_RESP_TYPE_SUCC_VAL,     "SUCC_VAL"},
67         {YPBIND_RESP_TYPE_FAIL_VAL,     "FAIL_VAL"},
68         {0, NULL}
69 };
70
71 #define YPBIND_ERROR_ERR        1
72 #define YPBIND_ERROR_NOSERV     2
73 #define YPBIND_ERROR_RESC       3
74 static const value_string error_vals[] = {
75         {YPBIND_ERROR_ERR,      "Internal error"},
76         {YPBIND_ERROR_NOSERV,   "No bound server for passed domain"},
77         {YPBIND_ERROR_RESC,     "System resource allocation failure"},
78         {0, NULL}
79 };
80
81 static int
82 dissect_ypbind_domain_v2_reply(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
83 {
84         guint32 type;
85         guint32 tid;
86
87         /* response type */
88         type=tvb_get_ntohl(tvb, offset);
89         offset = dissect_rpc_uint32(tvb, pinfo, tree, hf_ypbind_resp_type, offset);
90
91         switch(type){
92         case YPBIND_RESP_TYPE_SUCC_VAL:
93                 /* ip address */
94                 tid=tvb_get_ntohl(tvb,offset);
95                 tid=((tid&0x000000ff)<<24)|((tid&0x0000ff00)<<8)
96                         |((tid&0x00ff0000)>>8)|((tid&0xff000000)>>24);
97                 proto_tree_add_ipv4(tree, hf_ypbind_addr, 
98                         tvb, offset, 4, tid);
99                 offset += 4;
100
101                 /* port */
102                 offset = dissect_rpc_uint32(tvb, pinfo, tree, 
103                                 hf_ypbind_port, offset);
104                 
105                 break;
106         case YPBIND_RESP_TYPE_FAIL_VAL:
107                 /* error */
108                 offset = dissect_rpc_uint32(tvb, pinfo, tree, 
109                                 hf_ypbind_resp_type, offset);
110                 break;
111         }
112
113         return offset;
114 }
115
116 static int
117 dissect_ypbind_setdomain_v2_request(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree)
118 {
119         guint32 tid;
120
121         /* domain */
122         offset = dissect_rpc_string(tvb, pinfo, tree, 
123                         hf_ypbind_domain, offset, NULL);
124
125         /* ip address */
126         tid=tvb_get_ntohl(tvb,offset);
127         tid=((tid&0x000000ff)<<24)|((tid&0x0000ff00)<<8)
128                 |((tid&0x00ff0000)>>8)|((tid&0xff000000)>>24);
129         proto_tree_add_ipv4(tree, hf_ypbind_addr, 
130                 tvb, offset, 4, tid);
131         offset += 4;
132
133         /* port */
134         offset = dissect_rpc_uint32(tvb, pinfo, tree, 
135                         hf_ypbind_port, offset);
136                 
137         /* version */
138         offset = dissect_rpc_uint32(tvb, pinfo, tree, 
139                         hf_ypbind_setdom_version, offset);
140                 
141         return offset;
142 }
143
144
145
146 /* proc number, "proc name", dissect_request, dissect_reply */
147 /* NULL as function pointer means: type of arguments is "void". */
148 static const vsff ypbind1_proc[] = {
149         { YPBINDPROC_NULL,      "NULL",         NULL,                           NULL },
150         { YPBINDPROC_DOMAIN,    "DOMAIN",               NULL,                           NULL },
151         { YPBINDPROC_SETDOM,    "SETDOMAIN",            NULL,                           NULL },
152         { 0,    NULL,           NULL,                           NULL }
153 };
154 /* end of YPBind version 1 */
155
156 static const vsff ypbind2_proc[] = {
157         { YPBINDPROC_NULL,      "NULL",         NULL,                           NULL },
158         { YPBINDPROC_DOMAIN,    "DOMAIN",       
159                 dissect_ypbind_domain_v2_request, dissect_ypbind_domain_v2_reply},
160         { YPBINDPROC_SETDOM,    "SETDOMAIN",
161                 dissect_ypbind_setdomain_v2_request, NULL},
162         { 0,    NULL,       NULL,               NULL }
163 };
164 /* end of YPBind version 2 */
165
166
167 void
168 proto_register_ypbind(void)
169 {
170         static hf_register_info hf[] = {
171                 { &hf_ypbind_domain, {
172                         "Domain", "ypbind.domain", FT_STRING, BASE_DEC,
173                         NULL, 0, "Name of the NIS/YP Domain", HFILL }},
174
175                 { &hf_ypbind_resp_type, {
176                         "Response Type", "ypbind.resp_type", FT_UINT32, BASE_DEC,
177                         VALS(resp_type_vals), 0, "Response type", HFILL }},
178
179                 { &hf_ypbind_error, {
180                         "Error", "ypbind.error", FT_UINT32, BASE_DEC,
181                         VALS(error_vals), 0, "YPBIND Error code", HFILL }},
182
183                 { &hf_ypbind_addr, {
184                         "IP Addr", "ypbind.addr", FT_IPv4, BASE_DEC,
185                         NULL, 0, "IP Address of server", HFILL }},
186
187                 { &hf_ypbind_port, {
188                         "Port", "ypbind.port", FT_UINT32, BASE_DEC,
189                         NULL, 0, "Port to use", HFILL }},
190
191                 { &hf_ypbind_setdom_version, {
192                         "Version", "ypbind.setdom.version", FT_UINT32, BASE_DEC,
193                         NULL, 0, "Version of setdom", HFILL }},
194
195         };
196
197         static gint *ett[] = {
198                 &ett_ypbind,
199         };
200
201         proto_ypbind = proto_register_protocol("Yellow Pages Bind",
202             "YPBIND", "ypbind");
203         proto_register_field_array(proto_ypbind, hf, array_length(hf));
204         proto_register_subtree_array(ett, array_length(ett));
205 }
206
207 void
208 proto_reg_handoff_ypbind(void)
209 {
210         /* Register the protocol as RPC */
211         rpc_init_prog(proto_ypbind, YPBIND_PROGRAM, ett_ypbind);
212         /* Register the procedure tables */
213         rpc_init_proc_table(YPBIND_PROGRAM, 1, ypbind1_proc);
214         rpc_init_proc_table(YPBIND_PROGRAM, 2, ypbind2_proc);
215 }