Received: from eggs.gnu.org ([2001:4830:134:3::10]:46475) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VHDyu-0001ir-AF for qemu-devel@nongnu.org; Wed, 04 Sep 2013 10:22:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VHDyo-00083n-BU for qemu-devel@nongnu.org; Wed, 04 Sep 2013 10:22:52 -0400 Received: from mailhost.fzi.de ([141.21.8.250]:32679 helo=EX-E-1.perimeter.fzi.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VHDyo-00080a-0s for qemu-devel@nongnu.org; Wed, 04 Sep 2013 10:22:46 -0400 From: Sebastian Ottlik Date: Wed, 4 Sep 2013 16:22:18 +0200 Message-ID: <1378304540-1431-3-git-send-email-ottlik@fzi.de> In-Reply-To: <1378304540-1431-1-git-send-email-ottlik@fzi.de> References: <1378304540-1431-1-git-send-email-ottlik@fzi.de> MIME-Version: 1.0 Content-Type: text/plain Subject: [Qemu-devel] [PATCH RFC 2/4] net: do not set SO_REUSEADDR on Windows List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Jan Kiszka , Anthony Liguori , Sebastian Ottlik , Stefan Hajnoczi If a socket is closed it may remain in TIME_WAIT state for some time. On most operating systems the local port of the connection or socket may not be reeused while in this state unless SO_REUSEADDR was set on the socket. On windows on the other hand the default behaviour is to allow reuse (i.e. identical to SO_REUSEADDR on other operating systems) and setting SO_REUSEADDR on a socket allows it to be bound to a endpoint even if the endpoint is already used by another socket independently of the other sockets state. This may result in undefined behaviour. Fix this issue by no setting SO_REUSEADDR on windows. Signed-off-by: Sebastian Ottlik --- net/socket.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/net/socket.c b/net/socket.c index e61309d..f44ebcb 100644 --- a/net/socket.c +++ b/net/socket.c @@ -262,12 +262,14 @@ static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr return -1; } +#ifndef _WIN32 val = 1; ret = qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); if (ret < 0) { perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); goto fail; } +#endif ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); if (ret < 0) { @@ -523,8 +525,10 @@ static int net_socket_listen_init(NetClientState *peer, qemu_set_nonblock(fd); /* allow fast reuse */ +#ifndef _WIN32 val = 1; qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); +#endif ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); if (ret < 0) { @@ -661,6 +665,7 @@ static int net_socket_udp_init(NetClientState *peer, perror("socket(PF_INET, SOCK_DGRAM)"); return -1; } +#ifndef _WIN32 val = 1; ret = qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); @@ -669,6 +674,7 @@ static int net_socket_udp_init(NetClientState *peer, closesocket(fd); return -1; } +#endif ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr)); if (ret < 0) { perror("bind"); -- 1.7.9.5