]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/network.c
Recently #include directives for autoconf's config.h file were added in
[nfs-utils.git] / utils / mount / network.c
1 /*
2  * network.c -- Provide common network functions for NFS mount/umount
3  *
4  * Copyright (C) 2007 Oracle.  All rights reserved.
5  * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
6  *
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.
11  *
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.
16  *
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.
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <netdb.h>
35 #include <time.h>
36 #include <rpc/rpc.h>
37 #include <rpc/pmap_prot.h>
38 #include <rpc/pmap_clnt.h>
39 #include <sys/socket.h>
40
41 #include "xcommon.h"
42 #include "mount.h"
43 #include "nls.h"
44 #include "nfs_mount.h"
45 #include "mount_constants.h"
46 #include "network.h"
47
48 #ifdef HAVE_RPCSVC_NFS_PROT_H
49 #include <rpcsvc/nfs_prot.h>
50 #else
51 #include <linux/nfs.h>
52 #define nfsstat nfs_stat
53 #endif
54
55 #ifndef NFS_PORT
56 #define NFS_PORT 2049
57 #endif
58
59 #define PMAP_TIMEOUT    (10)
60 #define CONNECT_TIMEOUT (20)
61 #define MOUNT_TIMEOUT   (30)
62
63 #if SIZEOF_SOCKLEN_T - 0 == 0
64 #define socklen_t unsigned int
65 #endif
66
67 extern int nfs_mount_data_version;
68 extern char *progname;
69 extern int verbose;
70
71 static const unsigned long nfs_to_mnt[] = {
72         0,
73         0,
74         1,
75         3,
76 };
77
78 static const unsigned long mnt_to_nfs[] = {
79         0,
80         2,
81         2,
82         3,
83 };
84
85 /*
86  * Map an NFS version into the corresponding Mountd version
87  */
88 unsigned long nfsvers_to_mnt(const unsigned long vers)
89 {
90         if (vers <= 3)
91                 return nfs_to_mnt[vers];
92         return 0;
93 }
94
95 /*
96  * Map a Mountd version into the corresponding NFS version
97  */
98 static unsigned long mntvers_to_nfs(const unsigned long vers)
99 {
100         if (vers <= 3)
101                 return mnt_to_nfs[vers];
102         return 0;
103 }
104
105 static const unsigned int probe_udp_only[] = {
106         IPPROTO_UDP,
107         0,
108 };
109
110 static const unsigned int probe_udp_first[] = {
111         IPPROTO_UDP,
112         IPPROTO_TCP,
113         0,
114 };
115
116 static const unsigned int probe_tcp_first[] = {
117         IPPROTO_TCP,
118         IPPROTO_UDP,
119         0,
120 };
121
122 static const unsigned long probe_nfs2_only[] = {
123         2,
124         0,
125 };
126
127 static const unsigned long probe_nfs3_first[] = {
128         3,
129         2,
130         0,
131 };
132
133 static const unsigned long probe_mnt1_first[] = {
134         1,
135         2,
136         0,
137 };
138
139 static const unsigned long probe_mnt3_first[] = {
140         3,
141         1,
142         2,
143         0,
144 };
145
146 /**
147  * nfs_gethostbyname - resolve a hostname to an IPv4 address
148  * @hostname: pointer to a C string containing a DNS hostname
149  * @saddr: returns an IPv4 address 
150  *
151  * Returns 1 if successful, otherwise zero.
152  */
153 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
154 {
155         struct hostent *hp;
156
157         saddr->sin_family = AF_INET;
158         if (!inet_aton(hostname, &saddr->sin_addr)) {
159                 if ((hp = gethostbyname(hostname)) == NULL) {
160                         nfs_error(_("%s: can't get address for %s\n"),
161                                         progname, hostname);
162                         return 0;
163                 } else {
164                         if (hp->h_length > sizeof(*saddr)) {
165                                 nfs_error(_("%s: got bad hp->h_length\n"),
166                                                 progname);
167                                 hp->h_length = sizeof(*saddr);
168                         }
169                         memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
170                 }
171         }
172         return 1;
173 }
174
175 /*
176  * Attempt to connect a socket, but time out after "timeout" seconds.
177  *
178  * On error return, caller closes the socket.
179  */
180 static int connect_to(int fd, struct sockaddr *addr,
181                         socklen_t addrlen, int timeout)
182 {
183         int ret, saved;
184         fd_set rset, wset;
185         struct timeval tv = {
186                 .tv_sec = timeout,
187         };
188
189         saved = fcntl(fd, F_GETFL, 0);
190         fcntl(fd, F_SETFL, saved | O_NONBLOCK);
191
192         ret = connect(fd, addr, addrlen);
193         if (ret < 0 && errno != EINPROGRESS)
194                 return -1;
195         if (ret == 0)
196                 goto out;
197
198         FD_ZERO(&rset);
199         FD_SET(fd, &rset);
200         wset = rset;
201         ret = select(fd + 1, &rset, &wset, NULL, &tv);
202         if (ret == 0) {
203                 errno = ETIMEDOUT;
204                 return -1;
205         }
206         if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
207                 int error;
208                 socklen_t len = sizeof(error);
209                 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
210                         return -1;
211                 if (error) {
212                         errno = error;
213                         return -1;
214                 }
215         } else
216                 return -1;
217
218 out:
219         fcntl(fd, F_SETFL, saved);
220         return 0;
221 }
222
223 /*
224  * Create a socket that is locally bound to a reserved or non-reserved port.
225  *
226  * The caller should check rpc_createerr to determine the cause of any error.
227  */
228 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
229                         unsigned int timeout, int resvp, int conn)
230 {
231         int so, cc, type;
232         struct sockaddr_in laddr;
233         socklen_t namelen = sizeof(laddr);
234
235         type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
236         if ((so = socket (AF_INET, type, p_prot)) < 0)
237                 goto err_socket;
238
239         laddr.sin_family = AF_INET;
240         laddr.sin_port = 0;
241         laddr.sin_addr.s_addr = htonl(INADDR_ANY);
242         if (resvp) {
243                 if (bindresvport(so, &laddr) < 0)
244                         goto err_bindresvport;
245         } else {
246                 cc = bind(so, (struct sockaddr *)&laddr, namelen);
247                 if (cc < 0)
248                         goto err_bind;
249         }
250         if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
251                 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
252                                 timeout);
253                 if (cc < 0)
254                         goto err_connect;
255         }
256         return so;
257
258 err_socket:
259         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
260         rpc_createerr.cf_error.re_errno = errno;
261         if (verbose) {
262                 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
263                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
264                         errno, strerror(errno));
265         }
266         return RPC_ANYSOCK;
267
268 err_bindresvport:
269         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
270         rpc_createerr.cf_error.re_errno = errno;
271         if (verbose) {
272                 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
273                                 " (%s)\n"),
274                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
275                         errno, strerror(errno));
276         }
277         close(so);
278         return RPC_ANYSOCK;
279
280 err_bind:
281         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
282         rpc_createerr.cf_error.re_errno = errno;
283         if (verbose) {
284                 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
285                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
286                         errno, strerror(errno));
287         }
288         close(so);
289         return RPC_ANYSOCK;
290
291 err_connect:
292         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
293         rpc_createerr.cf_error.re_errno = errno;
294         if (verbose) {
295                 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
296                         progname, inet_ntoa(saddr->sin_addr),
297                         ntohs(saddr->sin_port), errno, strerror(errno));
298         }
299         close(so);
300         return RPC_ANYSOCK;
301 }
302
303 /*
304  * getport() is very similar to pmap_getport() with the exception that
305  * this version tries to use an ephemeral port, since reserved ports are
306  * not needed for GETPORT queries.  This conserves the very limited
307  * reserved port space, which helps reduce failed socket binds
308  * during mount storms.
309  *
310  * A side effect of calling this function is that rpccreateerr is set.
311  */
312 static unsigned short getport(struct sockaddr_in *saddr,
313                                 unsigned long program,
314                                 unsigned long version,
315                                 unsigned int proto)
316 {
317         struct sockaddr_in bind_saddr;
318         unsigned short port = 0;
319         int socket;
320         CLIENT *clnt = NULL;
321         enum clnt_stat stat;
322  
323         bind_saddr = *saddr;
324         bind_saddr.sin_port = htons(PMAPPORT);
325
326         socket = get_socket(&bind_saddr, proto, PMAP_TIMEOUT, FALSE, FALSE);
327         if (socket == RPC_ANYSOCK) {
328                 if (proto == IPPROTO_TCP &&
329                     rpc_createerr.cf_error.re_errno == ETIMEDOUT)
330                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
331                 return 0;
332         }
333
334         switch (proto) {
335         case IPPROTO_UDP:
336                 clnt = clntudp_bufcreate(&bind_saddr,
337                                          PMAPPROG, PMAPVERS,
338                                          RETRY_TIMEOUT, &socket,
339                                          RPCSMALLMSGSIZE,
340                                          RPCSMALLMSGSIZE);
341                 break;
342         case IPPROTO_TCP:
343                 clnt = clnttcp_create(&bind_saddr,
344                                       PMAPPROG, PMAPVERS,
345                                       &socket,
346                                       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
347                 break;
348         }
349         if (clnt != NULL) {
350                 struct pmap parms = {
351                         .pm_prog        = program,
352                         .pm_vers        = version,
353                         .pm_prot        = proto,
354                 };
355
356                 stat = clnt_call(clnt, PMAPPROC_GETPORT,
357                                  (xdrproc_t)xdr_pmap, (caddr_t)&parms,
358                                  (xdrproc_t)xdr_u_short, (caddr_t)&port,
359                                  TIMEOUT);
360                 if (stat) {
361                         clnt_geterr(clnt, &rpc_createerr.cf_error);
362                         rpc_createerr.cf_stat = stat;
363                 }
364                 clnt_destroy(clnt);
365                 if (stat != RPC_SUCCESS)
366                         port = 0;
367                 else if (port == 0)
368                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
369         }
370         close(socket);
371
372         return port;
373 }
374
375 /*
376  * Use the portmapper to discover whether or not the service we want is
377  * available. The lists 'versions' and 'protos' define ordered sequences
378  * of service versions and udp/tcp protocols to probe for.
379  */
380 static int probe_port(clnt_addr_t *server, const unsigned long *versions,
381                         const unsigned int *protos)
382 {
383         struct sockaddr_in *saddr = &server->saddr;
384         struct pmap *pmap = &server->pmap;
385         const unsigned long prog = pmap->pm_prog, *p_vers;
386         const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
387         const u_short port = (u_short) pmap->pm_port;
388         unsigned long vers = pmap->pm_vers;
389         unsigned short p_port;
390
391         p_prot = prot ? &prot : protos;
392         p_vers = vers ? &vers : versions;
393         rpc_createerr.cf_stat = 0;
394         for (;;) {
395                 p_port = getport(saddr, prog, *p_vers, *p_prot);
396                 if (p_port) {
397                         if (!port || port == p_port) {
398                                 saddr->sin_port = htons(p_port);
399                                 if (verbose) {
400                                         printf(_("%s: trying %s prog %ld vers "
401                                                 "%ld prot %s port %d\n"),
402                                                 progname,
403                                                 inet_ntoa(saddr->sin_addr),
404                                                 prog, *p_vers,
405                                                 *p_prot == IPPROTO_UDP ?
406                                                         _("UDP") : _("TCP"),
407                                                 p_port);
408                                 }
409                                 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
410                                         goto out_ok;
411                                 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
412                                         goto out_bad;
413                         }
414                 }
415                 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
416                     rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
417                         goto out_bad;
418
419                 if (!prot) {
420                         if (*++p_prot)
421                                 continue;
422                         p_prot = protos;
423                 }
424                 if (vers || !*++p_vers)
425                         break;
426         }
427
428 out_bad:
429         return 0;
430
431 out_ok:
432         if (!vers)
433                 pmap->pm_vers = *p_vers;
434         if (!prot)
435                 pmap->pm_prot = *p_prot;
436         if (!port)
437                 pmap->pm_port = p_port;
438         rpc_createerr.cf_stat = 0;
439         return 1;
440 }
441
442 static int probe_nfsport(clnt_addr_t *nfs_server)
443 {
444         struct pmap *pmap = &nfs_server->pmap;
445
446         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
447                 return 1;
448
449         if (nfs_mount_data_version >= 4)
450                 return probe_port(nfs_server, probe_nfs3_first, probe_tcp_first);
451         else
452                 return probe_port(nfs_server, probe_nfs2_only, probe_udp_only);
453 }
454
455 static int probe_mntport(clnt_addr_t *mnt_server)
456 {
457         struct pmap *pmap = &mnt_server->pmap;
458
459         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
460                 return 1;
461
462         if (nfs_mount_data_version >= 4)
463                 return probe_port(mnt_server, probe_mnt3_first, probe_udp_first);
464         else
465                 return probe_port(mnt_server, probe_mnt1_first, probe_udp_only);
466 }
467
468 /**
469  * probe_bothports - discover the RPC endpoints of mountd and NFS server
470  * @mnt_server: pointer to address and pmap argument for mountd results
471  * @nfs_server: pointer to address and pmap argument for NFS server
472  *
473  * Returns 1 if successful, otherwise zero if some error occurred.
474  * Note that the arguments are both input and output arguments.
475  *
476  * A side effect of calling this function is that rpccreateerr is set.
477  */
478 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
479 {
480         struct pmap *nfs_pmap = &nfs_server->pmap;
481         struct pmap *mnt_pmap = &mnt_server->pmap;
482         struct pmap save_nfs, save_mnt;
483         int res;
484         const unsigned long *probe_vers;
485
486         if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
487                 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
488         else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
489                 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
490         if (nfs_pmap->pm_vers)
491                 goto version_fixed;
492
493         memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
494         memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
495         probe_vers = (nfs_mount_data_version >= 4) ?
496                         probe_mnt3_first : probe_mnt1_first;
497
498         for (; *probe_vers; probe_vers++) {
499                 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
500                 if ((res = probe_nfsport(nfs_server) != 0)) {
501                         mnt_pmap->pm_vers = *probe_vers;
502                         if ((res = probe_mntport(mnt_server)) != 0)
503                                 return 1;
504                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
505                 }
506                 switch (rpc_createerr.cf_stat) {
507                 case RPC_PROGVERSMISMATCH:
508                 case RPC_PROGNOTREGISTERED:
509                         break;
510                 default:
511                         goto out_bad;
512                 }
513                 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
514         }
515
516 out_bad:
517         return 0;
518
519 version_fixed:
520         if (!probe_nfsport(nfs_server))
521                 goto out_bad;
522         return probe_mntport(mnt_server);
523 }
524
525 static int probe_statd(void)
526 {
527         struct sockaddr_in addr;
528         unsigned short port;
529
530         memset(&addr, 0, sizeof(addr));
531         addr.sin_family = AF_INET;
532         addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
533         port = getport(&addr, 100024, 1, IPPROTO_UDP);
534
535         if (port == 0)
536                 return 0;
537         addr.sin_port = htons(port);
538
539         if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
540                 return 0;
541
542         return 1;
543 }
544
545 /**
546  * start_statd - attempt to start rpc.statd
547  *
548  * Returns 1 if statd is running; otherwise zero.
549  */
550 int start_statd(void)
551 {
552 #ifdef START_STATD
553         struct stat stb;
554 #endif
555
556         if (probe_statd())
557                 return 1;
558
559 #ifdef START_STATD
560         if (stat(START_STATD, &stb) == 0) {
561                 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
562                         system(START_STATD);
563                         if (probe_statd())
564                                 return 1;
565                 }
566         }
567 #endif
568
569         return 0;
570 }
571
572 /**
573  * nfs_call_umount - ask the server to remove a share from it's rmtab
574  * @mnt_server: address of RPC MNT program server
575  * @argp: directory path of share to "unmount"
576  *
577  * Returns one if the unmount call succeeded; zero if the unmount
578  * failed for any reason.
579  *
580  * Note that a side effect of calling this function is that rpccreateerr
581  * is set.
582  */
583 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
584 {
585         CLIENT *clnt;
586         enum clnt_stat res = 0;
587         int msock;
588
589         if (!probe_mntport(mnt_server))
590                 return 0;
591         clnt = mnt_openclnt(mnt_server, &msock);
592         if (!clnt)
593                 return 0;
594         res = clnt_call(clnt, MOUNTPROC_UMNT,
595                         (xdrproc_t)xdr_dirpath, (caddr_t)argp,
596                         (xdrproc_t)xdr_void, NULL,
597                         TIMEOUT);
598         mnt_closeclnt(clnt, msock);
599
600         if (res == RPC_SUCCESS)
601                 return 1;
602         return 0;
603 }
604
605 /**
606  * mnt_openclnt - get a handle for a remote mountd service
607  * @mnt_server: address and pmap arguments of mountd service
608  * @msock: returns a file descriptor of the underlying transport socket
609  *
610  * Returns an active handle for the remote's mountd service
611  */
612 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
613 {
614         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
615         struct pmap *mnt_pmap = &mnt_server->pmap;
616         CLIENT *clnt = NULL;
617
618         mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
619         *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
620                                 TRUE, FALSE);
621         if (*msock == RPC_ANYSOCK) {
622                 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
623                         /*
624                          * Probably in-use by a TIME_WAIT connection,
625                          * It is worth waiting a while and trying again.
626                          */
627                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
628                 return NULL;
629         }
630
631         switch (mnt_pmap->pm_prot) {
632         case IPPROTO_UDP:
633                 clnt = clntudp_bufcreate(mnt_saddr,
634                                          mnt_pmap->pm_prog, mnt_pmap->pm_vers,
635                                          RETRY_TIMEOUT, msock,
636                                          MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
637                 break;
638         case IPPROTO_TCP:
639                 clnt = clnttcp_create(mnt_saddr,
640                                       mnt_pmap->pm_prog, mnt_pmap->pm_vers,
641                                       msock,
642                                       MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
643                 break;
644         }
645         if (clnt) {
646                 /* try to mount hostname:dirname */
647                 clnt->cl_auth = authunix_create_default();
648                 return clnt;
649         }
650         return NULL;
651 }
652
653 /**
654  * mnt_closeclnt - terminate a handle for a remote mountd service
655  * @clnt: pointer to an active handle for a remote mountd service
656  * @msock: file descriptor of the underlying transport socket
657  *
658  */
659 void mnt_closeclnt(CLIENT *clnt, int msock)
660 {
661         auth_destroy(clnt->cl_auth);
662         clnt_destroy(clnt);
663         close(msock);
664 }
665
666 /**
667  * clnt_ping - send an RPC ping to the remote RPC service endpoint
668  * @saddr: server's address
669  * @prog: target RPC program number
670  * @vers: target RPC version number
671  * @prot: target RPC protocol
672  * @caddr: filled in with our network address
673  *
674  * Sigh... getport() doesn't actually check the version number.
675  * In order to make sure that the server actually supports the service
676  * we're requesting, we open and RPC client, and fire off a NULL
677  * RPC call.
678  *
679  * caddr is the network address that the server will use to call us back.
680  * On multi-homed clients, this address depends on which NIC we use to
681  * route requests to the server.
682  *
683  * Returns one if successful, otherwise zero.
684  */
685 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
686                 const unsigned long vers, const unsigned int prot,
687                 struct sockaddr_in *caddr)
688 {
689         CLIENT *clnt = NULL;
690         int sock, stat;
691         static char clnt_res;
692         struct sockaddr dissolve;
693
694         rpc_createerr.cf_stat = stat = 0;
695         sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
696         if (sock == RPC_ANYSOCK) {
697                 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
698                         /*
699                          * TCP timeout. Bubble up the error to see 
700                          * how it should be handled.
701                          */
702                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
703                 }
704                 return 0;
705         }
706
707         if (caddr) {
708                 /* Get the address of our end of this connection */
709                 socklen_t len = sizeof(*caddr);
710                 if (getsockname(sock, caddr, &len) != 0)
711                         caddr->sin_family = 0;
712         }
713
714         switch(prot) {
715         case IPPROTO_UDP:
716                 /* The socket is connected (so we could getsockname successfully),
717                  * but some servers on multi-homed hosts reply from
718                  * the wrong address, so if we stay connected, we lose the reply.
719                  */
720                 dissolve.sa_family = AF_UNSPEC;
721                 connect(sock, &dissolve, sizeof(dissolve));
722
723                 clnt = clntudp_bufcreate(saddr, prog, vers,
724                                          RETRY_TIMEOUT, &sock,
725                                          RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
726                 break;
727         case IPPROTO_TCP:
728                 clnt = clnttcp_create(saddr, prog, vers, &sock,
729                                       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
730                 break;
731         }
732         if (!clnt) {
733                 close(sock);
734                 return 0;
735         }
736         memset(&clnt_res, 0, sizeof(clnt_res));
737         stat = clnt_call(clnt, NULLPROC,
738                          (xdrproc_t)xdr_void, (caddr_t)NULL,
739                          (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
740                          TIMEOUT);
741         if (stat) {
742                 clnt_geterr(clnt, &rpc_createerr.cf_error);
743                 rpc_createerr.cf_stat = stat;
744         }
745         clnt_destroy(clnt);
746         close(sock);
747
748         if (stat == RPC_SUCCESS)
749                 return 1;
750         else
751                 return 0;
752 }
753
754 /**
755  * get_client_address - acquire our local network address
756  * @saddr: server's address
757  * @caddr: filled in with our network address
758  *
759  * Discover a network address that the server will use to call us back.
760  * On multi-homed clients, this address depends on which NIC we use to
761  * route requests to the server.
762  *
763  * Use a connected datagram socket so as not to leave a socket in TIME_WAIT.
764  *
765  * Returns one if successful, otherwise zero.
766  */
767 int get_client_address(struct sockaddr_in *saddr, struct sockaddr_in *caddr)
768 {
769         socklen_t len = sizeof(*caddr);
770         int socket, err;
771
772         socket = get_socket(saddr, IPPROTO_UDP, CONNECT_TIMEOUT, FALSE, TRUE);
773         if (socket == RPC_ANYSOCK)
774                 return 0;
775
776         err = getsockname(socket, caddr, &len);
777         close(socket);
778
779         if (err && verbose) {
780                 nfs_error(_("%s: getsockname failed: %s"),
781                                 progname, strerror(errno));
782                 return 0;
783         }
784         return 1;
785 }