ua_discovery_manager.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2014-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2014, 2017 (c) Florian Palm
  7. * Copyright 2015-2016 (c) Sten Grüner
  8. * Copyright 2015 (c) Chris Iatrou
  9. * Copyright 2015-2016 (c) Oleksiy Vasylyev
  10. * Copyright 2016-2017 (c) Stefan Profanter, fortiss GmbH
  11. * Copyright 2017 (c) Julian Grothoff
  12. */
  13. #ifndef UA_DISCOVERY_MANAGER_H_
  14. #define UA_DISCOVERY_MANAGER_H_
  15. #include "ua_server.h"
  16. #include "open62541_queue.h"
  17. _UA_BEGIN_DECLS
  18. #ifdef UA_ENABLE_DISCOVERY
  19. typedef struct registeredServer_list_entry {
  20. #ifdef UA_ENABLE_MULTITHREADING
  21. UA_DelayedCallback delayedCleanup;
  22. #endif
  23. LIST_ENTRY(registeredServer_list_entry) pointers;
  24. UA_RegisteredServer registeredServer;
  25. UA_DateTime lastSeen;
  26. } registeredServer_list_entry;
  27. typedef struct periodicServerRegisterCallback_entry {
  28. #ifdef UA_ENABLE_MULTITHREADING
  29. UA_DelayedCallback delayedCleanup;
  30. #endif
  31. LIST_ENTRY(periodicServerRegisterCallback_entry) pointers;
  32. struct PeriodicServerRegisterCallback *callback;
  33. } periodicServerRegisterCallback_entry;
  34. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  35. #include "mdnsd/libmdnsd/mdnsd.h"
  36. /**
  37. * TXT record:
  38. * [servername]-[hostname]._opcua-tcp._tcp.local. TXT path=/ caps=NA,DA,...
  39. *
  40. * A/AAAA record for all ip addresses:
  41. * [servername]-[hostname]._opcua-tcp._tcp.local. A [ip].
  42. * [hostname]. A [ip].
  43. */
  44. typedef struct serverOnNetwork_list_entry {
  45. #ifdef UA_ENABLE_MULTITHREADING
  46. UA_DelayedCallback delayedCleanup;
  47. #endif
  48. LIST_ENTRY(serverOnNetwork_list_entry) pointers;
  49. UA_ServerOnNetwork serverOnNetwork;
  50. UA_DateTime created;
  51. UA_DateTime lastSeen;
  52. UA_Boolean txtSet;
  53. UA_Boolean srvSet;
  54. char* pathTmp;
  55. } serverOnNetwork_list_entry;
  56. #define SERVER_ON_NETWORK_HASH_PRIME 1009
  57. typedef struct serverOnNetwork_hash_entry {
  58. serverOnNetwork_list_entry* entry;
  59. struct serverOnNetwork_hash_entry* next;
  60. } serverOnNetwork_hash_entry;
  61. typedef struct mdnsHostnameToIp_list_entry {
  62. LIST_ENTRY(mdnsHostnameToIp_list_entry) pointers;
  63. UA_String mdnsHostname;
  64. struct in_addr addr;
  65. } mdnsHostnameToIp_list_entry;
  66. #define MDNS_HOSTNAME_TO_IP_HASH_PRIME 1009
  67. typedef struct mdnsHostnameToIp_hash_entry {
  68. mdnsHostnameToIp_list_entry* entry;
  69. struct mdnsHostnameToIp_hash_entry* next;
  70. } mdnsHostnameToIp_hash_entry;
  71. #endif
  72. typedef struct {
  73. LIST_HEAD(, periodicServerRegisterCallback_entry) periodicServerRegisterCallbacks;
  74. LIST_HEAD(, registeredServer_list_entry) registeredServers;
  75. size_t registeredServersSize;
  76. UA_Server_registerServerCallback registerServerCallback;
  77. void* registerServerCallbackData;
  78. # ifdef UA_ENABLE_DISCOVERY_MULTICAST
  79. mdns_daemon_t *mdnsDaemon;
  80. UA_SOCKET mdnsSocket;
  81. UA_Boolean mdnsMainSrvAdded;
  82. LIST_HEAD(, serverOnNetwork_list_entry) serverOnNetwork;
  83. size_t serverOnNetworkSize;
  84. UA_UInt32 serverOnNetworkRecordIdCounter;
  85. UA_DateTime serverOnNetworkRecordIdLastReset;
  86. /* hash mapping domain name to serverOnNetwork list entry */
  87. struct serverOnNetwork_hash_entry* serverOnNetworkHash[SERVER_ON_NETWORK_HASH_PRIME];
  88. UA_Server_serverOnNetworkCallback serverOnNetworkCallback;
  89. void* serverOnNetworkCallbackData;
  90. LIST_HEAD(, mdnsHostnameToIp_list_entry) mdnsHostnameToIp;
  91. /* hash mapping hostname to ip */
  92. struct mdnsHostnameToIp_hash_entry* mdnsHostnameToIpHash[MDNS_HOSTNAME_TO_IP_HASH_PRIME];
  93. # ifdef UA_ENABLE_MULTITHREADING
  94. pthread_t mdnsThread;
  95. UA_Boolean mdnsRunning;
  96. # endif
  97. # endif /* UA_ENABLE_DISCOVERY_MULTICAST */
  98. } UA_DiscoveryManager;
  99. void UA_DiscoveryManager_init(UA_DiscoveryManager *dm, UA_Server *server);
  100. void UA_DiscoveryManager_deleteMembers(UA_DiscoveryManager *dm, UA_Server *server);
  101. /* Checks if a registration timed out and removes that registration.
  102. * Should be called periodically in main loop */
  103. void UA_Discovery_cleanupTimedOut(UA_Server *server, UA_DateTime nowMonotonic);
  104. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  105. void
  106. UA_Server_updateMdnsForDiscoveryUrl(UA_Server *server, const UA_String *serverName,
  107. const UA_MdnsDiscoveryConfiguration *mdnsConfig,
  108. const UA_String *discoveryUrl,
  109. UA_Boolean isOnline, UA_Boolean updateTxt);
  110. void mdns_record_received(const struct resource *r, void *data);
  111. void mdns_create_txt(UA_Server *server, const char *fullServiceDomain,
  112. const char *path, const UA_String *capabilites,
  113. const size_t *capabilitiesSize,
  114. void (*conflict)(char *host, int type, void *arg));
  115. void mdns_set_address_record(UA_Server *server,
  116. const char *fullServiceDomain,
  117. const char *localDomain);
  118. mdns_record_t *
  119. mdns_find_record(mdns_daemon_t *mdnsDaemon, unsigned short type,
  120. const char *host, const char *rdname);
  121. void startMulticastDiscoveryServer(UA_Server *server);
  122. void stopMulticastDiscoveryServer(UA_Server *server);
  123. UA_StatusCode
  124. iterateMulticastDiscoveryServer(UA_Server* server, UA_DateTime *nextRepeat,
  125. UA_Boolean processIn);
  126. typedef enum {
  127. UA_DISCOVERY_TCP, /* OPC UA TCP mapping */
  128. UA_DISCOVERY_TLS /* OPC UA HTTPS mapping */
  129. } UA_DiscoveryProtocol;
  130. /* Send a multicast probe to find any other OPC UA server on the network through mDNS. */
  131. UA_StatusCode
  132. UA_Discovery_multicastQuery(UA_Server* server);
  133. UA_StatusCode
  134. UA_Discovery_addRecord(UA_Server *server, const UA_String *servername,
  135. const UA_String *hostname, UA_UInt16 port,
  136. const UA_String *path, const UA_DiscoveryProtocol protocol,
  137. UA_Boolean createTxt, const UA_String* capabilites,
  138. size_t *capabilitiesSize);
  139. UA_StatusCode
  140. UA_Discovery_removeRecord(UA_Server *server, const UA_String *servername,
  141. const UA_String *hostname, UA_UInt16 port,
  142. UA_Boolean removeTxt);
  143. #endif /* UA_ENABLE_DISCOVERY_MULTICAST */
  144. #endif /* UA_ENABLE_DISCOVERY */
  145. _UA_END_DECLS
  146. #endif /* UA_DISCOVERY_MANAGER_H_ */