Explorar o código

fix win32 mdns socket

StalderT %!s(int64=7) %!d(string=hai) anos
pai
achega
f8e7160d1a

+ 4 - 0
src/server/ua_server.c

@@ -222,7 +222,11 @@ UA_Server_new(const UA_ServerConfig *config) {
     /* Initialize multicast discovery */
 #if defined(UA_ENABLE_DISCOVERY) && defined(UA_ENABLE_DISCOVERY_MULTICAST)
     server->mdnsDaemon = NULL;
+#ifdef _WIN32
+    server->mdnsSocket = INVALID_SOCKET;
+#else
     server->mdnsSocket = -1;
+#endif
     server->mdnsMainSrvAdded = UA_FALSE;
     if(server->config.applicationDescription.applicationType == UA_APPLICATIONTYPE_DISCOVERYSERVER)
         initMulticastDiscoveryServer(server);

+ 4 - 0
src/server/ua_server_internal.h

@@ -81,7 +81,11 @@ struct UA_Server {
     void* registerServerCallbackData;
 # ifdef UA_ENABLE_DISCOVERY_MULTICAST
     mdns_daemon_t *mdnsDaemon;
+#ifdef _WIN32
+    SOCKET mdnsSocket;
+#else
     int mdnsSocket;
+#endif
     UA_Boolean mdnsMainSrvAdded;
 #  ifdef UA_ENABLE_MULTITHREADING
     pthread_t mdnsThread;

+ 32 - 2
src/server/ua_services_discovery_multicast.c

@@ -284,9 +284,18 @@ socket_mdns_set_nonblocking(int sockfd) {
 }
 
 /* Create multicast 224.0.0.251:5353 socket */
+#ifdef _WIN32
+static SOCKET
+#else
 static int
+#endif
 discovery_createMulticastSocket(void) {
-    int s, flag = 1, ittl = 255;
+#ifdef _WIN32
+    SOCKET s;
+#else
+    int s;
+#endif
+    int flag = 1, ittl = 255;
     struct sockaddr_in in;
     struct ip_mreq mc;
     char ttl = (char)255; // publish to complete net, not only subnet. See:
@@ -297,8 +306,13 @@ discovery_createMulticastSocket(void) {
     in.sin_port = htons(5353);
     in.sin_addr.s_addr = 0;
 
-    if ((s = (int)socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
+#ifdef _WIN32
+    if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
+        return INVALID_SOCKET;
+#else
+    if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
         return -1;
+#endif
 
 #ifdef SO_REUSEPORT
     setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (char *)&flag, sizeof(flag));
@@ -306,7 +320,11 @@ discovery_createMulticastSocket(void) {
     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof(flag));
     if (bind(s, (struct sockaddr *)&in, sizeof(in))) {
         CLOSESOCKET(s);
+#ifdef _WIN32
+        return INVALID_SOCKET;
+#else
         return -1;
+#endif
     }
 
     mc.imr_multiaddr.s_addr = inet_addr("224.0.0.251");
@@ -329,7 +347,11 @@ initMulticastDiscoveryServer(UA_Server* server) {
     WSAStartup(wVersionRequested, &wsaData);
 #endif
 
+#ifdef _WIN32
+    if((server->mdnsSocket = discovery_createMulticastSocket()) == INVALID_SOCKET) {
+#else
     if((server->mdnsSocket = discovery_createMulticastSocket()) < 0) {
+#endif
         UA_LOG_SOCKET_ERRNO_WRAP(
                 UA_LOG_ERROR(server->config.logger, UA_LOGCATEGORY_SERVER,
                      "Could not create multicast socket. Error: %s", errno_str));
@@ -343,9 +365,17 @@ initMulticastDiscoveryServer(UA_Server* server) {
 void destroyMulticastDiscoveryServer(UA_Server* server) {
     mdnsd_shutdown(server->mdnsDaemon);
     mdnsd_free(server->mdnsDaemon);
+#ifdef _WIN32
+    if (server->mdnsSocket != INVALID_SOCKET) {
+#else
     if (server->mdnsSocket >= 0) {
+#endif
         CLOSESOCKET(server->mdnsSocket);
+#ifdef _WIN32
+        server->mdnsSocket = INVALID_SOCKET;
+#else
         server->mdnsSocket = -1;
+#endif
     }
 }