ua_architecture.h 4.3 KB

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