ua_connection.c 10 KB

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