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
12 #include <rpc/pmap_prot.h>
13 #include <rpc/pmap_clnt.h>
14 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
23 /* Map an NFS version into the corresponding Mountd version */
24 u_long nfsvers_to_mnt(const u_long vers)
26 static const u_long nfs_to_mnt[] = { 0, 0, 1, 3 };
28 return nfs_to_mnt[vers];
32 /* Map a Mountd version into the corresponding NFS version */
33 u_long mntvers_to_nfs(const u_long vers)
35 static const u_long mnt_to_nfs[] = { 0, 2, 2, 3 };
37 return mnt_to_nfs[vers];
42 * Create a socket that is locally bound to a
43 * reserve or non-reserve port. For any failures,
44 * RPC_ANYSOCK is returned which will cause
45 * the RPC code to create the socket instead.
47 int get_socket(struct sockaddr_in *saddr, u_int p_prot, int resvp)
50 struct sockaddr_in laddr;
51 socklen_t namelen = sizeof(laddr);
53 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
54 if ((so = socket (AF_INET, type, p_prot)) < 0) {
55 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
56 rpc_createerr.cf_error.re_errno = errno;
59 "mount: Unable to create %s socket: errno %d (%s)\n",
60 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
61 errno, strerror(errno));
65 laddr.sin_family = AF_INET;
67 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
69 if (bindresvport(so, &laddr) < 0) {
70 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
71 rpc_createerr.cf_error.re_errno = errno;
74 "mount: Unable to bindresvport %s socket: errno %d (%s)\n",
75 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
76 errno, strerror(errno));
82 cc = bind(so, (struct sockaddr *)&laddr, namelen);
84 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
85 rpc_createerr.cf_error.re_errno = errno;
88 "mount: Unable to bind to %s socket: errno %d (%s)\n",
89 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
90 errno, strerror(errno));
96 if (type == SOCK_STREAM || type == SOCK_DGRAM) {
97 cc = connect(so, (struct sockaddr *)saddr, namelen);
99 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
100 rpc_createerr.cf_error.re_errno = errno;
103 "mount: Unable to connect to %s:%d, errno %d (%s)\n",
104 inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port),
105 errno, strerror(errno));
115 * Sigh... getport() doesn't actually check the version number.
116 * In order to make sure that the server actually supports the service
117 * we're requesting, we open and RPC client, and fire off a NULL
121 clnt_ping(struct sockaddr_in *saddr, const u_long prog, const u_long vers,
122 const u_int prot, struct sockaddr_in *caddr)
126 static char clnt_res;
128 rpc_createerr.cf_stat = stat = errno = 0;
129 sock = get_socket(saddr, prot, FALSE);
130 if (sock == RPC_ANYSOCK && errno == ETIMEDOUT) {
132 * TCP timeout. Bubble up the error to see
133 * how it should be handled.
135 rpc_createerr.cf_stat = RPC_TIMEDOUT;
141 clnt = clntudp_bufcreate(saddr, prog, vers,
142 RETRY_TIMEOUT, &sock,
143 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
146 clnt = clnttcp_create(saddr, prog, vers, &sock,
147 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
154 memset(&clnt_res, 0, sizeof(clnt_res));
155 stat = clnt_call(clnt, NULLPROC,
156 (xdrproc_t)xdr_void, (caddr_t)NULL,
157 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
160 clnt_geterr(clnt, &rpc_createerr.cf_error);
161 rpc_createerr.cf_stat = stat;
166 /* Get the address of our end of this connection */
167 int len = sizeof(*caddr);
168 if (getsockname(sock, caddr, &len) != 0)
169 caddr->sin_family = 0;
174 if (stat == RPC_SUCCESS)
181 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
183 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
184 struct pmap *mnt_pmap = &mnt_server->pmap;
187 /* contact the mount daemon via TCP */
188 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
189 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, TRUE);
191 switch (mnt_pmap->pm_prot) {
193 clnt = clntudp_bufcreate(mnt_saddr,
194 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
195 RETRY_TIMEOUT, msock,
196 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
199 clnt = clnttcp_create(mnt_saddr,
200 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
202 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
206 /* try to mount hostname:dirname */
207 clnt->cl_auth = authunix_create_default();
213 void mnt_closeclnt(CLIENT *clnt, int msock)
215 auth_destroy(clnt->cl_auth);