ua_architecture.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #ifndef _BSD_SOURCE
  11. # define _BSD_SOURCE
  12. #endif
  13. /* Disable some security warnings on MSVC */
  14. #ifdef _MSC_VER
  15. # define _CRT_SECURE_NO_WARNINGS
  16. #endif
  17. /* Assume that Windows versions are newer than Windows XP */
  18. #if defined(__MINGW32__) && (!defined(WINVER) || WINVER < 0x501)
  19. # undef WINVER
  20. # undef _WIN32_WINDOWS
  21. # undef _WIN32_WINNT
  22. # define WINVER 0x0501
  23. # define _WIN32_WINDOWS 0x0501
  24. # define _WIN32_WINNT 0x0501
  25. #endif
  26. /* Backup definition of SLIST_ENTRY on mingw winnt.h */
  27. #ifdef SLIST_ENTRY
  28. # pragma push_macro("SLIST_ENTRY")
  29. # undef SLIST_ENTRY
  30. # define POP_SLIST_ENTRY
  31. #endif
  32. /* Thread-Local Storage
  33. * --------------------
  34. * Thread-local storage is not required by the main library functionality. It is
  35. * only used for some testing strategies. ``UA_THREAD_LOCAL`` is empty if the
  36. * feature is not available. */
  37. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
  38. # define UA_THREAD_LOCAL _Thread_local /* C11 */
  39. #elif defined(__cplusplus) && __cplusplus > 199711L
  40. # define UA_THREAD_LOCAL thread_local /* C++11 */
  41. #elif defined(_MSC_VER)
  42. # define UA_THREAD_LOCAL __declspec(thread) /* MSVC extension */
  43. #else
  44. # define UA_THREAD_LOCAL
  45. #endif
  46. #ifdef _WIN32_WINNT
  47. #undef _WIN32_WINNT
  48. #endif
  49. #define _WIN32_WINNT 0x0600 //windows vista version, which included InepPton
  50. #include <stdlib.h>
  51. #if defined(_WIN32) && !defined(__clang__)
  52. # include <malloc.h>
  53. #endif
  54. #include <stdio.h>
  55. #include <errno.h>
  56. #include <winsock2.h>
  57. #include <windows.h>
  58. #include <ws2tcpip.h>
  59. #ifdef _MSC_VER
  60. # ifndef UNDER_CE
  61. # include <io.h> //access
  62. # define UA_access _access
  63. # endif
  64. #else
  65. # include <unistd.h> //access and tests
  66. # define UA_access access
  67. #endif
  68. #ifdef POP_SLIST_ENTRY
  69. # undef SLIST_ENTRY
  70. # undef POP_SLIST_ENTRY
  71. # pragma pop_macro("SLIST_ENTRY")
  72. #endif
  73. #define ssize_t int
  74. #define OPTVAL_TYPE char
  75. #ifndef UA_sleep_ms
  76. #define UA_sleep_ms(X) Sleep(X)
  77. #else /* UA_sleep_ms */
  78. /* With this one can define its own UA_sleep_ms using a preprocessor define.
  79. E.g. see unit tests. */
  80. void UA_sleep_ms(size_t ms);
  81. #endif
  82. // Windows does not support ansi colors
  83. // #define UA_ENABLE_LOG_COLORS
  84. #define UA_IPV6 1
  85. #if defined(__MINGW32__) //mingw defines SOCKET as long long unsigned int, giving errors in logging and when comparing with UA_Int32
  86. # define UA_SOCKET int
  87. # define UA_INVALID_SOCKET -1
  88. #else
  89. # define UA_SOCKET SOCKET
  90. # define UA_INVALID_SOCKET INVALID_SOCKET
  91. #endif
  92. #define UA_ERRNO WSAGetLastError()
  93. #define UA_INTERRUPTED WSAEINTR
  94. #define UA_AGAIN WSAEWOULDBLOCK
  95. #define UA_EAGAIN EAGAIN
  96. #define UA_WOULDBLOCK WSAEWOULDBLOCK
  97. #define UA_ERR_CONNECTION_PROGRESS WSAEWOULDBLOCK
  98. #define UA_fd_set(fd, fds) FD_SET((UA_SOCKET)fd, fds)
  99. #define UA_fd_isset(fd, fds) FD_ISSET((UA_SOCKET)fd, fds)
  100. #ifdef UNDER_CE
  101. # define errno
  102. #endif
  103. #define UA_getnameinfo getnameinfo
  104. #define UA_send(sockfd, buf, len, flags) send(sockfd, buf, (int)(len), flags)
  105. #define UA_recv recv
  106. #define UA_sendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, (const char*)(buf), (int)(len), flags, dest_addr, (int) (addrlen))
  107. #define UA_recvfrom(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, (char*)(buf), (int)(len), flags, src_addr, addrlen)
  108. #define UA_htonl htonl
  109. #define UA_ntohl ntohl
  110. #define UA_close closesocket
  111. #define UA_select(nfds, readfds, writefds, exceptfds, timeout) select((int)(nfds), readfds, writefds, exceptfds, timeout)
  112. #define UA_shutdown shutdown
  113. #define UA_socket socket
  114. #define UA_bind bind
  115. #define UA_listen listen
  116. #define UA_accept accept
  117. #define UA_connect(sockfd, addr, addrlen) connect(sockfd, addr, (int)(addrlen))
  118. #define UA_getaddrinfo getaddrinfo
  119. #define UA_getsockopt getsockopt
  120. #define UA_setsockopt(sockfd, level, optname, optval, optlen) setsockopt(sockfd, level, optname, (const char*) (optval), optlen)
  121. #define UA_freeaddrinfo freeaddrinfo
  122. #define UA_gethostname gethostname
  123. #define UA_inet_pton InetPton
  124. #if UA_IPV6
  125. # include <Iphlpapi.h>
  126. # define UA_if_nametoindex if_nametoindex
  127. #endif
  128. #ifdef maxStringLength //defined in mingw32
  129. # undef maxStringLength
  130. #endif
  131. #define UA_free free
  132. #define UA_malloc malloc
  133. #define UA_calloc calloc
  134. #define UA_realloc realloc
  135. #define UA_snprintf(source, size, string, ...) _snprintf_s(source, size, _TRUNCATE, string, __VA_ARGS__)
  136. #define UA_LOG_SOCKET_ERRNO_WRAP(LOG) { \
  137. char *errno_str = NULL; \
  138. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
  139. NULL, WSAGetLastError(), \
  140. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
  141. (LPSTR)&errno_str, 0, NULL); \
  142. LOG; \
  143. LocalFree(errno_str); \
  144. }
  145. #define UA_LOG_SOCKET_ERRNO_GAI_WRAP UA_LOG_SOCKET_ERRNO_WRAP
  146. #include "../ua_architecture_functions.h"
  147. #endif /* PLUGINS_ARCH_WIN32_UA_ARCHITECTURE_H_ */
  148. #endif /* UA_ARCHITECTURE_WIN32 */