ua_session.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "ua_util.h"
  2. #include "ua_session.h"
  3. #include "ua_statuscodes.h"
  4. UA_Session anonymousSession = {
  5. .clientDescription = {.applicationUri = {-1, UA_NULL}, .productUri = {-1, UA_NULL},
  6. .applicationName = {.locale = {-1, UA_NULL}, .text = {-1, UA_NULL}},
  7. .applicationType = UA_APPLICATIONTYPE_CLIENT,
  8. .gatewayServerUri = {-1, UA_NULL}, .discoveryProfileUri = {-1, UA_NULL},
  9. .discoveryUrlsSize = -1, .discoveryUrls = UA_NULL},
  10. .sessionName = {sizeof("Anonymous Session")-1, (UA_Byte*)"Anonymous Session"},
  11. .authenticationToken = {.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC,
  12. .identifier.numeric = 0},
  13. .sessionId = {.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = 0},
  14. .maxRequestMessageSize = UA_UINT32_MAX, .maxResponseMessageSize = UA_UINT32_MAX,
  15. .timeout = UA_INT64_MAX, .validTill = UA_INT64_MAX, .channel = UA_NULL,
  16. .continuationPoints = {UA_NULL}};
  17. UA_Session adminSession = {
  18. .clientDescription = {.applicationUri = {-1, UA_NULL}, .productUri = {-1, UA_NULL},
  19. .applicationName = {.locale = {-1, UA_NULL}, .text = {-1, UA_NULL}},
  20. .applicationType = UA_APPLICATIONTYPE_CLIENT,
  21. .gatewayServerUri = {-1, UA_NULL}, .discoveryProfileUri = {-1, UA_NULL},
  22. .discoveryUrlsSize = -1, .discoveryUrls = UA_NULL},
  23. .sessionName = {sizeof("Administrator Session")-1, (UA_Byte*)"Administrator Session"},
  24. .authenticationToken = {.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC,
  25. .identifier.numeric = 1},
  26. .sessionId = {.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = 1},
  27. .maxRequestMessageSize = UA_UINT32_MAX, .maxResponseMessageSize = UA_UINT32_MAX,
  28. .timeout = UA_INT64_MAX, .validTill = UA_INT64_MAX, .channel = UA_NULL,
  29. .continuationPoints = {UA_NULL}};
  30. void UA_Session_init(UA_Session *session) {
  31. UA_ApplicationDescription_init(&session->clientDescription);
  32. session->activated = UA_FALSE;
  33. UA_NodeId_init(&session->authenticationToken);
  34. UA_NodeId_init(&session->sessionId);
  35. UA_String_init(&session->sessionName);
  36. session->maxRequestMessageSize = 0;
  37. session->maxResponseMessageSize = 0;
  38. session->timeout = 0;
  39. UA_DateTime_init(&session->validTill);
  40. session->channel = UA_NULL;
  41. session->continuationPoints = (struct ContinuationPointList){UA_NULL};
  42. }
  43. void UA_Session_deleteMembers(UA_Session *session) {
  44. UA_ApplicationDescription_deleteMembers(&session->clientDescription);
  45. UA_NodeId_deleteMembers(&session->authenticationToken);
  46. UA_NodeId_deleteMembers(&session->sessionId);
  47. UA_String_deleteMembers(&session->sessionName);
  48. session->channel = UA_NULL;
  49. struct ContinuationPointEntry *cp;
  50. while((cp = LIST_FIRST(&session->continuationPoints))) {
  51. UA_ByteString_deleteMembers(&cp->identifier);
  52. UA_BrowseDescription_deleteMembers(&cp->browseDescription);
  53. LIST_REMOVE(cp, pointers);
  54. UA_free(cp);
  55. }
  56. }
  57. void UA_Session_updateLifetime(UA_Session *session) {
  58. session->validTill = UA_DateTime_now() + session->timeout * 10000; //timeout in ms
  59. }
  60. void UA_Session_detachSecureChannel(UA_Session *session) {
  61. #ifdef UA_MULTITHREADING
  62. UA_SecureChannel *channel = session->channel;
  63. if(channel)
  64. uatomic_cmpxchg(&channel->session, session, UA_NULL);
  65. uatomic_set(&session->channel, UA_NULL);
  66. #else
  67. if(session->channel)
  68. session->channel->session = UA_NULL;
  69. session->channel = UA_NULL;
  70. #endif
  71. }
  72. /* these functions are here, since they need to include ua_session.h */
  73. void UA_SecureChannel_attachSession(UA_SecureChannel *channel, UA_Session *session) {
  74. #ifdef UA_MULTITHREADING
  75. if(uatomic_cmpxchg(&session->channel, UA_NULL, channel) == UA_NULL)
  76. uatomic_set(&channel->session, session);
  77. #else
  78. if(session->channel != UA_NULL)
  79. return;
  80. session->channel = channel;
  81. channel->session = session;
  82. #endif
  83. }
  84. void UA_SecureChannel_detachSession(UA_SecureChannel *channel) {
  85. #ifdef UA_MULTITHREADING
  86. UA_Session *session = channel->session;
  87. if(session)
  88. uatomic_cmpxchg(&session->channel, channel, UA_NULL);
  89. uatomic_set(&channel->session, UA_NULL);
  90. #else
  91. if(channel->session)
  92. channel->session->channel = UA_NULL;
  93. channel->session = UA_NULL;
  94. #endif
  95. }