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 <quint...@redhat.com> --- 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); -- 1.7.4