ua_util.c 10 KB

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