net: fix leak of fd in bind() error path
[fio.git] / engines / net.c
index add98dc015cc531fe2f793f0af6cdfde4e5fb417..d036a581811ac5b945ae074169b90f66f090142f 100644 (file)
@@ -94,18 +94,22 @@ static struct fio_option options[] = {
                            .oval = FIO_TYPE_TCP,
                            .help = "Transmission Control Protocol",
                          },
+#ifdef CONFIG_IPV6
                          { .ival = "tcpv6",
                            .oval = FIO_TYPE_TCP_V6,
                            .help = "Transmission Control Protocol V6",
                          },
+#endif
                          { .ival = "udp",
                            .oval = FIO_TYPE_UDP,
                            .help = "User Datagram Protocol",
                          },
+#ifdef CONFIG_IPV6
                          { .ival = "udpv6",
                            .oval = FIO_TYPE_UDP_V6,
                            .help = "User Datagram Protocol V6",
                          },
+#endif
                          { .ival = "unix",
                            .oval = FIO_TYPE_UNIX,
                            .help = "UNIX domain socket",
@@ -388,7 +392,7 @@ static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
 
        do {
                if (is_udp(o)) {
-                       struct sockaddr *to;
+                       const struct sockaddr *to;
                        socklen_t len;
 
                        if (is_ipv6(o)) {
@@ -846,11 +850,52 @@ static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
        return ret;
 }
 
+static int fio_fill_addr(struct thread_data *td, const char *host, int af,
+                        void *dst, struct addrinfo **res)
+{
+       struct netio_options *o = td->eo;
+       struct addrinfo hints;
+       int ret;
+
+       if (inet_pton(af, host, dst))
+               return 0;
+
+       memset(&hints, 0, sizeof(hints));
+
+       if (is_tcp(o))
+               hints.ai_socktype = SOCK_STREAM;
+       else
+               hints.ai_socktype = SOCK_DGRAM;
+
+       if (is_ipv6(o))
+               hints.ai_family = AF_INET6;
+       else
+               hints.ai_family = AF_INET;
+
+       ret = getaddrinfo(host, NULL, &hints, res);
+       if (ret) {
+               int e = EINVAL;
+               char str[128];
+
+               if (ret == EAI_SYSTEM)
+                       e = errno;
+
+               snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
+               td_verror(td, e, str);
+               return 1;
+       }
+
+       return 0;
+}
+
 static int fio_netio_setup_connect_inet(struct thread_data *td,
                                        const char *host, unsigned short port)
 {
        struct netio_data *nd = td->io_ops->data;
        struct netio_options *o = td->eo;
+       struct addrinfo *res = NULL;
+       void *dst, *src;
+       int af, len;
 
        if (!host) {
                log_err("fio: connect with no host to connect to.\n");
@@ -861,44 +906,35 @@ static int fio_netio_setup_connect_inet(struct thread_data *td,
                return 1;
        }
 
-       if (is_ipv6(o)) {
-               nd->addr6.sin6_family = AF_INET6;
-               nd->addr6.sin6_port = htons(port);
-
-               if (!inet_pton(AF_INET6, host, &nd->addr6.sin6_addr)) {
-                       struct addrinfo hints, *res;
+       nd->addr.sin_family = AF_INET;
+       nd->addr.sin_port = htons(port);
+       nd->addr6.sin6_family = AF_INET6;
+       nd->addr6.sin6_port = htons(port);
 
-                       memset(&hints, 0, sizeof(hints));
-                       hints.ai_socktype = SOCK_STREAM;
-
-                       if (getaddrinfo(host, NULL, &hints, &res)) {
-                               td_verror(td, errno, "gethostbyname");
-                               return 1;
-                       }
-
-                       memcpy(&nd->addr6.sin6_addr, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(nd->addr6.sin6_addr));
-                       freeaddrinfo(res);
-               }
+       if (is_ipv6(o)) {
+               af = AF_INET6;
+               dst = &nd->addr6.sin6_addr;
        } else {
-               nd->addr.sin_family = AF_INET;
-               nd->addr.sin_port = htons(port);
-
-               if (!inet_pton(AF_INET, host, &nd->addr.sin_addr)) {
-                       struct addrinfo hints, *res;
+               af = AF_INET;
+               dst = &nd->addr.sin_addr;
+       }
 
-                       memset(&hints, 0, sizeof(hints));
-                       hints.ai_socktype = SOCK_STREAM;
+       if (fio_fill_addr(td, host, af, dst, &res))
+               return 1;
 
-                       if (getaddrinfo(host, NULL, &hints, &res)) {
-                               td_verror(td, errno, "gethostbyname");
-                               return 1;
-                       }
+       if (!res)
+               return 0;
 
-                       memcpy(&nd->addr.sin_addr, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(nd->addr.sin_addr));
-                       freeaddrinfo(res);
-               }
+       if (is_ipv6(o)) {
+               len = sizeof(nd->addr6.sin6_addr);
+               src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
+       } else {
+               len = sizeof(nd->addr.sin_addr);
+               src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
        }
 
+       memcpy(dst, src, len);
+       freeaddrinfo(res);
        return 0;
 }
 
@@ -1007,12 +1043,17 @@ static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
        }
 #endif
 
-       if (td->o.filename){
+       if (td->o.filename) {
                if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
                        log_err("fio: hostname not valid for non-multicast inbound network IO\n");
                        close(fd);
                        return 1;
                }
+               if (is_ipv6(o)) {
+                       log_err("fio: IPv6 not supported for multicast network IO");
+                       close(fd);
+                       return 1;
+               }
 
                inet_aton(td->o.filename, &sin.sin_addr);
 
@@ -1026,6 +1067,7 @@ static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
                } else {
                        mr.imr_interface.s_addr = htonl(INADDR_ANY);
                }
+
                if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
                        td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
                        close(fd);
@@ -1050,6 +1092,7 @@ static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
        }
 
        if (bind(fd, saddr, len) < 0) {
+               close(fd);
                td_verror(td, errno, "bind");
                return 1;
        }
@@ -1152,8 +1195,9 @@ static int fio_netio_setup(struct thread_data *td)
        struct netio_data *nd;
 
        if (!td->files_index) {
-               add_file(td, td->o.filename ?: "net");
+               add_file(td, td->o.filename ?: "net", 0, 0);
                td->o.nr_files = td->o.nr_files ?: 1;
+               td->o.open_files++;
        }
 
        if (!td->io_ops->data) {