ua_services_discovery_multicast.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 2017 (c) Stefan Profanter, fortiss GmbH
  6. * Copyright 2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  7. * Copyright 2017 (c) Thomas Stalder, Blue Time Concept SA
  8. */
  9. #include "ua_server_internal.h"
  10. #include "ua_services.h"
  11. #if defined(UA_ENABLE_DISCOVERY) && defined(UA_ENABLE_DISCOVERY_MULTICAST)
  12. #ifdef UA_ENABLE_MULTITHREADING
  13. static void *
  14. multicastWorkerLoop(UA_Server *server) {
  15. struct timeval next_sleep = {.tv_sec = 0, .tv_usec = 0};
  16. volatile UA_Boolean *running = &server->discoveryManager.mdnsRunning;
  17. fd_set fds;
  18. while(*running) {
  19. FD_ZERO(&fds);
  20. UA_fd_set(server->discoveryManager.mdnsSocket, &fds);
  21. select(server->discoveryManager.mdnsSocket + 1, &fds, 0, 0, &next_sleep);
  22. if(!*running)
  23. break;
  24. unsigned short retVal =
  25. mdnsd_step(server->discoveryManager.mdnsDaemon, server->discoveryManager.mdnsSocket,
  26. FD_ISSET(server->discoveryManager.mdnsSocket, &fds), true, &next_sleep);
  27. if(retVal == 1) {
  28. UA_LOG_SOCKET_ERRNO_WRAP(
  29. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  30. "Multicast error: Can not read from socket. %s", errno_str));
  31. break;
  32. } else if (retVal == 2) {
  33. UA_LOG_SOCKET_ERRNO_WRAP(
  34. UA_LOG_DEBUG(&server->config.logger, UA_LOGCATEGORY_SERVER,
  35. "Multicast error: Can not write to socket. %s", errno_str));
  36. break;
  37. }
  38. }
  39. return NULL;
  40. }
  41. static UA_StatusCode
  42. multicastListenStart(UA_Server* server) {
  43. int err = pthread_create(&server->discoveryManager.mdnsThread, NULL,
  44. (void* (*)(void*))multicastWorkerLoop, server);
  45. if(err != 0) {
  46. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  47. "Multicast error: Can not create multicast thread.");
  48. return UA_STATUSCODE_BADUNEXPECTEDERROR;
  49. }
  50. return UA_STATUSCODE_GOOD;
  51. }
  52. static UA_StatusCode
  53. multicastListenStop(UA_Server* server) {
  54. mdnsd_shutdown(server->discoveryManager.mdnsDaemon);
  55. // wake up select
  56. if(write(server->discoveryManager.mdnsSocket, "\0", 1)){}; //TODO: move to arch?
  57. if(pthread_join(server->discoveryManager.mdnsThread, NULL)) {
  58. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  59. "Multicast error: Can not stop thread.");
  60. return UA_STATUSCODE_BADUNEXPECTEDERROR;
  61. }
  62. return UA_STATUSCODE_BADNOTIMPLEMENTED;
  63. }
  64. # endif /* UA_ENABLE_MULTITHREADING */
  65. static UA_StatusCode
  66. addMdnsRecordForNetworkLayer(UA_Server *server, const UA_String *appName,
  67. const UA_ServerNetworkLayer* nl) {
  68. UA_String hostname = UA_STRING_NULL;
  69. UA_UInt16 port = 4840;
  70. UA_String path = UA_STRING_NULL;
  71. UA_StatusCode retval = UA_parseEndpointUrl(&nl->discoveryUrl, &hostname,
  72. &port, &path);
  73. if(retval != UA_STATUSCODE_GOOD) {
  74. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_NETWORK,
  75. "Server url is invalid: %.*s",
  76. (int)nl->discoveryUrl.length, nl->discoveryUrl.data);
  77. return retval;
  78. }
  79. UA_Discovery_addRecord(server, appName, &hostname, port,
  80. &path, UA_DISCOVERY_TCP, true,
  81. server->config.serverCapabilities,
  82. &server->config.serverCapabilitiesSize);
  83. return UA_STATUSCODE_GOOD;
  84. }
  85. void startMulticastDiscoveryServer(UA_Server *server) {
  86. UA_String *appName = &server->config.mdnsServerName;
  87. for(size_t i = 0; i < server->config.networkLayersSize; i++)
  88. addMdnsRecordForNetworkLayer(server, appName, &server->config.networkLayers[i]);
  89. /* find any other server on the net */
  90. UA_Discovery_multicastQuery(server);
  91. # ifdef UA_ENABLE_MULTITHREADING
  92. multicastListenStart(server);
  93. # endif
  94. }
  95. void
  96. stopMulticastDiscoveryServer(UA_Server *server) {
  97. char hostname[256];
  98. if(UA_gethostname(hostname, 255) == 0) {
  99. UA_String hnString = UA_STRING(hostname);
  100. UA_Discovery_removeRecord(server, &server->config.mdnsServerName,
  101. &hnString, 4840, true);
  102. } else {
  103. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  104. "Could not get hostname for multicast discovery.");
  105. }
  106. # ifdef UA_ENABLE_MULTITHREADING
  107. multicastListenStop(server);
  108. # else
  109. // send out last package with TTL = 0
  110. iterateMulticastDiscoveryServer(server, NULL, false);
  111. # endif
  112. }
  113. /* All filter criteria must be fulfilled */
  114. static UA_Boolean
  115. filterServerRecord(size_t serverCapabilityFilterSize, UA_String *serverCapabilityFilter,
  116. serverOnNetwork_list_entry* current) {
  117. for(size_t i = 0; i < serverCapabilityFilterSize; i++) {
  118. for(size_t j = 0; j < current->serverOnNetwork.serverCapabilitiesSize; j++)
  119. if(!UA_String_equal(&serverCapabilityFilter[i],
  120. &current->serverOnNetwork.serverCapabilities[j]))
  121. return false;
  122. }
  123. return true;
  124. }
  125. void Service_FindServersOnNetwork(UA_Server *server, UA_Session *session,
  126. const UA_FindServersOnNetworkRequest *request,
  127. UA_FindServersOnNetworkResponse *response) {
  128. /* Set LastCounterResetTime */
  129. UA_DateTime_copy(&server->discoveryManager.serverOnNetworkRecordIdLastReset,
  130. &response->lastCounterResetTime);
  131. /* Compute the max number of records to return */
  132. UA_UInt32 recordCount = 0;
  133. if(request->startingRecordId < server->discoveryManager.serverOnNetworkRecordIdCounter)
  134. recordCount = server->discoveryManager.serverOnNetworkRecordIdCounter -
  135. request->startingRecordId;
  136. if(request->maxRecordsToReturn && recordCount > request->maxRecordsToReturn)
  137. recordCount = UA_MIN(recordCount, request->maxRecordsToReturn);
  138. if(recordCount == 0) {
  139. response->serversSize = 0;
  140. return;
  141. }
  142. /* Iterate over all records and add to filtered list */
  143. UA_UInt32 filteredCount = 0;
  144. UA_STACKARRAY(UA_ServerOnNetwork*, filtered, recordCount);
  145. serverOnNetwork_list_entry* current;
  146. LIST_FOREACH(current, &server->discoveryManager.serverOnNetwork, pointers) {
  147. if(filteredCount >= recordCount)
  148. break;
  149. if(current->serverOnNetwork.recordId < request->startingRecordId)
  150. continue;
  151. if(!filterServerRecord(request->serverCapabilityFilterSize,
  152. request->serverCapabilityFilter, current))
  153. continue;
  154. filtered[filteredCount++] = &current->serverOnNetwork;
  155. }
  156. if(filteredCount == 0)
  157. return;
  158. /* Allocate the array for the response */
  159. response->servers =
  160. (UA_ServerOnNetwork*)UA_malloc(sizeof(UA_ServerOnNetwork)*filteredCount);
  161. if(!response->servers) {
  162. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  163. return;
  164. }
  165. response->serversSize = filteredCount;
  166. /* Copy the server names */
  167. for(size_t i = 0; i < filteredCount; i++)
  168. UA_ServerOnNetwork_copy(filtered[i], &response->servers[filteredCount-i-1]);
  169. }
  170. void
  171. UA_Server_updateMdnsForDiscoveryUrl(UA_Server *server, const UA_String *serverName,
  172. const UA_MdnsDiscoveryConfiguration *mdnsConfig,
  173. const UA_String *discoveryUrl,
  174. UA_Boolean isOnline, UA_Boolean updateTxt) {
  175. UA_String hostname = UA_STRING_NULL;
  176. UA_UInt16 port = 4840;
  177. UA_String path = UA_STRING_NULL;
  178. UA_StatusCode retval = UA_parseEndpointUrl(discoveryUrl, &hostname, &port, &path);
  179. if(retval != UA_STATUSCODE_GOOD) {
  180. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_NETWORK,
  181. "Server url invalid: %.*s",
  182. (int)discoveryUrl->length, discoveryUrl->data);
  183. return;
  184. }
  185. if(!isOnline) {
  186. UA_StatusCode removeRetval =
  187. UA_Discovery_removeRecord(server, serverName, &hostname,
  188. port, updateTxt);
  189. if(removeRetval != UA_STATUSCODE_GOOD)
  190. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER,
  191. "Could not remove mDNS record for hostname %.*s.",
  192. (int)serverName->length, serverName->data);
  193. return;
  194. }
  195. UA_String *capabilities = NULL;
  196. size_t capabilitiesSize = 0;
  197. if(mdnsConfig) {
  198. capabilities = mdnsConfig->serverCapabilities;
  199. capabilitiesSize = mdnsConfig->serverCapabilitiesSize;
  200. }
  201. UA_StatusCode addRetval =
  202. UA_Discovery_addRecord(server, serverName, &hostname,
  203. port, &path, UA_DISCOVERY_TCP, updateTxt,
  204. capabilities, &capabilitiesSize);
  205. if(addRetval != UA_STATUSCODE_GOOD)
  206. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER,
  207. "Could not add mDNS record for hostname %.*s.",
  208. (int)serverName->length, serverName->data);
  209. }
  210. void
  211. UA_Server_setServerOnNetworkCallback(UA_Server *server,
  212. UA_Server_serverOnNetworkCallback cb,
  213. void* data) {
  214. server->discoveryManager.serverOnNetworkCallback = cb;
  215. server->discoveryManager.serverOnNetworkCallbackData = data;
  216. }
  217. static void
  218. UA_Discovery_multicastConflict(char *name, int type, void *arg) {
  219. // cppcheck-suppress unreadVariable
  220. UA_Server *server = (UA_Server*) arg;
  221. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,
  222. "Multicast DNS name conflict detected: "
  223. "'%s' for type %d", name, type);
  224. }
  225. /* Create a service domain with the format [servername]-[hostname]._opcua-tcp._tcp.local. */
  226. static void
  227. createFullServiceDomain(char *outServiceDomain, size_t maxLen,
  228. const UA_String *servername, const UA_String *hostname) {
  229. size_t hostnameLen = hostname->length;
  230. size_t servernameLen = servername->length;
  231. maxLen -= 24; /* the length we have remaining before the opc ua postfix and
  232. * the trailing zero */
  233. /* Can we use hostname and servername with full length? */
  234. if(hostnameLen + servernameLen + 1 > maxLen) {
  235. if(servernameLen + 2 > maxLen) {
  236. servernameLen = maxLen;
  237. hostnameLen = 0;
  238. } else {
  239. hostnameLen = maxLen - servernameLen - 1;
  240. }
  241. }
  242. size_t offset = 0;
  243. if (hostnameLen > 0) {
  244. UA_snprintf(outServiceDomain, maxLen + 1, "%.*s-%.*s",
  245. (int) servernameLen, (char *) servername->data,
  246. (int) hostnameLen, (char *) hostname->data);
  247. offset = servernameLen + hostnameLen + 1;
  248. }
  249. else {
  250. UA_snprintf(outServiceDomain, maxLen + 1, "%.*s",
  251. (int) servernameLen, (char *) servername->data);
  252. offset = servernameLen;
  253. }
  254. UA_snprintf(&outServiceDomain[offset], 24, "._opcua-tcp._tcp.local.");
  255. }
  256. /* Check if mDNS already has an entry for given hostname and port combination */
  257. static UA_Boolean
  258. UA_Discovery_recordExists(UA_Server* server, const char* fullServiceDomain,
  259. unsigned short port, const UA_DiscoveryProtocol protocol) {
  260. // [servername]-[hostname]._opcua-tcp._tcp.local. 86400 IN SRV 0 5 port [hostname].
  261. mdns_record_t *r = mdnsd_get_published(server->discoveryManager.mdnsDaemon, fullServiceDomain);
  262. while(r) {
  263. const mdns_answer_t *data = mdnsd_record_data(r);
  264. if(data->type == QTYPE_SRV && (port == 0 || data->srv.port == port))
  265. return true;
  266. r = mdnsd_record_next(r);
  267. }
  268. return false;
  269. }
  270. static int
  271. discovery_multicastQueryAnswer(mdns_answer_t *a, void *arg) {
  272. UA_Server *server = (UA_Server*) arg;
  273. if(a->type != QTYPE_PTR)
  274. return 0;
  275. if(a->rdname == NULL)
  276. return 0;
  277. /* Skip, if we already know about this server */
  278. UA_Boolean exists =
  279. UA_Discovery_recordExists(server, a->rdname, 0, UA_DISCOVERY_TCP);
  280. if(exists == true)
  281. return 0;
  282. if(mdnsd_has_query(server->discoveryManager.mdnsDaemon, a->rdname))
  283. return 0;
  284. UA_LOG_DEBUG(&server->config.logger, UA_LOGCATEGORY_SERVER,
  285. "mDNS send query for: %s SRV&TXT %s", a->name, a->rdname);
  286. mdnsd_query(server->discoveryManager.mdnsDaemon, a->rdname, QTYPE_SRV,
  287. discovery_multicastQueryAnswer, server);
  288. mdnsd_query(server->discoveryManager.mdnsDaemon, a->rdname, QTYPE_TXT,
  289. discovery_multicastQueryAnswer, server);
  290. return 0;
  291. }
  292. UA_StatusCode
  293. UA_Discovery_multicastQuery(UA_Server* server) {
  294. mdnsd_query(server->discoveryManager.mdnsDaemon, "_opcua-tcp._tcp.local.",
  295. QTYPE_PTR,discovery_multicastQueryAnswer, server);
  296. return UA_STATUSCODE_GOOD;
  297. }
  298. UA_StatusCode
  299. UA_Discovery_addRecord(UA_Server *server, const UA_String *servername,
  300. const UA_String *hostname, UA_UInt16 port,
  301. const UA_String *path, const UA_DiscoveryProtocol protocol,
  302. UA_Boolean createTxt, const UA_String* capabilites,
  303. size_t *capabilitiesSize) {
  304. if(!capabilitiesSize || (*capabilitiesSize > 0 && !capabilites))
  305. return UA_STATUSCODE_BADINVALIDARGUMENT;
  306. size_t hostnameLen = hostname->length;
  307. size_t servernameLen = servername->length;
  308. if(hostnameLen == 0 || servernameLen == 0)
  309. return UA_STATUSCODE_BADOUTOFRANGE;
  310. // use a limit for the hostname length to make sure full string fits into 63
  311. // chars (limited by DNS spec)
  312. if(hostnameLen+servernameLen + 1 > 63) { // include dash between servername-hostname
  313. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER,
  314. "Multicast DNS: Combination of hostname+servername exceeds "
  315. "maximum of 62 chars. It will be truncated.");
  316. } else if(hostnameLen > 63) {
  317. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER,
  318. "Multicast DNS: Hostname length exceeds maximum of 63 chars. "
  319. "It will be truncated.");
  320. }
  321. if(!server->discoveryManager.mdnsMainSrvAdded) {
  322. mdns_record_t *r =
  323. mdnsd_shared(server->discoveryManager.mdnsDaemon, "_services._dns-sd._udp.local.",
  324. QTYPE_PTR, 600);
  325. mdnsd_set_host(server->discoveryManager.mdnsDaemon, r, "_opcua-tcp._tcp.local.");
  326. server->discoveryManager.mdnsMainSrvAdded = true;
  327. }
  328. // [servername]-[hostname]._opcua-tcp._tcp.local.
  329. char fullServiceDomain[63+24];
  330. createFullServiceDomain(fullServiceDomain, 63+24, servername, hostname);
  331. UA_Boolean exists = UA_Discovery_recordExists(server, fullServiceDomain, port, protocol);
  332. if(exists == true)
  333. return UA_STATUSCODE_GOOD;
  334. UA_LOG_INFO(&server->config.logger, UA_LOGCATEGORY_SERVER,
  335. "Multicast DNS: add record for domain: %s", fullServiceDomain);
  336. // _services._dns-sd._udp.local. PTR _opcua-tcp._tcp.local
  337. // check if there is already a PTR entry for the given service.
  338. // _opcua-tcp._tcp.local. PTR [servername]-[hostname]._opcua-tcp._tcp.local.
  339. mdns_record_t *r = mdns_find_record(server->discoveryManager.mdnsDaemon, QTYPE_PTR,
  340. "_opcua-tcp._tcp.local.", fullServiceDomain);
  341. if(!r) {
  342. r = mdnsd_shared(server->discoveryManager.mdnsDaemon, "_opcua-tcp._tcp.local.", QTYPE_PTR, 600);
  343. mdnsd_set_host(server->discoveryManager.mdnsDaemon, r, fullServiceDomain);
  344. }
  345. /* The first 63 characters of the hostname (or less) */
  346. size_t maxHostnameLen = UA_MIN(hostnameLen, 63);
  347. char localDomain[65];
  348. memcpy(localDomain, hostname->data, maxHostnameLen);
  349. localDomain[maxHostnameLen] = '.';
  350. localDomain[maxHostnameLen+1] = '\0';
  351. // [servername]-[hostname]._opcua-tcp._tcp.local. 86400 IN SRV 0 5 port [hostname].
  352. r = mdnsd_unique(server->discoveryManager.mdnsDaemon, fullServiceDomain, QTYPE_SRV, 600,
  353. UA_Discovery_multicastConflict, server);
  354. mdnsd_set_srv(server->discoveryManager.mdnsDaemon, r, 0, 0, port, localDomain);
  355. // A/AAAA record for all ip addresses.
  356. // [servername]-[hostname]._opcua-tcp._tcp.local. A [ip].
  357. // [hostname]. A [ip].
  358. mdns_set_address_record(server, fullServiceDomain, localDomain);
  359. // TXT record: [servername]-[hostname]._opcua-tcp._tcp.local. TXT path=/ caps=NA,DA,...
  360. UA_STACKARRAY(char, pathChars, path->length + 1);
  361. if(createTxt) {
  362. memcpy(pathChars, path->data, path->length);
  363. pathChars[path->length] = 0;
  364. mdns_create_txt(server, fullServiceDomain, pathChars, capabilites,
  365. capabilitiesSize, UA_Discovery_multicastConflict);
  366. }
  367. return UA_STATUSCODE_GOOD;
  368. }
  369. UA_StatusCode
  370. UA_Discovery_removeRecord(UA_Server *server, const UA_String *servername,
  371. const UA_String *hostname, UA_UInt16 port,
  372. UA_Boolean removeTxt) {
  373. // use a limit for the hostname length to make sure full string fits into 63
  374. // chars (limited by DNS spec)
  375. size_t hostnameLen = hostname->length;
  376. size_t servernameLen = servername->length;
  377. if(hostnameLen == 0 || servernameLen == 0)
  378. return UA_STATUSCODE_BADOUTOFRANGE;
  379. if(hostnameLen+servernameLen+1 > 63) { // include dash between servername-hostname
  380. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER,
  381. "Multicast DNS: Combination of hostname+servername exceeds "
  382. "maximum of 62 chars. It will be truncated.");
  383. }
  384. // [servername]-[hostname]._opcua-tcp._tcp.local.
  385. char fullServiceDomain[63 + 24];
  386. createFullServiceDomain(fullServiceDomain, 63+24, servername, hostname);
  387. UA_LOG_INFO(&server->config.logger, UA_LOGCATEGORY_SERVER,
  388. "Multicast DNS: remove record for domain: %s", fullServiceDomain);
  389. // _opcua-tcp._tcp.local. PTR [servername]-[hostname]._opcua-tcp._tcp.local.
  390. mdns_record_t *r = mdns_find_record(server->discoveryManager.mdnsDaemon, QTYPE_PTR,
  391. "_opcua-tcp._tcp.local.", fullServiceDomain);
  392. if(!r) {
  393. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER,
  394. "Multicast DNS: could not remove record. "
  395. "PTR Record not found for domain: %s", fullServiceDomain);
  396. return UA_STATUSCODE_BADNOTHINGTODO;
  397. }
  398. mdnsd_done(server->discoveryManager.mdnsDaemon, r);
  399. // looks for [servername]-[hostname]._opcua-tcp._tcp.local. 86400 IN SRV 0 5 port hostname.local.
  400. // and TXT record: [servername]-[hostname]._opcua-tcp._tcp.local. TXT path=/ caps=NA,DA,...
  401. // and A record: [servername]-[hostname]._opcua-tcp._tcp.local. A [ip]
  402. mdns_record_t *r2 = mdnsd_get_published(server->discoveryManager.mdnsDaemon, fullServiceDomain);
  403. if(!r2) {
  404. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER,
  405. "Multicast DNS: could not remove record. Record not "
  406. "found for domain: %s", fullServiceDomain);
  407. return UA_STATUSCODE_BADNOTHINGTODO;
  408. }
  409. while(r2) {
  410. const mdns_answer_t *data = mdnsd_record_data(r2);
  411. mdns_record_t *next = mdnsd_record_next(r2);
  412. if((removeTxt && data->type == QTYPE_TXT) ||
  413. (removeTxt && data->type == QTYPE_A) ||
  414. data->srv.port == port) {
  415. mdnsd_done(server->discoveryManager.mdnsDaemon, r2);
  416. }
  417. r2 = next;
  418. }
  419. return UA_STATUSCODE_GOOD;
  420. }
  421. UA_StatusCode
  422. iterateMulticastDiscoveryServer(UA_Server* server, UA_DateTime *nextRepeat,
  423. UA_Boolean processIn) {
  424. struct timeval next_sleep = { 0, 0 };
  425. unsigned short retval = mdnsd_step(server->discoveryManager.mdnsDaemon,
  426. server->discoveryManager.mdnsSocket,
  427. processIn, true, &next_sleep);
  428. if(retval == 1) {
  429. UA_LOG_SOCKET_ERRNO_WRAP(
  430. UA_LOG_DEBUG(&server->config.logger, UA_LOGCATEGORY_SERVER,
  431. "Multicast error: Can not read from socket. %s", errno_str));
  432. return UA_STATUSCODE_BADNOCOMMUNICATION;
  433. } else if(retval == 2) {
  434. UA_LOG_SOCKET_ERRNO_WRAP(
  435. UA_LOG_DEBUG(&server->config.logger, UA_LOGCATEGORY_SERVER,
  436. "Multicast error: Can not write to socket. %s", errno_str));
  437. return UA_STATUSCODE_BADNOCOMMUNICATION;
  438. }
  439. if(nextRepeat)
  440. *nextRepeat = UA_DateTime_now() +
  441. (UA_DateTime)((next_sleep.tv_sec * UA_DATETIME_SEC) +
  442. (next_sleep.tv_usec * UA_DATETIME_USEC));
  443. return UA_STATUSCODE_GOOD;
  444. }
  445. #endif /* defined(UA_ENABLE_DISCOVERY) && defined(UA_ENABLE_DISCOVERY_MULTICAST) */