]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
net: revert restricting SO_REUSEADDR
authorGerrit Renker <grenker@cscs.ch>
Wed, 3 Mar 2010 16:57:14 +0000 (17:57 +0100)
committerAndre Noll <maan@systemlinux.org>
Wed, 3 Mar 2010 16:57:14 +0000 (17:57 +0100)
This reverts part of commit a5927501e41fa3fca2975452617474e78ffecc48
(08_refactor-makesock.diff, 25 Feb 2010), which restricted by default
SO_REUSEADDR to connection-oriented protocols only - it used to be
set on all passive sockets.

However, the same problem exists also for UDP, whenever connecting
more than one listener on the same host, hence revert it.

net.c

diff --git a/net.c b/net.c
index aeeca20e41447d2f3912b3035e739219ff08c62d..b44d1d3e5d3a2012b1dfe135ddeec74d10b0fe58 100644 (file)
--- a/net.c
+++ b/net.c
@@ -384,7 +384,7 @@ int makesock(unsigned l4type, bool passive,
        struct addrinfo *local = NULL, *src,
                        *remote = NULL, *dst, hints;
        unsigned int    l3type = AF_UNSPEC;
        struct addrinfo *local = NULL, *src,
                        *remote = NULL, *dst, hints;
        unsigned int    l3type = AF_UNSPEC;
-       int             rc, sockfd = -1,
+       int             rc, on = 1, sockfd = -1,
                        socktype = sock_type(l4type);
        char port[6]; /* port number has at most 5 digits */
 
                        socktype = sock_type(l4type);
        char port[6]; /* port number has at most 5 digits */
 
@@ -429,6 +429,18 @@ int makesock(unsigned l4type, bool passive,
                if (sockfd < 0)
                        goto get_next_dst;
 
                if (sockfd < 0)
                        goto get_next_dst;
 
+               /*
+                * Reuse the address on passive sockets to avoid failure on
+                * restart (protocols using listen()) and when creating
+                * multiple listener instances (UDP multicast).
+                */
+               if (passive && setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
+                                                 &on, sizeof(on)) == -1) {
+                       rc = errno;
+                       PARA_ERROR_LOG("can not set SO_REUSEADDR: %s\n",
+                                      strerror(rc));
+                       return -ERRNO_TO_PARA_ERROR(rc);
+               }
                flowopt_setopts(sockfd, fo);
 
                if (src) {
                flowopt_setopts(sockfd, fo);
 
                if (src) {
@@ -480,15 +492,8 @@ get_next_src:
  */
 int para_listen(unsigned l4type, uint16_t port, struct flowopts *fo)
 {
  */
 int para_listen(unsigned l4type, uint16_t port, struct flowopts *fo)
 {
-       int fd, ret;
-
-       if (fo == NULL)
-               fo = flowopt_new();
-
-       /* Reuse the address to avoid failure on restart. */
-       OPT_ENABLE(fo, SOL_SOCKET, SO_REUSEADDR);
+       int ret, fd = makesock(l4type, 1, NULL, port, fo);
 
 
-       fd = makesock(l4type, 1, NULL, port, fo);
        if (fd > 0) {
                ret = listen(fd, BACKLOG);
                if (ret < 0) {
        if (fd > 0) {
                ret = listen(fd, BACKLOG);
                if (ret < 0) {