ua_architecture.h 4.8 KB

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