rpc_generic.c revision 1.13 1 1.13 yamt /* $NetBSD: rpc_generic.c,v 1.13 2003/06/07 07:41:41 yamt Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 1.1 fvdl * unrestricted use provided that this legend is included on all tape
6 1.1 fvdl * media and as a part of the software program in whole or part. Users
7 1.1 fvdl * may copy or modify Sun RPC without charge, but are not authorized
8 1.1 fvdl * to license or distribute it to anyone else except as part of a product or
9 1.1 fvdl * program developed by the user.
10 1.1 fvdl *
11 1.1 fvdl * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 1.1 fvdl * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 1.1 fvdl * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 1.1 fvdl *
15 1.1 fvdl * Sun RPC is provided with no support and without any obligation on the
16 1.1 fvdl * part of Sun Microsystems, Inc. to assist in its use, correction,
17 1.1 fvdl * modification or enhancement.
18 1.1 fvdl *
19 1.1 fvdl * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 1.1 fvdl * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 1.1 fvdl * OR ANY PART THEREOF.
22 1.1 fvdl *
23 1.1 fvdl * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 1.1 fvdl * or profits or other special, indirect and consequential damages, even if
25 1.1 fvdl * Sun has been advised of the possibility of such damages.
26 1.1 fvdl *
27 1.1 fvdl * Sun Microsystems, Inc.
28 1.1 fvdl * 2550 Garcia Avenue
29 1.1 fvdl * Mountain View, California 94043
30 1.1 fvdl */
31 1.1 fvdl /*
32 1.1 fvdl * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33 1.1 fvdl */
34 1.1 fvdl
35 1.1 fvdl /* #pragma ident "@(#)rpc_generic.c 1.17 94/04/24 SMI" */
36 1.1 fvdl
37 1.1 fvdl /*
38 1.1 fvdl * rpc_generic.c, Miscl routines for RPC.
39 1.1 fvdl *
40 1.1 fvdl */
41 1.1 fvdl
42 1.4 kleink #include "namespace.h"
43 1.12 thorpej #include "reentrant.h"
44 1.1 fvdl #include <sys/types.h>
45 1.1 fvdl #include <sys/param.h>
46 1.1 fvdl #include <sys/socket.h>
47 1.1 fvdl #include <sys/un.h>
48 1.1 fvdl #include <sys/resource.h>
49 1.1 fvdl #include <netinet/in.h>
50 1.1 fvdl #include <arpa/inet.h>
51 1.1 fvdl #include <rpc/rpc.h>
52 1.5 lukem #include <assert.h>
53 1.1 fvdl #include <ctype.h>
54 1.1 fvdl #include <stdio.h>
55 1.1 fvdl #include <netdb.h>
56 1.1 fvdl #include <netconfig.h>
57 1.1 fvdl #include <malloc.h>
58 1.1 fvdl #include <string.h>
59 1.2 assar #include <syslog.h>
60 1.1 fvdl #include <rpc/nettype.h>
61 1.10 fvdl #include "rpc_internal.h"
62 1.1 fvdl
63 1.1 fvdl struct handle {
64 1.1 fvdl NCONF_HANDLE *nhandle;
65 1.1 fvdl int nflag; /* Whether NETPATH or NETCONFIG */
66 1.1 fvdl int nettype;
67 1.1 fvdl };
68 1.1 fvdl
69 1.6 jdolecek static const struct _rpcnettype {
70 1.1 fvdl const char *name;
71 1.1 fvdl const int type;
72 1.1 fvdl } _rpctypelist[] = {
73 1.1 fvdl { "netpath", _RPC_NETPATH },
74 1.1 fvdl { "visible", _RPC_VISIBLE },
75 1.1 fvdl { "circuit_v", _RPC_CIRCUIT_V },
76 1.1 fvdl { "datagram_v", _RPC_DATAGRAM_V },
77 1.1 fvdl { "circuit_n", _RPC_CIRCUIT_N },
78 1.1 fvdl { "datagram_n", _RPC_DATAGRAM_N },
79 1.1 fvdl { "tcp", _RPC_TCP },
80 1.1 fvdl { "udp", _RPC_UDP },
81 1.1 fvdl { 0, _RPC_NONE }
82 1.1 fvdl };
83 1.1 fvdl
84 1.1 fvdl struct netid_af {
85 1.1 fvdl const char *netid;
86 1.1 fvdl int af;
87 1.1 fvdl int protocol;
88 1.1 fvdl };
89 1.1 fvdl
90 1.6 jdolecek static const struct netid_af na_cvt[] = {
91 1.1 fvdl { "udp", AF_INET, IPPROTO_UDP },
92 1.1 fvdl { "tcp", AF_INET, IPPROTO_TCP },
93 1.1 fvdl #ifdef INET6
94 1.1 fvdl { "udp6", AF_INET6, IPPROTO_UDP },
95 1.1 fvdl { "tcp6", AF_INET6, IPPROTO_TCP },
96 1.1 fvdl #endif
97 1.1 fvdl { "local", AF_LOCAL, 0 }
98 1.1 fvdl };
99 1.1 fvdl
100 1.3 christos #if 0
101 1.1 fvdl static char *strlocase __P((char *));
102 1.3 christos #endif
103 1.3 christos static int getnettype __P((const char *));
104 1.1 fvdl
105 1.1 fvdl /*
106 1.1 fvdl * Cache the result of getrlimit(), so we don't have to do an
107 1.1 fvdl * expensive call every time.
108 1.1 fvdl */
109 1.1 fvdl int
110 1.1 fvdl __rpc_dtbsize()
111 1.1 fvdl {
112 1.1 fvdl static int tbsize;
113 1.1 fvdl struct rlimit rl;
114 1.1 fvdl
115 1.1 fvdl if (tbsize) {
116 1.1 fvdl return (tbsize);
117 1.1 fvdl }
118 1.1 fvdl if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
119 1.3 christos return (tbsize = (int)rl.rlim_max);
120 1.1 fvdl }
121 1.1 fvdl /*
122 1.1 fvdl * Something wrong. I'll try to save face by returning a
123 1.1 fvdl * pessimistic number.
124 1.1 fvdl */
125 1.1 fvdl return (32);
126 1.1 fvdl }
127 1.1 fvdl
128 1.1 fvdl
129 1.1 fvdl /*
130 1.1 fvdl * Find the appropriate buffer size
131 1.1 fvdl */
132 1.1 fvdl u_int
133 1.3 christos /*ARGSUSED*/
134 1.1 fvdl __rpc_get_t_size(af, proto, size)
135 1.1 fvdl int af, proto;
136 1.1 fvdl int size; /* Size requested */
137 1.1 fvdl {
138 1.13 yamt int maxsize, defsize;
139 1.1 fvdl
140 1.13 yamt maxsize = 256 * 1024; /* XXX */
141 1.1 fvdl switch (proto) {
142 1.1 fvdl case IPPROTO_TCP:
143 1.13 yamt defsize = 64 * 1024; /* XXX */
144 1.1 fvdl break;
145 1.1 fvdl case IPPROTO_UDP:
146 1.13 yamt defsize = UDPMSGSIZE;
147 1.1 fvdl break;
148 1.1 fvdl default:
149 1.13 yamt defsize = RPC_MAXDATASIZE;
150 1.1 fvdl break;
151 1.1 fvdl }
152 1.1 fvdl if (size == 0)
153 1.13 yamt return defsize;
154 1.1 fvdl
155 1.1 fvdl /* Check whether the value is within the upper max limit */
156 1.1 fvdl return (size > maxsize ? (u_int)maxsize : (u_int)size);
157 1.1 fvdl }
158 1.1 fvdl
159 1.1 fvdl /*
160 1.1 fvdl * Find the appropriate address buffer size
161 1.1 fvdl */
162 1.1 fvdl u_int
163 1.1 fvdl __rpc_get_a_size(af)
164 1.1 fvdl int af;
165 1.1 fvdl {
166 1.1 fvdl switch (af) {
167 1.1 fvdl case AF_INET:
168 1.1 fvdl return sizeof (struct sockaddr_in);
169 1.1 fvdl #ifdef INET6
170 1.1 fvdl case AF_INET6:
171 1.1 fvdl return sizeof (struct sockaddr_in6);
172 1.1 fvdl #endif
173 1.1 fvdl case AF_LOCAL:
174 1.1 fvdl return sizeof (struct sockaddr_un);
175 1.1 fvdl default:
176 1.1 fvdl break;
177 1.1 fvdl }
178 1.1 fvdl return ((u_int)RPC_MAXADDRSIZE);
179 1.1 fvdl }
180 1.1 fvdl
181 1.3 christos #if 0
182 1.1 fvdl static char *
183 1.1 fvdl strlocase(p)
184 1.1 fvdl char *p;
185 1.1 fvdl {
186 1.1 fvdl char *t = p;
187 1.1 fvdl
188 1.5 lukem _DIAGASSERT(p != NULL);
189 1.5 lukem
190 1.1 fvdl for (; *p; p++)
191 1.1 fvdl if (isupper(*p))
192 1.1 fvdl *p = tolower(*p);
193 1.1 fvdl return (t);
194 1.1 fvdl }
195 1.3 christos #endif
196 1.1 fvdl
197 1.1 fvdl /*
198 1.1 fvdl * Returns the type of the network as defined in <rpc/nettype.h>
199 1.1 fvdl * If nettype is NULL, it defaults to NETPATH.
200 1.1 fvdl */
201 1.1 fvdl static int
202 1.1 fvdl getnettype(nettype)
203 1.3 christos const char *nettype;
204 1.1 fvdl {
205 1.1 fvdl int i;
206 1.1 fvdl
207 1.1 fvdl if ((nettype == NULL) || (nettype[0] == NULL)) {
208 1.1 fvdl return (_RPC_NETPATH); /* Default */
209 1.1 fvdl }
210 1.1 fvdl
211 1.3 christos #if 0
212 1.1 fvdl nettype = strlocase(nettype);
213 1.3 christos #endif
214 1.1 fvdl for (i = 0; _rpctypelist[i].name; i++)
215 1.3 christos if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
216 1.1 fvdl return (_rpctypelist[i].type);
217 1.1 fvdl }
218 1.1 fvdl return (_rpctypelist[i].type);
219 1.1 fvdl }
220 1.1 fvdl
221 1.1 fvdl /*
222 1.1 fvdl * For the given nettype (tcp or udp only), return the first structure found.
223 1.1 fvdl * This should be freed by calling freenetconfigent()
224 1.1 fvdl */
225 1.12 thorpej
226 1.12 thorpej #ifdef _REENTRANT
227 1.12 thorpej static thread_key_t tcp_key, udp_key;
228 1.12 thorpej static once_t __rpc_getconfigp_once = ONCE_INITIALIZER;
229 1.12 thorpej
230 1.12 thorpej static void
231 1.12 thorpej __rpc_getconfigp_setup(void)
232 1.12 thorpej {
233 1.12 thorpej
234 1.12 thorpej thr_keycreate(&tcp_key, free);
235 1.12 thorpej thr_keycreate(&udp_key, free);
236 1.12 thorpej }
237 1.12 thorpej #endif
238 1.12 thorpej
239 1.1 fvdl struct netconfig *
240 1.1 fvdl __rpc_getconfip(nettype)
241 1.3 christos const char *nettype;
242 1.1 fvdl {
243 1.1 fvdl char *netid;
244 1.1 fvdl char *netid_tcp = (char *) NULL;
245 1.1 fvdl char *netid_udp = (char *) NULL;
246 1.1 fvdl static char *netid_tcp_main;
247 1.1 fvdl static char *netid_udp_main;
248 1.1 fvdl struct netconfig *dummy;
249 1.12 thorpej #ifdef _REENTRANT
250 1.12 thorpej extern int __isthreaded;
251 1.1 fvdl
252 1.12 thorpej if (__isthreaded == 0) {
253 1.1 fvdl netid_udp = netid_udp_main;
254 1.1 fvdl netid_tcp = netid_tcp_main;
255 1.1 fvdl } else {
256 1.12 thorpej thr_once(&__rpc_getconfigp_once, __rpc_getconfigp_setup);
257 1.12 thorpej netid_tcp = thr_getspecific(tcp_key);
258 1.12 thorpej netid_udp = thr_getspecific(udp_key);
259 1.1 fvdl }
260 1.1 fvdl #else
261 1.1 fvdl netid_udp = netid_udp_main;
262 1.1 fvdl netid_tcp = netid_tcp_main;
263 1.1 fvdl #endif
264 1.5 lukem
265 1.5 lukem _DIAGASSERT(nettype != NULL);
266 1.5 lukem
267 1.1 fvdl if (!netid_udp && !netid_tcp) {
268 1.1 fvdl struct netconfig *nconf;
269 1.1 fvdl void *confighandle;
270 1.1 fvdl
271 1.1 fvdl if (!(confighandle = setnetconfig())) {
272 1.2 assar syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
273 1.1 fvdl return (NULL);
274 1.1 fvdl }
275 1.3 christos while ((nconf = getnetconfig(confighandle)) != NULL) {
276 1.1 fvdl if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
277 1.1 fvdl if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
278 1.1 fvdl netid_tcp = strdup(nconf->nc_netid);
279 1.12 thorpej #ifdef _REENTRANT
280 1.12 thorpej if (__isthreaded == 0)
281 1.1 fvdl netid_tcp_main = netid_tcp;
282 1.1 fvdl else
283 1.1 fvdl thr_setspecific(tcp_key,
284 1.1 fvdl (void *) netid_tcp);
285 1.1 fvdl #else
286 1.1 fvdl netid_tcp_main = netid_tcp;
287 1.1 fvdl #endif
288 1.1 fvdl } else
289 1.1 fvdl if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
290 1.1 fvdl netid_udp = strdup(nconf->nc_netid);
291 1.12 thorpej #ifdef _REENTRANT
292 1.12 thorpej if (__isthreaded == 0)
293 1.1 fvdl netid_udp_main = netid_udp;
294 1.1 fvdl else
295 1.1 fvdl thr_setspecific(udp_key,
296 1.1 fvdl (void *) netid_udp);
297 1.1 fvdl #else
298 1.1 fvdl netid_udp_main = netid_udp;
299 1.1 fvdl #endif
300 1.1 fvdl }
301 1.1 fvdl }
302 1.1 fvdl }
303 1.1 fvdl endnetconfig(confighandle);
304 1.1 fvdl }
305 1.1 fvdl if (strcmp(nettype, "udp") == 0)
306 1.1 fvdl netid = netid_udp;
307 1.1 fvdl else if (strcmp(nettype, "tcp") == 0)
308 1.1 fvdl netid = netid_tcp;
309 1.1 fvdl else {
310 1.3 christos return (NULL);
311 1.1 fvdl }
312 1.1 fvdl if ((netid == NULL) || (netid[0] == NULL)) {
313 1.3 christos return (NULL);
314 1.1 fvdl }
315 1.1 fvdl dummy = getnetconfigent(netid);
316 1.1 fvdl return (dummy);
317 1.1 fvdl }
318 1.1 fvdl
319 1.1 fvdl /*
320 1.1 fvdl * Returns the type of the nettype, which should then be used with
321 1.1 fvdl * __rpc_getconf().
322 1.1 fvdl */
323 1.1 fvdl void *
324 1.1 fvdl __rpc_setconf(nettype)
325 1.3 christos const char *nettype;
326 1.1 fvdl {
327 1.1 fvdl struct handle *handle;
328 1.1 fvdl
329 1.5 lukem /* nettype may be NULL; getnettype() supports that */
330 1.5 lukem
331 1.1 fvdl handle = (struct handle *) malloc(sizeof (struct handle));
332 1.1 fvdl if (handle == NULL) {
333 1.1 fvdl return (NULL);
334 1.1 fvdl }
335 1.1 fvdl switch (handle->nettype = getnettype(nettype)) {
336 1.1 fvdl case _RPC_NETPATH:
337 1.1 fvdl case _RPC_CIRCUIT_N:
338 1.1 fvdl case _RPC_DATAGRAM_N:
339 1.1 fvdl if (!(handle->nhandle = setnetpath())) {
340 1.1 fvdl free(handle);
341 1.1 fvdl return (NULL);
342 1.1 fvdl }
343 1.1 fvdl handle->nflag = TRUE;
344 1.1 fvdl break;
345 1.1 fvdl case _RPC_VISIBLE:
346 1.1 fvdl case _RPC_CIRCUIT_V:
347 1.1 fvdl case _RPC_DATAGRAM_V:
348 1.1 fvdl case _RPC_TCP:
349 1.1 fvdl case _RPC_UDP:
350 1.1 fvdl if (!(handle->nhandle = setnetconfig())) {
351 1.2 assar syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
352 1.1 fvdl free(handle);
353 1.1 fvdl return (NULL);
354 1.1 fvdl }
355 1.1 fvdl handle->nflag = FALSE;
356 1.1 fvdl break;
357 1.1 fvdl default:
358 1.1 fvdl return (NULL);
359 1.1 fvdl }
360 1.1 fvdl
361 1.1 fvdl return (handle);
362 1.1 fvdl }
363 1.1 fvdl
364 1.1 fvdl /*
365 1.1 fvdl * Returns the next netconfig struct for the given "net" type.
366 1.1 fvdl * __rpc_setconf() should have been called previously.
367 1.1 fvdl */
368 1.1 fvdl struct netconfig *
369 1.1 fvdl __rpc_getconf(vhandle)
370 1.1 fvdl void *vhandle;
371 1.1 fvdl {
372 1.1 fvdl struct handle *handle;
373 1.1 fvdl struct netconfig *nconf;
374 1.1 fvdl
375 1.1 fvdl handle = (struct handle *)vhandle;
376 1.1 fvdl if (handle == NULL) {
377 1.1 fvdl return (NULL);
378 1.1 fvdl }
379 1.3 christos for (;;) {
380 1.1 fvdl if (handle->nflag)
381 1.1 fvdl nconf = getnetpath(handle->nhandle);
382 1.1 fvdl else
383 1.1 fvdl nconf = getnetconfig(handle->nhandle);
384 1.3 christos if (nconf == NULL)
385 1.1 fvdl break;
386 1.1 fvdl if ((nconf->nc_semantics != NC_TPI_CLTS) &&
387 1.1 fvdl (nconf->nc_semantics != NC_TPI_COTS) &&
388 1.1 fvdl (nconf->nc_semantics != NC_TPI_COTS_ORD))
389 1.1 fvdl continue;
390 1.1 fvdl switch (handle->nettype) {
391 1.1 fvdl case _RPC_VISIBLE:
392 1.1 fvdl if (!(nconf->nc_flag & NC_VISIBLE))
393 1.1 fvdl continue;
394 1.1 fvdl /* FALLTHROUGH */
395 1.1 fvdl case _RPC_NETPATH: /* Be happy */
396 1.1 fvdl break;
397 1.1 fvdl case _RPC_CIRCUIT_V:
398 1.1 fvdl if (!(nconf->nc_flag & NC_VISIBLE))
399 1.1 fvdl continue;
400 1.1 fvdl /* FALLTHROUGH */
401 1.1 fvdl case _RPC_CIRCUIT_N:
402 1.1 fvdl if ((nconf->nc_semantics != NC_TPI_COTS) &&
403 1.1 fvdl (nconf->nc_semantics != NC_TPI_COTS_ORD))
404 1.1 fvdl continue;
405 1.1 fvdl break;
406 1.1 fvdl case _RPC_DATAGRAM_V:
407 1.1 fvdl if (!(nconf->nc_flag & NC_VISIBLE))
408 1.1 fvdl continue;
409 1.1 fvdl /* FALLTHROUGH */
410 1.1 fvdl case _RPC_DATAGRAM_N:
411 1.1 fvdl if (nconf->nc_semantics != NC_TPI_CLTS)
412 1.1 fvdl continue;
413 1.1 fvdl break;
414 1.1 fvdl case _RPC_TCP:
415 1.1 fvdl if (((nconf->nc_semantics != NC_TPI_COTS) &&
416 1.1 fvdl (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
417 1.1 fvdl (strcmp(nconf->nc_protofmly, NC_INET)
418 1.1 fvdl #ifdef INET6
419 1.1 fvdl && strcmp(nconf->nc_protofmly, NC_INET6))
420 1.1 fvdl #else
421 1.1 fvdl )
422 1.1 fvdl #endif
423 1.1 fvdl ||
424 1.1 fvdl strcmp(nconf->nc_proto, NC_TCP))
425 1.1 fvdl continue;
426 1.1 fvdl break;
427 1.1 fvdl case _RPC_UDP:
428 1.1 fvdl if ((nconf->nc_semantics != NC_TPI_CLTS) ||
429 1.1 fvdl (strcmp(nconf->nc_protofmly, NC_INET)
430 1.1 fvdl #ifdef INET6
431 1.1 fvdl && strcmp(nconf->nc_protofmly, NC_INET6))
432 1.1 fvdl #else
433 1.1 fvdl )
434 1.1 fvdl #endif
435 1.1 fvdl ||
436 1.1 fvdl strcmp(nconf->nc_proto, NC_UDP))
437 1.1 fvdl continue;
438 1.1 fvdl break;
439 1.1 fvdl }
440 1.1 fvdl break;
441 1.1 fvdl }
442 1.1 fvdl return (nconf);
443 1.1 fvdl }
444 1.1 fvdl
445 1.1 fvdl void
446 1.1 fvdl __rpc_endconf(vhandle)
447 1.1 fvdl void * vhandle;
448 1.1 fvdl {
449 1.1 fvdl struct handle *handle;
450 1.1 fvdl
451 1.1 fvdl handle = (struct handle *) vhandle;
452 1.1 fvdl if (handle == NULL) {
453 1.1 fvdl return;
454 1.1 fvdl }
455 1.1 fvdl if (handle->nflag) {
456 1.1 fvdl endnetpath(handle->nhandle);
457 1.1 fvdl } else {
458 1.1 fvdl endnetconfig(handle->nhandle);
459 1.1 fvdl }
460 1.1 fvdl free(handle);
461 1.1 fvdl }
462 1.1 fvdl
463 1.1 fvdl /*
464 1.1 fvdl * Used to ping the NULL procedure for clnt handle.
465 1.1 fvdl * Returns NULL if fails, else a non-NULL pointer.
466 1.1 fvdl */
467 1.1 fvdl void *
468 1.1 fvdl rpc_nullproc(clnt)
469 1.1 fvdl CLIENT *clnt;
470 1.1 fvdl {
471 1.1 fvdl struct timeval TIMEOUT = {25, 0};
472 1.1 fvdl
473 1.3 christos if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
474 1.3 christos (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
475 1.3 christos return (NULL);
476 1.1 fvdl }
477 1.1 fvdl return ((void *) clnt);
478 1.1 fvdl }
479 1.1 fvdl
480 1.1 fvdl /*
481 1.1 fvdl * Try all possible transports until
482 1.1 fvdl * one succeeds in finding the netconf for the given fd.
483 1.1 fvdl */
484 1.1 fvdl struct netconfig *
485 1.1 fvdl __rpcgettp(fd)
486 1.1 fvdl int fd;
487 1.1 fvdl {
488 1.1 fvdl const char *netid;
489 1.1 fvdl struct __rpc_sockinfo si;
490 1.1 fvdl
491 1.1 fvdl if (!__rpc_fd2sockinfo(fd, &si))
492 1.1 fvdl return NULL;
493 1.1 fvdl
494 1.1 fvdl if (!__rpc_sockinfo2netid(&si, &netid))
495 1.1 fvdl return NULL;
496 1.1 fvdl
497 1.3 christos /*LINTED const castaway*/
498 1.1 fvdl return getnetconfigent((char *)netid);
499 1.1 fvdl }
500 1.1 fvdl
501 1.1 fvdl int
502 1.1 fvdl __rpc_fd2sockinfo(int fd, struct __rpc_sockinfo *sip)
503 1.1 fvdl {
504 1.1 fvdl socklen_t len;
505 1.1 fvdl int type, proto;
506 1.1 fvdl struct sockaddr_storage ss;
507 1.1 fvdl
508 1.5 lukem _DIAGASSERT(sip != NULL);
509 1.5 lukem
510 1.1 fvdl len = sizeof ss;
511 1.3 christos if (getsockname(fd, (struct sockaddr *)(void *)&ss, &len) < 0)
512 1.1 fvdl return 0;
513 1.1 fvdl sip->si_alen = len;
514 1.1 fvdl
515 1.1 fvdl len = sizeof type;
516 1.1 fvdl if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
517 1.1 fvdl return 0;
518 1.1 fvdl
519 1.1 fvdl /* XXX */
520 1.1 fvdl if (ss.ss_family != AF_LOCAL) {
521 1.1 fvdl if (type == SOCK_STREAM)
522 1.1 fvdl proto = IPPROTO_TCP;
523 1.1 fvdl else if (type == SOCK_DGRAM)
524 1.1 fvdl proto = IPPROTO_UDP;
525 1.1 fvdl else
526 1.1 fvdl return 0;
527 1.1 fvdl } else
528 1.1 fvdl proto = 0;
529 1.1 fvdl
530 1.1 fvdl sip->si_af = ss.ss_family;
531 1.1 fvdl sip->si_proto = proto;
532 1.1 fvdl sip->si_socktype = type;
533 1.1 fvdl
534 1.1 fvdl return 1;
535 1.1 fvdl }
536 1.1 fvdl
537 1.1 fvdl /*
538 1.1 fvdl * Linear search, but the number of entries is small.
539 1.1 fvdl */
540 1.1 fvdl int
541 1.1 fvdl __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
542 1.1 fvdl {
543 1.11 thorpej size_t i;
544 1.1 fvdl
545 1.5 lukem _DIAGASSERT(nconf != NULL);
546 1.5 lukem _DIAGASSERT(sip != NULL);
547 1.5 lukem
548 1.1 fvdl for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
549 1.1 fvdl if (!strcmp(na_cvt[i].netid, nconf->nc_netid)) {
550 1.1 fvdl sip->si_af = na_cvt[i].af;
551 1.1 fvdl sip->si_proto = na_cvt[i].protocol;
552 1.1 fvdl sip->si_socktype =
553 1.3 christos __rpc_seman2socktype((int)nconf->nc_semantics);
554 1.1 fvdl if (sip->si_socktype == -1)
555 1.1 fvdl return 0;
556 1.1 fvdl sip->si_alen = __rpc_get_a_size(sip->si_af);
557 1.1 fvdl return 1;
558 1.1 fvdl }
559 1.1 fvdl
560 1.1 fvdl return 0;
561 1.1 fvdl }
562 1.1 fvdl
563 1.1 fvdl int
564 1.1 fvdl __rpc_nconf2fd(const struct netconfig *nconf)
565 1.1 fvdl {
566 1.1 fvdl struct __rpc_sockinfo si;
567 1.1 fvdl
568 1.5 lukem _DIAGASSERT(nconf != NULL);
569 1.5 lukem
570 1.1 fvdl if (!__rpc_nconf2sockinfo(nconf, &si))
571 1.1 fvdl return 0;
572 1.1 fvdl
573 1.1 fvdl return socket(si.si_af, si.si_socktype, si.si_proto);
574 1.1 fvdl }
575 1.1 fvdl
576 1.1 fvdl int
577 1.1 fvdl __rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid)
578 1.1 fvdl {
579 1.11 thorpej size_t i;
580 1.1 fvdl
581 1.5 lukem _DIAGASSERT(sip != NULL);
582 1.5 lukem /* netid may be NULL */
583 1.5 lukem
584 1.1 fvdl for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
585 1.1 fvdl if (na_cvt[i].af == sip->si_af &&
586 1.1 fvdl na_cvt[i].protocol == sip->si_proto) {
587 1.1 fvdl if (netid)
588 1.1 fvdl *netid = na_cvt[i].netid;
589 1.1 fvdl return 1;
590 1.1 fvdl }
591 1.1 fvdl
592 1.1 fvdl return 0;
593 1.1 fvdl }
594 1.1 fvdl
595 1.1 fvdl char *
596 1.1 fvdl taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
597 1.1 fvdl {
598 1.1 fvdl struct __rpc_sockinfo si;
599 1.1 fvdl
600 1.5 lukem _DIAGASSERT(nconf != NULL);
601 1.5 lukem _DIAGASSERT(nbuf != NULL);
602 1.5 lukem
603 1.1 fvdl if (!__rpc_nconf2sockinfo(nconf, &si))
604 1.1 fvdl return NULL;
605 1.1 fvdl return __rpc_taddr2uaddr_af(si.si_af, nbuf);
606 1.1 fvdl }
607 1.1 fvdl
608 1.1 fvdl struct netbuf *
609 1.1 fvdl uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
610 1.1 fvdl {
611 1.1 fvdl struct __rpc_sockinfo si;
612 1.5 lukem
613 1.5 lukem _DIAGASSERT(nconf != NULL);
614 1.5 lukem _DIAGASSERT(uaddr != NULL);
615 1.1 fvdl
616 1.1 fvdl if (!__rpc_nconf2sockinfo(nconf, &si))
617 1.1 fvdl return NULL;
618 1.1 fvdl return __rpc_uaddr2taddr_af(si.si_af, uaddr);
619 1.1 fvdl }
620 1.1 fvdl
621 1.1 fvdl char *
622 1.1 fvdl __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
623 1.1 fvdl {
624 1.1 fvdl char *ret;
625 1.9 lukem struct sockaddr_in *sinp;
626 1.1 fvdl struct sockaddr_un *sun;
627 1.1 fvdl char namebuf[INET_ADDRSTRLEN];
628 1.1 fvdl #ifdef INET6
629 1.1 fvdl struct sockaddr_in6 *sin6;
630 1.1 fvdl char namebuf6[INET6_ADDRSTRLEN];
631 1.1 fvdl #endif
632 1.1 fvdl u_int16_t port;
633 1.1 fvdl
634 1.5 lukem _DIAGASSERT(nbuf != NULL);
635 1.5 lukem
636 1.1 fvdl switch (af) {
637 1.1 fvdl case AF_INET:
638 1.9 lukem sinp = nbuf->buf;
639 1.9 lukem if (inet_ntop(af, &sinp->sin_addr, namebuf, sizeof namebuf)
640 1.1 fvdl == NULL)
641 1.1 fvdl return NULL;
642 1.9 lukem port = ntohs(sinp->sin_port);
643 1.3 christos if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8,
644 1.3 christos port & 0xff) < 0)
645 1.1 fvdl return NULL;
646 1.1 fvdl break;
647 1.1 fvdl #ifdef INET6
648 1.1 fvdl case AF_INET6:
649 1.1 fvdl sin6 = nbuf->buf;
650 1.1 fvdl if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
651 1.1 fvdl == NULL)
652 1.1 fvdl return NULL;
653 1.1 fvdl port = ntohs(sin6->sin6_port);
654 1.3 christos if (asprintf(&ret, "%s.%u.%u", namebuf6, ((u_int32_t)port) >> 8,
655 1.3 christos port & 0xff) < 0)
656 1.1 fvdl return NULL;
657 1.1 fvdl break;
658 1.1 fvdl #endif
659 1.1 fvdl case AF_LOCAL:
660 1.1 fvdl sun = nbuf->buf;
661 1.1 fvdl sun->sun_path[sizeof(sun->sun_path) - 1] = '\0'; /* safety */
662 1.1 fvdl ret = strdup(sun->sun_path);
663 1.1 fvdl break;
664 1.1 fvdl default:
665 1.1 fvdl return NULL;
666 1.1 fvdl }
667 1.1 fvdl
668 1.1 fvdl return ret;
669 1.1 fvdl }
670 1.1 fvdl
671 1.1 fvdl struct netbuf *
672 1.1 fvdl __rpc_uaddr2taddr_af(int af, const char *uaddr)
673 1.1 fvdl {
674 1.1 fvdl struct netbuf *ret = NULL;
675 1.1 fvdl char *addrstr, *p;
676 1.1 fvdl unsigned port, portlo, porthi;
677 1.9 lukem struct sockaddr_in *sinp;
678 1.1 fvdl #ifdef INET6
679 1.1 fvdl struct sockaddr_in6 *sin6;
680 1.1 fvdl #endif
681 1.1 fvdl struct sockaddr_un *sun;
682 1.1 fvdl
683 1.5 lukem _DIAGASSERT(uaddr != NULL);
684 1.5 lukem
685 1.1 fvdl addrstr = strdup(uaddr);
686 1.1 fvdl if (addrstr == NULL)
687 1.1 fvdl return NULL;
688 1.1 fvdl
689 1.1 fvdl /*
690 1.1 fvdl * AF_LOCAL addresses are expected to be absolute
691 1.1 fvdl * pathnames, anything else will be AF_INET or AF_INET6.
692 1.1 fvdl */
693 1.1 fvdl if (*addrstr != '/') {
694 1.1 fvdl p = strrchr(addrstr, '.');
695 1.1 fvdl if (p == NULL)
696 1.1 fvdl goto out;
697 1.1 fvdl portlo = (unsigned)atoi(p + 1);
698 1.1 fvdl *p = '\0';
699 1.1 fvdl
700 1.1 fvdl p = strrchr(addrstr, '.');
701 1.1 fvdl if (p == NULL)
702 1.1 fvdl goto out;
703 1.1 fvdl porthi = (unsigned)atoi(p + 1);
704 1.1 fvdl *p = '\0';
705 1.1 fvdl port = (porthi << 8) | portlo;
706 1.1 fvdl }
707 1.1 fvdl
708 1.1 fvdl ret = (struct netbuf *)malloc(sizeof *ret);
709 1.8 kristerw if (ret == NULL)
710 1.8 kristerw goto out;
711 1.1 fvdl
712 1.1 fvdl switch (af) {
713 1.1 fvdl case AF_INET:
714 1.9 lukem sinp = (struct sockaddr_in *)malloc(sizeof *sinp);
715 1.9 lukem if (sinp == NULL)
716 1.1 fvdl goto out;
717 1.9 lukem memset(sinp, 0, sizeof *sinp);
718 1.9 lukem sinp->sin_family = AF_INET;
719 1.9 lukem sinp->sin_port = htons(port);
720 1.9 lukem if (inet_pton(AF_INET, addrstr, &sinp->sin_addr) <= 0) {
721 1.9 lukem free(sinp);
722 1.1 fvdl free(ret);
723 1.1 fvdl ret = NULL;
724 1.1 fvdl goto out;
725 1.1 fvdl }
726 1.9 lukem sinp->sin_len = ret->maxlen = ret->len = sizeof *sinp;
727 1.9 lukem ret->buf = sinp;
728 1.1 fvdl break;
729 1.1 fvdl #ifdef INET6
730 1.1 fvdl case AF_INET6:
731 1.1 fvdl sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6);
732 1.1 fvdl if (sin6 == NULL)
733 1.1 fvdl goto out;
734 1.1 fvdl memset(sin6, 0, sizeof *sin6);
735 1.1 fvdl sin6->sin6_family = AF_INET6;
736 1.1 fvdl sin6->sin6_port = htons(port);
737 1.1 fvdl if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
738 1.8 kristerw free(sin6);
739 1.1 fvdl free(ret);
740 1.1 fvdl ret = NULL;
741 1.1 fvdl goto out;
742 1.1 fvdl }
743 1.1 fvdl sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
744 1.1 fvdl ret->buf = sin6;
745 1.1 fvdl break;
746 1.1 fvdl #endif
747 1.1 fvdl case AF_LOCAL:
748 1.1 fvdl sun = (struct sockaddr_un *)malloc(sizeof *sun);
749 1.1 fvdl if (sun == NULL)
750 1.1 fvdl goto out;
751 1.1 fvdl memset(sun, 0, sizeof *sun);
752 1.1 fvdl sun->sun_family = AF_LOCAL;
753 1.1 fvdl strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
754 1.7 fvdl ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
755 1.7 fvdl ret->buf = sun;
756 1.3 christos break;
757 1.1 fvdl default:
758 1.1 fvdl break;
759 1.1 fvdl }
760 1.1 fvdl out:
761 1.1 fvdl free(addrstr);
762 1.1 fvdl return ret;
763 1.1 fvdl }
764 1.1 fvdl
765 1.1 fvdl int
766 1.1 fvdl __rpc_seman2socktype(int semantics)
767 1.1 fvdl {
768 1.1 fvdl switch (semantics) {
769 1.1 fvdl case NC_TPI_CLTS:
770 1.1 fvdl return SOCK_DGRAM;
771 1.1 fvdl case NC_TPI_COTS_ORD:
772 1.1 fvdl return SOCK_STREAM;
773 1.1 fvdl case NC_TPI_RAW:
774 1.1 fvdl return SOCK_RAW;
775 1.1 fvdl default:
776 1.1 fvdl break;
777 1.1 fvdl }
778 1.1 fvdl
779 1.1 fvdl return -1;
780 1.1 fvdl }
781 1.1 fvdl
782 1.1 fvdl int
783 1.1 fvdl __rpc_socktype2seman(int socktype)
784 1.1 fvdl {
785 1.1 fvdl switch (socktype) {
786 1.1 fvdl case SOCK_DGRAM:
787 1.1 fvdl return NC_TPI_CLTS;
788 1.1 fvdl case SOCK_STREAM:
789 1.1 fvdl return NC_TPI_COTS_ORD;
790 1.1 fvdl case SOCK_RAW:
791 1.1 fvdl return NC_TPI_RAW;
792 1.1 fvdl default:
793 1.1 fvdl break;
794 1.1 fvdl }
795 1.1 fvdl
796 1.1 fvdl return -1;
797 1.1 fvdl }
798 1.1 fvdl
799 1.1 fvdl /*
800 1.1 fvdl * XXXX - IPv6 scope IDs can't be handled in universal addresses.
801 1.1 fvdl * Here, we compare the original server address to that of the RPC
802 1.1 fvdl * service we just received back from a call to rpcbind on the remote
803 1.1 fvdl * machine. If they are both "link local" or "site local", copy
804 1.1 fvdl * the scope id of the server address over to the service address.
805 1.1 fvdl */
806 1.1 fvdl int
807 1.1 fvdl __rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc)
808 1.1 fvdl {
809 1.1 fvdl #ifdef INET6
810 1.1 fvdl struct sockaddr *sa_new, *sa_svc;
811 1.1 fvdl struct sockaddr_in6 *sin6_new, *sin6_svc;
812 1.5 lukem
813 1.5 lukem _DIAGASSERT(new != NULL);
814 1.5 lukem _DIAGASSERT(svc != NULL);
815 1.1 fvdl
816 1.1 fvdl sa_svc = (struct sockaddr *)svc->buf;
817 1.1 fvdl sa_new = (struct sockaddr *)new->buf;
818 1.1 fvdl
819 1.1 fvdl if (sa_new->sa_family == sa_svc->sa_family &&
820 1.1 fvdl sa_new->sa_family == AF_INET6) {
821 1.1 fvdl sin6_new = (struct sockaddr_in6 *)new->buf;
822 1.1 fvdl sin6_svc = (struct sockaddr_in6 *)svc->buf;
823 1.1 fvdl
824 1.1 fvdl if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new->sin6_addr) &&
825 1.1 fvdl IN6_IS_ADDR_LINKLOCAL(&sin6_svc->sin6_addr)) ||
826 1.1 fvdl (IN6_IS_ADDR_SITELOCAL(&sin6_new->sin6_addr) &&
827 1.1 fvdl IN6_IS_ADDR_SITELOCAL(&sin6_svc->sin6_addr))) {
828 1.1 fvdl sin6_new->sin6_scope_id = sin6_svc->sin6_scope_id;
829 1.1 fvdl }
830 1.1 fvdl }
831 1.1 fvdl #endif
832 1.1 fvdl return 1;
833 1.1 fvdl }
834 1.1 fvdl
835 1.1 fvdl int
836 1.1 fvdl __rpc_sockisbound(int fd)
837 1.1 fvdl {
838 1.1 fvdl struct sockaddr_storage ss;
839 1.1 fvdl socklen_t slen;
840 1.1 fvdl
841 1.1 fvdl slen = sizeof (struct sockaddr_storage);
842 1.3 christos if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
843 1.1 fvdl return 0;
844 1.1 fvdl
845 1.1 fvdl switch (ss.ss_family) {
846 1.1 fvdl case AF_INET:
847 1.3 christos return (((struct sockaddr_in *)
848 1.3 christos (void *)&ss)->sin_port != 0);
849 1.1 fvdl #ifdef INET6
850 1.1 fvdl case AF_INET6:
851 1.3 christos return (((struct sockaddr_in6 *)
852 1.3 christos (void *)&ss)->sin6_port != 0);
853 1.1 fvdl #endif
854 1.1 fvdl case AF_LOCAL:
855 1.1 fvdl /* XXX check this */
856 1.3 christos return (((struct sockaddr_un *)
857 1.3 christos (void *)&ss)->sun_path[0] != '\0');
858 1.1 fvdl default:
859 1.1 fvdl break;
860 1.1 fvdl }
861 1.1 fvdl
862 1.1 fvdl return 0;
863 1.1 fvdl }
864