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
11 #include <rpc/pmap_prot.h>
12 #include <rpc/pmap_clnt.h>
13 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
22 /* Map an NFS version into the corresponding Mountd version */
23 u_long nfsvers_to_mnt(const u_long vers)
25 static const u_long nfs_to_mnt[] = { 0, 0, 1, 3 };
27 return nfs_to_mnt[vers];
31 /* Map a Mountd version into the corresponding NFS version */
32 u_long mntvers_to_nfs(const u_long vers)
34 static const u_long mnt_to_nfs[] = { 0, 2, 2, 3 };
36 return mnt_to_nfs[vers];
41 * Create a socket that is locally bound to a
42 * reserve or non-reserve port. For any failures,
43 * RPC_ANYSOCK is returned which will cause
44 * the RPC code to create the socket instead.
46 int get_socket(struct sockaddr_in *saddr, u_int p_prot, int resvp)
49 struct sockaddr_in laddr;
50 socklen_t namelen = sizeof(laddr);
52 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
53 if ((so = socket (AF_INET, type, p_prot)) < 0) {
54 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
55 rpc_createerr.cf_error.re_errno = errno;
58 "mount: Unable to create %s socket: errno %d (%s)\n",
59 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
60 errno, strerror(errno));
64 laddr.sin_family = AF_INET;
66 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
68 if (bindresvport(so, &laddr) < 0) {
69 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
70 rpc_createerr.cf_error.re_errno = errno;
73 "mount: Unable to bindresvport %s socket: errno %d (%s)\n",
74 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
75 errno, strerror(errno));
81 cc = bind(so, (struct sockaddr *)&laddr, namelen);
83 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
84 rpc_createerr.cf_error.re_errno = errno;
87 "mount: Unable to bind to %s socket: errno %d (%s)\n",
88 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
89 errno, strerror(errno));
95 if (type == SOCK_STREAM || type == SOCK_DGRAM) {
96 cc = connect(so, (struct sockaddr *)saddr, namelen);
98 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
99 rpc_createerr.cf_error.re_errno = errno;
102 "mount: Unable to connect to %s:%d, errno %d (%s)\n",
103 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port),
104 errno, strerror(errno));
114 * Sigh... getport() doesn't actually check the version number.
115 * In order to make sure that the server actually supports the service
116 * we're requesting, we open and RPC client, and fire off a NULL
120 clnt_ping(struct sockaddr_in *saddr, const u_long prog, const u_long vers,
121 const u_int prot, struct sockaddr_in *caddr)
125 static char clnt_res;
127 rpc_createerr.cf_stat = stat = errno = 0;
128 sock = get_socket(saddr, prot, FALSE);
129 if (sock == RPC_ANYSOCK && errno == ETIMEDOUT) {
131 * TCP timeout. Bubble up the error to see
132 * how it should be handled.
134 rpc_createerr.cf_stat = RPC_TIMEDOUT;
140 clnt = clntudp_bufcreate(saddr, prog, vers,
141 RETRY_TIMEOUT, &sock,
142 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
145 clnt = clnttcp_create(saddr, prog, vers, &sock,
146 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
153 memset(&clnt_res, 0, sizeof(clnt_res));
154 stat = clnt_call(clnt, NULLPROC,
155 (xdrproc_t)xdr_void, (caddr_t)NULL,
156 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
159 clnt_geterr(clnt, &rpc_createerr.cf_error);
160 rpc_createerr.cf_stat = stat;
165 /* Get the address of our end of this connection */
166 int len = sizeof(*caddr);
167 if (getsockname(sock, caddr, &len) != 0)
168 caddr->sin_family = 0;
173 if (stat == RPC_SUCCESS)
180 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
182 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
183 struct pmap *mnt_pmap = &mnt_server->pmap;
186 /* contact the mount daemon via TCP */
187 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
188 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, TRUE);
190 switch (mnt_pmap->pm_prot) {
192 clnt = clntudp_bufcreate(mnt_saddr,
193 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
194 RETRY_TIMEOUT, msock,
195 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
198 clnt = clnttcp_create(mnt_saddr,
199 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
201 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
205 /* try to mount hostname:dirname */
206 clnt->cl_auth = authunix_create_default();
212 void mnt_closeclnt(CLIENT *clnt, int msock)
214 auth_destroy(clnt->cl_auth);