ua_discovery_manager.c 6.1 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. #include "ua_server_internal.h"
  14. #ifdef UA_ENABLE_DISCOVERY
  15. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  16. /* Create multicast 224.0.0.251:5353 socket */
  17. static UA_SOCKET
  18. discovery_createMulticastSocket(void) {
  19. UA_SOCKET s;
  20. int flag = 1, ittl = 255;
  21. struct sockaddr_in in;
  22. struct ip_mreq mc;
  23. char ttl = (char)255; // publish to complete net, not only subnet. See:
  24. // https://docs.oracle.com/cd/E23824_01/html/821-1602/sockets-137.html
  25. memset(&in, 0, sizeof(in));
  26. in.sin_family = AF_INET;
  27. in.sin_port = htons(5353);
  28. in.sin_addr.s_addr = 0;
  29. if((s = UA_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == UA_INVALID_SOCKET)
  30. return UA_INVALID_SOCKET;
  31. #ifdef SO_REUSEPORT
  32. UA_setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (char *)&flag, sizeof(flag));
  33. #endif
  34. UA_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof(flag));
  35. if(UA_bind(s, (struct sockaddr *)&in, sizeof(in))) {
  36. UA_close(s);
  37. return UA_INVALID_SOCKET;
  38. }
  39. mc.imr_multiaddr.s_addr = inet_addr("224.0.0.251");
  40. mc.imr_interface.s_addr = htonl(INADDR_ANY);
  41. UA_setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mc, sizeof(mc));
  42. UA_setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ttl, sizeof(ttl));
  43. UA_setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ittl, sizeof(ittl));
  44. UA_socket_set_nonblocking(s); //TODO: check return value
  45. return s;
  46. }
  47. static UA_StatusCode
  48. initMulticastDiscoveryServer(UA_DiscoveryManager *dm, UA_Server* server) {
  49. server->discoveryManager.mdnsDaemon = mdnsd_new(QCLASS_IN, 1000);
  50. UA_initialize_architecture_network();
  51. if((server->discoveryManager.mdnsSocket = discovery_createMulticastSocket()) == UA_INVALID_SOCKET) {
  52. UA_LOG_SOCKET_ERRNO_WRAP(
  53. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  54. "Could not create multicast socket. Error: %s", errno_str));
  55. return UA_STATUSCODE_BADUNEXPECTEDERROR;
  56. }
  57. mdnsd_register_receive_callback(server->discoveryManager.mdnsDaemon,
  58. mdns_record_received, server);
  59. return UA_STATUSCODE_GOOD;
  60. }
  61. static void
  62. destroyMulticastDiscoveryServer(UA_DiscoveryManager *dm) {
  63. mdnsd_shutdown(dm->mdnsDaemon);
  64. mdnsd_free(dm->mdnsDaemon);
  65. if(dm->mdnsSocket != UA_INVALID_SOCKET) {
  66. UA_close(dm->mdnsSocket);
  67. dm->mdnsSocket = UA_INVALID_SOCKET;
  68. }
  69. }
  70. #endif /* UA_ENABLE_DISCOVERY_MULTICAST */
  71. void
  72. UA_DiscoveryManager_init(UA_DiscoveryManager *dm, UA_Server *server) {
  73. LIST_INIT(&dm->registeredServers);
  74. dm->registeredServersSize = 0;
  75. LIST_INIT(&dm->periodicServerRegisterCallbacks);
  76. dm->registerServerCallback = NULL;
  77. dm->registerServerCallbackData = NULL;
  78. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  79. dm->mdnsDaemon = NULL;
  80. dm->mdnsSocket = UA_INVALID_SOCKET;
  81. dm->mdnsMainSrvAdded = false;
  82. if(server->config.applicationDescription.applicationType == UA_APPLICATIONTYPE_DISCOVERYSERVER)
  83. initMulticastDiscoveryServer(dm, server);
  84. LIST_INIT(&dm->serverOnNetwork);
  85. dm->serverOnNetworkSize = 0;
  86. dm->serverOnNetworkRecordIdCounter = 0;
  87. dm->serverOnNetworkRecordIdLastReset = UA_DateTime_now();
  88. memset(dm->serverOnNetworkHash, 0,
  89. sizeof(struct serverOnNetwork_hash_entry*) * SERVER_ON_NETWORK_HASH_PRIME);
  90. LIST_INIT(&dm->mdnsHostnameToIp);
  91. memset(dm->mdnsHostnameToIpHash, 0,
  92. sizeof(struct mdnsHostnameToIp_hash_entry*) * MDNS_HOSTNAME_TO_IP_HASH_PRIME);
  93. dm->serverOnNetworkCallback = NULL;
  94. dm->serverOnNetworkCallbackData = NULL;
  95. #endif /* UA_ENABLE_DISCOVERY_MULTICAST */
  96. }
  97. void
  98. UA_DiscoveryManager_deleteMembers(UA_DiscoveryManager *dm, UA_Server *server) {
  99. registeredServer_list_entry *rs, *rs_tmp;
  100. LIST_FOREACH_SAFE(rs, &dm->registeredServers, pointers, rs_tmp) {
  101. LIST_REMOVE(rs, pointers);
  102. UA_RegisteredServer_deleteMembers(&rs->registeredServer);
  103. UA_free(rs);
  104. }
  105. periodicServerRegisterCallback_entry *ps, *ps_tmp;
  106. LIST_FOREACH_SAFE(ps, &dm->periodicServerRegisterCallbacks, pointers, ps_tmp) {
  107. LIST_REMOVE(ps, pointers);
  108. UA_free(ps->callback);
  109. UA_free(ps);
  110. }
  111. # ifdef UA_ENABLE_DISCOVERY_MULTICAST
  112. if(server->config.applicationDescription.applicationType == UA_APPLICATIONTYPE_DISCOVERYSERVER)
  113. destroyMulticastDiscoveryServer(dm);
  114. serverOnNetwork_list_entry *son, *son_tmp;
  115. LIST_FOREACH_SAFE(son, &dm->serverOnNetwork, pointers, son_tmp) {
  116. LIST_REMOVE(son, pointers);
  117. UA_ServerOnNetwork_deleteMembers(&son->serverOnNetwork);
  118. if(son->pathTmp)
  119. UA_free(son->pathTmp);
  120. UA_free(son);
  121. }
  122. for(size_t i = 0; i < SERVER_ON_NETWORK_HASH_PRIME; i++) {
  123. serverOnNetwork_hash_entry* currHash = dm->serverOnNetworkHash[i];
  124. while(currHash) {
  125. serverOnNetwork_hash_entry* nextHash = currHash->next;
  126. UA_free(currHash);
  127. currHash = nextHash;
  128. }
  129. }
  130. mdnsHostnameToIp_list_entry *mhi, *mhi_tmp;
  131. LIST_FOREACH_SAFE(mhi, &dm->mdnsHostnameToIp, pointers, mhi_tmp) {
  132. LIST_REMOVE(mhi, pointers);
  133. UA_String_deleteMembers(&mhi->mdnsHostname);
  134. UA_free(mhi);
  135. }
  136. for(size_t i = 0; i < MDNS_HOSTNAME_TO_IP_HASH_PRIME; i++) {
  137. mdnsHostnameToIp_hash_entry* currHash = dm->mdnsHostnameToIpHash[i];
  138. while(currHash) {
  139. mdnsHostnameToIp_hash_entry* nextHash = currHash->next;
  140. UA_free(currHash);
  141. currHash = nextHash;
  142. }
  143. }
  144. # endif /* UA_ENABLE_DISCOVERY_MULTICAST */
  145. }
  146. #endif /* UA_ENABLE_DISCOVERY */