ua_discovery_manager.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <open62541/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. #endif
  62. typedef struct {
  63. LIST_HEAD(, periodicServerRegisterCallback_entry) periodicServerRegisterCallbacks;
  64. LIST_HEAD(, registeredServer_list_entry) registeredServers;
  65. size_t registeredServersSize;
  66. UA_Server_registerServerCallback registerServerCallback;
  67. void* registerServerCallbackData;
  68. # ifdef UA_ENABLE_DISCOVERY_MULTICAST
  69. mdns_daemon_t *mdnsDaemon;
  70. UA_SOCKET mdnsSocket;
  71. UA_Boolean mdnsMainSrvAdded;
  72. LIST_HEAD(, serverOnNetwork_list_entry) serverOnNetwork;
  73. size_t serverOnNetworkSize;
  74. UA_UInt32 serverOnNetworkRecordIdCounter;
  75. UA_DateTime serverOnNetworkRecordIdLastReset;
  76. /* hash mapping domain name to serverOnNetwork list entry */
  77. struct serverOnNetwork_hash_entry* serverOnNetworkHash[SERVER_ON_NETWORK_HASH_PRIME];
  78. UA_Server_serverOnNetworkCallback serverOnNetworkCallback;
  79. void* serverOnNetworkCallbackData;
  80. # ifdef UA_ENABLE_MULTITHREADING
  81. pthread_t mdnsThread;
  82. UA_Boolean mdnsRunning;
  83. # endif
  84. # endif /* UA_ENABLE_DISCOVERY_MULTICAST */
  85. } UA_DiscoveryManager;
  86. void UA_DiscoveryManager_init(UA_DiscoveryManager *dm, UA_Server *server);
  87. void UA_DiscoveryManager_deleteMembers(UA_DiscoveryManager *dm, UA_Server *server);
  88. /* Checks if a registration timed out and removes that registration.
  89. * Should be called periodically in main loop */
  90. void UA_Discovery_cleanupTimedOut(UA_Server *server, UA_DateTime nowMonotonic);
  91. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  92. void
  93. UA_Server_updateMdnsForDiscoveryUrl(UA_Server *server, const UA_String *serverName,
  94. const UA_MdnsDiscoveryConfiguration *mdnsConfig,
  95. const UA_String *discoveryUrl,
  96. UA_Boolean isOnline, UA_Boolean updateTxt);
  97. void mdns_record_received(const struct resource *r, void *data);
  98. void mdns_create_txt(UA_Server *server, const char *fullServiceDomain,
  99. const char *path, const UA_String *capabilites,
  100. const size_t *capabilitiesSize,
  101. void (*conflict)(char *host, int type, void *arg));
  102. void mdns_set_address_record(UA_Server *server,
  103. const char *fullServiceDomain,
  104. const char *localDomain);
  105. mdns_record_t *
  106. mdns_find_record(mdns_daemon_t *mdnsDaemon, unsigned short type,
  107. const char *host, const char *rdname);
  108. void startMulticastDiscoveryServer(UA_Server *server);
  109. void stopMulticastDiscoveryServer(UA_Server *server);
  110. UA_StatusCode
  111. iterateMulticastDiscoveryServer(UA_Server* server, UA_DateTime *nextRepeat,
  112. UA_Boolean processIn);
  113. typedef enum {
  114. UA_DISCOVERY_TCP, /* OPC UA TCP mapping */
  115. UA_DISCOVERY_TLS /* OPC UA HTTPS mapping */
  116. } UA_DiscoveryProtocol;
  117. /* Send a multicast probe to find any other OPC UA server on the network through mDNS. */
  118. UA_StatusCode
  119. UA_Discovery_multicastQuery(UA_Server* server);
  120. UA_StatusCode
  121. UA_Discovery_addRecord(UA_Server *server, const UA_String *servername,
  122. const UA_String *hostname, UA_UInt16 port,
  123. const UA_String *path, const UA_DiscoveryProtocol protocol,
  124. UA_Boolean createTxt, const UA_String* capabilites,
  125. size_t *capabilitiesSize);
  126. UA_StatusCode
  127. UA_Discovery_removeRecord(UA_Server *server, const UA_String *servername,
  128. const UA_String *hostname, UA_UInt16 port,
  129. UA_Boolean removeTxt);
  130. #endif /* UA_ENABLE_DISCOVERY_MULTICAST */
  131. #endif /* UA_ENABLE_DISCOVERY */
  132. _UA_END_DECLS
  133. #endif /* UA_DISCOVERY_MANAGER_H_ */