ua_connection.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include "ua_util.h"
  5. #include "ua_connection_internal.h"
  6. #include "ua_types_encoding_binary.h"
  7. #include "ua_types_generated_encoding_binary.h"
  8. #include "ua_types_generated_handling.h"
  9. #include "ua_securechannel.h"
  10. void UA_Connection_deleteMembers(UA_Connection *connection) {
  11. UA_ByteString_deleteMembers(&connection->incompleteMessage);
  12. }
  13. static UA_StatusCode
  14. prependIncomplete(UA_Connection *connection, UA_ByteString * UA_RESTRICT message,
  15. UA_Boolean * UA_RESTRICT realloced) {
  16. UA_assert(connection->incompleteMessage.length > 0);
  17. /* Allocate the new message buffer */
  18. size_t length = connection->incompleteMessage.length + message->length;
  19. UA_Byte *data = (UA_Byte*)UA_realloc(connection->incompleteMessage.data, length);
  20. if(!data) {
  21. UA_ByteString_deleteMembers(&connection->incompleteMessage);
  22. connection->releaseRecvBuffer(connection, message);
  23. return UA_STATUSCODE_BADOUTOFMEMORY;
  24. }
  25. /* Copy / release the current message buffer */
  26. memcpy(&data[connection->incompleteMessage.length], message->data, message->length);
  27. connection->releaseRecvBuffer(connection, message);
  28. message->length = length;
  29. message->data = data;
  30. connection->incompleteMessage = UA_BYTESTRING_NULL;
  31. *realloced = true;
  32. return UA_STATUSCODE_GOOD;
  33. }
  34. static size_t
  35. completeChunksUntil(UA_Connection *connection, UA_ByteString * UA_RESTRICT message,
  36. UA_Boolean *garbage_end) {
  37. size_t complete_until = 0;
  38. /* At least 8 byte needed for the header... */
  39. while(message->length - complete_until >= 8) {
  40. /* Check the message type */
  41. UA_UInt32 msgtype = (UA_UInt32)message->data[complete_until] +
  42. ((UA_UInt32)message->data[complete_until+1] << 8) +
  43. ((UA_UInt32)message->data[complete_until+2] << 16);
  44. if(msgtype != ('M' + ('S' << 8) + ('G' << 16)) &&
  45. msgtype != ('E' + ('R' << 8) + ('R' << 16)) &&
  46. msgtype != ('O' + ('P' << 8) + ('N' << 16)) &&
  47. msgtype != ('H' + ('E' << 8) + ('L' << 16)) &&
  48. msgtype != ('A' + ('C' << 8) + ('K' << 16)) &&
  49. msgtype != ('C' + ('L' << 8) + ('O' << 16))) {
  50. *garbage_end = true; /* the message type is not recognized */
  51. break;
  52. }
  53. UA_Byte isFinal = message->data[complete_until+3];
  54. if (isFinal != 'C' && isFinal != 'F' && isFinal != 'A') {
  55. *garbage_end = true; /* the message type is not recognized */
  56. break;
  57. }
  58. /* Decoding failed or the message size is not allowed. The remaining
  59. * message is garbage. */
  60. UA_UInt32 chunk_length = 0;
  61. size_t length_pos = complete_until + 4;
  62. if(UA_UInt32_decodeBinary(message, &length_pos, &chunk_length) != UA_STATUSCODE_GOOD ||
  63. chunk_length < 16 || chunk_length > connection->localConf.recvBufferSize) {
  64. *garbage_end = true;
  65. break;
  66. }
  67. /* The chunk is okay but incomplete. Store the end. */
  68. if(chunk_length + complete_until > message->length)
  69. break;
  70. /* Continue to the next chunk */
  71. complete_until += chunk_length;
  72. }
  73. UA_assert(complete_until <= message->length);
  74. return complete_until;
  75. }
  76. static UA_StatusCode
  77. separateIncompleteChunk(UA_Connection *connection, UA_ByteString * UA_RESTRICT message,
  78. size_t complete_until, UA_Boolean garbage_end, UA_Boolean *realloced) {
  79. UA_assert(complete_until < message->length);
  80. /* Garbage after the last good chunk. No need to keep an incomplete message. */
  81. if(garbage_end) {
  82. if(complete_until == 0) /* All garbage */
  83. return UA_STATUSCODE_BADDECODINGERROR;
  84. message->length = complete_until;
  85. return UA_STATUSCODE_GOOD;
  86. }
  87. /* No good chunk, buffer the entire message */
  88. if(complete_until == 0) {
  89. if(!*realloced) {
  90. UA_StatusCode retval =
  91. UA_ByteString_allocBuffer(&connection->incompleteMessage, message->length);
  92. if(retval != UA_STATUSCODE_GOOD)
  93. return retval;
  94. memcpy(connection->incompleteMessage.data, message->data, message->length);
  95. connection->releaseRecvBuffer(connection, message);
  96. *realloced = true;
  97. } else {
  98. connection->incompleteMessage = *message;
  99. *message = UA_BYTESTRING_NULL;
  100. }
  101. return UA_STATUSCODE_GOOD;
  102. }
  103. /* At least one good chunk and an incomplete one */
  104. size_t incomplete_length = message->length - complete_until;
  105. UA_StatusCode retval =
  106. UA_ByteString_allocBuffer(&connection->incompleteMessage, incomplete_length);
  107. if(retval != UA_STATUSCODE_GOOD)
  108. return retval;
  109. memcpy(connection->incompleteMessage.data, &message->data[complete_until], incomplete_length);
  110. message->length = complete_until;
  111. return UA_STATUSCODE_GOOD;
  112. }
  113. UA_StatusCode
  114. UA_Connection_completeChunks(UA_Connection *connection,
  115. UA_ByteString * UA_RESTRICT message,
  116. UA_Boolean * UA_RESTRICT realloced) {
  117. /* If we have stored an incomplete chunk, prefix to the received message.
  118. * After this block, connection->incompleteMessage is always empty. The
  119. * message and the buffer is released if allocating the memory fails. */
  120. if(connection->incompleteMessage.length > 0) {
  121. UA_StatusCode retval = prependIncomplete(connection, message, realloced);
  122. if(retval != UA_STATUSCODE_GOOD)
  123. return retval;
  124. }
  125. /* Find the end of the last complete chunk */
  126. UA_Boolean garbage_end = false; /* garbage after the last complete message */
  127. size_t complete_until = completeChunksUntil(connection, message, &garbage_end);
  128. /* Buffer incomplete chunk (at the end of the message) in the connection and
  129. * adjust the size of the message. */
  130. if(complete_until < message->length) {
  131. UA_StatusCode retval = separateIncompleteChunk(connection, message, complete_until,
  132. garbage_end, realloced);
  133. if(retval != UA_STATUSCODE_GOOD) {
  134. if(*realloced)
  135. UA_ByteString_deleteMembers(message);
  136. else
  137. connection->releaseRecvBuffer(connection, message);
  138. return retval;
  139. }
  140. }
  141. return UA_STATUSCODE_GOOD;
  142. }
  143. UA_StatusCode
  144. UA_Connection_receiveChunksBlocking(UA_Connection *connection, UA_ByteString *chunks,
  145. UA_Boolean *realloced, UA_UInt32 timeout) {
  146. UA_DateTime now = UA_DateTime_nowMonotonic();
  147. UA_DateTime maxDate = now + (timeout * UA_MSEC_TO_DATETIME);
  148. *realloced = false;
  149. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  150. while(true) {
  151. /* Listen for messages to arrive */
  152. retval = connection->recv(connection, chunks, timeout);
  153. /* Get complete chunks and return */
  154. retval |= UA_Connection_completeChunks(connection, chunks, realloced);
  155. if(retval != UA_STATUSCODE_GOOD || chunks->length > 0)
  156. break;
  157. /* We received a message. But the chunk is incomplete. Compute the
  158. * remaining timeout. */
  159. now = UA_DateTime_nowMonotonic();
  160. if(now > maxDate)
  161. return UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  162. timeout = (UA_UInt32)((maxDate - now) / UA_MSEC_TO_DATETIME);
  163. }
  164. return retval;
  165. }
  166. void UA_Connection_detachSecureChannel(UA_Connection *connection) {
  167. UA_SecureChannel *channel = connection->channel;
  168. if(channel)
  169. /* only replace when the channel points to this connection */
  170. UA_atomic_cmpxchg((void**)&channel->connection, connection, NULL);
  171. UA_atomic_xchg((void**)&connection->channel, NULL);
  172. }
  173. // TODO: Return an error code
  174. void
  175. UA_Connection_attachSecureChannel(UA_Connection *connection, UA_SecureChannel *channel) {
  176. if(UA_atomic_cmpxchg((void**)&channel->connection, NULL, connection) == NULL)
  177. UA_atomic_xchg((void**)&connection->channel, (void*)channel);
  178. }