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
13 #include <rpc/pmap_prot.h>
14 #include <rpc/pmap_clnt.h>
15 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
22 #if SIZEOF_SOCKLEN_T - 0 == 0
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.
34 int get_socket(struct sockaddr_in *saddr, u_int p_prot, int resvp, int conn)
37 struct sockaddr_in laddr;
38 socklen_t namelen = sizeof(laddr);
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;
46 "mount: Unable to create %s socket: errno %d (%s)\n",
47 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
48 errno, strerror(errno));
52 laddr.sin_family = AF_INET;
54 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
56 if (bindresvport(so, &laddr) < 0) {
57 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
58 rpc_createerr.cf_error.re_errno = errno;
61 "mount: Unable to bindresvport %s socket: errno %d (%s)\n",
62 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
63 errno, strerror(errno));
69 cc = bind(so, (struct sockaddr *)&laddr, namelen);
71 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
72 rpc_createerr.cf_error.re_errno = errno;
75 "mount: Unable to bind to %s socket: errno %d (%s)\n",
76 p_prot == IPPROTO_UDP ? "UDP" : "TCP",
77 errno, strerror(errno));
83 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
84 cc = connect(so, (struct sockaddr *)saddr, namelen);
86 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
87 rpc_createerr.cf_error.re_errno = errno;
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));