Browse Source

feat(core): Allow setting custom ip address list if platform does not support getifaddr

This is the case e.g. on an ESP32 chip
Stefan Profanter 5 years ago
parent
commit
60ea3d6f5d

+ 3 - 0
arch/freertosLWIP/ua_architecture.h

@@ -26,6 +26,9 @@
 #define UA_LOCK_ASSERT(mutexName, num)
 #endif
 
+// freeRTOS does not have getifaddr
+#undef UA_HAS_GETIFADDR
+
 #include <open62541/architecture_functions.h>
 
 #endif /* PLUGINS_ARCH_FREERTOSLWIP_UA_ARCHITECTURE_H_ */

+ 3 - 0
include/open62541/config.h.in

@@ -103,6 +103,9 @@
 # endif
 #endif
 
+// specific architectures can undef this
+#define UA_HAS_GETIFADDR
+
 #include "${UA_ARCHITECTURE}/ua_architecture.h"
 
 #endif /* UA_CONFIG_H_ */

+ 4 - 0
include/open62541/server_config.h

@@ -78,6 +78,10 @@ typedef struct {
 #ifdef UA_ENABLE_DISCOVERY_MULTICAST
     UA_MdnsDiscoveryConfiguration mdns;
     UA_String mdnsInterfaceIP;
+# if !defined(UA_HAS_GETIFADDR)
+    uint32_t *ipAddressList;
+    size_t ipAddressListSize;
+# endif
 #endif
 
 } UA_ServerConfig_Discovery;

+ 4 - 0
plugins/ua_config_default.c

@@ -152,6 +152,10 @@ setDefaultConfig(UA_ServerConfig *conf) {
 #ifdef UA_ENABLE_DISCOVERY_MULTICAST
     UA_MdnsDiscoveryConfiguration_clear(&conf->discovery.mdns);
     conf->discovery.mdnsInterfaceIP = UA_STRING_NULL;
+# if !defined(UA_HAS_GETIFADDR)
+    conf->discovery.ipAddressList = NULL;
+    conf->discovery.ipAddressListSize = 0;
+# endif
 #endif
 
     /* Custom DataTypes */

+ 5 - 0
src/server/ua_server_config.c

@@ -18,6 +18,11 @@ UA_ServerConfig_clean(UA_ServerConfig *config) {
 #ifdef UA_ENABLE_DISCOVERY_MULTICAST
     UA_MdnsDiscoveryConfiguration_clear(&config->discovery.mdns);
     UA_String_clear(&config->discovery.mdnsInterfaceIP);
+# if !defined(UA_HAS_GETIFADDR)
+    if (config->discovery.ipAddressListSize) {
+        UA_free(config->discovery.ipAddressList);
+    }
+# endif
 #endif
 
     /* Custom DataTypes */

+ 39 - 5
src/server/ua_server_discovery_mdns.c

@@ -24,7 +24,9 @@
 #else
 # include <sys/time.h> // for struct timeval
 # include <netinet/in.h> // for struct ip_mreq
-# include <ifaddrs.h>
+# if defined(UA_HAS_GETIFADDR)
+#  include <ifaddrs.h>
+# endif /* UA_HAS_GETIFADDR */
 # include <net/if.h> /* for IFF_RUNNING */
 # include <netdb.h> // for recvfrom in cygwin
 #endif
@@ -200,13 +202,19 @@ mdns_is_self_announce(const UA_Server *server, const struct serverOnNetwork_list
                      "getifaddrs returned an unexpected error. Not setting mDNS A records.");
         return false;
     }
-#else
+#elif defined(UA_HAS_GETIFADDR)
     struct ifaddrs *ifaddr, *ifa;
     if(getifaddrs(&ifaddr) == -1) {
         UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
                      "getifaddrs returned an unexpected error. Not setting mDNS A records.");
         return false;
     }
+#else
+    if (server->config.discovery.ipAddressListSize == 0) {
+        UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
+                     "If UA_HAS_GETIFADDR is false, config.discovery.ipAddressList must be set");
+        return false;
+    }
 #endif
 
 
@@ -255,7 +263,7 @@ mdns_is_self_announce(const UA_Server *server, const struct serverOnNetwork_list
             if (isSelf)
                 break;
         }
-#else
+#elif defined(UA_HAS_GETIFADDR)
         /* Walk through linked list, maintaining head pointer so we can free
          * list later */
         int n;
@@ -282,6 +290,15 @@ mdns_is_self_announce(const UA_Server *server, const struct serverOnNetwork_list
 
             /* IPv6 not implemented yet */
         }
+#else
+        for(size_t idx=0; i<server->config.discovery.ipAddressListSize; idx++) {
+            char *ipStr = inet_ntoa(*( (struct in_addr *) &server->config.discovery.ipAddressList[idx]));
+            if(strncmp((const char*)hostnameRemote.data, ipStr,
+                       hostnameRemote.length) == 0) {
+                isSelf = true;
+                break;
+            }
+        }
 #endif
         if (isSelf)
             break;
@@ -291,7 +308,7 @@ mdns_is_self_announce(const UA_Server *server, const struct serverOnNetwork_list
     /* Cleanup */
     UA_free(adapter_addresses);
     adapter_addresses = NULL;
-#else
+#elif defined(UA_HAS_GETIFADDR)
     /* Clean up */
     freeifaddrs(ifaddr);
 #endif
@@ -669,7 +686,7 @@ void mdns_set_address_record(UA_Server *server, const char *fullServiceDomain,
     adapter_addresses = NULL;
 }
 
-#else /* _WIN32 */
+#elif defined(UA_HAS_GETIFADDR)
 
 void
 mdns_set_address_record(UA_Server *server, const char *fullServiceDomain,
@@ -705,6 +722,23 @@ mdns_set_address_record(UA_Server *server, const char *fullServiceDomain,
     /* Clean up */
     freeifaddrs(ifaddr);
 }
+#else /* _WIN32 */
+
+void
+mdns_set_address_record(UA_Server *server, const char *fullServiceDomain,
+                        const char *localDomain) {
+
+    if (server->config.discovery.ipAddressListSize == 0) {
+        UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
+                     "If UA_HAS_GETIFADDR is false, config.discovery.ipAddressList must be set");
+        return;
+    }
+
+    for(size_t i=0; i<server->config.discovery.ipAddressListSize; i++) {
+        mdns_set_address_record_if(&server->discoveryManager, fullServiceDomain,
+                                   localDomain, (char*)&server->config.discovery.ipAddressList[i], 4);
+    }
+}
 
 #endif /* _WIN32 */