ua_services_discovery.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "ua_server_internal.h"
  2. #include "ua_services.h"
  3. #include "ua_util.h"
  4. void Service_FindServers(UA_Server *server, UA_Session *session,
  5. const UA_FindServersRequest *request, UA_FindServersResponse *response) {
  6. /* copy ApplicationDescription from the config */
  7. UA_ApplicationDescription *descr = UA_malloc(sizeof(UA_ApplicationDescription));
  8. if(!descr) {
  9. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  10. return;
  11. }
  12. response->responseHeader.serviceResult =
  13. UA_ApplicationDescription_copy(&server->config.applicationDescription, descr);
  14. if(response->responseHeader.serviceResult != UA_STATUSCODE_GOOD) {
  15. UA_free(descr);
  16. return;
  17. }
  18. /* add the discoveryUrls from the networklayers */
  19. UA_String *disc = UA_realloc(descr->discoveryUrls, sizeof(UA_String) *
  20. (descr->discoveryUrlsSize + server->config.networkLayersSize));
  21. if(!disc) {
  22. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  23. UA_ApplicationDescription_delete(descr);
  24. return;
  25. }
  26. size_t index = descr->discoveryUrlsSize;
  27. descr->discoveryUrls = disc;
  28. descr->discoveryUrlsSize += server->config.networkLayersSize;
  29. // TODO: Add nl only if discoveryUrl not already present
  30. for(size_t i = 0; i < server->config.networkLayersSize; i++) {
  31. UA_ServerNetworkLayer *nl = &server->config.networkLayers[i];
  32. UA_String_copy(&nl->discoveryUrl, &descr->discoveryUrls[index + i]);
  33. }
  34. response->servers = descr;
  35. response->serversSize = 1;
  36. }
  37. void Service_GetEndpoints(UA_Server *server, UA_Session *session, const UA_GetEndpointsRequest *request,
  38. UA_GetEndpointsResponse *response) {
  39. /* Test if one of the networklayers exposes the discoveryUrl of the requested endpoint */
  40. /* Disabled, servers in a virtualbox don't know their external hostname */
  41. /* UA_Boolean foundUri = UA_FALSE; */
  42. /* for(size_t i = 0; i < server->config.networkLayersSize; i++) { */
  43. /* if(UA_String_equal(&request->endpointUrl, &server->config.networkLayers[i].discoveryUrl)) { */
  44. /* foundUri = UA_TRUE; */
  45. /* break; */
  46. /* } */
  47. /* } */
  48. /* if(!foundUri) { */
  49. /* response->endpointsSize = 0; */
  50. /* return; */
  51. /* } */
  52. /* test if the supported binary profile shall be returned */
  53. #ifdef NO_ALLOCA
  54. UA_Boolean relevant_endpoints[server->endpointDescriptionsSize];
  55. #else
  56. UA_Boolean *relevant_endpoints = UA_alloca(sizeof(UA_Byte) * server->endpointDescriptionsSize);
  57. #endif
  58. size_t relevant_count = 0;
  59. for(size_t j = 0; j < server->endpointDescriptionsSize; j++) {
  60. relevant_endpoints[j] = UA_FALSE;
  61. if(request->profileUrisSize == 0) {
  62. relevant_endpoints[j] = UA_TRUE;
  63. relevant_count++;
  64. continue;
  65. }
  66. for(size_t i = 0; i < request->profileUrisSize; i++) {
  67. if(UA_String_equal(&request->profileUris[i], &server->endpointDescriptions->transportProfileUri)) {
  68. relevant_endpoints[j] = UA_TRUE;
  69. relevant_count++;
  70. break;
  71. }
  72. }
  73. }
  74. if(relevant_count == 0) {
  75. response->endpointsSize = 0;
  76. return;
  77. }
  78. response->endpoints = UA_malloc(sizeof(UA_EndpointDescription) * relevant_count);
  79. if(!response->endpoints) {
  80. response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
  81. return;
  82. }
  83. size_t k = 0;
  84. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  85. for(size_t j = 0; j < server->endpointDescriptionsSize && retval == UA_STATUSCODE_GOOD; j++) {
  86. if(!relevant_endpoints[j])
  87. continue;
  88. retval = UA_EndpointDescription_copy(&server->endpointDescriptions[j], &response->endpoints[k]);
  89. UA_String_deleteMembers(&response->endpoints[k].endpointUrl);
  90. retval |= UA_String_copy(&request->endpointUrl, &response->endpoints[k].endpointUrl);
  91. k++;
  92. }
  93. if(retval != UA_STATUSCODE_GOOD) {
  94. response->responseHeader.serviceResult = retval;
  95. UA_Array_delete(response->endpoints, --k, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  96. return;
  97. }
  98. response->endpointsSize = relevant_count;
  99. }