IPV6: Add basic IPv6 support
[deb_libnfs.git] / lib / socket.c
index c5068bae24ea754e4bbbf787a2fa44ca17d721e6..d025e070e017091e1f796178c139e8ff44c7cf48 100644 (file)
 #include <sys/socket.h>
 #endif
 
+#ifdef HAVE_NETINET_TCP_H
+#include <netinet/tcp.h>
+#endif
+
 #ifdef HAVE_NETDB_H
 #include <netdb.h>
 #endif
@@ -76,7 +80,6 @@
 #include "win32_errnowrapper.h"
 #endif
 
-
 static int rpc_reconnect_requeue(struct rpc_context *rpc);
 static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_storage *s);
 
@@ -92,6 +95,26 @@ static void set_nonblocking(int fd)
 #endif //FIXME
 }
 
+#ifdef HAVE_NETINET_TCP_H
+int set_tcp_sockopt(int sockfd, int optname, int value)
+{
+       int level;
+
+       #if defined(__FreeBSD__) || defined(__sun) || (defined(__APPLE__) && defined(__MACH__))
+       struct protoent *buf;
+
+       if ((buf = getprotobyname("tcp")) != NULL)
+               level = buf->p_proto;
+       else
+               return -1;
+       #else
+               level = SOL_TCP;
+       #endif
+
+       return setsockopt(sockfd, level, optname, (char *)&value, sizeof(value));
+}
+#endif
+
 int rpc_get_fd(struct rpc_context *rpc)
 {
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
@@ -186,6 +209,7 @@ static int rpc_read_from_socket(struct rpc_context *rpc)
                if (count < 0) {
                        rpc_set_error(rpc, "Failed recvfrom: %s", strerror(errno));
                        free(buf);
+                       return -1;
                }
                if (rpc_process_pdu(rpc, buf, count) != 0) {
                        rpc_set_error(rpc, "Invalid/garbage pdu received from server. Ignoring PDU");
@@ -227,7 +251,7 @@ static int rpc_read_from_socket(struct rpc_context *rpc)
        pdu_size = rpc_get_pdu_size(rpc->inbuf);
        if (rpc->insize < pdu_size) {
                unsigned char *buf;
-               
+
                buf = malloc(pdu_size);
                if (buf == NULL) {
                        rpc_set_error(rpc, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size, errno);
@@ -266,7 +290,7 @@ static int rpc_read_from_socket(struct rpc_context *rpc)
                        rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket");
                        return -1;
                }
-               free(rpc->inbuf);
+               free(buf);
        }
 
        return 0;
@@ -368,6 +392,17 @@ void rpc_unset_autoreconnect(struct rpc_context *rpc)
        rpc->auto_reconnect = 0;
 }
 
+void rpc_set_tcp_syncnt(struct rpc_context *rpc, int v)
+{
+       assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
+       rpc->tcp_syncnt = v;
+}
+
+#ifndef TCP_SYNCNT
+#define TCP_SYNCNT        7
+#endif
+
 static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_storage *s)
 {
        int socksize;
@@ -378,6 +413,20 @@ static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_s
        case AF_INET:
                socksize = sizeof(struct sockaddr_in);
                rpc->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+#ifdef HAVE_NETINET_TCP_H
+               if (rpc->tcp_syncnt != RPC_PARAM_UNDEFINED) {
+                       set_tcp_sockopt(rpc->fd, TCP_SYNCNT, rpc->tcp_syncnt);
+               }
+#endif
+               break;
+       case AF_INET6:
+               socksize = sizeof(struct sockaddr_in6);
+               rpc->fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
+#ifdef HAVE_NETINET_TCP_H
+               if (rpc->tcp_syncnt != RPC_PARAM_UNDEFINED) {
+                       set_tcp_sockopt(rpc->fd, TCP_SYNCNT, rpc->tcp_syncnt);
+               }
+#endif
                break;
        default:
                rpc_set_error(rpc, "Can not handle AF_FAMILY:%d", s->ss_family);
@@ -408,7 +457,7 @@ static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_s
         * binding will usually succeed.
         */
        {
-               struct sockaddr_in sin;
+               struct sockaddr_storage ss;
                static int portOfs = 0;
                const int firstPort = 512;      /* >= 512 according to Sun docs */
                const int portCount = IPPORT_RESERVED - firstPort;
@@ -425,12 +474,26 @@ static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_s
 
                        /* skip well-known ports */
                        if (!getservbyport(port, "tcp")) {
-                               memset(&sin, 0, sizeof(sin));
-                               sin.sin_port        = port;
-                               sin.sin_family      = AF_INET;
-                               sin.sin_addr.s_addr = 0;
+                               memset(&ss, 0, sizeof(ss));
 
-                               rc = bind(rpc->fd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in));
+                               switch (s->ss_family) {
+                               case AF_INET:
+                                       ((struct sockaddr_in *)&ss)->sin_port = port;
+                                       ((struct sockaddr_in *)&ss)->sin_family      = AF_INET;
+#ifdef HAVE_SOCKADDR_LEN
+                                       ((struct sockaddr_in *)&ss)->sin_len = sizeof(struct sockaddr_in);
+#endif
+                                       break;
+                               case AF_INET6:
+                                       ((struct sockaddr_in6 *)&ss)->sin6_port = port;
+                                       ((struct sockaddr_in6 *)&ss)->sin6_family      = AF_INET6;
+#ifdef HAVE_SOCKADDR_LEN
+                                       ((struct sockaddr_in6 *)&ss)->sin6_len = sizeof(struct sockaddr_in);
+#endif
+                                       break;
+                               }
+
+                               rc = bind(rpc->fd, (struct sockaddr *)&ss, socksize);
 #if !defined(WIN32)
                                /* we got EACCES, so don't try again */
                                if (rc != 0 && errno == EACCES)
@@ -445,14 +508,14 @@ static int rpc_connect_sockaddr_async(struct rpc_context *rpc, struct sockaddr_s
        if (connect(rpc->fd, (struct sockaddr *)s, socksize) != 0 && errno != EINPROGRESS) {
                rpc_set_error(rpc, "connect() to server failed. %s(%d)", strerror(errno), errno);
                return -1;
-       }               
+       }
 
        return 0;
-}          
+}
 
 int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data)
 {
-       struct sockaddr_in *sin = (struct sockaddr_in *)&rpc->s;
+       struct addrinfo *ai = NULL;
 
        assert(rpc->magic == RPC_CONTEXT_MAGIC);
 
@@ -468,18 +531,25 @@ int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc
 
        rpc->auto_reconnect = 0;
 
-       sin->sin_family = AF_INET;
-       sin->sin_port   = htons(port);
-       if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) {
-               rpc_set_error(rpc, "Not a valid server ip address");
+       if (getaddrinfo(server, NULL, NULL, &ai) != 0) {
+               rpc_set_error(rpc, "Invalid address:%s. "
+                             "Can not resolv into IPv4/v6 structure.", server);
                return -1;
-       }
-
+       }
 
-       switch (rpc->s.ss_family) {
+       switch (ai->ai_family) {
        case AF_INET:
+               ((struct sockaddr_in *)&rpc->s)->sin_family = ai->ai_family;
+               ((struct sockaddr_in *)&rpc->s)->sin_port   = htons(port);
 #ifdef HAVE_SOCKADDR_LEN
-               sin->sin_len = sizeof(struct sockaddr_in);
+               ((struct sockaddr_in *)&rpc->s)->sin_len = sizeof(struct sockaddr_in);
+#endif
+               break;
+       case AF_INET6:
+               ((struct sockaddr_in6 *)&rpc->s)->sin6_family = ai->ai_family;
+               ((struct sockaddr_in6 *)&rpc->s)->sin6_port   = htons(port);
+#ifdef HAVE_SOCKADDR_LEN
+               ((struct sockaddr_in6 *)&rpc->s)->sin6_len = sizeof(struct sockaddr_in6);
 #endif
                break;
        }
@@ -487,12 +557,14 @@ int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc
        rpc->connect_cb  = cb;
        rpc->connect_data = private_data;
 
+       freeaddrinfo(ai);
+
        if (rpc_connect_sockaddr_async(rpc, &rpc->s) != 0) {
                return -1;
        }
 
        return 0;
-}          
+}
 
 int rpc_disconnect(struct rpc_context *rpc, char *error)
 {
@@ -577,7 +649,7 @@ int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port)
        sprintf(service, "%d", port);
        if (getaddrinfo(addr, service, NULL, &ai) != 0) {
                rpc_set_error(rpc, "Invalid address:%s. "
-                       "Can not resolv into IPv4/v6 structure.");
+                             "Can not resolv into IPv4/v6 structure.", addr);
                return -1;
        }
 
@@ -585,13 +657,13 @@ int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port)
        case AF_INET:
                rpc->fd = socket(ai->ai_family, SOCK_DGRAM, 0);
                if (rpc->fd == -1) {
-                       rpc_set_error(rpc, "Failed to create UDP socket: %s", strerror(errno)); 
+                       rpc_set_error(rpc, "Failed to create UDP socket: %s", strerror(errno));
                        freeaddrinfo(ai);
                        return -1;
                }
 
                if (bind(rpc->fd, (struct sockaddr *)ai->ai_addr, sizeof(struct sockaddr_in)) != 0) {
-                       rpc_set_error(rpc, "Failed to bind to UDP socket: %s",strerror(errno)); 
+                       rpc_set_error(rpc, "Failed to bind to UDP socket: %s",strerror(errno));
                        freeaddrinfo(ai);
                        return -1;
                }
@@ -622,7 +694,7 @@ int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int i
        sprintf(service, "%d", port);
        if (getaddrinfo(addr, service, NULL, &ai) != 0) {
                rpc_set_error(rpc, "Invalid address:%s. "
-                       "Can not resolv into IPv4/v6 structure.");
+                             "Can not resolv into IPv4/v6 structure.", addr);
                return -1;
        }
 
@@ -667,3 +739,10 @@ int rpc_queue_length(struct rpc_context *rpc)
        }
        return i;
 }
+
+void rpc_set_fd(struct rpc_context *rpc, int fd)
+{
+       assert(rpc->magic == RPC_CONTEXT_MAGIC);
+
+       rpc->fd = fd;
+}