ua_architecture.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include <stdlib.h>
  47. #if defined(_WIN32) && !defined(__clang__)
  48. # include <malloc.h>
  49. #endif
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <winsock2.h>
  53. #include <ws2tcpip.h>
  54. #include <windows.h>
  55. #ifdef _MSC_VER
  56. # ifndef UNDER_CE
  57. # include <io.h> //access
  58. # define UA_access _access
  59. # endif
  60. #else
  61. # include <unistd.h> //access and tests
  62. # define UA_access access
  63. #endif
  64. #ifdef POP_SLIST_ENTRY
  65. # undef SLIST_ENTRY
  66. # undef POP_SLIST_ENTRY
  67. # pragma pop_macro("SLIST_ENTRY")
  68. #endif
  69. #define ssize_t int
  70. #define OPTVAL_TYPE char
  71. #ifndef UA_sleep_ms
  72. #define UA_sleep_ms(X) Sleep(X)
  73. #else /* UA_sleep_ms */
  74. /* With this one can define its own UA_sleep_ms using a preprocessor define.
  75. E.g. see unit tests. */
  76. void UA_sleep_ms(size_t ms);
  77. #endif
  78. #define UA_fd_set(fd, fds) FD_SET((unsigned int)fd, fds)
  79. #define UA_fd_isset(fd, fds) FD_ISSET((unsigned int)fd, fds)
  80. #ifdef UNDER_CE
  81. # define errno
  82. #endif
  83. // Windows does not support ansi colors
  84. // #define UA_ENABLE_LOG_COLORS
  85. #define UA_IPV6 1
  86. #define UA_SOCKET int
  87. #define UA_INVALID_SOCKET -1
  88. #define UA_ERRNO WSAGetLastError()
  89. #define UA_INTERRUPTED WSAEINTR
  90. #define UA_AGAIN WSAEWOULDBLOCK
  91. #define UA_EAGAIN EAGAIN
  92. #define UA_WOULDBLOCK WSAEWOULDBLOCK
  93. #define UA_ERR_CONNECTION_PROGRESS WSAEWOULDBLOCK
  94. #define UA_getnameinfo getnameinfo
  95. #define UA_send send
  96. #define UA_recv recv
  97. #define UA_close closesocket
  98. #define UA_select select
  99. #define UA_shutdown shutdown
  100. #define UA_socket socket
  101. #define UA_bind bind
  102. #define UA_listen listen
  103. #define UA_accept accept
  104. #define UA_connect connect
  105. #define UA_getaddrinfo getaddrinfo
  106. #define UA_getsockopt getsockopt
  107. #define UA_setsockopt setsockopt
  108. #define UA_freeaddrinfo freeaddrinfo
  109. #define UA_gethostname gethostname
  110. #define UA_free free
  111. #define UA_malloc malloc
  112. #define UA_calloc calloc
  113. #define UA_realloc realloc
  114. #define UA_snprintf(source, size, string, ...) _snprintf_s(source, size, _TRUNCATE, string, __VA_ARGS__)
  115. #define UA_LOG_SOCKET_ERRNO_WRAP(LOG) { \
  116. char *errno_str = NULL; \
  117. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
  118. NULL, WSAGetLastError(), \
  119. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
  120. (LPSTR)&errno_str, 0, NULL); \
  121. LOG; \
  122. LocalFree(errno_str); \
  123. }
  124. #define UA_LOG_SOCKET_ERRNO_GAI_WRAP UA_LOG_SOCKET_ERRNO_WRAP
  125. #include "../ua_architecture_functions.h"
  126. #endif /* PLUGINS_ARCH_WIN32_UA_ARCHITECTURE_H_ */
  127. #endif /* UA_ARCHITECTURE_WIN32 */