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)
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 || 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;
133 rpc_createerr.cf_stat = stat = errno = 0;
134 sock = get_socket(saddr, prot, FALSE);
135 if (sock == RPC_ANYSOCK && errno == ETIMEDOUT) {
137 * TCP timeout. Bubble up the error to see
138 * how it should be handled.
140 rpc_createerr.cf_stat = RPC_TIMEDOUT;
146 clnt = clntudp_bufcreate(saddr, prog, vers,
147 RETRY_TIMEOUT, &sock,
148 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
151 clnt = clnttcp_create(saddr, prog, vers, &sock,
152 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
159 memset(&clnt_res, 0, sizeof(clnt_res));
160 stat = clnt_call(clnt, NULLPROC,
161 (xdrproc_t)xdr_void, (caddr_t)NULL,
162 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
165 clnt_geterr(clnt, &rpc_createerr.cf_error);
166 rpc_createerr.cf_stat = stat;
171 /* Get the address of our end of this connection */
172 socklen_t len = sizeof(*caddr);
173 if (getsockname(sock, caddr, &len) != 0)
174 caddr->sin_family = 0;
179 if (stat == RPC_SUCCESS)
186 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
188 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
189 struct pmap *mnt_pmap = &mnt_server->pmap;
192 /* contact the mount daemon via TCP */
193 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
194 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, TRUE);
196 switch (mnt_pmap->pm_prot) {
198 clnt = clntudp_bufcreate(mnt_saddr,
199 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
200 RETRY_TIMEOUT, msock,
201 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
204 clnt = clnttcp_create(mnt_saddr,
205 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
207 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
211 /* try to mount hostname:dirname */
212 clnt->cl_auth = authunix_create_default();
218 void mnt_closeclnt(CLIENT *clnt, int msock)
220 auth_destroy(clnt->cl_auth);