ua_connection.c 10 KB

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