08_refactor-makesock.diff
authorGerrit Renker <grenker@cscs.ch>
Thu, 25 Feb 2010 16:05:45 +0000 (17:05 +0100)
committerAndre Noll <maan@systemlinux.org>
Thu, 25 Feb 2010 16:05:45 +0000 (17:05 +0100)
This is a refactoring of makesock():
 * the ubiquitous AF_UNSPEC is promoted as the default;
 * a wrapper for active sockets, para_connect_simple();
 * para_listen() now supports flowopts;
 * a wrapper for passive sockets, para_listen_simple();
 * for consistency, port numbers (which are 2 byte in
   UDP, TCP, and DCCP) have all been set to 'uint16_t'.

client_common.c
dccp_recv.c
http_recv.c
net.c
net.h
send_common.c
server.c
udp_recv.c
udp_send.c

index f3c96aada17a5bfe219038edce2a8feebe290a3b..197f031c476a79ee00bc2143945356a20330595c 100644 (file)
@@ -311,8 +311,8 @@ static int client_connect(struct client_task *ct)
        int ret;
 
        ct->rc4c.fd = -1;
-       ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, ct->conf.hostname_arg,
-                      ct->conf.server_port_arg, NULL);
+       ret = para_connect_simple(IPPROTO_TCP, ct->conf.hostname_arg,
+                                              ct->conf.server_port_arg);
        if (ret < 0)
                return ret;
        ct->rc4c.fd = ret;
index 647d31a671c4e87fb0eb999385cf21379374b10e..0a7b3c069271329152f131afa1f1334416473d10 100644 (file)
@@ -57,9 +57,8 @@ static int dccp_recv_open(struct receiver_node *rn)
 {
        struct private_dccp_recv_data *pdd;
        struct dccp_recv_args_info *conf = rn->conf;
-       int fd, ret = makesock(AF_UNSPEC, IPPROTO_DCCP, 0, conf->host_arg,
-                              conf->port_arg, NULL);
-
+       int fd, ret = para_connect_simple(IPPROTO_DCCP, conf->host_arg,
+                                                       conf->port_arg);
        if (ret < 0)
                return ret;
        fd = ret;
index 107bfdcdf576bb452bb8d32d38b3ebe71488b9e1..3078c5fdf13a67599c65bc3b0c3fac4e8da4053f 100644 (file)
@@ -165,8 +165,8 @@ static int http_recv_open(struct receiver_node *rn)
 {
        struct private_http_recv_data *phd;
        struct http_recv_args_info *conf = rn->conf;
-       int fd, ret = makesock(AF_UNSPEC, IPPROTO_TCP, 0, conf->host_arg,
-                              conf->port_arg, NULL);
+       int fd, ret = para_connect_simple(IPPROTO_TCP, conf->host_arg,
+                                                      conf->port_arg);
 
        if (ret < 0)
                return ret;
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;
diff --git a/net.h b/net.h
index 96fa07eeb43c4745687fd4efde9b7f16d4dabe5d..831cd3b8b72f2b3a7f90592c64f6a5f725c4b7d7 100644 (file)
--- a/net.h
+++ b/net.h
@@ -87,9 +87,16 @@ _static_inline_ bool is_valid_ipv6_address(const char *address)
 /**
  * Generic socket creation (passive and active sockets).
  */
-extern int makesock(unsigned l3type, unsigned l4type, int passive,
-                   const char *host, unsigned short port_number,
+extern int makesock(unsigned l4type, bool passive,
+                   const char *host, uint16_t port_number,
                    struct flowopts *fo);
+
+static inline int para_connect_simple(unsigned l4type,
+                                     const char *host, uint16_t port)
+{
+       return makesock(l4type, 0, host, port, NULL);
+}
+
 extern struct in_addr extract_v4_addr(const struct sockaddr_storage *ss);
 
 /**
@@ -97,7 +104,12 @@ extern struct in_addr extract_v4_addr(const struct sockaddr_storage *ss);
  */
 /** How many pending connections queue of a listening server will hold. */
 #define BACKLOG        10
-extern int para_listen(unsigned l3type, unsigned l4type, unsigned short port);
+extern int para_listen(unsigned l4type, uint16_t port, struct flowopts *fo);
+
+static inline int para_listen_simple(unsigned l4type, uint16_t port)
+{
+       return para_listen(l4type, port, NULL);
+}
 
 /** Pretty-printing of IPv4/6 socket addresses */
 extern char *local_name(int sockfd);
index bc6892ccc2dbb0479e1cf6b8724417a32d1b35cc..d9616248b5dcf1a814ef7e3c457891bdd64c2ffb 100644 (file)
@@ -42,7 +42,7 @@
  */
 static int open_sender(unsigned l4type, int port)
 {
-       int fd, ret = para_listen(AF_UNSPEC, l4type, port);
+       int fd, ret = para_listen_simple(l4type, port);
 
        if (ret < 0)
                return ret;
index 31123302ab239172980d51ebb73ef2822487ff5f..71aeaf1ce725209c4a39f0506d1d41169999c0e4 100644 (file)
--- a/server.c
+++ b/server.c
@@ -425,7 +425,7 @@ static void init_server_command_task(int argc, char **argv)
        sct->task.post_select = command_post_select;
        sct->argc = argc;
        sct->argv = argv;
-       ret = para_listen(AF_UNSPEC, IPPROTO_TCP, conf.port_arg);
+       ret = para_listen_simple(IPPROTO_TCP, conf.port_arg);
        if (ret < 0)
                goto err;
        sct->listen_fd = ret;
index 7354945384e5f9ec47763d986044a2f90496f684..ae2d49f1d44567034f849b7f833923af633e08ed 100644 (file)
@@ -181,7 +181,7 @@ static int udp_recv_open(struct receiver_node *rn)
        rn->private_data = para_calloc(sizeof(struct private_udp_recv_data));
        purd = rn->private_data;
 
-       ret = makesock(AF_UNSPEC, IPPROTO_UDP, 1, c->host_arg, c->port_arg, NULL);
+       ret = makesock(IPPROTO_UDP, 1, c->host_arg, c->port_arg, NULL);
        if (ret < 0)
                goto err;
        purd->fd = ret;
index 13d2a2ca20d52336f6743458cc4a060b8ec0ef36..4678e936d620da06d736e779eaff21f1c3383145 100644 (file)
@@ -160,7 +160,7 @@ static int udp_init_session(struct udp_target *ut)
        if (ut->fd >= 0) /* nothing to do */
                return 0;
 
-       ret = makesock(AF_UNSPEC, IPPROTO_UDP, 0, ut->host, ut->port, NULL);
+       ret = para_connect_simple(IPPROTO_UDP, ut->host, ut->port);
        if (ret < 0)
                return ret;
        ut->fd = ret;