1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
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
25/** @file */
26
27#ifndef LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
28#define LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
29
30#ifdef __cplusplus
31#include <cstddef>
32#include <cstdarg>
33
34extern "C" {
35#else
36#include <stdarg.h>
37#endif
38
39#include <string.h>
40#include <stdlib.h>
41
42#include "lws_config.h"
43
44/* place for one-shot opaque forward references */
45
46typedef struct lws_context * lws_ctx_t;
47struct lws_sequencer;
48struct lws_dsh;
49
50/*
51 * CARE: everything using cmake defines needs to be below here
52 */
53
54#define LWS_US_PER_SEC ((lws_usec_t)1000000)
55#define LWS_MS_PER_SEC ((lws_usec_t)1000)
56#define LWS_US_PER_MS ((lws_usec_t)1000)
57#define LWS_NS_PER_US ((lws_usec_t)1000)
58
59#define LWS_KI (1024)
60#define LWS_MI (LWS_KI * 1024)
61#define LWS_GI (LWS_MI * 1024)
62#define LWS_TI ((uint64_t)LWS_GI * 1024)
63#define LWS_PI ((uint64_t)LWS_TI * 1024)
64
65#define LWS_US_TO_MS(x) ((x + (LWS_US_PER_MS / 2)) / LWS_US_PER_MS)
66
67#if defined(LWS_HAS_INTPTR_T)
68#include <stdint.h>
69#define lws_intptr_t intptr_t
70#else
71typedef unsigned long long lws_intptr_t;
72#endif
73
74#if defined(WIN32) || defined(_WIN32)
75#ifndef WIN32_LEAN_AND_MEAN
76#define WIN32_LEAN_AND_MEAN
77#endif
78
79#include <winsock2.h>
80#include <ws2tcpip.h>
81#include <stddef.h>
82#include <basetsd.h>
83#include <io.h>
84#ifndef _WIN32_WCE
85#include <fcntl.h>
86#else
87#define _O_RDONLY 0x0000
88#define O_RDONLY _O_RDONLY
89#endif
90
91typedef int uid_t;
92typedef int gid_t;
93typedef unsigned short sa_family_t;
94#if !defined(LWS_HAVE_SUSECONDS_T)
95typedef unsigned int useconds_t;
96typedef int suseconds_t;
97#endif
98
99#define LWS_INLINE __inline
100#define LWS_VISIBLE
101#define LWS_WARN_UNUSED_RESULT
102#define LWS_WARN_DEPRECATED
103#define LWS_FORMAT(string_index)
104
105#if !defined(LWS_EXTERN) && defined(LWS_BUILDING_SHARED)
106#ifdef LWS_DLL
107#ifdef LWS_INTERNAL
108#define LWS_EXTERN extern __declspec(dllexport)
109#else
110#define LWS_EXTERN extern __declspec(dllimport)
111#endif
112#endif
113#endif
114
115#if !defined(LWS_INTERNAL) && !defined(LWS_EXTERN)
116#define LWS_EXTERN
117#define LWS_VISIBLE
118#endif
119
120#if !defined(LWS_EXTERN)
121#define LWS_EXTERN
122#endif
123
124#if defined(__MINGW32__)
125#define LWS_INVALID_FILE -1
126#else
127#define LWS_INVALID_FILE INVALID_HANDLE_VALUE
128#endif
129#define LWS_SOCK_INVALID (INVALID_SOCKET)
130#define LWS_O_RDONLY _O_RDONLY
131#define LWS_O_WRONLY _O_WRONLY
132#define LWS_O_CREAT _O_CREAT
133#define LWS_O_TRUNC _O_TRUNC
134
135#ifndef __func__
136#define __func__ __FUNCTION__
137#endif
138
139#else /* NOT WIN32 */
140#include <unistd.h>
141#if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
142#include <sys/capability.h>
143#endif
144
145#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__QNX__) || defined(__OpenBSD__)
146#include <sys/socket.h>
147#include <netinet/in.h>
148#endif
149
150#define LWS_INLINE inline
151#define LWS_O_RDONLY O_RDONLY
152#define LWS_O_WRONLY O_WRONLY
153#define LWS_O_CREAT O_CREAT
154#define LWS_O_TRUNC O_TRUNC
155
156#if !defined(LWS_PLAT_OPTEE) && !defined(OPTEE_TA) && !defined(LWS_PLAT_FREERTOS)
157#include <poll.h>
158#include <netdb.h>
159#define LWS_INVALID_FILE -1
160#define LWS_SOCK_INVALID (-1)
161#else
162#define getdtablesize() (30)
163#if defined(LWS_PLAT_FREERTOS)
164#define LWS_INVALID_FILE NULL
165#define LWS_SOCK_INVALID (-1)
166#else
167#define LWS_INVALID_FILE NULL
168#define LWS_SOCK_INVALID (-1)
169#endif
170#endif
171
172#if defined(__FreeBSD__)
173#include <sys/signal.h>
174#endif
175#if defined(__GNUC__)
176
177/* warn_unused_result attribute only supported by GCC 3.4 or later */
178#if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
179#define LWS_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
180#else
181#define LWS_WARN_UNUSED_RESULT
182#endif
183
184#if defined(LWS_BUILDING_SHARED)
185/* this is only set when we're building lws itself shared */
186#define LWS_VISIBLE __attribute__((visibility("default")))
187#define LWS_EXTERN extern
188
189#else /* not shared */
190#if defined(WIN32) || defined(_WIN32) || defined(__MINGW32__)
191#define LWS_VISIBLE
192#define LWS_EXTERN extern
193#else
194/*
195 * If we explicitly say hidden here, symbols exist as T but
196 * cannot be imported at link-time.
197 */
198#define LWS_VISIBLE
199#define LWS_EXTERN
200#endif
201
202#endif /* not shared */
203
204#define LWS_WARN_DEPRECATED __attribute__ ((deprecated))
205#define LWS_FORMAT(string_index) __attribute__ ((format(printf, string_index, string_index+1)))
206#else /* not GNUC */
207
208#define LWS_VISIBLE
209#define LWS_WARN_UNUSED_RESULT
210#define LWS_WARN_DEPRECATED
211#define LWS_FORMAT(string_index)
212#if !defined(LWS_EXTERN)
213#define LWS_EXTERN extern
214#endif
215#endif
216
217
218#if defined(__ANDROID__)
219#include <netinet/in.h>
220#include <unistd.h>
221#endif
222#endif
223
224#ifdef _WIN32
225#define random rand
226#else
227#if !defined(LWS_PLAT_OPTEE)
228#include <sys/time.h>
229#include <unistd.h>
230#endif
231#endif
232
233#if defined(LWS_WITH_LIBUV_INTERNAL)
234#include <uv.h>
235
236#ifdef LWS_HAVE_UV_VERSION_H
237#include <uv-version.h>
238#endif
239
240#ifdef LWS_HAVE_NEW_UV_VERSION_H
241#include <uv/version.h>
242#endif
243#endif
244
245#if defined(LWS_WITH_TLS)
246
247#ifdef USE_WOLFSSL
248#ifdef USE_OLD_CYASSL
249#ifdef _WIN32
250/*
251 * Include user-controlled settings for windows from
252 * <wolfssl-root>/IDE/WIN/user_settings.h
253 */
254#include <IDE/WIN/user_settings.h>
255#include <cyassl/ctaocrypt/settings.h>
256#else
257#include <cyassl/options.h>
258#endif
259#include <cyassl/openssl/ssl.h>
260#include <cyassl/error-ssl.h>
261
262#else
263#ifdef _WIN32
264/*
265 * Include user-controlled settings for windows from
266 * <wolfssl-root>/IDE/WIN/user_settings.h
267 */
268#include <IDE/WIN/user_settings.h>
269#include <wolfssl/wolfcrypt/settings.h>
270#else
271#include <wolfssl/options.h>
272#endif
273#include <wolfssl/openssl/ssl.h>
274#include <wolfssl/error-ssl.h>
275#endif /* not USE_OLD_CYASSL */
276#else
277#if defined(LWS_WITH_MBEDTLS)
278#if defined(LWS_PLAT_FREERTOS)
279/* this filepath is passed to us but without quotes or <> */
280#if !defined(LWS_AMAZON_RTOS)
281/* AMAZON RTOS has its own setting via MTK_MBEDTLS_CONFIG_FILE */
282#undef MBEDTLS_CONFIG_FILE
283#define MBEDTLS_CONFIG_FILE <mbedtls/esp_config.h>
284#endif
285#endif
286#if defined(LWS_WITH_TLS)
287#include <mbedtls/ssl.h>
288#include <mbedtls/entropy.h>
289#include <mbedtls/ctr_drbg.h>
290#include <mbedtls/version.h>
291
292#if !defined(MBEDTLS_PRIVATE)
293#define MBEDTLS_PRIVATE(_q) _q
294#endif
295
296#if (MBEDTLS_VERSION_MAJOR == 3) && (MBEDTLS_VERSION_MINOR == 0)
297#define MBEDTLS_PRIVATE_V30_ONLY(_q) MBEDTLS_PRIVATE(_q)
298#else
299#define MBEDTLS_PRIVATE_V30_ONLY(_q) _q
300#endif
301
302#endif
303#else
304#include <openssl/ssl.h>
305#if !defined(LWS_WITH_MBEDTLS)
306#include <openssl/err.h>
307#endif
308#endif
309#endif /* not USE_WOLFSSL */
310#endif
311
312/*
313 * Helpers for pthread mutex in user code... if lws is built for
314 * multiple service threads, these resolve to pthread mutex
315 * operations. In the case LWS_MAX_SMP is 1 (the default), they
316 * are all NOPs and no pthread type or api is referenced.
317 */
318
319#if LWS_MAX_SMP > 1
320
321#include <pthread.h>
322
323#define lws_pthread_mutex(name) pthread_mutex_t name;
324
325static LWS_INLINE void
326lws_pthread_mutex_init(pthread_mutex_t *lock)
327{
328 pthread_mutex_init(lock, NULL);
329}
330
331static LWS_INLINE void
332lws_pthread_mutex_destroy(pthread_mutex_t *lock)
333{
334 pthread_mutex_destroy(lock);
335}
336
337static LWS_INLINE void
338lws_pthread_mutex_lock(pthread_mutex_t *lock)
339{
340 pthread_mutex_lock(lock);
341}
342
343static LWS_INLINE void
344lws_pthread_mutex_unlock(pthread_mutex_t *lock)
345{
346 pthread_mutex_unlock(lock);
347}
348
349#else
350#define lws_pthread_mutex(name)
351#define lws_pthread_mutex_init(_a)
352#define lws_pthread_mutex_destroy(_a)
353#define lws_pthread_mutex_lock(_a)
354#define lws_pthread_mutex_unlock(_a)
355#endif
356
357
358#define CONTEXT_PORT_NO_LISTEN -1
359#define CONTEXT_PORT_NO_LISTEN_SERVER -2
360
361#include <libwebsockets/lws-logs.h>
362
363
364#include <stddef.h>
365
366#ifndef lws_container_of
367#define lws_container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
368#endif
369#define LWS_ALIGN_TO(x, bou) x += ((bou) - ((x) % (bou))) % (bou)
370
371struct lws;
372
373/* api change list for user code to test against */
374
375#define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
376
377/* the struct lws_protocols has the id field present */
378#define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
379
380/* you can call lws_get_peer_write_allowance */
381#define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
382
383/* extra parameter introduced in 917f43ab821 */
384#define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
385
386/* File operations stuff exists */
387#define LWS_FEATURE_FOPS
388
389
390#if defined(_WIN32)
391#if !defined(LWS_WIN32_HANDLE_TYPES)
392typedef SOCKET lws_sockfd_type;
393#if defined(__MINGW32__)
394typedef int lws_filefd_type;
395#else
396typedef HANDLE lws_filefd_type;
397#endif
398#endif
399
400
401#define lws_pollfd pollfd
402#define LWS_POLLHUP (POLLHUP)
403#define LWS_POLLIN (POLLRDNORM | POLLRDBAND)
404#define LWS_POLLOUT (POLLWRNORM)
405
406#else
407
408
409#if defined(LWS_PLAT_FREERTOS)
410#include <libwebsockets/lws-freertos.h>
411#else
412typedef int lws_sockfd_type;
413typedef int lws_filefd_type;
414#endif
415
416#if defined(LWS_PLAT_OPTEE)
417#include <time.h>
418struct timeval {
419 time_t tv_sec;
420 unsigned int tv_usec;
421};
422#if defined(LWS_WITH_NETWORK)
423// #include <poll.h>
424#define lws_pollfd pollfd
425
426struct timezone;
427
428int gettimeofday(struct timeval *tv, struct timezone *tz);
429
430 /* Internet address. */
431 struct in_addr {
432 uint32_t s_addr; /* address in network byte order */
433 };
434
435typedef unsigned short sa_family_t;
436typedef unsigned short in_port_t;
437typedef uint32_t socklen_t;
438
439#include <libwebsockets/lws-optee.h>
440
441#if !defined(TEE_SE_READER_NAME_MAX)
442 struct addrinfo {
443 int ai_flags;
444 int ai_family;
445 int ai_socktype;
446 int ai_protocol;
447 socklen_t ai_addrlen;
448 struct sockaddr *ai_addr;
449 char *ai_canonname;
450 struct addrinfo *ai_next;
451 };
452#endif
453
454ssize_t recv(int sockfd, void *buf, size_t len, int flags);
455ssize_t send(int sockfd, const void *buf, size_t len, int flags);
456ssize_t read(int fd, void *buf, size_t count);
457int getsockopt(int sockfd, int level, int optname,
458 void *optval, socklen_t *optlen);
459 int setsockopt(int sockfd, int level, int optname,
460 const void *optval, socklen_t optlen);
461int connect(int sockfd, const struct sockaddr *addr,
462 socklen_t addrlen);
463
464extern int errno;
465
466uint16_t ntohs(uint16_t netshort);
467uint16_t htons(uint16_t hostshort);
468
469int bind(int sockfd, const struct sockaddr *addr,
470 socklen_t addrlen);
471
472
473#define MSG_NOSIGNAL 0x4000
474#define EAGAIN 11
475#define EINTR 4
476#define EWOULDBLOCK EAGAIN
477#define EADDRINUSE 98
478#define INADDR_ANY 0
479#define AF_INET 2
480#define SHUT_WR 1
481#define AF_UNSPEC 0
482#define PF_UNSPEC 0
483#define SOCK_STREAM 1
484#define SOCK_DGRAM 2
485# define AI_PASSIVE 0x0001
486#define IPPROTO_UDP 17
487#define SOL_SOCKET 1
488#define SO_SNDBUF 7
489#define EISCONN 106
490#define EALREADY 114
491#define EINPROGRESS 115
492int shutdown(int sockfd, int how);
493int close(int fd);
494int atoi(const char *nptr);
495long long atoll(const char *nptr);
496
497int socket(int domain, int type, int protocol);
498 int getaddrinfo(const char *node, const char *service,
499 const struct addrinfo *hints,
500 struct addrinfo **res);
501
502 void freeaddrinfo(struct addrinfo *res);
503
504#if !defined(TEE_SE_READER_NAME_MAX)
505struct lws_pollfd
506{
507 int fd; /* File descriptor to poll. */
508 short int events; /* Types of events poller cares about. */
509 short int revents; /* Types of events that actually occurred. */
510};
511#endif
512
513int poll(struct pollfd *fds, int nfds, int timeout);
514
515#define LWS_POLLHUP (0x18)
516#define LWS_POLLIN (1)
517#define LWS_POLLOUT (4)
518#else
519struct lws_pollfd;
520struct sockaddr_in;
521#endif
522#else
523#define lws_pollfd pollfd
524#define LWS_POLLHUP (POLLHUP | POLLERR)
525#define LWS_POLLIN (POLLIN)
526#define LWS_POLLOUT (POLLOUT)
527#endif
528#endif
529
530
531#if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
532/* ... */
533#define ssize_t SSIZE_T
534#endif
535
536#if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
537#include <sys/types.h>
538#include <sys/stat.h>
539#endif
540
541#if defined(LWS_HAVE_STDINT_H)
542#include <stdint.h>
543#else
544#if defined(WIN32) || defined(_WIN32)
545/* !!! >:-[ */
546typedef __int64 int64_t;
547typedef unsigned __int64 uint64_t;
548typedef __int32 int32_t;
549typedef unsigned __int32 uint32_t;
550typedef __int16 int16_t;
551typedef unsigned __int16 uint16_t;
552typedef unsigned __int8 uint8_t;
553#else
554typedef unsigned int uint32_t;
555typedef unsigned short uint16_t;
556typedef unsigned char uint8_t;
557#endif
558#endif
559
560typedef int64_t lws_usec_t;
561typedef unsigned long long lws_filepos_t;
562typedef long long lws_fileofs_t;
563typedef uint32_t lws_fop_flags_t;
564
565#define lws_concat_temp(_t, _l) (_t + sizeof(_t) - _l)
566#define lws_concat_used(_t, _l) (sizeof(_t) - _l)
567
568/** struct lws_pollargs - argument structure for all external poll related calls
569 * passed in via 'in' */
570struct lws_pollargs {
571 lws_sockfd_type fd; /**< applicable socket descriptor */
572 int events; /**< the new event mask */
573 int prev_events; /**< the previous event mask */
574};
575
576struct lws_extension; /* needed even with ws exts disabled for create context */
577struct lws_token_limits;
578struct lws_protocols;
579struct lws_context;
580struct lws_tokens;
581struct lws_vhost;
582struct lws;
583
584#include <libwebsockets/lws-dll2.h>
585#include <libwebsockets/lws-map.h>
586
587#include <libwebsockets/lws-fault-injection.h>
588#include <libwebsockets/lws-timeout-timer.h>
589#include <libwebsockets/lws-cache-ttl.h>
590#if defined(LWS_WITH_SYS_SMD)
591#include <libwebsockets/lws-smd.h>
592#endif
593#include <libwebsockets/lws-state.h>
594#include <libwebsockets/lws-retry.h>
595#include <libwebsockets/lws-adopt.h>
596#include <libwebsockets/lws-network-helper.h>
597#include <libwebsockets/lws-metrics.h>
598#include <libwebsockets/lws-system.h>
599#include <libwebsockets/lws-ws-close.h>
600#include <libwebsockets/lws-callbacks.h>
601#include <libwebsockets/lws-ws-state.h>
602#include <libwebsockets/lws-ws-ext.h>
603#include <libwebsockets/lws-protocols-plugins.h>
604
605#include <libwebsockets/lws-context-vhost.h>
606
607#if defined(LWS_WITH_CONMON)
608#include <libwebsockets/lws-conmon.h>
609#endif
610
611#if defined(LWS_ROLE_MQTT)
612#include <libwebsockets/lws-mqtt.h>
613#endif
614#include <libwebsockets/lws-client.h>
615#include <libwebsockets/lws-http.h>
616#include <libwebsockets/lws-spa.h>
617#include <libwebsockets/lws-purify.h>
618#include <libwebsockets/lws-misc.h>
619#include <libwebsockets/lws-dsh.h>
620#include <libwebsockets/lws-service.h>
621#include <libwebsockets/lws-write.h>
622#include <libwebsockets/lws-writeable.h>
623#include <libwebsockets/lws-ring.h>
624#include <libwebsockets/lws-sha1-base64.h>
625#include <libwebsockets/lws-x509.h>
626#include <libwebsockets/lws-cgi.h>
627#if defined(LWS_WITH_FILE_OPS)
628#include <libwebsockets/lws-vfs.h>
629#endif
630#include <libwebsockets/lws-gencrypto.h>
631
632#include <libwebsockets/lws-lejp.h>
633#include <libwebsockets/lws-lecp.h>
634#include <libwebsockets/lws-cose.h>
635#include <libwebsockets/lws-struct.h>
636#include <libwebsockets/lws-threadpool.h>
637#include <libwebsockets/lws-tokenize.h>
638#include <libwebsockets/lws-lwsac.h>
639#include <libwebsockets/lws-fts.h>
640#include <libwebsockets/lws-diskcache.h>
641#include <libwebsockets/lws-sequencer.h>
642#include <libwebsockets/lws-secure-streams.h>
643#include <libwebsockets/lws-secure-streams-policy.h>
644#include <libwebsockets/lws-secure-streams-client.h>
645
646#if !defined(LWS_PLAT_FREERTOS)
647#include <libwebsockets/abstract/abstract.h>
648
649#include <libwebsockets/lws-test-sequencer.h>
650#endif
651#include <libwebsockets/lws-async-dns.h>
652
653#if defined(LWS_WITH_TLS)
654
655#include <libwebsockets/lws-tls-sessions.h>
656
657#if defined(LWS_WITH_MBEDTLS)
658#include <mbedtls/md5.h>
659#include <mbedtls/sha1.h>
660#include <mbedtls/sha256.h>
661#include <mbedtls/sha512.h>
662#endif
663
664#include <libwebsockets/lws-genhash.h>
665#include <libwebsockets/lws-genrsa.h>
666#include <libwebsockets/lws-genaes.h>
667#include <libwebsockets/lws-genec.h>
668
669#include <libwebsockets/lws-jwk.h>
670#include <libwebsockets/lws-jose.h>
671#include <libwebsockets/lws-jws.h>
672#include <libwebsockets/lws-jwe.h>
673
674#endif
675
676#include <libwebsockets/lws-eventlib-exports.h>
677#include <libwebsockets/lws-i2c.h>
678#include <libwebsockets/lws-spi.h>
679#include <libwebsockets/lws-gpio.h>
680#include <libwebsockets/lws-bb-i2c.h>
681#include <libwebsockets/lws-bb-spi.h>
682#include <libwebsockets/lws-button.h>
683#include <libwebsockets/lws-led.h>
684#include <libwebsockets/lws-pwm.h>
685#include <libwebsockets/lws-display.h>
686#include <libwebsockets/lws-ssd1306-i2c.h>
687#include <libwebsockets/lws-ili9341-spi.h>
688#include <libwebsockets/lws-settings.h>
689#include <libwebsockets/lws-netdev.h>
690
691#ifdef __cplusplus
692}
693#endif
694
695#endif
696