ua_server_discovery.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  7. */
  8. #include "ua_server_internal.h"
  9. #include "ua_client.h"
  10. #ifdef UA_ENABLE_DISCOVERY
  11. static UA_StatusCode
  12. register_server_with_discovery_server(UA_Server *server,
  13. UA_Client *client,
  14. const UA_Boolean isUnregister,
  15. const char* semaphoreFilePath) {
  16. /* Prepare the request. Do not cleanup the request after the service call,
  17. * as the members are stack-allocated or point into the server config. */
  18. UA_RegisterServer2Request request;
  19. UA_RegisterServer2Request_init(&request);
  20. request.requestHeader.timestamp = UA_DateTime_now();
  21. request.requestHeader.timeoutHint = 10000;
  22. request.server.isOnline = !isUnregister;
  23. request.server.serverUri = server->config.applicationDescription.applicationUri;
  24. request.server.productUri = server->config.applicationDescription.productUri;
  25. request.server.serverType = server->config.applicationDescription.applicationType;
  26. request.server.gatewayServerUri = server->config.applicationDescription.gatewayServerUri;
  27. if(semaphoreFilePath) {
  28. #ifdef UA_ENABLE_DISCOVERY_SEMAPHORE
  29. request.server.semaphoreFilePath =
  30. UA_STRING((char*)(uintptr_t)semaphoreFilePath); /* dirty cast */
  31. #else
  32. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_CLIENT,
  33. "Ignoring semaphore file path. open62541 not compiled "
  34. "with UA_ENABLE_DISCOVERY_SEMAPHORE=ON");
  35. #endif
  36. }
  37. request.server.serverNames = &server->config.applicationDescription.applicationName;
  38. request.server.serverNamesSize = 1;
  39. /* Copy the discovery urls from the server config and the network layers*/
  40. size_t config_discurls = server->config.applicationDescription.discoveryUrlsSize;
  41. size_t nl_discurls = server->config.networkLayersSize;
  42. size_t total_discurls = config_discurls + nl_discurls;
  43. UA_STACKARRAY(UA_String, urlsBuf, total_discurls);
  44. request.server.discoveryUrls = urlsBuf;
  45. request.server.discoveryUrlsSize = total_discurls;
  46. for(size_t i = 0; i < config_discurls; ++i)
  47. request.server.discoveryUrls[i] = server->config.applicationDescription.discoveryUrls[i];
  48. /* TODO: Add nl only if discoveryUrl not already present */
  49. for(size_t i = 0; i < nl_discurls; ++i) {
  50. UA_ServerNetworkLayer *nl = &server->config.networkLayers[i];
  51. request.server.discoveryUrls[config_discurls + i] = nl->discoveryUrl;
  52. }
  53. UA_MdnsDiscoveryConfiguration mdnsConfig;
  54. UA_MdnsDiscoveryConfiguration_init(&mdnsConfig);
  55. request.discoveryConfigurationSize = 1;
  56. request.discoveryConfiguration = UA_ExtensionObject_new();
  57. UA_ExtensionObject_init(&request.discoveryConfiguration[0]);
  58. request.discoveryConfiguration[0].encoding = UA_EXTENSIONOBJECT_DECODED_NODELETE;
  59. request.discoveryConfiguration[0].content.decoded.type = &UA_TYPES[UA_TYPES_MDNSDISCOVERYCONFIGURATION];
  60. request.discoveryConfiguration[0].content.decoded.data = &mdnsConfig;
  61. mdnsConfig.mdnsServerName = server->config.mdnsServerName;
  62. mdnsConfig.serverCapabilities = server->config.serverCapabilities;
  63. mdnsConfig.serverCapabilitiesSize = server->config.serverCapabilitiesSize;
  64. // First try with RegisterServer2, if that isn't implemented, use RegisterServer
  65. UA_RegisterServer2Response response;
  66. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_REGISTERSERVER2REQUEST],
  67. &response, &UA_TYPES[UA_TYPES_REGISTERSERVER2RESPONSE]);
  68. UA_StatusCode serviceResult = response.responseHeader.serviceResult;
  69. UA_RegisterServer2Response_deleteMembers(&response);
  70. UA_ExtensionObject_delete(request.discoveryConfiguration);
  71. if(serviceResult == UA_STATUSCODE_BADNOTIMPLEMENTED ||
  72. serviceResult == UA_STATUSCODE_BADSERVICEUNSUPPORTED) {
  73. /* Try RegisterServer */
  74. UA_RegisterServerRequest request_fallback;
  75. UA_RegisterServerRequest_init(&request_fallback);
  76. /* Copy from RegisterServer2 request */
  77. request_fallback.requestHeader = request.requestHeader;
  78. request_fallback.server = request.server;
  79. UA_RegisterServerResponse response_fallback;
  80. __UA_Client_Service(client, &request_fallback,
  81. &UA_TYPES[UA_TYPES_REGISTERSERVERREQUEST],
  82. &response_fallback,
  83. &UA_TYPES[UA_TYPES_REGISTERSERVERRESPONSE]);
  84. serviceResult = response_fallback.responseHeader.serviceResult;
  85. UA_RegisterServerResponse_deleteMembers(&response_fallback);
  86. }
  87. if(serviceResult != UA_STATUSCODE_GOOD) {
  88. UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_CLIENT,
  89. "RegisterServer/RegisterServer2 failed with statuscode %s",
  90. UA_StatusCode_name(serviceResult));
  91. }
  92. return serviceResult;
  93. }
  94. UA_StatusCode
  95. UA_Server_register_discovery(UA_Server *server, UA_Client *client,
  96. const char* semaphoreFilePath) {
  97. return register_server_with_discovery_server(server, client,
  98. false, semaphoreFilePath);
  99. }
  100. UA_StatusCode
  101. UA_Server_unregister_discovery(UA_Server *server, UA_Client *client) {
  102. return register_server_with_discovery_server(server, client,
  103. true, NULL);
  104. }
  105. #endif /* UA_ENABLE_DISCOVERY */