ua_transport_binary.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "ua_transport_connection.h"
  2. #include <memory.h>
  3. #include "ua_transport_binary.h"
  4. #include "ua_transport.h"
  5. #include "ua_transport_binary_secure.h"
  6. static UA_Int32 TL_handleHello(UA_TL_Connection *connection, const UA_ByteString* msg, UA_UInt32* pos){
  7. UA_Int32 retval = UA_SUCCESS;
  8. UA_UInt32 tmpPos = 0;
  9. UA_Int32 connectionState;
  10. UA_OPCUATcpHelloMessage helloMessage;
  11. UA_TL_Connection_getState(connection, &connectionState);
  12. if (connectionState == CONNECTIONSTATE_CLOSED){
  13. DBG_VERBOSE(printf("TL_handleHello - extracting header information \n"));
  14. UA_OPCUATcpHelloMessage_decodeBinary(msg,pos,&helloMessage);
  15. UA_TL_Connection_configByHello(connection, &helloMessage);
  16. DBG_VERBOSE(printf("TL_handleHello - protocolVersion = %d \n",connection->remoteConf.protocolVersion));
  17. DBG_VERBOSE(printf("TL_handleHello - recvBufferSize = %d \n",connection->remoteConf.recvBufferSize));
  18. DBG_VERBOSE(printf("TL_handleHello - sendBufferSize = %d \n",connection->remoteConf.sendBufferSize));
  19. DBG_VERBOSE(printf("TL_handleHello - maxMessageSize = %d \n",connection->remoteConf.maxMessageSize));
  20. DBG_VERBOSE(printf("TL_handleHello - maxChunkCount = %d \n",connection->remoteConf.maxChunkCount));
  21. // build acknowledge response
  22. UA_OPCUATcpAcknowledgeMessage ackMessage;
  23. TL_Buffer localConfig;
  24. UA_TL_Connection_getLocalConfig(connection, &localConfig);
  25. ackMessage.protocolVersion = localConfig.protocolVersion;
  26. ackMessage.receiveBufferSize = localConfig.recvBufferSize;
  27. ackMessage.sendBufferSize = localConfig.sendBufferSize;
  28. ackMessage.maxMessageSize = localConfig.maxMessageSize;
  29. ackMessage.maxChunkCount = localConfig.maxChunkCount;
  30. UA_OPCUATcpMessageHeader ackHeader;
  31. ackHeader.messageType = UA_MESSAGETYPE_ACK;
  32. ackHeader.isFinal = 'F';
  33. // encode header and message to buffer
  34. tmpPos = 0;
  35. ackHeader.messageSize = UA_OPCUATcpAcknowledgeMessage_calcSizeBinary(&ackMessage) + UA_OPCUATcpMessageHeader_calcSizeBinary(&ackHeader);
  36. UA_ByteString *ack_msg;
  37. UA_alloc((void **)&ack_msg, sizeof(UA_ByteString));
  38. UA_ByteString_newMembers(ack_msg, ackHeader.messageSize);
  39. UA_OPCUATcpMessageHeader_encodeBinary(&ackHeader,ack_msg,&tmpPos);
  40. UA_OPCUATcpAcknowledgeMessage_encodeBinary(&ackMessage, ack_msg,&tmpPos);
  41. DBG_VERBOSE(printf("TL_handleHello - Size messageToSend = %d, pos=%d\n",ackHeader.messageSize, tmpPos));
  42. DBG_VERBOSE(UA_ByteString_printx("_handleHello - ack=", ack_msg));
  43. TL_Send(connection, (const UA_ByteString **) &ack_msg, 1);
  44. DBG_VERBOSE(printf("TL_handleHello - finished writing\n"));
  45. UA_ByteString_delete(ack_msg);
  46. } else {
  47. DBG_ERR(printf("TL_handleHello - wrong connection state \n"));
  48. retval = UA_ERROR_MULTIPLE_HEL;
  49. }
  50. return retval;
  51. }
  52. static UA_Int32 TL_handleOpen(UA_TL_Connection *connection, UA_Server *server, const UA_ByteString* msg, UA_UInt32* pos) {
  53. UA_Int32 retval = UA_SUCCESS;
  54. UA_Int32 state;
  55. SL_Channel *channel;
  56. UA_UInt32 secureChannelId;
  57. retval |= UA_TL_Connection_getState(connection, &state);
  58. if (state == CONNECTIONSTATE_ESTABLISHED) {
  59. UA_UInt32_decodeBinary(msg, pos, &secureChannelId);
  60. SL_ChannelManager_getChannel(secureChannelId, &channel);
  61. if(channel == UA_NULL)
  62. {
  63. SL_Channel *newChannel;
  64. //create new channel
  65. retval |= SL_Channel_new(&newChannel);//just create channel
  66. retval |= SL_Channel_init(newChannel, connection,SL_ChannelManager_generateChannelId, SL_ChannelManager_generateToken);
  67. retval |= SL_Channel_bind(newChannel,connection);
  68. retval |= SL_ProcessOpenChannel(newChannel, server, msg, pos);
  69. retval |= SL_ChannelManager_addChannel(newChannel);
  70. return retval;
  71. }
  72. // channel already exists, renew token?
  73. retval |= SL_ProcessOpenChannel(channel, server, msg, pos);
  74. return retval;
  75. }else{
  76. printf("TL_handleOpen - ERROR: could not create new secureChannel");
  77. }
  78. return UA_ERR_INVALID_VALUE;
  79. }
  80. static UA_Int32 TL_handleMsg(UA_TL_Connection *connection, UA_Server *server, const UA_ByteString* msg, UA_UInt32* pos) {
  81. UA_Int32 state;
  82. UA_TL_Connection_getState(connection,&state);
  83. if (state == CONNECTIONSTATE_ESTABLISHED) {
  84. return SL_Process(server, msg, pos);
  85. }
  86. return UA_ERR_INVALID_VALUE;
  87. }
  88. static UA_Int32 TL_handleClo(UA_TL_Connection *connection, UA_Server *server, const UA_ByteString* msg, UA_UInt32* pos) {
  89. UA_Int32 retval = UA_SUCCESS;
  90. SL_Process(server, msg,pos);
  91. // just prepare closing, closing and freeing structures is done elsewhere
  92. // UA_TL_Connection_close(connection);
  93. UA_TL_Connection_setState(connection, CONNECTIONSTATE_CLOSE);
  94. return retval;
  95. }
  96. UA_Int32 TL_Process(UA_TL_Connection *connection, UA_Server *server, const UA_ByteString* msg) {
  97. UA_Int32 retval = UA_SUCCESS;
  98. UA_UInt32 pos = 0;
  99. UA_OPCUATcpMessageHeader tcpMessageHeader;
  100. DBG_VERBOSE(printf("TL_Process - entered \n"));
  101. UA_Int32 messageCounter = 0;
  102. do{
  103. if ((retval = UA_OPCUATcpMessageHeader_decodeBinary(msg,&pos,&tcpMessageHeader)) == UA_SUCCESS) {
  104. printf("TL_Process - messageType=%.*s\n",3,msg->data);
  105. switch(tcpMessageHeader.messageType) {
  106. case UA_MESSAGETYPE_HEL:
  107. retval = TL_handleHello(connection, msg, &pos);
  108. break;
  109. case UA_MESSAGETYPE_OPN:
  110. retval = TL_handleOpen(connection, server, msg, &pos);
  111. break;
  112. case UA_MESSAGETYPE_MSG:
  113. retval = TL_handleMsg(connection, server, msg, &pos);
  114. break;
  115. case UA_MESSAGETYPE_CLO:
  116. retval = TL_handleClo(connection, server, msg, &pos);
  117. break;
  118. default: // dispatch processing to secureLayer
  119. //Invalid packet was received which could have led to a wrong offset (pos).
  120. //It was not possible to extract the following packet from the buffer
  121. retval = UA_ERR_INVALID_VALUE;
  122. break;
  123. }
  124. }
  125. else
  126. {
  127. printf("TL_Process - ERROR:decoding of header failed \n");
  128. }
  129. messageCounter++;
  130. printf("TL_Process - multipleMessage in Buffer: %i \n",messageCounter);
  131. }while(msg->length > (UA_Int32)pos);
  132. /* if (retval != UA_SUCCESS) { */
  133. /* // FIXME: compose real error message */
  134. /* UA_ByteString errorMsg; */
  135. /* UA_ByteString *errorMsg_ptr = &errorMsg; */
  136. /* UA_ByteString_newMembers(&errorMsg,10); */
  137. /* TL_Send(connection,(const UA_ByteString **)&errorMsg_ptr, 1); */
  138. /* UA_ByteString_deleteMembers(&errorMsg); */
  139. /* } */
  140. UA_OPCUATcpMessageHeader_deleteMembers(&tcpMessageHeader);
  141. return retval;
  142. }
  143. /** respond to client request */
  144. UA_Int32 TL_Send(UA_TL_Connection *connection, const UA_ByteString** gather_buf, UA_UInt32 gather_len) {
  145. UA_Int32 retval = UA_SUCCESS;
  146. DBG_VERBOSE(printf("TL_send - entered \n"));
  147. // if (TL_check(connection,msg,TL_CHECK_REMOTE) == UA_SUCCESS) {
  148. retval = UA_TL_Connection_callWriter(connection, gather_buf, gather_len);
  149. DBG_VERBOSE(printf("TL_send - exited \n"));
  150. //}
  151. /* else */
  152. /* { */
  153. /* DBG_ERR(printf("TL_send - ERROR: packet size greater than remote buffer size")); */
  154. /* retval = UA_ERROR; */
  155. /* } */
  156. return retval;
  157. }