1 /* $NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 /* #pragma ident "@(#)rpc_generic.c 1.17 94/04/24 SMI" */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * rpc_generic.c, Miscl routines for RPC.
44 #include "opt_inet6.h"
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
51 #include <sys/protosw.h>
53 #include <sys/systm.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/syslog.h>
59 #include <rpc/nettype.h>
61 #include <rpc/rpc_com.h>
63 #if __FreeBSD_version < 700000
64 #define strrchr rindex
68 NCONF_HANDLE *nhandle;
69 int nflag; /* Whether NETPATH or NETCONFIG */
73 static const struct _rpcnettype {
77 { "netpath", _RPC_NETPATH },
78 { "visible", _RPC_VISIBLE },
79 { "circuit_v", _RPC_CIRCUIT_V },
80 { "datagram_v", _RPC_DATAGRAM_V },
81 { "circuit_n", _RPC_CIRCUIT_N },
82 { "datagram_n", _RPC_DATAGRAM_N },
94 static const struct netid_af na_cvt[] = {
95 { "udp", AF_INET, IPPROTO_UDP },
96 { "tcp", AF_INET, IPPROTO_TCP },
98 { "udp6", AF_INET6, IPPROTO_UDP },
99 { "tcp6", AF_INET6, IPPROTO_TCP },
101 { "local", AF_LOCAL, 0 }
104 struct rpc_createerr rpc_createerr;
107 * Find the appropriate buffer size
111 __rpc_get_t_size(int af, int proto, int size)
113 int maxsize, defsize;
115 maxsize = 256 * 1024; /* XXX */
118 defsize = 64 * 1024; /* XXX */
121 defsize = UDPMSGSIZE;
124 defsize = RPC_MAXDATASIZE;
130 /* Check whether the value is within the upper max limit */
131 return (size > maxsize ? (u_int)maxsize : (u_int)size);
135 * Find the appropriate address buffer size
143 return sizeof (struct sockaddr_in);
146 return sizeof (struct sockaddr_in6);
149 return sizeof (struct sockaddr_un);
153 return ((u_int)RPC_MAXADDRSIZE);
159 * Used to ping the NULL procedure for clnt handle.
160 * Returns NULL if fails, else a non-NULL pointer.
166 struct timeval TIMEOUT = {25, 0};
168 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
169 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
172 return ((void *) clnt);
178 __rpc_socket2sockinfo(struct socket *so, struct __rpc_sockinfo *sip)
186 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
190 sip->si_alen = sa->sa_len;
191 family = sa->sa_family;
194 opt.sopt_dir = SOPT_GET;
195 opt.sopt_level = SOL_SOCKET;
196 opt.sopt_name = SO_TYPE;
197 opt.sopt_val = &type;
198 opt.sopt_valsize = sizeof type;
200 error = sogetopt(so, &opt);
205 if (family != AF_LOCAL) {
206 if (type == SOCK_STREAM)
208 else if (type == SOCK_DGRAM)
216 sip->si_proto = proto;
217 sip->si_socktype = type;
223 * Linear search, but the number of entries is small.
226 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
230 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
231 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
232 strcmp(nconf->nc_netid, "unix") == 0 &&
233 strcmp(na_cvt[i].netid, "local") == 0)) {
234 sip->si_af = na_cvt[i].af;
235 sip->si_proto = na_cvt[i].protocol;
237 __rpc_seman2socktype((int)nconf->nc_semantics);
238 if (sip->si_socktype == -1)
240 sip->si_alen = __rpc_get_a_size(sip->si_af);
248 __rpc_nconf2socket(const struct netconfig *nconf)
250 struct __rpc_sockinfo si;
254 if (!__rpc_nconf2sockinfo(nconf, &si))
258 error = socreate(si.si_af, &so, si.si_socktype, si.si_proto,
259 curthread->td_ucred, curthread);
268 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
270 struct __rpc_sockinfo si;
272 if (!__rpc_nconf2sockinfo(nconf, &si))
274 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
278 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
280 struct __rpc_sockinfo si;
282 if (!__rpc_nconf2sockinfo(nconf, &si))
284 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
288 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
292 struct sockaddr_in *sin;
293 struct sockaddr_un *sun;
294 char namebuf[INET_ADDRSTRLEN];
296 struct sockaddr_in6 *sin6;
297 char namebuf6[INET6_ADDRSTRLEN];
301 sbuf_new(&sb, NULL, 0, SBUF_AUTOEXTEND);
306 if (__rpc_inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
309 port = ntohs(sin->sin_port);
310 if (sbuf_printf(&sb, "%s.%u.%u", namebuf,
311 ((uint32_t)port) >> 8,
318 if (__rpc_inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
321 port = ntohs(sin6->sin6_port);
322 if (sbuf_printf(&sb, "%s.%u.%u", namebuf6,
323 ((uint32_t)port) >> 8,
330 if (sbuf_printf(&sb, "%.*s", (int)(sun->sun_len -
331 offsetof(struct sockaddr_un, sun_path)),
340 ret = strdup(sbuf_data(&sb), M_RPC);
347 __rpc_uaddr2taddr_af(int af, const char *uaddr)
349 struct netbuf *ret = NULL;
351 unsigned port, portlo, porthi;
352 struct sockaddr_in *sin;
354 struct sockaddr_in6 *sin6;
356 struct sockaddr_un *sun;
360 addrstr = strdup(uaddr, M_RPC);
365 * AF_LOCAL addresses are expected to be absolute
366 * pathnames, anything else will be AF_INET or AF_INET6.
368 if (*addrstr != '/') {
369 p = strrchr(addrstr, '.');
372 portlo = (unsigned)strtol(p + 1, NULL, 10);
375 p = strrchr(addrstr, '.');
378 porthi = (unsigned)strtol(p + 1, NULL, 10);
380 port = (porthi << 8) | portlo;
383 ret = (struct netbuf *)malloc(sizeof *ret, M_RPC, M_WAITOK);
389 sin = (struct sockaddr_in *)malloc(sizeof *sin, M_RPC,
393 memset(sin, 0, sizeof *sin);
394 sin->sin_family = AF_INET;
395 sin->sin_port = htons(port);
396 if (__rpc_inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
402 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
407 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6, M_RPC,
411 memset(sin6, 0, sizeof *sin6);
412 sin6->sin6_family = AF_INET6;
413 sin6->sin6_port = htons(port);
414 if (__rpc_inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
420 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
425 sun = (struct sockaddr_un *)malloc(sizeof *sun, M_RPC,
429 memset(sun, 0, sizeof *sun);
430 sun->sun_family = AF_LOCAL;
431 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
432 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
439 free(addrstr, M_RPC);
444 __rpc_seman2socktype(int semantics)
449 case NC_TPI_COTS_ORD:
461 __rpc_socktype2seman(int socktype)
467 return NC_TPI_COTS_ORD;
478 * Returns the type of the network as defined in <rpc/nettype.h>
479 * If nettype is NULL, it defaults to NETPATH.
482 getnettype(const char *nettype)
486 if ((nettype == NULL) || (nettype[0] == 0)) {
487 return (_RPC_NETPATH); /* Default */
491 nettype = strlocase(nettype);
493 for (i = 0; _rpctypelist[i].name; i++)
494 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
495 return (_rpctypelist[i].type);
497 return (_rpctypelist[i].type);
501 * For the given nettype (tcp or udp only), return the first structure found.
502 * This should be freed by calling freenetconfigent()
505 __rpc_getconfip(const char *nettype)
508 static char *netid_tcp = (char *) NULL;
509 static char *netid_udp = (char *) NULL;
510 struct netconfig *dummy;
512 if (!netid_udp && !netid_tcp) {
513 struct netconfig *nconf;
516 if (!(confighandle = setnetconfig())) {
517 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
520 while ((nconf = getnetconfig(confighandle)) != NULL) {
521 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
522 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
523 netid_tcp = strdup(nconf->nc_netid,
526 if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
527 netid_udp = strdup(nconf->nc_netid,
532 endnetconfig(confighandle);
534 if (strcmp(nettype, "udp") == 0)
536 else if (strcmp(nettype, "tcp") == 0)
541 if ((netid == NULL) || (netid[0] == 0)) {
544 dummy = getnetconfigent(netid);
549 * Returns the type of the nettype, which should then be used with
552 * For simplicity in the kernel, we don't support the NETPATH
553 * environment variable. We behave as userland would then NETPATH is
554 * unset, i.e. iterate over all visible entries in netconfig.
557 __rpc_setconf(nettype)
560 struct handle *handle;
562 handle = (struct handle *) malloc(sizeof (struct handle),
564 switch (handle->nettype = getnettype(nettype)) {
567 case _RPC_DATAGRAM_N:
568 if (!(handle->nhandle = setnetconfig()))
570 handle->nflag = TRUE;
574 case _RPC_DATAGRAM_V:
577 if (!(handle->nhandle = setnetconfig())) {
578 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
581 handle->nflag = FALSE;
595 * Returns the next netconfig struct for the given "net" type.
596 * __rpc_setconf() should have been called previously.
599 __rpc_getconf(void *vhandle)
601 struct handle *handle;
602 struct netconfig *nconf;
604 handle = (struct handle *)vhandle;
605 if (handle == NULL) {
610 nconf = getnetconfig(handle->nhandle);
611 if (nconf && !(nconf->nc_flag & NC_VISIBLE))
614 nconf = getnetconfig(handle->nhandle);
618 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
619 (nconf->nc_semantics != NC_TPI_COTS) &&
620 (nconf->nc_semantics != NC_TPI_COTS_ORD))
622 switch (handle->nettype) {
624 if (!(nconf->nc_flag & NC_VISIBLE))
627 case _RPC_NETPATH: /* Be happy */
630 if (!(nconf->nc_flag & NC_VISIBLE))
634 if ((nconf->nc_semantics != NC_TPI_COTS) &&
635 (nconf->nc_semantics != NC_TPI_COTS_ORD))
638 case _RPC_DATAGRAM_V:
639 if (!(nconf->nc_flag & NC_VISIBLE))
642 case _RPC_DATAGRAM_N:
643 if (nconf->nc_semantics != NC_TPI_CLTS)
647 if (((nconf->nc_semantics != NC_TPI_COTS) &&
648 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
649 (strcmp(nconf->nc_protofmly, NC_INET)
651 && strcmp(nconf->nc_protofmly, NC_INET6))
656 strcmp(nconf->nc_proto, NC_TCP))
660 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
661 (strcmp(nconf->nc_protofmly, NC_INET)
663 && strcmp(nconf->nc_protofmly, NC_INET6))
668 strcmp(nconf->nc_proto, NC_UDP))
678 __rpc_endconf(vhandle)
681 struct handle *handle;
683 handle = (struct handle *) vhandle;
684 if (handle == NULL) {
687 endnetconfig(handle->nhandle);
692 __rpc_sockisbound(struct socket *so)
697 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
701 switch (sa->sa_family) {
703 bound = (((struct sockaddr_in *) sa)->sin_port != 0);
707 bound = (((struct sockaddr_in6 *) sa)->sin6_port != 0);
712 bound = (((struct sockaddr_un *) sa)->sun_path[0] != '\0');
728 krpc_modevent(module_t mod, int type, void *data)
733 static moduledata_t krpc_mod = {
738 DECLARE_MODULE(krpc, krpc_mod, SI_SUB_VFS, SI_ORDER_ANY);
740 /* So that loader and kldload(2) can find us, wherever we are.. */
741 MODULE_VERSION(krpc, 1);