ua_stack_channel_manager.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_UInt32 maxChannelCount;
  10. UA_Int32 lastChannelId;
  11. UA_UInt32 currentChannelCount;
  12. UA_DateTime maxChannelLifeTime;
  13. UA_list_List channels;
  14. UA_MessageSecurityMode securityMode;
  15. UA_String endpointUrl;
  16. UA_DateTime channelLifeTime;
  17. UA_UInt32 lastTokenId;
  18. }SL_ChannelManager;
  19. static SL_ChannelManager *channelManager;
  20. UA_Int32 SL_ChannelManager_init(UA_UInt32 maxChannelCount,UA_UInt32 tokenLifetime, UA_UInt32 startChannelId, UA_UInt32 startTokenId, UA_String *endpointUrl)
  21. {
  22. UA_alloc((void**)&channelManager,sizeof(SL_ChannelManager));
  23. UA_list_init(&(channelManager->channels));
  24. channelManager->lastChannelId = startChannelId;
  25. channelManager->lastTokenId = startTokenId;
  26. UA_String_copy(endpointUrl,&channelManager->endpointUrl);
  27. channelManager->maxChannelLifeTime = tokenLifetime;
  28. channelManager->maxChannelCount = maxChannelCount;
  29. return UA_SUCCESS;
  30. }
  31. UA_Int32 SL_ChannelManager_addChannel(SL_Channel *channel)
  32. {
  33. if (channelManager->maxChannelCount > channelManager->currentChannelCount)
  34. {
  35. //TODO lock access (mulitthreading)------------
  36. // UA_list_Element *element;
  37. // UA_alloc((void**)&element, sizeof(UA_list_Element));
  38. // UA_list_initElement(element);
  39. // element->payload =(void*) channel;
  40. UA_list_addPayloadToBack(&channelManager->channels,(void*)channel);
  41. // UA_list_addElementToBack(&channelManager->channels,element);
  42. return UA_SUCCESS;
  43. //set id in channel object which was added
  44. //TODO lock access------------
  45. }
  46. return UA_ERROR;
  47. }
  48. UA_Int32 generateNewTokenId()
  49. {
  50. //TODO lock accesss
  51. return channelManager->lastTokenId++;
  52. }
  53. UA_Int32 SL_ChannelManager_generateChannelId(UA_UInt32 *newChannelId)
  54. {
  55. if(channelManager)
  56. {
  57. *newChannelId = channelManager->lastChannelId++;
  58. return UA_SUCCESS;
  59. }
  60. return UA_ERROR;
  61. }
  62. UA_UInt32 SL_ChannelManager_generateNewTokenId()
  63. {
  64. return channelManager->lastTokenId++;
  65. }
  66. UA_Int32 SL_ChannelManager_generateToken(SL_Channel channel, UA_Int32 requestedLifeTime,
  67. SecurityTokenRequestType requestType,
  68. UA_ChannelSecurityToken* newToken)
  69. {
  70. UA_UInt32 tokenId;
  71. if(channel){
  72. SL_Channel_getTokenId(channel,&tokenId);
  73. if(requestType==UA_SECURITYTOKEN_ISSUE)
  74. {
  75. SL_Channel_getChannelId(channel,&newToken->channelId);
  76. newToken->createdAt = UA_DateTime_now();
  77. newToken->revisedLifetime = requestedLifeTime > channelManager->maxChannelLifeTime ? channelManager->maxChannelLifeTime : requestedLifeTime;
  78. newToken->tokenId = SL_ChannelManager_generateNewTokenId();
  79. return UA_SUCCESS;
  80. }
  81. else if(requestType==UA_SECURITYTOKEN_RENEW)
  82. {
  83. SL_Channel_getChannelId(channel,&newToken->channelId);
  84. newToken->createdAt = UA_DateTime_now();
  85. newToken->revisedLifetime = requestedLifeTime > channelManager->maxChannelLifeTime ? channelManager->maxChannelLifeTime : requestedLifeTime;
  86. return UA_SUCCESS;
  87. }
  88. else
  89. {
  90. return UA_ERROR;
  91. }
  92. }
  93. return UA_ERROR;
  94. }
  95. UA_Int32 SL_ChannelManager_updateChannels()
  96. {
  97. //TODO lock access
  98. UA_Int32 channelLifetime;
  99. UA_UInt32 channelId;
  100. UA_list_Element* current = channelManager->channels.first;
  101. while (current)
  102. {
  103. if (current->payload)
  104. {
  105. UA_indexedList_Element* elem =
  106. (UA_indexedList_Element*) current->payload;
  107. SL_Channel *channel = (SL_Channel*) elem->payload;
  108. //remove channels with expired lifetime, close linked list
  109. if (channel)
  110. {
  111. UA_list_addPayloadToBack(&channelManager->channels,(void*)channel);
  112. SL_Channel_getRemainingLifetime(*channel,&channelLifetime);
  113. if(channelLifetime <= 0)
  114. {
  115. //removeChannel
  116. }
  117. }
  118. else
  119. {
  120. SL_Channel_getChannelId(*channel, &channelId);
  121. //not possible to remove channel
  122. printf(
  123. "UA_SL_ChannelManager_updateChannels - could not remove channel with id: %i \n",
  124. channelId);
  125. return UA_SUCCESS;
  126. }
  127. }
  128. }
  129. return UA_SUCCESS;
  130. }
  131. UA_Int32 SL_ChannelManager_removeChannel(UA_Int32 channelId)
  132. {
  133. //TODO lock access
  134. SL_Channel channel;
  135. UA_Int32 retval = UA_SUCCESS;
  136. SL_ChannelManager_getChannel(channelId, &channel);
  137. UA_list_Element *element = UA_list_search(&channelManager->channels, (UA_list_PayloadComparer)SL_Channel_equal, &channel);
  138. if(element){
  139. retval |= UA_list_removeElement(element,(UA_list_PayloadVisitor)SL_Channel_delete);
  140. return retval;
  141. }
  142. //TODO notify server application that secureChannel has been closed part 6 - §7.1.4
  143. return UA_ERROR;
  144. }
  145. UA_Int32 SL_ChannelManager_getChannelLifeTime(UA_DateTime *lifeTime)
  146. {
  147. *lifeTime = channelManager->channelLifeTime;
  148. return UA_SUCCESS;
  149. }
  150. /*UA_Int32 SL_ChannelManager_getChannelsByConnectionId(UA_Int32 connectionId,
  151. SL_secureChannel **channels, UA_Int32 *noOfChannels)
  152. {
  153. return UA_SUCCESS;UA_list_Element
  154. }
  155. */
  156. UA_Int32 SL_ChannelManager_getChannel(UA_UInt32 channelId, SL_Channel *channel)
  157. {
  158. UA_UInt32 tmpChannelId;
  159. UA_list_Element* current = channelManager->channels.first;
  160. while (current)
  161. {
  162. if (current->payload)
  163. {
  164. UA_list_Element* elem = (UA_list_Element*) current;
  165. *channel = *((SL_Channel*) (elem->payload));
  166. SL_Channel_getChannelId(*channel, &tmpChannelId);
  167. if(tmpChannelId == channelId)
  168. {
  169. return UA_SUCCESS;
  170. }
  171. }
  172. current = current->next;
  173. }
  174. *channel = UA_NULL;
  175. return UA_ERROR;
  176. }