ua_architecture.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /*#ifdef POP_SLIST_ENTRY
  59. # undef SLIST_ENTRY
  60. # undef POP_SLIST_ENTRY
  61. # pragma pop_macro("SLIST_ENTRY")
  62. #endif*/
  63. #define ssize_t int
  64. #define OPTVAL_TYPE char
  65. #ifndef UA_sleep_ms
  66. #define UA_sleep_ms(X) Sleep(X)
  67. #else /* UA_sleep_ms */
  68. /* With this one can define its own UA_sleep_ms using a preprocessor define.
  69. E.g. see unit tests. */
  70. void UA_sleep_ms(size_t ms);
  71. #endif
  72. // Windows does not support ansi colors
  73. // #define UA_ENABLE_LOG_COLORS
  74. #define UA_IPV6 1
  75. #if defined(__MINGW32__) //mingw defines SOCKET as long long unsigned int, giving errors in logging and when comparing with UA_Int32
  76. # define UA_SOCKET int
  77. # define UA_INVALID_SOCKET -1
  78. #else
  79. # define UA_SOCKET SOCKET
  80. # define UA_INVALID_SOCKET INVALID_SOCKET
  81. #endif
  82. #define UA_ERRNO WSAGetLastError()
  83. #define UA_INTERRUPTED WSAEINTR
  84. #define UA_AGAIN WSAEWOULDBLOCK
  85. #define UA_EAGAIN EAGAIN
  86. #define UA_WOULDBLOCK WSAEWOULDBLOCK
  87. #define UA_ERR_CONNECTION_PROGRESS WSAEWOULDBLOCK
  88. #define UA_fd_set(fd, fds) FD_SET((UA_SOCKET)fd, fds)
  89. #define UA_fd_isset(fd, fds) FD_ISSET((UA_SOCKET)fd, fds)
  90. #ifdef UNDER_CE
  91. # define errno
  92. #endif
  93. #define UA_getnameinfo getnameinfo
  94. #define UA_send(sockfd, buf, len, flags) send(sockfd, buf, (int)(len), flags)
  95. #define UA_recv recv
  96. #define UA_sendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, (const char*)(buf), (int)(len), flags, dest_addr, (int) (addrlen))
  97. #define UA_recvfrom(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, (char*)(buf), (int)(len), flags, src_addr, addrlen)
  98. #define UA_htonl htonl
  99. #define UA_ntohl ntohl
  100. #define UA_close closesocket
  101. #define UA_select(nfds, readfds, writefds, exceptfds, timeout) select((int)(nfds), readfds, writefds, exceptfds, timeout)
  102. #define UA_shutdown shutdown
  103. #define UA_socket socket
  104. #define UA_bind bind
  105. #define UA_listen listen
  106. #define UA_accept accept
  107. #define UA_connect(sockfd, addr, addrlen) connect(sockfd, addr, (int)(addrlen))
  108. #define UA_getaddrinfo getaddrinfo
  109. #define UA_getsockopt getsockopt
  110. #define UA_setsockopt(sockfd, level, optname, optval, optlen) setsockopt(sockfd, level, optname, (const char*) (optval), optlen)
  111. #define UA_freeaddrinfo freeaddrinfo
  112. #define UA_gethostname gethostname
  113. #define UA_inet_pton InetPton
  114. #if UA_IPV6
  115. # include <iphlpapi.h>
  116. # define UA_if_nametoindex if_nametoindex
  117. #endif
  118. #ifdef maxStringLength //defined in mingw64
  119. # undef maxStringLength
  120. #endif
  121. #define UA_free free
  122. #define UA_malloc malloc
  123. #define UA_calloc calloc
  124. #define UA_realloc realloc
  125. #define UA_snprintf(source, size, string, ...) _snprintf_s(source, size, _TRUNCATE, string, __VA_ARGS__)
  126. #define UA_LOG_SOCKET_ERRNO_WRAP(LOG) { \
  127. char *errno_str = NULL; \
  128. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
  129. NULL, WSAGetLastError(), \
  130. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
  131. (LPSTR)&errno_str, 0, NULL); \
  132. LOG; \
  133. LocalFree(errno_str); \
  134. }
  135. #define UA_LOG_SOCKET_ERRNO_GAI_WRAP UA_LOG_SOCKET_ERRNO_WRAP
  136. #include "../ua_architecture_functions.h"
  137. #endif /* PLUGINS_ARCH_WIN32_UA_ARCHITECTURE_H_ */
  138. #endif /* UA_ARCHITECTURE_WIN32 */