ua_util.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. *
  5. * Copyright 2014, 2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2014 (c) Florian Palm
  7. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  8. */
  9. #include "ua_types_generated_handling.h"
  10. #include "ua_util.h"
  11. #include "ua_util_internal.h"
  12. #include "base64.h"
  13. size_t
  14. UA_readNumberWithBase(UA_Byte *buf, size_t buflen, UA_UInt32 *number, UA_Byte base) {
  15. UA_assert(buf);
  16. UA_assert(number);
  17. u32 n = 0;
  18. size_t progress = 0;
  19. /* read numbers until the end or a non-number character appears */
  20. while(progress < buflen) {
  21. u8 c = buf[progress];
  22. if(c >= '0' && c <= '9' && c <= '0' + (base-1))
  23. n = (n * base) + c - '0';
  24. else if(base > 9 && c >= 'a' && c <= 'z' && c <= 'a' + (base-11))
  25. n = (n * base) + c-'a' + 10;
  26. else if(base > 9 && c >= 'A' && c <= 'Z' && c <= 'A' + (base-11))
  27. n = (n * base) + c-'A' + 10;
  28. else
  29. break;
  30. ++progress;
  31. }
  32. *number = n;
  33. return progress;
  34. }
  35. size_t
  36. UA_readNumber(UA_Byte *buf, size_t buflen, UA_UInt32 *number)
  37. {
  38. return UA_readNumberWithBase(buf, buflen, number, 10);
  39. }
  40. UA_StatusCode
  41. UA_parseEndpointUrl(const UA_String *endpointUrl, UA_String *outHostname,
  42. u16 *outPort, UA_String *outPath) {
  43. /* Url must begin with "opc.tcp://" or opc.udp:// (if pubsub enabled) */
  44. if(endpointUrl->length < 11) {
  45. return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  46. } else if (strncmp((char*)endpointUrl->data, "opc.tcp://", 10) != 0) {
  47. #ifdef UA_ENABLE_PUBSUB
  48. if (strncmp((char*)endpointUrl->data, "opc.udp://", 10) != 0 &&
  49. strncmp((char*)endpointUrl->data, "opc.mqtt://", 11) != 0) {
  50. return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  51. }
  52. #else
  53. return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  54. #endif
  55. }
  56. /* Where does the hostname end? */
  57. size_t curr = 10;
  58. if(endpointUrl->data[curr] == '[') {
  59. /* IPv6: opc.tcp://[2001:0db8:85a3::8a2e:0370:7334]:1234/path */
  60. for(; curr < endpointUrl->length; ++curr) {
  61. if(endpointUrl->data[curr] == ']')
  62. break;
  63. }
  64. if(curr == endpointUrl->length)
  65. return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  66. curr++;
  67. } else {
  68. /* IPv4 or hostname: opc.tcp://something.something:1234/path */
  69. for(; curr < endpointUrl->length; ++curr) {
  70. if(endpointUrl->data[curr] == ':' || endpointUrl->data[curr] == '/')
  71. break;
  72. }
  73. }
  74. /* Set the hostname */
  75. outHostname->data = &endpointUrl->data[10];
  76. outHostname->length = curr - 10;
  77. if(curr == endpointUrl->length)
  78. return UA_STATUSCODE_GOOD;
  79. /* Set the port */
  80. if(endpointUrl->data[curr] == ':') {
  81. if(++curr == endpointUrl->length)
  82. return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  83. u32 largeNum;
  84. size_t progress = UA_readNumber(&endpointUrl->data[curr], endpointUrl->length - curr, &largeNum);
  85. if(progress == 0 || largeNum > 65535)
  86. return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  87. /* Test if the end of a valid port was reached */
  88. curr += progress;
  89. if(curr == endpointUrl->length || endpointUrl->data[curr] == '/')
  90. *outPort = (u16)largeNum;
  91. if(curr == endpointUrl->length)
  92. return UA_STATUSCODE_GOOD;
  93. }
  94. /* Set the path */
  95. UA_assert(curr < endpointUrl->length);
  96. if(endpointUrl->data[curr] != '/')
  97. return UA_STATUSCODE_BADTCPENDPOINTURLINVALID;
  98. if(++curr == endpointUrl->length)
  99. return UA_STATUSCODE_GOOD;
  100. outPath->data = &endpointUrl->data[curr];
  101. outPath->length = endpointUrl->length - curr;
  102. /* Remove trailing slash from the path */
  103. if(endpointUrl->data[endpointUrl->length - 1] == '/')
  104. outPath->length--;
  105. return UA_STATUSCODE_GOOD;
  106. }
  107. UA_StatusCode
  108. UA_parseEndpointUrlEthernet(const UA_String *endpointUrl, UA_String *target,
  109. UA_UInt16 *vid, UA_Byte *prio) {
  110. /* Url must begin with "opc.eth://" */
  111. if(endpointUrl->length < 11) {
  112. return UA_STATUSCODE_BADINTERNALERROR;
  113. } else if(strncmp((char*) endpointUrl->data, "opc.eth://", 10) != 0) {
  114. return UA_STATUSCODE_BADINTERNALERROR;
  115. }
  116. /* Where does the host address end? */
  117. size_t curr = 10;
  118. for(; curr < endpointUrl->length; ++curr) {
  119. if(endpointUrl->data[curr] == ':') {
  120. break;
  121. }
  122. }
  123. /* set host address */
  124. target->data = &endpointUrl->data[10];
  125. target->length = curr - 10;
  126. if(curr == endpointUrl->length) {
  127. return UA_STATUSCODE_GOOD;
  128. }
  129. /* Set VLAN */
  130. u32 value = 0;
  131. curr++; /* skip ':' */
  132. size_t progress = UA_readNumber(&endpointUrl->data[curr],
  133. endpointUrl->length - curr, &value);
  134. if(progress == 0 || value > 4096) {
  135. return UA_STATUSCODE_BADINTERNALERROR;
  136. }
  137. curr += progress;
  138. if(curr == endpointUrl->length || endpointUrl->data[curr] == '.') {
  139. *vid = (UA_UInt16) value;
  140. }
  141. if(curr == endpointUrl->length) {
  142. return UA_STATUSCODE_GOOD;
  143. }
  144. /* Set priority */
  145. if(endpointUrl->data[curr] != '.') {
  146. return UA_STATUSCODE_BADINTERNALERROR;
  147. }
  148. curr++; /* skip '.' */
  149. progress = UA_readNumber(&endpointUrl->data[curr],
  150. endpointUrl->length - curr, &value);
  151. if(progress == 0 || value > 7) {
  152. return UA_STATUSCODE_BADINTERNALERROR;
  153. }
  154. curr += progress;
  155. if(curr != endpointUrl->length) {
  156. return UA_STATUSCODE_BADINTERNALERROR;
  157. }
  158. *prio = (UA_Byte) value;
  159. return UA_STATUSCODE_GOOD;
  160. }
  161. UA_StatusCode UA_ByteString_toBase64String(const UA_ByteString *byteString, UA_String *str) {
  162. if (str->length != 0) {
  163. UA_free(str->data);
  164. str->data = NULL;
  165. str->length = 0;
  166. }
  167. if (byteString == NULL || byteString->data == NULL)
  168. return UA_STATUSCODE_GOOD;
  169. if (byteString == str)
  170. return UA_STATUSCODE_BADINVALIDARGUMENT;
  171. int resSize = 0;
  172. str->data = (UA_Byte*)UA_base64(byteString->data, (int)byteString->length, &resSize);
  173. str->length = (size_t) resSize;
  174. if (str->data == NULL)
  175. return UA_STATUSCODE_BADOUTOFMEMORY;
  176. return UA_STATUSCODE_GOOD;
  177. }
  178. UA_StatusCode
  179. UA_NodeId_toString(const UA_NodeId *nodeId, UA_String *nodeIdStr) {
  180. if (nodeIdStr->length != 0) {
  181. UA_free(nodeIdStr->data);
  182. nodeIdStr->data = NULL;
  183. nodeIdStr->length = 0;
  184. }
  185. if (nodeId == NULL)
  186. return UA_STATUSCODE_GOOD;
  187. char *nsStr = NULL;
  188. long snprintfLen = 0;
  189. size_t nsLen = 0;
  190. if (nodeId->namespaceIndex != 0) {
  191. nsStr = (char*)UA_malloc(9+1); // strlen("ns=XXXXX;") = 9 + Nullbyte
  192. snprintfLen = UA_snprintf(nsStr, 10, "ns=%d;", nodeId->namespaceIndex);
  193. if (snprintfLen < 0 || snprintfLen >= 10) {
  194. UA_free(nsStr);
  195. return UA_STATUSCODE_BADINTERNALERROR;
  196. }
  197. nsLen = (size_t)(snprintfLen);
  198. }
  199. UA_ByteString byteStr = UA_BYTESTRING_NULL;
  200. switch (nodeId->identifierType) {
  201. case UA_NODEIDTYPE_NUMERIC:
  202. /* ns (2 byte, 65535) = 5 chars, numeric (4 byte, 4294967295) = 10 chars, delim = 1 , nullbyte = 1-> 17 chars */
  203. nodeIdStr->length = nsLen + 2 + 10 + 1;
  204. nodeIdStr->data = (UA_Byte*)UA_malloc(nodeIdStr->length);
  205. if (nodeIdStr->data == NULL) {
  206. nodeIdStr->length = 0;
  207. UA_free(nsStr);
  208. return UA_STATUSCODE_BADOUTOFMEMORY;
  209. }
  210. snprintfLen =UA_snprintf((char*)nodeIdStr->data, nodeIdStr->length, "%si=%lu",
  211. nsLen > 0 ? nsStr : "",
  212. (unsigned long )nodeId->identifier.numeric);
  213. break;
  214. case UA_NODEIDTYPE_STRING:
  215. /* ns (16bit) = 5 chars, strlen + nullbyte */
  216. nodeIdStr->length = nsLen + 2 + nodeId->identifier.string.length + 1;
  217. nodeIdStr->data = (UA_Byte*)UA_malloc(nodeIdStr->length);
  218. if (nodeIdStr->data == NULL) {
  219. nodeIdStr->length = 0;
  220. UA_free(nsStr);
  221. return UA_STATUSCODE_BADOUTOFMEMORY;
  222. }
  223. snprintfLen =UA_snprintf((char*)nodeIdStr->data, nodeIdStr->length, "%ss=%.*s",
  224. nsLen > 0 ? nsStr : "",
  225. (int)nodeId->identifier.string.length, nodeId->identifier.string.data);
  226. break;
  227. case UA_NODEIDTYPE_GUID:
  228. /* ns (16bit) = 5 chars + strlen(A123456C-0ABC-1A2B-815F-687212AAEE1B)=36 + nullbyte */
  229. nodeIdStr->length = nsLen + 2 + 36 + 1;
  230. nodeIdStr->data = (UA_Byte*)UA_malloc(nodeIdStr->length);
  231. if (nodeIdStr->data == NULL) {
  232. nodeIdStr->length = 0;
  233. UA_free(nsStr);
  234. return UA_STATUSCODE_BADOUTOFMEMORY;
  235. }
  236. snprintfLen = UA_snprintf((char*)nodeIdStr->data, nodeIdStr->length, "%sg=" UA_PRINTF_GUID_FORMAT,
  237. nsLen > 0 ? nsStr : "",
  238. UA_PRINTF_GUID_DATA(nodeId->identifier.guid));
  239. break;
  240. case UA_NODEIDTYPE_BYTESTRING:
  241. UA_ByteString_toBase64String(&nodeId->identifier.byteString, &byteStr);
  242. /* ns (16bit) = 5 chars + LEN + nullbyte */
  243. nodeIdStr->length = nsLen + 2 + byteStr.length + 1;
  244. nodeIdStr->data = (UA_Byte*)UA_malloc(nodeIdStr->length);
  245. if (nodeIdStr->data == NULL) {
  246. nodeIdStr->length = 0;
  247. UA_String_deleteMembers(&byteStr);
  248. UA_free(nsStr);
  249. return UA_STATUSCODE_BADOUTOFMEMORY;
  250. }
  251. snprintfLen = UA_snprintf((char*)nodeIdStr->data, nodeIdStr->length, "%sb=%.*s",
  252. nsLen > 0 ? nsStr : "",
  253. (int)byteStr.length, byteStr.data);
  254. UA_String_deleteMembers(&byteStr);
  255. break;
  256. }
  257. UA_free(nsStr);
  258. if (snprintfLen < 0 || snprintfLen >= (long) nodeIdStr->length) {
  259. UA_free(nodeIdStr->data);
  260. nodeIdStr->data = NULL;
  261. nodeIdStr->length = 0;
  262. return UA_STATUSCODE_BADINTERNALERROR;
  263. }
  264. nodeIdStr->length = (size_t)snprintfLen;
  265. return UA_STATUSCODE_GOOD;
  266. }