ua_stack_channel_manager.c 4.5 KB

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