ua_connection.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. UA_StatusCode
  14. UA_Connection_completeMessages(UA_Connection *connection, UA_ByteString *message,
  15. UA_Boolean *realloced) {
  16. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  17. /* We have a stored an incomplete chunk. Concat the received message to the end.
  18. * After this block, connection->incompleteMessage is always empty. */
  19. if(connection->incompleteMessage.length > 0) {
  20. size_t length = connection->incompleteMessage.length + message->length;
  21. UA_Byte *data = (UA_Byte*)UA_realloc(connection->incompleteMessage.data, length);
  22. if(!data) {
  23. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  24. goto cleanup;
  25. }
  26. memcpy(&data[connection->incompleteMessage.length], message->data, message->length);
  27. connection->releaseRecvBuffer(connection, message);
  28. message->data = data;
  29. message->length = length;
  30. *realloced = true;
  31. connection->incompleteMessage = UA_BYTESTRING_NULL;
  32. }
  33. /* Loop over the chunks in the received buffer */
  34. size_t complete_until = 0; /* the received complete chunks end at this point */
  35. UA_Boolean garbage_end = false; /* garbage after the last complete message */
  36. while(message->length - complete_until >= 8) {
  37. /* Check the message type */
  38. UA_UInt32 msgtype = (UA_UInt32)message->data[complete_until] +
  39. ((UA_UInt32)message->data[complete_until+1] << 8) +
  40. ((UA_UInt32)message->data[complete_until+2] << 16);
  41. if(msgtype != ('M' + ('S' << 8) + ('G' << 16)) &&
  42. msgtype != ('E' + ('R' << 8) + ('R' << 16)) &&
  43. msgtype != ('O' + ('P' << 8) + ('N' << 16)) &&
  44. msgtype != ('H' + ('E' << 8) + ('L' << 16)) &&
  45. msgtype != ('A' + ('C' << 8) + ('K' << 16)) &&
  46. msgtype != ('C' + ('L' << 8) + ('O' << 16))) {
  47. garbage_end = true; /* the message type is not recognized */
  48. break;
  49. }
  50. /* Decode the length of the chunk */
  51. UA_UInt32 chunk_length = 0;
  52. size_t length_pos = complete_until + 4;
  53. UA_StatusCode decode_retval = UA_UInt32_decodeBinary(message, &length_pos, &chunk_length);
  54. /* The message size is not allowed. Throw the remaining bytestring away */
  55. if(decode_retval != UA_STATUSCODE_GOOD ||
  56. chunk_length < 16 ||
  57. chunk_length > connection->localConf.recvBufferSize) {
  58. garbage_end = true;
  59. break;
  60. }
  61. /* The chunk is okay but incomplete. Store the end. */
  62. if(chunk_length + complete_until > message->length)
  63. break;
  64. complete_until += chunk_length; /* Go to the next chunk */
  65. }
  66. /* Separate incomplete chunks */
  67. if(complete_until != message->length) {
  68. /* Garbage after the last good chunk. No need to keep a buffer */
  69. if(garbage_end) {
  70. if(complete_until == 0)
  71. goto cleanup; /* All garbage, only happens on messages from the network layer */
  72. message->length = complete_until;
  73. return UA_STATUSCODE_GOOD;
  74. }
  75. /* No good chunk, only an incomplete one */
  76. if(complete_until == 0) {
  77. if(!*realloced) {
  78. retval = UA_ByteString_allocBuffer(&connection->incompleteMessage, message->length);
  79. if(retval != UA_STATUSCODE_GOOD)
  80. goto cleanup;
  81. memcpy(connection->incompleteMessage.data, message->data, message->length);
  82. connection->releaseRecvBuffer(connection, message);
  83. *realloced = true;
  84. } else {
  85. connection->incompleteMessage = *message;
  86. *message = UA_BYTESTRING_NULL;
  87. }
  88. return UA_STATUSCODE_GOOD;
  89. }
  90. /* At least one good chunk and an incomplete one */
  91. size_t incomplete_length = message->length - complete_until;
  92. retval = UA_ByteString_allocBuffer(&connection->incompleteMessage, incomplete_length);
  93. if(retval != UA_STATUSCODE_GOOD)
  94. goto cleanup;
  95. memcpy(connection->incompleteMessage.data,
  96. &message->data[complete_until], incomplete_length);
  97. message->length = complete_until;
  98. }
  99. return UA_STATUSCODE_GOOD;
  100. cleanup:
  101. if(!*realloced)
  102. connection->releaseRecvBuffer(connection, message);
  103. UA_ByteString_deleteMembers(&connection->incompleteMessage);
  104. return retval;
  105. }
  106. UA_StatusCode
  107. UA_Connection_receiveChunksBlocking(UA_Connection *connection, UA_ByteString *chunks,
  108. UA_Boolean *realloced, UA_UInt32 timeout) {
  109. UA_DateTime now = UA_DateTime_nowMonotonic();
  110. UA_DateTime maxDate = now + (timeout * UA_MSEC_TO_DATETIME);
  111. *realloced = false;
  112. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  113. while(true) {
  114. /* Listen for messages to arrive */
  115. retval = connection->recv(connection, chunks, timeout);
  116. /* Get complete chunks and return */
  117. retval |= UA_Connection_completeMessages(connection, chunks, realloced);
  118. if(retval != UA_STATUSCODE_GOOD || chunks->length > 0)
  119. break;
  120. /* We received a message. But the chunk is incomplete. Compute the
  121. * remaining timeout. */
  122. now = UA_DateTime_nowMonotonic();
  123. if(now > maxDate)
  124. return UA_STATUSCODE_GOODNONCRITICALTIMEOUT;
  125. timeout = (UA_UInt32)((maxDate - now) / UA_MSEC_TO_DATETIME);
  126. }
  127. return retval;
  128. }
  129. void UA_Connection_detachSecureChannel(UA_Connection *connection) {
  130. UA_SecureChannel *channel = connection->channel;
  131. if(channel)
  132. /* only replace when the channel points to this connection */
  133. UA_atomic_cmpxchg((void**)&channel->connection, connection, NULL);
  134. UA_atomic_xchg((void**)&connection->channel, NULL);
  135. }
  136. // TODO: Return an error code
  137. void
  138. UA_Connection_attachSecureChannel(UA_Connection *connection,
  139. UA_SecureChannel *channel) {
  140. if(UA_atomic_cmpxchg((void**)&channel->connection, NULL, connection) == NULL)
  141. UA_atomic_xchg((void**)&connection->channel, (void*)channel);
  142. }
  143. UA_StatusCode
  144. UA_EndpointUrl_split_ptr(const char *endpointUrl, char *hostname,
  145. const char ** port, const char **path) {
  146. if (!endpointUrl || !hostname)
  147. return UA_STATUSCODE_BADINVALIDARGUMENT;
  148. size_t urlLength = strlen(endpointUrl);
  149. if(urlLength < 10 || urlLength >= 256)
  150. return UA_STATUSCODE_BADOUTOFRANGE;
  151. if(strncmp(endpointUrl, "opc.tcp://", 10) != 0)
  152. return UA_STATUSCODE_BADATTRIBUTEIDINVALID;
  153. if (urlLength == 10) {
  154. hostname[0] = '\0';
  155. port = NULL;
  156. *path = NULL;
  157. }
  158. /* where does the port begin? */
  159. size_t portpos = 10;
  160. // opc.tcp://[2001:0db8:85a3::8a2e:0370:7334]:1234/path
  161. // if ip6, then end not found, otherwise we are fine
  162. UA_Boolean ip6_end_found = endpointUrl[portpos] != '[';
  163. for(; portpos < urlLength; ++portpos) {
  164. if (!ip6_end_found) {
  165. if (endpointUrl[portpos] == ']')
  166. ip6_end_found = UA_TRUE;
  167. continue;
  168. }
  169. if(endpointUrl[portpos] == ':' || endpointUrl[portpos] == '/')
  170. break;
  171. }
  172. memcpy(hostname, &endpointUrl[10], portpos - 10);
  173. hostname[portpos-10] = 0;
  174. if(port) {
  175. if (portpos < urlLength - 1) {
  176. if (endpointUrl[portpos] == '/')
  177. *port = NULL;
  178. else
  179. *port = &endpointUrl[portpos + 1];
  180. } else {
  181. *port = NULL;
  182. }
  183. }
  184. if(path) {
  185. size_t pathpos = portpos < urlLength ? portpos : 10;
  186. for(; pathpos < urlLength; ++pathpos) {
  187. if(endpointUrl[pathpos] == '/')
  188. break;
  189. }
  190. if (pathpos < urlLength-1)
  191. *path = &endpointUrl[pathpos+1]; // do not include slash in path
  192. else
  193. *path = NULL;
  194. }
  195. return UA_STATUSCODE_GOOD;
  196. }
  197. UA_StatusCode
  198. UA_EndpointUrl_split(const char *endpointUrl, char *hostname,
  199. UA_UInt16 * port, const char ** path) {
  200. const char* portTmp = NULL;
  201. const char* pathTmp = NULL;
  202. UA_StatusCode retval = UA_EndpointUrl_split_ptr(endpointUrl, hostname, &portTmp, &pathTmp);
  203. if(retval != UA_STATUSCODE_GOOD) {
  204. if(hostname)
  205. hostname[0] = '\0';
  206. return retval;
  207. }
  208. if(!port && !path)
  209. return UA_STATUSCODE_GOOD;
  210. char portStr[6];
  211. portStr[0] = '\0';
  212. if(portTmp) {
  213. size_t portLen;
  214. if (pathTmp)
  215. portLen = (size_t)(pathTmp-portTmp-1);
  216. else
  217. portLen = strlen(portTmp);
  218. if (portLen > 5) // max is 65535
  219. return UA_STATUSCODE_BADOUTOFRANGE;
  220. memcpy(portStr, portTmp, portLen);
  221. portStr[portLen]='\0';
  222. if(port) {
  223. for (size_t i=0; i<6; ++i) {
  224. if (portStr[i] == 0)
  225. break;
  226. if (portStr[i] < '0' || portStr[i] > '9')
  227. return UA_STATUSCODE_BADOUTOFRANGE;
  228. }
  229. UA_UInt32 p;
  230. UA_readNumber((UA_Byte*)portStr, 6, &p);
  231. if (p>65535)
  232. return UA_STATUSCODE_BADOUTOFRANGE;
  233. *port = (UA_UInt16)p;
  234. }
  235. } else {
  236. if (port)
  237. *port = 0;
  238. }
  239. if (path)
  240. *path = pathTmp;
  241. return UA_STATUSCODE_GOOD;
  242. }
  243. size_t UA_readNumber(UA_Byte *buf, size_t buflen, UA_UInt32 *number) {
  244. if (!buf)
  245. return 0;
  246. UA_UInt32 n = 0;
  247. size_t progress = 0;
  248. /* read numbers until the end or a non-number character appears */
  249. while(progress < buflen) {
  250. UA_Byte c = buf[progress];
  251. if('0' > c || '9' < c)
  252. break;
  253. n = (n*10) + (UA_UInt32)(c-'0');
  254. ++progress;
  255. }
  256. *number = n;
  257. return progress;
  258. }