-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibwebsockets.h
2534 lines (2187 loc) · 84.1 KB
/
libwebsockets.h
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
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010-2016 Andy Green <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation:
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#ifndef LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
#define LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
#ifdef __cplusplus
#include <cstddef>
#include <cstdarg>
#ifdef MBED_OPERATORS
#include "mbed-drivers/mbed.h"
#include "sal-iface-eth/EthernetInterface.h"
#include "sockets/TCPListener.h"
#include "sal-stack-lwip/lwipv4_init.h"
namespace {
}
using namespace mbed::Sockets::v0;
struct sockaddr_in;
struct lws;
class lws_conn {
public:
lws_conn():
ts(NULL),
wsi(NULL),
writeable(1),
awaiting_on_writeable(0)
{
}
public:
void set_wsi(struct lws *_wsi) { wsi = _wsi; }
int actual_onRX(Socket *s);
void onRX(Socket *s);
void onError(Socket *s, socket_error_t err);
void onDisconnect(TCPStream *s);
void onSent(Socket *s, uint16_t len);
void serialized_writeable(struct lws *wsi);
public:
TCPStream *ts;
public:
struct lws *wsi;
char writeable;
char awaiting_on_writeable;
};
class lws_conn_listener : lws_conn {
public:
lws_conn_listener():
srv(SOCKET_STACK_LWIP_IPV4)
{
srv.setOnError(TCPStream::ErrorHandler_t(this,
&lws_conn_listener::onError));
}
void start(const uint16_t port);
protected:
void onRX(Socket *s);
void onError(Socket *s, socket_error_t err);
void onIncoming(TCPListener *s, void *impl);
void onDisconnect(TCPStream *s);
public:
TCPListener srv;
};
#endif
extern "C" {
#else
#include <stdarg.h>
#endif
#ifdef MBED_OPERATORS
struct sockaddr_in;
#define LWS_POSIX 0
#else
#define LWS_POSIX 1
#endif
#include "lws_config.h"
#if defined(WIN32) || defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stddef.h>
#include <basetsd.h>
#ifndef _WIN32_WCE
#include <stdint.h>
#include <fcntl.h>
#else
#define _O_RDONLY 0x0000
#define O_RDONLY _O_RDONLY
#endif
// Visual studio older than 2015 and WIN_CE has only _stricmp
#if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(_WIN32_WCE)
#define strcasecmp _stricmp
#else
#define strcasecmp stricmp
#endif
#define getdtablesize() 30000
#define LWS_INLINE __inline
#define LWS_VISIBLE
#define LWS_WARN_UNUSED_RESULT
#define LWS_WARN_DEPRECATED
#ifdef LWS_DLL
#ifdef LWS_INTERNAL
#define LWS_EXTERN extern __declspec(dllexport)
#else
#define LWS_EXTERN extern __declspec(dllimport)
#endif
#else
#define LWS_EXTERN
#endif
#define LWS_INVALID_FILE INVALID_HANDLE_VALUE
#define LWS_O_RDONLY _O_RDONLY
#if !defined(_MSC_VER) || _MSC_VER < 1900 /* Visual Studio 2015 already defines this in <stdio.h> */
#define snprintf _snprintf
#endif
#ifndef __func__
#define __func__ __FUNCTION__
#endif
#else /* NOT WIN32 */
#include <unistd.h>
#if defined(__NetBSD__) || defined(__FreeBSD__)
#include <netinet/in.h>
#endif
#define LWS_INLINE inline
#define LWS_O_RDONLY O_RDONLY
#ifndef MBED_OPERATORS
#include <poll.h>
#include <netdb.h>
#define LWS_INVALID_FILE -1
#else
#define getdtablesize() (20)
#define LWS_INVALID_FILE NULL
#endif
#if defined(__GNUC__)
/* warn_unused_result attribute only supported by GCC 3.4 or later */
#if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
#define LWS_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#else
#define LWS_WARN_UNUSED_RESULT
#endif
#define LWS_VISIBLE __attribute__((visibility("default")))
#define LWS_WARN_DEPRECATED __attribute__ ((deprecated))
#else
#define LWS_VISIBLE
#define LWS_WARN_UNUSED_RESULT
#define LWS_WARN_DEPRECATED
#endif
#if defined(__ANDROID__)
#include <unistd.h>
#define getdtablesize() sysconf(_SC_OPEN_MAX)
#endif
#endif
#ifdef LWS_USE_LIBEV
#include <ev.h>
#endif /* LWS_USE_LIBEV */
#ifdef LWS_USE_LIBUV
#include <uv.h>
#endif /* LWS_USE_LIBUV */
#ifndef LWS_EXTERN
#define LWS_EXTERN extern
#endif
#ifdef _WIN32
#define random rand
#else
#include <sys/time.h>
#include <unistd.h>
#endif
#ifdef LWS_OPENSSL_SUPPORT
#ifdef USE_WOLFSSL
#ifdef USE_OLD_CYASSL
#include <cyassl/openssl/ssl.h>
#include <cyassl/error-ssl.h>
#else
#include <wolfssl/openssl/ssl.h>
#include <wolfssl/error-ssl.h>
#endif /* not USE_OLD_CYASSL */
#else
#if defined(LWS_USE_POLARSSL)
#include <polarssl/ssl.h>
struct lws_polarssl_context {
x509_crt ca;
x509_crt certificate;
rsa_context key;
};
typedef struct lws_polarssl_context SSL_CTX;
typedef ssl_context SSL;
#else
#if defined(LWS_USE_MBEDTLS)
#include <mbedtls/ssl.h>
#else
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif /* not USE_MBEDTLS */
#endif /* not USE_POLARSSL */
#endif /* not USE_WOLFSSL */
#endif
#define CONTEXT_PORT_NO_LISTEN -1
enum lws_log_levels {
LLL_ERR = 1 << 0,
LLL_WARN = 1 << 1,
LLL_NOTICE = 1 << 2,
LLL_INFO = 1 << 3,
LLL_DEBUG = 1 << 4,
LLL_PARSER = 1 << 5,
LLL_HEADER = 1 << 6,
LLL_EXT = 1 << 7,
LLL_CLIENT = 1 << 8,
LLL_LATENCY = 1 << 9,
LLL_COUNT = 10 /* set to count of valid flags */
};
#ifdef __cplusplus
extern "C"{
#endif
LWS_VISIBLE LWS_EXTERN void _lws_log(int filter, const char *format, ...);
LWS_VISIBLE LWS_EXTERN void _lws_logv(int filter, const char *format, va_list vl);
LWS_VISIBLE LWS_EXTERN int
lwsl_timestamp(int level, char *p, int len);
#ifdef __cplusplus
}
#endif
/* notice, warn and log are always compiled in */
#define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__)
#define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__)
#define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__)
/*
* weaker logging can be deselected at configure time using --disable-debug
* that gets rid of the overhead of checking while keeping _warn and _err
* active
*/
#ifdef _DEBUG
#define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__)
#define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__)
#define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__)
#define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__)
#define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__)
#define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__)
#define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__)
LWS_VISIBLE LWS_EXTERN void lwsl_hexdump(void *buf, size_t len);
#else /* no debug */
#define lwsl_info(...) {}
#define lwsl_debug(...) {}
#define lwsl_parser(...) {}
#define lwsl_header(...) {}
#define lwsl_ext(...) {}
#define lwsl_client(...) {}
#define lwsl_latency(...) {}
#define lwsl_hexdump(a, b)
#endif
#include <stddef.h>
#ifndef lws_container_of
#define lws_container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
#endif
struct lws;
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif
/* api change list for user code to test against */
#define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
/* the struct lws_protocols has the id field present */
#define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
/* you can call lws_get_peer_write_allowance */
#define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
/* extra parameter introduced in 917f43ab821 */
#define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
/* File operations stuff exists */
#define LWS_FEATURE_FOPS
/*
* NOTE: These public enums are part of the abi. If you want to add one,
* add it at where specified so existing users are unaffected.
*/
/**
* enum lws_context_options() - context + vhost options
*
* LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT: (VH) Don't allow the
* connection unless the client has a client cert that we recognize;
* provides LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT
*
* LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME: (CTX) Don't try to get the
* server's hostname
*
* LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT: (VH) Allow non-SSL (plaintext)
* connections on the same port as SSL is listening... undermines the
* security of SSL; provides LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT
*
* LWS_SERVER_OPTION_LIBEV: (CTX) Use libev event loop
*
* LWS_SERVER_OPTION_DISABLE_IPV6: (VH) Disable IPV6 support
*
* LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS: (VH) Don't load OS CA certs, you
* will need to load your own CA cert(s)
*
* LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED: (VH) Accept connections with no
* valid Cert (eg, selfsigned)
*
* LWS_SERVER_OPTION_VALIDATE_UTF8: (VH) Check UT-8 correctness
*
* LWS_SERVER_OPTION_SSL_ECDH: (VH) initialize ECDH ciphers
*
* LWS_SERVER_OPTION_LIBUV: (CTX) Use libuv event loop
*
* LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS: (VH) Use http redirect to force
* http to https (deprecated: use mount redirection)
*
* LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT: (CTX) Initialize the SSL library
* at all
*
* LWS_SERVER_OPTION_EXPLICIT_VHOSTS: (CTX) Only create the context when
* calling context create api, user code will create its own vhosts
*
* LWS_SERVER_OPTION_UNIX_SOCK: (VH) Use Unix socket
*
* LWS_SERVER_OPTION_STS: (VH) Send Strict Transport Security header, making
* clients subsequently go to https even if user asked for http
*
* LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY: (VH) Enable
* LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE to take effect
*
* LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE: (VH) if set, only ipv6 allowed on the
* vhost
*/
enum lws_context_options {
LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = (1 << 1) |
(1 << 12),
LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = (1 << 2),
LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT = (1 << 3) |
(1 << 12),
LWS_SERVER_OPTION_LIBEV = (1 << 4),
LWS_SERVER_OPTION_DISABLE_IPV6 = (1 << 5),
LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS = (1 << 6),
LWS_SERVER_OPTION_PEER_CERT_NOT_REQUIRED = (1 << 7),
LWS_SERVER_OPTION_VALIDATE_UTF8 = (1 << 8),
LWS_SERVER_OPTION_SSL_ECDH = (1 << 9) |
(1 << 12),
LWS_SERVER_OPTION_LIBUV = (1 << 10),
LWS_SERVER_OPTION_REDIRECT_HTTP_TO_HTTPS = (1 << 11) |
(1 << 3) |
(1 << 12),
LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT = (1 << 12),
LWS_SERVER_OPTION_EXPLICIT_VHOSTS = (1 << 13),
LWS_SERVER_OPTION_UNIX_SOCK = (1 << 14),
LWS_SERVER_OPTION_STS = (1 << 15),
LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY = (1 << 16),
LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE = (1 << 17),
/****** add new things just above ---^ ******/
};
#define lws_check_opt(c, f) (((c) & (f)) == (f))
/*
* NOTE: These public enums are part of the abi. If you want to add one,
* add it at where specified so existing users are unaffected.
*/
enum lws_callback_reasons {
LWS_CALLBACK_ESTABLISHED = 0,
LWS_CALLBACK_CLIENT_CONNECTION_ERROR = 1,
LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH = 2,
LWS_CALLBACK_CLIENT_ESTABLISHED = 3,
LWS_CALLBACK_CLOSED = 4,
LWS_CALLBACK_CLOSED_HTTP = 5,
LWS_CALLBACK_RECEIVE = 6,
LWS_CALLBACK_RECEIVE_PONG = 7,
LWS_CALLBACK_CLIENT_RECEIVE = 8,
LWS_CALLBACK_CLIENT_RECEIVE_PONG = 9,
LWS_CALLBACK_CLIENT_WRITEABLE = 10,
LWS_CALLBACK_SERVER_WRITEABLE = 11,
LWS_CALLBACK_HTTP = 12,
LWS_CALLBACK_HTTP_BODY = 13,
LWS_CALLBACK_HTTP_BODY_COMPLETION = 14,
LWS_CALLBACK_HTTP_FILE_COMPLETION = 15,
LWS_CALLBACK_HTTP_WRITEABLE = 16,
LWS_CALLBACK_FILTER_NETWORK_CONNECTION = 17,
LWS_CALLBACK_FILTER_HTTP_CONNECTION = 18,
LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED = 19,
LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION = 20,
LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS = 21,
LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS = 22,
LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION = 23,
LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER = 24,
LWS_CALLBACK_CONFIRM_EXTENSION_OKAY = 25,
LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED = 26,
LWS_CALLBACK_PROTOCOL_INIT = 27,
LWS_CALLBACK_PROTOCOL_DESTROY = 28,
LWS_CALLBACK_WSI_CREATE /* always protocol[0] */ = 29,
LWS_CALLBACK_WSI_DESTROY /* always protocol[0] */ = 30,
LWS_CALLBACK_GET_THREAD_ID = 31,
/* external poll() management support */
LWS_CALLBACK_ADD_POLL_FD = 32,
LWS_CALLBACK_DEL_POLL_FD = 33,
LWS_CALLBACK_CHANGE_MODE_POLL_FD = 34,
LWS_CALLBACK_LOCK_POLL = 35,
LWS_CALLBACK_UNLOCK_POLL = 36,
LWS_CALLBACK_OPENSSL_CONTEXT_REQUIRES_PRIVATE_KEY = 37,
LWS_CALLBACK_WS_PEER_INITIATED_CLOSE = 38,
LWS_CALLBACK_WS_EXT_DEFAULTS = 39,
LWS_CALLBACK_CGI = 40,
LWS_CALLBACK_CGI_TERMINATED = 41,
LWS_CALLBACK_CGI_STDIN_DATA = 42,
LWS_CALLBACK_CGI_STDIN_COMPLETED = 43,
LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP = 44,
LWS_CALLBACK_CLOSED_CLIENT_HTTP = 45,
LWS_CALLBACK_RECEIVE_CLIENT_HTTP = 46,
LWS_CALLBACK_COMPLETED_CLIENT_HTTP = 47,
LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ = 48,
LWS_CALLBACK_HTTP_BIND_PROTOCOL = 49,
LWS_CALLBACK_HTTP_DROP_PROTOCOL = 50,
LWS_CALLBACK_CHECK_ACCESS_RIGHTS = 51,
LWS_CALLBACK_PROCESS_HTML = 52,
LWS_CALLBACK_ADD_HEADERS = 53,
LWS_CALLBACK_SESSION_INFO = 54,
LWS_CALLBACK_GS_EVENT = 55,
/****** add new things just above ---^ ******/
LWS_CALLBACK_USER = 1000, /* user code can use any including / above */
};
#if defined(_WIN32)
typedef SOCKET lws_sockfd_type;
typedef HANDLE lws_filefd_type;
#define lws_sockfd_valid(sfd) (!!sfd)
struct lws_pollfd {
lws_sockfd_type fd;
SHORT events;
SHORT revents;
};
#else
#if defined(MBED_OPERATORS)
/* it's a class lws_conn * */
typedef void * lws_sockfd_type;
typedef void * lws_filefd_type;
#define lws_sockfd_valid(sfd) (!!sfd)
struct pollfd {
lws_sockfd_type fd;
short events;
short revents;
};
#define POLLIN 0x0001
#define POLLPRI 0x0002
#define POLLOUT 0x0004
#define POLLERR 0x0008
#define POLLHUP 0x0010
#define POLLNVAL 0x0020
struct lws;
void * mbed3_create_tcp_stream_socket(void);
void mbed3_delete_tcp_stream_socket(void *sockfd);
void mbed3_tcp_stream_bind(void *sock, int port, struct lws *);
void mbed3_tcp_stream_accept(void *sock, struct lws *);
#else
typedef int lws_sockfd_type;
typedef int lws_filefd_type;
#define lws_sockfd_valid(sfd) (sfd >= 0)
#endif
#define lws_pollfd pollfd
#endif
/* argument structure for all external poll related calls
* passed in via 'in'
*/
struct lws_pollargs {
lws_sockfd_type fd; /* applicable socket descriptor */
int events; /* the new event mask */
int prev_events; /* the previous event mask */
};
/**
* struct lws_plat_file_ops - Platform-specific file operations
*
* These provide platform-agnostic ways to deal with filesystem access in the
* library and in the user code.
*
* @open: Open file (always binary access if plat supports it)
* filelen is filled on exit to be the length of the file
* flags should be set to O_RDONLY or O_RDWR
* @close: Close file
* @seek_cur: Seek from current position
* @read: Read fron file *amount is set on exit to amount read
* @write: Write to file *amount is set on exit as amount written
*/
struct lws_plat_file_ops {
lws_filefd_type (*open)(struct lws *wsi, const char *filename,
unsigned long *filelen, int flags);
int (*close)(struct lws *wsi, lws_filefd_type fd);
unsigned long (*seek_cur)(struct lws *wsi, lws_filefd_type fd,
long offset_from_cur_pos);
int (*read)(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
unsigned char *buf, unsigned long len);
int (*write)(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
unsigned char *buf, unsigned long len);
/* Add new things just above here ---^
* This is part of the ABI, don't needlessly break compatibility */
};
/*
* NOTE: These public enums are part of the abi. If you want to add one,
* add it at where specified so existing users are unaffected.
*/
enum lws_extension_callback_reasons {
LWS_EXT_CB_SERVER_CONTEXT_CONSTRUCT = 0,
LWS_EXT_CB_CLIENT_CONTEXT_CONSTRUCT = 1,
LWS_EXT_CB_SERVER_CONTEXT_DESTRUCT = 2,
LWS_EXT_CB_CLIENT_CONTEXT_DESTRUCT = 3,
LWS_EXT_CB_CONSTRUCT = 4,
LWS_EXT_CB_CLIENT_CONSTRUCT = 5,
LWS_EXT_CB_CHECK_OK_TO_REALLY_CLOSE = 6,
LWS_EXT_CB_CHECK_OK_TO_PROPOSE_EXTENSION = 7,
LWS_EXT_CB_DESTROY = 8,
LWS_EXT_CB_DESTROY_ANY_WSI_CLOSING = 9,
LWS_EXT_CB_ANY_WSI_ESTABLISHED = 10,
LWS_EXT_CB_PACKET_RX_PREPARSE = 11,
LWS_EXT_CB_PACKET_TX_PRESEND = 12,
LWS_EXT_CB_PACKET_TX_DO_SEND = 13,
LWS_EXT_CB_HANDSHAKE_REPLY_TX = 14,
LWS_EXT_CB_FLUSH_PENDING_TX = 15,
LWS_EXT_CB_EXTENDED_PAYLOAD_RX = 16,
LWS_EXT_CB_CAN_PROXY_CLIENT_CONNECTION = 17,
LWS_EXT_CB_1HZ = 18,
LWS_EXT_CB_REQUEST_ON_WRITEABLE = 19,
LWS_EXT_CB_IS_WRITEABLE = 20,
LWS_EXT_CB_PAYLOAD_TX = 21,
LWS_EXT_CB_PAYLOAD_RX = 22,
LWS_EXT_CB_OPTION_DEFAULT = 23,
LWS_EXT_CB_OPTION_SET = 24,
LWS_EXT_CB_OPTION_CONFIRM = 25,
LWS_EXT_CB_NAMED_OPTION_SET = 26,
/****** add new things just above ---^ ******/
};
/*
* NOTE: These public enums are part of the abi. If you want to add one,
* add it at where specified so existing users are unaffected.
*/
enum lws_write_protocol {
LWS_WRITE_TEXT = 0,
LWS_WRITE_BINARY = 1,
LWS_WRITE_CONTINUATION = 2,
LWS_WRITE_HTTP = 3,
/* special 04+ opcodes */
/* LWS_WRITE_CLOSE is handled by lws_close_reason() */
LWS_WRITE_PING = 5,
LWS_WRITE_PONG = 6,
/* Same as write_http but we know this write ends the transaction */
LWS_WRITE_HTTP_FINAL = 7,
/* HTTP2 */
LWS_WRITE_HTTP_HEADERS = 8,
/****** add new things just above ---^ ******/
/* flags */
LWS_WRITE_NO_FIN = 0x40,
/*
* client packet payload goes out on wire unmunged
* only useful for security tests since normal servers cannot
* decode the content if used
*/
LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80
};
/*
* you need these to look at headers that have been parsed if using the
* LWS_CALLBACK_FILTER_CONNECTION callback. If a header from the enum
* list below is absent, .token = NULL and token_len = 0. Otherwise .token
* points to .token_len chars containing that header content.
*/
struct lws_tokens {
char *token;
int token_len;
};
/*
* these have to be kept in sync with lextable.h / minilex.c
*
* NOTE: These public enums are part of the abi. If you want to add one,
* add it at where specified so existing users are unaffected.
*/
enum lws_token_indexes {
WSI_TOKEN_GET_URI = 0,
WSI_TOKEN_POST_URI = 1,
WSI_TOKEN_OPTIONS_URI = 2,
WSI_TOKEN_HOST = 3,
WSI_TOKEN_CONNECTION = 4,
WSI_TOKEN_UPGRADE = 5,
WSI_TOKEN_ORIGIN = 6,
WSI_TOKEN_DRAFT = 7,
WSI_TOKEN_CHALLENGE = 8,
WSI_TOKEN_EXTENSIONS = 9,
WSI_TOKEN_KEY1 = 10,
WSI_TOKEN_KEY2 = 11,
WSI_TOKEN_PROTOCOL = 12,
WSI_TOKEN_ACCEPT = 13,
WSI_TOKEN_NONCE = 14,
WSI_TOKEN_HTTP = 15,
WSI_TOKEN_HTTP2_SETTINGS = 16,
WSI_TOKEN_HTTP_ACCEPT = 17,
WSI_TOKEN_HTTP_AC_REQUEST_HEADERS = 18,
WSI_TOKEN_HTTP_IF_MODIFIED_SINCE = 19,
WSI_TOKEN_HTTP_IF_NONE_MATCH = 20,
WSI_TOKEN_HTTP_ACCEPT_ENCODING = 21,
WSI_TOKEN_HTTP_ACCEPT_LANGUAGE = 22,
WSI_TOKEN_HTTP_PRAGMA = 23,
WSI_TOKEN_HTTP_CACHE_CONTROL = 24,
WSI_TOKEN_HTTP_AUTHORIZATION = 25,
WSI_TOKEN_HTTP_COOKIE = 26,
WSI_TOKEN_HTTP_CONTENT_LENGTH = 27,
WSI_TOKEN_HTTP_CONTENT_TYPE = 28,
WSI_TOKEN_HTTP_DATE = 29,
WSI_TOKEN_HTTP_RANGE = 30,
WSI_TOKEN_HTTP_REFERER = 31,
WSI_TOKEN_KEY = 32,
WSI_TOKEN_VERSION = 33,
WSI_TOKEN_SWORIGIN = 34,
WSI_TOKEN_HTTP_COLON_AUTHORITY = 35,
WSI_TOKEN_HTTP_COLON_METHOD = 36,
WSI_TOKEN_HTTP_COLON_PATH = 37,
WSI_TOKEN_HTTP_COLON_SCHEME = 38,
WSI_TOKEN_HTTP_COLON_STATUS = 39,
WSI_TOKEN_HTTP_ACCEPT_CHARSET = 40,
WSI_TOKEN_HTTP_ACCEPT_RANGES = 41,
WSI_TOKEN_HTTP_ACCESS_CONTROL_ALLOW_ORIGIN = 42,
WSI_TOKEN_HTTP_AGE = 43,
WSI_TOKEN_HTTP_ALLOW = 44,
WSI_TOKEN_HTTP_CONTENT_DISPOSITION = 45,
WSI_TOKEN_HTTP_CONTENT_ENCODING = 46,
WSI_TOKEN_HTTP_CONTENT_LANGUAGE = 47,
WSI_TOKEN_HTTP_CONTENT_LOCATION = 48,
WSI_TOKEN_HTTP_CONTENT_RANGE = 49,
WSI_TOKEN_HTTP_ETAG = 50,
WSI_TOKEN_HTTP_EXPECT = 51,
WSI_TOKEN_HTTP_EXPIRES = 52,
WSI_TOKEN_HTTP_FROM = 53,
WSI_TOKEN_HTTP_IF_MATCH = 54,
WSI_TOKEN_HTTP_IF_RANGE = 55,
WSI_TOKEN_HTTP_IF_UNMODIFIED_SINCE = 56,
WSI_TOKEN_HTTP_LAST_MODIFIED = 57,
WSI_TOKEN_HTTP_LINK = 58,
WSI_TOKEN_HTTP_LOCATION = 59,
WSI_TOKEN_HTTP_MAX_FORWARDS = 60,
WSI_TOKEN_HTTP_PROXY_AUTHENTICATE = 61,
WSI_TOKEN_HTTP_PROXY_AUTHORIZATION = 62,
WSI_TOKEN_HTTP_REFRESH = 63,
WSI_TOKEN_HTTP_RETRY_AFTER = 64,
WSI_TOKEN_HTTP_SERVER = 65,
WSI_TOKEN_HTTP_SET_COOKIE = 66,
WSI_TOKEN_HTTP_STRICT_TRANSPORT_SECURITY = 67,
WSI_TOKEN_HTTP_TRANSFER_ENCODING = 68,
WSI_TOKEN_HTTP_USER_AGENT = 69,
WSI_TOKEN_HTTP_VARY = 70,
WSI_TOKEN_HTTP_VIA = 71,
WSI_TOKEN_HTTP_WWW_AUTHENTICATE = 72,
WSI_TOKEN_PATCH_URI = 73,
WSI_TOKEN_PUT_URI = 74,
WSI_TOKEN_DELETE_URI = 75,
WSI_TOKEN_HTTP_URI_ARGS = 76,
WSI_TOKEN_PROXY = 77,
WSI_TOKEN_HTTP_X_REAL_IP = 78,
WSI_TOKEN_HTTP1_0 = 79,
/****** add new things just above ---^ ******/
/* use token storage to stash these internally, not for
* user use */
_WSI_TOKEN_CLIENT_SENT_PROTOCOLS,
_WSI_TOKEN_CLIENT_PEER_ADDRESS,
_WSI_TOKEN_CLIENT_URI,
_WSI_TOKEN_CLIENT_HOST,
_WSI_TOKEN_CLIENT_ORIGIN,
_WSI_TOKEN_CLIENT_METHOD,
/* always last real token index*/
WSI_TOKEN_COUNT,
/* parser state additions, no storage associated */
WSI_TOKEN_NAME_PART,
WSI_TOKEN_SKIPPING,
WSI_TOKEN_SKIPPING_SAW_CR,
WSI_PARSING_COMPLETE,
WSI_INIT_TOKEN_MUXURL,
};
struct lws_token_limits {
unsigned short token_limit[WSI_TOKEN_COUNT];
};
/*
* From RFC 6455
1000
1000 indicates a normal closure, meaning that the purpose for
which the connection was established has been fulfilled.
1001
1001 indicates that an endpoint is "going away", such as a server
going down or a browser having navigated away from a page.
1002
1002 indicates that an endpoint is terminating the connection due
to a protocol error.
1003
1003 indicates that an endpoint is terminating the connection
because it has received a type of data it cannot accept (e.g., an
endpoint that understands only text data MAY send this if it
receives a binary message).
1004
Reserved. The specific meaning might be defined in the future.
1005
1005 is a reserved value and MUST NOT be set as a status code in a
Close control frame by an endpoint. It is designated for use in
applications expecting a status code to indicate that no status
code was actually present.
1006
1006 is a reserved value and MUST NOT be set as a status code in a
Close control frame by an endpoint. It is designated for use in
applications expecting a status code to indicate that the
connection was closed abnormally, e.g., without sending or
receiving a Close control frame.
1007
1007 indicates that an endpoint is terminating the connection
because it has received data within a message that was not
consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
data within a text message).
1008
1008 indicates that an endpoint is terminating the connection
because it has received a message that violates its policy. This
is a generic status code that can be returned when there is no
other more suitable status code (e.g., 1003 or 1009) or if there
is a need to hide specific details about the policy.
1009
1009 indicates that an endpoint is terminating the connection
because it has received a message that is too big for it to
process.
1010
1010 indicates that an endpoint (client) is terminating the
connection because it has expected the server to negotiate one or
more extension, but the server didn't return them in the response
message of the WebSocket handshake. The list of extensions that
are needed SHOULD appear in the /reason/ part of the Close frame.
Note that this status code is not used by the server, because it
can fail the WebSocket handshake instead.
1011
1011 indicates that a server is terminating the connection because
it encountered an unexpected condition that prevented it from
fulfilling the request.
1015
1015 is a reserved value and MUST NOT be set as a status code in a
Close control frame by an endpoint. It is designated for use in
applications expecting a status code to indicate that the
connection was closed due to a failure to perform a TLS handshake
(e.g., the server certificate can't be verified).
*/
/*
* NOTE: These public enums are part of the abi. If you want to add one,
* add it at where specified so existing users are unaffected.
*/
enum lws_close_status {
LWS_CLOSE_STATUS_NOSTATUS = 0,
LWS_CLOSE_STATUS_NORMAL = 1000,
LWS_CLOSE_STATUS_GOINGAWAY = 1001,
LWS_CLOSE_STATUS_PROTOCOL_ERR = 1002,
LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE = 1003,
LWS_CLOSE_STATUS_RESERVED = 1004,
LWS_CLOSE_STATUS_NO_STATUS = 1005,
LWS_CLOSE_STATUS_ABNORMAL_CLOSE = 1006,
LWS_CLOSE_STATUS_INVALID_PAYLOAD = 1007,
LWS_CLOSE_STATUS_POLICY_VIOLATION = 1008,
LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE = 1009,
LWS_CLOSE_STATUS_EXTENSION_REQUIRED = 1010,
LWS_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011,
LWS_CLOSE_STATUS_TLS_FAILURE = 1015,
/****** add new things just above ---^ ******/
LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY = 9999,
};
enum http_status {
HTTP_STATUS_OK = 200,
HTTP_STATUS_NO_CONTENT = 204,
HTTP_STATUS_MOVED_PERMANENTLY = 301,
HTTP_STATUS_FOUND = 302,
HTTP_STATUS_SEE_OTHER = 303,
HTTP_STATUS_BAD_REQUEST = 400,
HTTP_STATUS_UNAUTHORIZED,
HTTP_STATUS_PAYMENT_REQUIRED,
HTTP_STATUS_FORBIDDEN,
HTTP_STATUS_NOT_FOUND,
HTTP_STATUS_METHOD_NOT_ALLOWED,
HTTP_STATUS_NOT_ACCEPTABLE,
HTTP_STATUS_PROXY_AUTH_REQUIRED,
HTTP_STATUS_REQUEST_TIMEOUT,
HTTP_STATUS_CONFLICT,
HTTP_STATUS_GONE,
HTTP_STATUS_LENGTH_REQUIRED,
HTTP_STATUS_PRECONDITION_FAILED,
HTTP_STATUS_REQ_ENTITY_TOO_LARGE,
HTTP_STATUS_REQ_URI_TOO_LONG,
HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE,
HTTP_STATUS_EXPECTATION_FAILED,
HTTP_STATUS_INTERNAL_SERVER_ERROR = 500,
HTTP_STATUS_NOT_IMPLEMENTED,
HTTP_STATUS_BAD_GATEWAY,
HTTP_STATUS_SERVICE_UNAVAILABLE,
HTTP_STATUS_GATEWAY_TIMEOUT,
HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED,
};
struct lws;
struct lws_context;
/* needed even with extensions disabled for create context */
struct lws_extension;
/**
* typedef lws_callback_function() - User server actions
* @wsi: Opaque websocket instance pointer
* @reason: The reason for the call
* @user: Pointer to per-session user data allocated by library
* @in: Pointer used for some callback reasons
* @len: Length set for some callback reasons
*
* This callback is the way the user controls what is served. All the
* protocol detail is hidden and handled by the library.
*
* For each connection / session there is user data allocated that is
* pointed to by "user". You set the size of this user data area when
* the library is initialized with lws_create_server.
*
* You get an opportunity to initialize user data when called back with
* LWS_CALLBACK_ESTABLISHED reason.
*
* LWS_CALLBACK_ESTABLISHED: after the server completes a handshake with
* an incoming client. If you built the library
* with ssl support, @in is a pointer to the
* ssl struct associated with the connection or
* NULL.
*
* LWS_CALLBACK_CLIENT_CONNECTION_ERROR: the request client connection has
* been unable to complete a handshake with the remote server. If
* in is non-NULL, you can find an error string of length len where
* it points to.
*
* LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: this is the last chance for the
* client user code to examine the http headers
* and decide to reject the connection. If the
* content in the headers is interesting to the
* client (url, etc) it needs to copy it out at
* this point since it will be destroyed before
* the CLIENT_ESTABLISHED call
*
* LWS_CALLBACK_CLIENT_ESTABLISHED: after your client connection completed
* a handshake with the remote server
*
* LWS_CALLBACK_CLOSED: when the websocket session ends
*
* LWS_CALLBACK_CLOSED_HTTP: when a HTTP (non-websocket) session ends
*
* LWS_CALLBACK_RECEIVE: data has appeared for this server endpoint from a
* remote client, it can be found at *in and is
* len bytes long
*
* LWS_CALLBACK_CLIENT_RECEIVE_PONG: if you elected to see PONG packets,
* they appear with this callback reason. PONG
* packets only exist in 04+ protocol
*
* LWS_CALLBACK_CLIENT_RECEIVE: data has appeared from the server for the
* client connection, it can be found at *in and
* is len bytes long
*
* LWS_CALLBACK_HTTP: an http request has come from a client that is not
* asking to upgrade the connection to a websocket
* one. This is a chance to serve http content,
* for example, to send a script to the client