]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - net.c
08_refactor-makesock.diff
[paraslash.git] / net.c
diff --git a/net.c b/net.c
index d85321d931d51f2d21a0fa8e8ea160df432b1ece..d9b3a9defa8164e98be1dda341df3a62b1e4bf9b 100644 (file)
--- a/net.c
+++ b/net.c
@@ -377,13 +377,14 @@ static void flowopt_cleanup(struct flowopts *fo)
  *
  *  \sa ipv6(7), getaddrinfo(3), bind(2), connect(2).
  */
-int makesock(unsigned l3type, unsigned l4type, int passive,
-            const char *host, unsigned short port_number,
+int makesock(unsigned l4type, bool passive,
+            const char *host, uint16_t port_number,
             struct flowopts *fo)
 {
        struct addrinfo *local = NULL, *src,
                        *remote = NULL, *dst, hints;
-       int             rc, on = 1, sockfd = -1,
+       unsigned int    l3type = AF_UNSPEC;
+       int             rc, sockfd = -1,
                        socktype = sock_type(l4type);
        char port[6]; /* port number has at most 5 digits */
 
@@ -428,17 +429,6 @@ int makesock(unsigned l3type, unsigned l4type, int passive,
                if (sockfd < 0)
                        goto get_next_dst;
 
-               /*
-                * Set those options that need to be set before establishing
-                * the connection. Reuse the address on passive (listening)
-                * sockets to avoid failure on restart.
-                */
-               if (passive && setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
-                               &on, sizeof(on)) == -1) {
-                       PARA_ERROR_LOG("can not set SO_REUSEADDR: %s\n",
-                               strerror(errno));
-                       return -ERRNO_TO_PARA_ERROR(errno);
-               }
                flowopt_setopts(sockfd, fo);
 
                if (src) {
@@ -478,19 +468,26 @@ get_next_src:
 /**
  * Create a passive / listening socket.
  *
- * \param l3type The network-layer type (\p AF_xxx).
  * \param l4type The transport-layer type (\p IPPROTO_xxx).
  * \param port The decimal port number to listen on.
+ * \param fo Flowopts (if any) to set before starting to listen.
  *
  * \return Positive integer (socket descriptor) on success, negative value
  * otherwise.
  *
  * \sa makesock(), ip(7), ipv6(7), bind(2), listen(2).
  */
-int para_listen(unsigned l3type, unsigned l4type, unsigned short port)
+int para_listen(unsigned l4type, uint16_t port, struct flowopts *fo)
 {
-       int ret, fd = makesock(l3type, l4type, 1, NULL, port, NULL);
+       int fd, ret;
+
+       if (fo == NULL)
+               fo = flowopt_new();
+
+       /* Reuse the address to avoid failure on restart. */
+       OPT_ENABLE(fo, SOL_SOCKET, SO_REUSEADDR);
 
+       fd = makesock(l4type, 1, NULL, port, fo);
        if (fd > 0) {
                ret = listen(fd, BACKLOG);
                if (ret < 0) {
@@ -782,7 +779,7 @@ int para_accept(int fd, void *addr, socklen_t size)
  */
 const uint8_t *dccp_available_ccids(uint8_t *ccids, uint8_t *nccids)
 {
-       int fd = makesock(AF_UNSPEC, IPPROTO_DCCP, 1, NULL, 0, NULL);
+       int fd = makesock(IPPROTO_DCCP, 1, NULL, 0, NULL);
 
        if (fd < 0)
                return NULL;