private-libwebsockets.h 51 KB

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