ua_architecture.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. */
  7. #ifdef UA_ARCHITECTURE_WIN32
  8. #ifndef PLUGINS_ARCH_WIN32_UA_ARCHITECTURE_H_
  9. #define PLUGINS_ARCH_WIN32_UA_ARCHITECTURE_H_
  10. #include <open62541/architecture_base.h>
  11. #ifndef _BSD_SOURCE
  12. # define _BSD_SOURCE
  13. #endif
  14. /* Disable some security warnings on MSVC */
  15. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  16. # define _CRT_SECURE_NO_WARNINGS
  17. #endif
  18. /* Assume that Windows versions are newer than Windows XP */
  19. #if defined(__MINGW32__) && (!defined(WINVER) || WINVER < 0x501)
  20. # undef WINVER
  21. # undef _WIN32_WINDOWS
  22. # undef _WIN32_WINNT
  23. # define WINVER 0x0600
  24. # define _WIN32_WINDOWS 0x0600
  25. # define _WIN32_WINNT 0x0600 //windows vista version, which included InepPton
  26. #endif
  27. #include <stdlib.h>
  28. #if defined(_WIN32) && !defined(__clang__)
  29. # include <malloc.h>
  30. #endif
  31. #include <stdio.h>
  32. #include <errno.h>
  33. #include <winsock2.h>
  34. #include <windows.h>
  35. #include <ws2tcpip.h>
  36. #if defined (_MSC_VER) || defined(__clang__)
  37. # ifndef UNDER_CE
  38. # include <io.h> //access
  39. # define UA_access _access
  40. # endif
  41. #else
  42. # include <unistd.h> //access and tests
  43. # define UA_access access
  44. #endif
  45. #define ssize_t int
  46. #define OPTVAL_TYPE char
  47. #ifndef UA_sleep_ms
  48. # define UA_sleep_ms(X) Sleep(X)
  49. #endif
  50. // Windows does not support ansi colors
  51. // #define UA_ENABLE_LOG_COLORS
  52. #define UA_IPV6 1
  53. #if defined(__MINGW32__) && !defined(__clang__) //mingw defines SOCKET as long long unsigned int, giving errors in logging and when comparing with UA_Int32
  54. # define UA_SOCKET int
  55. # define UA_INVALID_SOCKET -1
  56. #else
  57. # define UA_SOCKET SOCKET
  58. # define UA_INVALID_SOCKET INVALID_SOCKET
  59. #endif
  60. #define UA_ERRNO WSAGetLastError()
  61. #define UA_INTERRUPTED WSAEINTR
  62. #define UA_AGAIN WSAEWOULDBLOCK
  63. #define UA_EAGAIN EAGAIN
  64. #define UA_WOULDBLOCK WSAEWOULDBLOCK
  65. #define UA_ERR_CONNECTION_PROGRESS WSAEWOULDBLOCK
  66. #define UA_fd_set(fd, fds) FD_SET((UA_SOCKET)fd, fds)
  67. #define UA_fd_isset(fd, fds) FD_ISSET((UA_SOCKET)fd, fds)
  68. #ifdef UNDER_CE
  69. # define errno
  70. #endif
  71. #define UA_getnameinfo getnameinfo
  72. #define UA_send(sockfd, buf, len, flags) send(sockfd, buf, (int)(len), flags)
  73. #define UA_recv(sockfd, buf, len, flags) recv(sockfd, buf, (int)(len), flags)
  74. #define UA_sendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, (const char*)(buf), (int)(len), flags, dest_addr, (int) (addrlen))
  75. #define UA_recvfrom(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, (char*)(buf), (int)(len), flags, src_addr, addrlen)
  76. #define UA_htonl htonl
  77. #define UA_ntohl ntohl
  78. #define UA_close closesocket
  79. #define UA_select(nfds, readfds, writefds, exceptfds, timeout) select((int)(nfds), readfds, writefds, exceptfds, timeout)
  80. #define UA_shutdown shutdown
  81. #define UA_socket socket
  82. #define UA_bind bind
  83. #define UA_listen listen
  84. #define UA_accept accept
  85. #define UA_connect(sockfd, addr, addrlen) connect(sockfd, addr, (int)(addrlen))
  86. #define UA_getaddrinfo getaddrinfo
  87. #define UA_getsockopt getsockopt
  88. #define UA_setsockopt(sockfd, level, optname, optval, optlen) setsockopt(sockfd, level, optname, (const char*) (optval), optlen)
  89. #define UA_freeaddrinfo freeaddrinfo
  90. #define UA_gethostname gethostname
  91. #define UA_getsockname getsockname
  92. #define UA_inet_pton InetPton
  93. #if UA_IPV6
  94. # include <iphlpapi.h>
  95. # define UA_if_nametoindex if_nametoindex
  96. #endif
  97. #ifdef maxStringLength //defined in mingw64
  98. # undef maxStringLength
  99. #endif
  100. #ifndef UA_free
  101. #define UA_free free
  102. #endif
  103. #ifndef UA_malloc
  104. #define UA_malloc malloc
  105. #endif
  106. #ifndef UA_calloc
  107. #define UA_calloc calloc
  108. #endif
  109. #ifndef UA_realloc
  110. #define UA_realloc realloc
  111. #endif
  112. /* 3rd Argument is the string */
  113. #define UA_snprintf(source, size, ...) _snprintf_s(source, size, _TRUNCATE, __VA_ARGS__)
  114. #define UA_LOG_SOCKET_ERRNO_WRAP(LOG) { \
  115. char *errno_str = NULL; \
  116. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
  117. NULL, WSAGetLastError(), \
  118. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
  119. (LPSTR)&errno_str, 0, NULL); \
  120. LOG; \
  121. LocalFree(errno_str); \
  122. }
  123. #define UA_LOG_SOCKET_ERRNO_GAI_WRAP UA_LOG_SOCKET_ERRNO_WRAP
  124. #if UA_MULTITHREADING >= 100
  125. #define UA_LOCK_TYPE(mutexName) CRITICAL_SECTION mutexName; \
  126. int mutexName##Counter;
  127. #define UA_LOCK_INIT(mutexName) InitializeCriticalSection(&mutexName); \
  128. mutexName##Counter = 0;;
  129. #define UA_LOCK_DESTROY(mutexName) DeleteCriticalSection(&mutexName);
  130. #define UA_LOCK(mutexName) EnterCriticalSection(&mutexName); \
  131. UA_assert(++(mutexName##Counter) == 1);
  132. #define UA_UNLOCK(mutexName) UA_assert(--(mutexName##Counter) == 0); \
  133. LeaveCriticalSection(&mutexName);
  134. #define UA_LOCK_ASSERT(mutexName, num) UA_assert(mutexName##Counter == num);
  135. #else
  136. #define UA_LOCK_TYPE(mutexName)
  137. #define UA_LOCK_TYPE_POINTER(mutexName)
  138. #define UA_LOCK_INIT(mutexName)
  139. #define UA_LOCK_DESTROY(mutexName)
  140. #define UA_LOCK(mutexName)
  141. #define UA_UNLOCK(mutexName)
  142. #define UA_LOCK_ASSERT(mutexName, num)
  143. #endif
  144. #include <open62541/architecture_functions.h>
  145. /* Fix redefinition of SLIST_ENTRY on mingw winnt.h */
  146. #if !defined(_SYS_QUEUE_H_) && defined(SLIST_ENTRY)
  147. # undef SLIST_ENTRY
  148. #endif
  149. #endif /* PLUGINS_ARCH_WIN32_UA_ARCHITECTURE_H_ */
  150. #endif /* UA_ARCHITECTURE_WIN32 */