1 /* $NetBSD: rpc_generic.c,v 1.24 2010/12/08 02:06:38 joerg 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" */
38 * rpc_generic.c, Miscl routines for RPC.
42 #include <sys/cdefs.h>
43 #if defined(LIBC_SCCS) && !defined(lint)
44 __RCSID("$NetBSD: rpc_generic.c,v 1.24 2010/12/08 02:06:38 joerg Exp $");
47 #include "namespace.h"
48 #include "reentrant.h"
49 #include <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/socket.h>
53 #include <sys/resource.h>
54 #include <netinet/in.h>
55 #include <netinet/tcp.h>
56 #include <arpa/inet.h>
62 #include <netconfig.h>
66 #include <rpc/nettype.h>
67 #include "rpc_internal.h"
70 __weak_alias(taddr2uaddr
,_taddr2uaddr
)
71 __weak_alias(uaddr2taddr
,_uaddr2taddr
)
75 NCONF_HANDLE
*nhandle
;
76 int nflag
; /* Whether NETPATH or NETCONFIG */
80 static const struct _rpcnettype
{
84 { "netpath", _RPC_NETPATH
},
85 { "visible", _RPC_VISIBLE
},
86 { "circuit_v", _RPC_CIRCUIT_V
},
87 { "datagram_v", _RPC_DATAGRAM_V
},
88 { "circuit_n", _RPC_CIRCUIT_N
},
89 { "datagram_n", _RPC_DATAGRAM_N
},
101 static const struct netid_af na_cvt
[] = {
102 { "udp", AF_INET
, IPPROTO_UDP
},
103 { "tcp", AF_INET
, IPPROTO_TCP
},
105 { "udp6", AF_INET6
, IPPROTO_UDP
},
106 { "tcp6", AF_INET6
, IPPROTO_TCP
},
108 { "local", AF_LOCAL
, 0 }
112 static char *strlocase
__P((char *));
114 static int getnettype
__P((const char *));
117 * Cache the result of getrlimit(), so we don't have to do an
118 * expensive call every time.
129 if (getrlimit(RLIMIT_NOFILE
, &rl
) == 0) {
130 return (tbsize
= (int)rl
.rlim_max
);
133 * Something wrong. I'll try to save face by returning a
134 * pessimistic number.
141 * Find the appropriate buffer size
145 __rpc_get_t_size(af
, proto
, size
)
147 int size
; /* Size requested */
149 int maxsize
, defsize
;
151 maxsize
= 256 * 1024; /* XXX */
154 defsize
= 64 * 1024; /* XXX */
157 defsize
= UDPMSGSIZE
;
160 defsize
= RPC_MAXDATASIZE
;
166 /* Check whether the value is within the upper max limit */
167 return (size
> maxsize
? (u_int
)maxsize
: (u_int
)size
);
171 * Find the appropriate address buffer size
179 return sizeof (struct sockaddr_in
);
182 return sizeof (struct sockaddr_in6
);
185 return sizeof (struct sockaddr_un
);
189 return ((u_int
)RPC_MAXADDRSIZE
);
199 _DIAGASSERT(p
!= NULL
);
209 * Returns the type of the network as defined in <rpc/nettype.h>
210 * If nettype is NULL, it defaults to NETPATH.
218 if ((nettype
== NULL
) || (nettype
[0] == 0)) {
219 return (_RPC_NETPATH
); /* Default */
223 nettype
= strlocase(nettype
);
225 for (i
= 0; _rpctypelist
[i
].name
; i
++)
226 if (strcasecmp(nettype
, _rpctypelist
[i
].name
) == 0) {
227 return (_rpctypelist
[i
].type
);
229 return (_rpctypelist
[i
].type
);
233 * For the given nettype (tcp or udp only), return the first structure found.
234 * This should be freed by calling freenetconfigent()
238 static thread_key_t tcp_key
, udp_key
;
239 static once_t __rpc_getconfigp_once
= ONCE_INITIALIZER
;
242 __rpc_getconfigp_setup(void)
245 thr_keycreate(&tcp_key
, free
);
246 thr_keycreate(&udp_key
, free
);
251 __rpc_getconfip(nettype
)
255 char *netid_tcp
= NULL
;
256 char *netid_udp
= NULL
;
257 static char *netid_tcp_main
;
258 static char *netid_udp_main
;
259 struct netconfig
*dummy
;
261 if (__isthreaded
== 0) {
262 netid_udp
= netid_udp_main
;
263 netid_tcp
= netid_tcp_main
;
265 thr_once(&__rpc_getconfigp_once
, __rpc_getconfigp_setup
);
266 netid_tcp
= thr_getspecific(tcp_key
);
267 netid_udp
= thr_getspecific(udp_key
);
270 netid_udp
= netid_udp_main
;
271 netid_tcp
= netid_tcp_main
;
274 _DIAGASSERT(nettype
!= NULL
);
276 if (!netid_udp
&& !netid_tcp
) {
277 struct netconfig
*nconf
;
280 if (!(confighandle
= setnetconfig())) {
281 syslog (LOG_ERR
, "rpc: failed to open " NETCONFIG
);
284 while ((nconf
= getnetconfig(confighandle
)) != NULL
) {
285 if (strcmp(nconf
->nc_protofmly
, NC_INET
) == 0) {
286 if (strcmp(nconf
->nc_proto
, NC_TCP
) == 0) {
287 netid_tcp
= strdup(nconf
->nc_netid
);
288 if (netid_tcp
== NULL
)
291 if (__isthreaded
== 0)
292 netid_tcp_main
= netid_tcp
;
294 thr_setspecific(tcp_key
,
297 netid_tcp_main
= netid_tcp
;
300 if (strcmp(nconf
->nc_proto
, NC_UDP
) == 0) {
301 netid_udp
= strdup(nconf
->nc_netid
);
302 if (netid_udp
== NULL
)
305 if (__isthreaded
== 0)
306 netid_udp_main
= netid_udp
;
308 thr_setspecific(udp_key
,
311 netid_udp_main
= netid_udp
;
316 endnetconfig(confighandle
);
318 if (strcmp(nettype
, "udp") == 0)
320 else if (strcmp(nettype
, "tcp") == 0)
325 if ((netid
== NULL
) || (netid
[0] == 0)) {
328 dummy
= getnetconfigent(netid
);
333 * Returns the type of the nettype, which should then be used with
337 __rpc_setconf(nettype
)
340 struct handle
*handle
;
342 /* nettype may be NULL; getnettype() supports that */
344 handle
= malloc(sizeof(*handle
));
345 if (handle
== NULL
) {
348 switch (handle
->nettype
= getnettype(nettype
)) {
351 case _RPC_DATAGRAM_N
:
352 if (!(handle
->nhandle
= setnetpath())) {
356 handle
->nflag
= TRUE
;
360 case _RPC_DATAGRAM_V
:
363 if (!(handle
->nhandle
= setnetconfig())) {
364 syslog (LOG_ERR
, "rpc: failed to open " NETCONFIG
);
368 handle
->nflag
= FALSE
;
379 * Returns the next netconfig struct for the given "net" type.
380 * __rpc_setconf() should have been called previously.
383 __rpc_getconf(vhandle
)
386 struct handle
*handle
;
387 struct netconfig
*nconf
;
389 handle
= (struct handle
*)vhandle
;
390 if (handle
== NULL
) {
395 nconf
= getnetpath(handle
->nhandle
);
397 nconf
= getnetconfig(handle
->nhandle
);
400 if ((nconf
->nc_semantics
!= NC_TPI_CLTS
) &&
401 (nconf
->nc_semantics
!= NC_TPI_COTS
) &&
402 (nconf
->nc_semantics
!= NC_TPI_COTS_ORD
))
404 switch (handle
->nettype
) {
406 if (!(nconf
->nc_flag
& NC_VISIBLE
))
409 case _RPC_NETPATH
: /* Be happy */
412 if (!(nconf
->nc_flag
& NC_VISIBLE
))
416 if ((nconf
->nc_semantics
!= NC_TPI_COTS
) &&
417 (nconf
->nc_semantics
!= NC_TPI_COTS_ORD
))
420 case _RPC_DATAGRAM_V
:
421 if (!(nconf
->nc_flag
& NC_VISIBLE
))
424 case _RPC_DATAGRAM_N
:
425 if (nconf
->nc_semantics
!= NC_TPI_CLTS
)
429 if (((nconf
->nc_semantics
!= NC_TPI_COTS
) &&
430 (nconf
->nc_semantics
!= NC_TPI_COTS_ORD
)) ||
431 (strcmp(nconf
->nc_protofmly
, NC_INET
)
433 && strcmp(nconf
->nc_protofmly
, NC_INET6
))
438 strcmp(nconf
->nc_proto
, NC_TCP
))
442 if ((nconf
->nc_semantics
!= NC_TPI_CLTS
) ||
443 (strcmp(nconf
->nc_protofmly
, NC_INET
)
445 && strcmp(nconf
->nc_protofmly
, NC_INET6
))
450 strcmp(nconf
->nc_proto
, NC_UDP
))
460 __rpc_endconf(vhandle
)
463 struct handle
*handle
;
465 handle
= (struct handle
*) vhandle
;
466 if (handle
== NULL
) {
470 endnetpath(handle
->nhandle
);
472 endnetconfig(handle
->nhandle
);
478 * Used to ping the NULL procedure for clnt handle.
479 * Returns NULL if fails, else a non-NULL pointer.
485 struct timeval TIMEOUT
= {25, 0};
487 if (clnt_call(clnt
, NULLPROC
, (xdrproc_t
) xdr_void
, NULL
,
488 (xdrproc_t
) xdr_void
, NULL
, TIMEOUT
) != RPC_SUCCESS
) {
491 return ((void *) clnt
);
495 * Try all possible transports until
496 * one succeeds in finding the netconf for the given fd.
503 struct __rpc_sockinfo si
;
505 if (!__rpc_fd2sockinfo(fd
, &si
))
508 if (!__rpc_sockinfo2netid(&si
, &netid
))
511 return getnetconfigent(__UNCONST(netid
));
515 __rpc_fd2sockinfo(int fd
, struct __rpc_sockinfo
*sip
)
519 struct sockaddr_storage ss
;
521 _DIAGASSERT(sip
!= NULL
);
524 if (getsockname(fd
, (struct sockaddr
*)(void *)&ss
, &len
) < 0)
529 if (getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &type
, &len
) < 0)
533 if (ss
.ss_family
!= AF_LOCAL
) {
534 if (type
== SOCK_STREAM
)
536 else if (type
== SOCK_DGRAM
)
543 sip
->si_af
= ss
.ss_family
;
544 sip
->si_proto
= proto
;
545 sip
->si_socktype
= type
;
551 * Linear search, but the number of entries is small.
554 __rpc_nconf2sockinfo(const struct netconfig
*nconf
, struct __rpc_sockinfo
*sip
)
558 _DIAGASSERT(nconf
!= NULL
);
559 _DIAGASSERT(sip
!= NULL
);
561 for (i
= 0; i
< (sizeof na_cvt
) / (sizeof (struct netid_af
)); i
++)
562 if (!strcmp(na_cvt
[i
].netid
, nconf
->nc_netid
)) {
563 sip
->si_af
= na_cvt
[i
].af
;
564 sip
->si_proto
= na_cvt
[i
].protocol
;
566 __rpc_seman2socktype((int)nconf
->nc_semantics
);
567 if (sip
->si_socktype
== -1)
569 sip
->si_alen
= __rpc_get_a_size(sip
->si_af
);
577 __rpc_nconf2fd(const struct netconfig
*nconf
)
579 struct __rpc_sockinfo si
;
581 _DIAGASSERT(nconf
!= NULL
);
583 if (!__rpc_nconf2sockinfo(nconf
, &si
))
586 return socket(si
.si_af
, si
.si_socktype
, si
.si_proto
);
590 __rpc_sockinfo2netid(struct __rpc_sockinfo
*sip
, const char **netid
)
594 _DIAGASSERT(sip
!= NULL
);
595 /* netid may be NULL */
597 for (i
= 0; i
< (sizeof na_cvt
) / (sizeof (struct netid_af
)); i
++)
598 if (na_cvt
[i
].af
== sip
->si_af
&&
599 na_cvt
[i
].protocol
== sip
->si_proto
) {
601 *netid
= na_cvt
[i
].netid
;
609 taddr2uaddr(const struct netconfig
*nconf
, const struct netbuf
*nbuf
)
611 struct __rpc_sockinfo si
;
613 _DIAGASSERT(nconf
!= NULL
);
614 _DIAGASSERT(nbuf
!= NULL
);
616 if (!__rpc_nconf2sockinfo(nconf
, &si
))
618 return __rpc_taddr2uaddr_af(si
.si_af
, nbuf
);
622 uaddr2taddr(const struct netconfig
*nconf
, const char *uaddr
)
624 struct __rpc_sockinfo si
;
626 _DIAGASSERT(nconf
!= NULL
);
627 _DIAGASSERT(uaddr
!= NULL
);
629 if (!__rpc_nconf2sockinfo(nconf
, &si
))
631 return __rpc_uaddr2taddr_af(si
.si_af
, uaddr
);
635 __rpc_taddr2uaddr_af(int af
, const struct netbuf
*nbuf
)
638 struct sockaddr_in
*sinp
;
639 struct sockaddr_un
*sun
;
640 char namebuf
[INET_ADDRSTRLEN
];
642 struct sockaddr_in6
*sin6
;
643 char namebuf6
[INET6_ADDRSTRLEN
];
647 _DIAGASSERT(nbuf
!= NULL
);
652 if (inet_ntop(af
, &sinp
->sin_addr
, namebuf
, sizeof namebuf
)
655 port
= ntohs(sinp
->sin_port
);
656 if (asprintf(&ret
, "%s.%u.%u", namebuf
, ((u_int32_t
)port
) >> 8,
663 if (inet_ntop(af
, &sin6
->sin6_addr
, namebuf6
, sizeof namebuf6
)
666 port
= ntohs(sin6
->sin6_port
);
667 if (asprintf(&ret
, "%s.%u.%u", namebuf6
, ((u_int32_t
)port
) >> 8,
674 sun
->sun_path
[sizeof(sun
->sun_path
) - 1] = '\0'; /* safety */
675 ret
= strdup(sun
->sun_path
);
685 __rpc_uaddr2taddr_af(int af
, const char *uaddr
)
687 struct netbuf
*ret
= NULL
;
689 unsigned port
, portlo
, porthi
;
690 struct sockaddr_in
*sinp
;
692 struct sockaddr_in6
*sin6
;
694 struct sockaddr_un
*sun
;
696 _DIAGASSERT(uaddr
!= NULL
);
698 addrstr
= strdup(uaddr
);
703 * AF_LOCAL addresses are expected to be absolute
704 * pathnames, anything else will be AF_INET or AF_INET6.
707 if (*addrstr
!= '/') {
708 p
= strrchr(addrstr
, '.');
711 portlo
= (unsigned)atoi(p
+ 1);
714 p
= strrchr(addrstr
, '.');
717 porthi
= (unsigned)atoi(p
+ 1);
719 port
= (porthi
<< 8) | portlo
;
722 ret
= malloc(sizeof(*ret
));
728 sinp
= malloc(sizeof(*sinp
));
731 memset(sinp
, 0, sizeof *sinp
);
732 sinp
->sin_family
= AF_INET
;
733 sinp
->sin_port
= htons(port
);
734 if (inet_pton(AF_INET
, addrstr
, &sinp
->sin_addr
) <= 0) {
740 sinp
->sin_len
= ret
->maxlen
= ret
->len
= sizeof *sinp
;
745 sin6
= malloc(sizeof(*sin6
));
748 memset(sin6
, 0, sizeof *sin6
);
749 sin6
->sin6_family
= AF_INET6
;
750 sin6
->sin6_port
= htons(port
);
751 if (inet_pton(AF_INET6
, addrstr
, &sin6
->sin6_addr
) <= 0) {
757 sin6
->sin6_len
= ret
->maxlen
= ret
->len
= sizeof *sin6
;
762 sun
= malloc(sizeof(*sun
));
765 memset(sun
, 0, sizeof *sun
);
766 sun
->sun_family
= AF_LOCAL
;
767 strncpy(sun
->sun_path
, addrstr
, sizeof(sun
->sun_path
) - 1);
768 ret
->len
= ret
->maxlen
= sun
->sun_len
= SUN_LEN(sun
);
780 __rpc_seman2socktype(int semantics
)
785 case NC_TPI_COTS_ORD
:
797 __rpc_socktype2seman(int socktype
)
803 return NC_TPI_COTS_ORD
;
814 * XXXX - IPv6 scope IDs can't be handled in universal addresses.
815 * Here, we compare the original server address to that of the RPC
816 * service we just received back from a call to rpcbind on the remote
817 * machine. If they are both "link local" or "site local", copy
818 * the scope id of the server address over to the service address.
822 __rpc_fixup_addr(struct netbuf
*new, const struct netbuf
*svc
)
825 struct sockaddr
*sa_new
, *sa_svc
;
826 struct sockaddr_in6
*sin6_new
, *sin6_svc
;
828 _DIAGASSERT(new != NULL
);
829 _DIAGASSERT(svc
!= NULL
);
831 sa_svc
= (struct sockaddr
*)svc
->buf
;
832 sa_new
= (struct sockaddr
*)new->buf
;
834 if (sa_new
->sa_family
== sa_svc
->sa_family
&&
835 sa_new
->sa_family
== AF_INET6
) {
836 sin6_new
= (struct sockaddr_in6
*)new->buf
;
837 sin6_svc
= (struct sockaddr_in6
*)svc
->buf
;
839 if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new
->sin6_addr
) &&
840 IN6_IS_ADDR_LINKLOCAL(&sin6_svc
->sin6_addr
)) ||
841 (IN6_IS_ADDR_SITELOCAL(&sin6_new
->sin6_addr
) &&
842 IN6_IS_ADDR_SITELOCAL(&sin6_svc
->sin6_addr
))) {
843 sin6_new
->sin6_scope_id
= sin6_svc
->sin6_scope_id
;
851 __rpc_sockisbound(int fd
)
853 struct sockaddr_storage ss
;
856 slen
= sizeof (struct sockaddr_storage
);
857 if (getsockname(fd
, (struct sockaddr
*)(void *)&ss
, &slen
) < 0)
860 switch (ss
.ss_family
) {
862 return (((struct sockaddr_in
*)
863 (void *)&ss
)->sin_port
!= 0);
866 return (((struct sockaddr_in6
*)
867 (void *)&ss
)->sin6_port
!= 0);
871 return (((struct sockaddr_un
*)
872 (void *)&ss
)->sun_path
[0] != '\0');
881 * For TCP transport, Host Requirements RFCs mandate
882 * Nagle (RFC-896) processing. But for RPC, Nagle
883 * processing adds adds unwanted latency to the last,
884 * partial TCP segment of each RPC message. See:
885 * R. W. Scheifler and J. Gettys, The X Window System,
886 * ACM Transactions on Graphics 16:8 (Aug. 1983), pp. 57-69.
887 * So for TCP transport, disable Nagle via TCP_NODELAY.
888 * XXX: moral equivalent for non-TCP protocols?
891 __rpc_setnodelay(int fd
, const struct __rpc_sockinfo
*si
)
894 if (si
->si_proto
!= IPPROTO_TCP
)
896 return setsockopt(fd
, si
->si_proto
, TCP_NODELAY
, &one
, sizeof(one
));