ua_stack_session.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. typedef struct UA_SessionType
  11. {
  12. UA_NodeId authenticationToken;
  13. UA_NodeId sessionId;
  14. UA_String name;
  15. Application *application;
  16. UA_list_List pendingRequests;
  17. SL_secureChannel channel;
  18. UA_UInt32 maxRequestMessageSize;
  19. UA_UInt32 maxResponseMessageSize;
  20. }UA_SessionType;
  21. /* mock up function to generate tokens for authentication */
  22. UA_Int32 UA_Session_generateAuthenticationToken(UA_NodeId *newToken)
  23. {
  24. //Random token generation
  25. UA_Int32 retval = UA_SUCCESS;
  26. srand(time(NULL));
  27. UA_Int32 i = 0;
  28. UA_Int32 r = 0;
  29. //retval |= UA_NodeId_new(newToken);
  30. newToken->encodingByte = 0x04; //GUID
  31. newToken->identifier.guid.data1 = rand();
  32. r = rand();
  33. newToken->identifier.guid.data2 = (UA_UInt16)((r>>16) );
  34. r = rand();
  35. UA_Int32 r1 = (r>>16);
  36. UA_Int32 r2 = r1 && 0xFFFF;
  37. r2 = r2 * 1;
  38. newToken->identifier.guid.data3 = (UA_UInt16)((r>>16) );
  39. for(i=0;i<8;i++)
  40. {
  41. r = rand();
  42. newToken->identifier.guid.data4[i] = (UA_Byte)((r>>28) );
  43. }
  44. return retval;
  45. }
  46. UA_Int32 UA_Session_bind(UA_Session session, SL_secureChannel channel)
  47. {
  48. if(channel && session)
  49. {
  50. ((UA_SessionType*)session)->channel = channel;
  51. return UA_SUCCESS;
  52. }
  53. return UA_ERROR;
  54. }
  55. UA_Int32 UA_Session_new(UA_Session **newSession)
  56. {
  57. UA_Int32 retval = UA_SUCCESS;
  58. UA_Session *session;
  59. retval |= UA_alloc((void**)&session,sizeof(UA_Session));
  60. retval |= UA_alloc((void**)session,sizeof(UA_SessionType));
  61. *newSession = session;
  62. **newSession = *session;
  63. //get memory for request list
  64. return retval;
  65. }
  66. UA_Int32 UA_Session_delete(UA_Session *session)
  67. {
  68. UA_Int32 retval = UA_SUCCESS;
  69. retval |= UA_free((UA_SessionType*)(*session));
  70. return retval;
  71. }
  72. UA_Int32 UA_Session_init(UA_Session session, UA_String *sessionName, UA_Double requestedSessionTimeout,
  73. UA_UInt32 maxRequestMessageSize,
  74. UA_UInt32 maxResponseMessageSize,
  75. UA_Session_idProvider idProvider){
  76. UA_Int32 retval = UA_SUCCESS;
  77. retval |= UA_String_copy(sessionName, &((UA_SessionType*)session)->name);
  78. ((UA_SessionType*)session)->maxRequestMessageSize = maxRequestMessageSize;
  79. ((UA_SessionType*)session)->maxResponseMessageSize = maxResponseMessageSize;
  80. UA_Session_generateAuthenticationToken(&((UA_SessionType*)session)->authenticationToken);
  81. idProvider(&((UA_SessionType*)session)->sessionId);
  82. //TODO handle requestedSessionTimeout
  83. return retval;
  84. }
  85. UA_Boolean UA_Session_compare(UA_Session session1, UA_Session session2)
  86. {
  87. if(session1 && session2){
  88. return UA_NodeId_compare(&((UA_SessionType*)session1)->sessionId,
  89. &((UA_SessionType*)session2)->sessionId) == 0;
  90. }
  91. return UA_FALSE;
  92. }
  93. UA_Boolean UA_Session_compareByToken(UA_Session session, UA_NodeId *token)
  94. {
  95. if(session && token){
  96. return UA_NodeId_compare(&((UA_SessionType*)session)->authenticationToken, token);
  97. }
  98. return UA_FALSE;
  99. }
  100. UA_Boolean UA_Session_compareById(UA_Session session, UA_NodeId *sessionId)
  101. {
  102. if(session && sessionId){
  103. return UA_NodeId_compare(&((UA_SessionType*)session)->sessionId, sessionId);
  104. }
  105. return UA_FALSE;
  106. }
  107. UA_Int32 UA_Session_getId(UA_Session session, UA_NodeId *sessionId)
  108. {
  109. if(session)
  110. {
  111. return UA_NodeId_copy(&((UA_SessionType*)session)->sessionId, sessionId);
  112. }
  113. return UA_ERROR;
  114. }
  115. UA_Int32 UA_Session_getToken(UA_Session session, UA_NodeId *authenticationToken)
  116. {
  117. if(session)
  118. {
  119. return UA_NodeId_copy(&((UA_SessionType*)session)->authenticationToken, authenticationToken);
  120. }
  121. return UA_ERROR;
  122. }
  123. UA_Int32 UA_Session_getChannel(UA_Session session, SL_secureChannel *channel)
  124. {
  125. if(session)
  126. {
  127. *channel = ((UA_SessionType*)session)->channel;
  128. return UA_SUCCESS;
  129. }
  130. return UA_ERROR;
  131. }
  132. UA_Int32 UA_Session_getApplicationPointer(UA_Session session, Application** application)
  133. {
  134. if(session)
  135. {
  136. *application = ((UA_SessionType*)session)->application;
  137. return UA_SUCCESS;
  138. }
  139. *application = UA_NULL;
  140. return UA_ERROR;
  141. }
  142. UA_Int32 UA_Session_setApplicationPointer(UA_Session session, Application* application)
  143. {
  144. if(session)
  145. {
  146. ((UA_SessionType*)session)->application = application;
  147. return UA_SUCCESS;
  148. }
  149. return UA_ERROR;
  150. }