2 * Provide a variety of APIs that query an rpcbind daemon to
3 * discover RPC service ports and allowed protocol version
6 * Copyright (C) 2008 Oracle Corporation. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 021110-1307, USA.
29 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
38 #include <arpa/inet.h>
41 #include <rpc/pmap_prot.h>
44 #include <netconfig.h>
45 #include <rpc/rpcb_prot.h>
51 * Try a local socket first to access the local rpcbind daemon
53 * Rpcbind's local socket service does not seem to be working.
54 * Disable this logic for now.
58 #else /* !HAVE_LIBTIRPC */
60 #endif /* !HAVE_LIBTIRPC */
63 static const rpcvers_t default_rpcb_version = RPCBVERS_4;
64 #else /* !HAVE_LIBTIRPC */
65 static const rpcvers_t default_rpcb_version = PMAPVERS;
66 #endif /* !HAVE_LIBTIRPC */
68 #ifdef HAVE_DECL_AI_ADDRCONFIG
70 * getaddrinfo(3) generates a usable loopback address based on how the
71 * local network interfaces are configured. RFC 3484 requires that the
72 * results are sorted so that the first result has the best likelihood
73 * of working, so we try just that first result.
75 * Returns TRUE on success.
77 static int nfs_gp_loopback_address(struct sockaddr *sap, socklen_t *salen)
79 struct addrinfo *gai_results;
80 struct addrinfo gai_hint = {
81 .ai_flags = AI_ADDRCONFIG,
83 socklen_t len = *salen;
86 if (getaddrinfo(NULL, "sunrpc", &gai_hint, &gai_results))
89 switch (gai_results->ai_addr->sa_family) {
92 if (len >= gai_results->ai_addrlen) {
93 memcpy(sap, gai_results->ai_addr,
94 gai_results->ai_addrlen);
95 *salen = gai_results->ai_addrlen;
100 freeaddrinfo(gai_results);
105 * Old versions of getaddrinfo(3) don't support AI_ADDRCONFIG, so we
106 * have a fallback for building on legacy systems.
108 static int nfs_gp_loopback_address(struct sockaddr *sap, socklen_t *salen)
110 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
112 memset(sin, 0, sizeof(*sin));
114 sin->sin_family = AF_INET;
115 sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
116 *salen = sizeof(*sin);
123 * Plant port number in @sap. @port is already in network byte order.
125 static void nfs_gp_set_port(struct sockaddr *sap, const in_port_t port)
127 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
128 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
130 switch (sap->sa_family) {
132 sin->sin_port = port;
135 sin6->sin6_port = port;
141 * Look up a network service in /etc/services and return the
142 * network-order port number of that service.
144 static in_port_t nfs_gp_getservbyname(const char *service,
145 const unsigned short protocol)
147 const struct addrinfo gai_hint = {
148 .ai_family = AF_INET,
149 .ai_protocol = protocol,
150 .ai_flags = AI_PASSIVE,
152 struct addrinfo *gai_results;
153 const struct sockaddr_in *sin;
156 if (getaddrinfo(NULL, service, &gai_hint, &gai_results) != 0)
159 sin = (const struct sockaddr_in *)gai_results->ai_addr;
160 port = sin->sin_port;
162 freeaddrinfo(gai_results);
167 * Discover the port number that should be used to contact an
168 * rpcbind service. This will detect if the port has a local
169 * value that may have been set in /etc/services.
171 * Returns network byte-order port number of rpcbind service
174 static in_port_t nfs_gp_get_rpcb_port(const unsigned short protocol)
176 static const char *rpcb_netnametbl[] = {
184 for (i = 0; rpcb_netnametbl[i] != NULL; i++) {
187 port = nfs_gp_getservbyname(rpcb_netnametbl[i], protocol);
192 return (in_port_t)htons((uint16_t)PMAPPORT);
196 * Set up an RPC client for communicating with an rpcbind daemon at
197 * @sap over @transport with protocol version @version.
199 * Returns a pointer to a prepared RPC client if successful, and
200 * @timeout is initialized; caller must destroy a non-NULL returned RPC
201 * client. Otherwise returns NULL, and rpc_createerr.cf_stat is set to
204 static CLIENT *nfs_gp_get_rpcbclient(const struct sockaddr *sap,
205 const socklen_t salen,
206 const unsigned short transport,
207 const rpcvers_t version,
208 struct timeval *timeout)
210 static const char *rpcb_pgmtbl[] = {
217 struct sockaddr_storage address;
218 struct sockaddr *saddr = (struct sockaddr *)&address;
219 rpcprog_t rpcb_prog = nfs_getrpcbyname(RPCBPROG, rpcb_pgmtbl);
221 memcpy(saddr, sap, (size_t)salen);
222 nfs_gp_set_port(saddr, nfs_gp_get_rpcb_port(transport));
224 return nfs_get_rpcclient(saddr, salen, transport, rpcb_prog,
229 * One of the arguments passed when querying remote rpcbind services
230 * via rpcbind v3 or v4 is a netid string. This replaces the pm_prot
231 * field used in legacy PMAP_GETPORT calls.
233 * RFC 1833 says netids are not standard but rather defined on the local
234 * host. There are, however, standard definitions for nc_protofmly and
235 * nc_proto that can be used to derive a netid string on the local host,
236 * based on the contents of /etc/netconfig.
238 * Walk through the local netconfig database and grab the netid of the
239 * first entry that matches @family and @protocol and whose netid string
240 * fits in the provided buffer.
242 * Returns a '\0'-terminated string if successful; otherwise NULL.
243 * rpc_createerr.cf_stat is set to reflect the error.
247 static char *nfs_gp_get_netid(const sa_family_t family,
248 const unsigned short protocol)
250 char *nc_protofmly, *nc_proto, *nc_netid;
251 struct netconfig *nconf;
252 struct protoent *proto;
258 nc_protofmly = NC_INET;
261 nc_protofmly = NC_INET6;
267 proto = getprotobynumber(protocol);
270 nc_proto = proto->p_name;
272 handle = setnetconfig();
273 while ((nconf = getnetconfig(handle)) != NULL) {
275 if (nconf->nc_protofmly != NULL &&
276 strcmp(nconf->nc_protofmly, nc_protofmly) != 0)
278 if (nconf->nc_proto != NULL &&
279 strcmp(nconf->nc_proto, nc_proto) != 0)
282 nc_netid = strdup(nconf->nc_netid);
283 endnetconfig(handle);
286 endnetconfig(handle);
289 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
293 #endif /* HAVE_LIBTIRPC */
296 * Extract a port number from a universal address, and terminate the
297 * string in @addrstr just after the address part.
299 * Returns -1 if unsuccesful; otherwise a decoded port number (possibly 0)
302 static int nfs_gp_universal_porthelper(char *addrstr)
305 unsigned long portlo, porthi;
308 p = strrchr(addrstr, '.');
311 portlo = strtoul(p + 1, &endptr, 10);
312 if (*endptr != '\0' || portlo > 255)
316 p = strrchr(addrstr, '.');
319 porthi = strtoul(p + 1, &endptr, 10);
320 if (*endptr != '\0' || porthi > 255)
323 port = (porthi << 8) | portlo;
330 * nfs_universal2port - extract port number from a "universal address"
331 * @uaddr: '\0'-terminated C string containing a universal address
333 * Universal addresses (defined in RFC 1833) are used when calling an
334 * rpcbind daemon via protocol versions 3 or 4..
336 * Returns -1 if unsuccesful; otherwise a decoded port number (possibly 0)
339 int nfs_universal2port(const char *uaddr)
344 addrstr = strdup(uaddr);
345 if (addrstr != NULL) {
346 port = nfs_gp_universal_porthelper(addrstr);
353 * nfs_sockaddr2universal - convert a sockaddr to a "universal address"
354 * @sap: pointer to a socket address
355 * @salen: length of socket address
357 * Universal addresses (defined in RFC 1833) are used when calling an
358 * rpcbind daemon via protocol versions 3 or 4..
360 * Returns a '\0'-terminated string if successful; caller must free
361 * the returned string. Otherwise NULL is returned and
362 * rpc_createerr.cf_stat is set to reflect the error.
365 #ifdef HAVE_GETNAMEINFO
367 char *nfs_sockaddr2universal(const struct sockaddr *sap,
368 const socklen_t salen)
370 struct sockaddr_un *sun = (struct sockaddr_un *)sap;
371 char buf[NI_MAXHOST];
374 switch (sap->sa_family) {
376 return strndup(sun->sun_path, sizeof(sun->sun_path));
378 if (getnameinfo(sap, salen, buf, (socklen_t)sizeof(buf),
379 NULL, 0, NI_NUMERICHOST) != 0)
381 port = ntohs(((struct sockaddr_in *)sap)->sin_port);
384 if (getnameinfo(sap, salen, buf, (socklen_t)sizeof(buf),
385 NULL, 0, NI_NUMERICHOST) != 0)
387 port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
393 (void)snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ".%u.%u",
394 (unsigned)(port >> 8), (unsigned)(port & 0xff));
399 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
403 #else /* HAVE_GETNAMEINFO */
405 char *nfs_sockaddr2universal(const struct sockaddr *sap,
406 const socklen_t salen)
408 struct sockaddr_un *sun = (struct sockaddr_un *)sap;
409 char buf[NI_MAXHOST];
413 switch (sap->sa_family) {
415 return strndup(sun->sun_path, sizeof(sun->sun_path));
417 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
418 if (addr != NULL && strlen(addr) > sizeof(buf))
421 port = ntohs(((struct sockaddr_in *)sap)->sin_port);
427 (void)snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ".%u.%u",
428 (unsigned)(port >> 8), (unsigned)(port & 0xff));
433 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
437 #endif /* HAVE_GETNAMEINFO */
440 * Send a NULL request to the indicated RPC service.
442 * Returns 1 if the service responded; otherwise 0;
444 static int nfs_gp_ping(CLIENT *client, struct timeval timeout)
446 enum clnt_stat status;
448 status = CLNT_CALL(client, NULLPROC,
449 (xdrproc_t)xdr_void, NULL,
450 (xdrproc_t)xdr_void, NULL,
453 return (int)(status == RPC_SUCCESS);
459 * Initialize the rpcb argument for a GETADDR request.
461 * Returns 1 if successful, and caller must free strings pointed
462 * to by r_netid and r_addr; otherwise 0.
464 static int nfs_gp_init_rpcb_parms(const struct sockaddr *sap,
465 const socklen_t salen,
466 const rpcprog_t program,
467 const rpcvers_t version,
468 const unsigned short protocol,
473 netid = nfs_gp_get_netid(sap->sa_family, protocol);
477 addr = nfs_sockaddr2universal(sap, salen);
483 memset(parms, 0, sizeof(*parms));
484 parms->r_prog = program;
485 parms->r_vers = version;
486 parms->r_netid = netid;
487 parms->r_addr = addr;
493 static void nfs_gp_free_rpcb_parms(struct rpcb *parms)
495 free(parms->r_netid);
500 * Try rpcbind GETADDR via version 4. If that fails, try same
501 * request via version 3.
503 * Returns non-zero port number on success; otherwise returns
504 * zero. rpccreateerr is set to reflect the nature of the error.
506 static unsigned short nfs_gp_rpcb_getaddr(CLIENT *client,
508 struct timeval timeout)
510 rpcvers_t rpcb_version;
511 struct rpc_err rpcerr;
514 for (rpcb_version = RPCBVERS_4;
515 rpcb_version >= RPCBVERS_3;
517 enum clnt_stat status;
520 CLNT_CONTROL(client, CLSET_VERS, (void *)&rpcb_version);
521 status = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
522 (xdrproc_t)xdr_rpcb, (void *)parms,
523 (xdrproc_t)xdr_wrapstring, (void *)&uaddr,
528 if ((uaddr == NULL) || (uaddr[0] == '\0')) {
529 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
533 port = nfs_universal2port(uaddr);
534 xdr_free((xdrproc_t)xdr_wrapstring, (char *)&uaddr);
536 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
539 return (unsigned short)port;
540 case RPC_PROGVERSMISMATCH:
541 clnt_geterr(client, &rpcerr);
542 if (rpcerr.re_vers.low > RPCBVERS4)
545 case RPC_PROCUNAVAIL:
546 case RPC_PROGUNAVAIL:
549 /* Most likely RPC_TIMEDOUT or RPC_CANTRECV */
550 rpc_createerr.cf_stat = status;
551 clnt_geterr(client, &rpc_createerr.cf_error);
558 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
559 clnt_geterr(client, &rpc_createerr.cf_error);
564 #endif /* HAVE_LIBTIRPC */
567 * Try GETPORT request via rpcbind version 2.
569 * Returns non-zero port number on success; otherwise returns
570 * zero. rpccreateerr is set to reflect the nature of the error.
572 static unsigned long nfs_gp_pmap_getport(CLIENT *client,
574 struct timeval timeout)
576 enum clnt_stat status;
579 status = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
580 (xdrproc_t)xdr_pmap, (void *)parms,
581 (xdrproc_t)xdr_u_long, (void *)&port,
584 if (status != RPC_SUCCESS) {
585 rpc_createerr.cf_stat = status;
586 clnt_geterr(client, &rpc_createerr.cf_error);
588 } else if (port == 0)
589 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
596 static unsigned short nfs_gp_getport_rpcb(CLIENT *client,
597 const struct sockaddr *sap,
598 const socklen_t salen,
599 const rpcprog_t program,
600 const rpcvers_t version,
601 const unsigned short protocol,
602 struct timeval timeout)
604 unsigned short port = 0;
607 if (nfs_gp_init_rpcb_parms(sap, salen, program,
608 version, protocol, &parms) != 0) {
609 port = nfs_gp_rpcb_getaddr(client, &parms, timeout);
610 nfs_gp_free_rpcb_parms(&parms);
616 #endif /* HAVE_LIBTIRPC */
618 static unsigned long nfs_gp_getport_pmap(CLIENT *client,
619 const rpcprog_t program,
620 const rpcvers_t version,
621 const unsigned short protocol,
622 struct timeval timeout)
624 struct pmap parms = {
629 rpcvers_t pmap_version = PMAPVERS;
631 CLNT_CONTROL(client, CLSET_VERS, (void *)&pmap_version);
632 return nfs_gp_pmap_getport(client, &parms, timeout);
636 * Try an AF_INET6 request via rpcbind v4/v3; try an AF_INET
637 * request via rpcbind v2.
639 * Returns non-zero port number on success; otherwise returns
640 * zero. rpccreateerr is set to reflect the nature of the error.
642 static unsigned short nfs_gp_getport(CLIENT *client,
643 const struct sockaddr *sap,
644 const socklen_t salen,
645 const rpcprog_t program,
646 const rpcvers_t version,
647 const unsigned short protocol,
648 struct timeval timeout)
650 switch (sap->sa_family) {
653 return nfs_gp_getport_rpcb(client, sap, salen, program,
654 version, protocol, timeout);
655 #endif /* HAVE_LIBTIRPC */
657 return nfs_gp_getport_pmap(client, program, version,
661 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
666 * nfs_rcp_ping - Determine if RPC service is responding to requests
667 * @sap: pointer to address of server to query (port is already filled in)
668 * @salen: length of server address
669 * @program: requested RPC program number
670 * @version: requested RPC version number
671 * @protocol: requested IPPROTO_ value of transport protocol
672 * @timeout: pointer to request timeout (NULL means use default timeout)
674 * Returns 1 if the remote service responded without an error; otherwise
677 int nfs_rpc_ping(const struct sockaddr *sap, const socklen_t salen,
678 const rpcprog_t program, const rpcvers_t version,
679 const unsigned short protocol, const struct timeval *timeout)
682 struct timeval tout = { -1, 0 };
688 client = nfs_get_rpcclient(sap, salen, protocol, program, version, &tout);
689 if (client != NULL) {
690 result = nfs_gp_ping(client, tout);
691 CLNT_DESTROY(client);
698 * nfs_getport - query server's rpcbind to get port number for an RPC service
699 * @sap: pointer to address of server to query
700 * @salen: length of server's address
701 * @program: requested RPC program number
702 * @version: requested RPC version number
703 * @protocol: IPPROTO_ value of requested transport protocol
705 * Uses any acceptable rpcbind version to discover the port number for the
706 * RPC service described by the given [program, version, transport] tuple.
707 * Uses a quick timeout and an ephemeral source port. Supports AF_INET and
708 * AF_INET6 server addresses.
710 * Returns a positive integer representing the port number of the RPC
711 * service advertised by the server (in host byte order), or zero if the
712 * service is not advertised or there was some problem querying the server's
713 * rpcbind daemon. rpccreateerr is set to reflect the underlying cause of
716 * There are a variety of ways to choose which transport and rpcbind versions
717 * to use. We chose to conserve local resources and try to avoid incurring
721 * To provide rudimentary support for traversing firewalls, query the remote
722 * using the same transport as the requested service. This provides some
723 * guarantee that the requested transport is available between this client
724 * and the server, and if the caller specifically requests TCP, for example,
725 * this may be becuase a firewall is in place that blocks UDP traffic. We
726 * could try both, but that could involve a lengthy timeout in several cases,
727 * and would often consume an extra ephemeral port.
730 * To avoid using up too many ephemeral ports, AF_INET queries use tried-and-
731 * true rpcbindv2, and don't try the newer versions; and AF_INET6 queries use
732 * rpcbindv4, then rpcbindv3 on the same socket. The newer rpcbind protocol
733 * versions can adequately detect if a remote RPC service does not support
734 * AF_INET6 at all. The rpcbind socket is re-used in an attempt to keep the
735 * overall number of consumed ephemeral ports low.
737 unsigned short nfs_getport(const struct sockaddr *sap,
738 const socklen_t salen,
739 const rpcprog_t program,
740 const rpcvers_t version,
741 const unsigned short protocol)
743 struct timeval timeout = { -1, 0 };
744 unsigned short port = 0;
747 client = nfs_gp_get_rpcbclient(sap, salen, protocol,
748 default_rpcb_version, &timeout);
749 if (client != NULL) {
750 port = nfs_gp_getport(client, sap, salen, program,
751 version, protocol, timeout);
752 CLNT_DESTROY(client);
759 * nfs_getport_ping - query server's rpcbind and do RPC ping to verify result
760 * @sap: IN: pointer to address of server to query;
761 * OUT: pointer to updated address
762 * @salen: length of server's address
763 * @program: requested RPC program number
764 * @version: requested RPC version number
765 * @protocol: IPPROTO_ value of requested transport protocol
767 * Uses any acceptable rpcbind version to discover the port number for the
768 * RPC service described by the given [program, version, transport] tuple.
769 * Uses a quick timeout and an ephemeral source port. Supports AF_INET and
770 * AF_INET6 server addresses.
772 * Returns a 1 and sets the port number in the passed-in server address
773 * if both the query and the ping were successful; otherwise zero.
774 * rpccreateerr is set to reflect the underlying cause of the error.
776 int nfs_getport_ping(struct sockaddr *sap, const socklen_t salen,
777 const rpcprog_t program, const rpcvers_t version,
778 const unsigned short protocol)
780 struct timeval timeout = { -1, 0 };
781 unsigned short port = 0;
785 client = nfs_gp_get_rpcbclient(sap, salen, protocol,
786 default_rpcb_version, &timeout);
787 if (client != NULL) {
788 port = nfs_gp_getport(client, sap, salen, program,
789 version, protocol, timeout);
790 CLNT_DESTROY(client);
795 struct sockaddr_storage address;
796 struct sockaddr *saddr = (struct sockaddr *)&address;
798 memcpy(saddr, sap, (size_t)salen);
799 nfs_gp_set_port(saddr, htons(port));
801 client = nfs_get_rpcclient(saddr, salen, protocol,
802 program, version, &timeout);
803 if (client != NULL) {
804 result = nfs_gp_ping(client, timeout);
805 CLNT_DESTROY(client);
810 nfs_gp_set_port(sap, htons(port));
816 * nfs_getlocalport - query local rpcbind to get port number for an RPC service
817 * @program: requested RPC program number
818 * @version: requested RPC version number
819 * @protocol: IPPROTO_ value of requested transport protocol
821 * Uses any acceptable rpcbind version to discover the port number for the
822 * RPC service described by the given [program, version, transport] tuple.
823 * Uses a quick timeout and an ephemeral source port. Supports AF_INET and
824 * AF_INET6 local addresses.
826 * Returns a positive integer representing the port number of the RPC
827 * service advertised by the server (in host byte order), or zero if the
828 * service is not advertised or there was some problem querying the server's
829 * rpcbind daemon. rpccreateerr is set to reflect the underlying cause of
832 * Try an AF_LOCAL connection first. The rpcbind daemon implementation should
833 * listen on AF_LOCAL.
835 * If that doesn't work (for example, if portmapper is running, or rpcbind
836 * isn't listening on /var/run/rpcbind.sock), send a query via UDP to localhost
837 * (UDP doesn't leave a socket in TIME_WAIT, and the timeout is a relatively
840 * getaddrinfo(3) generates a usable loopback address. RFC 3484 requires that
841 * the results are sorted so that the first result has the best likelihood of
842 * working, so we try just that first result. If IPv6 is all that is
843 * available, we are sure to generate an AF_INET6 loopback address and use
844 * rpcbindv4/v3 GETADDR. AF_INET6 requests go via rpcbind v4/3 in order to
845 * detect if the requested RPC service supports AF_INET6 or not.
847 unsigned short nfs_getlocalport(const rpcprot_t program,
848 const rpcvers_t version,
849 const unsigned short protocol)
851 struct sockaddr_storage address;
852 struct sockaddr *lb_addr = (struct sockaddr *)&address;
853 socklen_t lb_len = sizeof(*lb_addr);
854 unsigned short port = 0;
857 const struct sockaddr_un sun = {
858 .sun_family = AF_LOCAL,
859 .sun_path = _PATH_RPCBINDSOCK,
861 const struct sockaddr *sap = (struct sockaddr *)&sun;
862 const socklen_t salen = SUN_LEN(&sun);
864 struct timeval timeout = { -1, 0 };
866 client = nfs_gp_get_rpcbclient(sap, salen, 0, RPCBVERS_4, &timeout);
867 if (client != NULL) {
870 if (nfs_gp_init_rpcb_parms(sap, salen, program, version,
871 protocol, &parms) != 0) {
872 port = nfs_gp_rpcb_getaddr(client, &parms, timeout);
873 nfs_gp_free_rpcb_parms(&parms);
875 CLNT_DESTROY(client);
877 #endif /* NFS_GP_LOCAL */
880 if (nfs_gp_loopback_address(lb_addr, &lb_len)) {
881 port = nfs_getport(lb_addr, lb_len,
882 program, version, protocol);
884 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
891 * nfs_rpcb_getaddr - query rpcbind via rpcbind versions 4 and 3
892 * @sap: pointer to address of server to query
893 * @salen: length of server address
894 * @transport: transport protocol to use for the query
895 * @addr: pointer to r_addr address
896 * @addrlen: length of address
897 * @program: requested RPC program number
898 * @version: requested RPC version number
899 * @protocol: requested IPPROTO_ value of transport protocol
900 * @timeout: pointer to request timeout (NULL means use default timeout)
902 * Returns a positive integer representing the port number of the RPC
903 * service advertised by the server (in host byte order), or zero if the
904 * service is not advertised or there was some problem querying the
905 * server's rpcbind daemon. rpccreateerr is set to reflect the
906 * underlying cause of the error.
908 * This function provides similar functionality to nfs_pmap_getport(),
909 * but performs the rpcbind lookup via rpcbind version 4. If the server
910 * doesn't support rpcbind version 4, it will retry with version 3.
911 * The GETADDR procedure is exactly the same in these two versions of
912 * the rpcbind protocol, so the socket, RPC client, and arguments are
913 * re-used when retrying, saving ephemeral port space.
915 * These RPC procedures take a universal address as an argument, so the
916 * query will fail if the remote rpcbind daemon doesn't find an entry
917 * with a matching address. A matching address includes an ANYADDR
918 * address of the same address family. In this way an RPC server can
919 * advertise via rpcbind that it does not support AF_INET6.
923 unsigned short nfs_rpcb_getaddr(const struct sockaddr *sap,
924 const socklen_t salen,
925 const unsigned short transport,
926 const struct sockaddr *addr,
927 const socklen_t addrlen,
928 const rpcprog_t program,
929 const rpcvers_t version,
930 const unsigned short protocol,
931 const struct timeval *timeout)
935 struct timeval tout = { -1, 0 };
936 unsigned short port = 0;
941 client = nfs_gp_get_rpcbclient(sap, salen, transport, RPCBVERS_4, &tout);
942 if (client != NULL) {
943 if (nfs_gp_init_rpcb_parms(addr, addrlen, program, version,
944 protocol, &parms) != 0) {
945 port = nfs_gp_rpcb_getaddr(client, &parms, tout);
946 nfs_gp_free_rpcb_parms(&parms);
948 CLNT_DESTROY(client);
954 #else /* !HAVE_LIBTIRPC */
956 unsigned short nfs_rpcb_getaddr(const struct sockaddr *sap,
957 const socklen_t salen,
958 const unsigned short transport,
959 const struct sockaddr *addr,
960 const socklen_t addrlen,
961 const rpcprog_t program,
962 const rpcvers_t version,
963 const unsigned short protocol,
964 const struct timeval *timeout)
966 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
970 #endif /* !HAVE_LIBTIRPC */
973 * nfs_pmap_getport - query rpcbind via the portmap protocol (rpcbindv2)
974 * @sin: pointer to AF_INET address of server to query
975 * @transport: transport protocol to use for the query
976 * @program: requested RPC program number
977 * @version: requested RPC version number
978 * @protocol: requested IPPROTO_ value of transport protocol
979 * @timeout: pointer to request timeout (NULL means use default timeout)
981 * Returns a positive integer representing the port number of the RPC service
982 * advertised by the server (in host byte order), or zero if the service is
983 * not advertised or there was some problem querying the server's rpcbind
984 * daemon. rpccreateerr is set to reflect the underlying cause of the error.
986 * nfs_pmap_getport() is very similar to pmap_getport(), except that:
988 * 1. This version always tries to use an ephemeral port, since reserved
989 * ports are not needed for GETPORT queries. This conserves the very
990 * limited reserved port space, helping reduce failed socket binds
991 * during mount storms.
993 * 2. This version times out quickly by default. It time-limits the
994 * connect process as well as the actual RPC call, and even allows the
995 * caller to specify the timeout.
997 * 3. This version shares code with the rpcbindv3 and rpcbindv4 query
998 * functions. It can use a TI-RPC generated CLIENT.
1000 unsigned long nfs_pmap_getport(const struct sockaddr_in *sin,
1001 const unsigned short transport,
1002 const unsigned long program,
1003 const unsigned long version,
1004 const unsigned long protocol,
1005 const struct timeval *timeout)
1008 struct pmap parms = {
1011 .pm_prot = protocol,
1013 struct timeval tout = { -1, 0 };
1014 unsigned long port = 0;
1016 if (timeout != NULL)
1019 client = nfs_gp_get_rpcbclient((struct sockaddr *)sin,
1020 (socklen_t)sizeof(*sin),
1021 transport, PMAPVERS, &tout);
1022 if (client != NULL) {
1023 port = nfs_gp_pmap_getport(client, &parms, tout);
1024 CLNT_DESTROY(client);