types.h 7.2 KB

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