open62541 1.3.12
Open source implementation of OPC UA
Loading...
Searching...
No Matches
ua_architecture.h
Go to the documentation of this file.
1/** This work is licensed under a Creative Commons CCZero 1.0 Universal License.
2 * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
3 *
4 * Copyright 2016-2017 (c) Julius Pfrommer, Fraunhofer IOSB
5 * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
6 * Copyright 2021 (c) Christian von Arnim, ISW University of Stuttgart (for VDW and umati)
7 */
8
9#ifdef UA_ARCHITECTURE_WIN32
10
11#ifndef PLUGINS_ARCH_WIN32_UA_ARCHITECTURE_H_
12#define PLUGINS_ARCH_WIN32_UA_ARCHITECTURE_H_
13
14#ifndef _BSD_SOURCE
15# define _BSD_SOURCE
16#endif
17
18/** Disable some security warnings on MSVC */
19#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
20# define _CRT_SECURE_NO_WARNINGS
21#endif
22
23/** Assume that Windows versions are newer than Windows XP */
24#if defined(__MINGW32__) && (!defined(WINVER) || WINVER < 0x501)
25# undef WINVER
26# undef _WIN32_WINDOWS
27# undef _WIN32_WINNT
28# define WINVER 0x0600
29# define _WIN32_WINDOWS 0x0600
30# define _WIN32_WINNT 0x0600 //windows vista version, which included InepPton
31#endif
32
33#include <stdlib.h>
34#if defined(_WIN32) && !defined(__clang__)
35# include <malloc.h>
36#endif
37
39
40#include <stdio.h>
41#include <errno.h>
42#include <winsock2.h>
43#include <windows.h>
44#include <ws2tcpip.h>
45#include <winsock2.h>
46
47#if defined (_MSC_VER) || defined(__clang__)
48# ifndef UNDER_CE
49# include <io.h> //access
50# define UA_access _access
51# endif
52#else
53# include <unistd.h> //access and tests
54# define UA_access access
55#endif
56
57#ifndef _SSIZE_T_DEFINED
58# define ssize_t int
59#endif
60
61#define OPTVAL_TYPE int
62#ifdef UA_sleep_ms
63void UA_sleep_ms(unsigned long ms);
64#else
65# define UA_sleep_ms(X) Sleep(X)
66#endif
67
68// Windows does not support ansi colors
69// #define UA_ENABLE_LOG_COLORS
70
71#define UA_IPV6 1
72
73#if defined(__MINGW32__) && !defined(__clang__) //mingw defines SOCKET as long long unsigned int, giving errors in logging and when comparing with UA_Int32
74# define UA_SOCKET int
75# define UA_INVALID_SOCKET -1
76#else
77# define UA_SOCKET SOCKET
78# define UA_INVALID_SOCKET INVALID_SOCKET
79#endif
80#define UA_ERRNO WSAGetLastError()
81#define UA_INTERRUPTED WSAEINTR
82#define UA_AGAIN WSAEWOULDBLOCK
83#define UA_EAGAIN EAGAIN
84#define UA_WOULDBLOCK WSAEWOULDBLOCK
85#define UA_ERR_CONNECTION_PROGRESS WSAEWOULDBLOCK
86
87#define UA_POLLIN POLLRDNORM
88#define UA_POLLOUT POLLWRNORM
89
90#define UA_fd_set(fd, fds) FD_SET((UA_SOCKET)fd, fds)
91#define UA_fd_isset(fd, fds) FD_ISSET((UA_SOCKET)fd, fds)
92
93#ifdef UNDER_CE
94# define errno
95#endif
96
97#define UA_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) \
98 getnameinfo(sa, (socklen_t)salen, host, (DWORD)hostlen, serv, (DWORD)servlen, flags)
99#define UA_poll(fds,nfds,timeout) WSAPoll((LPWSAPOLLFD)fds, nfds, timeout)
100#define UA_send(sockfd, buf, len, flags) send(sockfd, buf, (int)(len), flags)
101#define UA_recv(sockfd, buf, len, flags) recv(sockfd, buf, (int)(len), flags)
102#define UA_sendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, (const char*)(buf), (int)(len), flags, dest_addr, (int) (addrlen))
103#define UA_recvfrom(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, (char*)(buf), (int)(len), flags, src_addr, addrlen)
104#define UA_recvmsg
105#define UA_htonl htonl
106#define UA_ntohl ntohl
107#define UA_close closesocket
108#define UA_select(nfds, readfds, writefds, exceptfds, timeout) select((int)(nfds), readfds, writefds, exceptfds, timeout)
109#define UA_shutdown shutdown
110#define UA_socket socket
111#define UA_bind bind
112#define UA_listen listen
113#define UA_accept accept
114#define UA_connect(sockfd, addr, addrlen) connect(sockfd, addr, (int)(addrlen))
115#define UA_getaddrinfo getaddrinfo
116#define UA_getsockopt(sockfd, level, optname, optval, optlen) getsockopt(sockfd, level, optname, (char*) (optval), optlen)
117#define UA_setsockopt(sockfd, level, optname, optval, optlen) setsockopt(sockfd, level, optname, (const char*) (optval), optlen)
118#define UA_ioctl
119#define UA_freeaddrinfo freeaddrinfo
120#define UA_gethostname gethostname
121#define UA_getsockname getsockname
122#define UA_inet_pton InetPton
123
124#if UA_IPV6
125# if defined(__WINCRYPT_H__) && defined(UA_ENABLE_ENCRYPTION_LIBRESSL)
126# error "Wincrypt is not compatible with LibreSSL"
127# endif
128# ifdef UA_ENABLE_ENCRYPTION_LIBRESSL
129/** Hack: Prevent Wincrypt-Includes */
130# define __WINCRYPT_H__
131# endif
132
133# include <iphlpapi.h>
134
135# ifdef UA_ENABLE_ENCRYPTION_LIBRESSL
136# undef __WINCRYPT_H__
137# endif
138
139# define UA_if_nametoindex if_nametoindex
140#endif
141
142#ifdef maxStringLength //defined in mingw64
143# undef maxStringLength
144#endif
145
146/** Use the standard malloc */
147#ifndef UA_free
148# define UA_free free
149# define UA_malloc malloc
150# define UA_calloc calloc
151# define UA_realloc realloc
152#endif
153
154#ifdef __CODEGEARC__
155#define _snprintf_s(a,b,c,...) snprintf(a,b,__VA_ARGS__)
156#endif
157
158/** 3rd Argument is the string */
159#define UA_snprintf(source, size, ...) _snprintf_s(source, size, _TRUNCATE, __VA_ARGS__)
160#define UA_strncasecmp _strnicmp
161
162#define UA_LOG_SOCKET_ERRNO_WRAP(LOG) { \
163 char *errno_str = NULL; \
164 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
165 NULL, WSAGetLastError(), \
166 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
167 (LPSTR)&errno_str, 0, NULL); \
168 LOG; \
169 LocalFree(errno_str); \
170}
171#define UA_LOG_SOCKET_ERRNO_GAI_WRAP UA_LOG_SOCKET_ERRNO_WRAP
172
173#if UA_MULTITHREADING >= 100
174
175typedef struct {
176 CRITICAL_SECTION mutex;
177 int mutexCounter;
178} UA_Lock;
179
180static void
181UA_LOCK_INIT(UA_Lock *lock) {
182 InitializeCriticalSection(&lock->mutex);
183 lock->mutexCounter = 0;
184}
185
186static void
187UA_LOCK_DESTROY(UA_Lock *lock) {
188 DeleteCriticalSection(&lock->mutex);
189}
190
191static void
192UA_LOCK(UA_Lock *lock) {
193 EnterCriticalSection(&lock->mutex);
194 UA_assert(++(lock->mutexCounter) == 1);
195}
196
197static void
198UA_UNLOCK(UA_Lock *lock) {
199 UA_assert(--(lock->mutexCounter) == 0);
200 LeaveCriticalSection(&lock->mutex);
201}
202
203static void
204UA_LOCK_ASSERT(UA_Lock *lock, int num) {
205 UA_assert(lock->mutexCounter == num);
206}
207#else
208#define UA_LOCK_INIT(lock)
209#define UA_LOCK_DESTROY(lock)
210#define UA_LOCK(lock)
211#define UA_UNLOCK(lock)
212#define UA_LOCK_ASSERT(lock, num)
213#endif
214
216
217/** Fix redefinition of SLIST_ENTRY on mingw winnt.h */
218#if !defined(_SYS_QUEUE_H_) && defined(SLIST_ENTRY)
219# undef SLIST_ENTRY
220#endif
221
222#endif /* PLUGINS_ARCH_WIN32_UA_ARCHITECTURE_H_ */
223
224#endif /* UA_ARCHITECTURE_WIN32 */
#define UA_assert(ignore)
#define UA_sleep_ms(X)
This work is licensed under a Creative Commons CCZero 1.0 Universal License.
Definition ua_freeRTOS.h:17