libwebsockets.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2018 Andy Green <[email protected]>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. /** @file */
  22. #ifndef LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
  23. #define LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
  24. #ifdef __cplusplus
  25. #include <cstddef>
  26. #include <cstdarg>
  27. extern "C" {
  28. #else
  29. #include <stdarg.h>
  30. #endif
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include "lws_config.h"
  34. /*
  35. * CARE: everything using cmake defines needs to be below here
  36. */
  37. #if defined(LWS_HAS_INTPTR_T)
  38. #include <stdint.h>
  39. #define lws_intptr_t intptr_t
  40. #else
  41. typedef unsigned long long lws_intptr_t;
  42. #endif
  43. #if defined(WIN32) || defined(_WIN32)
  44. #ifndef WIN32_LEAN_AND_MEAN
  45. #define WIN32_LEAN_AND_MEAN
  46. #endif
  47. #include <winsock2.h>
  48. #include <ws2tcpip.h>
  49. #include <stddef.h>
  50. #include <basetsd.h>
  51. #include <io.h>
  52. #ifndef _WIN32_WCE
  53. #include <fcntl.h>
  54. #else
  55. #define _O_RDONLY 0x0000
  56. #define O_RDONLY _O_RDONLY
  57. #endif
  58. #define LWS_INLINE __inline
  59. #define LWS_VISIBLE
  60. #define LWS_WARN_UNUSED_RESULT
  61. #define LWS_WARN_DEPRECATED
  62. #define LWS_FORMAT(string_index)
  63. #if !defined(LWS_EXTERN)
  64. #ifdef LWS_DLL
  65. #ifdef LWS_INTERNAL
  66. #define LWS_EXTERN extern __declspec(dllexport)
  67. #else
  68. #define LWS_EXTERN extern __declspec(dllimport)
  69. #endif
  70. #else
  71. #define LWS_EXTERN
  72. #endif
  73. #endif
  74. #define LWS_INVALID_FILE INVALID_HANDLE_VALUE
  75. #define LWS_O_RDONLY _O_RDONLY
  76. #define LWS_O_WRONLY _O_WRONLY
  77. #define LWS_O_CREAT _O_CREAT
  78. #define LWS_O_TRUNC _O_TRUNC
  79. #ifndef __func__
  80. #define __func__ __FUNCTION__
  81. #endif
  82. #else /* NOT WIN32 */
  83. #include <unistd.h>
  84. #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
  85. #include <sys/capability.h>
  86. #endif
  87. #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__QNX__) || defined(__OpenBSD__)
  88. #include <sys/socket.h>
  89. #include <netinet/in.h>
  90. #endif
  91. #define LWS_INLINE inline
  92. #define LWS_O_RDONLY O_RDONLY
  93. #define LWS_O_WRONLY O_WRONLY
  94. #define LWS_O_CREAT O_CREAT
  95. #define LWS_O_TRUNC O_TRUNC
  96. #if !defined(LWS_PLAT_OPTEE) && !defined(OPTEE_TA) && !defined(LWS_WITH_ESP32)
  97. #include <poll.h>
  98. #include <netdb.h>
  99. #define LWS_INVALID_FILE -1
  100. #else
  101. #define getdtablesize() (30)
  102. #if defined(LWS_WITH_ESP32)
  103. #define LWS_INVALID_FILE NULL
  104. #else
  105. #define LWS_INVALID_FILE NULL
  106. #endif
  107. #endif
  108. #if defined(__GNUC__)
  109. /* warn_unused_result attribute only supported by GCC 3.4 or later */
  110. #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
  111. #define LWS_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  112. #else
  113. #define LWS_WARN_UNUSED_RESULT
  114. #endif
  115. #define LWS_VISIBLE __attribute__((visibility("default")))
  116. #define LWS_WARN_DEPRECATED __attribute__ ((deprecated))
  117. #define LWS_FORMAT(string_index) __attribute__ ((format(printf, string_index, string_index+1)))
  118. #else
  119. #define LWS_VISIBLE
  120. #define LWS_WARN_UNUSED_RESULT
  121. #define LWS_WARN_DEPRECATED
  122. #define LWS_FORMAT(string_index)
  123. #endif
  124. #if defined(__ANDROID__)
  125. #include <netinet/in.h>
  126. #include <unistd.h>
  127. #define getdtablesize() sysconf(_SC_OPEN_MAX)
  128. #endif
  129. #endif
  130. #if defined(LWS_WITH_LIBEV)
  131. #include <ev.h>
  132. #endif /* LWS_WITH_LIBEV */
  133. #ifdef LWS_WITH_LIBUV
  134. #include <uv.h>
  135. #ifdef LWS_HAVE_UV_VERSION_H
  136. #include <uv-version.h>
  137. #endif
  138. #ifdef LWS_HAVE_NEW_UV_VERSION_H
  139. #include <uv/version.h>
  140. #endif
  141. #endif /* LWS_WITH_LIBUV */
  142. #if defined(LWS_WITH_LIBEVENT)
  143. #include <event2/event.h>
  144. #endif /* LWS_WITH_LIBEVENT */
  145. #ifndef LWS_EXTERN
  146. #define LWS_EXTERN extern
  147. #endif
  148. #ifdef _WIN32
  149. #define random rand
  150. #else
  151. #if !defined(OPTEE_TA)
  152. #include <sys/time.h>
  153. #include <unistd.h>
  154. #endif
  155. #endif
  156. #if defined(LWS_WITH_TLS)
  157. #ifdef USE_WOLFSSL
  158. #ifdef USE_OLD_CYASSL
  159. #ifdef _WIN32
  160. /*
  161. * Include user-controlled settings for windows from
  162. * <wolfssl-root>/IDE/WIN/user_settings.h
  163. */
  164. #include <IDE/WIN/user_settings.h>
  165. #include <cyassl/ctaocrypt/settings.h>
  166. #else
  167. #include <cyassl/options.h>
  168. #endif
  169. #include <cyassl/openssl/ssl.h>
  170. #include <cyassl/error-ssl.h>
  171. #else
  172. #ifdef _WIN32
  173. /*
  174. * Include user-controlled settings for windows from
  175. * <wolfssl-root>/IDE/WIN/user_settings.h
  176. */
  177. #include <IDE/WIN/user_settings.h>
  178. #include <wolfssl/wolfcrypt/settings.h>
  179. #else
  180. #include <wolfssl/options.h>
  181. #endif
  182. #include <wolfssl/openssl/ssl.h>
  183. #include <wolfssl/error-ssl.h>
  184. #endif /* not USE_OLD_CYASSL */
  185. #else
  186. #if defined(LWS_WITH_MBEDTLS)
  187. #if defined(LWS_WITH_ESP32)
  188. /* this filepath is passed to us but without quotes or <> */
  189. #undef MBEDTLS_CONFIG_FILE
  190. #define MBEDTLS_CONFIG_FILE <mbedtls/esp_config.h>
  191. #endif
  192. #include <mbedtls/ssl.h>
  193. #else
  194. #include <openssl/ssl.h>
  195. #if !defined(LWS_WITH_MBEDTLS)
  196. #include <openssl/err.h>
  197. #endif
  198. #endif
  199. #endif /* not USE_WOLFSSL */
  200. #endif
  201. /*
  202. * Helpers for pthread mutex in user code... if lws is built for
  203. * multiple service threads, these resolve to pthread mutex
  204. * operations. In the case LWS_MAX_SMP is 1 (the default), they
  205. * are all NOPs and no pthread type or api is referenced.
  206. */
  207. #if LWS_MAX_SMP > 1
  208. #include <pthread.h>
  209. #define lws_pthread_mutex(name) pthread_mutex_t name;
  210. static LWS_INLINE void
  211. lws_pthread_mutex_init(pthread_mutex_t *lock)
  212. {
  213. pthread_mutex_init(lock, NULL);
  214. }
  215. static LWS_INLINE void
  216. lws_pthread_mutex_destroy(pthread_mutex_t *lock)
  217. {
  218. pthread_mutex_destroy(lock);
  219. }
  220. static LWS_INLINE void
  221. lws_pthread_mutex_lock(pthread_mutex_t *lock)
  222. {
  223. pthread_mutex_lock(lock);
  224. }
  225. static LWS_INLINE void
  226. lws_pthread_mutex_unlock(pthread_mutex_t *lock)
  227. {
  228. pthread_mutex_unlock(lock);
  229. }
  230. #else
  231. #define lws_pthread_mutex(name)
  232. #define lws_pthread_mutex_init(_a)
  233. #define lws_pthread_mutex_destroy(_a)
  234. #define lws_pthread_mutex_lock(_a)
  235. #define lws_pthread_mutex_unlock(_a)
  236. #endif
  237. #define CONTEXT_PORT_NO_LISTEN -1
  238. #define CONTEXT_PORT_NO_LISTEN_SERVER -2
  239. #include <libwebsockets/lws-logs.h>
  240. #include <stddef.h>
  241. #ifndef lws_container_of
  242. #define lws_container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
  243. #endif
  244. struct lws;
  245. typedef int64_t lws_usec_t;
  246. /* api change list for user code to test against */
  247. #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
  248. /* the struct lws_protocols has the id field present */
  249. #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
  250. /* you can call lws_get_peer_write_allowance */
  251. #define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
  252. /* extra parameter introduced in 917f43ab821 */
  253. #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
  254. /* File operations stuff exists */
  255. #define LWS_FEATURE_FOPS
  256. #if defined(_WIN32)
  257. #if !defined(LWS_WIN32_HANDLE_TYPES)
  258. typedef SOCKET lws_sockfd_type;
  259. typedef HANDLE lws_filefd_type;
  260. #endif
  261. struct lws_pollfd {
  262. lws_sockfd_type fd; /**< file descriptor */
  263. SHORT events; /**< which events to respond to */
  264. SHORT revents; /**< which events happened */
  265. };
  266. #define LWS_POLLHUP (FD_CLOSE)
  267. #define LWS_POLLIN (FD_READ | FD_ACCEPT)
  268. #define LWS_POLLOUT (FD_WRITE)
  269. #else
  270. #if defined(LWS_WITH_ESP32)
  271. #include <libwebsockets/lws-esp32.h>
  272. #else
  273. typedef int lws_sockfd_type;
  274. typedef int lws_filefd_type;
  275. #endif
  276. #define lws_pollfd pollfd
  277. #define LWS_POLLHUP (POLLHUP|POLLERR)
  278. #define LWS_POLLIN (POLLIN)
  279. #define LWS_POLLOUT (POLLOUT)
  280. #endif
  281. #if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
  282. /* ... */
  283. #define ssize_t SSIZE_T
  284. #endif
  285. #if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
  286. #include <sys/types.h>
  287. #include <sys/stat.h>
  288. #endif
  289. #if defined(LWS_HAVE_STDINT_H)
  290. #include <stdint.h>
  291. #else
  292. #if defined(WIN32) || defined(_WIN32)
  293. /* !!! >:-[ */
  294. typedef unsigned __int32 uint32_t;
  295. typedef unsigned __int16 uint16_t;
  296. typedef unsigned __int8 uint8_t;
  297. #else
  298. typedef unsigned int uint32_t;
  299. typedef unsigned short uint16_t;
  300. typedef unsigned char uint8_t;
  301. #endif
  302. #endif
  303. typedef unsigned long long lws_filepos_t;
  304. typedef long long lws_fileofs_t;
  305. typedef uint32_t lws_fop_flags_t;
  306. /** struct lws_pollargs - argument structure for all external poll related calls
  307. * passed in via 'in' */
  308. struct lws_pollargs {
  309. lws_sockfd_type fd; /**< applicable socket descriptor */
  310. int events; /**< the new event mask */
  311. int prev_events; /**< the previous event mask */
  312. };
  313. struct lws_extension; /* needed even with ws exts disabled for create context */
  314. struct lws_token_limits;
  315. struct lws_context;
  316. struct lws_tokens;
  317. struct lws_vhost;
  318. struct lws;
  319. #include <libwebsockets/lws-ws-close.h>
  320. #include <libwebsockets/lws-callbacks.h>
  321. #include <libwebsockets/lws-ws-state.h>
  322. #include <libwebsockets/lws-ws-ext.h>
  323. #include <libwebsockets/lws-protocols-plugins.h>
  324. #include <libwebsockets/lws-plugin-generic-sessions.h>
  325. #include <libwebsockets/lws-context-vhost.h>
  326. #include <libwebsockets/lws-client.h>
  327. #include <libwebsockets/lws-http.h>
  328. #include <libwebsockets/lws-spa.h>
  329. #include <libwebsockets/lws-purify.h>
  330. #include <libwebsockets/lws-timeout-timer.h>
  331. #include <libwebsockets/lws-service.h>
  332. #include <libwebsockets/lws-write.h>
  333. #include <libwebsockets/lws-writeable.h>
  334. #include <libwebsockets/lws-adopt.h>
  335. #include <libwebsockets/lws-network-helper.h>
  336. #include <libwebsockets/lws-misc.h>
  337. #include <libwebsockets/lws-ring.h>
  338. #include <libwebsockets/lws-sha1-base64.h>
  339. #include <libwebsockets/lws-x509.h>
  340. #include <libwebsockets/lws-cgi.h>
  341. #include <libwebsockets/lws-vfs.h>
  342. #include <libwebsockets/lws-lejp.h>
  343. #include <libwebsockets/lws-stats.h>
  344. #include <libwebsockets/lws-threadpool.h>
  345. #include <libwebsockets/lws-tokenize.h>
  346. #include <libwebsockets/lws-lwsac.h>
  347. #include <libwebsockets/lws-fts.h>
  348. #include <libwebsockets/lws-diskcache.h>
  349. #if defined(LWS_WITH_TLS)
  350. #if defined(LWS_WITH_MBEDTLS)
  351. #include <mbedtls/sha1.h>
  352. #include <mbedtls/sha256.h>
  353. #include <mbedtls/sha512.h>
  354. #endif
  355. #include <libwebsockets/lws-genhash.h>
  356. #include <libwebsockets/lws-genrsa.h>
  357. #include <libwebsockets/lws-jwk.h>
  358. #include <libwebsockets/lws-jws.h>
  359. #endif
  360. #ifdef __cplusplus
  361. }
  362. #endif
  363. #endif