From patchwork Fri Mar 18 13:22:48 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juan Quintela X-Patchwork-Id: 87532 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 8ED68B6FE1 for ; Sat, 19 Mar 2011 00:27:34 +1100 (EST) Received: from localhost ([127.0.0.1]:42538 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q0Zi8-0004Tt-2B for incoming@patchwork.ozlabs.org; Fri, 18 Mar 2011 09:27:24 -0400 Received: from [140.186.70.92] (port=45977 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Q0Zdu-0001tz-Qt for qemu-devel@nongnu.org; Fri, 18 Mar 2011 09:23:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Q0Zdt-0004qS-06 for qemu-devel@nongnu.org; Fri, 18 Mar 2011 09:23:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:8571) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Q0Zds-0004q5-G4 for qemu-devel@nongnu.org; Fri, 18 Mar 2011 09:23:00 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p2IDMxRE003167 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 18 Mar 2011 09:22:59 -0400 Received: from trasno.mitica (ovpn-113-108.phx2.redhat.com [10.3.113.108]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p2IDMudO028014; Fri, 18 Mar 2011 09:22:59 -0400 From: Juan Quintela To: qemu-devel@nongnu.org Date: Fri, 18 Mar 2011 14:22:48 +0100 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/3] create socket_set_reuseaddr() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Windows is different than unix, SO_REUSEADDR is the default value there. Create one function to do it and change all callers. Signed-off-by: Juan Quintela --- gdbstub.c | 6 ++---- migration-tcp.c | 4 +--- nbd.c | 5 +---- net/socket.c | 13 ++++--------- os-posix.c | 1 + oslib-posix.c | 8 ++++++++ oslib-win32.c | 6 ++++++ qemu-sockets.c | 6 +++--- qemu_socket.h | 1 + tests/linux-test.c | 4 +--- 10 files changed, 28 insertions(+), 26 deletions(-) diff --git a/gdbstub.c b/gdbstub.c index 1e9f931..b12e20e 100644 --- a/gdbstub.c +++ b/gdbstub.c @@ -2591,7 +2591,7 @@ static void gdb_accept(void) static int gdbserver_open(int port) { struct sockaddr_in sockaddr; - int fd, val, ret; + int fd, ret; fd = socket(PF_INET, SOCK_STREAM, 0); if (fd < 0) { @@ -2602,9 +2602,7 @@ static int gdbserver_open(int port) fcntl(fd, F_SETFD, FD_CLOEXEC); #endif - /* allow fast reuse */ - val = 1; - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)); + socket_set_reuseaddr(fd); sockaddr.sin_family = AF_INET; sockaddr.sin_port = htons(port); diff --git a/migration-tcp.c b/migration-tcp.c index b55f419..2340b55 100644 --- a/migration-tcp.c +++ b/migration-tcp.c @@ -172,7 +172,6 @@ out2: int tcp_start_incoming_migration(const char *host_port) { struct sockaddr_in addr; - int val; int s; if (parse_host_port(&addr, host_port) < 0) { @@ -184,8 +183,7 @@ int tcp_start_incoming_migration(const char *host_port) if (s == -1) return -socket_error(); - val = 1; - setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); + socket_set_reuseaddr(s); if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) == -1) goto err; diff --git a/nbd.c b/nbd.c index d8ebc42..3ecc792 100644 --- a/nbd.c +++ b/nbd.c @@ -148,7 +148,6 @@ int tcp_socket_incoming(const char *address, uint16_t port) int s; struct in_addr in; struct sockaddr_in addr; - int opt; s = socket(PF_INET, SOCK_STREAM, 0); if (s == -1) { @@ -170,9 +169,7 @@ int tcp_socket_incoming(const char *address, uint16_t port) addr.sin_port = htons(port); memcpy(&addr.sin_addr.s_addr, &in, sizeof(in)); - opt = 1; - if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, - (const void *) &opt, sizeof(opt)) == -1) { + if (socket_set_reuseaddr(s)) { goto error; } diff --git a/net/socket.c b/net/socket.c index 7337f4f..de46506 100644 --- a/net/socket.c +++ b/net/socket.c @@ -167,11 +167,9 @@ static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr return -1; } - val = 1; - ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, - (const char *)&val, sizeof(val)); + ret = socket_set_reuseaddr(fd); if (ret < 0) { - perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); + perror("socket_set_reuseaddr"); goto fail; } @@ -392,7 +390,7 @@ static int net_socket_listen_init(VLANState *vlan, const char *host_str) { NetSocketListenState *s; - int fd, val, ret; + int fd, ret; struct sockaddr_in saddr; if (parse_host_port(&saddr, host_str) < 0) @@ -406,10 +404,7 @@ static int net_socket_listen_init(VLANState *vlan, return -1; } socket_set_nonblock(fd); - - /* allow fast reuse */ - val = 1; - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); + socket_set_reuseaddr(fd); ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); if (ret < 0) { diff --git a/os-posix.c b/os-posix.c index 38c29d1..e7116c7 100644 --- a/os-posix.c +++ b/os-posix.c @@ -382,3 +382,4 @@ int qemu_create_pidfile(const char *filename) return 0; } + diff --git a/oslib-posix.c b/oslib-posix.c index 7bc5f7c..2f3383a 100644 --- a/oslib-posix.c +++ b/oslib-posix.c @@ -72,6 +72,14 @@ void qemu_vfree(void *ptr) free(ptr); } +int socket_set_reuseaddr(int fd) +{ + int val = 1; + /* allow fast reuse */ + return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, + sizeof(val)); +} + void socket_set_nonblock(int fd) { int f; diff --git a/oslib-win32.c b/oslib-win32.c index 5f0759f..1641e1f 100644 --- a/oslib-win32.c +++ b/oslib-win32.c @@ -79,6 +79,12 @@ void socket_set_nonblock(int fd) ioctlsocket(fd, FIONBIO, &opt); } +int socket_set_reuseaddr(int fd) +{ + /* Nothing, this is the default windows behaviour */ + return 0; +} + int inet_aton(const char *cp, struct in_addr *ia) { uint32_t addr = inet_addr(cp); diff --git a/qemu-sockets.c b/qemu-sockets.c index c526324..293dad6 100644 --- a/qemu-sockets.c +++ b/qemu-sockets.c @@ -168,7 +168,7 @@ int inet_listen_opts(QemuOpts *opts, int port_offset) continue; } - setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); + socket_set_reuseaddr(slisten); #ifdef IPV6_V6ONLY if (e->ai_family == PF_INET6) { /* listen on both ipv4 and ipv6 */ @@ -265,7 +265,7 @@ int inet_connect_opts(QemuOpts *opts) inet_strfamily(e->ai_family), strerror(errno)); continue; } - setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); + socket_set_reuseaddr(sock); /* connect to peer */ if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) { @@ -358,7 +358,7 @@ int inet_dgram_opts(QemuOpts *opts) inet_strfamily(peer->ai_family), strerror(errno)); goto err; } - setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); + socket_set_reuseaddr(sock); /* bind socket */ if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen, diff --git a/qemu_socket.h b/qemu_socket.h index 180e4db..f9a7f95 100644 --- a/qemu_socket.h +++ b/qemu_socket.h @@ -36,6 +36,7 @@ int inet_aton(const char *cp, struct in_addr *ia); int qemu_socket(int domain, int type, int protocol); int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen); void socket_set_nonblock(int fd); +int socket_set_reuseaddr(int fd); int send_all(int fd, const void *buf, int len1); /* New, ipv6-ready socket helper functions, see qemu-sockets.c */ diff --git a/tests/linux-test.c b/tests/linux-test.c index 2e4a746..2635d64 100644 --- a/tests/linux-test.c +++ b/tests/linux-test.c @@ -283,9 +283,7 @@ int server_socket(void) /* server socket */ fd = chk_error(socket(PF_INET, SOCK_STREAM, 0)); - - val = 1; - chk_error(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))); + chk_error(socket_set_reuseaddr(fd)); sockaddr.sin_family = AF_INET; sockaddr.sin_port = htons(TESTPORT);