ua_architecture.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 2018 (c) Stephan Kantelberg
  5. */
  6. #ifdef UA_ARCHITECTURE_WEC7
  7. #ifndef PLUGINS_ARCH_WEC7_UA_ARCHITECTURE_H_
  8. #define PLUGINS_ARCH_WEC7_UA_ARCHITECTURE_H_
  9. #ifndef _BSD_SOURCE
  10. # define _BSD_SOURCE
  11. #endif
  12. /* Disable some security warnings on MSVC */
  13. #ifdef _MSC_VER
  14. # define _CRT_SECURE_NO_WARNINGS
  15. #endif
  16. #include "stdint.h"
  17. #ifdef UNDER_CE
  18. #define MAX_STRERROR 31
  19. static char *errorStrings[]= {"Error 0","","No such file or directory","","","","","Arg list too long",
  20. "Exec format error","Bad file number","","","Not enough core","Permission denied","","",
  21. "","File exists","Cross-device link","","","","Invalid argument","","Too many open files",
  22. "","","","No space left on device","","","","","Math argument","Result too large","",
  23. "Resource deadlock would occur", "Unknown error under wince"};
  24. char *strerror(int errnum);
  25. #endif
  26. #include <stdlib.h>
  27. #if defined(_WIN32)
  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. #ifdef _MSC_VER
  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. #if defined(__MINGW32__) //mingw defines SOCKET as long long unsigned int, giving errors in logging and when comparing with UA_Int32
  56. # define UA_SOCKET int
  57. # define UA_INVALID_SOCKET -1
  58. #else
  59. # define UA_SOCKET SOCKET
  60. # define UA_INVALID_SOCKET INVALID_SOCKET
  61. #endif
  62. #define UA_ERRNO WSAGetLastError()
  63. #define UA_INTERRUPTED WSAEINTR
  64. #define UA_AGAIN WSAEWOULDBLOCK
  65. #define UA_EAGAIN EAGAIN
  66. #define UA_WOULDBLOCK WSAEWOULDBLOCK
  67. #define UA_ERR_CONNECTION_PROGRESS WSAEWOULDBLOCK
  68. #define UA_fd_set(fd, fds) FD_SET((UA_SOCKET)fd, fds)
  69. #define UA_fd_isset(fd, fds) FD_ISSET((UA_SOCKET)fd, fds)
  70. #ifdef UNDER_CE
  71. #define UA_ERRNO WSAGetLastError()
  72. #endif
  73. #define UA_getnameinfo getnameinfo
  74. #define UA_send(sockfd, buf, len, flags) send(sockfd, buf, (int)(len), flags)
  75. #define UA_recv recv
  76. #define UA_sendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, (const char*)(buf), (int)(len), flags, dest_addr, (int) (addrlen))
  77. #define UA_recvfrom(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, (char*)(buf), (int)(len), flags, src_addr, addrlen)
  78. #define UA_htonl htonl
  79. #define UA_ntohl ntohl
  80. #define UA_close closesocket
  81. #define UA_select(nfds, readfds, writefds, exceptfds, timeout) select((int)(nfds), readfds, writefds, exceptfds, timeout)
  82. #define UA_shutdown shutdown
  83. #define UA_socket socket
  84. #define UA_bind bind
  85. #define UA_listen listen
  86. #define UA_accept accept
  87. #define UA_connect(sockfd, addr, addrlen) connect(sockfd, addr, (int)(addrlen))
  88. #define UA_getaddrinfo getaddrinfo
  89. #define UA_getsockopt getsockopt
  90. #define UA_setsockopt(sockfd, level, optname, optval, optlen) setsockopt(sockfd, level, optname, (const char*) (optval), optlen)
  91. #define UA_freeaddrinfo freeaddrinfo
  92. #define UA_gethostname gethostname
  93. #define UA_inet_pton InetPton
  94. #ifdef maxStringLength //defined in mingw64
  95. # undef maxStringLength
  96. #endif
  97. #define UA_free free
  98. #define UA_malloc malloc
  99. #define UA_calloc calloc
  100. #define UA_realloc realloc
  101. #define UA_snprintf(source, size, string, ...) _snprintf_s(source, size, _TRUNCATE, string, __VA_ARGS__)
  102. #define UA_LOG_SOCKET_ERRNO_WRAP(LOG) { \
  103. LPVOID errno_str = NULL; \
  104. FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
  105. NULL, WSAGetLastError(), \
  106. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
  107. (LPWSTR)&errno_str, \
  108. 0, NULL); \
  109. LOG; \
  110. LocalFree(errno_str); \
  111. }
  112. #define UA_LOG_SOCKET_ERRNO_GAI_WRAP UA_LOG_SOCKET_ERRNO_WRAP
  113. #include "../ua_architecture_functions.h"
  114. /* Fix redefinition of SLIST_ENTRY on mingw winnt.h */
  115. #ifdef SLIST_ENTRY
  116. # undef SLIST_ENTRY
  117. #endif
  118. #endif /* PLUGINS_ARCH_WEC7_UA_ARCHITECTURE_H_ */
  119. #endif /* UA_ARCHITECTURE_WEC7 */