ua_stack_channel_manager.c 6.0 KB

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