ua_connection.c 12 KB

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