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