Don't use SO_REUSEADDR on Win32 platforms

SO_REUSEADDR on Windows is actually akin to SO_REUSEPORT
on Linux/BSD. ie it allows 2 apps to listen to the same
port at once. Thus we must not set it on Win32 platforms

See http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2014-04-25 17:47:08 +01:00
parent 837154a151
commit 37697d828b
5 changed files with 28 additions and 8 deletions

View File

@ -2060,6 +2060,7 @@ virSetCloseExec;
virSetDeviceUnprivSGIO;
virSetInherit;
virSetNonBlock;
virSetSockReuseAddr;
virSetUIDGID;
virSetUIDGIDWithCaps;
virStrIsPrint;

View File

@ -255,8 +255,7 @@ int virNetSocketNewListenTCP(const char *nodename,
goto error;
}
int opt = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
if (virSetSockReuseAddr(fd) < 0) {
virReportSystemError(errno, "%s", _("Unable to enable port reuse"));
goto error;
}
@ -460,15 +459,13 @@ int virNetSocketNewConnectTCP(const char *nodename,
runp = ai;
while (runp) {
int opt = 1;
if ((fd = socket(runp->ai_family, runp->ai_socktype,
runp->ai_protocol)) < 0) {
virReportSystemError(errno, "%s", _("Unable to create socket"));
goto error;
}
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
if (virSetSockReuseAddr(fd) < 0) {
VIR_WARN("Unable to enable port reuse");
}

View File

@ -116,7 +116,6 @@ static int virPortAllocatorBindToPort(bool *used,
struct sockaddr* addr;
size_t addrlen;
int v6only = 1;
int reuse = 1;
int ret = -1;
int fd = -1;
bool ipv6 = false;
@ -143,8 +142,7 @@ static int virPortAllocatorBindToPort(bool *used,
goto cleanup;
}
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&reuse,
sizeof(reuse)) < 0) {
if (virSetSockReuseAddr(fd) < 0) {
virReportSystemError(errno, "%s",
_("Unable to set socket reuse addr flag"));
goto cleanup;

View File

@ -136,6 +136,29 @@ int virSetCloseExec(int fd)
return virSetInherit(fd, false);
}
#ifdef WIN32
int virSetSockReuseAddr(int fd ATTRIBUTE_UNUSED)
{
/*
* SO_REUSEADDR on Windows is actually akin to SO_REUSEPORT
* on Linux/BSD. ie it allows 2 apps to listen to the same
* port at once which is certainly not what we want here.
*
* Win32 sockets have Linux/BSD-like SO_REUSEADDR behaviour
* by default, so we can be a no-op.
*
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx
*/
return 0;
}
#else
int virSetSockReuseAddr(int fd)
{
int opt = 1;
return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
}
#endif
int
virPipeReadUntilEOF(int outfd, int errfd,
char **outbuf, char **errbuf) {

View File

@ -42,6 +42,7 @@ int virSetBlocking(int fd, bool blocking) ATTRIBUTE_RETURN_CHECK;
int virSetNonBlock(int fd) ATTRIBUTE_RETURN_CHECK;
int virSetInherit(int fd, bool inherit) ATTRIBUTE_RETURN_CHECK;
int virSetCloseExec(int fd) ATTRIBUTE_RETURN_CHECK;
int virSetSockReuseAddr(int fd) ATTRIBUTE_RETURN_CHECK;
int virPipeReadUntilEOF(int outfd, int errfd,
char **outbuf, char **errbuf);