private-libwebsockets.h 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2016 Andy Green <andy@warmcat.com>
  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. #include "lws_config.h"
  22. #include "lws_config_private.h"
  23. #if defined(LWS_WITH_CGI) && defined(LWS_HAVE_VFORK)
  24. #define _GNU_SOURCE
  25. #endif
  26. #ifdef LWS_HAVE_SYS_TYPES_H
  27. #include <sys/types.h>
  28. #endif
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <time.h>
  33. #include <ctype.h>
  34. #include <limits.h>
  35. #include <stdarg.h>
  36. #if defined(LWS_WITH_ESP8266)
  37. #include <user_interface.h>
  38. #define assert(n)
  39. /* rom-provided stdc functions for free, ensure use these instead of libc ones */
  40. int ets_vsprintf(char *str, const char *format, va_list argptr);
  41. int ets_vsnprintf(char *buffer, size_t sizeOfBuffer, const char *format, va_list argptr);
  42. int ets_snprintf(char *str, size_t size, const char *format, ...);
  43. int ets_sprintf(char *str, const char *format, ...);
  44. int os_printf_plus(const char *format, ...);
  45. #undef malloc
  46. #undef realloc
  47. #undef free
  48. void *pvPortMalloc(size_t s, const char *f, int line);
  49. #define malloc(s) pvPortMalloc(s, "", 0)
  50. void *pvPortRealloc(void *p, size_t s, const char *f, int line);
  51. #define realloc(p, s) pvPortRealloc(p, s, "", 0)
  52. void vPortFree(void *p, const char *f, int line);
  53. #define free(p) vPortFree(p, "", 0)
  54. #undef memcpy
  55. void *ets_memcpy(void *dest, const void *src, size_t n);
  56. #define memcpy ets_memcpy
  57. void *ets_memset(void *dest, int v, size_t n);
  58. #define memset ets_memset
  59. char *ets_strcpy(char *dest, const char *src);
  60. #define strcpy ets_strcpy
  61. char *ets_strncpy(char *dest, const char *src, size_t n);
  62. #define strncpy ets_strncpy
  63. char *ets_strstr(const char *haystack, const char *needle);
  64. #define strstr ets_strstr
  65. int ets_strcmp(const char *s1, const char *s2);
  66. int ets_strncmp(const char *s1, const char *s2, size_t n);
  67. #define strcmp ets_strcmp
  68. #define strncmp ets_strncmp
  69. size_t ets_strlen(const char *s);
  70. #define strlen ets_strlen
  71. void *ets_memmove(void *dest, const void *src, size_t n);
  72. #define memmove ets_memmove
  73. char *ets_strchr(const char *s, int c);
  74. #define strchr_ets_strchr
  75. #undef _DEBUG
  76. #include <osapi.h>
  77. #else
  78. #define STORE_IN_ROM
  79. #include <assert.h>
  80. #endif
  81. #if LWS_MAX_SMP > 1
  82. #include <pthread.h>
  83. #endif
  84. #ifdef LWS_HAVE_SYS_STAT_H
  85. #include <sys/stat.h>
  86. #endif
  87. #if defined(WIN32) || defined(_WIN32)
  88. #if (WINVER < 0x0501)
  89. #undef WINVER
  90. #undef _WIN32_WINNT
  91. #define WINVER 0x0501
  92. #define _WIN32_WINNT WINVER
  93. #endif
  94. #define LWS_NO_DAEMONIZE
  95. #define LWS_ERRNO WSAGetLastError()
  96. #define LWS_EAGAIN WSAEWOULDBLOCK
  97. #define LWS_EALREADY WSAEALREADY
  98. #define LWS_EINPROGRESS WSAEINPROGRESS
  99. #define LWS_EINTR WSAEINTR
  100. #define LWS_EISCONN WSAEISCONN
  101. #define LWS_EWOULDBLOCK WSAEWOULDBLOCK
  102. #define MSG_NOSIGNAL 0
  103. #define SHUT_RDWR SD_BOTH
  104. #define SOL_TCP IPPROTO_TCP
  105. #define SHUT_WR SD_SEND
  106. #define compatible_close(fd) closesocket(fd)
  107. #define lws_set_blocking_send(wsi) wsi->sock_send_blocking = 1
  108. #define lws_socket_is_valid(x) (!!x)
  109. #define LWS_SOCK_INVALID 0
  110. #include <winsock2.h>
  111. #include <ws2tcpip.h>
  112. #include <windows.h>
  113. #include <tchar.h>
  114. #ifdef LWS_HAVE_IN6ADDR_H
  115. #include <in6addr.h>
  116. #endif
  117. #include <mstcpip.h>
  118. #include <io.h>
  119. #ifndef __func__
  120. #define __func__ __FUNCTION__
  121. #endif
  122. #if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(_WIN32_WCE)
  123. #define vsnprintf _vsnprintf
  124. #else
  125. #ifdef LWS_HAVE__VSNPRINTF
  126. #define vsnprintf _vsnprintf
  127. #endif
  128. #endif
  129. #ifdef LWS_HAVE__SNPRINTF
  130. #define lws_snprintf _snprintf
  131. #endif
  132. #else /* not windows --> */
  133. #include <fcntl.h>
  134. #include <strings.h>
  135. #include <unistd.h>
  136. #include <sys/types.h>
  137. #ifndef MBED_OPERATORS
  138. #ifndef __cplusplus
  139. #include <errno.h>
  140. #endif
  141. #include <netdb.h>
  142. #include <signal.h>
  143. #ifdef LWS_WITH_ESP8266
  144. #include <sockets.h>
  145. #define vsnprintf ets_vsnprintf
  146. #define snprintf ets_snprintf
  147. #define sprintf ets_sprintf
  148. #else
  149. #include <sys/socket.h>
  150. #endif
  151. #ifdef LWS_WITH_HTTP_PROXY
  152. #include <hubbub/hubbub.h>
  153. #include <hubbub/parser.h>
  154. #endif
  155. #if defined(LWS_BUILTIN_GETIFADDRS)
  156. #include <getifaddrs.h>
  157. #else
  158. #if !defined(LWS_WITH_ESP8266)
  159. #include <ifaddrs.h>
  160. #endif
  161. #endif
  162. #if defined (__ANDROID__)
  163. #include <syslog.h>
  164. #include <sys/resource.h>
  165. #elif defined (__sun)
  166. #include <syslog.h>
  167. #else
  168. #if !defined(LWS_WITH_ESP8266)
  169. #include <sys/syslog.h>
  170. #endif
  171. #endif
  172. #include <netdb.h>
  173. #if !defined(LWS_WITH_ESP8266)
  174. #include <sys/mman.h>
  175. #include <sys/un.h>
  176. #include <netinet/in.h>
  177. #include <netinet/tcp.h>
  178. #include <arpa/inet.h>
  179. #include <poll.h>
  180. #endif
  181. #ifdef LWS_USE_LIBEV
  182. #include <ev.h>
  183. #endif
  184. #ifdef LWS_USE_LIBUV
  185. #include <uv.h>
  186. #endif
  187. #endif /* MBED */
  188. #ifndef LWS_NO_FORK
  189. #ifdef LWS_HAVE_SYS_PRCTL_H
  190. #include <sys/prctl.h>
  191. #endif
  192. #endif
  193. #include <sys/time.h>
  194. #define LWS_ERRNO errno
  195. #define LWS_EAGAIN EAGAIN
  196. #define LWS_EALREADY EALREADY
  197. #define LWS_EINPROGRESS EINPROGRESS
  198. #define LWS_EINTR EINTR
  199. #define LWS_EISCONN EISCONN
  200. #define LWS_EWOULDBLOCK EWOULDBLOCK
  201. #define lws_set_blocking_send(wsi)
  202. #if defined(MBED_OPERATORS) || defined(LWS_WITH_ESP8266)
  203. #define lws_socket_is_valid(x) ((x) != NULL)
  204. #define LWS_SOCK_INVALID (NULL)
  205. struct lws;
  206. const char *
  207. lws_plat_get_peer_simple(struct lws *wsi, char *name, int namelen);
  208. #else
  209. #define lws_socket_is_valid(x) (x >= 0)
  210. #define LWS_SOCK_INVALID (-1)
  211. #endif
  212. #endif
  213. #ifndef LWS_HAVE_BZERO
  214. #ifndef bzero
  215. #define bzero(b, len) (memset((b), '\0', (len)), (void) 0)
  216. #endif
  217. #endif
  218. #ifndef LWS_HAVE_STRERROR
  219. #define strerror(x) ""
  220. #endif
  221. #ifdef LWS_OPENSSL_SUPPORT
  222. #ifdef USE_WOLFSSL
  223. #ifdef USE_OLD_CYASSL
  224. #include <cyassl/openssl/ssl.h>
  225. #include <cyassl/error-ssl.h>
  226. #else
  227. #include <wolfssl/openssl/ssl.h>
  228. #include <wolfssl/error-ssl.h>
  229. #endif /* not USE_OLD_CYASSL */
  230. #else
  231. #if defined(LWS_USE_POLARSSL)
  232. #include <polarssl/ssl.h>
  233. #include <polarssl/error.h>
  234. #include <polarssl/md5.h>
  235. #include <polarssl/sha1.h>
  236. #include <polarssl/ecdh.h>
  237. #define SSL_ERROR_WANT_READ POLARSSL_ERR_NET_WANT_READ
  238. #define SSL_ERROR_WANT_WRITE POLARSSL_ERR_NET_WANT_WRITE
  239. #define OPENSSL_VERSION_NUMBER 0x10002000L
  240. #else
  241. #if defined(LWS_USE_MBEDTLS)
  242. #include <mbedtls/ssl.h>
  243. #include <mbedtls/error.h>
  244. #include <mbedtls/md5.h>
  245. #include <mbedtls/sha1.h>
  246. #include <mbedtls/ecdh.h>
  247. #else
  248. #include <openssl/ssl.h>
  249. #include <openssl/evp.h>
  250. #include <openssl/err.h>
  251. #include <openssl/md5.h>
  252. #include <openssl/sha.h>
  253. #ifdef LWS_HAVE_OPENSSL_ECDH_H
  254. #include <openssl/ecdh.h>
  255. #endif
  256. #include <openssl/x509v3.h>
  257. #endif /* not USE_MBEDTLS */
  258. #endif /* not USE_POLARSSL */
  259. #endif /* not USE_WOLFSSL */
  260. #endif
  261. #include "libwebsockets.h"
  262. #if defined(WIN32) || defined(_WIN32)
  263. #else
  264. static inline int compatible_close(int fd) { return close(fd); }
  265. #endif
  266. #if defined(MBED_OPERATORS)
  267. #undef compatible_close
  268. #define compatible_close(fd) mbed3_delete_tcp_stream_socket(fd)
  269. #ifndef BIG_ENDIAN
  270. #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
  271. #endif
  272. #ifndef LITTLE_ENDIAN
  273. #define LITTLE_ENDIAN 1234
  274. #endif
  275. #ifndef BYTE_ORDER
  276. #define BYTE_ORDER LITTLE_ENDIAN
  277. #endif
  278. #endif
  279. #if defined(LWS_WITH_ESP8266)
  280. #undef compatible_close
  281. #define compatible_close(fd) { fd->state=ESPCONN_CLOSE; espconn_delete(fd); }
  282. lws_sockfd_type
  283. esp8266_create_tcp_stream_socket(void);
  284. void
  285. esp8266_tcp_stream_bind(lws_sockfd_type fd, int port, struct lws *wsi);
  286. #ifndef BIG_ENDIAN
  287. #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
  288. #endif
  289. #ifndef LITTLE_ENDIAN
  290. #define LITTLE_ENDIAN 1234
  291. #endif
  292. #ifndef BYTE_ORDER
  293. #define BYTE_ORDER LITTLE_ENDIAN
  294. #endif
  295. #endif
  296. #if defined(WIN32) || defined(_WIN32)
  297. #ifndef BIG_ENDIAN
  298. #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc) */
  299. #endif
  300. #ifndef LITTLE_ENDIAN
  301. #define LITTLE_ENDIAN 1234
  302. #endif
  303. #ifndef BYTE_ORDER
  304. #define BYTE_ORDER LITTLE_ENDIAN
  305. #endif
  306. #ifndef u_int64_t
  307. typedef unsigned __int64 u_int64_t;
  308. #endif
  309. #undef __P
  310. #ifndef __P
  311. #if __STDC__
  312. #define __P(protos) protos
  313. #else
  314. #define __P(protos) ()
  315. #endif
  316. #endif
  317. #else
  318. #include <sys/stat.h>
  319. #include <sys/time.h>
  320. #if defined(__APPLE__)
  321. #include <machine/endian.h>
  322. #elif defined(__FreeBSD__)
  323. #include <sys/endian.h>
  324. #elif defined(__linux__)
  325. #include <endian.h>
  326. #endif
  327. #ifdef __cplusplus
  328. extern "C" {
  329. #endif
  330. #if defined(__QNX__)
  331. #include <gulliver.h>
  332. #if defined(__LITTLEENDIAN__)
  333. #define BYTE_ORDER __LITTLEENDIAN__
  334. #define LITTLE_ENDIAN __LITTLEENDIAN__
  335. #define BIG_ENDIAN 4321 /* to show byte order (taken from gcc); for suppres warning that BIG_ENDIAN is not defined. */
  336. #endif
  337. #if defined(__BIGENDIAN__)
  338. #define BYTE_ORDER __BIGENDIAN__
  339. #define LITTLE_ENDIAN 1234 /* to show byte order (taken from gcc); for suppres warning that LITTLE_ENDIAN is not defined. */
  340. #define BIG_ENDIAN __BIGENDIAN__
  341. #endif
  342. #endif
  343. #if defined(__sun) && defined(__GNUC__)
  344. # define BYTE_ORDER __BYTE_ORDER__
  345. # define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
  346. # define BIG_ENDIAN __ORDER_BIG_ENDIAN__
  347. #endif
  348. #if !defined(BYTE_ORDER)
  349. # define BYTE_ORDER __BYTE_ORDER
  350. #endif
  351. #if !defined(LITTLE_ENDIAN)
  352. # define LITTLE_ENDIAN __LITTLE_ENDIAN
  353. #endif
  354. #if !defined(BIG_ENDIAN)
  355. # define BIG_ENDIAN __BIG_ENDIAN
  356. #endif
  357. #endif
  358. /*
  359. * Mac OSX as well as iOS do not define the MSG_NOSIGNAL flag,
  360. * but happily have something equivalent in the SO_NOSIGPIPE flag.
  361. */
  362. #ifdef __APPLE__
  363. #define MSG_NOSIGNAL SO_NOSIGPIPE
  364. #endif
  365. /*
  366. * Solaris 11.X only supports POSIX 2001, MSG_NOSIGNAL appears in
  367. * POSIX 2008.
  368. */
  369. #ifdef __sun
  370. #define MSG_NOSIGNAL 0
  371. #endif
  372. #ifdef _WIN32
  373. #ifndef FD_HASHTABLE_MODULUS
  374. #define FD_HASHTABLE_MODULUS 32
  375. #endif
  376. #endif
  377. #ifndef LWS_DEF_HEADER_LEN
  378. #define LWS_DEF_HEADER_LEN 4096
  379. #endif
  380. #ifndef LWS_DEF_HEADER_POOL
  381. #define LWS_DEF_HEADER_POOL 4
  382. #endif
  383. #ifndef LWS_MAX_PROTOCOLS
  384. #define LWS_MAX_PROTOCOLS 5
  385. #endif
  386. #ifndef LWS_MAX_EXTENSIONS_ACTIVE
  387. #define LWS_MAX_EXTENSIONS_ACTIVE 2
  388. #endif
  389. #ifndef LWS_MAX_EXT_OFFERS
  390. #define LWS_MAX_EXT_OFFERS 8
  391. #endif
  392. #ifndef SPEC_LATEST_SUPPORTED
  393. #define SPEC_LATEST_SUPPORTED 13
  394. #endif
  395. #ifndef AWAITING_TIMEOUT
  396. #define AWAITING_TIMEOUT 20
  397. #endif
  398. #ifndef CIPHERS_LIST_STRING
  399. #define CIPHERS_LIST_STRING "DEFAULT"
  400. #endif
  401. #ifndef LWS_SOMAXCONN
  402. #define LWS_SOMAXCONN SOMAXCONN
  403. #endif
  404. #define MAX_WEBSOCKET_04_KEY_LEN 128
  405. #ifndef SYSTEM_RANDOM_FILEPATH
  406. #define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
  407. #endif
  408. enum lws_websocket_opcodes_07 {
  409. LWSWSOPC_CONTINUATION = 0,
  410. LWSWSOPC_TEXT_FRAME = 1,
  411. LWSWSOPC_BINARY_FRAME = 2,
  412. LWSWSOPC_NOSPEC__MUX = 7,
  413. /* control extensions 8+ */
  414. LWSWSOPC_CLOSE = 8,
  415. LWSWSOPC_PING = 9,
  416. LWSWSOPC_PONG = 0xa,
  417. };
  418. enum lws_connection_states {
  419. LWSS_HTTP,
  420. LWSS_HTTP_ISSUING_FILE,
  421. LWSS_HTTP_HEADERS,
  422. LWSS_HTTP_BODY,
  423. LWSS_DEAD_SOCKET,
  424. LWSS_ESTABLISHED,
  425. LWSS_CLIENT_HTTP_ESTABLISHED,
  426. LWSS_CLIENT_UNCONNECTED,
  427. LWSS_RETURNED_CLOSE_ALREADY,
  428. LWSS_AWAITING_CLOSE_ACK,
  429. LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE,
  430. LWSS_SHUTDOWN,
  431. LWSS_HTTP2_AWAIT_CLIENT_PREFACE,
  432. LWSS_HTTP2_ESTABLISHED_PRE_SETTINGS,
  433. LWSS_HTTP2_ESTABLISHED,
  434. LWSS_CGI,
  435. };
  436. enum http_version {
  437. HTTP_VERSION_1_0,
  438. HTTP_VERSION_1_1,
  439. HTTP_VERSION_2
  440. };
  441. enum http_connection_type {
  442. HTTP_CONNECTION_CLOSE,
  443. HTTP_CONNECTION_KEEP_ALIVE
  444. };
  445. enum lws_pending_protocol_send {
  446. LWS_PPS_NONE,
  447. LWS_PPS_HTTP2_MY_SETTINGS,
  448. LWS_PPS_HTTP2_ACK_SETTINGS,
  449. LWS_PPS_HTTP2_PONG,
  450. };
  451. enum lws_rx_parse_state {
  452. LWS_RXPS_NEW,
  453. LWS_RXPS_04_mask_1,
  454. LWS_RXPS_04_mask_2,
  455. LWS_RXPS_04_mask_3,
  456. LWS_RXPS_04_FRAME_HDR_1,
  457. LWS_RXPS_04_FRAME_HDR_LEN,
  458. LWS_RXPS_04_FRAME_HDR_LEN16_2,
  459. LWS_RXPS_04_FRAME_HDR_LEN16_1,
  460. LWS_RXPS_04_FRAME_HDR_LEN64_8,
  461. LWS_RXPS_04_FRAME_HDR_LEN64_7,
  462. LWS_RXPS_04_FRAME_HDR_LEN64_6,
  463. LWS_RXPS_04_FRAME_HDR_LEN64_5,
  464. LWS_RXPS_04_FRAME_HDR_LEN64_4,
  465. LWS_RXPS_04_FRAME_HDR_LEN64_3,
  466. LWS_RXPS_04_FRAME_HDR_LEN64_2,
  467. LWS_RXPS_04_FRAME_HDR_LEN64_1,
  468. LWS_RXPS_07_COLLECT_FRAME_KEY_1,
  469. LWS_RXPS_07_COLLECT_FRAME_KEY_2,
  470. LWS_RXPS_07_COLLECT_FRAME_KEY_3,
  471. LWS_RXPS_07_COLLECT_FRAME_KEY_4,
  472. LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED
  473. };
  474. #define LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP 32
  475. enum connection_mode {
  476. LWSCM_HTTP_SERVING,
  477. LWSCM_HTTP_SERVING_ACCEPTED, /* actual HTTP service going on */
  478. LWSCM_PRE_WS_SERVING_ACCEPT,
  479. LWSCM_WS_SERVING,
  480. LWSCM_WS_CLIENT,
  481. LWSCM_HTTP2_SERVING,
  482. /* transient, ssl delay hiding */
  483. LWSCM_SSL_ACK_PENDING,
  484. LWSCM_SSL_INIT,
  485. /* special internal types */
  486. LWSCM_SERVER_LISTENER,
  487. LWSCM_CGI, /* stdin, stdout, stderr for another cgi master wsi */
  488. /* HTTP Client related */
  489. LWSCM_HTTP_CLIENT = LWSCM_FLAG_IMPLIES_CALLBACK_CLOSED_CLIENT_HTTP,
  490. LWSCM_HTTP_CLIENT_ACCEPTED, /* actual HTTP service going on */
  491. LWSCM_WSCL_WAITING_CONNECT,
  492. LWSCM_WSCL_WAITING_PROXY_REPLY,
  493. LWSCM_WSCL_ISSUE_HANDSHAKE,
  494. LWSCM_WSCL_ISSUE_HANDSHAKE2,
  495. LWSCM_WSCL_ISSUE_HTTP_BODY,
  496. LWSCM_WSCL_WAITING_SSL,
  497. LWSCM_WSCL_WAITING_SERVER_REPLY,
  498. LWSCM_WSCL_WAITING_EXTENSION_CONNECT,
  499. LWSCM_WSCL_PENDING_CANDIDATE_CHILD,
  500. /****** add new things just above ---^ ******/
  501. };
  502. enum {
  503. LWS_RXFLOW_ALLOW = (1 << 0),
  504. LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
  505. };
  506. /* this is not usable directly by user code any more, lws_close_reason() */
  507. #define LWS_WRITE_CLOSE 4
  508. struct lws_protocols;
  509. struct lws;
  510. #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
  511. struct lws_io_watcher {
  512. #ifdef LWS_USE_LIBEV
  513. ev_io ev_watcher;
  514. #endif
  515. #ifdef LWS_USE_LIBUV
  516. uv_poll_t uv_watcher;
  517. #endif
  518. struct lws_context *context;
  519. };
  520. struct lws_signal_watcher {
  521. #ifdef LWS_USE_LIBEV
  522. ev_signal ev_watcher;
  523. #endif
  524. #ifdef LWS_USE_LIBUV
  525. uv_signal_t uv_watcher;
  526. #endif
  527. struct lws_context *context;
  528. };
  529. #endif
  530. #ifdef _WIN32
  531. #define LWS_FD_HASH(fd) ((fd ^ (fd >> 8) ^ (fd >> 16)) % FD_HASHTABLE_MODULUS)
  532. struct lws_fd_hashtable {
  533. struct lws **wsi;
  534. int length;
  535. };
  536. #endif
  537. /*
  538. * This is totally opaque to code using the library. It's exported as a
  539. * forward-reference pointer-only declaration; the user can use the pointer with
  540. * other APIs to get information out of it.
  541. */
  542. struct lws_fragments {
  543. unsigned int offset;
  544. unsigned short len;
  545. unsigned char nfrag; /* which ah->frag[] continues this content, or 0 */
  546. };
  547. /*
  548. * these are assigned from a pool held in the context.
  549. * Both client and server mode uses them for http header analysis
  550. */
  551. struct allocated_headers {
  552. struct lws *wsi; /* owner */
  553. char *data; /* prepared by context init to point to dedicated storage */
  554. /*
  555. * the randomly ordered fragments, indexed by frag_index and
  556. * lws_fragments->nfrag for continuation.
  557. */
  558. struct lws_fragments frags[WSI_TOKEN_COUNT * 2];
  559. time_t assigned;
  560. /*
  561. * for each recognized token, frag_index says which frag[] his data
  562. * starts in (0 means the token did not appear)
  563. * the actual header data gets dumped as it comes in, into data[]
  564. */
  565. unsigned char frag_index[WSI_TOKEN_COUNT];
  566. unsigned char rx[2048];
  567. unsigned int rxpos;
  568. unsigned int rxlen;
  569. unsigned int pos;
  570. #ifndef LWS_NO_CLIENT
  571. char initial_handshake_hash_base64[30];
  572. #endif
  573. unsigned char in_use;
  574. unsigned char nfrag;
  575. };
  576. /*
  577. * so we can have n connections being serviced simultaneously,
  578. * these things need to be isolated per-thread.
  579. */
  580. struct lws_context_per_thread {
  581. #if LWS_MAX_SMP > 1
  582. pthread_mutex_t lock;
  583. #endif
  584. struct lws_pollfd *fds;
  585. #if defined(LWS_WITH_ESP8266)
  586. struct lws **lws_vs_fds_index;
  587. #endif
  588. struct lws *rx_draining_ext_list;
  589. struct lws *tx_draining_ext_list;
  590. struct lws *timeout_list;
  591. #ifdef LWS_USE_LIBUV
  592. struct lws_context *context;
  593. #endif
  594. #ifdef LWS_WITH_CGI
  595. struct lws_cgi *cgi_list;
  596. #endif
  597. void *http_header_data;
  598. struct allocated_headers *ah_pool;
  599. struct lws *ah_wait_list;
  600. int ah_wait_list_length;
  601. #ifdef LWS_OPENSSL_SUPPORT
  602. struct lws *pending_read_list; /* linked list */
  603. #endif
  604. #ifndef LWS_NO_SERVER
  605. struct lws *wsi_listening;
  606. #endif
  607. #if defined(LWS_USE_LIBEV)
  608. struct ev_loop *io_loop_ev;
  609. #endif
  610. #if defined(LWS_USE_LIBUV)
  611. uv_loop_t *io_loop_uv;
  612. uv_signal_t signals[8];
  613. uv_timer_t uv_timeout_watcher;
  614. uv_idle_t uv_idle;
  615. #endif
  616. #if defined(LWS_USE_LIBEV)
  617. struct lws_io_watcher w_accept;
  618. #endif
  619. #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
  620. struct lws_signal_watcher w_sigint;
  621. unsigned char ev_loop_foreign:1;
  622. #endif
  623. unsigned long count_conns;
  624. /*
  625. * usable by anything in the service code, but only if the scope
  626. * does not last longer than the service action (since next service
  627. * of any socket can likewise use it and overwrite)
  628. */
  629. unsigned char *serv_buf;
  630. #ifdef _WIN32
  631. WSAEVENT *events;
  632. #else
  633. lws_sockfd_type dummy_pipe_fds[2];
  634. #endif
  635. unsigned int fds_count;
  636. short ah_count_in_use;
  637. unsigned char tid;
  638. unsigned char lock_depth;
  639. };
  640. /*
  641. * virtual host -related context information
  642. * vhostwide SSL context
  643. * vhostwide proxy
  644. *
  645. * hierarchy:
  646. *
  647. * context -> vhost -> wsi
  648. *
  649. * incoming connection non-SSL vhost binding:
  650. *
  651. * listen socket -> wsi -> select vhost after first headers
  652. *
  653. * incoming connection SSL vhost binding:
  654. *
  655. * SSL SNI -> wsi -> bind after SSL negotiation
  656. */
  657. struct lws_vhost {
  658. #if !defined(LWS_WITH_ESP8266)
  659. char http_proxy_address[128];
  660. char proxy_basic_auth_token[128];
  661. #endif
  662. #if defined(LWS_WITH_ESP8266)
  663. /* listen sockets need a place to hang their hat */
  664. esp_tcp tcp;
  665. #endif
  666. struct lws_context *context;
  667. struct lws_vhost *vhost_next;
  668. const struct lws_http_mount *mount_list;
  669. struct lws *lserv_wsi;
  670. const char *name;
  671. const char *iface;
  672. const struct lws_protocols *protocols;
  673. void **protocol_vh_privs;
  674. const struct lws_protocol_vhost_options *pvo;
  675. const struct lws_protocol_vhost_options *headers;
  676. struct lws **same_vh_protocol_list;
  677. #ifdef LWS_OPENSSL_SUPPORT
  678. SSL_CTX *ssl_ctx;
  679. SSL_CTX *ssl_client_ctx;
  680. #endif
  681. #ifndef LWS_NO_EXTENSIONS
  682. const struct lws_extension *extensions;
  683. #endif
  684. unsigned long long rx, tx;
  685. unsigned long conn, trans, ws_upgrades, http2_upgrades;
  686. int listen_port;
  687. unsigned int http_proxy_port;
  688. unsigned int options;
  689. int count_protocols;
  690. int ka_time;
  691. int ka_probes;
  692. int ka_interval;
  693. int keepalive_timeout;
  694. #ifdef LWS_WITH_ACCESS_LOG
  695. int log_fd;
  696. #endif
  697. #ifdef LWS_OPENSSL_SUPPORT
  698. int use_ssl;
  699. int allow_non_ssl_on_ssl_port;
  700. unsigned int user_supplied_ssl_ctx:1;
  701. #endif
  702. unsigned char default_protocol_index;
  703. };
  704. /*
  705. * the rest is managed per-context, that includes
  706. *
  707. * - processwide single fd -> wsi lookup
  708. * - contextwide headers pool
  709. */
  710. struct lws_context {
  711. time_t last_timeout_check_s;
  712. time_t last_ws_ping_pong_check_s;
  713. time_t time_up;
  714. struct lws_plat_file_ops fops;
  715. struct lws_context_per_thread pt[LWS_MAX_SMP];
  716. #ifdef _WIN32
  717. /* different implementation between unix and windows */
  718. struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
  719. #else
  720. #if defined(LWS_WITH_ESP8266)
  721. struct espconn **connpool; /* .reverse points to the wsi */
  722. void *rxd;
  723. int rxd_len;
  724. os_timer_t to_timer;
  725. #else
  726. struct lws **lws_lookup; /* fd to wsi */
  727. #endif
  728. #endif
  729. struct lws_vhost *vhost_list;
  730. struct lws_plugin *plugin_list;
  731. const struct lws_token_limits *token_limits;
  732. void *user_space;
  733. const char *server_string;
  734. #if defined(LWS_USE_LIBEV)
  735. lws_ev_signal_cb_t * lws_ev_sigint_cb;
  736. #endif
  737. #if defined(LWS_USE_LIBUV)
  738. uv_signal_cb lws_uv_sigint_cb;
  739. #endif
  740. char canonical_hostname[128];
  741. #ifdef LWS_LATENCY
  742. unsigned long worst_latency;
  743. char worst_latency_info[256];
  744. #endif
  745. int max_fds;
  746. #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
  747. int use_ev_sigint;
  748. #endif
  749. int started_with_parent;
  750. int uid, gid;
  751. int fd_random;
  752. #ifdef LWS_OPENSSL_SUPPORT
  753. #define lws_ssl_anybody_has_buffered_read(w) \
  754. (w->vhost->use_ssl && \
  755. w->context->pt[(int)w->tsi].pending_read_list)
  756. #define lws_ssl_anybody_has_buffered_read_tsi(c, t) \
  757. (/*c->use_ssl && */ \
  758. c->pt[(int)t].pending_read_list)
  759. #else
  760. #define lws_ssl_anybody_has_buffered_read(ctx) (0)
  761. #define lws_ssl_anybody_has_buffered_read_tsi(ctx, t) (0)
  762. #endif
  763. int count_wsi_allocated;
  764. int count_cgi_spawned;
  765. unsigned int options;
  766. unsigned int fd_limit_per_thread;
  767. unsigned int timeout_secs;
  768. unsigned int pt_serv_buf_size;
  769. int max_http_header_data;
  770. /*
  771. * set to the Thread ID that's doing the service loop just before entry
  772. * to poll indicates service thread likely idling in poll()
  773. * volatile because other threads may check it as part of processing
  774. * for pollfd event change.
  775. */
  776. volatile int service_tid;
  777. int service_tid_detected;
  778. short max_http_header_pool;
  779. short count_threads;
  780. short plugin_protocol_count;
  781. short plugin_extension_count;
  782. short server_string_len;
  783. unsigned short ws_ping_pong_interval;
  784. unsigned int being_destroyed:1;
  785. unsigned int requested_kill:1;
  786. unsigned int protocol_init_done:1;
  787. };
  788. #define lws_get_context_protocol(ctx, x) ctx->vhost_list->protocols[x]
  789. #define lws_get_vh_protocol(vh, x) vh->protocols[x]
  790. LWS_EXTERN void
  791. lws_close_free_wsi_final(struct lws *wsi);
  792. LWS_EXTERN void
  793. lws_libuv_closehandle(struct lws *wsi);
  794. LWS_VISIBLE LWS_EXTERN int
  795. lws_plat_plugins_init(struct lws_context * context, const char * const *d);
  796. LWS_VISIBLE LWS_EXTERN int
  797. lws_plat_plugins_destroy(struct lws_context * context);
  798. LWS_EXTERN void
  799. lws_restart_ws_ping_pong_timer(struct lws *wsi);
  800. struct lws *
  801. lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd);
  802. enum {
  803. LWS_EV_READ = (1 << 0),
  804. LWS_EV_WRITE = (1 << 1),
  805. LWS_EV_START = (1 << 2),
  806. LWS_EV_STOP = (1 << 3),
  807. LWS_EV_PREPARE_DELETION = (1 << 31),
  808. };
  809. #if defined(LWS_USE_LIBEV)
  810. LWS_EXTERN void
  811. lws_libev_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
  812. LWS_EXTERN void
  813. lws_libev_io(struct lws *wsi, int flags);
  814. LWS_EXTERN int
  815. lws_libev_init_fd_table(struct lws_context *context);
  816. LWS_EXTERN void
  817. lws_libev_destroyloop(struct lws_context *context, int tsi);
  818. LWS_EXTERN void
  819. lws_libev_run(const struct lws_context *context, int tsi);
  820. #define LWS_LIBEV_ENABLED(context) lws_check_opt(context->options, LWS_SERVER_OPTION_LIBEV)
  821. LWS_EXTERN void lws_feature_status_libev(struct lws_context_creation_info *info);
  822. #else
  823. #define lws_libev_accept(_a, _b) ((void) 0)
  824. #define lws_libev_io(_a, _b) ((void) 0)
  825. #define lws_libev_init_fd_table(_a) (0)
  826. #define lws_libev_run(_a, _b) ((void) 0)
  827. #define lws_libev_destroyloop(_a, _b) ((void) 0)
  828. #define LWS_LIBEV_ENABLED(context) (0)
  829. #if LWS_POSIX
  830. #define lws_feature_status_libev(_a) \
  831. lwsl_notice("libev support not compiled in\n")
  832. #else
  833. #define lws_feature_status_libev(_a)
  834. #endif
  835. #endif
  836. #if defined(LWS_USE_LIBUV)
  837. LWS_EXTERN void
  838. lws_libuv_accept(struct lws *new_wsi, lws_sockfd_type accept_fd);
  839. LWS_EXTERN void
  840. lws_libuv_io(struct lws *wsi, int flags);
  841. LWS_EXTERN int
  842. lws_libuv_init_fd_table(struct lws_context *context);
  843. LWS_EXTERN void
  844. lws_libuv_run(const struct lws_context *context, int tsi);
  845. LWS_EXTERN void
  846. lws_libuv_destroyloop(struct lws_context *context, int tsi);
  847. #define LWS_LIBUV_ENABLED(context) lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV)
  848. LWS_EXTERN void lws_feature_status_libuv(struct lws_context_creation_info *info);
  849. #else
  850. #define lws_libuv_accept(_a, _b) ((void) 0)
  851. #define lws_libuv_io(_a, _b) ((void) 0)
  852. #define lws_libuv_init_fd_table(_a) (0)
  853. #define lws_libuv_run(_a, _b) ((void) 0)
  854. #define lws_libuv_destroyloop(_a, _b) ((void) 0)
  855. #define LWS_LIBUV_ENABLED(context) (0)
  856. #if LWS_POSIX
  857. #define lws_feature_status_libuv(_a) \
  858. lwsl_notice("libuv support not compiled in\n")
  859. #else
  860. #define lws_feature_status_libuv(_a)
  861. #endif
  862. #endif
  863. #ifdef LWS_USE_IPV6
  864. #define LWS_IPV6_ENABLED(vh) \
  865. (!lws_check_opt(vh->context->options, LWS_SERVER_OPTION_DISABLE_IPV6) && \
  866. !lws_check_opt(vh->options, LWS_SERVER_OPTION_DISABLE_IPV6))
  867. #else
  868. #define LWS_IPV6_ENABLED(context) (0)
  869. #endif
  870. #ifdef LWS_USE_UNIX_SOCK
  871. #define LWS_UNIX_SOCK_ENABLED(vhost) \
  872. (vhost->options & LWS_SERVER_OPTION_UNIX_SOCK)
  873. #else
  874. #define LWS_UNIX_SOCK_ENABLED(vhost) (0)
  875. #endif
  876. enum uri_path_states {
  877. URIPS_IDLE,
  878. URIPS_SEEN_SLASH,
  879. URIPS_SEEN_SLASH_DOT,
  880. URIPS_SEEN_SLASH_DOT_DOT,
  881. };
  882. enum uri_esc_states {
  883. URIES_IDLE,
  884. URIES_SEEN_PERCENT,
  885. URIES_SEEN_PERCENT_H1,
  886. };
  887. /* notice that these union members:
  888. *
  889. * hdr
  890. * http
  891. * http2
  892. *
  893. * all have a pointer to allocated_headers struct as their first member.
  894. *
  895. * It means for allocated_headers access, the three union paths can all be
  896. * used interchangeably to access the same data
  897. */
  898. #ifndef LWS_NO_CLIENT
  899. struct client_info_stash {
  900. char address[256];
  901. char path[1024];
  902. char host[256];
  903. char origin[256];
  904. char protocol[256];
  905. char method[16];
  906. };
  907. #endif
  908. struct _lws_header_related {
  909. /* MUST be first in struct */
  910. struct allocated_headers *ah;
  911. struct lws *ah_wait_list;
  912. unsigned char *preamble_rx;
  913. #ifndef LWS_NO_CLIENT
  914. struct client_info_stash *stash;
  915. #endif
  916. unsigned int preamble_rx_len;
  917. enum uri_path_states ups;
  918. enum uri_esc_states ues;
  919. short lextable_pos;
  920. unsigned int current_token_limit;
  921. #ifndef LWS_NO_CLIENT
  922. unsigned short c_port;
  923. #endif
  924. char esc_stash;
  925. char post_literal_equal;
  926. unsigned char parser_state; /* enum lws_token_indexes */
  927. char redirects;
  928. };
  929. struct _lws_http_mode_related {
  930. /* MUST be first in struct */
  931. struct allocated_headers *ah; /* mirroring _lws_header_related */
  932. struct lws *ah_wait_list;
  933. unsigned char *preamble_rx;
  934. #ifndef LWS_NO_CLIENT
  935. struct client_info_stash *stash;
  936. #endif
  937. unsigned int preamble_rx_len;
  938. struct lws *new_wsi_list;
  939. unsigned long filepos;
  940. unsigned long filelen;
  941. lws_filefd_type fd;
  942. enum http_version request_version;
  943. enum http_connection_type connection_type;
  944. unsigned int content_length;
  945. unsigned int content_remain;
  946. };
  947. #ifdef LWS_USE_HTTP2
  948. enum lws_http2_settings {
  949. LWS_HTTP2_SETTINGS__HEADER_TABLE_SIZE = 1,
  950. LWS_HTTP2_SETTINGS__ENABLE_PUSH,
  951. LWS_HTTP2_SETTINGS__MAX_CONCURRENT_STREAMS,
  952. LWS_HTTP2_SETTINGS__INITIAL_WINDOW_SIZE,
  953. LWS_HTTP2_SETTINGS__MAX_FRAME_SIZE,
  954. LWS_HTTP2_SETTINGS__MAX_HEADER_LIST_SIZE,
  955. LWS_HTTP2_SETTINGS__COUNT /* always last */
  956. };
  957. enum lws_http2_wellknown_frame_types {
  958. LWS_HTTP2_FRAME_TYPE_DATA,
  959. LWS_HTTP2_FRAME_TYPE_HEADERS,
  960. LWS_HTTP2_FRAME_TYPE_PRIORITY,
  961. LWS_HTTP2_FRAME_TYPE_RST_STREAM,
  962. LWS_HTTP2_FRAME_TYPE_SETTINGS,
  963. LWS_HTTP2_FRAME_TYPE_PUSH_PROMISE,
  964. LWS_HTTP2_FRAME_TYPE_PING,
  965. LWS_HTTP2_FRAME_TYPE_GOAWAY,
  966. LWS_HTTP2_FRAME_TYPE_WINDOW_UPDATE,
  967. LWS_HTTP2_FRAME_TYPE_CONTINUATION,
  968. LWS_HTTP2_FRAME_TYPE_COUNT /* always last */
  969. };
  970. enum lws_http2_flags {
  971. LWS_HTTP2_FLAG_END_STREAM = 1,
  972. LWS_HTTP2_FLAG_END_HEADERS = 4,
  973. LWS_HTTP2_FLAG_PADDED = 8,
  974. LWS_HTTP2_FLAG_PRIORITY = 0x20,
  975. LWS_HTTP2_FLAG_SETTINGS_ACK = 1,
  976. };
  977. #define LWS_HTTP2_STREAM_ID_MASTER 0
  978. #define LWS_HTTP2_FRAME_HEADER_LENGTH 9
  979. #define LWS_HTTP2_SETTINGS_LENGTH 6
  980. struct http2_settings {
  981. unsigned int setting[LWS_HTTP2_SETTINGS__COUNT];
  982. };
  983. enum http2_hpack_state {
  984. /* optional before first header block */
  985. HPKS_OPT_PADDING,
  986. HKPS_OPT_E_DEPENDENCY,
  987. HKPS_OPT_WEIGHT,
  988. /* header block */
  989. HPKS_TYPE,
  990. HPKS_IDX_EXT,
  991. HPKS_HLEN,
  992. HPKS_HLEN_EXT,
  993. HPKS_DATA,
  994. /* optional after last header block */
  995. HKPS_OPT_DISCARD_PADDING,
  996. };
  997. enum http2_hpack_type {
  998. HPKT_INDEXED_HDR_7,
  999. HPKT_INDEXED_HDR_6_VALUE_INCR,
  1000. HPKT_LITERAL_HDR_VALUE_INCR,
  1001. HPKT_INDEXED_HDR_4_VALUE,
  1002. HPKT_LITERAL_HDR_VALUE,
  1003. HPKT_SIZE_5
  1004. };
  1005. struct hpack_dt_entry {
  1006. int token; /* additions that don't map to a token are ignored */
  1007. int arg_offset;
  1008. int arg_len;
  1009. };
  1010. struct hpack_dynamic_table {
  1011. struct hpack_dt_entry *entries;
  1012. char *args;
  1013. int pos;
  1014. int next;
  1015. int num_entries;
  1016. int args_length;
  1017. };
  1018. struct _lws_http2_related {
  1019. /*
  1020. * having this first lets us also re-use all HTTP union code
  1021. * and in turn, http_mode_related has allocated headers in right
  1022. * place so we can use the header apis on the wsi directly still
  1023. */
  1024. struct _lws_http_mode_related http; /* MUST BE FIRST IN STRUCT */
  1025. struct http2_settings my_settings;
  1026. struct http2_settings peer_settings;
  1027. struct lws *parent_wsi;
  1028. struct lws *next_child_wsi;
  1029. struct hpack_dynamic_table *hpack_dyn_table;
  1030. struct lws *stream_wsi;
  1031. unsigned char ping_payload[8];
  1032. unsigned char one_setting[LWS_HTTP2_SETTINGS_LENGTH];
  1033. unsigned int count;
  1034. unsigned int length;
  1035. unsigned int stream_id;
  1036. enum http2_hpack_state hpack;
  1037. enum http2_hpack_type hpack_type;
  1038. unsigned int header_index;
  1039. unsigned int hpack_len;
  1040. unsigned int hpack_e_dep;
  1041. int tx_credit;
  1042. unsigned int my_stream_id;
  1043. unsigned int child_count;
  1044. int my_priority;
  1045. unsigned int END_STREAM:1;
  1046. unsigned int END_HEADERS:1;
  1047. unsigned int send_END_STREAM:1;
  1048. unsigned int GOING_AWAY;
  1049. unsigned int requested_POLLOUT:1;
  1050. unsigned int waiting_tx_credit:1;
  1051. unsigned int huff:1;
  1052. unsigned int value:1;
  1053. unsigned short round_robin_POLLOUT;
  1054. unsigned short count_POLLOUT_children;
  1055. unsigned short hpack_pos;
  1056. unsigned char type;
  1057. unsigned char flags;
  1058. unsigned char frame_state;
  1059. unsigned char padding;
  1060. unsigned char hpack_m;
  1061. unsigned char initialized;
  1062. };
  1063. #define HTTP2_IS_TOPLEVEL_WSI(wsi) (!wsi->u.http2.parent_wsi)
  1064. #endif
  1065. struct _lws_websocket_related {
  1066. /* cheapest way to deal with ah overlap with ws union transition */
  1067. struct _lws_header_related hdr;
  1068. char *rx_ubuf;
  1069. unsigned int rx_ubuf_alloc;
  1070. struct lws *rx_draining_ext_list;
  1071. struct lws *tx_draining_ext_list;
  1072. time_t time_next_ping_check;
  1073. size_t rx_packet_length;
  1074. unsigned int rx_ubuf_head;
  1075. unsigned char mask[4];
  1076. /* Also used for close content... control opcode == < 128 */
  1077. unsigned char ping_payload_buf[128 - 3 + LWS_PRE];
  1078. unsigned char ping_payload_len;
  1079. unsigned char mask_idx;
  1080. unsigned char opcode;
  1081. unsigned char rsv;
  1082. unsigned char rsv_first_msg;
  1083. /* zero if no info, or length including 2-byte close code */
  1084. unsigned char close_in_ping_buffer_len;
  1085. unsigned char utf8;
  1086. unsigned char stashed_write_type;
  1087. unsigned char tx_draining_stashed_wp;
  1088. unsigned int final:1;
  1089. unsigned int frame_is_binary:1;
  1090. unsigned int all_zero_nonce:1;
  1091. unsigned int this_frame_masked:1;
  1092. unsigned int inside_frame:1; /* next write will be more of frame */
  1093. unsigned int clean_buffer:1; /* buffer not rewritten by extension */
  1094. unsigned int payload_is_close:1; /* process as PONG, but it is close */
  1095. unsigned int ping_pending_flag:1;
  1096. unsigned int continuation_possible:1;
  1097. unsigned int owed_a_fin:1;
  1098. unsigned int check_utf8:1;
  1099. unsigned int defeat_check_utf8:1;
  1100. unsigned int pmce_compressed_message:1;
  1101. unsigned int stashed_write_pending:1;
  1102. unsigned int rx_draining_ext:1;
  1103. unsigned int tx_draining_ext:1;
  1104. unsigned int send_check_ping:1;
  1105. };
  1106. #ifdef LWS_WITH_CGI
  1107. /* wsi who is master of the cgi points to an lws_cgi */
  1108. struct lws_cgi {
  1109. struct lws_cgi *cgi_list;
  1110. struct lws *stdwsi[3]; /* points to the associated stdin/out/err wsis */
  1111. struct lws *wsi; /* owner */
  1112. unsigned long content_length;
  1113. unsigned long content_length_seen;
  1114. int pipe_fds[3][2];
  1115. int pid;
  1116. unsigned int being_closed:1;
  1117. };
  1118. #endif
  1119. signed char char_to_hex(const char c);
  1120. #ifndef LWS_NO_CLIENT
  1121. enum lws_chunk_parser {
  1122. ELCP_HEX,
  1123. ELCP_CR,
  1124. ELCP_CONTENT,
  1125. ELCP_POST_CR,
  1126. ELCP_POST_LF,
  1127. };
  1128. #endif
  1129. struct lws_rewrite;
  1130. #ifdef LWS_WITH_ACCESS_LOG
  1131. struct lws_access_log {
  1132. char *header_log;
  1133. char *user_agent;
  1134. unsigned long sent;
  1135. int response;
  1136. };
  1137. #endif
  1138. struct lws {
  1139. /* structs */
  1140. /* members with mutually exclusive lifetimes are unionized */
  1141. union u {
  1142. struct _lws_http_mode_related http;
  1143. #ifdef LWS_USE_HTTP2
  1144. struct _lws_http2_related http2;
  1145. #endif
  1146. struct _lws_header_related hdr;
  1147. struct _lws_websocket_related ws;
  1148. } u;
  1149. /* lifetime members */
  1150. #if defined(LWS_USE_LIBEV) || defined(LWS_USE_LIBUV)
  1151. struct lws_io_watcher w_read;
  1152. #endif
  1153. #if defined(LWS_USE_LIBEV)
  1154. struct lws_io_watcher w_write;
  1155. #endif
  1156. time_t pending_timeout_limit;
  1157. /* pointers */
  1158. struct lws_context *context;
  1159. struct lws_vhost *vhost;
  1160. struct lws *parent; /* points to parent, if any */
  1161. struct lws *child_list; /* points to first child */
  1162. struct lws *sibling_list; /* subsequent children at same level */
  1163. #ifdef LWS_WITH_CGI
  1164. struct lws_cgi *cgi; /* wsi being cgi master have one of these */
  1165. #endif
  1166. const struct lws_protocols *protocol;
  1167. struct lws **same_vh_protocol_prev, *same_vh_protocol_next;
  1168. struct lws *timeout_list;
  1169. struct lws **timeout_list_prev;
  1170. #ifdef LWS_WITH_ACCESS_LOG
  1171. struct lws_access_log access_log;
  1172. #endif
  1173. void *user_space;
  1174. /* rxflow handling */
  1175. unsigned char *rxflow_buffer;
  1176. /* truncated send handling */
  1177. unsigned char *trunc_alloc; /* non-NULL means buffering in progress */
  1178. #if defined (LWS_WITH_ESP8266)
  1179. void *premature_rx;
  1180. unsigned short prem_rx_size, prem_rx_pos;
  1181. #endif
  1182. #ifndef LWS_NO_EXTENSIONS
  1183. const struct lws_extension *active_extensions[LWS_MAX_EXTENSIONS_ACTIVE];
  1184. void *act_ext_user[LWS_MAX_EXTENSIONS_ACTIVE];
  1185. #endif
  1186. #ifdef LWS_OPENSSL_SUPPORT
  1187. SSL *ssl;
  1188. #if !defined(LWS_USE_POLARSSL) && !defined(LWS_USE_MBEDTLS)
  1189. BIO *client_bio;
  1190. #endif
  1191. struct lws *pending_read_list_prev, *pending_read_list_next;
  1192. #endif
  1193. #ifdef LWS_WITH_HTTP_PROXY
  1194. struct lws_rewrite *rw;
  1195. #endif
  1196. #ifdef LWS_LATENCY
  1197. unsigned long action_start;
  1198. unsigned long latency_start;
  1199. #endif
  1200. /* pointer / int */
  1201. lws_sockfd_type sock;
  1202. /* ints */
  1203. int position_in_fds_table;
  1204. int rxflow_len;
  1205. int rxflow_pos;
  1206. unsigned int trunc_alloc_len; /* size of malloc */
  1207. unsigned int trunc_offset; /* where we are in terms of spilling */
  1208. unsigned int trunc_len; /* how much is buffered */
  1209. #ifndef LWS_NO_CLIENT
  1210. int chunk_remaining;
  1211. #endif
  1212. unsigned int cache_secs;
  1213. unsigned int hdr_parsing_completed:1;
  1214. unsigned int http2_substream:1;
  1215. unsigned int listener:1;
  1216. unsigned int user_space_externally_allocated:1;
  1217. unsigned int socket_is_permanently_unusable:1;
  1218. unsigned int rxflow_change_to:2;
  1219. unsigned int more_rx_waiting:1; /* has to live here since ah may stick to end */
  1220. unsigned int conn_stat_done:1;
  1221. unsigned int cache_reuse:1;
  1222. unsigned int cache_revalidate:1;
  1223. unsigned int cache_intermediaries:1;
  1224. unsigned int favoured_pollin:1;
  1225. unsigned int sending_chunked:1;
  1226. unsigned int already_did_cce:1;
  1227. unsigned int told_user_closed:1;
  1228. #if defined(LWS_WITH_ESP8266)
  1229. unsigned int pending_send_completion:3;
  1230. unsigned int close_is_pending_send_completion:1;
  1231. #endif
  1232. #ifdef LWS_WITH_ACCESS_LOG
  1233. unsigned int access_log_pending:1;
  1234. #endif
  1235. #ifndef LWS_NO_CLIENT
  1236. unsigned int do_ws:1; /* whether we are doing http or ws flow */
  1237. unsigned int chunked:1; /* if the clientside connection is chunked */
  1238. unsigned int client_rx_avail:1;
  1239. unsigned int client_http_body_pending:1;
  1240. #endif
  1241. #ifdef LWS_WITH_HTTP_PROXY
  1242. unsigned int perform_rewrite:1;
  1243. #endif
  1244. #ifndef LWS_NO_EXTENSIONS
  1245. unsigned int extension_data_pending:1;
  1246. #endif
  1247. #ifdef LWS_OPENSSL_SUPPORT
  1248. unsigned int use_ssl:3;
  1249. unsigned int upgraded:1;
  1250. #endif
  1251. #ifdef _WIN32
  1252. unsigned int sock_send_blocking:1;
  1253. #endif
  1254. #ifdef LWS_OPENSSL_SUPPORT
  1255. unsigned int redirect_to_https:1;
  1256. #endif
  1257. /* chars */
  1258. #ifndef LWS_NO_EXTENSIONS
  1259. unsigned char count_act_ext;
  1260. #endif
  1261. unsigned char ietf_spec_revision;
  1262. char mode; /* enum connection_mode */
  1263. char state; /* enum lws_connection_states */
  1264. char state_pre_close;
  1265. char lws_rx_parse_state; /* enum lws_rx_parse_state */
  1266. char rx_frame_type; /* enum lws_write_protocol */
  1267. char pending_timeout; /* enum pending_timeout */
  1268. char pps; /* enum lws_pending_protocol_send */
  1269. char tsi; /* thread service index we belong to */
  1270. char protocol_interpret_idx;
  1271. #ifdef LWS_WITH_CGI
  1272. char cgi_channel; /* which of stdin/out/err */
  1273. char hdr_state;
  1274. #endif
  1275. #ifndef LWS_NO_CLIENT
  1276. char chunk_parser; /* enum lws_chunk_parser */
  1277. #endif
  1278. #if defined(LWS_WITH_CGI) || !defined(LWS_NO_CLIENT)
  1279. char reason_bf; /* internal writeable callback reason bitfield */
  1280. #endif
  1281. };
  1282. LWS_EXTERN int log_level;
  1283. LWS_EXTERN int
  1284. lws_socket_bind(struct lws_vhost *vhost, int sockfd, int port,
  1285. const char *iface);
  1286. LWS_EXTERN void
  1287. lws_close_free_wsi(struct lws *wsi, enum lws_close_status);
  1288. LWS_EXTERN int
  1289. remove_wsi_socket_from_fds(struct lws *wsi);
  1290. LWS_EXTERN int
  1291. lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
  1292. #ifndef LWS_LATENCY
  1293. static inline void
  1294. lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
  1295. int ret, int completion) {
  1296. do {
  1297. (void)context; (void)wsi; (void)action; (void)ret;
  1298. (void)completion;
  1299. } while (0);
  1300. }
  1301. static inline void
  1302. lws_latency_pre(struct lws_context *context, struct lws *wsi) {
  1303. do { (void)context; (void)wsi; } while (0);
  1304. }
  1305. #else
  1306. #define lws_latency_pre(_context, _wsi) lws_latency(_context, _wsi, NULL, 0, 0)
  1307. extern void
  1308. lws_latency(struct lws_context *context, struct lws *wsi, const char *action,
  1309. int ret, int completion);
  1310. #endif
  1311. LWS_EXTERN void
  1312. lws_set_protocol_write_pending(struct lws *wsi,
  1313. enum lws_pending_protocol_send pend);
  1314. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1315. lws_client_rx_sm(struct lws *wsi, unsigned char c);
  1316. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1317. lws_parse(struct lws *wsi, unsigned char c);
  1318. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1319. lws_http_action(struct lws *wsi);
  1320. LWS_EXTERN int
  1321. lws_b64_selftest(void);
  1322. LWS_EXTERN int
  1323. lws_service_flag_pending(struct lws_context *context, int tsi);
  1324. #if defined(_WIN32) || defined(MBED_OPERATORS) || defined(LWS_WITH_ESP8266)
  1325. LWS_EXTERN struct lws *
  1326. wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd);
  1327. LWS_EXTERN int
  1328. insert_wsi(struct lws_context *context, struct lws *wsi);
  1329. LWS_EXTERN int
  1330. delete_from_fd(struct lws_context *context, lws_sockfd_type fd);
  1331. #else
  1332. #define wsi_from_fd(A,B) A->lws_lookup[B]
  1333. #define insert_wsi(A,B) assert(A->lws_lookup[B->sock] == 0); A->lws_lookup[B->sock]=B
  1334. #define delete_from_fd(A,B) A->lws_lookup[B]=0
  1335. #endif
  1336. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1337. insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
  1338. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1339. lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
  1340. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1341. lws_service_timeout_check(struct lws *wsi, unsigned int sec);
  1342. LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  1343. lws_client_connect_2(struct lws *wsi);
  1344. LWS_VISIBLE struct lws * LWS_WARN_UNUSED_RESULT
  1345. lws_client_reset(struct lws *wsi, int ssl, const char *address, int port,
  1346. const char *path, const char *host);
  1347. LWS_EXTERN struct lws * LWS_WARN_UNUSED_RESULT
  1348. lws_create_new_server_wsi(struct lws_vhost *vhost);
  1349. LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
  1350. lws_generate_client_handshake(struct lws *wsi, char *pkt);
  1351. LWS_EXTERN int
  1352. lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
  1353. LWS_EXTERN struct lws *
  1354. lws_client_connect_via_info2(struct lws *wsi);
  1355. /*
  1356. * EXTENSIONS
  1357. */
  1358. #ifndef LWS_NO_EXTENSIONS
  1359. LWS_VISIBLE void
  1360. lws_context_init_extensions(struct lws_context_creation_info *info,
  1361. struct lws_context *context);
  1362. LWS_EXTERN int
  1363. lws_any_extension_handled(struct lws *wsi, enum lws_extension_callback_reasons r,
  1364. void *v, size_t len);
  1365. LWS_EXTERN int
  1366. lws_ext_cb_active(struct lws *wsi, int reason, void *buf, int len);
  1367. LWS_EXTERN int
  1368. lws_ext_cb_all_exts(struct lws_context *context, struct lws *wsi, int reason,
  1369. void *arg, int len);
  1370. #else
  1371. #define lws_any_extension_handled(_a, _b, _c, _d) (0)
  1372. #define lws_ext_cb_active(_a, _b, _c, _d) (0)
  1373. #define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
  1374. #define lws_issue_raw_ext_access lws_issue_raw
  1375. #define lws_context_init_extensions(_a, _b)
  1376. #endif
  1377. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1378. lws_client_interpret_server_handshake(struct lws *wsi);
  1379. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1380. lws_rx_sm(struct lws *wsi, unsigned char c);
  1381. LWS_EXTERN int
  1382. lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf, size_t *len);
  1383. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1384. lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
  1385. LWS_EXTERN void
  1386. lws_union_transition(struct lws *wsi, enum connection_mode mode);
  1387. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1388. user_callback_handle_rxflow(lws_callback_function, struct lws *wsi,
  1389. enum lws_callback_reasons reason, void *user,
  1390. void *in, size_t len);
  1391. #ifdef LWS_USE_HTTP2
  1392. LWS_EXTERN struct lws *lws_http2_get_network_wsi(struct lws *wsi);
  1393. struct lws * lws_http2_get_nth_child(struct lws *wsi, int n);
  1394. LWS_EXTERN int
  1395. lws_http2_interpret_settings_payload(struct http2_settings *settings,
  1396. unsigned char *buf, int len);
  1397. LWS_EXTERN void lws_http2_init(struct http2_settings *settings);
  1398. LWS_EXTERN int
  1399. lws_http2_parser(struct lws *wsi, unsigned char c);
  1400. LWS_EXTERN int lws_http2_do_pps_send(struct lws_context *context,
  1401. struct lws *wsi);
  1402. LWS_EXTERN int lws_http2_frame_write(struct lws *wsi, int type, int flags,
  1403. unsigned int sid, unsigned int len,
  1404. unsigned char *buf);
  1405. LWS_EXTERN struct lws *
  1406. lws_http2_wsi_from_id(struct lws *wsi, unsigned int sid);
  1407. LWS_EXTERN int lws_hpack_interpret(struct lws *wsi,
  1408. unsigned char c);
  1409. LWS_EXTERN int
  1410. lws_add_http2_header_by_name(struct lws *wsi,
  1411. const unsigned char *name,
  1412. const unsigned char *value, int length,
  1413. unsigned char **p, unsigned char *end);
  1414. LWS_EXTERN int
  1415. lws_add_http2_header_by_token(struct lws *wsi,
  1416. enum lws_token_indexes token,
  1417. const unsigned char *value, int length,
  1418. unsigned char **p, unsigned char *end);
  1419. LWS_EXTERN int
  1420. lws_add_http2_header_status(struct lws *wsi,
  1421. unsigned int code, unsigned char **p,
  1422. unsigned char *end);
  1423. LWS_EXTERN
  1424. void lws_http2_configure_if_upgraded(struct lws *wsi);
  1425. #else
  1426. #define lws_http2_configure_if_upgraded(x)
  1427. #endif
  1428. LWS_EXTERN int
  1429. lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd);
  1430. LWS_EXTERN int
  1431. lws_plat_check_connection_error(struct lws *wsi);
  1432. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1433. lws_header_table_attach(struct lws *wsi, int autoservice);
  1434. LWS_EXTERN int
  1435. lws_header_table_detach(struct lws *wsi, int autoservice);
  1436. LWS_EXTERN void
  1437. lws_header_table_reset(struct lws *wsi, int autoservice);
  1438. LWS_EXTERN char * LWS_WARN_UNUSED_RESULT
  1439. lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
  1440. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1441. lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
  1442. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1443. lws_ensure_user_space(struct lws *wsi);
  1444. LWS_EXTERN int
  1445. lws_change_pollfd(struct lws *wsi, int _and, int _or);
  1446. #ifndef LWS_NO_SERVER
  1447. int lws_context_init_server(struct lws_context_creation_info *info,
  1448. struct lws_vhost *vhost);
  1449. LWS_EXTERN struct lws_vhost *
  1450. lws_select_vhost(struct lws_context *context, int port, const char *servername);
  1451. LWS_EXTERN int
  1452. handshake_0405(struct lws_context *context, struct lws *wsi);
  1453. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1454. lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len);
  1455. LWS_EXTERN void
  1456. lws_server_get_canonical_hostname(struct lws_context *context,
  1457. struct lws_context_creation_info *info);
  1458. #else
  1459. #define lws_context_init_server(_a, _b) (0)
  1460. #define lws_interpret_incoming_packet(_a, _b, _c) (0)
  1461. #define lws_server_get_canonical_hostname(_a, _b)
  1462. #endif
  1463. #ifndef LWS_NO_DAEMONIZE
  1464. LWS_EXTERN int get_daemonize_pid();
  1465. #else
  1466. #define get_daemonize_pid() (0)
  1467. #endif
  1468. #if !defined(MBED_OPERATORS) && !defined(LWS_WITH_ESP8266)
  1469. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1470. interface_to_sa(struct lws_vhost *vh, const char *ifname,
  1471. struct sockaddr_in *addr, size_t addrlen);
  1472. #endif
  1473. LWS_EXTERN void lwsl_emit_stderr(int level, const char *line);
  1474. enum lws_ssl_capable_status {
  1475. LWS_SSL_CAPABLE_ERROR = -1,
  1476. LWS_SSL_CAPABLE_MORE_SERVICE = -2,
  1477. };
  1478. #ifndef LWS_OPENSSL_SUPPORT
  1479. #define LWS_SSL_ENABLED(context) (0)
  1480. #define lws_context_init_server_ssl(_a, _b) (0)
  1481. #define lws_ssl_destroy(_a)
  1482. #define lws_context_init_http2_ssl(_a)
  1483. #define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
  1484. #define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
  1485. #define lws_ssl_pending lws_ssl_pending_no_ssl
  1486. #define lws_server_socket_service_ssl(_b, _c) (0)
  1487. #define lws_ssl_close(_a) (0)
  1488. #define lws_ssl_context_destroy(_a)
  1489. #define lws_ssl_SSL_CTX_destroy(_a)
  1490. #define lws_ssl_remove_wsi_from_buffered_list(_a)
  1491. #define lws_context_init_ssl_library(_a)
  1492. #else
  1493. #define LWS_SSL_ENABLED(context) (context->use_ssl)
  1494. LWS_EXTERN int openssl_websocket_private_data_index;
  1495. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1496. lws_ssl_capable_read(struct lws *wsi, unsigned char *buf, int len);
  1497. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1498. lws_ssl_capable_write(struct lws *wsi, unsigned char *buf, int len);
  1499. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1500. lws_ssl_pending(struct lws *wsi);
  1501. LWS_EXTERN int
  1502. lws_context_init_ssl_library(struct lws_context_creation_info *info);
  1503. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1504. lws_server_socket_service_ssl(struct lws *new_wsi, lws_sockfd_type accept_fd);
  1505. LWS_EXTERN int
  1506. lws_ssl_close(struct lws *wsi);
  1507. LWS_EXTERN void
  1508. lws_ssl_SSL_CTX_destroy(struct lws_vhost *vhost);
  1509. LWS_EXTERN void
  1510. lws_ssl_context_destroy(struct lws_context *context);
  1511. LWS_VISIBLE void
  1512. lws_ssl_remove_wsi_from_buffered_list(struct lws *wsi);
  1513. LWS_EXTERN int
  1514. lws_ssl_client_bio_create(struct lws *wsi);
  1515. LWS_EXTERN int
  1516. lws_ssl_client_connect1(struct lws *wsi);
  1517. LWS_EXTERN int
  1518. lws_ssl_client_connect2(struct lws *wsi);
  1519. LWS_EXTERN void
  1520. lws_ssl_elaborate_error(void);
  1521. #ifndef LWS_NO_SERVER
  1522. LWS_EXTERN int
  1523. lws_context_init_server_ssl(struct lws_context_creation_info *info,
  1524. struct lws_vhost *vhost);
  1525. #else
  1526. #define lws_context_init_server_ssl(_a, _b) (0)
  1527. #endif
  1528. LWS_EXTERN void
  1529. lws_ssl_destroy(struct lws_vhost *vhost);
  1530. /* HTTP2-related */
  1531. #ifdef LWS_USE_HTTP2
  1532. LWS_EXTERN void
  1533. lws_context_init_http2_ssl(struct lws_vhost *vhost);
  1534. #else
  1535. #define lws_context_init_http2_ssl(_a)
  1536. #endif
  1537. #endif
  1538. #if LWS_MAX_SMP > 1
  1539. static LWS_INLINE void
  1540. lws_pt_mutex_init(struct lws_context_per_thread *pt)
  1541. {
  1542. pthread_mutex_init(&pt->lock, NULL);
  1543. }
  1544. static LWS_INLINE void
  1545. lws_pt_mutex_destroy(struct lws_context_per_thread *pt)
  1546. {
  1547. pthread_mutex_destroy(&pt->lock);
  1548. }
  1549. static LWS_INLINE void
  1550. lws_pt_lock(struct lws_context_per_thread *pt)
  1551. {
  1552. if (!pt->lock_depth++)
  1553. pthread_mutex_lock(&pt->lock);
  1554. }
  1555. static LWS_INLINE void
  1556. lws_pt_unlock(struct lws_context_per_thread *pt)
  1557. {
  1558. if (!(--pt->lock_depth))
  1559. pthread_mutex_unlock(&pt->lock);
  1560. }
  1561. #else
  1562. #define lws_pt_mutex_init(_a) (void)(_a)
  1563. #define lws_pt_mutex_destroy(_a) (void)(_a)
  1564. #define lws_pt_lock(_a) (void)(_a)
  1565. #define lws_pt_unlock(_a) (void)(_a)
  1566. #endif
  1567. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1568. lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len);
  1569. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1570. lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len);
  1571. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1572. lws_ssl_pending_no_ssl(struct lws *wsi);
  1573. #ifdef LWS_WITH_HTTP_PROXY
  1574. struct lws_rewrite {
  1575. hubbub_parser *parser;
  1576. hubbub_parser_optparams params;
  1577. const char *from, *to;
  1578. int from_len, to_len;
  1579. unsigned char *p, *end;
  1580. struct lws *wsi;
  1581. };
  1582. static LWS_INLINE int hstrcmp(hubbub_string *s, const char *p, int len)
  1583. {
  1584. if (s->len != len)
  1585. return 1;
  1586. return strncmp((const char *)s->ptr, p, len);
  1587. }
  1588. typedef hubbub_error (*hubbub_callback_t)(const hubbub_token *token, void *pw);
  1589. LWS_EXTERN struct lws_rewrite *
  1590. lws_rewrite_create(struct lws *wsi, hubbub_callback_t cb, const char *from, const char *to);
  1591. LWS_EXTERN void
  1592. lws_rewrite_destroy(struct lws_rewrite *r);
  1593. LWS_EXTERN int
  1594. lws_rewrite_parse(struct lws_rewrite *r, const unsigned char *in, int in_len);
  1595. #endif
  1596. #ifndef LWS_NO_CLIENT
  1597. LWS_EXTERN int lws_client_socket_service(struct lws_context *context,
  1598. struct lws *wsi,
  1599. struct lws_pollfd *pollfd);
  1600. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1601. lws_http_transaction_completed_client(struct lws *wsi);
  1602. #ifdef LWS_OPENSSL_SUPPORT
  1603. LWS_EXTERN int
  1604. lws_context_init_client_ssl(struct lws_context_creation_info *info,
  1605. struct lws_vhost *vhost);
  1606. #else
  1607. #define lws_context_init_client_ssl(_a, _b) (0)
  1608. #endif
  1609. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1610. lws_handshake_client(struct lws *wsi, unsigned char **buf, size_t len);
  1611. LWS_EXTERN void
  1612. lws_decode_ssl_error(void);
  1613. #else
  1614. #define lws_context_init_client_ssl(_a, _b) (0)
  1615. #define lws_handshake_client(_a, _b, _c) (0)
  1616. #endif
  1617. LWS_EXTERN int
  1618. _lws_rx_flow_control(struct lws *wsi);
  1619. LWS_EXTERN int
  1620. _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa);
  1621. #ifndef LWS_NO_SERVER
  1622. LWS_EXTERN int
  1623. lws_server_socket_service(struct lws_context *context, struct lws *wsi,
  1624. struct lws_pollfd *pollfd);
  1625. LWS_EXTERN int
  1626. lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
  1627. LWS_EXTERN int
  1628. _lws_server_listen_accept_flow_control(struct lws *twsi, int on);
  1629. #else
  1630. #define lws_server_socket_service(_a, _b, _c) (0)
  1631. #define lws_handshake_server(_a, _b, _c) (0)
  1632. #define _lws_server_listen_accept_flow_control(a, b) (0)
  1633. #endif
  1634. #ifdef LWS_WITH_ACCESS_LOG
  1635. LWS_EXTERN int
  1636. lws_access_log(struct lws *wsi);
  1637. #else
  1638. #define lws_access_log(_a)
  1639. #endif
  1640. LWS_EXTERN int
  1641. lws_cgi_kill_terminated(struct lws_context_per_thread *pt);
  1642. int
  1643. lws_protocol_init(struct lws_context *context);
  1644. int
  1645. lws_bind_protocol(struct lws *wsi, const struct lws_protocols *p);
  1646. const struct lws_http_mount *
  1647. lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len);
  1648. /*
  1649. * custom allocator
  1650. */
  1651. LWS_EXTERN void *
  1652. lws_realloc(void *ptr, size_t size);
  1653. LWS_EXTERN void * LWS_WARN_UNUSED_RESULT
  1654. lws_zalloc(size_t size);
  1655. #define lws_malloc(S) lws_realloc(NULL, S)
  1656. #define lws_free(P) lws_realloc(P, 0)
  1657. #define lws_free_set_NULL(P) do { lws_realloc(P, 0); (P) = NULL; } while(0)
  1658. /* lws_plat_ */
  1659. LWS_EXTERN void
  1660. lws_plat_delete_socket_from_fds(struct lws_context *context,
  1661. struct lws *wsi, int m);
  1662. LWS_EXTERN void
  1663. lws_plat_insert_socket_into_fds(struct lws_context *context,
  1664. struct lws *wsi);
  1665. LWS_EXTERN void
  1666. lws_plat_service_periodic(struct lws_context *context);
  1667. LWS_EXTERN int
  1668. lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
  1669. struct lws_pollfd *pfd);
  1670. LWS_EXTERN int
  1671. lws_plat_context_early_init(void);
  1672. LWS_EXTERN void
  1673. lws_plat_context_early_destroy(struct lws_context *context);
  1674. LWS_EXTERN void
  1675. lws_plat_context_late_destroy(struct lws_context *context);
  1676. LWS_EXTERN int
  1677. lws_poll_listen_fd(struct lws_pollfd *fd);
  1678. LWS_EXTERN int
  1679. lws_plat_service(struct lws_context *context, int timeout_ms);
  1680. LWS_EXTERN LWS_VISIBLE int
  1681. lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
  1682. LWS_EXTERN int
  1683. lws_plat_init(struct lws_context *context,
  1684. struct lws_context_creation_info *info);
  1685. LWS_EXTERN void
  1686. lws_plat_drop_app_privileges(struct lws_context_creation_info *info);
  1687. LWS_EXTERN unsigned long long
  1688. time_in_microseconds(void);
  1689. LWS_EXTERN const char * LWS_WARN_UNUSED_RESULT
  1690. lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
  1691. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  1692. lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len);
  1693. #ifdef __cplusplus
  1694. };
  1695. #endif