ua_transport_binary.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include <memory.h>
  2. #include "ua_transport_binary.h"
  3. #include "ua_transport.h"
  4. #include "ua_transport_binary_secure.h"
  5. static UA_Int32 TL_check(TL_Connection* connection, UA_ByteString* msg) {
  6. if(msg->length > (UA_Int32) connection->localConf.maxMessageSize || msg->length > (UA_Int32) connection->remoteConf.maxMessageSize) {
  7. DBG_ERR(printf("TL_check - length error \n"));
  8. return UA_ERR_INCONSISTENT;
  9. }
  10. return UA_SUCCESS;
  11. }
  12. static UA_Int32 TL_handleHello(TL_Connection* connection, const UA_ByteString* msg, UA_UInt32* offset) {
  13. UA_Int32 retval = UA_SUCCESS;
  14. UA_OPCUATcpHelloMessage helloMessage;
  15. if (connection->connectionState == CONNECTIONSTATE_CLOSED) {
  16. DBG_VERBOSE(printf("TL_handleHello - extracting header information \n"));
  17. UA_OPCUATcpHelloMessage_decodeBinary(msg,offset,&helloMessage);
  18. // memorize buffer info and change mode to established
  19. connection->remoteConf.protocolVersion = helloMessage.protocolVersion;
  20. connection->remoteConf.recvBufferSize = helloMessage.receiveBufferSize;
  21. connection->remoteConf.sendBufferSize = helloMessage.sendBufferSize;
  22. connection->remoteConf.maxMessageSize = helloMessage.maxMessageSize;
  23. connection->remoteConf.maxChunkCount = helloMessage.maxChunkCount;
  24. UA_String_copy(&(helloMessage.endpointUrl), &(connection->remoteEndpointUrl));
  25. UA_OPCUATcpHelloMessage_deleteMembers(&helloMessage);
  26. DBG_VERBOSE(printf("TL_handleHello - protocolVersion = %d \n",connection->remoteConf.protocolVersion));
  27. DBG_VERBOSE(printf("TL_handleHello - recvBufferSize = %d \n",connection->remoteConf.recvBufferSize));
  28. DBG_VERBOSE(printf("TL_handleHello - sendBufferSize = %d \n",connection->remoteConf.sendBufferSize));
  29. DBG_VERBOSE(printf("TL_handleHello - maxMessageSize = %d \n",connection->remoteConf.maxMessageSize));
  30. DBG_VERBOSE(printf("TL_handleHello - maxChunkCount = %d \n",connection->remoteConf.maxChunkCount));
  31. connection->connectionState = CONNECTIONSTATE_ESTABLISHED;
  32. // build acknowledge response
  33. UA_OPCUATcpAcknowledgeMessage ackMessage;
  34. ackMessage.protocolVersion = connection->localConf.protocolVersion;
  35. ackMessage.receiveBufferSize = connection->localConf.recvBufferSize;
  36. ackMessage.sendBufferSize = connection->localConf.sendBufferSize;
  37. ackMessage.maxMessageSize = connection->localConf.maxMessageSize;
  38. ackMessage.maxChunkCount = connection->localConf.maxChunkCount;
  39. UA_OPCUATcpMessageHeader ackHeader;
  40. ackHeader.messageType = UA_MESSAGETYPE_ACK;
  41. ackHeader.isFinal = 'F';
  42. // encode header and message to buffer
  43. UA_UInt32 tmpOffset = 0;
  44. ackHeader.messageSize = UA_OPCUATcpAcknowledgeMessage_calcSizeBinary(&ackMessage) + UA_OPCUATcpMessageHeader_calcSizeBinary(&ackHeader);
  45. UA_ByteString *ack_msg;
  46. UA_alloc((void **)&ack_msg, sizeof(UA_ByteString));
  47. UA_ByteString_newMembers(ack_msg, ackHeader.messageSize);
  48. UA_OPCUATcpMessageHeader_encodeBinary(&ackHeader,ack_msg,&tmpOffset);
  49. UA_OPCUATcpAcknowledgeMessage_encodeBinary(&ackMessage,ack_msg,&tmpOffset);
  50. DBG_VERBOSE(printf("TL_handleHello - Size messageToSend = %d, offset=%d\n",ackHeader.messageSize, tmpOffset));
  51. DBG_VERBOSE(UA_ByteString_printx("_handleHello - ack=", ack_msg));
  52. TL_Send(connection, (const UA_ByteString **) &ack_msg, 1);
  53. DBG_VERBOSE(printf("TL_handleHello - finished writing\n"));
  54. UA_ByteString_delete(ack_msg);
  55. } else {
  56. DBG_ERR(printf("TL_handleHello - wrong connection state \n"));
  57. retval = UA_ERROR_MULTIPLE_HEL;
  58. }
  59. return retval;
  60. }
  61. static UA_Int32 TL_handleOpen(TL_Connection* connection, const UA_ByteString* msg, UA_UInt32* pos) {
  62. if (connection->connectionState == CONNECTIONSTATE_ESTABLISHED) {
  63. return SL_Channel_new(connection, msg, pos);
  64. }
  65. return UA_ERR_INVALID_VALUE;
  66. }
  67. static UA_Int32 TL_handleMsg(TL_Connection* connection, const UA_ByteString* msg, UA_UInt32* pos) {
  68. SL_Channel* slc = connection->secureChannel;
  69. return SL_Process(slc,msg,pos);
  70. }
  71. static UA_Int32 TL_handleClo(TL_Connection* connection, const UA_ByteString* msg, UA_UInt32* pos) {
  72. SL_Channel* slc = connection->secureChannel;
  73. connection->connectionState = CONNECTIONSTATE_CLOSE;
  74. connection->secureChannel = UA_NULL;
  75. slc->tlConnection = UA_NULL;
  76. return UA_SUCCESS;
  77. }
  78. UA_Int32 TL_Process(TL_Connection* connection, const UA_ByteString* msg) {
  79. UA_Int32 retval = UA_SUCCESS;
  80. UA_UInt32 pos = 0;
  81. UA_OPCUATcpMessageHeader tcpMessageHeader;
  82. DBG_VERBOSE(printf("TL_Process - entered \n"));
  83. do{
  84. if ((retval = UA_OPCUATcpMessageHeader_decodeBinary(msg,&pos,&tcpMessageHeader)) == UA_SUCCESS) {
  85. printf("TL_Process - messageType=%.*s\n",3,msg->data);
  86. switch(tcpMessageHeader.messageType) {
  87. case UA_MESSAGETYPE_HEL:
  88. retval = TL_handleHello(connection, msg, &pos);
  89. break;
  90. case UA_MESSAGETYPE_OPN:
  91. retval = TL_handleOpen(connection, msg, &pos);
  92. break;
  93. case UA_MESSAGETYPE_MSG:
  94. retval = TL_handleMsg(connection, msg, &pos);
  95. break;
  96. case UA_MESSAGETYPE_CLO:
  97. retval = TL_handleClo(connection, msg, &pos);
  98. break;
  99. default: // dispatch processing to secureLayer
  100. retval = UA_ERR_INVALID_VALUE;
  101. break;
  102. }
  103. }
  104. }while(msg->length > (UA_Int32)pos);
  105. /* if (retval != UA_SUCCESS) { */
  106. /* // FIXME: compose real error message */
  107. /* UA_ByteString errorMsg; */
  108. /* UA_ByteString *errorMsg_ptr = &errorMsg; */
  109. /* UA_ByteString_newMembers(&errorMsg,10); */
  110. /* TL_Send(connection,(const UA_ByteString **)&errorMsg_ptr, 1); */
  111. /* UA_ByteString_deleteMembers(&errorMsg); */
  112. /* } */
  113. UA_OPCUATcpMessageHeader_deleteMembers(&tcpMessageHeader);
  114. return retval;
  115. }
  116. /** respond to client request */
  117. UA_Int32 TL_Send(TL_Connection* connection, const UA_ByteString** gather_buf, UA_UInt32 gather_len) {
  118. UA_Int32 retval = UA_SUCCESS;
  119. DBG_VERBOSE(printf("TL_send - entered \n"));
  120. // if (TL_check(connection,msg,TL_CHECK_REMOTE) == UA_SUCCESS) {
  121. retval = connection->writerCallback(connection, gather_buf, gather_len);
  122. DBG_VERBOSE(printf("TL_send - exited \n"));
  123. //}
  124. /* else */
  125. /* { */
  126. /* DBG_ERR(printf("TL_send - ERROR: packet size greater than remote buffer size")); */
  127. /* retval = UA_ERROR; */
  128. /* } */
  129. return retval;
  130. }