private-lib-core-net.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #if !defined(__LWS_CORE_NET_PRIVATE_H__)
  25. #define __LWS_CORE_NET_PRIVATE_H__
  26. #if !defined(_POSIX_C_SOURCE)
  27. #define _POSIX_C_SOURCE 200112L
  28. #endif
  29. /*
  30. * Generic pieces needed to manage muxable stream protocols like h2
  31. */
  32. struct lws_muxable {
  33. struct lws *parent_wsi;
  34. struct lws *child_list;
  35. struct lws *sibling_list;
  36. unsigned int my_sid;
  37. unsigned int child_count;
  38. uint32_t highest_sid;
  39. uint8_t requested_POLLOUT;
  40. };
  41. #include "private-lib-roles.h"
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /*
  46. * All lws_tls...() functions must return this type, converting the
  47. * native backend result and doing the extra work to determine which one
  48. * as needed.
  49. *
  50. * Native TLS backend return codes are NOT ALLOWED outside the backend.
  51. *
  52. * Non-SSL mode also uses these types.
  53. */
  54. enum lws_ssl_capable_status {
  55. LWS_SSL_CAPABLE_ERROR = -1, /* it failed */
  56. LWS_SSL_CAPABLE_DONE = 0, /* it succeeded */
  57. LWS_SSL_CAPABLE_MORE_SERVICE_READ = -2, /* retry WANT_READ */
  58. LWS_SSL_CAPABLE_MORE_SERVICE_WRITE = -3, /* retry WANT_WRITE */
  59. LWS_SSL_CAPABLE_MORE_SERVICE = -4, /* general retry */
  60. };
  61. #define __lws_sul_insert_us(owner, sul, _us) \
  62. (sul)->us = lws_now_usecs() + _us; \
  63. __lws_sul_insert(owner, sul)
  64. /*
  65. *
  66. * ------ roles ------
  67. *
  68. */
  69. /* null-terminated array of pointers to roles lws built with */
  70. extern const struct lws_role_ops *available_roles[];
  71. #define LWS_FOR_EVERY_AVAILABLE_ROLE_START(xx) { \
  72. const struct lws_role_ops **ppxx = available_roles; \
  73. while (*ppxx) { \
  74. const struct lws_role_ops *xx = *ppxx++;
  75. #define LWS_FOR_EVERY_AVAILABLE_ROLE_END }}
  76. /*
  77. *
  78. * ------ event_loop ops ------
  79. *
  80. */
  81. /* enums of socks version */
  82. enum socks_version {
  83. SOCKS_VERSION_4 = 4,
  84. SOCKS_VERSION_5 = 5
  85. };
  86. /* enums of subnegotiation version */
  87. enum socks_subnegotiation_version {
  88. SOCKS_SUBNEGOTIATION_VERSION_1 = 1,
  89. };
  90. /* enums of socks commands */
  91. enum socks_command {
  92. SOCKS_COMMAND_CONNECT = 1,
  93. SOCKS_COMMAND_BIND = 2,
  94. SOCKS_COMMAND_UDP_ASSOCIATE = 3
  95. };
  96. /* enums of socks address type */
  97. enum socks_atyp {
  98. SOCKS_ATYP_IPV4 = 1,
  99. SOCKS_ATYP_DOMAINNAME = 3,
  100. SOCKS_ATYP_IPV6 = 4
  101. };
  102. /* enums of socks authentication methods */
  103. enum socks_auth_method {
  104. SOCKS_AUTH_NO_AUTH = 0,
  105. SOCKS_AUTH_GSSAPI = 1,
  106. SOCKS_AUTH_USERNAME_PASSWORD = 2
  107. };
  108. /* enums of subnegotiation status */
  109. enum socks_subnegotiation_status {
  110. SOCKS_SUBNEGOTIATION_STATUS_SUCCESS = 0,
  111. };
  112. /* enums of socks request reply */
  113. enum socks_request_reply {
  114. SOCKS_REQUEST_REPLY_SUCCESS = 0,
  115. SOCKS_REQUEST_REPLY_FAILURE_GENERAL = 1,
  116. SOCKS_REQUEST_REPLY_CONNECTION_NOT_ALLOWED = 2,
  117. SOCKS_REQUEST_REPLY_NETWORK_UNREACHABLE = 3,
  118. SOCKS_REQUEST_REPLY_HOST_UNREACHABLE = 4,
  119. SOCKS_REQUEST_REPLY_CONNECTION_REFUSED = 5,
  120. SOCKS_REQUEST_REPLY_TTL_EXPIRED = 6,
  121. SOCKS_REQUEST_REPLY_COMMAND_NOT_SUPPORTED = 7,
  122. SOCKS_REQUEST_REPLY_ATYP_NOT_SUPPORTED = 8
  123. };
  124. /* enums used to generate socks messages */
  125. enum socks_msg_type {
  126. /* greeting */
  127. SOCKS_MSG_GREETING,
  128. /* credential, user name and password */
  129. SOCKS_MSG_USERNAME_PASSWORD,
  130. /* connect command */
  131. SOCKS_MSG_CONNECT
  132. };
  133. enum {
  134. LWS_RXFLOW_ALLOW = (1 << 0),
  135. LWS_RXFLOW_PENDING_CHANGE = (1 << 1),
  136. };
  137. typedef enum lws_parser_return {
  138. LPR_FORBIDDEN = -2,
  139. LPR_FAIL = -1,
  140. LPR_OK = 0,
  141. LPR_DO_FALLBACK = 2,
  142. } lws_parser_return_t;
  143. enum pmd_return {
  144. PMDR_UNKNOWN,
  145. PMDR_DID_NOTHING,
  146. PMDR_HAS_PENDING,
  147. PMDR_EMPTY_NONFINAL,
  148. PMDR_EMPTY_FINAL,
  149. PMDR_NOTHING_WE_SHOULD_DO,
  150. PMDR_FAILED = -1
  151. };
  152. #if defined(LWS_WITH_PEER_LIMITS)
  153. struct lws_peer {
  154. struct lws_peer *next;
  155. struct lws_peer *peer_wait_list;
  156. lws_sockaddr46 sa46;
  157. time_t time_created;
  158. time_t time_closed_all;
  159. uint32_t hash;
  160. uint32_t count_wsi;
  161. uint32_t total_wsi;
  162. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  163. struct lws_peer_role_http http;
  164. #endif
  165. };
  166. #endif
  167. enum {
  168. LWS_EV_READ = (1 << 0),
  169. LWS_EV_WRITE = (1 << 1),
  170. LWS_EV_START = (1 << 2),
  171. LWS_EV_STOP = (1 << 3),
  172. LWS_EV_PREPARE_DELETION = (1u << 31),
  173. };
  174. #ifdef LWS_WITH_IPV6
  175. #define LWS_IPV6_ENABLED(vh) \
  176. (!lws_check_opt(vh->context->options, LWS_SERVER_OPTION_DISABLE_IPV6) && \
  177. !lws_check_opt(vh->options, LWS_SERVER_OPTION_DISABLE_IPV6))
  178. #else
  179. #define LWS_IPV6_ENABLED(context) (0)
  180. #endif
  181. #ifdef LWS_WITH_UNIX_SOCK
  182. #define LWS_UNIX_SOCK_ENABLED(vhost) \
  183. (vhost->options & LWS_SERVER_OPTION_UNIX_SOCK)
  184. #else
  185. #define LWS_UNIX_SOCK_ENABLED(vhost) (0)
  186. #endif
  187. enum uri_path_states {
  188. URIPS_IDLE,
  189. URIPS_SEEN_SLASH,
  190. URIPS_SEEN_SLASH_DOT,
  191. URIPS_SEEN_SLASH_DOT_DOT,
  192. };
  193. enum uri_esc_states {
  194. URIES_IDLE,
  195. URIES_SEEN_PERCENT,
  196. URIES_SEEN_PERCENT_H1,
  197. };
  198. #if defined(LWS_WITH_CLIENT)
  199. enum {
  200. CIS_ADDRESS,
  201. CIS_PATH,
  202. CIS_HOST,
  203. CIS_ORIGIN,
  204. CIS_PROTOCOL,
  205. CIS_METHOD,
  206. CIS_IFACE,
  207. CIS_ALPN,
  208. CIS_COUNT
  209. };
  210. struct client_info_stash {
  211. char *cis[CIS_COUNT];
  212. void *opaque_user_data; /* not allocated or freed by lws */
  213. };
  214. #endif
  215. #if defined(LWS_WITH_UDP)
  216. #define lws_wsi_is_udp(___wsi) (!!___wsi->udp)
  217. #endif
  218. #define LWS_H2_FRAME_HEADER_LENGTH 9
  219. lws_usec_t
  220. __lws_sul_service_ripe(lws_dll2_owner_t *own, int num_own, lws_usec_t usnow);
  221. #if defined(LWS_WITH_DEPRECATED_THINGS)
  222. struct lws_timed_vh_protocol {
  223. struct lws_timed_vh_protocol *next;
  224. lws_sorted_usec_list_t sul;
  225. const struct lws_protocols *protocol;
  226. struct lws_vhost *vhost; /* only used for pending processing */
  227. int reason;
  228. int tsi_req;
  229. };
  230. #endif
  231. /*
  232. * lws_dsh
  233. */
  234. typedef struct lws_dsh_obj_head {
  235. lws_dll2_owner_t owner;
  236. int kind;
  237. } lws_dsh_obj_head_t;
  238. typedef struct lws_dsh_obj {
  239. lws_dll2_t list; /* must be first */
  240. struct lws_dsh *dsh; /* invalid when on free list */
  241. size_t size; /* invalid when on free list */
  242. size_t asize;
  243. } lws_dsh_obj_t;
  244. typedef struct lws_dsh {
  245. lws_dll2_t list;
  246. uint8_t *buf;
  247. lws_dsh_obj_head_t *oha; /* array of object heads/kind */
  248. size_t buffer_size;
  249. size_t locally_in_use;
  250. size_t locally_free;
  251. int count_kinds;
  252. uint8_t being_destroyed;
  253. /*
  254. * Overallocations at create:
  255. *
  256. * - the buffer itself
  257. * - the object heads array
  258. */
  259. } lws_dsh_t;
  260. /*
  261. * lws_async_dns
  262. */
  263. typedef struct lws_async_dns {
  264. lws_sockaddr46 sa46; /* nameserver */
  265. lws_dll2_owner_t waiting;
  266. lws_dll2_owner_t cached;
  267. struct lws *wsi;
  268. time_t time_set_server;
  269. char dns_server_set;
  270. } lws_async_dns_t;
  271. typedef enum {
  272. LADNS_CONF_SERVER_UNKNOWN = -1,
  273. LADNS_CONF_SERVER_SAME,
  274. LADNS_CONF_SERVER_CHANGED
  275. } lws_async_dns_server_check_t;
  276. #if defined(LWS_WITH_SYS_ASYNC_DNS)
  277. void
  278. lws_aysnc_dns_completed(struct lws *wsi, void *sa, size_t salen,
  279. lws_async_dns_retcode_t ret);
  280. #endif
  281. void
  282. lws_async_dns_cancel(struct lws *wsi);
  283. /*
  284. * so we can have n connections being serviced simultaneously,
  285. * these things need to be isolated per-thread.
  286. */
  287. struct lws_context_per_thread {
  288. #if LWS_MAX_SMP > 1
  289. pthread_mutex_t lock_stats;
  290. struct lws_mutex_refcount mr;
  291. pthread_t self;
  292. #endif
  293. struct lws_dll2_owner dll_buflist_owner; /* guys with pending rxflow */
  294. struct lws_dll2_owner seq_owner; /* list of lws_sequencer-s */
  295. lws_dll2_owner_t attach_owner; /* pending lws_attach */
  296. #if defined(LWS_WITH_SECURE_STREAMS)
  297. lws_dll2_owner_t ss_owner;
  298. #endif
  299. #if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) || \
  300. defined(LWS_WITH_SECURE_STREAMS_THREAD_API)
  301. lws_dll2_owner_t ss_dsh_owner;
  302. lws_dll2_owner_t ss_client_owner;
  303. #endif
  304. struct lws_dll2_owner pt_sul_owner[LWS_COUNT_PT_SUL_OWNERS];
  305. #if defined (LWS_WITH_SEQUENCER)
  306. lws_sorted_usec_list_t sul_seq_heartbeat;
  307. #endif
  308. #if (defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)) && defined(LWS_WITH_SERVER)
  309. lws_sorted_usec_list_t sul_ah_lifecheck;
  310. #endif
  311. #if defined(LWS_WITH_TLS) && defined(LWS_WITH_SERVER)
  312. lws_sorted_usec_list_t sul_tls;
  313. #endif
  314. #if defined(LWS_PLAT_UNIX)
  315. lws_sorted_usec_list_t sul_plat;
  316. #endif
  317. #if defined(LWS_ROLE_CGI)
  318. lws_sorted_usec_list_t sul_cgi;
  319. #endif
  320. #if defined(LWS_WITH_STATS)
  321. uint64_t lws_stats[LWSSTATS_SIZE];
  322. int updated;
  323. lws_sorted_usec_list_t sul_stats;
  324. #endif
  325. #if defined(LWS_WITH_PEER_LIMITS)
  326. lws_sorted_usec_list_t sul_peer_limits;
  327. #endif
  328. #if !defined(LWS_PLAT_FREERTOS)
  329. struct lws *fake_wsi; /* used for callbacks where there's no wsi */
  330. #endif
  331. #if defined(LWS_WITH_TLS)
  332. struct lws_pt_tls tls;
  333. #endif
  334. struct lws_context *context;
  335. /*
  336. * usable by anything in the service code, but only if the scope
  337. * does not last longer than the service action (since next service
  338. * of any socket can likewise use it and overwrite)
  339. */
  340. unsigned char *serv_buf;
  341. struct lws_pollfd *fds;
  342. volatile struct lws_foreign_thread_pollfd * volatile foreign_pfd_list;
  343. #ifdef _WIN32
  344. WSAEVENT events[WSA_MAXIMUM_WAIT_EVENTS];
  345. CRITICAL_SECTION interrupt_lock;
  346. #endif
  347. lws_sockfd_type dummy_pipe_fds[2];
  348. struct lws *pipe_wsi;
  349. /* --- role based members --- */
  350. #if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
  351. struct lws_pt_role_ws ws;
  352. #endif
  353. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  354. struct lws_pt_role_http http;
  355. #endif
  356. #if defined(LWS_ROLE_DBUS)
  357. struct lws_pt_role_dbus dbus;
  358. #endif
  359. /* --- event library based members --- */
  360. #if defined(LWS_WITH_EVENT_LIBS)
  361. void *evlib_pt; /* overallocated */
  362. #endif
  363. #if defined(LWS_WITH_DETAILED_LATENCY)
  364. lws_usec_t ust_left_poll;
  365. #endif
  366. /* --- */
  367. unsigned long count_conns;
  368. unsigned int fds_count;
  369. /*
  370. * set to the Thread ID that's doing the service loop just before entry
  371. * to poll indicates service thread likely idling in poll()
  372. * volatile because other threads may check it as part of processing
  373. * for pollfd event change.
  374. */
  375. volatile int service_tid;
  376. int service_tid_detected;
  377. volatile unsigned char inside_poll;
  378. volatile unsigned char foreign_spinlock;
  379. unsigned char tid;
  380. unsigned char inside_service:1;
  381. unsigned char inside_lws_service:1;
  382. unsigned char event_loop_foreign:1;
  383. unsigned char event_loop_destroy_processing_done:1;
  384. unsigned char destroy_self:1;
  385. unsigned char is_destroyed:1;
  386. };
  387. #if defined(LWS_WITH_SERVER_STATUS)
  388. struct lws_conn_stats {
  389. unsigned long long rx, tx;
  390. unsigned long h1_conn, h1_trans, h2_trans, ws_upg, h2_alpn, h2_subs,
  391. h2_upg, rejected, mqtt_subs;
  392. };
  393. #endif
  394. /*
  395. * virtual host -related context information
  396. * vhostwide SSL context
  397. * vhostwide proxy
  398. *
  399. * hierarchy:
  400. *
  401. * context -> vhost -> wsi
  402. *
  403. * incoming connection non-SSL vhost binding:
  404. *
  405. * listen socket -> wsi -> select vhost after first headers
  406. *
  407. * incoming connection SSL vhost binding:
  408. *
  409. * SSL SNI -> wsi -> bind after SSL negotiation
  410. */
  411. struct lws_vhost {
  412. #if defined(LWS_WITH_CLIENT) && defined(LWS_CLIENT_HTTP_PROXYING)
  413. char proxy_basic_auth_token[128];
  414. #endif
  415. #if LWS_MAX_SMP > 1
  416. pthread_mutex_t lock;
  417. char close_flow_vs_tsi[LWS_MAX_SMP];
  418. #endif
  419. #if defined(LWS_ROLE_H2)
  420. struct lws_vhost_role_h2 h2;
  421. #endif
  422. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  423. struct lws_vhost_role_http http;
  424. #endif
  425. #if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
  426. struct lws_vhost_role_ws ws;
  427. #endif
  428. #if defined(LWS_WITH_SOCKS5)
  429. char socks_proxy_address[128];
  430. char socks_user[96];
  431. char socks_password[96];
  432. #endif
  433. #if defined(LWS_WITH_EVENT_LIBS)
  434. void *evlib_vh; /* overallocated */
  435. #endif
  436. #if defined(LWS_WITH_SERVER_STATUS)
  437. struct lws_conn_stats conn_stats;
  438. #endif
  439. uint64_t options;
  440. struct lws_context *context;
  441. struct lws_vhost *vhost_next;
  442. const lws_retry_bo_t *retry_policy;
  443. #if defined(LWS_WITH_SERVER) && defined(LWS_WITH_SECURE_STREAMS)
  444. lws_ss_handle_t *ss_handle; /* ss handle for the server obj */
  445. #endif
  446. struct lws *lserv_wsi;
  447. const char *name;
  448. const char *iface;
  449. const char *listen_accept_role;
  450. const char *listen_accept_protocol;
  451. const char *unix_socket_perms;
  452. void (*finalize)(struct lws_vhost *vh, void *arg);
  453. void *finalize_arg;
  454. const struct lws_protocols *protocols;
  455. void **protocol_vh_privs;
  456. const struct lws_protocol_vhost_options *pvo;
  457. const struct lws_protocol_vhost_options *headers;
  458. struct lws_dll2_owner *same_vh_protocol_owner;
  459. struct lws_vhost *no_listener_vhost_list;
  460. struct lws_dll2_owner abstract_instances_owner; /* vh lock */
  461. #if defined(LWS_WITH_CLIENT)
  462. struct lws_dll2_owner dll_cli_active_conns_owner;
  463. #endif
  464. struct lws_dll2_owner vh_awaiting_socket_owner;
  465. #if defined(LWS_WITH_TLS)
  466. struct lws_vhost_tls tls;
  467. #endif
  468. #if defined(LWS_WITH_DEPRECATED_THINGS)
  469. struct lws_timed_vh_protocol *timed_vh_protocol_list;
  470. #endif
  471. void *user;
  472. int listen_port;
  473. #if !defined(LWS_PLAT_FREERTOS) && !defined(OPTEE_TA) && !defined(WIN32)
  474. int bind_iface;
  475. #endif
  476. #if defined(LWS_WITH_SOCKS5)
  477. unsigned int socks_proxy_port;
  478. #endif
  479. int count_protocols;
  480. int ka_time;
  481. int ka_probes;
  482. int ka_interval;
  483. int keepalive_timeout;
  484. int timeout_secs_ah_idle;
  485. int connect_timeout_secs;
  486. int count_bound_wsi;
  487. #ifdef LWS_WITH_ACCESS_LOG
  488. int log_fd;
  489. #endif
  490. #if defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY)
  491. int8_t ss_refcount;
  492. /**< refcount of number of ss connections with streamtypes using this
  493. * trust store */
  494. #endif
  495. uint8_t allocated_vhost_protocols:1;
  496. uint8_t created_vhost_protocols:1;
  497. uint8_t being_destroyed:1;
  498. uint8_t from_ss_policy:1;
  499. unsigned char default_protocol_index;
  500. unsigned char raw_protocol_index;
  501. };
  502. void
  503. __lws_vhost_destroy2(struct lws_vhost *vh);
  504. #define mux_to_wsi(_m) lws_container_of(_m, struct lws, mux)
  505. void
  506. lws_wsi_mux_insert(struct lws *wsi, struct lws *parent_wsi, int sid);
  507. int
  508. lws_wsi_mux_mark_parents_needing_writeable(struct lws *wsi);
  509. struct lws *
  510. lws_wsi_mux_move_child_to_tail(struct lws **wsi2);
  511. int
  512. lws_wsi_mux_action_pending_writeable_reqs(struct lws *wsi);
  513. void
  514. lws_wsi_mux_dump_children(struct lws *wsi);
  515. void
  516. lws_wsi_mux_close_children(struct lws *wsi, int reason);
  517. void
  518. lws_wsi_mux_sibling_disconnect(struct lws *wsi);
  519. void
  520. lws_wsi_mux_dump_waiting_children(struct lws *wsi);
  521. int
  522. lws_wsi_mux_apply_queue(struct lws *wsi);
  523. /*
  524. * struct lws
  525. */
  526. /*
  527. * These pieces are very commonly used (via accessors) in user protocol handlers
  528. * and have to be valid, even in the case no real wsi is available for the cb.
  529. *
  530. * We put all this category of pointers in there and compose it at the top of
  531. * struct lws, so a dummy wsi providing these only needs to be this big, while
  532. * still being castable for being a struct wsi *
  533. */
  534. struct lws_a {
  535. struct lws_context *context;
  536. struct lws_vhost *vhost;
  537. const struct lws_protocols *protocol;
  538. void *opaque_user_data;
  539. };
  540. /*
  541. * For RTOS-class platforms, their code is relatively new, post-minimal examples
  542. * and tend to not have legacy user protocol handler baggage touching unexpected
  543. * things in fakewsi unconditionally... we can use an lws_a on the stack and
  544. * don't need to define the rest of the wsi content, just cast it, this saves
  545. * a wsi footprint in heap (typ 800 bytes nowadays even on RTOS).
  546. *
  547. * For other platforms that have been around for years and have thousands of
  548. * different user protocol handler implementations, it's likely some of them
  549. * will be touching the struct lws content unconditionally in the handler even
  550. * when we are calling back with a non wsi-specific reason, and may react badly
  551. * to it being garbage. So continue to implement those as a full, zero-ed down
  552. * prepared fakewsi on heap at context creation time.
  553. */
  554. #if defined(LWS_PLAT_FREERTOS)
  555. #define lws_fakewsi_def_plwsa(pt) struct lws_a lwsa, *plwsa = &lwsa
  556. #else
  557. #define lws_fakewsi_def_plwsa(pt) struct lws_a *plwsa = &(pt)->fake_wsi->a
  558. #endif
  559. /* since we reuse the pt version, also correct to zero down the lws_a part */
  560. #define lws_fakewsi_prep_plwsa_ctx(_c) \
  561. memset(plwsa, 0, sizeof(*plwsa)); plwsa->context = _c
  562. struct lws {
  563. struct lws_a a;
  564. /* structs */
  565. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  566. struct _lws_http_mode_related http;
  567. #endif
  568. #if defined(LWS_ROLE_H2)
  569. struct _lws_h2_related h2;
  570. #endif
  571. #if defined(LWS_ROLE_WS)
  572. struct _lws_websocket_related *ws; /* allocated if we upgrade to ws */
  573. #endif
  574. #if defined(LWS_ROLE_DBUS)
  575. struct _lws_dbus_mode_related dbus;
  576. #endif
  577. #if defined(LWS_ROLE_MQTT)
  578. struct _lws_mqtt_related *mqtt;
  579. #endif
  580. #if defined(LWS_ROLE_H2) || defined(LWS_ROLE_MQTT)
  581. struct lws_muxable mux;
  582. struct lws_tx_credit txc;
  583. #endif
  584. /* lifetime members */
  585. #if defined(LWS_WITH_EVENT_LIBS)
  586. void *evlib_wsi; /* overallocated */
  587. #endif
  588. #if defined(LWS_WITH_DETAILED_LATENCY)
  589. lws_detlat_t detlat;
  590. #endif
  591. lws_sorted_usec_list_t sul_timeout;
  592. lws_sorted_usec_list_t sul_hrtimer;
  593. lws_sorted_usec_list_t sul_validity;
  594. lws_sorted_usec_list_t sul_connect_timeout;
  595. struct lws_dll2 dll_buflist; /* guys with pending rxflow */
  596. struct lws_dll2 same_vh_protocol;
  597. struct lws_dll2 vh_awaiting_socket;
  598. #if defined(LWS_WITH_SYS_ASYNC_DNS)
  599. struct lws_dll2 adns; /* on adns list of guys to tell result */
  600. lws_async_dns_cb_t adns_cb; /* callback with result */
  601. #endif
  602. #if defined(LWS_WITH_CLIENT)
  603. struct lws_dll2 dll_cli_active_conns;
  604. struct lws_dll2 dll2_cli_txn_queue;
  605. struct lws_dll2_owner dll2_cli_txn_queue_owner;
  606. #endif
  607. #if defined(LWS_WITH_ACCESS_LOG)
  608. char simple_ip[(8 * 5)];
  609. #endif
  610. /* pointers */
  611. struct lws *parent; /* points to parent, if any */
  612. struct lws *child_list; /* points to first child */
  613. struct lws *sibling_list; /* subsequent children at same level */
  614. const struct lws_role_ops *role_ops;
  615. struct lws_sequencer *seq; /* associated sequencer if any */
  616. const lws_retry_bo_t *retry_policy;
  617. #if defined(LWS_WITH_THREADPOOL)
  618. lws_dll2_owner_t tp_task_owner; /* struct lws_threadpool_task */
  619. #endif
  620. #if defined(LWS_WITH_PEER_LIMITS)
  621. struct lws_peer *peer;
  622. #endif
  623. #if defined(LWS_WITH_UDP)
  624. struct lws_udp *udp;
  625. #endif
  626. #if defined(LWS_WITH_CLIENT)
  627. struct client_info_stash *stash;
  628. char *cli_hostname_copy;
  629. const struct addrinfo *dns_results;
  630. const struct addrinfo *dns_results_next;
  631. #endif
  632. void *user_space;
  633. void *opaque_parent_data;
  634. struct lws_buflist *buflist; /* input-side buflist */
  635. struct lws_buflist *buflist_out; /* output-side buflist */
  636. #if defined(LWS_WITH_TLS)
  637. struct lws_lws_tls tls;
  638. #endif
  639. lws_sock_file_fd_type desc; /* .filefd / .sockfd */
  640. #if defined(LWS_WITH_STATS)
  641. uint64_t active_writable_req_us;
  642. #if defined(LWS_WITH_TLS)
  643. uint64_t accept_start_us;
  644. #endif
  645. #endif
  646. lws_wsi_state_t wsistate;
  647. lws_wsi_state_t wsistate_pre_close;
  648. /* ints */
  649. #define LWS_NO_FDS_POS (-1)
  650. int position_in_fds_table;
  651. #if defined(LWS_WITH_CLIENT)
  652. int chunk_remaining;
  653. int flags;
  654. #endif
  655. unsigned int cache_secs;
  656. unsigned int hdr_parsing_completed:1;
  657. unsigned int mux_substream:1;
  658. unsigned int upgraded_to_http2:1;
  659. unsigned int mux_stream_immortal:1;
  660. unsigned int h2_stream_carries_ws:1; /* immortal set as well */
  661. unsigned int h2_stream_carries_sse:1; /* immortal set as well */
  662. unsigned int h2_acked_settings:1;
  663. unsigned int seen_nonpseudoheader:1;
  664. unsigned int listener:1;
  665. unsigned int pf_packet:1;
  666. unsigned int do_broadcast:1;
  667. unsigned int user_space_externally_allocated:1;
  668. unsigned int socket_is_permanently_unusable:1;
  669. unsigned int rxflow_change_to:2;
  670. unsigned int conn_stat_done:1;
  671. unsigned int cache_reuse:1;
  672. unsigned int cache_revalidate:1;
  673. unsigned int cache_intermediaries:1;
  674. unsigned int favoured_pollin:1;
  675. unsigned int sending_chunked:1;
  676. unsigned int interpreting:1;
  677. unsigned int already_did_cce:1;
  678. unsigned int told_user_closed:1;
  679. unsigned int told_event_loop_closed:1;
  680. unsigned int waiting_to_send_close_frame:1;
  681. unsigned int close_needs_ack:1;
  682. unsigned int ipv6:1;
  683. unsigned int parent_pending_cb_on_writable:1;
  684. unsigned int cgi_stdout_zero_length:1;
  685. unsigned int seen_zero_length_recv:1;
  686. unsigned int rxflow_will_be_applied:1;
  687. unsigned int event_pipe:1;
  688. unsigned int handling_404:1;
  689. unsigned int protocol_bind_balance:1;
  690. unsigned int unix_skt:1;
  691. unsigned int close_when_buffered_out_drained:1;
  692. unsigned int h1_ws_proxied:1;
  693. unsigned int proxied_ws_parent:1;
  694. unsigned int do_bind:1;
  695. unsigned int validity_hup:1;
  696. unsigned int skip_fallback:1;
  697. unsigned int file_desc:1;
  698. unsigned int conn_validity_wakesuspend:1;
  699. unsigned int could_have_pending:1; /* detect back-to-back writes */
  700. unsigned int outer_will_close:1;
  701. unsigned int shadow:1; /* we do not control fd lifecycle at all */
  702. #if defined(LWS_WITH_SECURE_STREAMS)
  703. unsigned int for_ss:1;
  704. #endif
  705. #ifdef LWS_WITH_ACCESS_LOG
  706. unsigned int access_log_pending:1;
  707. #endif
  708. #if defined(LWS_WITH_CLIENT)
  709. unsigned int do_ws:1; /* whether we are doing http or ws flow */
  710. unsigned int chunked:1; /* if the clientside connection is chunked */
  711. unsigned int client_rx_avail:1;
  712. unsigned int client_http_body_pending:1;
  713. unsigned int transaction_from_pipeline_queue:1;
  714. unsigned int keepalive_active:1;
  715. unsigned int keepalive_rejected:1;
  716. unsigned int redirected_to_get:1;
  717. unsigned int client_pipeline:1;
  718. unsigned int client_h2_alpn:1;
  719. unsigned int client_mux_substream:1;
  720. unsigned int client_mux_migrated:1;
  721. unsigned int client_subsequent_mime_part:1;
  722. unsigned int client_no_follow_redirect:1;
  723. unsigned int client_suppress_CONNECTION_ERROR:1;
  724. /**< because the client connection creation api is still the parent of
  725. * this activity, and will report the failure */
  726. #endif
  727. #ifdef _WIN32
  728. unsigned int sock_send_blocking:1;
  729. #endif
  730. uint16_t ocport, c_port;
  731. uint16_t retry;
  732. #if defined(LWS_WITH_CLIENT)
  733. uint16_t keep_warm_secs;
  734. #endif
  735. /* chars */
  736. char lws_rx_parse_state; /* enum lws_rx_parse_state */
  737. char rx_frame_type; /* enum lws_write_protocol */
  738. char pending_timeout; /* enum pending_timeout */
  739. char tsi; /* thread service index we belong to */
  740. char protocol_interpret_idx;
  741. char redirects;
  742. uint8_t rxflow_bitmap;
  743. uint8_t bound_vhost_index;
  744. uint8_t lsp_channel; /* which of stdin/out/err */
  745. #ifdef LWS_WITH_CGI
  746. char hdr_state;
  747. #endif
  748. #if defined(LWS_WITH_CLIENT)
  749. char chunk_parser; /* enum lws_chunk_parser */
  750. uint8_t addrinfo_idx;
  751. uint8_t sys_tls_client_cert;
  752. #endif
  753. #if defined(LWS_WITH_CGI) || defined(LWS_WITH_CLIENT)
  754. char reason_bf; /* internal writeable callback reason bitfield */
  755. #endif
  756. #if defined(LWS_WITH_STATS) && defined(LWS_WITH_TLS)
  757. char seen_rx;
  758. #endif
  759. uint8_t immortal_substream_count;
  760. /* volatile to make sure code is aware other thread can change */
  761. volatile char handling_pollout;
  762. volatile char leave_pollout_active;
  763. #if LWS_MAX_SMP > 1
  764. volatile char undergoing_init_from_other_pt;
  765. #endif
  766. };
  767. #define lws_is_flowcontrolled(w) (!!(wsi->rxflow_bitmap))
  768. #if defined(LWS_WITH_SPAWN)
  769. #if defined(WIN32) || defined(_WIN32)
  770. #else
  771. #include <sys/wait.h>
  772. #include <sys/times.h>
  773. #endif
  774. struct lws_spawn_piped {
  775. struct lws_spawn_piped_info info;
  776. struct lws_dll2 dll;
  777. lws_sorted_usec_list_t sul;
  778. lws_sorted_usec_list_t sul_reap;
  779. struct lws_context *context;
  780. struct lws *stdwsi[3];
  781. lws_filefd_type pipe_fds[3][2];
  782. int count_log_lines;
  783. lws_usec_t created; /* set by lws_spawn_piped() */
  784. lws_usec_t reaped;
  785. lws_usec_t accounting[4];
  786. #if defined(WIN32)
  787. HANDLE child_pid;
  788. lws_sorted_usec_list_t sul_poll;
  789. #else
  790. pid_t child_pid;
  791. siginfo_t si;
  792. #endif
  793. int reap_retry_budget;
  794. uint8_t pipes_alive:2;
  795. uint8_t we_killed_him_timeout:1;
  796. uint8_t we_killed_him_spew:1;
  797. uint8_t ungraceful:1;
  798. };
  799. void
  800. lws_spawn_piped_destroy(struct lws_spawn_piped **lsp);
  801. int
  802. lws_spawn_reap(struct lws_spawn_piped *lsp);
  803. #endif
  804. void
  805. lws_service_do_ripe_rxflow(struct lws_context_per_thread *pt);
  806. const struct lws_role_ops *
  807. lws_role_by_name(const char *name);
  808. int
  809. lws_socket_bind(struct lws_vhost *vhost, lws_sockfd_type sockfd, int port,
  810. const char *iface, int ipv6_allowed);
  811. #if defined(LWS_WITH_IPV6)
  812. unsigned long
  813. lws_get_addr_scope(const char *ipaddr);
  814. #endif
  815. void
  816. lws_close_free_wsi(struct lws *wsi, enum lws_close_status, const char *caller);
  817. void
  818. __lws_close_free_wsi(struct lws *wsi, enum lws_close_status, const char *caller);
  819. void
  820. __lws_free_wsi(struct lws *wsi);
  821. #if LWS_MAX_SMP > 1
  822. static LWS_INLINE void
  823. lws_pt_mutex_init(struct lws_context_per_thread *pt)
  824. {
  825. lws_mutex_refcount_init(&pt->mr);
  826. pthread_mutex_init(&pt->lock_stats, NULL);
  827. }
  828. static LWS_INLINE void
  829. lws_pt_mutex_destroy(struct lws_context_per_thread *pt)
  830. {
  831. pthread_mutex_destroy(&pt->lock_stats);
  832. lws_mutex_refcount_destroy(&pt->mr);
  833. }
  834. #define lws_pt_lock(pt, reason) lws_mutex_refcount_lock(&pt->mr, reason)
  835. #define lws_pt_unlock(pt) lws_mutex_refcount_unlock(&pt->mr)
  836. #define lws_pt_assert_lock_held(pt) lws_mutex_refcount_assert_held(&pt->mr)
  837. static LWS_INLINE void
  838. lws_pt_stats_lock(struct lws_context_per_thread *pt)
  839. {
  840. pthread_mutex_lock(&pt->lock_stats);
  841. }
  842. static LWS_INLINE void
  843. lws_pt_stats_unlock(struct lws_context_per_thread *pt)
  844. {
  845. pthread_mutex_unlock(&pt->lock_stats);
  846. }
  847. #endif
  848. /*
  849. * EXTENSIONS
  850. */
  851. #if defined(LWS_WITHOUT_EXTENSIONS)
  852. #define lws_any_extension_handled(_a, _b, _c, _d) (0)
  853. #define lws_ext_cb_active(_a, _b, _c, _d) (0)
  854. #define lws_ext_cb_all_exts(_a, _b, _c, _d, _e) (0)
  855. #define lws_issue_raw_ext_access lws_issue_raw
  856. #define lws_context_init_extensions(_a, _b)
  857. #endif
  858. int LWS_WARN_UNUSED_RESULT
  859. lws_client_interpret_server_handshake(struct lws *wsi);
  860. int LWS_WARN_UNUSED_RESULT
  861. lws_ws_rx_sm(struct lws *wsi, char already_processed, unsigned char c);
  862. int LWS_WARN_UNUSED_RESULT
  863. lws_issue_raw_ext_access(struct lws *wsi, unsigned char *buf, size_t len);
  864. void
  865. lws_role_transition(struct lws *wsi, enum lwsi_role role, enum lwsi_state state,
  866. const struct lws_role_ops *ops);
  867. int
  868. lws_http_to_fallback(struct lws *wsi, unsigned char *buf, size_t len);
  869. int LWS_WARN_UNUSED_RESULT
  870. user_callback_handle_rxflow(lws_callback_function, struct lws *wsi,
  871. enum lws_callback_reasons reason, void *user,
  872. void *in, size_t len);
  873. int
  874. lws_plat_set_nonblocking(lws_sockfd_type fd);
  875. int
  876. lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd,
  877. int unix_skt);
  878. int
  879. lws_plat_check_connection_error(struct lws *wsi);
  880. int LWS_WARN_UNUSED_RESULT
  881. lws_header_table_attach(struct lws *wsi, int autoservice);
  882. int
  883. lws_header_table_detach(struct lws *wsi, int autoservice);
  884. int
  885. __lws_header_table_detach(struct lws *wsi, int autoservice);
  886. void
  887. lws_header_table_reset(struct lws *wsi, int autoservice);
  888. void
  889. __lws_header_table_reset(struct lws *wsi, int autoservice);
  890. char * LWS_WARN_UNUSED_RESULT
  891. lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h);
  892. int LWS_WARN_UNUSED_RESULT
  893. lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s);
  894. int LWS_WARN_UNUSED_RESULT
  895. lws_ensure_user_space(struct lws *wsi);
  896. int LWS_WARN_UNUSED_RESULT
  897. lws_change_pollfd(struct lws *wsi, int _and, int _or);
  898. #if defined(LWS_WITH_SERVER)
  899. int _lws_vhost_init_server(const struct lws_context_creation_info *info,
  900. struct lws_vhost *vhost);
  901. LWS_EXTERN struct lws_vhost *
  902. lws_select_vhost(struct lws_context *context, int port, const char *servername);
  903. LWS_EXTERN int LWS_WARN_UNUSED_RESULT
  904. lws_parse_ws(struct lws *wsi, unsigned char **buf, size_t len);
  905. LWS_EXTERN void
  906. lws_server_get_canonical_hostname(struct lws_context *context,
  907. const struct lws_context_creation_info *info);
  908. #else
  909. #define _lws_vhost_init_server(_a, _b) (0)
  910. #define lws_parse_ws(_a, _b, _c) (0)
  911. #define lws_server_get_canonical_hostname(_a, _b)
  912. #endif
  913. int
  914. __remove_wsi_socket_from_fds(struct lws *wsi);
  915. enum {
  916. LWSRXFC_ERROR = -1,
  917. LWSRXFC_CACHED = 0,
  918. LWSRXFC_ADDITIONAL = 1,
  919. LWSRXFC_TRIMMED = 2,
  920. };
  921. int
  922. _lws_plat_service_forced_tsi(struct lws_context *context, int tsi);
  923. int
  924. lws_rxflow_cache(struct lws *wsi, unsigned char *buf, int n, int len);
  925. int
  926. lws_service_flag_pending(struct lws_context *context, int tsi);
  927. static LWS_INLINE int
  928. lws_has_buffered_out(struct lws *wsi) { return !!wsi->buflist_out; }
  929. int LWS_WARN_UNUSED_RESULT
  930. lws_ws_client_rx_sm(struct lws *wsi, unsigned char c);
  931. lws_parser_return_t LWS_WARN_UNUSED_RESULT
  932. lws_parse(struct lws *wsi, unsigned char *buf, int *len);
  933. int LWS_WARN_UNUSED_RESULT
  934. lws_parse_urldecode(struct lws *wsi, uint8_t *_c);
  935. int LWS_WARN_UNUSED_RESULT
  936. lws_http_action(struct lws *wsi);
  937. void
  938. __lws_close_free_wsi_final(struct lws *wsi);
  939. void
  940. lws_libuv_closehandle(struct lws *wsi);
  941. int
  942. lws_libuv_check_watcher_active(struct lws *wsi);
  943. #if defined(LWS_WITH_EVLIB_PLUGINS) || defined(LWS_WITH_PLUGINS)
  944. const lws_plugin_header_t *
  945. lws_plat_dlopen(struct lws_plugin **pplugin, const char *libpath,
  946. const char *sofilename, const char *_class,
  947. each_plugin_cb_t each, void *each_user);
  948. int
  949. lws_plat_destroy_dl(struct lws_plugin *p);
  950. #endif
  951. struct lws *
  952. lws_adopt_socket_vhost(struct lws_vhost *vh, lws_sockfd_type accept_fd);
  953. void
  954. lws_vhost_bind_wsi(struct lws_vhost *vh, struct lws *wsi);
  955. void
  956. lws_vhost_unbind_wsi(struct lws *wsi);
  957. void
  958. __lws_set_timeout(struct lws *wsi, enum pending_timeout reason, int secs);
  959. int
  960. __lws_change_pollfd(struct lws *wsi, int _and, int _or);
  961. int
  962. lws_callback_as_writeable(struct lws *wsi);
  963. int
  964. lws_role_call_client_bind(struct lws *wsi,
  965. const struct lws_client_connect_info *i);
  966. void
  967. lws_remove_child_from_any_parent(struct lws *wsi);
  968. char *
  969. lws_generate_client_ws_handshake(struct lws *wsi, char *p, const char *conn1);
  970. int
  971. lws_client_ws_upgrade(struct lws *wsi, const char **cce);
  972. int
  973. lws_create_client_ws_object(const struct lws_client_connect_info *i,
  974. struct lws *wsi);
  975. int
  976. lws_alpn_comma_to_openssl(const char *comma, uint8_t *os, int len);
  977. int
  978. lws_role_call_alpn_negotiated(struct lws *wsi, const char *alpn);
  979. int
  980. lws_tls_server_conn_alpn(struct lws *wsi);
  981. int
  982. lws_ws_client_rx_sm_block(struct lws *wsi, unsigned char **buf, size_t len);
  983. void
  984. lws_destroy_event_pipe(struct lws *wsi);
  985. /* socks */
  986. int
  987. lws_socks5c_generate_msg(struct lws *wsi, enum socks_msg_type type, ssize_t *msg_len);
  988. #if defined(LWS_WITH_SERVER_STATUS)
  989. void
  990. lws_sum_stats(const struct lws_context *ctx, struct lws_conn_stats *cs);
  991. #endif
  992. #if defined(LWS_WITH_DEPRECATED_THINGS)
  993. int
  994. __lws_timed_callback_remove(struct lws_vhost *vh, struct lws_timed_vh_protocol *p);
  995. #endif
  996. int LWS_WARN_UNUSED_RESULT
  997. __insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi);
  998. int LWS_WARN_UNUSED_RESULT
  999. lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len);
  1000. lws_usec_t
  1001. __lws_seq_timeout_check(struct lws_context_per_thread *pt, lws_usec_t usnow);
  1002. lws_usec_t
  1003. __lws_ss_timeout_check(struct lws_context_per_thread *pt, lws_usec_t usnow);
  1004. struct lws * LWS_WARN_UNUSED_RESULT
  1005. lws_client_connect_2_dnsreq(struct lws *wsi);
  1006. LWS_VISIBLE struct lws * LWS_WARN_UNUSED_RESULT
  1007. lws_client_reset(struct lws **wsi, int ssl, const char *address, int port,
  1008. const char *path, const char *host, char weak);
  1009. struct lws * LWS_WARN_UNUSED_RESULT
  1010. lws_create_new_server_wsi(struct lws_vhost *vhost, int fixed_tsi);
  1011. char * LWS_WARN_UNUSED_RESULT
  1012. lws_generate_client_handshake(struct lws *wsi, char *pkt);
  1013. int
  1014. lws_handle_POLLOUT_event(struct lws *wsi, struct lws_pollfd *pollfd);
  1015. struct lws *
  1016. lws_http_client_connect_via_info2(struct lws *wsi);
  1017. #if defined(LWS_WITH_CLIENT)
  1018. int
  1019. lws_client_socket_service(struct lws *wsi, struct lws_pollfd *pollfd);
  1020. int LWS_WARN_UNUSED_RESULT
  1021. lws_http_transaction_completed_client(struct lws *wsi);
  1022. #if !defined(LWS_WITH_TLS)
  1023. #define lws_context_init_client_ssl(_a, _b) (0)
  1024. #endif
  1025. void
  1026. lws_decode_ssl_error(void);
  1027. #else
  1028. #define lws_context_init_client_ssl(_a, _b) (0)
  1029. #endif
  1030. int
  1031. __lws_rx_flow_control(struct lws *wsi);
  1032. int
  1033. _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa);
  1034. #if defined(LWS_WITH_SERVER)
  1035. int
  1036. lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len);
  1037. #else
  1038. #define lws_server_socket_service(_b, _c) (0)
  1039. #define lws_handshake_server(_a, _b, _c) (0)
  1040. #endif
  1041. #ifdef LWS_WITH_ACCESS_LOG
  1042. int
  1043. lws_access_log(struct lws *wsi);
  1044. void
  1045. lws_prepare_access_log_info(struct lws *wsi, char *uri_ptr, int len, int meth);
  1046. #else
  1047. #define lws_access_log(_a)
  1048. #endif
  1049. #if defined(_DEBUG)
  1050. void
  1051. lws_wsi_txc_describe(struct lws_tx_credit *txc, const char *at, uint32_t sid);
  1052. #else
  1053. #define lws_wsi_txc_describe(x, y, z) { (void)x; }
  1054. #endif
  1055. int
  1056. lws_wsi_txc_check_skint(struct lws_tx_credit *txc, int32_t tx_cr);
  1057. int
  1058. lws_wsi_txc_report_manual_txcr_in(struct lws *wsi, int32_t bump);
  1059. void
  1060. lws_mux_mark_immortal(struct lws *wsi);
  1061. void
  1062. lws_http_close_immortal(struct lws *wsi);
  1063. int
  1064. lws_cgi_kill_terminated(struct lws_context_per_thread *pt);
  1065. void
  1066. lws_cgi_remove_and_kill(struct lws *wsi);
  1067. void
  1068. lws_plat_delete_socket_from_fds(struct lws_context *context,
  1069. struct lws *wsi, int m);
  1070. void
  1071. lws_plat_insert_socket_into_fds(struct lws_context *context,
  1072. struct lws *wsi);
  1073. int
  1074. lws_plat_change_pollfd(struct lws_context *context, struct lws *wsi,
  1075. struct lws_pollfd *pfd);
  1076. #if defined(LWS_WITH_SERVER) && defined(LWS_WITH_SECURE_STREAMS)
  1077. int
  1078. lws_adopt_ss_server_accept(struct lws *new_wsi);
  1079. #endif
  1080. int
  1081. lws_plat_pipe_create(struct lws *wsi);
  1082. int
  1083. lws_plat_pipe_signal(struct lws *wsi);
  1084. void
  1085. lws_plat_pipe_close(struct lws *wsi);
  1086. void
  1087. lws_addrinfo_clean(struct lws *wsi);
  1088. void
  1089. lws_add_wsi_to_draining_ext_list(struct lws *wsi);
  1090. void
  1091. lws_remove_wsi_from_draining_ext_list(struct lws *wsi);
  1092. int
  1093. lws_poll_listen_fd(struct lws_pollfd *fd);
  1094. int
  1095. lws_plat_service(struct lws_context *context, int timeout_ms);
  1096. LWS_VISIBLE int
  1097. _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi);
  1098. int
  1099. lws_pthread_self_to_tsi(struct lws_context *context);
  1100. const char * LWS_WARN_UNUSED_RESULT
  1101. lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
  1102. int LWS_WARN_UNUSED_RESULT
  1103. lws_plat_inet_pton(int af, const char *src, void *dst);
  1104. void
  1105. lws_same_vh_protocol_remove(struct lws *wsi);
  1106. void
  1107. __lws_same_vh_protocol_remove(struct lws *wsi);
  1108. void
  1109. lws_same_vh_protocol_insert(struct lws *wsi, int n);
  1110. void
  1111. lws_seq_destroy_all_on_pt(struct lws_context_per_thread *pt);
  1112. int
  1113. lws_broadcast(struct lws_context_per_thread *pt, int reason, void *in, size_t len);
  1114. #if defined(LWS_WITH_STATS)
  1115. void
  1116. lws_stats_bump(struct lws_context_per_thread *pt, int i, uint64_t bump);
  1117. void
  1118. lws_stats_max(struct lws_context_per_thread *pt, int index, uint64_t val);
  1119. #else
  1120. static LWS_INLINE uint64_t lws_stats_bump(
  1121. struct lws_context_per_thread *pt, int index, uint64_t bump) {
  1122. (void)pt; (void)index; (void)bump; return 0; }
  1123. static LWS_INLINE uint64_t lws_stats_max(
  1124. struct lws_context_per_thread *pt, int index, uint64_t val) {
  1125. (void)pt; (void)index; (void)val; return 0; }
  1126. #endif
  1127. #if defined(LWS_WITH_PEER_LIMITS)
  1128. void
  1129. lws_peer_track_wsi_close(struct lws_context *context, struct lws_peer *peer);
  1130. int
  1131. lws_peer_confirm_ah_attach_ok(struct lws_context *context,
  1132. struct lws_peer *peer);
  1133. void
  1134. lws_peer_track_ah_detach(struct lws_context *context, struct lws_peer *peer);
  1135. void
  1136. lws_peer_cull_peer_wait_list(struct lws_context *context);
  1137. struct lws_peer *
  1138. lws_get_or_create_peer(struct lws_vhost *vhost, lws_sockfd_type sockfd);
  1139. void
  1140. lws_peer_add_wsi(struct lws_context *context, struct lws_peer *peer,
  1141. struct lws *wsi);
  1142. void
  1143. lws_peer_dump_from_wsi(struct lws *wsi);
  1144. #endif
  1145. #ifdef LWS_WITH_HUBBUB
  1146. hubbub_error
  1147. html_parser_cb(const hubbub_token *token, void *pw);
  1148. #endif
  1149. int
  1150. lws_threadpool_tsi_context(struct lws_context *context, int tsi);
  1151. void
  1152. __lws_wsi_remove_from_sul(struct lws *wsi);
  1153. void
  1154. lws_validity_confirmed(struct lws *wsi);
  1155. void
  1156. _lws_validity_confirmed_role(struct lws *wsi);
  1157. int
  1158. lws_seq_pt_init(struct lws_context_per_thread *pt);
  1159. int
  1160. lws_buflist_aware_read(struct lws_context_per_thread *pt, struct lws *wsi,
  1161. struct lws_tokens *ebuf, char fr, const char *hint);
  1162. int
  1163. lws_buflist_aware_finished_consuming(struct lws *wsi, struct lws_tokens *ebuf,
  1164. int used, int buffered, const char *hint);
  1165. extern const struct lws_protocols protocol_abs_client_raw_skt,
  1166. protocol_abs_client_unit_test;
  1167. void
  1168. __lws_reset_wsi(struct lws *wsi);
  1169. void
  1170. lws_inform_client_conn_fail(struct lws *wsi, void *arg, size_t len);
  1171. #if defined(LWS_WITH_SYS_ASYNC_DNS)
  1172. lws_async_dns_server_check_t
  1173. lws_plat_asyncdns_init(struct lws_context *context, lws_sockaddr46 *sa);
  1174. int
  1175. lws_async_dns_init(struct lws_context *context);
  1176. void
  1177. lws_async_dns_deinit(lws_async_dns_t *dns);
  1178. #endif
  1179. int
  1180. lws_protocol_init_vhost(struct lws_vhost *vh, int *any);
  1181. int
  1182. _lws_generic_transaction_completed_active_conn(struct lws **wsi, char take_vh_lock);
  1183. #define ACTIVE_CONNS_SOLO 0
  1184. #define ACTIVE_CONNS_MUXED 1
  1185. #define ACTIVE_CONNS_QUEUED 2
  1186. #define ACTIVE_CONNS_FAILED 3
  1187. #if defined(_DEBUG) && !defined(LWS_PLAT_FREERTOS) && !defined(WIN32) && !defined(LWS_PLAT_OPTEE)
  1188. int
  1189. sanity_assert_no_wsi_traces(const struct lws_context *context, struct lws *wsi);
  1190. int
  1191. sanity_assert_no_sockfd_traces(const struct lws_context *context,
  1192. lws_sockfd_type sfd);
  1193. #else
  1194. static inline int sanity_assert_no_wsi_traces(const struct lws_context *context, struct lws *wsi) { (void)context; (void)wsi; return 0; }
  1195. static inline int sanity_assert_no_sockfd_traces(const struct lws_context *context, lws_sockfd_type sfd) { (void)context; (void)sfd; return 0; }
  1196. #endif
  1197. void
  1198. delete_from_fdwsi(const struct lws_context *context, struct lws *wsi);
  1199. int
  1200. lws_vhost_active_conns(struct lws *wsi, struct lws **nwsi, const char *adsin);
  1201. const char *
  1202. lws_wsi_client_stash_item(struct lws *wsi, int stash_idx, int hdr_idx);
  1203. int
  1204. lws_plat_BINDTODEVICE(lws_sockfd_type fd, const char *ifname);
  1205. int
  1206. lws_socks5c_ads_server(struct lws_vhost *vh,
  1207. const struct lws_context_creation_info *info);
  1208. int
  1209. lws_socks5c_handle_state(struct lws *wsi, struct lws_pollfd *pollfd,
  1210. const char **pcce);
  1211. int
  1212. lws_socks5c_greet(struct lws *wsi, const char **pcce);
  1213. int
  1214. lws_plat_mbedtls_net_send(void *ctx, const uint8_t *buf, size_t len);
  1215. int
  1216. lws_plat_mbedtls_net_recv(void *ctx, unsigned char *buf, size_t len);
  1217. lws_usec_t
  1218. lws_sul_nonmonotonic_adjust(struct lws_context *ctx, int64_t step_us);
  1219. void
  1220. lws_netdev_instance_remove_destroy(struct lws_netdev_instance *ni);
  1221. #if defined(LWS_WITH_SYS_SMD)
  1222. int
  1223. lws_netdev_smd_cb(void *opaque, lws_smd_class_t _class, lws_usec_t timestamp,
  1224. void *buf, size_t len);
  1225. #endif
  1226. void
  1227. lws_netdev_instance_create(lws_netdev_instance_t *ni, struct lws_context *ctx,
  1228. const lws_netdev_ops_t *ops, const char *name,
  1229. void *platinfo);
  1230. int
  1231. lws_netdev_wifi_rssi_sort_compare(const lws_dll2_t *d, const lws_dll2_t *i);
  1232. void
  1233. lws_netdev_wifi_scan_empty(lws_netdev_instance_wifi_t *wnd);
  1234. lws_wifi_sta_t *
  1235. lws_netdev_wifi_scan_find(lws_netdev_instance_wifi_t *wnd, const char *ssid,
  1236. const uint8_t *bssid);
  1237. int
  1238. lws_netdev_wifi_scan_select(lws_netdev_instance_wifi_t *wnd);
  1239. lws_wifi_creds_t *
  1240. lws_netdev_credentials_find(lws_netdevs_t *netdevs, const char *ssid,
  1241. const uint8_t *bssid);
  1242. int
  1243. lws_netdev_wifi_redo_last(lws_netdev_instance_wifi_t *wnd);
  1244. void
  1245. lws_ntpc_trigger(struct lws_context *ctx);
  1246. void
  1247. lws_netdev_wifi_scan(lws_sorted_usec_list_t *sul);
  1248. #define lws_netdevs_from_ndi(ni) \
  1249. lws_container_of((ni)->list.owner, lws_netdevs_t, owner)
  1250. #define lws_context_from_netdevs(nd) \
  1251. lws_container_of(nd, struct lws_context, netdevs)
  1252. /* get the owner of the ni, then compute the context the owner is embedded in */
  1253. #define netdev_instance_to_ctx(ni) \
  1254. lws_container_of(lws_netdevs_from_ndi(ni), \
  1255. struct lws_context, netdevs)
  1256. enum {
  1257. LW5CHS_RET_RET0,
  1258. LW5CHS_RET_BAIL3,
  1259. LW5CHS_RET_STARTHS,
  1260. LW5CHS_RET_NOTHING
  1261. };
  1262. #ifdef __cplusplus
  1263. };
  1264. #endif
  1265. #endif