ua_architecture.h 4.4 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 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. #include "ua_architecture_base.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. #include "stdint.h"
  18. #ifdef UNDER_CE
  19. #define MAX_STRERROR 31
  20. static char *errorStrings[]= {"Error 0","","No such file or directory","","","","","Arg list too long",
  21. "Exec format error","Bad file number","","","Not enough core","Permission denied","","",
  22. "","File exists","Cross-device link","","","","Invalid argument","","Too many open files",
  23. "","","","No space left on device","","","","","Math argument","Result too large","",
  24. "Resource deadlock would occur", "Unknown error under wince"};
  25. char *strerror(int errnum);
  26. #endif
  27. #include <stdlib.h>
  28. #if defined(_WIN32)
  29. # include <malloc.h>
  30. #endif
  31. #include <stdio.h>
  32. #include <errno.h>
  33. #include <winsock2.h>
  34. #include <windows.h>
  35. #include <ws2tcpip.h>
  36. #ifdef _MSC_VER
  37. # ifndef UNDER_CE
  38. # include <io.h> //access
  39. # define UA_access _access
  40. # endif
  41. #else
  42. # include <unistd.h> //access and tests
  43. # define UA_access access
  44. #endif
  45. #define ssize_t int
  46. #define OPTVAL_TYPE char
  47. #ifndef UA_sleep_ms
  48. # define UA_sleep_ms(X) Sleep(X)
  49. #endif
  50. // Windows does not support ansi colors
  51. // #define UA_ENABLE_LOG_COLORS
  52. #if defined(__MINGW32__) //mingw defines SOCKET as long long unsigned int, giving errors in logging and when comparing with UA_Int32
  53. # define UA_SOCKET int
  54. # define UA_INVALID_SOCKET -1
  55. #else
  56. # define UA_SOCKET SOCKET
  57. # define UA_INVALID_SOCKET INVALID_SOCKET
  58. #endif
  59. #define UA_ERRNO WSAGetLastError()
  60. #define UA_INTERRUPTED WSAEINTR
  61. #define UA_AGAIN WSAEWOULDBLOCK
  62. #define UA_EAGAIN EAGAIN
  63. #define UA_WOULDBLOCK WSAEWOULDBLOCK
  64. #define UA_ERR_CONNECTION_PROGRESS WSAEWOULDBLOCK
  65. #define UA_fd_set(fd, fds) FD_SET((UA_SOCKET)fd, fds)
  66. #define UA_fd_isset(fd, fds) FD_ISSET((UA_SOCKET)fd, fds)
  67. #ifdef UNDER_CE
  68. #define UA_ERRNO WSAGetLastError()
  69. #endif
  70. #define UA_getnameinfo getnameinfo
  71. #define UA_send(sockfd, buf, len, flags) send(sockfd, buf, (int)(len), flags)
  72. #define UA_recv recv
  73. #define UA_sendto(sockfd, buf, len, flags, dest_addr, addrlen) sendto(sockfd, (const char*)(buf), (int)(len), flags, dest_addr, (int) (addrlen))
  74. #define UA_recvfrom(sockfd, buf, len, flags, src_addr, addrlen) recvfrom(sockfd, (char*)(buf), (int)(len), flags, src_addr, addrlen)
  75. #define UA_htonl htonl
  76. #define UA_ntohl ntohl
  77. #define UA_close closesocket
  78. #define UA_select(nfds, readfds, writefds, exceptfds, timeout) select((int)(nfds), readfds, writefds, exceptfds, timeout)
  79. #define UA_shutdown shutdown
  80. #define UA_socket socket
  81. #define UA_bind bind
  82. #define UA_listen listen
  83. #define UA_accept accept
  84. #define UA_connect(sockfd, addr, addrlen) connect(sockfd, addr, (int)(addrlen))
  85. #define UA_getaddrinfo getaddrinfo
  86. #define UA_getsockopt getsockopt
  87. #define UA_setsockopt(sockfd, level, optname, optval, optlen) setsockopt(sockfd, level, optname, (const char*) (optval), optlen)
  88. #define UA_freeaddrinfo freeaddrinfo
  89. #define UA_gethostname gethostname
  90. #define UA_inet_pton InetPton
  91. #ifdef maxStringLength //defined in mingw64
  92. # undef maxStringLength
  93. #endif
  94. #ifndef UA_free
  95. #define UA_free free
  96. #endif
  97. #ifndef UA_malloc
  98. #define UA_malloc malloc
  99. #endif
  100. #ifndef UA_calloc
  101. #define UA_calloc calloc
  102. #endif
  103. #ifndef UA_realloc
  104. #define UA_realloc realloc
  105. #endif
  106. #define UA_snprintf(source, size, string, ...) _snprintf_s(source, size, _TRUNCATE, string, __VA_ARGS__)
  107. #define UA_LOG_SOCKET_ERRNO_WRAP(LOG) { \
  108. LPVOID errno_str = NULL; \
  109. FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
  110. NULL, WSAGetLastError(), \
  111. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
  112. (LPWSTR)&errno_str, \
  113. 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_WEC7_UA_ARCHITECTURE_H_ */
  124. #endif /* UA_ARCHITECTURE_WEC7 */