]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/conn.c
970abfe38025d3031cf0ec1c547c931226afecd8
[nfs-utils.git] / support / nfs / conn.c
1 /*
2  * conn.c -- NFS client mount / umount connection code support functions
3  *
4  * 2006-06-06 Amit Gud <agud@redhat.com>
5  * - Moved code snippets to nfs-utils/support/nfs from util-linux/mount/nfsmount.c
6  *
7  */
8
9 #include "config.h"
10 #include <errno.h>
11 #include <unistd.h>
12 #include <rpc/rpc.h>
13 #include <rpc/pmap_prot.h>
14 #include <rpc/pmap_clnt.h>
15 #include <sys/socket.h>
16 #include <sys/stat.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19
20 #include "conn.h"
21
22 #if SIZEOF_SOCKLEN_T - 0 == 0
23 #define socklen_t int
24 #endif
25
26 extern int verbose;
27
28 /*
29  * Create a socket that is locally bound to a 
30  * reserve or non-reserve port. For any failures,
31  * RPC_ANYSOCK is returned which will cause 
32  * the RPC code to create the socket instead. 
33  */
34 int get_socket(struct sockaddr_in *saddr, u_int p_prot, int resvp, int conn)
35 {
36         int so, cc, type;
37         struct sockaddr_in laddr;
38         socklen_t namelen = sizeof(laddr);
39
40         type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
41         if ((so = socket (AF_INET, type, p_prot)) < 0) {
42                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
43                 rpc_createerr.cf_error.re_errno = errno;
44                 if (verbose) {
45                         fprintf(stderr, 
46                                 "mount: Unable to create %s socket: errno %d (%s)\n",
47                                 p_prot == IPPROTO_UDP ? "UDP" : "TCP", 
48                                 errno, strerror(errno));
49                 }
50                 return RPC_ANYSOCK;
51         }
52         laddr.sin_family = AF_INET;
53         laddr.sin_port = 0;
54         laddr.sin_addr.s_addr = htonl(INADDR_ANY);
55         if (resvp) {
56                 if (bindresvport(so, &laddr) < 0) {
57                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
58                         rpc_createerr.cf_error.re_errno = errno;
59                         if (verbose) {
60                                 fprintf(stderr, 
61                                         "mount: Unable to bindresvport %s socket: errno %d (%s)\n",
62                                         p_prot == IPPROTO_UDP ? "UDP" : "TCP", 
63                                         errno, strerror(errno));
64                         }
65                         close(so);
66                         return RPC_ANYSOCK;
67                 }
68         } else {
69                 cc = bind(so, (struct sockaddr *)&laddr, namelen);
70                 if (cc < 0) {
71                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
72                         rpc_createerr.cf_error.re_errno = errno;
73                         if (verbose) {
74                                 fprintf(stderr, 
75                                         "mount: Unable to bind to %s socket: errno %d (%s)\n",
76                                         p_prot == IPPROTO_UDP ? "UDP" : "TCP", 
77                                         errno, strerror(errno));
78                         }
79                         close(so);
80                         return RPC_ANYSOCK;
81                 }
82         }
83         if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
84                 cc = connect(so, (struct sockaddr *)saddr, namelen);
85                 if (cc < 0) {
86                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
87                         rpc_createerr.cf_error.re_errno = errno;
88                         if (verbose) {
89                                 fprintf(stderr, 
90                                         "mount: Unable to connect to %s:%d, errno %d (%s)\n",
91                                         inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port),
92                                         errno, strerror(errno));
93                         }
94                         close(so);
95                         return RPC_ANYSOCK;
96                 }
97         }
98         return so;
99 }
100
101 /*
102  * Sigh... getport() doesn't actually check the version number.
103  * In order to make sure that the server actually supports the service
104  * we're requesting, we open and RPC client, and fire off a NULL
105  * RPC call.
106  */
107 int
108 clnt_ping(struct sockaddr_in *saddr, const u_long prog, const u_long vers,
109           const u_int prot, struct sockaddr_in *caddr)
110 {
111         CLIENT *clnt=NULL;
112         int sock, stat;
113         static char clnt_res;
114         struct sockaddr dissolve;
115
116         rpc_createerr.cf_stat = stat = errno = 0;
117         sock = get_socket(saddr, prot, FALSE, TRUE);
118         if (sock == RPC_ANYSOCK) {
119                 if (errno == ETIMEDOUT) {
120                         /*
121                          * TCP timeout. Bubble up the error to see 
122                          * how it should be handled.
123                          */
124                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
125                 }
126                 return 0;
127         }
128
129         if (caddr) {
130                 /* Get the address of our end of this connection */
131                 socklen_t len = sizeof(*caddr);
132                 if (getsockname(sock, caddr, &len) != 0)
133                         caddr->sin_family = 0;
134         }
135
136         switch(prot) {
137         case IPPROTO_UDP:
138                 /* The socket is connected (so we could getsockname successfully),
139                  * but some servers on multi-homed hosts reply from
140                  * the wrong address, so if we stay connected, we lose the reply.
141                  */
142                 dissolve.sa_family = AF_UNSPEC;
143                 connect(sock, &dissolve, sizeof(dissolve));
144
145                 clnt = clntudp_bufcreate(saddr, prog, vers,
146                                          RETRY_TIMEOUT, &sock,
147                                          RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
148                 break;
149         case IPPROTO_TCP:
150                 clnt = clnttcp_create(saddr, prog, vers, &sock,
151                                       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
152                 break;
153         }
154         if (!clnt) {
155                 close(sock);
156                 return 0;
157         }
158         memset(&clnt_res, 0, sizeof(clnt_res));
159         stat = clnt_call(clnt, NULLPROC,
160                          (xdrproc_t)xdr_void, (caddr_t)NULL,
161                          (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
162                          TIMEOUT);
163         if (stat) {
164                 clnt_geterr(clnt, &rpc_createerr.cf_error);
165                 rpc_createerr.cf_stat = stat;
166         }
167         clnt_destroy(clnt);
168         close(sock);
169
170         if (stat == RPC_SUCCESS)
171                 return 1;
172         else
173                 return 0;
174 }