ua_transport_binary.c 5.5 KB

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