ua_stack_session.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * ua_stack_session.c
  3. *
  4. * Created on: 05.06.2014
  5. * Author: root
  6. */
  7. #include <time.h>
  8. #include <stdlib.h>
  9. #include "ua_stack_session.h"
  10. struct UA_Session
  11. {
  12. UA_NodeId authenticationToken;
  13. UA_NodeId sessionId;
  14. UA_String name;
  15. Application *application;
  16. // UA_list_List pendingRequests;
  17. SL_Channel *channel;
  18. UA_UInt32 maxRequestMessageSize;
  19. UA_UInt32 maxResponseMessageSize;
  20. UA_Int64 timeout;
  21. UA_DateTime validTill;
  22. };
  23. /* mock up function to generate tokens for authentication */
  24. UA_Int32 UA_Session_generateToken(UA_NodeId *newToken)
  25. {
  26. //Random token generation
  27. UA_Int32 retval = UA_SUCCESS;
  28. srand(time(NULL));
  29. UA_Int32 i = 0;
  30. UA_Int32 r = 0;
  31. //retval |= UA_NodeId_new(newToken);
  32. newToken->nodeIdType = UA_NODEIDTYPE_GUID;
  33. newToken->namespace = 0; // where else?
  34. newToken->identifier.guid.data1 = rand();
  35. r = rand();
  36. newToken->identifier.guid.data2 = (UA_UInt16)((r>>16) );
  37. r = rand();
  38. UA_Int32 r1 = (r>>16);
  39. UA_Int32 r2 = r1 && 0xFFFF;
  40. r2 = r2 * 1;
  41. newToken->identifier.guid.data3 = (UA_UInt16)((r>>16) );
  42. for(i=0;i<8;i++)
  43. {
  44. r = rand();
  45. newToken->identifier.guid.data4[i] = (UA_Byte)((r>>28) );
  46. }
  47. return retval;
  48. }
  49. UA_Int32 UA_Session_bind(UA_Session *session, SL_Channel *channel)
  50. {
  51. if(channel && session)
  52. {
  53. session->channel = channel;
  54. return UA_SUCCESS;
  55. }
  56. return UA_ERROR;
  57. }
  58. UA_Int32 UA_Session_new(UA_Session **newSession)
  59. {
  60. UA_Int32 retval = UA_SUCCESS;
  61. UA_Session *session;
  62. retval |= UA_alloc((void**)&session,sizeof(UA_Session));
  63. retval |= UA_alloc((void**)session,sizeof(UA_Session));
  64. *newSession = session;
  65. **newSession = *session;
  66. //get memory for request list
  67. return retval;
  68. }
  69. UA_Int32 UA_Session_deleteMembers(UA_Session *session)
  70. {
  71. UA_Int32 retval = UA_SUCCESS;
  72. retval |= UA_NodeId_deleteMembers(&session->authenticationToken);
  73. retval |= UA_String_deleteMembers(&session->name);
  74. retval |= UA_NodeId_deleteMembers(&session->sessionId);
  75. return retval;
  76. }
  77. UA_Int32 UA_Session_delete(UA_Session *session)
  78. {
  79. UA_Int32 retval = UA_SUCCESS;
  80. UA_Session_deleteMembers(session);
  81. retval |= UA_free(session);
  82. return retval;
  83. }
  84. UA_Int32 UA_Session_init(UA_Session *session, UA_String *sessionName, UA_Double requestedSessionTimeout,
  85. UA_UInt32 maxRequestMessageSize,
  86. UA_UInt32 maxResponseMessageSize,
  87. UA_Session_idProvider idProvider,
  88. UA_Int64 timeout){
  89. UA_Int32 retval = UA_SUCCESS;
  90. retval |= UA_String_copy(sessionName, &session->name);
  91. session->maxRequestMessageSize = maxRequestMessageSize;
  92. session->maxResponseMessageSize = maxResponseMessageSize;
  93. UA_Session_generateToken(&session->authenticationToken);
  94. idProvider(&session->sessionId);
  95. session->timeout = requestedSessionTimeout > timeout ? timeout : requestedSessionTimeout;
  96. UA_Session_updateLifetime(session);
  97. return retval;
  98. }
  99. UA_Boolean UA_Session_compare(UA_Session *session1, UA_Session *session2)
  100. {
  101. if(session1 && session2){
  102. if (UA_NodeId_equal(&session1->sessionId,
  103. &session2->sessionId) == UA_EQUAL){
  104. return UA_TRUE;
  105. }
  106. }
  107. return UA_FALSE;
  108. }
  109. UA_Boolean UA_Session_compareByToken(UA_Session *session, UA_NodeId *token)
  110. {
  111. if(session && token){
  112. return UA_NodeId_equal(&session->authenticationToken, token);
  113. }
  114. return UA_NOT_EQUAL;
  115. }
  116. UA_Boolean UA_Session_compareById(UA_Session *session, UA_NodeId *sessionId)
  117. {
  118. if(session && sessionId){
  119. return UA_NodeId_equal(&session->sessionId, sessionId);
  120. }
  121. return UA_NOT_EQUAL;
  122. }
  123. UA_Int32 UA_Session_getId(UA_Session *session, UA_NodeId *sessionId)
  124. {
  125. if(session)
  126. {
  127. return UA_NodeId_copy(&session->sessionId, sessionId);
  128. }
  129. return UA_ERROR;
  130. }
  131. UA_Int32 UA_Session_getToken(UA_Session *session, UA_NodeId *authenticationToken)
  132. {
  133. if(session)
  134. {
  135. return UA_NodeId_copy(&session->authenticationToken, authenticationToken);
  136. }
  137. return UA_ERROR;
  138. }
  139. UA_Int32 UA_Session_updateLifetime(UA_Session *session)
  140. {
  141. if(session)
  142. {
  143. session->validTill = UA_DateTime_now() +
  144. session->timeout * 100000; //timeout in ms
  145. return UA_SUCCESS;
  146. }
  147. return UA_ERROR;
  148. }
  149. UA_Int32 UA_Session_getChannel(UA_Session *session, SL_Channel **channel)
  150. {
  151. if(session)
  152. {
  153. *channel = session->channel;
  154. return UA_SUCCESS;
  155. }
  156. return UA_ERROR;
  157. }
  158. UA_Int32 UA_Session_getPendingLifetime(UA_Session *session, UA_Double *pendingLifetime_ms)
  159. {
  160. if(session)
  161. {
  162. *pendingLifetime_ms = (session->validTill- UA_DateTime_now() ) / 10000000; //difference in ms
  163. return UA_SUCCESS;
  164. }
  165. return UA_ERROR;
  166. }
  167. UA_Boolean UA_Session_verifyChannel(UA_Session *session, SL_Channel *channel)
  168. {
  169. if(session && channel)
  170. {
  171. if(SL_Channel_compare(session->channel, channel) == UA_TRUE) {
  172. return UA_TRUE;
  173. }
  174. }
  175. return UA_FALSE;
  176. }
  177. UA_Int32 UA_Session_getApplicationPointer(UA_Session *session, Application** application)
  178. {
  179. if(session)
  180. {
  181. *application = session->application;
  182. return UA_SUCCESS;
  183. }
  184. *application = UA_NULL;
  185. return UA_ERROR;
  186. }
  187. UA_Int32 UA_Session_setApplicationPointer(UA_Session *session, Application* application)
  188. {
  189. if(session)
  190. {
  191. session->application = application;
  192. return UA_SUCCESS;
  193. }
  194. return UA_ERROR;
  195. }