ua_discovery_manager.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. dm->serverOnNetworkCallback = NULL;
  91. dm->serverOnNetworkCallbackData = NULL;
  92. #endif /* UA_ENABLE_DISCOVERY_MULTICAST */
  93. }
  94. void
  95. UA_DiscoveryManager_deleteMembers(UA_DiscoveryManager *dm, UA_Server *server) {
  96. registeredServer_list_entry *rs, *rs_tmp;
  97. LIST_FOREACH_SAFE(rs, &dm->registeredServers, pointers, rs_tmp) {
  98. LIST_REMOVE(rs, pointers);
  99. UA_RegisteredServer_deleteMembers(&rs->registeredServer);
  100. UA_free(rs);
  101. }
  102. periodicServerRegisterCallback_entry *ps, *ps_tmp;
  103. LIST_FOREACH_SAFE(ps, &dm->periodicServerRegisterCallbacks, pointers, ps_tmp) {
  104. LIST_REMOVE(ps, pointers);
  105. UA_free(ps->callback);
  106. UA_free(ps);
  107. }
  108. # ifdef UA_ENABLE_DISCOVERY_MULTICAST
  109. if(server->config.applicationDescription.applicationType == UA_APPLICATIONTYPE_DISCOVERYSERVER)
  110. destroyMulticastDiscoveryServer(dm);
  111. serverOnNetwork_list_entry *son, *son_tmp;
  112. LIST_FOREACH_SAFE(son, &dm->serverOnNetwork, pointers, son_tmp) {
  113. LIST_REMOVE(son, pointers);
  114. UA_ServerOnNetwork_deleteMembers(&son->serverOnNetwork);
  115. if(son->pathTmp)
  116. UA_free(son->pathTmp);
  117. UA_free(son);
  118. }
  119. for(size_t i = 0; i < SERVER_ON_NETWORK_HASH_PRIME; i++) {
  120. serverOnNetwork_hash_entry* currHash = dm->serverOnNetworkHash[i];
  121. while(currHash) {
  122. serverOnNetwork_hash_entry* nextHash = currHash->next;
  123. UA_free(currHash);
  124. currHash = nextHash;
  125. }
  126. }
  127. # endif /* UA_ENABLE_DISCOVERY_MULTICAST */
  128. }
  129. #endif /* UA_ENABLE_DISCOVERY */