ua_stack_channel_manager.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "ua_stack_channel_manager.h"
  2. struct SL_ChannelManager {
  3. UA_Int32 maxChannelCount;
  4. UA_Int32 lastChannelId;
  5. UA_DateTime maxChannelLifeTime;
  6. UA_list_List channels;
  7. UA_MessageSecurityMode securityMode;
  8. UA_String endpointUrl;
  9. UA_DateTime channelLifeTime;
  10. UA_UInt32 lastTokenId;
  11. };
  12. static SL_ChannelManager *channelManager;
  13. UA_Int32 SL_ChannelManager_init(UA_UInt32 maxChannelCount, UA_UInt32 tokenLifetime, UA_UInt32 startChannelId,
  14. UA_UInt32 startTokenId, UA_String *endpointUrl) {
  15. UA_alloc((void**)&channelManager,sizeof(SL_ChannelManager));
  16. UA_list_init(&(channelManager->channels));
  17. channelManager->lastChannelId = startChannelId;
  18. channelManager->lastTokenId = startTokenId;
  19. UA_String_copy(endpointUrl,&channelManager->endpointUrl);
  20. channelManager->maxChannelLifeTime = tokenLifetime;
  21. channelManager->maxChannelCount = maxChannelCount;
  22. return UA_SUCCESS;
  23. }
  24. UA_Int32 SL_ChannelManager_addChannel(SL_Channel *channel) {
  25. if (!channelManager || (channelManager->maxChannelCount <= channelManager->channels.size))
  26. return UA_ERROR;
  27. //TODO lock access (mulitthreading)------------
  28. UA_list_addPayloadToBack(&channelManager->channels,(void*)channel);
  29. return UA_SUCCESS;
  30. //TODO lock access------------
  31. }
  32. UA_Int32 generateNewTokenId() {
  33. //TODO lock accesss
  34. return channelManager->lastTokenId++;
  35. }
  36. UA_Int32 SL_ChannelManager_generateChannelId(UA_UInt32 *newChannelId) {
  37. if(channelManager) {
  38. *newChannelId = channelManager->lastChannelId++;
  39. return UA_SUCCESS;
  40. }
  41. return UA_ERROR;
  42. }
  43. UA_UInt32 SL_ChannelManager_generateNewTokenId() {
  44. return channelManager->lastTokenId++;
  45. }
  46. UA_Int32 SL_ChannelManager_generateToken(SL_Channel *channel, UA_Int32 requestedLifeTime,
  47. SecurityTokenRequestType requestType,
  48. UA_ChannelSecurityToken* newToken) {
  49. UA_UInt32 tokenId;
  50. if(channel){
  51. SL_Channel_getTokenId(channel,&tokenId);
  52. if(requestType==UA_SECURITYTOKEN_ISSUE) {
  53. SL_Channel_getChannelId(channel,&newToken->channelId);
  54. newToken->createdAt = UA_DateTime_now();
  55. newToken->revisedLifetime = requestedLifeTime > channelManager->maxChannelLifeTime ? channelManager->maxChannelLifeTime : requestedLifeTime;
  56. newToken->tokenId = SL_ChannelManager_generateNewTokenId();
  57. return UA_SUCCESS;
  58. } else if(requestType==UA_SECURITYTOKEN_RENEW) {
  59. SL_Channel_getChannelId(channel,&newToken->channelId);
  60. newToken->createdAt = UA_DateTime_now();
  61. newToken->revisedLifetime = requestedLifeTime > channelManager->maxChannelLifeTime ? channelManager->maxChannelLifeTime : requestedLifeTime;
  62. return UA_SUCCESS;
  63. }
  64. }
  65. return UA_ERROR;
  66. }
  67. UA_Int32 SL_ChannelManager_removeChannel(UA_Int32 channelId) {
  68. //TODO lock access
  69. SL_Channel *channel;
  70. UA_Int32 retval = UA_SUCCESS;
  71. SL_ChannelManager_getChannel(channelId, &channel);
  72. UA_list_Element *element = UA_list_search(&channelManager->channels, (UA_list_PayloadComparer)SL_Channel_compare, &channel);
  73. if(element) {
  74. retval |= UA_list_removeElement(element,(UA_list_PayloadVisitor)SL_Channel_delete);
  75. return retval;
  76. }
  77. //TODO notify server application that secureChannel has been closed part 6 - §7.1.4
  78. return UA_ERROR;
  79. }
  80. UA_Int32 SL_ChannelManager_getChannelLifeTime(UA_DateTime *lifeTime) {
  81. *lifeTime = channelManager->channelLifeTime;
  82. return UA_SUCCESS;
  83. }
  84. /*UA_Int32 SL_ChannelManager_getChannelsByConnectionId(UA_Int32 connectionId,
  85. SL_secureChannel **channels, UA_Int32 *noOfChannels)
  86. {
  87. return UA_SUCCESS;UA_list_Element
  88. }
  89. */
  90. UA_Int32 SL_ChannelManager_getChannel(UA_UInt32 channelId, SL_Channel **channel) {
  91. UA_UInt32 tmpChannelId;
  92. if(channelManager==UA_NULL){
  93. *channel = UA_NULL;
  94. return UA_ERROR;
  95. }
  96. UA_list_Element* current = channelManager->channels.first;
  97. while (current) {
  98. if (current->payload) {
  99. UA_list_Element* elem = (UA_list_Element*) current;
  100. *channel = ((SL_Channel*) (elem->payload));
  101. SL_Channel_getChannelId(*channel, &tmpChannelId);
  102. if(tmpChannelId == channelId)
  103. return UA_SUCCESS;
  104. }
  105. current = current->next;
  106. }
  107. #ifdef DEBUG
  108. // SL_Channel c1 = *(SL_Channel*)(channelManager->channels.first->payload);
  109. // SL_Channel c2 = *(SL_Channel*)(channelManager->channels.last->payload);
  110. // UA_UInt32 id1,id2;
  111. // SL_Channel_getChannelId(c1,&id1);
  112. // SL_Channel_getChannelId(c2,&id2);
  113. //
  114. // printf("SL_ChannelManager_getChannel c1: %i \n",id1);
  115. // printf("SL_ChannelManager_getChannel c2: %i \n",id2);
  116. #endif
  117. *channel = UA_NULL;
  118. return UA_ERROR;
  119. }