]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/conn.c
Move NFS mount code from util-linux to nfs-utils - part 2
[nfs-utils.git] / support / nfs / conn.c
1 /*
2  * conn.c -- NFS client mount / umount connection code support functions
3  *
4  * 2006-06-06 Amit Gud <agud@redhat.com>
5  * - Moved code snippets to nfs-utils/support/nfs from util-linux/mount/nfsmount.c
6  *
7  */
8
9 #include <errno.h>
10 #include <rpc/rpc.h>
11 #include <rpc/pmap_prot.h>
12 #include <rpc/pmap_clnt.h>
13 #include <sys/socket.h>
14 #include <sys/stat.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17
18 #include "conn.h"
19
20 extern int verbose;
21
22 /* Map an NFS version into the corresponding Mountd version */
23 u_long nfsvers_to_mnt(const u_long vers)
24 {
25         static const u_long nfs_to_mnt[] = { 0, 0, 1, 3 };
26         if (vers <= 3)
27                 return nfs_to_mnt[vers];
28         return 0;
29 }
30
31 /* Map a Mountd version into the corresponding NFS version */
32 u_long mntvers_to_nfs(const u_long vers)
33 {
34         static const u_long mnt_to_nfs[] = { 0, 2, 2, 3 };
35         if (vers <= 3)
36                 return mnt_to_nfs[vers];
37         return 0;
38 }
39
40 /*
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. 
45  */
46 int get_socket(struct sockaddr_in *saddr, u_int p_prot, int resvp)
47 {
48         int so, cc, type;
49         struct sockaddr_in laddr;
50         socklen_t namelen = sizeof(laddr);
51
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;
56                 if (verbose) {
57                         fprintf(stderr, 
58                                 "mount: Unable to create %s socket: errno %d (%s)\n",
59                                 p_prot == IPPROTO_UDP ? "UDP" : "TCP", 
60                                 errno, strerror(errno));
61                 }
62                 return RPC_ANYSOCK;
63         }
64         laddr.sin_family = AF_INET;
65         laddr.sin_port = 0;
66         laddr.sin_addr.s_addr = htonl(INADDR_ANY);
67         if (resvp) {
68                 if (bindresvport(so, &laddr) < 0) {
69                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
70                         rpc_createerr.cf_error.re_errno = errno;
71                         if (verbose) {
72                                 fprintf(stderr, 
73                                         "mount: Unable to bindresvport %s socket: errno %d (%s)\n",
74                                         p_prot == IPPROTO_UDP ? "UDP" : "TCP", 
75                                         errno, strerror(errno));
76                         }
77                         close(so);
78                         return RPC_ANYSOCK;
79                 }
80         } else {
81                 cc = bind(so, (struct sockaddr *)&laddr, namelen);
82                 if (cc < 0) {
83                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
84                         rpc_createerr.cf_error.re_errno = errno;
85                         if (verbose) {
86                                 fprintf(stderr, 
87                                         "mount: Unable to bind to %s socket: errno %d (%s)\n",
88                                         p_prot == IPPROTO_UDP ? "UDP" : "TCP", 
89                                         errno, strerror(errno));
90                         }
91                         close(so);
92                         return RPC_ANYSOCK;
93                 }
94         }
95         if (type == SOCK_STREAM) {
96                 cc = connect(so, (struct sockaddr *)saddr, namelen);
97                 if (cc < 0) {
98                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
99                         rpc_createerr.cf_error.re_errno = errno;
100                         if (verbose) {
101                                 fprintf(stderr, 
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));
105                         }
106                         close(so);
107                         return RPC_ANYSOCK;
108                 }
109         }
110         return so;
111 }
112
113 /*
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
117  * RPC call.
118  */
119 int
120 clnt_ping(struct sockaddr_in *saddr, const u_long prog, const u_long vers,
121           const u_int prot)
122 {
123         CLIENT *clnt=NULL;
124         int sock, stat;
125         static char clnt_res;
126
127         rpc_createerr.cf_stat = stat = errno = 0;
128         sock = get_socket(saddr, prot, FALSE);
129         if (sock == RPC_ANYSOCK && errno == ETIMEDOUT) {
130                 /*
131                  * TCP timeout. Bubble up the error to see 
132                  * how it should be handled.
133                  */
134                 rpc_createerr.cf_stat = RPC_TIMEDOUT;
135                 goto out_bad;
136         }
137
138         switch(prot) {
139         case IPPROTO_UDP:
140                 clnt = clntudp_bufcreate(saddr, prog, vers,
141                                          RETRY_TIMEOUT, &sock,
142                                          RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
143                 break;
144         case IPPROTO_TCP:
145                 clnt = clnttcp_create(saddr, prog, vers, &sock,
146                                       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
147                 break;
148         default:
149                 goto out_bad;
150         }
151         if (!clnt)
152                 goto out_bad;
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,
157                          TIMEOUT);
158         if (stat) {
159                 clnt_geterr(clnt, &rpc_createerr.cf_error);
160                 rpc_createerr.cf_stat = stat;
161         }
162         clnt_destroy(clnt);
163         if (sock != -1)
164                 close(sock);
165
166         if (stat == RPC_SUCCESS)
167                 return 1;
168
169  out_bad:
170         return 0;
171 }
172
173 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
174 {
175         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
176         struct pmap *mnt_pmap = &mnt_server->pmap;
177         CLIENT *clnt = NULL;
178
179         /* contact the mount daemon via TCP */
180         mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
181         *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, TRUE);
182
183         switch (mnt_pmap->pm_prot) {
184         case IPPROTO_UDP:
185                 clnt = clntudp_bufcreate(mnt_saddr,
186                                          mnt_pmap->pm_prog, mnt_pmap->pm_vers,
187                                          RETRY_TIMEOUT, msock,
188                                          MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
189                 break;
190         case IPPROTO_TCP:
191                 clnt = clnttcp_create(mnt_saddr,
192                                       mnt_pmap->pm_prog, mnt_pmap->pm_vers,
193                                       msock,
194                                       MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
195                 break;
196         }
197         if (clnt) {
198                 /* try to mount hostname:dirname */
199                 clnt->cl_auth = authunix_create_default();
200                 return clnt;
201         }
202         return NULL;
203 }
204
205 void mnt_closeclnt(CLIENT *clnt, int msock)
206 {
207         auth_destroy(clnt->cl_auth);
208         clnt_destroy(clnt);
209         close(msock);
210 }
211