virSetDeviceUnprivSGIO;
virSetInherit;
virSetNonBlock;
+virSetSockReuseAddr;
virSetUIDGID;
virSetUIDGIDWithCaps;
virStrIsPrint;
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;
}
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");
}
struct sockaddr* addr;
size_t addrlen;
int v6only = 1;
- int reuse = 1;
int ret = -1;
int fd = -1;
bool ipv6 = false;
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;
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) {
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);