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
28 /* Map an NFS version into the corresponding Mountd version */
29 u_long nfsvers_to_mnt(const u_long vers)
31 static const u_long nfs_to_mnt[] = { 0, 0, 1, 3 };
33 return nfs_to_mnt[vers];
37 /* Map a Mountd version into the corresponding NFS version */
38 u_long mntvers_to_nfs(const u_long vers)
40 static const u_long mnt_to_nfs[] = { 0, 2, 2, 3 };
42 return mnt_to_nfs[vers];
47 * Create a socket that is locally bound to a
48 * reserve or non-reserve port. For any failures,
49 * RPC_ANYSOCK is returned which will cause
50 * the RPC code to create the socket instead.
52 int get_socket(struct sockaddr_in *saddr, u_int p_prot, int resvp, int conn)
55 struct sockaddr_in laddr;
56 socklen_t namelen = sizeof(laddr);
58 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
59 if ((so = socket (AF_INET, type, p_prot)) < 0) {
60 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
61 rpc_createerr.cf_error.re_errno = errno;
64 "mount: Unable to create %s socket: errno %d (%s)\n",
65 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
66 errno, strerror(errno));
70 laddr.sin_family = AF_INET;
72 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
74 if (bindresvport(so, &laddr) < 0) {
75 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
76 rpc_createerr.cf_error.re_errno = errno;
79 "mount: Unable to bindresvport %s socket: errno %d (%s)\n",
80 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
81 errno, strerror(errno));
87 cc = bind(so, (struct sockaddr *)&laddr, namelen);
89 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
90 rpc_createerr.cf_error.re_errno = errno;
93 "mount: Unable to bind to %s socket: errno %d (%s)\n",
94 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
95 errno, strerror(errno));
101 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
102 cc = connect(so, (struct sockaddr *)saddr, namelen);
104 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
105 rpc_createerr.cf_error.re_errno = errno;
108 "mount: Unable to connect to %s:%d, errno %d (%s)\n",
109 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port),
110 errno, strerror(errno));
120 * Sigh... getport() doesn't actually check the version number.
121 * In order to make sure that the server actually supports the service
122 * we're requesting, we open and RPC client, and fire off a NULL
126 clnt_ping(struct sockaddr_in *saddr, const u_long prog, const u_long vers,
127 const u_int prot, struct sockaddr_in *caddr)
131 static char clnt_res;
132 struct sockaddr dissolve;
134 rpc_createerr.cf_stat = stat = errno = 0;
135 sock = get_socket(saddr, prot, FALSE, TRUE);
136 if (sock == RPC_ANYSOCK) {
137 if (errno == ETIMEDOUT) {
139 * TCP timeout. Bubble up the error to see
140 * how it should be handled.
142 rpc_createerr.cf_stat = RPC_TIMEDOUT;
148 /* Get the address of our end of this connection */
149 socklen_t len = sizeof(*caddr);
150 if (getsockname(sock, caddr, &len) != 0)
151 caddr->sin_family = 0;
156 /* The socket is connected (so we could getsockname successfully),
157 * but some servers on multi-homed hosts reply from
158 * the wrong address, so if we stay connected, we lose the reply.
160 dissolve.sa_family = AF_UNSPEC;
161 connect(sock, &dissolve, sizeof(dissolve));
163 clnt = clntudp_bufcreate(saddr, prog, vers,
164 RETRY_TIMEOUT, &sock,
165 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
168 clnt = clnttcp_create(saddr, prog, vers, &sock,
169 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
176 memset(&clnt_res, 0, sizeof(clnt_res));
177 stat = clnt_call(clnt, NULLPROC,
178 (xdrproc_t)xdr_void, (caddr_t)NULL,
179 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
182 clnt_geterr(clnt, &rpc_createerr.cf_error);
183 rpc_createerr.cf_stat = stat;
188 if (stat == RPC_SUCCESS)
194 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
196 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
197 struct pmap *mnt_pmap = &mnt_server->pmap;
200 /* contact the mount daemon via TCP */
201 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
202 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, TRUE, FALSE);
204 switch (mnt_pmap->pm_prot) {
206 clnt = clntudp_bufcreate(mnt_saddr,
207 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
208 RETRY_TIMEOUT, msock,
209 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
212 clnt = clnttcp_create(mnt_saddr,
213 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
215 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
219 /* try to mount hostname:dirname */
220 clnt->cl_auth = authunix_create_default();
226 void mnt_closeclnt(CLIENT *clnt, int msock)
228 auth_destroy(clnt->cl_auth);