aboutsummaryrefslogtreecommitdiff
path: root/lib/core/private-lib-core.h
blob: a138e44309c943b76be8e79a788d2b02fce78d8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
/*
 * libwebsockets - small server side websockets and web server implementation
 *
 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#if !defined(__LWS_PRIVATE_LIB_CORE_H__)
#define __LWS_PRIVATE_LIB_CORE_H__

#include "lws_config.h"
#include "lws_config_private.h"


#if defined(LWS_WITH_CGI) && defined(LWS_HAVE_VFORK) && \
    !defined(NO_GNU_SOURCE_THIS_TIME) && !defined(_GNU_SOURCE)
 #define  _GNU_SOURCE
#endif

#if defined(LWS_SUPPRESS_DEPRECATED_API_WARNINGS)
#define OPENSSL_SUPPRESS_DEPRECATED
#endif

/*
#if !defined(_POSIX_C_SOURCE)
#define _POSIX_C_SOURCE 200112L
#endif
*/

#ifdef LWS_HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if !defined(PICO_SDK_PATH)
#ifdef LWS_HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#endif

//#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <errno.h>

#include <assert.h>

#if defined(LWS_HAVE_SYS_STAT_H) && !defined(LWS_PLAT_OPTEE)
 #include <sys/stat.h>
#endif

#if LWS_MAX_SMP > 1 || defined(LWS_WITH_SYS_SMD)
 /* https://stackoverflow.com/questions/33557506/timespec-redefinition-error */
 #define HAVE_STRUCT_TIMESPEC
 #include <pthread.h>
#else
 #if !defined(pid_t) && defined(WIN32)
 #define pid_t int
 #endif
#endif

#ifndef LWS_DEF_HEADER_LEN
#define LWS_DEF_HEADER_LEN 4096
#endif
#ifndef LWS_DEF_HEADER_POOL
#define LWS_DEF_HEADER_POOL 4
#endif
#ifndef LWS_MAX_PROTOCOLS
#define LWS_MAX_PROTOCOLS 5
#endif
#ifndef LWS_MAX_EXTENSIONS_ACTIVE
#define LWS_MAX_EXTENSIONS_ACTIVE 1
#endif
#ifndef LWS_MAX_EXT_OFFERS
#define LWS_MAX_EXT_OFFERS 8
#endif
#ifndef SPEC_LATEST_SUPPORTED
#define SPEC_LATEST_SUPPORTED 13
#endif
#ifndef CIPHERS_LIST_STRING
#define CIPHERS_LIST_STRING "DEFAULT"
#endif
#ifndef LWS_SOMAXCONN
#define LWS_SOMAXCONN SOMAXCONN
#endif

#define MAX_WEBSOCKET_04_KEY_LEN 128

#ifndef SYSTEM_RANDOM_FILEPATH
#define SYSTEM_RANDOM_FILEPATH "/dev/urandom"
#endif

#define LWS_H2_RX_SCRATCH_SIZE 512

#define lws_socket_is_valid(x) (x != LWS_SOCK_INVALID)

#ifndef LWS_HAVE_STRERROR
 #define strerror(x) ""
#endif

 /*
  *
  *  ------ private platform defines ------
  *
  */

 #if defined(LWS_PLAT_FREERTOS)
  #include "private-lib-plat-freertos.h"
 #else
  #if defined(WIN32) || defined(_WIN32)
   #include "private-lib-plat-windows.h"
  #else
   #if defined(LWS_PLAT_BAREMETAL)
   #else
    #if defined(LWS_PLAT_OPTEE)
     #include "private-lib-plat.h"
    #else
     #include "private-lib-plat-unix.h"
    #endif
   #endif
  #endif
 #endif

 /*
  *
  *  ------ public api ------
  *
  */

#include "libwebsockets.h"

/*
 * lws_dsh
*/

typedef struct lws_dsh_obj_head {
	lws_dll2_owner_t		owner;
	size_t				total_size; /* for this kind in dsh */
	int				kind;
} lws_dsh_obj_head_t;

typedef struct lws_dsh_obj {
	lws_dll2_t			list;	/* must be first */
	struct lws_dsh	  		*dsh;	/* invalid when on free list */
	size_t				size;	/* invalid when on free list */
	size_t				pos;    /* invalid when on free list */
	size_t				asize;
	int				kind; /* so we can account at free */
} lws_dsh_obj_t;

typedef struct lws_dsh {
	lws_dll2_t			list;
	uint8_t				*buf;
	lws_dsh_obj_head_t		*oha;	/* array of object heads/kind */
	size_t				splitat;
	size_t				buffer_size;
	size_t				locally_in_use;
	size_t				locally_free;
	int				count_kinds;
	uint32_t			flags;
	uint8_t				being_destroyed;
	/*
	 * Overallocations at create:
	 *
	 *  - the buffer itself
	 *  - the object heads array
	 */
} lws_dsh_t;

 /*
  *
  *  ------ lifecycle defines ------
  *
  */

typedef struct lws_lifecycle_group {
	lws_dll2_owner_t		owner; /* active count / list */
	uint64_t			ordinal; /* monotonic uid count */
	const char			*tag_prefix; /* eg, "wsi" */
} lws_lifecycle_group_t;

typedef struct lws_lifecycle {
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
	/* we append parent streams on the tag */
	char				gutag[96]; /* object unique tag + relationship info */
#else
	char				gutag[64];
#endif
	lws_dll2_t			list; /* group list membership */
	uint64_t			us_creation; /* creation timestamp */
	lws_log_cx_t			*log_cx;
} lws_lifecycle_t;

void
__lws_lc_tag(struct lws_context *cx, lws_lifecycle_group_t *grp,
	     lws_lifecycle_t *lc, const char *format, ...);

void
__lws_lc_tag_append(lws_lifecycle_t *lc, const char *app);

void
__lws_lc_untag(struct lws_context *cx, lws_lifecycle_t *lc);

const char *
lws_lc_tag(lws_lifecycle_t *lc);

extern lws_log_cx_t log_cx;

/*
 * Generic bidi tx credit management
 */

struct lws_tx_credit {
	int32_t			tx_cr;		/* our credit to write peer */
	int32_t			peer_tx_cr_est; /* peer's credit to write us */

	int32_t			manual_initial_tx_credit;

	uint8_t			skint; /* unable to write anything */
	uint8_t			manual;
};

#ifdef LWS_WITH_IPV6
#if defined(WIN32) || defined(_WIN32)
#include <iphlpapi.h>
#else
#include <net/if.h>
#endif
#endif

#undef X509_NAME

/*
 * All lws_tls...() functions must return this type, converting the
 * native backend result and doing the extra work to determine which one
 * as needed.
 *
 * Native TLS backend return codes are NOT ALLOWED outside the backend.
 *
 * Non-SSL mode also uses these types.
 */
enum lws_ssl_capable_status {
	LWS_SSL_CAPABLE_ERROR			= -1, /* it failed */
	LWS_SSL_CAPABLE_DONE			= 0,  /* it succeeded */
	LWS_SSL_CAPABLE_MORE_SERVICE_READ	= -2, /* retry WANT_READ */
	LWS_SSL_CAPABLE_MORE_SERVICE_WRITE	= -3, /* retry WANT_WRITE */
	LWS_SSL_CAPABLE_MORE_SERVICE		= -4, /* general retry */
};

enum lws_context_destroy {
	LWSCD_NO_DESTROY,		/* running */
	LWSCD_PT_WAS_DEFERRED,		/* destroy from inside service */
	LWSCD_PT_WAIT_ALL_DESTROYED,	/* libuv ends up here later */
	LWSCD_FINALIZATION		/* the final destruction of context */
};

#if defined(LWS_WITH_TLS)
#include "private-lib-tls.h"
#endif

#if defined(WIN32) || defined(_WIN32)
	 // Visual studio older than 2015 and WIN_CE has only _stricmp
	#if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(_WIN32_WCE)
	#define strcasecmp _stricmp
	#define strncasecmp _strnicmp
	#elif !defined(__MINGW32__)
	#define strcasecmp stricmp
	#define strncasecmp strnicmp
	#endif
	#define getdtablesize() 30000
#endif

#ifndef LWS_ARRAY_SIZE
#define LWS_ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif

#ifdef __cplusplus
extern "C" {
#endif

#define lws_safe_modulo(_a, _b) ((_b) ? ((_a) % (_b)) : 0)

#if defined(__clang__)
#define lws_memory_barrier() __sync_synchronize()
#elif defined(__GNUC__)
#define lws_memory_barrier() __sync_synchronize()
#else
#define lws_memory_barrier()
#endif


struct lws_ring {
	void *buf;
	void (*destroy_element)(void *element);
	uint32_t buflen;
	uint32_t element_len;
	uint32_t head;
	uint32_t oldest_tail;
};

struct lws_protocols;
struct lws;

#include "private-lib-secure-streams.h"

#if defined(LWS_WITH_NETWORK) /* network */
#include "private-lib-event-libs.h"

#if defined(LWS_WITH_SYS_SMD)
#include "private-lib-system-smd.h"
#endif

#if defined(LWS_WITH_SYS_FAULT_INJECTION)
#include "private-lib-system-fault-injection.h"
#endif

#include "private-lib-system-metrics.h"

struct lws_foreign_thread_pollfd {
	struct lws_foreign_thread_pollfd *next;
	int fd_index;
	int _and;
	int _or;
};

#include "private-lib-core-net.h"
#endif /* network */

struct lws_system_blob {
	union {
		struct lws_buflist *bl;
		struct {
			const uint8_t *ptr;
			size_t len;
		} direct;
	} u;
	char	is_direct;
};


typedef struct lws_attach_item {
	lws_dll2_t			list;
	lws_attach_cb_t			cb;
	void				*opaque;
	lws_system_states_t		state;
} lws_attach_item_t;

/*
 * These are the context's lifecycle group indexes that exist in this build
 * configuration.  If you add some, make sure to also add the tag_prefix in
 * context.c context creation with matching preprocessor conditionals.
 */

enum {
	LWSLCG_WSI,			/* generic wsi, eg, pipe, listen */
	LWSLCG_VHOST,

	LWSLCG_WSI_SERVER,		/* server wsi */

#if defined(LWS_ROLE_H2) || defined(LWS_ROLE_MQTT)
	LWSLCG_WSI_MUX,			/* a mux child wsi */
#endif

#if defined(LWS_WITH_CLIENT)
	LWSLCG_WSI_CLIENT,		/* client wsi */
#endif

#if defined(LWS_WITH_SECURE_STREAMS)
#if defined(LWS_WITH_CLIENT)
	LWSLCG_SS_CLIENT,		/* secstream client handle */
#endif
#if defined(LWS_WITH_SERVER)
	LWSLCG_SS_SERVER,		/* secstream server handle */
#endif
#if defined(LWS_WITH_CLIENT)
	LWSLCG_WSI_SS_CLIENT,		/* wsi bound to ss client handle */
#endif
#if defined(LWS_WITH_SERVER)
	LWSLCG_WSI_SS_SERVER,		/* wsi bound to ss server handle */
#endif
#endif

#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
#if defined(LWS_WITH_CLIENT)
	LWSLCG_SSP_CLIENT,		/* SSPC handle client connection to proxy */
#endif
#if defined(LWS_WITH_SERVER)
	LWSLCG_SSP_ONWARD,		/* SS handle at proxy for onward conn */
#endif
#if defined(LWS_WITH_CLIENT)
	LWSLCG_WSI_SSP_CLIENT,		/* wsi bound to SSPC cli conn to proxy */
#endif
#if defined(LWS_WITH_SERVER)
	LWSLCG_WSI_SSP_ONWARD,		/* wsi bound to Proxy onward connection */
#endif
#endif

#if defined(LWS_WITH_SERVER)
	LWSLCG_WSI_SSP_SINK,		/* accepted sink conn */
	LWSLCG_WSI_SSP_SOURCE,		/* accepted source conn */
#endif

	/* always last */
	LWSLCG_COUNT
};

#if defined(LWS_WITH_SECURE_STREAMS) && defined(LWS_WITH_SERVER)
typedef struct lws_ss_sinks {
	lws_dll2_t				list;
	lws_ss_info_t				info;
	lws_dll2_owner_t			accepts;
} lws_ss_sinks_t;
#endif

/*
 * the rest is managed per-context, that includes
 *
 *  - processwide single fd -> wsi lookup
 *  - contextwide headers pool
 */

struct lws_context {
 #if defined(LWS_WITH_SERVER)
	char canonical_hostname[96];
 #endif
#if defined(LWS_HAVE_SSL_CTX_set_keylog_callback) && \
	defined(LWS_WITH_TLS) && defined(LWS_WITH_CLIENT)
	char					keylog_file[96];
#endif

#if defined(LWS_WITH_FILE_OPS)
	struct lws_plat_file_ops fops_platform;
#endif

#if defined(LWS_WITH_ZIP_FOPS)
	struct lws_plat_file_ops fops_zip;
#endif

	lws_system_blob_t system_blobs[LWS_SYSBLOB_TYPE_COUNT];

#if defined(LWS_WITH_SYS_SMD)
	lws_smd_t				smd;
#endif
#if defined(LWS_WITH_SECURE_STREAMS)
	struct lws_ss_handle			*ss_cpd;
#endif
	lws_sorted_usec_list_t			sul_cpd_defer;

#if defined(LWS_WITH_DLO)
	lws_dll2_owner_t			fonts;
	lws_dll2_owner_t			dlo_file;
#if defined(LWS_WITH_SECURE_STREAMS)
	lws_dll2_owner_t			active_assets; /* dloss_t */
#endif
#endif

#if defined(LWS_WITH_NETWORK)
	struct lws_context_per_thread		pt[LWS_MAX_SMP];
	lws_retry_bo_t				default_retry;
	lws_sorted_usec_list_t			sul_system_state;

	lws_lifecycle_group_t			lcg[LWSLCG_COUNT];

	const struct lws_protocols		*protocols_copy;

#if defined(LWS_WITH_NETLINK)
	lws_sorted_usec_list_t			sul_nl_coldplug;
	/* process can only have one netlink socket, have to do it in ctx */
	lws_dll2_owner_t			routing_table;
	struct lws				*netlink;
#endif

#if defined(LWS_PLAT_FREERTOS)
	struct sockaddr_in			frt_pipe_si;
#endif

#if defined(LWS_WITH_HTTP2)
	struct http2_settings			set;
#endif

#if LWS_MAX_SMP > 1
	struct lws_mutex_refcount		mr;
#endif

#if defined(LWS_WITH_SYS_METRICS)
	lws_dll2_owner_t			owner_mtr_dynpol;
	/**< owner for lws_metric_policy_dyn_t (dynamic part of metric pols) */
	lws_dll2_owner_t			owner_mtr_no_pol;
	/**< owner for lws_metric_pub_t with no policy to bind to */
#endif

#if defined(LWS_WITH_NETWORK)
/*
 * LWS_WITH_NETWORK =====>
 */

	lws_dll2_owner_t		owner_vh_being_destroyed;

	lws_metric_t			*mt_service; /* doing service */
	const lws_metric_policy_t	*metrics_policies;
	const char			*metrics_prefix;

#if defined(LWS_WITH_SYS_METRICS) && defined(LWS_WITH_CLIENT)
	lws_metric_t			*mt_conn_tcp; /* client tcp conns */
	lws_metric_t			*mt_conn_tls; /* client tcp conns */
	lws_metric_t			*mt_conn_dns; /* client dns external lookups */
	lws_metric_t			*mth_conn_failures; /* histogram of conn failure reasons */
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
	lws_metric_t			*mt_http_txn; /* client http transaction */
#endif
#if defined(LWS_WITH_SYS_ASYNC_DNS)
	lws_metric_t			*mt_adns_cache; /* async dns lookup lat */
#endif
#if defined(LWS_WITH_SECURE_STREAMS)
	lws_metric_t			*mth_ss_conn; /* SS connection outcomes */
#endif
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
	lws_metric_t			*mt_ss_cliprox_conn; /* SS cli->prox conn */
	lws_metric_t			*mt_ss_cliprox_paylat; /* cli->prox payload latency */
	lws_metric_t			*mt_ss_proxcli_paylat; /* prox->cli payload latency */
#endif
#endif /* client */

#if defined(LWS_WITH_SERVER)
	lws_metric_t			*mth_srv;
#endif

#if defined(LWS_WITH_EVENT_LIBS)
	struct lws_plugin		*evlib_plugin_list;
	void				*evlib_ctx; /* overallocated */
#endif

#if defined(LWS_WITH_TLS)
	struct lws_context_tls		tls;
#if defined (LWS_WITH_TLS_JIT_TRUST)
	lws_dll2_owner_t		jit_inflight;
	/* ongoing sync or async jit trust lookups */
	struct lws_cache_ttl_lru	*trust_cache;
	/* caches host -> truncated trust SKID mappings */
#endif
#endif
#if defined(LWS_WITH_DRIVERS)
	lws_netdevs_t			netdevs;
#endif

#if defined(LWS_WITH_SYS_ASYNC_DNS)
	lws_async_dns_t			async_dns;
#endif

#if defined(LWS_WITH_SYS_FAULT_INJECTION)
	lws_fi_ctx_t			fic;
	/**< Toplevel Fault Injection ctx */
#endif

#if defined(LWS_WITH_CACHE_NSCOOKIEJAR) && defined(LWS_WITH_CLIENT)
	struct lws_cache_ttl_lru *l1, *nsc;
#endif

#if defined(LWS_WITH_SYS_NTPCLIENT)
	void				*ntpclient_priv;
#endif

#if defined(LWS_WITH_SECURE_STREAMS)
	struct lws_ss_handle		*hss_fetch_policy;
#if defined(LWS_WITH_SECURE_STREAMS_SYS_AUTH_API_AMAZON_COM)
	struct lws_ss_handle		*hss_auth;
	lws_sorted_usec_list_t		sul_api_amazon_com;
	lws_sorted_usec_list_t		sul_api_amazon_com_kick;
#endif
#if !defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY)
	struct lws_ss_x509		*server_der_list;
#endif
#endif

#if defined(LWS_WITH_SYS_STATE)
	lws_state_manager_t		mgr_system;
	lws_state_notify_link_t		protocols_notify;
#endif
#if defined (LWS_WITH_SYS_DHCP_CLIENT)
	lws_dll2_owner_t		dhcpc_owner;
					/**< list of ifaces with dhcpc */
#endif

	/* pointers */

	struct lws_vhost		*vhost_list;
	struct lws_vhost		*no_listener_vhost_list;
	struct lws_vhost		*vhost_pending_destruction_list;
	struct lws_vhost		*vhost_system;

#if defined(LWS_WITH_SERVER)
	const char			*server_string;
#endif

	const struct lws_event_loop_ops	*event_loop_ops;
#endif

#if defined(LWS_WITH_TLS)
	const struct lws_tls_ops	*tls_ops;
#endif

#if defined(LWS_WITH_PLUGINS)
	struct lws_plugin		*plugin_list;
#endif
#ifdef _WIN32
/* different implementation between unix and windows */
	struct lws_fd_hashtable fd_hashtable[FD_HASHTABLE_MODULUS];
#else
	struct lws **lws_lookup;

#endif

#if defined(LWS_WITH_OTA)
	lws_sorted_usec_list_t		sul_ota_periodic;
	lws_ss_handle_t			* ota_ss;	/* opaque to platform */
#endif

/*
 * <====== LWS_WITH_NETWORK end
 */

#endif /* NETWORK */

	lws_log_cx_t				*log_cx;
	const char				*name;

#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
	const char				*ss_proxy_bind;
	const char				*ss_proxy_address;

	lws_txp_path_proxy_t			txp_ppath;
	lws_txp_path_client_t			txp_cpath;

	const void				*txp_ssproxy_info;

#endif

#if defined(LWS_WITH_FILE_OPS)
	const struct lws_plat_file_ops *fops;
#endif

	struct lws_context **pcontext_finalize;
#if !defined(LWS_PLAT_FREERTOS)
	const char *username, *groupname;
#endif

#if defined(LWS_WITH_MBEDTLS)
	mbedtls_entropy_context mec;
	mbedtls_ctr_drbg_context mcdc;
#endif

#if defined(LWS_WITH_THREADPOOL) && defined(LWS_HAVE_PTHREAD_H)
	struct lws_threadpool *tp_list_head;
#endif

#if defined(LWS_WITH_PEER_LIMITS)
	struct lws_peer			**pl_hash_table;
	struct lws_peer			*peer_wait_list;
	lws_peer_limits_notify_t	pl_notify_cb;
	time_t				next_cull;
#endif

	const lws_system_ops_t		*system_ops;

#if defined(LWS_WITH_SECURE_STREAMS)
#if !defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY)
	const char			*pss_policies_json;
	struct lwsac			*ac_policy;
	void				*pol_args;
#endif
	const lws_ss_policy_t		*pss_policies;
	const lws_ss_auth_t		*pss_auths;
#if defined(LWS_WITH_SERVER)
	lws_dll2_owner_t		sinks;
#endif
#if defined(LWS_WITH_SSPLUGINS)
	const lws_ss_plugin_t		**pss_plugins;
#endif
#endif

	void *external_baggage_free_on_destroy;
	const struct lws_token_limits *token_limits;
	void *user_space;
#if defined(LWS_WITH_SERVER)
	const struct lws_protocol_vhost_options *reject_service_keywords;
	lws_reload_func deprecation_cb;
#endif
#if !defined(LWS_PLAT_FREERTOS)
	void (*eventlib_signal_cb)(void *event_lib_handle, int signum);
#endif

#if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
	cap_value_t caps[4];
	char count_caps;
#endif

	lws_usec_t time_up; /* monotonic */
#if defined(LWS_WITH_SYS_SMD)
	lws_usec_t smd_ttl_us;
#endif
	uint64_t options;

	time_t last_ws_ping_pong_check_s;
#if defined(LWS_WITH_SECURE_STREAMS)
	time_t					last_policy;
#endif

#if defined(LWS_PLAT_FREERTOS)
	unsigned long time_last_state_dump;
	uint32_t last_free_heap;
#endif

	unsigned int max_fds;
#if !defined(LWS_NO_DAEMONIZE)
	pid_t started_with_parent;
#endif

#if !defined(LWS_PLAT_FREERTOS)
	uid_t uid;
	gid_t gid;
	int fd_random;
	int count_cgi_spawned;
#endif
#if defined(WIN32)
	unsigned int win32_connect_check_interval_usec;
#endif

	unsigned int fd_limit_per_thread;
	unsigned int timeout_secs;
	unsigned int pt_serv_buf_size;
	unsigned int max_http_header_data;
	unsigned int max_http_header_pool;
	int simultaneous_ssl_restriction;
	int simultaneous_ssl;
	int simultaneous_ssl_handshake_restriction;
	int simultaneous_ssl_handshake;
#if defined(LWS_WITH_TLS_JIT_TRUST)
	int		vh_idle_grace_ms;
#endif
#if defined(LWS_WITH_PEER_LIMITS)
	uint32_t pl_hash_elements;	/* protected by context->lock */
	uint32_t count_peers;		/* protected by context->lock */
	unsigned short ip_limit_ah;
	unsigned short ip_limit_wsi;
#endif

#if defined(LWS_WITH_SYS_SMD)
	uint16_t smd_queue_depth;
#endif

#if defined(LWS_WITH_NETLINK) && defined(LWS_WITH_NETWORK)
	lws_route_uidx_t			route_uidx;
#endif

	char		tls_gate_accepts;

	unsigned int deprecated:1;
	unsigned int interrupted:1;
	unsigned int inside_context_destroy:1;
	unsigned int being_destroyed:1;
	unsigned int service_no_longer_possible:1;
	unsigned int being_destroyed2:1;
	unsigned int requested_stop_internal_loops:1;
	unsigned int protocol_init_done:1;
	unsigned int doing_protocol_init:1;
	unsigned int done_protocol_destroy_cb:1;
	unsigned int evlib_finalize_destroy_after_int_loops_stop:1;
	unsigned int max_fds_unrelated_to_ulimit:1;
	unsigned int policy_updated:1;
#if defined(LWS_WITH_NETLINK)
	unsigned int nl_initial_done:1;
#endif

	unsigned short count_threads;
	unsigned short undestroyed_threads;
	short plugin_protocol_count;
	short plugin_extension_count;
	short server_string_len;
	unsigned short deprecation_pending_listen_close_count;
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
	uint16_t	ss_proxy_port;
#endif
	/* 0 if not known, else us resolution of the poll wait */
	uint16_t us_wait_resolution;

	uint8_t max_fi;
	uint8_t captive_portal_detect;
	uint8_t captive_portal_detect_type;

	uint8_t		destroy_state; /* enum lws_context_destroy */
};

#define lws_get_context_protocol(ctx, x) ctx->vhost_list->protocols[x]
#define lws_get_vh_protocol(vh, x) vh->protocols[x]

int
lws_jws_base64_enc(const char *in, size_t in_len, char *out, size_t out_max);

void
lws_vhost_destroy1(struct lws_vhost *vh);

#if defined(LWS_WITH_CACHE_NSCOOKIEJAR) && defined(LWS_WITH_CLIENT)
int
lws_parse_set_cookie(struct lws *wsi);

int
lws_cookie_send_cookies(struct lws *wsi, char **pp, char *end);
#endif

#if defined(LWS_PLAT_FREERTOS)
int
lws_find_string_in_file(const char *filename, const char *str, int stringlen);
#endif

signed char char_to_hex(const char c);

#if defined(LWS_WITH_NETWORK)
int
lws_system_do_attach(struct lws_context_per_thread *pt);
#endif

struct lws_buflist {
	struct lws_buflist *next;
	size_t len;
	size_t pos;
};

char *
lws_strdup(const char *s);

int
lws_b64_selftest(void);

#define FIRST_LENGTH_CODE_INDEX		257
#define LAST_LENGTH_CODE_INDEX		285

/*256 literals, the end code, some length codes, and 2 unused codes */
#define NUM_DEFLATE_CODE_SYMBOLS	288
/*the distance codes have their own symbols, 30 used, 2 unused */
#define NUM_DISTANCE_SYMBOLS		32
/* The code length codes. 0-15: code lengths, 16: copy previous 3-6 times,
 * 17: 3-10 zeros, 18: 11-138 zeros */
#define NUM_CODE_LENGTH_CODES		19
/* largest number of symbols used by any tree type */
#define MAX_SYMBOLS			288

#define DEFLATE_CODE_BITLEN		15
#define DISTANCE_BITLEN			15
#define CODE_LENGTH_BITLEN		7
/* largest bitlen used by any tree type */
#define MAX_BIT_LENGTH			15

#define DEFLATE_CODE_BUFFER_SIZE	(NUM_DEFLATE_CODE_SYMBOLS * 2)
#define DISTANCE_BUFFER_SIZE		(NUM_DISTANCE_SYMBOLS * 2)
#define CODE_LENGTH_BUFFER_SIZE		(NUM_DISTANCE_SYMBOLS * 2)

typedef uint16_t huff_t;

typedef enum {
	UPNS_ID_BL_GB_DONE,
	UPNS_ID_BL_GB_BTYPEb0,
	UPNS_ID_BL_GB_BTYPEb1,

	UPNS_ID_BL_GB_BTYPE_0,
	UPNS_ID_BL_GB_BTYPE_1,
	UPNS_ID_BL_GB_BTYPE_2,

	UPNS_ID_BL_GB_BTYPE_0a,
	UPNS_ID_BL_GB_BTYPE_0b,
	UPNS_ID_BL_GB_BTYPE_0c,
	UPNS_ID_BL_GB_BTYPE_0d,

	UPNS_ID_BL_GB_BTYPE_2a,
	UPNS_ID_BL_GB_BTYPE_2b,
	UPNS_ID_BL_GB_BTYPE_2c,
	UPNS_ID_BL_GB_BTYPE_2d,
	UPNS_ID_BL_GB_BTYPE_2e,

	UPNS_ID_BL_GB_BTYPE_2_16,
	UPNS_ID_BL_GB_BTYPE_2_17,
	UPNS_ID_BL_GB_BTYPE_2_18,

	UPNS_ID_BL_GB_SPIN,

	UPNS_ID_BL_GB_SPINa,
	UPNS_ID_BL_GB_SPINb,
	UPNS_ID_BL_GB_SPINc,
	UPNS_ID_BL_GB_SPINd,
	UPNS_ID_BL_GB_SPINe,

	UPNS_ID_BL_GB_GZIP_ID1,
	UPNS_ID_BL_GB_GZIP_ID2,
	UPNS_ID_BL_GB_GZIP_METHOD,
	UPNS_ID_BL_GB_GZIP_FLAGS,
	UPNS_ID_BL_GB_GZIP_EOH,
	UPNS_ID_BL_GB_GZIP_SKIP_EXTRA_C1,
	UPNS_ID_BL_GB_GZIP_SKIP_EXTRA_C2,
	UPNS_ID_BL_GB_GZIP_SKIP_EXTRA,
	UPNS_ID_BL_GB_GZIP_SKIP_FILENAME,
	UPNS_ID_BL_GB_GZIP_SKIP_COMMENT,
	UPNS_ID_BL_GB_GZIP_SKIP_CRC,

} upng_inflate_states_t;

typedef struct htree {
	huff_t			*tree2d;
	/*maximum number of bits a single code can get */
	uint16_t		maxbitlen;
	/*number of symbols in the alphabet = number of codes */
	uint16_t		numcodes;
} htree_t;

typedef struct inflator_ctx {
	unsigned int		clenc[NUM_CODE_LENGTH_CODES];
	unsigned int		bitlen[NUM_DEFLATE_CODE_SYMBOLS];
	unsigned int		bitlenD[NUM_DISTANCE_SYMBOLS];
	huff_t			clct_buffer[CODE_LENGTH_BUFFER_SIZE];
	huff_t			ct_buffer[DEFLATE_CODE_BUFFER_SIZE];
	huff_t			ctD_buffer[DISTANCE_BUFFER_SIZE];

	lws_upng_t		*upng;

	const uint8_t		*in;
	uint8_t			*out;

	htree_t			clct;
	htree_t			ct;
	htree_t			ctD;

	size_t			bp;
	size_t			inpos;
	size_t			inlen;
	size_t			archive_pos;
	size_t			outpos;
	size_t			outpos_linear;
	size_t			consumed_linear;
	size_t			outlen;
	size_t			length;
	size_t			start;
	size_t			forward;
	size_t			backward;
	size_t			exbits;
	size_t			bypl;

	upng_inflate_states_t	state;

	unsigned int		len;
	unsigned int		nlen;
	unsigned int		n;
	unsigned int		hlit;
	unsigned int		hdist;
	unsigned int		hclen;
	unsigned int		i;
	unsigned int		t;
	unsigned int		codeD;
	unsigned int		distance;
	unsigned int		exbitsD;
	unsigned int		code;
	unsigned int		treepos;

	unsigned int		read_bits_shifter;
	unsigned int		read_bits_limit;
	unsigned int 		read_bits_i;

	unsigned int		info_size;

	uint16_t		ctr;
	uint8_t			subsequent;
	uint8_t			btype;
	uint8_t			done;
	uint8_t			gz_flags;

	char			read_bits_ongoing;
} inflator_ctx_t;

lws_stateful_ret_t
_lws_upng_inflate_data(inflator_ctx_t *inf);

#ifndef LWS_NO_DAEMONIZE
 pid_t get_daemonize_pid();
#else
 #define get_daemonize_pid() (0)
#endif

void lwsl_emit_stderr(int level, const char *line);

#if !defined(LWS_WITH_TLS)
 #define LWS_SSL_ENABLED(context) (0)
 #define lws_context_init_server_ssl(_a, _b) (0)
 #define lws_ssl_destroy(_a)
 #define lws_context_init_alpn(_a)
 #define lws_ssl_capable_read lws_ssl_capable_read_no_ssl
 #define lws_ssl_capable_write lws_ssl_capable_write_no_ssl
 #define lws_ssl_pending lws_ssl_pending_no_ssl
 #define lws_server_socket_service_ssl(_b, _c, _d) (0)
 #define lws_ssl_close(_a) (0)
 #define lws_ssl_context_destroy(_a)
 #define lws_ssl_SSL_CTX_destroy(_a)
 #define lws_ssl_remove_wsi_from_buffered_list(_a)
 #define __lws_ssl_remove_wsi_from_buffered_list(_a)
 #define lws_context_init_ssl_library(_a, _b)
 #define lws_context_deinit_ssl_library(_a)
 #define lws_tls_check_all_cert_lifetimes(_a)
 #define lws_tls_acme_sni_cert_destroy(_a)
#endif



#if LWS_MAX_SMP > 1
#define lws_context_lock(c, reason) lws_mutex_refcount_lock(&c->mr, reason)
#define lws_context_unlock(c) lws_mutex_refcount_unlock(&c->mr)
#define lws_context_assert_lock_held(c) lws_mutex_refcount_assert_held(&c->mr)
#define lws_vhost_assert_lock_held(v) lws_mutex_refcount_assert_held(&v->mr)
/* enforce context lock held */
#define lws_vhost_lock(v) lws_mutex_refcount_lock(&v->mr, __func__)
#define lws_vhost_unlock(v) lws_mutex_refcount_unlock(&v->mr)


#else
#define lws_pt_mutex_init(_a) (void)(_a)
#define lws_pt_mutex_destroy(_a) (void)(_a)
#define lws_pt_lock(_a, b) (void)(_a)
#define lws_pt_assert_lock_held(_a) (void)(_a)
#define lws_pt_unlock(_a) (void)(_a)
#define lws_context_lock(_a, _b) (void)(_a)
#define lws_context_unlock(_a) (void)(_a)
#define lws_context_assert_lock_held(_a) (void)(_a)
#define lws_vhost_assert_lock_held(_a) (void)(_a)
#define lws_vhost_lock(_a) (void)(_a)
#define lws_vhost_unlock(_a) (void)(_a)
#define lws_pt_stats_lock(_a) (void)(_a)
#define lws_pt_stats_unlock(_a) (void)(_a)
#endif

void
lws_ota_periodic_cb(lws_sorted_usec_list_t *sul);

int LWS_WARN_UNUSED_RESULT
lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, size_t len);

int LWS_WARN_UNUSED_RESULT
lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, size_t len);

int LWS_WARN_UNUSED_RESULT
lws_ssl_pending_no_ssl(struct lws *wsi);

int
lws_tls_check_cert_lifetime(struct lws_vhost *vhost);

int lws_jws_selftest(void);
int lws_jwe_selftest(void);

int
lws_protocol_init(struct lws_context *context);

int
lws_bind_protocol(struct lws *wsi, const struct lws_protocols *p,
		  const char *reason);

const struct lws_protocol_vhost_options *
lws_vhost_protocol_options(struct lws_vhost *vh, const char *name);

const struct lws_http_mount *
lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len);

#ifdef LWS_WITH_HTTP2
int lws_wsi_is_h2(struct lws *wsi);
#endif
/*
 * custom allocator
 */
void *
lws_realloc(void *ptr, size_t size, const char *reason);

void * LWS_WARN_UNUSED_RESULT
lws_zalloc(size_t size, const char *reason);

#ifdef LWS_PLAT_OPTEE
void *lws_malloc(size_t size, const char *reason);
void lws_free(void *p);
#define lws_free_set_NULL(P)    do { lws_free(P); (P) = NULL; } while(0)
#else
#define lws_malloc(S, R)	lws_realloc(NULL, S, R)
#define lws_free(P)	lws_realloc(P, 0, "lws_free")
#define lws_free_set_NULL(P)	do { lws_realloc(P, 0, "free"); (P) = NULL; } while(0)
#endif

int
__lws_create_event_pipes(struct lws_context *context);

int
lws_plat_apply_FD_CLOEXEC(int n);

const struct lws_plat_file_ops *
lws_vfs_select_fops(const struct lws_plat_file_ops *fops, const char *vfs_path,
		    const char **vpath);

/* lws_plat_ */

int
lws_plat_context_early_init(void);
void
lws_plat_context_early_destroy(struct lws_context *context);
void
lws_plat_context_late_destroy(struct lws_context *context);

int
lws_plat_init(struct lws_context *context,
	      const struct lws_context_creation_info *info);
int
lws_plat_drop_app_privileges(struct lws_context *context, int actually_drop);

#if defined(LWS_WITH_UNIX_SOCK) && !defined(WIN32)
int
lws_plat_user_colon_group_to_ids(const char *u_colon_g, uid_t *puid, gid_t *pgid);
#endif

int
lws_plat_ntpclient_config(struct lws_context *context);

int
lws_plat_ifname_to_hwaddr(int fd, const char *ifname, uint8_t *hwaddr, int len);

int
lws_plat_vhost_tls_client_ctx_init(struct lws_vhost *vhost);

int
lws_check_byte_utf8(unsigned char state, unsigned char c);
int LWS_WARN_UNUSED_RESULT
lws_check_utf8(unsigned char *state, unsigned char *buf, size_t len);
int alloc_file(struct lws_context *context, const char *filename,
			  uint8_t **buf, lws_filepos_t *amount);

int
lws_lec_scratch(lws_lec_pctx_t *ctx);
void
lws_lec_signed(lws_lec_pctx_t *ctx, int64_t num);

int
lws_cose_key_checks(const lws_cose_key_t *key, int64_t kty, int64_t alg,
		    int key_op, const char *crv);

void lws_msleep(unsigned int);

void
lws_context_destroy2(struct lws_context *context);

/* it's public extern const lws_transport_client_ops_t lws_txp_inside_sspc; */

lws_transport_mux_ch_t *
lws_transport_mux_create_channel(lws_transport_mux_t *tm, lws_mux_ch_idx_t i);

lws_transport_mux_ch_t *
lws_transport_mux_add_channel(lws_transport_mux_t *tm, lws_transport_priv_t priv);

void
lws_transport_mux_destroy_channel(lws_transport_mux_ch_t **_mc);

lws_transport_mux_ch_t *
lws_transport_mux_get_channel(lws_transport_mux_t *tm, lws_mux_ch_idx_t i);

int
lws_transport_mux_next_free(lws_transport_mux_t *tm, lws_mux_ch_idx_t *result);

void
sul_ping_cb(lws_sorted_usec_list_t *sul);

/* Added Declaration of this function to make common for openssl-server */
#if defined(LWS_HAVE_SSL_CTX_set_keylog_callback) && \
	defined(LWS_WITH_TLS)
void
lws_klog_dump(const SSL *ssl, const char *line);
#endif

#if !defined(PRIu64)
#define PRIu64 "llu"
#endif

#if defined(LWS_WITH_ABSTRACT)
#include "private-lib-abstract.h"
#endif

#ifdef __cplusplus
};
#endif

#endif