open62541 1.4.15
Open source implementation of OPC UA
Loading...
Searching...
No Matches
util.h
Go to the documentation of this file.
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
8#ifndef UA_HELPER_H_
9#define UA_HELPER_H_
10
11#include <open62541/types.h>
14
16
17
18
23
28
29
30
31void
33
35UA_UInt32_random(void); /* no cryptographic entropy */
36
38UA_Guid_random(void); /* no cryptographic entropy */
39
40
41
42typedef struct {
43 size_t mapSize;
46
47UA_EXPORT extern const UA_KeyValueMap UA_KEYVALUEMAP_NULL;
48
49UA_EXPORT UA_KeyValueMap *
51
52UA_EXPORT void
54
55UA_EXPORT void
57
58/** Is the map empty (or NULL)? */
59UA_EXPORT UA_Boolean
61
62/** Does the map contain an entry for the key? */
63UA_EXPORT UA_Boolean
65
66/** Insert a copy of the value. Can reallocate the underlying array. This
67 * invalidates pointers into the previous array. If the key exists already, the
68 * value is overwritten (upsert semantics). */
69UA_EXPORT UA_StatusCode
71 const UA_QualifiedName key,
72 const UA_Variant *value);
73
74/** Helper function for scalar insertion that internally calls
75 * `UA_KeyValueMap_set` */
76UA_EXPORT UA_StatusCode
78 const UA_QualifiedName key,
79 void *p,
80 const UA_DataType *type);
81
82/** Returns a pointer to the value or NULL if the key is not found */
83UA_EXPORT const UA_Variant *
85 const UA_QualifiedName key);
86
87/** Returns NULL if the value for the key is not defined, not of the right
88 * datatype or not a scalar */
89UA_EXPORT const void *
91 const UA_QualifiedName key,
92 const UA_DataType *type);
93
94/** Remove a single entry. To delete the entire map, use `UA_KeyValueMap_clear`. */
95UA_EXPORT UA_StatusCode
97 const UA_QualifiedName key);
98
99/** Create a deep copy of the given KeyValueMap */
100UA_EXPORT UA_StatusCode
102
103/** Copy entries from the right-hand-side into the left-hand-size. Reallocates
104 * previous memory in the left-hand-side. If the operation fails, both maps are
105 * left untouched. */
106UA_EXPORT UA_StatusCode
108
109
110
120
121
122
123/** The default for variables is "BaseDataType" for the datatype, -2 for the
124 * valuerank and a read-accesslevel. */
127
128/** Methods are executable by default */
130
131/** The remaining attribute definitions are currently all zeroed out */
136UA_EXPORT extern const UA_ViewAttributes UA_ViewAttributes_default;
137
138
139
140/** Split the given endpoint url into hostname, port and path. All arguments must
141 * be non-NULL. EndpointUrls have the form "opc.tcp://hostname:port/path", port
142 * and path may be omitted (together with the prefix colon and slash).
143 *
144 * @param endpointUrl The endpoint URL.
145 * @param outHostname Set to the parsed hostname. The string points into the
146 * original endpointUrl, so no memory is allocated. If an IPv6 address is
147 * given, hostname contains e.g. '[2001:0db8:85a3::8a2e:0370:7334]'
148 * @param outPort Set to the port of the url or left unchanged.
149 * @param outPath Set to the path if one is present in the endpointUrl. Can be
150 * NULL. Then not path is returned. Starting or trailing '/' are NOT
151 * included in the path. The string points into the original endpointUrl,
152 * so no memory is allocated.
153 * @return Returns UA_STATUSCODE_BADTCPENDPOINTURLINVALID if parsing failed. */
155UA_parseEndpointUrl(const UA_String *endpointUrl, UA_String *outHostname,
156 UA_UInt16 *outPort, UA_String *outPath);
157
158/** Split the given endpoint url into hostname, vid and pcp. All arguments must
159 * be non-NULL. EndpointUrls have the form "opc.eth://<host>[:<VID>[.PCP]]".
160 * The host is a MAC address, an IP address or a registered name like a
161 * hostname. The format of a MAC address is six groups of hexadecimal digits,
162 * separated by hyphens (e.g. 01-23-45-67-89-ab). A system may also accept
163 * hostnames and/or IP addresses if it provides means to resolve it to a MAC
164 * address (e.g. DNS and Reverse-ARP).
165 *
166 * Note: currently only parsing MAC address is supported.
167 *
168 * @param endpointUrl The endpoint URL.
169 * @param vid Set to VLAN ID.
170 * @param pcp Set to Priority Code Point.
171 * @return Returns UA_STATUSCODE_BADINTERNALERROR if parsing failed. */
174 UA_UInt16 *vid, UA_Byte *pcp);
175
176/** Convert given byte string to a positive number. Returns the number of valid
177 * digits. Stops if a non-digit char is found and returns the number of digits
178 * up to that point. */
179size_t
180UA_readNumber(const UA_Byte *buf, size_t buflen, UA_UInt32 *number);
181
182/** Same as UA_ReadNumber but with a base parameter */
183size_t
184UA_readNumberWithBase(const UA_Byte *buf, size_t buflen,
185 UA_UInt32 *number, UA_Byte base);
186
187#ifndef UA_MIN
188#define UA_MIN(A, B) ((A) > (B) ? (B) : (A))
189#endif
190
191#ifndef UA_MAX
192#define UA_MAX(A, B) ((A) > (B) ? (A) : (B))
193#endif
194
195
196#ifdef UA_ENABLE_PARSING
197UA_EXPORT UA_StatusCode
198UA_RelativePath_parse(UA_RelativePath *rp, const UA_String str);
199#endif
200
201
202#define UA_PRINTF_GUID_FORMAT "%08" PRIx32 "-%04" PRIx16 "-%04" PRIx16 \
203 "-%02" PRIx8 "%02" PRIx8 "-%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8
204#define UA_PRINTF_GUID_DATA(GUID) (GUID).data1, (GUID).data2, (GUID).data3, \
205 (GUID).data4[0], (GUID).data4[1], (GUID).data4[2], (GUID).data4[3], \
206 (GUID).data4[4], (GUID).data4[5], (GUID).data4[6], (GUID).data4[7]
207
208#define UA_PRINTF_STRING_FORMAT "\"%.*s\""
209#define UA_PRINTF_STRING_DATA(STRING) (int)(STRING).length, (STRING).data
210
211
212
213/** Compare memory in constant time to mitigate timing attacks.
214 * Returns true if ptr1 and ptr2 are equal for length bytes. */
215UA_EXPORT UA_Boolean
216UA_constantTimeEqual(const void *ptr1, const void *ptr2, size_t length);
217
218/** Zero-out memory in a way that is not removed by compiler-optimizations. Use
219 * this to ensure cryptographic secrets don't leave traces after the memory was
220 * freed. */
221UA_EXPORT void
223
225
226#endif /* UA_HELPER_H_ */
#define _UA_BEGIN_DECLS
#undef UA_DEBUG_DUMP_PKGS
Definition config.h:100
#define _UA_END_DECLS
Definition config.h:107
UA_UInt32 sendBufferSize
Definition util.h:114
UA_UInt32 remoteMaxChunkCount
Definition util.h:118
UA_UInt32 localMaxMessageSize
Definition util.h:115
UA_UInt32 localMaxChunkCount
Definition util.h:117
UA_UInt32 protocolVersion
Definition util.h:112
UA_UInt32 recvBufferSize
Definition util.h:113
UA_UInt32 remoteMaxMessageSize
Definition util.h:116
DataTypeAttributes.
UA_Duration max
Definition util.h:26
UA_Duration min
Definition util.h:25
UA_KeyValuePair * map
Definition util.h:44
size_t mapSize
Definition util.h:43
KeyValuePair.
MethodAttributes.
ObjectAttributes.
ObjectTypeAttributes.
ReferenceTypeAttributes.
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition util.h:19
UA_UInt32 max
Definition util.h:21
UA_UInt32 min
Definition util.h:20
VariableAttributes.
VariableTypeAttributes.
ViewAttributes.
_UA_BEGIN_DECLS typedef bool UA_Boolean
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition types.h:27
uint16_t UA_UInt16
Definition types.h:47
uint32_t UA_UInt32
Definition types.h:57
void * dst
Definition types.h:948
uint32_t UA_StatusCode
Definition types.h:82
uint8_t UA_Byte
Definition types.h:37
uint64_t UA_UInt64
Definition types.h:67
const UA_DataType * type
Definition types.h:626
UA_Double UA_Duration
Duration.
UA_EXPORT const UA_ObjectTypeAttributes UA_ObjectTypeAttributes_default
UA_EXPORT const void * UA_KeyValueMap_getScalar(const UA_KeyValueMap *map, const UA_QualifiedName key, const UA_DataType *type)
Returns NULL if the value for the key is not defined, not of the right datatype or not a scalar.
UA_EXPORT const UA_VariableAttributes UA_VariableAttributes_default
The default for variables is "BaseDataType" for the datatype, -2 for the valuerank and a read-accessl...
UA_EXPORT UA_StatusCode UA_KeyValueMap_copy(const UA_KeyValueMap *src, UA_KeyValueMap *dst)
Create a deep copy of the given KeyValueMap.
UA_EXPORT UA_Boolean UA_KeyValueMap_isEmpty(const UA_KeyValueMap *map)
Is the map empty (or NULL)?
UA_Guid UA_Guid_random(void)
UA_UInt32 UA_UInt32_random(void)
UA_EXPORT const UA_DataTypeAttributes UA_DataTypeAttributes_default
UA_EXPORT UA_StatusCode UA_KeyValueMap_set(UA_KeyValueMap *map, const UA_QualifiedName key, const UA_Variant *value)
Insert a copy of the value.
UA_EXPORT const UA_Variant * UA_KeyValueMap_get(const UA_KeyValueMap *map, const UA_QualifiedName key)
Returns a pointer to the value or NULL if the key is not found.
UA_StatusCode UA_parseEndpointUrlEthernet(const UA_String *endpointUrl, UA_String *target, UA_UInt16 *vid, UA_Byte *pcp)
Split the given endpoint url into hostname, vid and pcp.
void UA_random_seed(UA_UInt64 seed)
UA_EXPORT UA_StatusCode UA_KeyValueMap_merge(UA_KeyValueMap *lhs, const UA_KeyValueMap *rhs)
Copy entries from the right-hand-side into the left-hand-size.
UA_EXPORT const UA_ViewAttributes UA_ViewAttributes_default
UA_EXPORT void UA_KeyValueMap_clear(UA_KeyValueMap *map)
UA_EXPORT void UA_ByteString_memZero(UA_ByteString *bs)
Zero-out memory in a way that is not removed by compiler-optimizations.
UA_StatusCode UA_parseEndpointUrl(const UA_String *endpointUrl, UA_String *outHostname, UA_UInt16 *outPort, UA_String *outPath)
Split the given endpoint url into hostname, port and path.
UA_EXPORT void UA_KeyValueMap_delete(UA_KeyValueMap *map)
UA_EXPORT const UA_VariableTypeAttributes UA_VariableTypeAttributes_default
UA_EXPORT UA_KeyValueMap * UA_KeyValueMap_new(void)
UA_EXPORT const UA_KeyValueMap UA_KEYVALUEMAP_NULL
UA_EXPORT UA_StatusCode UA_KeyValueMap_remove(UA_KeyValueMap *map, const UA_QualifiedName key)
Remove a single entry.
UA_EXPORT const UA_MethodAttributes UA_MethodAttributes_default
Methods are executable by default.
UA_EXPORT UA_Boolean UA_constantTimeEqual(const void *ptr1, const void *ptr2, size_t length)
Compare memory in constant time to mitigate timing attacks.
size_t UA_readNumberWithBase(const UA_Byte *buf, size_t buflen, UA_UInt32 *number, UA_Byte base)
Same as UA_ReadNumber but with a base parameter.
UA_EXPORT UA_Boolean UA_KeyValueMap_contains(const UA_KeyValueMap *map, const UA_QualifiedName key)
Does the map contain an entry for the key?
UA_EXPORT const UA_ReferenceTypeAttributes UA_ReferenceTypeAttributes_default
UA_EXPORT UA_StatusCode UA_KeyValueMap_setScalar(UA_KeyValueMap *map, const UA_QualifiedName key, void *p, const UA_DataType *type)
Helper function for scalar insertion that internally calls UA_KeyValueMap_set
UA_EXPORT const UA_ObjectAttributes UA_ObjectAttributes_default
The remaining attribute definitions are currently all zeroed out.
size_t UA_readNumber(const UA_Byte *buf, size_t buflen, UA_UInt32 *number)
Convert given byte string to a positive number.