types.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #ifndef __types_h
  2. #define __types_h
  3. #ifndef CONFIG
  4. #define CONFIG "config.h"
  5. #endif // CONFIG
  6. #include CONFIG
  7. #define ANDROID_API_LEVEL ANDROID_HELPER1(__ANDROID_API__)
  8. #define ANDROID_HELPER1(s) ANDROID_HELPER2(s)
  9. #define ANDROID_HELPER2(s) #s
  10. #if !defined(_WIN32) && !__CYGWIN__
  11. #define __declspec(x) __attribute__((__visibility__("default")))
  12. #endif
  13. #if !defined(EXTERNAL)
  14. #define EXTERNAL dllimport
  15. #endif
  16. #ifdef __cplusplus
  17. #define EXTERNC extern "C"
  18. #else
  19. #define EXTERNC
  20. #endif
  21. #include <stdlib.h>
  22. #include <limits.h>
  23. #include <stdint.h>
  24. #ifdef __ANDROID__
  25. #include <android/api-level.h>
  26. #endif // __ANDROID__
  27. #ifndef _WIN32
  28. #include <netinet/in.h>
  29. #endif // _WIN32
  30. #if __linux__ // Some versions of uclibc do not define IP_FREEBIND in the correct header file
  31. #ifndef IP_FREEBIND
  32. #define IP_FREEBIND 15
  33. #endif // IP_FREEBIND
  34. #endif // __linux__
  35. #if (IP_BINDANY || IP_FREEBIND || IPV6_BINDANY || IP_NONLOCALOK) && !defined(NO_FREEBIND) && !defined(USE_MSRPC) && !defined(SIMPLE_SOCKETS)
  36. #define HAVE_FREEBIND 1
  37. #endif
  38. #if !defined(NO_GETIFADDRS) && !defined(USE_MSRPC) && !defined(SIMPLE_SOCKETS) && !defined(NO_SOCKETS) && !defined(NO_PRIVATE_IP_DETECT)
  39. #define HAVE_GETIFADDR 1
  40. #endif
  41. #ifndef alloca
  42. #ifdef __GNUC__
  43. #define alloca(x) __builtin_alloca(x)
  44. #endif // __GNUC__
  45. #endif // alloca
  46. #ifndef alloca
  47. #if _MSC_VER
  48. #define alloca _malloca
  49. #endif // _MSC_VER
  50. #endif // alloca
  51. #ifndef alloca
  52. #ifdef __has_builtin // clang feature test
  53. #if __has_builtin(__builtin_alloca)
  54. #define alloca(x) __builtin_alloca(x)
  55. #endif // __has_builtin(__builtin_alloca)
  56. #endif // __has_builtin
  57. #endif // alloca
  58. #ifndef alloca
  59. #include <alloca.h>
  60. #endif
  61. #ifndef __packed
  62. #if _MSC_VER
  63. #define __packed
  64. #else // !_MSC_VER
  65. #define __packed __attribute__((packed))
  66. #endif // !_MSC_VER
  67. #endif
  68. #ifndef __pure
  69. #define __pure __attribute__((pure))
  70. #endif
  71. #ifndef __noreturn
  72. #define __noreturn __attribute__((noreturn))
  73. #endif
  74. #define restrict __restrict
  75. typedef struct __packed
  76. {
  77. uint16_t val[0];
  78. } PACKED16;
  79. typedef struct __packed
  80. {
  81. uint32_t val[0];
  82. } PACKED32;
  83. typedef struct __packed
  84. {
  85. uint64_t val[0];
  86. } PACKED64;
  87. // Extend this type to 16 or 32 bits if more than 254 products appear
  88. typedef uint8_t ProdListIndex_t;
  89. // Deal with Mingw32-w64 C++ header which defines a _countof that is incompatible with vlmcsd
  90. #define vlmcsd_countof(x) ( sizeof(x) / sizeof(x[0]) )
  91. // PATH_MAX is optional in Posix. We use a default of 260 here
  92. #ifndef PATH_MAX
  93. #ifdef _WIN32
  94. #define PATH_MAX MAX_PATH
  95. #else
  96. #define PATH_MAX 260
  97. #endif // _WIN32
  98. #endif // !PATH_MAX
  99. #if PATH_MAX > 260
  100. #define VLMCSD_PATH_MAX 260
  101. #else
  102. #define VLMCSD_PATH_MAX PATH_MAX
  103. #endif
  104. // Synchronization Objects
  105. // Mutexes
  106. #ifdef USE_THREADS
  107. #if !defined(_WIN32) && !defined(__CYGWIN__)
  108. #define lock_mutex(x) pthread_mutex_lock(x)
  109. #define unlock_mutex(x) pthread_mutex_unlock(x)
  110. #else
  111. #define lock_mutex(x) EnterCriticalSection(x)
  112. #define unlock_mutex(x) LeaveCriticalSection(x)
  113. #endif
  114. #else // !USE_THREADS
  115. //defines to nothing
  116. #define lock_mutex(x)
  117. #define unlock_mutex(x)
  118. #endif // !USE_THREADS
  119. // Semaphores
  120. #ifndef _WIN32
  121. #define semaphore_wait(x) sem_wait(x)
  122. #define semaphore_post(x) sem_post(x)
  123. #else // _WIN32
  124. #define semaphore_wait(x) WaitForSingleObject(x, INFINITE)
  125. #define semaphore_post(x) ReleaseSemaphore(x, 1, NULL)
  126. #endif // _WIN32
  127. // Stupid MingW just uses rand() from msvcrt.dll which uses RAND_MAX of 0x7fff
  128. #if RAND_MAX < 0x7fffffff
  129. #define rand32(x) ((uint32_t)((rand(x) << 17) | (rand(x) << 2) | (rand(x) & 3)))
  130. #elif RAND_MAX < 0xffffffff
  131. #define rand32(x) ((uint32_t)((rand(x) << 1) | (rand(x) & 1)))
  132. #else
  133. #define rand32(x) (uint32_t)rand(x)
  134. #endif
  135. #if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(NO_SOCKETS)
  136. #define _NTSERVICE
  137. #endif
  138. #if (defined(__CYGWIN__) || defined(_WIN32) || defined(NO_SOCKETS)) && !defined(NO_SIGHUP)
  139. #define NO_SIGHUP
  140. #endif // (defined(__CYGWIN__) || defined(_WIN32) || defined(NO_SOCKETS)) && !defined(NO_SIGHUP)
  141. #ifdef _WIN32
  142. #ifndef USE_THREADS
  143. #define USE_THREADS
  144. #endif
  145. #endif
  146. #if defined(USE_THREADS)
  147. #define _TLS __thread
  148. #else
  149. #define _TLS
  150. #endif
  151. #define GUID_STRING_LENGTH 36
  152. #if defined(_WIN32)
  153. #ifndef USE_MSRPC
  154. #include <winsock2.h>
  155. #include <ws2tcpip.h>
  156. #endif // USE_MSRPC
  157. #include <windows.h>
  158. //#include <VersionHelpers.h>
  159. typedef char* sockopt_t;
  160. // Map VLMCSD error codes to WSAGetLastError() codes
  161. // Add more if you need them
  162. #define VLMCSD_EADDRINUSE WSAEADDRINUSE
  163. #define VLMCSD_ENODEV WSAENODEV
  164. #define VLMCSD_EADDRNOTAVAIL WSAEADDRNOTAVAIL
  165. #define VLMCSD_EACCES WSAEACCES
  166. #define VLMCSD_EINVAL WSAEINVAL
  167. #define VLMCSD_ENOTSOCK WSAENOTSOCK
  168. #define VLMCSD_EINTR WSAEINTR
  169. #define VLMCSD_EINPROGRESS WSAEINPROGRESS
  170. #define VLMCSD_ECONNABORTED WSAECONNABORTED
  171. #define socket_errno WSAGetLastError()
  172. #define socketclose(x) (closesocket(x))
  173. #define vlmcsd_strerror(x) win_strerror(x)
  174. #define VLMCSD_SHUT_RD SD_RECEIVE
  175. #define VLMCSD_SHUT_WR SD_SEND
  176. #define VLMCSD_SHUT_RDWR SD_BOTH
  177. /* Unknown Winsock error codes */
  178. #define WSAENODEV -1
  179. #elif defined(__CYGWIN__)
  180. #include <windows.h>
  181. // Resolve conflicts between OpenSSL and MS Crypto API
  182. #ifdef _CRYPTO_OPENSSL
  183. #undef OCSP_RESPONSE
  184. #undef X509_NAME
  185. #endif
  186. #else
  187. typedef uint32_t DWORD;
  188. typedef uint16_t WORD;
  189. typedef uint8_t BYTE;
  190. typedef uint16_t WCHAR;
  191. typedef int BOOL;
  192. #define FALSE 0
  193. #define TRUE !0
  194. typedef struct {
  195. DWORD Data1;
  196. WORD Data2;
  197. WORD Data3;
  198. BYTE Data4[8];
  199. } /*__packed*/ GUID;
  200. typedef struct {
  201. DWORD dwLowDateTime;
  202. DWORD dwHighDateTime;
  203. } /*__packed*/ FILETIME;
  204. #endif // defined(__CYGWIN__)
  205. #ifndef _WIN32
  206. // Map VLMCSD error codes to POSIX codes
  207. // Add more if you need them
  208. #define VLMCSD_EADDRINUSE EADDRINUSE
  209. #define VLMCSD_ENODEV ENODEV
  210. #define VLMCSD_EADDRNOTAVAIL EADDRNOTAVAIL
  211. #define VLMCSD_EACCES EACCES
  212. #define VLMCSD_EINVAL EINVAL
  213. #define VLMCSD_ENOTSOCK ENOTSOCK
  214. #define VLMCSD_EINTR EINTR
  215. #define VLMCSD_EINPROGRESS EINPROGRESS
  216. #define VLMCSD_ECONNABORTED ECONNABORTED
  217. typedef void* sockopt_t;
  218. #define _countof(x) ( sizeof(x) / sizeof(x[0]) )
  219. #define SOCKET int
  220. #define INVALID_SOCKET -1
  221. #define socket_errno errno
  222. #define socketclose(x) (close(x))
  223. #define vlmcsd_strerror strerror
  224. #define VLMCSD_SHUT_RD SHUT_RD
  225. #define VLMCSD_SHUT_WR SHUT_WR
  226. #define VLMCSD_SHUT_RDWR SHUT_RDWR
  227. #endif // __MINGW__
  228. #define INVALID_UID ((uid_t)~0)
  229. #define INVALID_GID ((gid_t)~0)
  230. #undef IsEqualGUID
  231. #define IsEqualGUID(a, b) ( !memcmp(a, b, sizeof(GUID)) )
  232. #ifndef __stdcall
  233. #define __stdcall
  234. #endif
  235. #ifndef __cdecl
  236. #define __cdecl
  237. #endif
  238. typedef const char *const * CARGV;
  239. typedef struct {
  240. SOCKET socket;
  241. DWORD RpcAssocGroup;
  242. } CLDATA, *const PCLDATA;
  243. #endif // __types_h