ua_session.c 5.5 KB

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