ua_util.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 2018 (c) Stefan Profanter, fortiss GmbH
  6. */
  7. #ifndef UA_HELPER_H_
  8. #define UA_HELPER_H_
  9. #include "ua_config.h"
  10. #include "ua_types.h"
  11. _UA_BEGIN_DECLS
  12. /**
  13. * Endpoint URL Parser
  14. * -------------------
  15. * The endpoint URL parser is generally useful for the implementation of network
  16. * layer plugins. */
  17. /* Split the given endpoint url into hostname, port and path. All arguments must
  18. * be non-NULL. EndpointUrls have the form "opc.tcp://hostname:port/path", port
  19. * and path may be omitted (together with the prefix colon and slash).
  20. *
  21. * @param endpointUrl The endpoint URL.
  22. * @param outHostname Set to the parsed hostname. The string points into the
  23. * original endpointUrl, so no memory is allocated. If an IPv6 address is
  24. * given, hostname contains e.g. '[2001:0db8:85a3::8a2e:0370:7334]'
  25. * @param outPort Set to the port of the url or left unchanged.
  26. * @param outPath Set to the path if one is present in the endpointUrl.
  27. * Starting or trailing '/' are NOT included in the path. The string
  28. * points into the original endpointUrl, so no memory is allocated.
  29. * @return Returns UA_STATUSCODE_BADTCPENDPOINTURLINVALID if parsing failed. */
  30. UA_StatusCode UA_EXPORT
  31. UA_parseEndpointUrl(const UA_String *endpointUrl, UA_String *outHostname,
  32. UA_UInt16 *outPort, UA_String *outPath);
  33. /* Split the given endpoint url into hostname, vid and pcp. All arguments must
  34. * be non-NULL. EndpointUrls have the form "opc.eth://<host>[:<VID>[.PCP]]".
  35. * The host is a MAC address, an IP address or a registered name like a
  36. * hostname. The format of a MAC address is six groups of hexadecimal digits,
  37. * separated by hyphens (e.g. 01-23-45-67-89-ab). A system may also accept
  38. * hostnames and/or IP addresses if it provides means to resolve it to a MAC
  39. * address (e.g. DNS and Reverse-ARP).
  40. *
  41. * Note: currently only parsing MAC address is supported.
  42. *
  43. * @param endpointUrl The endpoint URL.
  44. * @param vid Set to VLAN ID.
  45. * @param pcp Set to Priority Code Point.
  46. * @return Returns UA_STATUSCODE_BADINTERNALERROR if parsing failed. */
  47. UA_StatusCode UA_EXPORT
  48. UA_parseEndpointUrlEthernet(const UA_String *endpointUrl, UA_String *target,
  49. UA_UInt16 *vid, UA_Byte *pcp);
  50. /* Convert given byte string to a positive number. Returns the number of valid
  51. * digits. Stops if a non-digit char is found and returns the number of digits
  52. * up to that point. */
  53. size_t UA_readNumber(UA_Byte *buf, size_t buflen, UA_UInt32 *number);
  54. /* Same as UA_ReadNumber but with a base parameter */
  55. size_t UA_readNumberWithBase(UA_Byte *buf, size_t buflen, UA_UInt32 *number, UA_Byte base);
  56. #ifndef UA_MIN
  57. #define UA_MIN(A,B) (A > B ? B : A)
  58. #endif
  59. #ifndef UA_MAX
  60. #define UA_MAX(A,B) (A > B ? A : B)
  61. #endif
  62. /**
  63. * Convenience macros for complex types
  64. * ------------------------------------ */
  65. #define UA_PRINTF_GUID_FORMAT "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x"
  66. #define UA_PRINTF_GUID_DATA(GUID) (GUID).data1, (GUID).data2, (GUID).data3, \
  67. (GUID).data4[0], (GUID).data4[1], (GUID).data4[2], (GUID).data4[3], \
  68. (GUID).data4[4], (GUID).data4[5], (GUID).data4[6], (GUID).data4[7]
  69. #define UA_PRINTF_STRING_FORMAT "\"%.*s\""
  70. #define UA_PRINTF_STRING_DATA(STRING) (int)(STRING).length, (STRING).data
  71. /**
  72. * Helper functions for converting data types
  73. * ------------------------------------ */
  74. /*
  75. * Converts a bytestring to the corresponding base64 encoded string representation.
  76. *
  77. * @param byteString the original byte string
  78. * @param str the resulting base64 encoded byte string
  79. *
  80. * @return UA_STATUSCODE_GOOD on success.
  81. */
  82. UA_StatusCode UA_EXPORT
  83. UA_ByteString_toBase64String(const UA_ByteString *byteString, UA_String *str);
  84. /*
  85. * Converts a node id to the corresponding string representation.
  86. * It can be one of:
  87. * - Numeric: ns=0;i=123
  88. * - String: ns=0;s=Some String
  89. * - Guid: ns=0;g=A123456C-0ABC-1A2B-815F-687212AAEE1B
  90. * - ByteString: ns=0;b=AA==
  91. *
  92. */
  93. UA_StatusCode UA_EXPORT
  94. UA_NodeId_toString(const UA_NodeId *nodeId, UA_String *nodeIdStr);
  95. /*
  96. * Compare memory in constant time to mitigate timing attacks.
  97. *
  98. * @return true if ptr1 and ptr2 are equal for length bytes.
  99. */
  100. static UA_INLINE UA_Boolean
  101. UA_constantTimeEqual(const void *ptr1, const void *ptr2, size_t length) {
  102. volatile const UA_Byte *a = (volatile const UA_Byte *)ptr1;
  103. volatile const UA_Byte *b = (volatile const UA_Byte *)ptr2;
  104. volatile UA_Byte c = 0;
  105. for(size_t i = 0; i < length; ++i) {
  106. UA_Byte x = a[i], y = b[i];
  107. c |= x ^ y;
  108. }
  109. return !c;
  110. }
  111. _UA_END_DECLS
  112. #endif /* UA_HELPER_H_ */