ua_connection.c 8.9 KB

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