ua_session.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /* TODO: Nobody seems to call this function right now */
  31. static UA_StatusCode UA_Session_generateToken(UA_NodeId *newToken, UA_UInt32 *seed) {
  32. newToken->namespaceIndex = 0; // where else?
  33. newToken->identifierType = UA_NODEIDTYPE_GUID;
  34. newToken->identifier.guid = UA_Guid_random(seed);
  35. return UA_STATUSCODE_GOOD;
  36. }
  37. void UA_Session_init(UA_Session *session) {
  38. UA_ApplicationDescription_init(&session->clientDescription);
  39. UA_NodeId_init(&session->authenticationToken);
  40. UA_NodeId_init(&session->sessionId);
  41. UA_String_init(&session->sessionName);
  42. session->maxRequestMessageSize = 0;
  43. session->maxResponseMessageSize = 0;
  44. session->timeout = 0;
  45. UA_DateTime_init(&session->validTill);
  46. session->channel = UA_NULL;
  47. session->continuationPoints = (struct ContinuationPointList){UA_NULL};
  48. }
  49. void UA_Session_deleteMembers(UA_Session *session) {
  50. UA_ApplicationDescription_deleteMembers(&session->clientDescription);
  51. UA_NodeId_deleteMembers(&session->authenticationToken);
  52. UA_NodeId_deleteMembers(&session->sessionId);
  53. UA_String_deleteMembers(&session->sessionName);
  54. session->channel = UA_NULL;
  55. struct ContinuationPointEntry *cp;
  56. while((cp = LIST_FIRST(&session->continuationPoints))) {
  57. UA_ByteString_deleteMembers(&cp->identifier);
  58. UA_BrowseDescription_deleteMembers(&cp->browseDescription);
  59. LIST_REMOVE(cp, pointers);
  60. UA_free(cp);
  61. }
  62. }
  63. UA_StatusCode UA_Session_setExpirationDate(UA_Session *session) {
  64. if(!session)
  65. return UA_STATUSCODE_BADINTERNALERROR;
  66. session->validTill = UA_DateTime_now() + session->timeout * 100000; //timeout in ms
  67. return UA_STATUSCODE_GOOD;
  68. }
  69. UA_StatusCode UA_Session_getPendingLifetime(UA_Session *session, UA_Double *pendingLifetime_ms) {
  70. if(!session)
  71. return UA_STATUSCODE_BADINTERNALERROR;
  72. *pendingLifetime_ms = (session->validTill - UA_DateTime_now())/10000000; //difference in ms
  73. return UA_STATUSCODE_GOOD;
  74. }
  75. void UA_Session_detachSecureChannel(UA_Session *session) {
  76. #ifdef UA_MULTITHREADING
  77. UA_SecureChannel *channel = session->channel;
  78. if(channel)
  79. uatomic_cmpxchg(&channel->session, session, UA_NULL);
  80. uatomic_set(&session->channel, UA_NULL);
  81. #else
  82. if(session->channel)
  83. session->channel->session = UA_NULL;
  84. session->channel = UA_NULL;
  85. #endif
  86. }
  87. /* these functions are here, since they need to include ua_session.h */
  88. void UA_SecureChannel_attachSession(UA_SecureChannel *channel, UA_Session *session) {
  89. #ifdef UA_MULTITHREADING
  90. if(uatomic_cmpxchg(&session->channel, UA_NULL, channel) == UA_NULL)
  91. uatomic_set(&channel->session, session);
  92. #else
  93. if(session->channel != UA_NULL)
  94. return;
  95. session->channel = channel;
  96. channel->session = session;
  97. #endif
  98. }
  99. void UA_SecureChannel_detachSession(UA_SecureChannel *channel) {
  100. #ifdef UA_MULTITHREADING
  101. UA_Session *session = channel->session;
  102. if(session)
  103. uatomic_cmpxchg(&session->channel, channel, UA_NULL);
  104. uatomic_set(&channel->session, UA_NULL);
  105. #else
  106. if(channel->session)
  107. channel->session->channel = UA_NULL;
  108. channel->session = UA_NULL;
  109. #endif
  110. }