2 * conn.c -- NFS client mount / umount connection code support functions
4 * 2006-06-06 Amit Gud <agud@redhat.com>
5 * - Moved code snippets to nfs-utils/support/nfs from util-linux/mount/nfsmount.c
13 #include <rpc/pmap_prot.h>
14 #include <rpc/pmap_clnt.h>
15 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
22 #if SIZEOF_SOCKLEN_T - 0 == 0
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.
34 int get_socket(struct sockaddr_in *saddr, u_int p_prot, int resvp, int conn)
37 struct sockaddr_in laddr;
38 socklen_t namelen = sizeof(laddr);
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;
46 "mount: Unable to create %s socket: errno %d (%s)\n",
47 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
48 errno, strerror(errno));
52 laddr.sin_family = AF_INET;
54 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
56 if (bindresvport(so, &laddr) < 0) {
57 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
58 rpc_createerr.cf_error.re_errno = errno;
61 "mount: Unable to bindresvport %s socket: errno %d (%s)\n",
62 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
63 errno, strerror(errno));
69 cc = bind(so, (struct sockaddr *)&laddr, namelen);
71 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
72 rpc_createerr.cf_error.re_errno = errno;
75 "mount: Unable to bind to %s socket: errno %d (%s)\n",
76 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
77 errno, strerror(errno));
83 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
84 cc = connect(so, (struct sockaddr *)saddr, namelen);
86 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
87 rpc_createerr.cf_error.re_errno = errno;
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));
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
108 clnt_ping(struct sockaddr_in *saddr, const u_long prog, const u_long vers,
109 const u_int prot, struct sockaddr_in *caddr)
113 static char clnt_res;
114 struct sockaddr dissolve;
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) {
121 * TCP timeout. Bubble up the error to see
122 * how it should be handled.
124 rpc_createerr.cf_stat = RPC_TIMEDOUT;
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;
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.
142 dissolve.sa_family = AF_UNSPEC;
143 connect(sock, &dissolve, sizeof(dissolve));
145 clnt = clntudp_bufcreate(saddr, prog, vers,
146 RETRY_TIMEOUT, &sock,
147 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
150 clnt = clnttcp_create(saddr, prog, vers, &sock,
151 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
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,
164 clnt_geterr(clnt, &rpc_createerr.cf_error);
165 rpc_createerr.cf_stat = stat;
170 if (stat == RPC_SUCCESS)