2 * network.c -- Provide common network functions for NFS mount/umount
4 * Copyright (C) 2007 Oracle. All rights reserved.
5 * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 021110-1307, USA.
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
41 #include <rpc/pmap_prot.h>
42 #include <rpc/pmap_clnt.h>
47 #include "nfs_mount.h"
48 #include "mount_constants.h"
52 * Earlier versions of glibc's /usr/include/netdb.h exclude these
53 * definitions because it was thought they were not part of a stable
54 * POSIX standard. However, they are defined by RFC 2553 and 3493
55 * and in POSIX 1003.1-2001, so these definitions were added in later
56 * versions of netdb.h.
59 #define AI_V4MAPPED 0x0008 /* IPv4-mapped addresses are acceptable. */
60 #endif /* AI_V4MAPPED */
62 #define AI_ALL 0x0010 /* Return both IPv4 and IPv6 addresses. */
65 #define AI_ADDRCONFIG 0x0020 /* Use configuration of this host to choose \
66 returned address type. */
67 #endif /* AI_ADDRCONFIG */
69 #define PMAP_TIMEOUT (10)
70 #define CONNECT_TIMEOUT (20)
71 #define MOUNT_TIMEOUT (30)
73 #if SIZEOF_SOCKLEN_T - 0 == 0
74 #define socklen_t unsigned int
77 extern int nfs_mount_data_version;
78 extern char *progname;
81 static const unsigned long nfs_to_mnt[] = {
88 static const unsigned long mnt_to_nfs[] = {
96 * Map an NFS version into the corresponding Mountd version
98 unsigned long nfsvers_to_mnt(const unsigned long vers)
101 return nfs_to_mnt[vers];
106 * Map a Mountd version into the corresponding NFS version
108 static unsigned long mntvers_to_nfs(const unsigned long vers)
111 return mnt_to_nfs[vers];
115 static const unsigned int probe_udp_only[] = {
120 static const unsigned int probe_udp_first[] = {
126 static const unsigned int probe_tcp_first[] = {
132 static const unsigned long probe_nfs2_only[] = {
137 static const unsigned long probe_nfs3_first[] = {
143 static const unsigned long probe_mnt1_first[] = {
149 static const unsigned long probe_mnt3_first[] = {
157 * nfs_name_to_address - resolve hostname to an IPv4 or IPv6 socket address
158 * @hostname: pointer to C string containing DNS hostname to resolve
159 * @sap: pointer to buffer to fill with socket address
160 * @len: IN: size of buffer to fill; OUT: size of socket address
162 * Returns 1 and places a socket address at @sap if successful;
165 int nfs_name_to_address(const char *hostname,
166 const sa_family_t af_hint,
167 struct sockaddr *sap, socklen_t *salen)
169 struct addrinfo *gai_results;
170 struct addrinfo gai_hint = {
171 .ai_family = af_hint,
172 .ai_flags = AI_ADDRCONFIG,
174 socklen_t len = *salen;
177 if (af_hint == AF_INET6)
178 gai_hint.ai_flags |= AI_V4MAPPED|AI_ALL;
182 error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
184 nfs_error(_("%s: DNS resolution failed for %s: %s"),
185 progname, hostname, (error == EAI_SYSTEM ?
186 strerror(errno) : gai_strerror(error)));
190 switch (gai_results->ai_addr->sa_family) {
193 if (len >= gai_results->ai_addrlen) {
194 *salen = gai_results->ai_addrlen;
195 memcpy(sap, gai_results->ai_addr, *salen);
200 /* things are really broken if we get here, so warn */
201 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
206 freeaddrinfo(gai_results);
211 * nfs_gethostbyname - resolve a hostname to an IPv4 address
212 * @hostname: pointer to a C string containing a DNS hostname
213 * @saddr: returns an IPv4 address
215 * Returns 1 if successful, otherwise zero.
217 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
219 socklen_t len = sizeof(*sin);
221 return nfs_name_to_address(hostname, AF_INET,
222 (struct sockaddr *)sin, &len);
226 * nfs_string_to_sockaddr - convert string address to sockaddr
227 * @address: pointer to presentation format address to convert
228 * @addrlen: length of presentation address
229 * @sap: pointer to socket address buffer to fill in
230 * @salen: IN: length of address buffer
231 * OUT: length of converted socket address
233 * Convert a presentation format address string to a socket address.
234 * Similar to nfs_name_to_address(), but the DNS query is squelched,
235 * and won't make any noise if the getaddrinfo() call fails.
237 * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
239 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
240 * on presenting IPv6 addresses as text strings.
242 int nfs_string_to_sockaddr(const char *address, const size_t addrlen,
243 struct sockaddr *sap, socklen_t *salen)
245 struct addrinfo *gai_results;
246 struct addrinfo gai_hint = {
247 .ai_flags = AI_NUMERICHOST,
249 socklen_t len = *salen;
254 if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
255 switch (gai_results->ai_addr->sa_family) {
258 if (len >= gai_results->ai_addrlen) {
259 *salen = gai_results->ai_addrlen;
260 memcpy(sap, gai_results->ai_addr, *salen);
265 freeaddrinfo(gai_results);
272 * nfs_present_sockaddr - convert sockaddr to string
273 * @sap: pointer to socket address to convert
274 * @salen: length of socket address
275 * @buf: pointer to buffer to fill in
276 * @buflen: length of buffer
278 * Convert the passed-in sockaddr-style address to presentation format.
279 * The presentation format address is placed in @buf and is
282 * Returns 1 if successful; otherwise zero.
284 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
285 * on presenting IPv6 addresses as text strings.
287 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
288 char *buf, const size_t buflen)
290 #ifdef HAVE_GETNAMEINFO
293 result = getnameinfo(sap, salen, buf, buflen,
294 NULL, 0, NI_NUMERICHOST);
298 nfs_error(_("%s: invalid server address: %s"), progname,
299 gai_strerror(result));
301 #else /* HAVE_GETNAMEINFO */
304 if (sap->sa_family == AF_INET) {
305 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
306 if (addr && strlen(addr) < buflen) {
312 nfs_error(_("%s: invalid server address"), progname);
314 #endif /* HAVE_GETNAMEINFO */
318 * Attempt to connect a socket, but time out after "timeout" seconds.
320 * On error return, caller closes the socket.
322 static int connect_to(int fd, struct sockaddr *addr,
323 socklen_t addrlen, int timeout)
327 struct timeval tv = {
331 saved = fcntl(fd, F_GETFL, 0);
332 fcntl(fd, F_SETFL, saved | O_NONBLOCK);
334 ret = connect(fd, addr, addrlen);
335 if (ret < 0 && errno != EINPROGRESS)
343 ret = select(fd + 1, &rset, &wset, NULL, &tv);
348 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
350 socklen_t len = sizeof(error);
351 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
361 fcntl(fd, F_SETFL, saved);
366 * Create a socket that is locally bound to a reserved or non-reserved port.
368 * The caller should check rpc_createerr to determine the cause of any error.
370 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
371 unsigned int timeout, int resvp, int conn)
374 struct sockaddr_in laddr;
375 socklen_t namelen = sizeof(laddr);
377 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
378 if ((so = socket (AF_INET, type, p_prot)) < 0)
381 laddr.sin_family = AF_INET;
383 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
385 if (bindresvport(so, &laddr) < 0)
386 goto err_bindresvport;
388 cc = bind(so, (struct sockaddr *)&laddr, namelen);
392 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
393 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
401 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
402 rpc_createerr.cf_error.re_errno = errno;
404 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
405 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
406 errno, strerror(errno));
411 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
412 rpc_createerr.cf_error.re_errno = errno;
414 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
416 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
417 errno, strerror(errno));
423 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
424 rpc_createerr.cf_error.re_errno = errno;
426 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
427 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
428 errno, strerror(errno));
434 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
435 rpc_createerr.cf_error.re_errno = errno;
437 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
438 progname, inet_ntoa(saddr->sin_addr),
439 ntohs(saddr->sin_port), errno, strerror(errno));
446 * getport() is very similar to pmap_getport() with the exception that
447 * this version tries to use an ephemeral port, since reserved ports are
448 * not needed for GETPORT queries. This conserves the very limited
449 * reserved port space, which helps reduce failed socket binds
450 * during mount storms.
452 * A side effect of calling this function is that rpccreateerr is set.
454 static unsigned short getport(struct sockaddr_in *saddr,
455 unsigned long program,
456 unsigned long version,
459 struct sockaddr_in bind_saddr;
460 unsigned short port = 0;
466 bind_saddr.sin_port = htons(PMAPPORT);
468 socket = get_socket(&bind_saddr, proto, PMAP_TIMEOUT, FALSE, TRUE);
469 if (socket == RPC_ANYSOCK) {
470 if (proto == IPPROTO_TCP &&
471 rpc_createerr.cf_error.re_errno == ETIMEDOUT)
472 rpc_createerr.cf_stat = RPC_TIMEDOUT;
478 clnt = clntudp_bufcreate(&bind_saddr,
480 RETRY_TIMEOUT, &socket,
485 clnt = clnttcp_create(&bind_saddr,
488 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
492 struct pmap parms = {
498 stat = clnt_call(clnt, PMAPPROC_GETPORT,
499 (xdrproc_t)xdr_pmap, (caddr_t)&parms,
500 (xdrproc_t)xdr_u_short, (caddr_t)&port,
503 clnt_geterr(clnt, &rpc_createerr.cf_error);
504 rpc_createerr.cf_stat = stat;
507 if (stat != RPC_SUCCESS)
510 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
518 * Use the portmapper to discover whether or not the service we want is
519 * available. The lists 'versions' and 'protos' define ordered sequences
520 * of service versions and udp/tcp protocols to probe for.
522 static int probe_port(clnt_addr_t *server, const unsigned long *versions,
523 const unsigned int *protos)
525 struct sockaddr_in *saddr = &server->saddr;
526 struct pmap *pmap = &server->pmap;
527 const unsigned long prog = pmap->pm_prog, *p_vers;
528 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
529 const u_short port = (u_short) pmap->pm_port;
530 unsigned long vers = pmap->pm_vers;
531 unsigned short p_port;
533 p_prot = prot ? &prot : protos;
534 p_vers = vers ? &vers : versions;
535 rpc_createerr.cf_stat = 0;
537 p_port = getport(saddr, prog, *p_vers, *p_prot);
539 if (!port || port == p_port) {
540 saddr->sin_port = htons(p_port);
542 printf(_("%s: trying %s prog %ld vers "
543 "%ld prot %s port %d\n"),
545 inet_ntoa(saddr->sin_addr),
547 *p_prot == IPPROTO_UDP ?
551 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
555 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
556 rpc_createerr.cf_stat != RPC_TIMEDOUT &&
557 rpc_createerr.cf_stat != RPC_CANTRECV &&
558 rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
566 if (rpc_createerr.cf_stat == RPC_TIMEDOUT ||
567 rpc_createerr.cf_stat == RPC_CANTRECV)
570 if (vers || !*++p_vers)
579 pmap->pm_vers = *p_vers;
581 pmap->pm_prot = *p_prot;
583 pmap->pm_port = p_port;
584 rpc_createerr.cf_stat = 0;
588 static int probe_nfsport(clnt_addr_t *nfs_server)
590 struct pmap *pmap = &nfs_server->pmap;
592 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
595 if (nfs_mount_data_version >= 4)
596 return probe_port(nfs_server, probe_nfs3_first, probe_tcp_first);
598 return probe_port(nfs_server, probe_nfs2_only, probe_udp_only);
601 static int probe_mntport(clnt_addr_t *mnt_server)
603 struct pmap *pmap = &mnt_server->pmap;
605 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
608 if (nfs_mount_data_version >= 4)
609 return probe_port(mnt_server, probe_mnt3_first, probe_udp_first);
611 return probe_port(mnt_server, probe_mnt1_first, probe_udp_only);
615 * probe_bothports - discover the RPC endpoints of mountd and NFS server
616 * @mnt_server: pointer to address and pmap argument for mountd results
617 * @nfs_server: pointer to address and pmap argument for NFS server
619 * Returns 1 if successful, otherwise zero if some error occurred.
620 * Note that the arguments are both input and output arguments.
622 * A side effect of calling this function is that rpccreateerr is set.
624 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
626 struct pmap *nfs_pmap = &nfs_server->pmap;
627 struct pmap *mnt_pmap = &mnt_server->pmap;
628 struct pmap save_nfs, save_mnt;
630 const unsigned long *probe_vers;
632 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
633 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
634 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
635 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
636 if (nfs_pmap->pm_vers)
639 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
640 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
641 probe_vers = (nfs_mount_data_version >= 4) ?
642 probe_mnt3_first : probe_mnt1_first;
644 for (; *probe_vers; probe_vers++) {
645 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
646 if ((res = probe_nfsport(nfs_server) != 0)) {
647 mnt_pmap->pm_vers = *probe_vers;
648 if ((res = probe_mntport(mnt_server)) != 0)
650 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
652 switch (rpc_createerr.cf_stat) {
653 case RPC_PROGVERSMISMATCH:
654 case RPC_PROGNOTREGISTERED:
659 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
666 if (!probe_nfsport(nfs_server))
668 return probe_mntport(mnt_server);
671 static int probe_statd(void)
673 struct sockaddr_in addr;
676 memset(&addr, 0, sizeof(addr));
677 addr.sin_family = AF_INET;
678 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
679 port = getport(&addr, 100024, 1, IPPROTO_UDP);
683 addr.sin_port = htons(port);
685 if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
692 * start_statd - attempt to start rpc.statd
694 * Returns 1 if statd is running; otherwise zero.
696 int start_statd(void)
706 if (stat(START_STATD, &stb) == 0) {
707 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
719 * nfs_call_umount - ask the server to remove a share from it's rmtab
720 * @mnt_server: address of RPC MNT program server
721 * @argp: directory path of share to "unmount"
723 * Returns one if the unmount call succeeded; zero if the unmount
724 * failed for any reason.
726 * Note that a side effect of calling this function is that rpccreateerr
729 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
732 enum clnt_stat res = 0;
735 if (!probe_mntport(mnt_server))
737 clnt = mnt_openclnt(mnt_server, &msock);
740 res = clnt_call(clnt, MOUNTPROC_UMNT,
741 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
742 (xdrproc_t)xdr_void, NULL,
744 mnt_closeclnt(clnt, msock);
746 if (res == RPC_SUCCESS)
752 * mnt_openclnt - get a handle for a remote mountd service
753 * @mnt_server: address and pmap arguments of mountd service
754 * @msock: returns a file descriptor of the underlying transport socket
756 * Returns an active handle for the remote's mountd service
758 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
760 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
761 struct pmap *mnt_pmap = &mnt_server->pmap;
764 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
765 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
767 if (*msock == RPC_ANYSOCK) {
768 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
770 * Probably in-use by a TIME_WAIT connection,
771 * It is worth waiting a while and trying again.
773 rpc_createerr.cf_stat = RPC_TIMEDOUT;
777 switch (mnt_pmap->pm_prot) {
779 clnt = clntudp_bufcreate(mnt_saddr,
780 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
781 RETRY_TIMEOUT, msock,
782 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
785 clnt = clnttcp_create(mnt_saddr,
786 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
788 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
792 /* try to mount hostname:dirname */
793 clnt->cl_auth = authunix_create_default();
800 * mnt_closeclnt - terminate a handle for a remote mountd service
801 * @clnt: pointer to an active handle for a remote mountd service
802 * @msock: file descriptor of the underlying transport socket
805 void mnt_closeclnt(CLIENT *clnt, int msock)
807 auth_destroy(clnt->cl_auth);
813 * clnt_ping - send an RPC ping to the remote RPC service endpoint
814 * @saddr: server's address
815 * @prog: target RPC program number
816 * @vers: target RPC version number
817 * @prot: target RPC protocol
818 * @caddr: filled in with our network address
820 * Sigh... getport() doesn't actually check the version number.
821 * In order to make sure that the server actually supports the service
822 * we're requesting, we open and RPC client, and fire off a NULL
825 * caddr is the network address that the server will use to call us back.
826 * On multi-homed clients, this address depends on which NIC we use to
827 * route requests to the server.
829 * Returns one if successful, otherwise zero.
831 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
832 const unsigned long vers, const unsigned int prot,
833 struct sockaddr_in *caddr)
837 static char clnt_res;
838 struct sockaddr dissolve;
840 rpc_createerr.cf_stat = stat = 0;
841 sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
842 if (sock == RPC_ANYSOCK) {
843 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
845 * TCP timeout. Bubble up the error to see
846 * how it should be handled.
848 rpc_createerr.cf_stat = RPC_TIMEDOUT;
854 /* Get the address of our end of this connection */
855 socklen_t len = sizeof(*caddr);
856 if (getsockname(sock, caddr, &len) != 0)
857 caddr->sin_family = 0;
862 /* The socket is connected (so we could getsockname successfully),
863 * but some servers on multi-homed hosts reply from
864 * the wrong address, so if we stay connected, we lose the reply.
866 dissolve.sa_family = AF_UNSPEC;
867 connect(sock, &dissolve, sizeof(dissolve));
869 clnt = clntudp_bufcreate(saddr, prog, vers,
870 RETRY_TIMEOUT, &sock,
871 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
874 clnt = clnttcp_create(saddr, prog, vers, &sock,
875 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
882 memset(&clnt_res, 0, sizeof(clnt_res));
883 stat = clnt_call(clnt, NULLPROC,
884 (xdrproc_t)xdr_void, (caddr_t)NULL,
885 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
888 clnt_geterr(clnt, &rpc_createerr.cf_error);
889 rpc_createerr.cf_stat = stat;
894 if (stat == RPC_SUCCESS)
901 * Try a getsockname() on a connected datagram socket.
903 * Returns 1 and fills in @buf if successful; otherwise, zero.
905 * A connected datagram socket prevents leaving a socket in TIME_WAIT.
906 * This conserves the ephemeral port number space, helping reduce failed
907 * socket binds during mount storms.
909 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
910 struct sockaddr *buf, socklen_t *buflen)
912 struct sockaddr_in sin = {
913 .sin_family = AF_INET,
914 .sin_addr.s_addr = htonl(INADDR_ANY),
916 struct sockaddr_in6 sin6 = {
917 .sin6_family = AF_INET6,
918 .sin6_addr = IN6ADDR_ANY_INIT,
922 sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
926 switch (sap->sa_family) {
928 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
934 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
940 errno = EAFNOSUPPORT;
944 if (connect(sock, sap, salen) < 0) {
949 return !getsockname(sock, buf, buflen);
953 * Try to generate an address that prevents the server from calling us.
955 * Returns 1 and fills in @buf if successful; otherwise, zero.
957 static int nfs_ca_gai(const struct sockaddr *sap, const socklen_t salen,
958 struct sockaddr *buf, socklen_t *buflen)
960 struct addrinfo *gai_results;
961 struct addrinfo gai_hint = {
962 .ai_family = sap->sa_family,
963 .ai_flags = AI_PASSIVE, /* ANYADDR */
966 if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
969 *buflen = gai_results->ai_addrlen;
970 memcpy(buf, gai_results->ai_addr, *buflen);
972 freeaddrinfo(gai_results);
978 * nfs_callback_address - acquire our local network address
979 * @sap: pointer to address of remote
980 * @sap_len: length of address
981 * @buf: pointer to buffer to be filled in with local network address
982 * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
984 * Discover a network address that an NFSv4 server can use to call us back.
985 * On multi-homed clients, this address depends on which NIC we use to
986 * route requests to the server.
988 * Returns 1 and fills in @buf if an unambiguous local address is
989 * available; returns 1 and fills in an appropriate ANYADDR address
990 * if a local address isn't available; otherwise, returns zero.
992 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
993 struct sockaddr *buf, socklen_t *buflen)
995 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
997 if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
998 if (nfs_ca_gai(sap, salen, buf, buflen) == 0)
1002 * The server can't use an interface ID that was generated
1003 * here on the client, so always clear sin6_scope_id.
1005 if (sin6->sin6_family == AF_INET6)
1006 sin6->sin6_scope_id = 0;
1013 nfs_error(_("%s: failed to construct callback address"));