2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2019 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 #ifndef LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
25 #define LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
39 #include "lws_config.h"
42 * CARE: everything using cmake defines needs to be below here
45 #define LWS_US_PER_SEC 1000000
46 #define LWS_MS_PER_SEC 1000
47 #define LWS_US_PER_MS 1000
48 #define LWS_NS_PER_US 1000
51 #define LWS_MI (LWS_KI * 1024)
52 #define LWS_GI (LWS_MI * 1024)
53 #define LWS_TI ((uint64_t)LWS_GI * 1024)
54 #define LWS_PI ((uint64_t)LWS_TI * 1024)
56 #define LWS_US_TO_MS(x) ((x + (LWS_US_PER_MS / 2)) / LWS_US_PER_MS)
58 #if defined(LWS_HAS_INTPTR_T)
60 #define lws_intptr_t intptr_t
62 typedef unsigned long long lws_intptr_t;
65 #if defined(WIN32) || defined(_WIN32)
66 #ifndef WIN32_LEAN_AND_MEAN
67 #define WIN32_LEAN_AND_MEAN
78 #define _O_RDONLY 0x0000
79 #define O_RDONLY _O_RDONLY
82 #define LWS_INLINE __inline
84 #define LWS_WARN_UNUSED_RESULT
85 #define LWS_WARN_DEPRECATED
86 #define LWS_FORMAT(string_index)
88 #if !defined(LWS_EXTERN)
91 #define LWS_EXTERN extern __declspec(dllexport)
93 #define LWS_EXTERN extern __declspec(dllimport)
100 #define LWS_INVALID_FILE INVALID_HANDLE_VALUE
101 #define LWS_SOCK_INVALID (INVALID_SOCKET)
102 #define LWS_O_RDONLY _O_RDONLY
103 #define LWS_O_WRONLY _O_WRONLY
104 #define LWS_O_CREAT _O_CREAT
105 #define LWS_O_TRUNC _O_TRUNC
108 #define __func__ __FUNCTION__
111 #else /* NOT WIN32 */
113 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
114 #include <sys/capability.h>
117 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__QNX__) || defined(__OpenBSD__)
118 #include <sys/socket.h>
119 #include <netinet/in.h>
122 #define LWS_INLINE inline
123 #define LWS_O_RDONLY O_RDONLY
124 #define LWS_O_WRONLY O_WRONLY
125 #define LWS_O_CREAT O_CREAT
126 #define LWS_O_TRUNC O_TRUNC
128 #if !defined(LWS_PLAT_OPTEE) && !defined(OPTEE_TA) && !defined(LWS_WITH_ESP32)
131 #define LWS_INVALID_FILE -1
132 #define LWS_SOCK_INVALID (-1)
134 #define getdtablesize() (30)
135 #if defined(LWS_WITH_ESP32)
136 #define LWS_INVALID_FILE NULL
137 #define LWS_SOCK_INVALID (-1)
139 #define LWS_INVALID_FILE NULL
140 #define LWS_SOCK_INVALID (-1)
144 #if defined(__GNUC__)
146 /* warn_unused_result attribute only supported by GCC 3.4 or later */
147 #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
148 #define LWS_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
150 #define LWS_WARN_UNUSED_RESULT
153 #define LWS_VISIBLE __attribute__((visibility("default")))
154 #define LWS_WARN_DEPRECATED __attribute__ ((deprecated))
155 #define LWS_FORMAT(string_index) __attribute__ ((format(printf, string_index, string_index+1)))
158 #define LWS_WARN_UNUSED_RESULT
159 #define LWS_WARN_DEPRECATED
160 #define LWS_FORMAT(string_index)
163 #if defined(__ANDROID__)
164 #include <netinet/in.h>
170 #if defined(LWS_WITH_LIBEV)
172 #endif /* LWS_WITH_LIBEV */
173 #ifdef LWS_WITH_LIBUV
175 #ifdef LWS_HAVE_UV_VERSION_H
176 #include <uv-version.h>
178 #ifdef LWS_HAVE_NEW_UV_VERSION_H
179 #include <uv/version.h>
181 #endif /* LWS_WITH_LIBUV */
182 #if defined(LWS_WITH_LIBEVENT)
183 #include <event2/event.h>
184 #endif /* LWS_WITH_LIBEVENT */
187 #define LWS_EXTERN extern
193 #if !defined(LWS_PLAT_OPTEE)
194 #include <sys/time.h>
199 #if defined(LWS_WITH_TLS)
202 #ifdef USE_OLD_CYASSL
205 * Include user-controlled settings for windows from
206 * <wolfssl-root>/IDE/WIN/user_settings.h
208 #include <IDE/WIN/user_settings.h>
209 #include <cyassl/ctaocrypt/settings.h>
211 #include <cyassl/options.h>
213 #include <cyassl/openssl/ssl.h>
214 #include <cyassl/error-ssl.h>
219 * Include user-controlled settings for windows from
220 * <wolfssl-root>/IDE/WIN/user_settings.h
222 #include <IDE/WIN/user_settings.h>
223 #include <wolfssl/wolfcrypt/settings.h>
225 #include <wolfssl/options.h>
227 #include <wolfssl/openssl/ssl.h>
228 #include <wolfssl/error-ssl.h>
229 #endif /* not USE_OLD_CYASSL */
231 #if defined(LWS_WITH_MBEDTLS)
232 #if defined(LWS_WITH_ESP32)
233 /* this filepath is passed to us but without quotes or <> */
234 #if !defined(LWS_AMAZON_RTOS)
235 /* AMAZON RTOS has its own setting via MTK_MBEDTLS_CONFIG_FILE */
236 #undef MBEDTLS_CONFIG_FILE
237 #define MBEDTLS_CONFIG_FILE <mbedtls/esp_config.h>
240 #include <mbedtls/ssl.h>
241 #include <mbedtls/entropy.h>
242 #include <mbedtls/ctr_drbg.h>
244 #include <openssl/ssl.h>
245 #if !defined(LWS_WITH_MBEDTLS)
246 #include <openssl/err.h>
249 #endif /* not USE_WOLFSSL */
253 * Helpers for pthread mutex in user code... if lws is built for
254 * multiple service threads, these resolve to pthread mutex
255 * operations. In the case LWS_MAX_SMP is 1 (the default), they
256 * are all NOPs and no pthread type or api is referenced.
263 #define lws_pthread_mutex(name) pthread_mutex_t name;
265 static LWS_INLINE void
266 lws_pthread_mutex_init(pthread_mutex_t *lock)
268 pthread_mutex_init(lock, NULL);
271 static LWS_INLINE void
272 lws_pthread_mutex_destroy(pthread_mutex_t *lock)
274 pthread_mutex_destroy(lock);
277 static LWS_INLINE void
278 lws_pthread_mutex_lock(pthread_mutex_t *lock)
280 pthread_mutex_lock(lock);
283 static LWS_INLINE void
284 lws_pthread_mutex_unlock(pthread_mutex_t *lock)
286 pthread_mutex_unlock(lock);
290 #define lws_pthread_mutex(name)
291 #define lws_pthread_mutex_init(_a)
292 #define lws_pthread_mutex_destroy(_a)
293 #define lws_pthread_mutex_lock(_a)
294 #define lws_pthread_mutex_unlock(_a)
298 #define CONTEXT_PORT_NO_LISTEN -1
299 #define CONTEXT_PORT_NO_LISTEN_SERVER -2
301 #include <libwebsockets/lws-logs.h>
306 #ifndef lws_container_of
307 #define lws_container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
312 /* api change list for user code to test against */
314 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
316 /* the struct lws_protocols has the id field present */
317 #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
319 /* you can call lws_get_peer_write_allowance */
320 #define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
322 /* extra parameter introduced in 917f43ab821 */
323 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
325 /* File operations stuff exists */
326 #define LWS_FEATURE_FOPS
330 #if !defined(LWS_WIN32_HANDLE_TYPES)
331 typedef SOCKET lws_sockfd_type;
332 typedef HANDLE lws_filefd_type;
336 lws_sockfd_type fd; /**< file descriptor */
337 SHORT events; /**< which events to respond to */
338 SHORT revents; /**< which events happened */
340 #define LWS_POLLHUP (FD_CLOSE)
341 #define LWS_POLLIN (FD_READ | FD_ACCEPT)
342 #define LWS_POLLOUT (FD_WRITE)
346 #if defined(LWS_WITH_ESP32)
347 #include <libwebsockets/lws-esp32.h>
349 typedef int lws_sockfd_type;
350 typedef int lws_filefd_type;
353 #if defined(LWS_PLAT_OPTEE)
357 unsigned int tv_usec;
359 #if defined(LWS_WITH_NETWORK)
361 #define lws_pollfd pollfd
365 int gettimeofday(struct timeval *tv, struct timezone *tz);
367 /* Internet address. */
369 uint32_t s_addr; /* address in network byte order */
372 typedef unsigned short sa_family_t;
373 typedef unsigned short in_port_t;
374 typedef uint32_t socklen_t;
376 #include <libwebsockets/lws-optee.h>
378 #if !defined(TEE_SE_READER_NAME_MAX)
384 socklen_t ai_addrlen;
385 struct sockaddr *ai_addr;
387 struct addrinfo *ai_next;
391 ssize_t recv(int sockfd, void *buf, size_t len, int flags);
392 ssize_t send(int sockfd, const void *buf, size_t len, int flags);
393 ssize_t read(int fd, void *buf, size_t count);
394 int getsockopt(int sockfd, int level, int optname,
395 void *optval, socklen_t *optlen);
396 int setsockopt(int sockfd, int level, int optname,
397 const void *optval, socklen_t optlen);
398 int connect(int sockfd, const struct sockaddr *addr,
403 uint16_t ntohs(uint16_t netshort);
404 uint16_t htons(uint16_t hostshort);
406 int bind(int sockfd, const struct sockaddr *addr,
410 #define MSG_NOSIGNAL 0x4000
413 #define EWOULDBLOCK EAGAIN
414 #define EADDRINUSE 98
420 #define SOCK_STREAM 1
422 # define AI_PASSIVE 0x0001
423 #define IPPROTO_UDP 17
428 #define EINPROGRESS 115
429 int shutdown(int sockfd, int how);
431 int atoi(const char *nptr);
432 long long atoll(const char *nptr);
434 int socket(int domain, int type, int protocol);
435 int getaddrinfo(const char *node, const char *service,
436 const struct addrinfo *hints,
437 struct addrinfo **res);
439 void freeaddrinfo(struct addrinfo *res);
441 #if !defined(TEE_SE_READER_NAME_MAX)
444 int fd; /* File descriptor to poll. */
445 short int events; /* Types of events poller cares about. */
446 short int revents; /* Types of events that actually occurred. */
450 int poll(struct pollfd *fds, int nfds, int timeout);
452 #define LWS_POLLHUP (0x18)
453 #define LWS_POLLIN (1)
454 #define LWS_POLLOUT (4)
460 #define lws_pollfd pollfd
461 #define LWS_POLLHUP (POLLHUP | POLLERR)
462 #define LWS_POLLIN (POLLIN)
463 #define LWS_POLLOUT (POLLOUT)
468 #if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
470 #define ssize_t SSIZE_T
473 #if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
474 #include <sys/types.h>
475 #include <sys/stat.h>
478 #if defined(LWS_HAVE_STDINT_H)
481 #if defined(WIN32) || defined(_WIN32)
483 typedef __int64 int64_t;
484 typedef unsigned __int64 uint64_t;
485 typedef __int32 int32_t;
486 typedef unsigned __int32 uint32_t;
487 typedef __int16 int16_t;
488 typedef unsigned __int16 uint16_t;
489 typedef unsigned __int8 uint8_t;
491 typedef unsigned int uint32_t;
492 typedef unsigned short uint16_t;
493 typedef unsigned char uint8_t;
497 typedef int64_t lws_usec_t;
498 typedef unsigned long long lws_filepos_t;
499 typedef long long lws_fileofs_t;
500 typedef uint32_t lws_fop_flags_t;
502 #define lws_concat_temp(_t, _l) (_t + sizeof(_t) - _l)
503 #define lws_concat_used(_t, _l) (sizeof(_t) - _l)
505 /** struct lws_pollargs - argument structure for all external poll related calls
506 * passed in via 'in' */
507 struct lws_pollargs {
508 lws_sockfd_type fd; /**< applicable socket descriptor */
509 int events; /**< the new event mask */
510 int prev_events; /**< the previous event mask */
513 struct lws_extension; /* needed even with ws exts disabled for create context */
514 struct lws_token_limits;
520 #include <libwebsockets/lws-system.h>
521 #include <libwebsockets/lws-ws-close.h>
522 #include <libwebsockets/lws-callbacks.h>
523 #include <libwebsockets/lws-ws-state.h>
524 #include <libwebsockets/lws-ws-ext.h>
525 #include <libwebsockets/lws-protocols-plugins.h>
526 #include <libwebsockets/lws-plugin-generic-sessions.h>
527 #include <libwebsockets/lws-context-vhost.h>
528 #include <libwebsockets/lws-client.h>
529 #include <libwebsockets/lws-http.h>
530 #include <libwebsockets/lws-spa.h>
531 #include <libwebsockets/lws-purify.h>
532 #include <libwebsockets/lws-misc.h>
533 #include <libwebsockets/lws-dsh.h>
534 #include <libwebsockets/lws-timeout-timer.h>
535 #include <libwebsockets/lws-service.h>
536 #include <libwebsockets/lws-write.h>
537 #include <libwebsockets/lws-writeable.h>
538 #include <libwebsockets/lws-adopt.h>
539 #include <libwebsockets/lws-network-helper.h>
540 #include <libwebsockets/lws-ring.h>
541 #include <libwebsockets/lws-sha1-base64.h>
542 #include <libwebsockets/lws-x509.h>
543 #include <libwebsockets/lws-cgi.h>
544 #include <libwebsockets/lws-vfs.h>
545 #include <libwebsockets/lws-lejp.h>
546 #include <libwebsockets/lws-stats.h>
547 #include <libwebsockets/lws-struct.h>
548 #include <libwebsockets/lws-threadpool.h>
549 #include <libwebsockets/lws-tokenize.h>
550 #include <libwebsockets/lws-lwsac.h>
551 #include <libwebsockets/lws-fts.h>
552 #include <libwebsockets/lws-diskcache.h>
553 #include <libwebsockets/lws-retry.h>
554 #include <libwebsockets/lws-sequencer.h>
556 #include <libwebsockets/abstract/abstract.h>
558 #include <libwebsockets/lws-test-sequencer.h>
560 #if defined(LWS_WITH_TLS)
562 #if defined(LWS_WITH_MBEDTLS)
563 #include <mbedtls/md5.h>
564 #include <mbedtls/sha1.h>
565 #include <mbedtls/sha256.h>
566 #include <mbedtls/sha512.h>
569 #include <libwebsockets/lws-gencrypto.h>
570 #include <libwebsockets/lws-genhash.h>
571 #include <libwebsockets/lws-genrsa.h>
572 #include <libwebsockets/lws-genaes.h>
573 #include <libwebsockets/lws-genec.h>
575 #include <libwebsockets/lws-jwk.h>
576 #include <libwebsockets/lws-jose.h>
577 #include <libwebsockets/lws-jws.h>
578 #include <libwebsockets/lws-jwe.h>