]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/conn.c
libnfs.a: move clnt_ping() to utils/mount
[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 "config.h"
10 #include <errno.h>
11 #include <unistd.h>
12 #include <rpc/rpc.h>
13 #include <rpc/pmap_prot.h>
14 #include <rpc/pmap_clnt.h>
15 #include <sys/socket.h>
16 #include <sys/stat.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19
20 #include "conn.h"
21
22 #if SIZEOF_SOCKLEN_T - 0 == 0
23 #define socklen_t int
24 #endif
25
26 extern int verbose;
27
28 /*
29  * Create a socket that is locally bound to a 
30  * reserve or non-reserve port. For any failures,
31  * RPC_ANYSOCK is returned which will cause 
32  * the RPC code to create the socket instead. 
33  */
34 int get_socket(struct sockaddr_in *saddr, u_int p_prot, int resvp, int conn)
35 {
36         int so, cc, type;
37         struct sockaddr_in laddr;
38         socklen_t namelen = sizeof(laddr);
39
40         type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
41         if ((so = socket (AF_INET, type, p_prot)) < 0) {
42                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
43                 rpc_createerr.cf_error.re_errno = errno;
44                 if (verbose) {
45                         fprintf(stderr, 
46                                 "mount: Unable to create %s socket: errno %d (%s)\n",
47                                 p_prot == IPPROTO_UDP ? "UDP" : "TCP", 
48                                 errno, strerror(errno));
49                 }
50                 return RPC_ANYSOCK;
51         }
52         laddr.sin_family = AF_INET;
53         laddr.sin_port = 0;
54         laddr.sin_addr.s_addr = htonl(INADDR_ANY);
55         if (resvp) {
56                 if (bindresvport(so, &laddr) < 0) {
57                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
58                         rpc_createerr.cf_error.re_errno = errno;
59                         if (verbose) {
60                                 fprintf(stderr, 
61                                         "mount: Unable to bindresvport %s socket: errno %d (%s)\n",
62                                         p_prot == IPPROTO_UDP ? "UDP" : "TCP", 
63                                         errno, strerror(errno));
64                         }
65                         close(so);
66                         return RPC_ANYSOCK;
67                 }
68         } else {
69                 cc = bind(so, (struct sockaddr *)&laddr, namelen);
70                 if (cc < 0) {
71                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
72                         rpc_createerr.cf_error.re_errno = errno;
73                         if (verbose) {
74                                 fprintf(stderr, 
75                                         "mount: Unable to bind to %s socket: errno %d (%s)\n",
76                                         p_prot == IPPROTO_UDP ? "UDP" : "TCP", 
77                                         errno, strerror(errno));
78                         }
79                         close(so);
80                         return RPC_ANYSOCK;
81                 }
82         }
83         if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
84                 cc = connect(so, (struct sockaddr *)saddr, namelen);
85                 if (cc < 0) {
86                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
87                         rpc_createerr.cf_error.re_errno = errno;
88                         if (verbose) {
89                                 fprintf(stderr, 
90                                         "mount: Unable to connect to %s:%d, errno %d (%s)\n",
91                                         inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_port),
92                                         errno, strerror(errno));
93                         }
94                         close(so);
95                         return RPC_ANYSOCK;
96                 }
97         }
98         return so;
99 }