提交 37697d82 编写于 作者: D Daniel P. Berrange

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.aspxSigned-off-by: NDaniel P. Berrange <berrange@redhat.com>
上级 837154a1
......@@ -2060,6 +2060,7 @@ virSetCloseExec;
virSetDeviceUnprivSGIO;
virSetInherit;
virSetNonBlock;
virSetSockReuseAddr;
virSetUIDGID;
virSetUIDGIDWithCaps;
virStrIsPrint;
......
......@@ -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");
}
......
......@@ -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;
......
......@@ -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) {
......
......@@ -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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册