Imported Upstream version 3.2.0
[platform/upstream/libwebsockets.git] / include / libwebsockets.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2019 Andy Green <andy@warmcat.com>
5  *
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.
10  *
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.
15  *
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,
19  *  MA  02110-1301  USA
20  */
21
22 /** @file */
23
24 #ifndef LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
25 #define LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C
26
27 #ifdef __cplusplus
28 #include <cstddef>
29 #include <cstdarg>
30
31 extern "C" {
32 #else
33 #include <stdarg.h>
34 #endif
35
36 #include <string.h>
37 #include <stdlib.h>
38
39 #include "lws_config.h"
40
41 /*
42  * CARE: everything using cmake defines needs to be below here
43  */
44
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
49
50 #define LWS_KI (1024)
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)
55
56 #define LWS_US_TO_MS(x) ((x + (LWS_US_PER_MS / 2)) / LWS_US_PER_MS)
57
58 #if defined(LWS_HAS_INTPTR_T)
59 #include <stdint.h>
60 #define lws_intptr_t intptr_t
61 #else
62 typedef unsigned long long lws_intptr_t;
63 #endif
64
65 #if defined(WIN32) || defined(_WIN32)
66 #ifndef WIN32_LEAN_AND_MEAN
67 #define WIN32_LEAN_AND_MEAN
68 #endif
69
70 #include <winsock2.h>
71 #include <ws2tcpip.h>
72 #include <stddef.h>
73 #include <basetsd.h>
74 #include <io.h>
75 #ifndef _WIN32_WCE
76 #include <fcntl.h>
77 #else
78 #define _O_RDONLY       0x0000
79 #define O_RDONLY        _O_RDONLY
80 #endif
81
82 #define LWS_INLINE __inline
83 #define LWS_VISIBLE
84 #define LWS_WARN_UNUSED_RESULT
85 #define LWS_WARN_DEPRECATED
86 #define LWS_FORMAT(string_index)
87
88 #if !defined(LWS_EXTERN)
89 #ifdef LWS_DLL
90 #ifdef LWS_INTERNAL
91 #define LWS_EXTERN extern __declspec(dllexport)
92 #else
93 #define LWS_EXTERN extern __declspec(dllimport)
94 #endif
95 #else
96 #define LWS_EXTERN
97 #endif
98 #endif
99
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
106
107 #ifndef __func__
108 #define __func__ __FUNCTION__
109 #endif
110
111 #else /* NOT WIN32 */
112 #include <unistd.h>
113 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
114 #include <sys/capability.h>
115 #endif
116
117 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__QNX__) || defined(__OpenBSD__)
118 #include <sys/socket.h>
119 #include <netinet/in.h>
120 #endif
121
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
127
128 #if !defined(LWS_PLAT_OPTEE) && !defined(OPTEE_TA) && !defined(LWS_WITH_ESP32)
129 #include <poll.h>
130 #include <netdb.h>
131 #define LWS_INVALID_FILE -1
132 #define LWS_SOCK_INVALID (-1)
133 #else
134 #define getdtablesize() (30)
135 #if defined(LWS_WITH_ESP32)
136 #define LWS_INVALID_FILE NULL
137 #define LWS_SOCK_INVALID (-1)
138 #else
139 #define LWS_INVALID_FILE NULL
140 #define LWS_SOCK_INVALID (-1)
141 #endif
142 #endif
143
144 #if defined(__GNUC__)
145
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))
149 #else
150 #define LWS_WARN_UNUSED_RESULT
151 #endif
152
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)))
156 #else
157 #define LWS_VISIBLE
158 #define LWS_WARN_UNUSED_RESULT
159 #define LWS_WARN_DEPRECATED
160 #define LWS_FORMAT(string_index)
161 #endif
162
163 #if defined(__ANDROID__)
164 #include <netinet/in.h>
165 #include <unistd.h>
166 #endif
167
168 #endif
169
170 #if defined(LWS_WITH_LIBEV)
171 #include <ev.h>
172 #endif /* LWS_WITH_LIBEV */
173 #ifdef LWS_WITH_LIBUV
174 #include <uv.h>
175 #ifdef LWS_HAVE_UV_VERSION_H
176 #include <uv-version.h>
177 #endif
178 #ifdef LWS_HAVE_NEW_UV_VERSION_H
179 #include <uv/version.h>
180 #endif
181 #endif /* LWS_WITH_LIBUV */
182 #if defined(LWS_WITH_LIBEVENT)
183 #include <event2/event.h>
184 #endif /* LWS_WITH_LIBEVENT */
185
186 #ifndef LWS_EXTERN
187 #define LWS_EXTERN extern
188 #endif
189
190 #ifdef _WIN32
191 #define random rand
192 #else
193 #if !defined(LWS_PLAT_OPTEE)
194 #include <sys/time.h>
195 #include <unistd.h>
196 #endif
197 #endif
198
199 #if defined(LWS_WITH_TLS)
200
201 #ifdef USE_WOLFSSL
202 #ifdef USE_OLD_CYASSL
203 #ifdef _WIN32
204 /*
205  * Include user-controlled settings for windows from
206  * <wolfssl-root>/IDE/WIN/user_settings.h
207  */
208 #include <IDE/WIN/user_settings.h>
209 #include <cyassl/ctaocrypt/settings.h>
210 #else
211 #include <cyassl/options.h>
212 #endif
213 #include <cyassl/openssl/ssl.h>
214 #include <cyassl/error-ssl.h>
215
216 #else
217 #ifdef _WIN32
218 /*
219  * Include user-controlled settings for windows from
220  * <wolfssl-root>/IDE/WIN/user_settings.h
221  */
222 #include <IDE/WIN/user_settings.h>
223 #include <wolfssl/wolfcrypt/settings.h>
224 #else
225 #include <wolfssl/options.h>
226 #endif
227 #include <wolfssl/openssl/ssl.h>
228 #include <wolfssl/error-ssl.h>
229 #endif /* not USE_OLD_CYASSL */
230 #else
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>
238 #endif
239 #endif
240 #include <mbedtls/ssl.h>
241 #include <mbedtls/entropy.h>
242 #include <mbedtls/ctr_drbg.h>
243 #else
244 #include <openssl/ssl.h>
245 #if !defined(LWS_WITH_MBEDTLS)
246 #include <openssl/err.h>
247 #endif
248 #endif
249 #endif /* not USE_WOLFSSL */
250 #endif
251
252 /*
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.
257  */
258
259 #if LWS_MAX_SMP > 1
260
261 #include <pthread.h>
262
263 #define lws_pthread_mutex(name) pthread_mutex_t name;
264
265 static LWS_INLINE void
266 lws_pthread_mutex_init(pthread_mutex_t *lock)
267 {
268         pthread_mutex_init(lock, NULL);
269 }
270
271 static LWS_INLINE void
272 lws_pthread_mutex_destroy(pthread_mutex_t *lock)
273 {
274         pthread_mutex_destroy(lock);
275 }
276
277 static LWS_INLINE void
278 lws_pthread_mutex_lock(pthread_mutex_t *lock)
279 {
280         pthread_mutex_lock(lock);
281 }
282
283 static LWS_INLINE void
284 lws_pthread_mutex_unlock(pthread_mutex_t *lock)
285 {
286         pthread_mutex_unlock(lock);
287 }
288
289 #else
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)
295 #endif
296
297
298 #define CONTEXT_PORT_NO_LISTEN -1
299 #define CONTEXT_PORT_NO_LISTEN_SERVER -2
300
301 #include <libwebsockets/lws-logs.h>
302
303
304 #include <stddef.h>
305
306 #ifndef lws_container_of
307 #define lws_container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
308 #endif
309
310 struct lws;
311
312 /* api change list for user code to test against */
313
314 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
315
316 /* the struct lws_protocols has the id field present */
317 #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
318
319 /* you can call lws_get_peer_write_allowance */
320 #define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
321
322 /* extra parameter introduced in 917f43ab821 */
323 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
324
325 /* File operations stuff exists */
326 #define LWS_FEATURE_FOPS
327
328
329 #if defined(_WIN32)
330 #if !defined(LWS_WIN32_HANDLE_TYPES)
331 typedef SOCKET lws_sockfd_type;
332 typedef HANDLE lws_filefd_type;
333 #endif
334
335 struct lws_pollfd {
336         lws_sockfd_type fd; /**< file descriptor */
337         SHORT events; /**< which events to respond to */
338         SHORT revents; /**< which events happened */
339 };
340 #define LWS_POLLHUP (FD_CLOSE)
341 #define LWS_POLLIN (FD_READ | FD_ACCEPT)
342 #define LWS_POLLOUT (FD_WRITE)
343 #else
344
345
346 #if defined(LWS_WITH_ESP32)
347 #include <libwebsockets/lws-esp32.h>
348 #else
349 typedef int lws_sockfd_type;
350 typedef int lws_filefd_type;
351 #endif
352
353 #if defined(LWS_PLAT_OPTEE)
354 #include <time.h>
355 struct timeval {
356         time_t          tv_sec;
357         unsigned int    tv_usec;
358 };
359 #if defined(LWS_WITH_NETWORK)
360 // #include <poll.h>
361 #define lws_pollfd pollfd
362
363 struct timezone;
364
365 int gettimeofday(struct timeval *tv, struct timezone *tz);
366
367     /* Internet address. */
368     struct in_addr {
369         uint32_t       s_addr;     /* address in network byte order */
370     };
371
372 typedef unsigned short sa_family_t;
373 typedef unsigned short in_port_t;
374 typedef uint32_t socklen_t;
375
376 #include <libwebsockets/lws-optee.h>
377
378 #if !defined(TEE_SE_READER_NAME_MAX)
379            struct addrinfo {
380                int              ai_flags;
381                int              ai_family;
382                int              ai_socktype;
383                int              ai_protocol;
384                socklen_t        ai_addrlen;
385                struct sockaddr *ai_addr;
386                char            *ai_canonname;
387                struct addrinfo *ai_next;
388            };
389 #endif
390
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,
399                    socklen_t addrlen);
400
401 extern int errno;
402
403 uint16_t ntohs(uint16_t netshort);
404 uint16_t htons(uint16_t hostshort);
405
406 int bind(int sockfd, const struct sockaddr *addr,
407                 socklen_t addrlen);
408
409
410 #define  MSG_NOSIGNAL 0x4000
411 #define EAGAIN          11
412 #define EINTR           4
413 #define EWOULDBLOCK     EAGAIN
414 #define EADDRINUSE      98      
415 #define INADDR_ANY      0
416 #define AF_INET         2
417 #define SHUT_WR 1
418 #define AF_UNSPEC       0
419 #define PF_UNSPEC       0
420 #define SOCK_STREAM     1
421 #define SOCK_DGRAM      2
422 # define AI_PASSIVE     0x0001
423 #define IPPROTO_UDP     17
424 #define SOL_SOCKET      1
425 #define SO_SNDBUF       7
426 #define EISCONN         106     
427 #define EALREADY        114
428 #define EINPROGRESS     115
429 int shutdown(int sockfd, int how);
430 int close(int fd);
431 int atoi(const char *nptr);
432 long long atoll(const char *nptr);
433
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);
438
439        void freeaddrinfo(struct addrinfo *res);
440
441 #if !defined(TEE_SE_READER_NAME_MAX)
442 struct lws_pollfd
443 {
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.  */
447 };
448 #endif
449
450 int poll(struct pollfd *fds, int nfds, int timeout);
451
452 #define LWS_POLLHUP (0x18)
453 #define LWS_POLLIN (1)
454 #define LWS_POLLOUT (4)
455 #else
456 struct lws_pollfd;
457 struct sockaddr_in;
458 #endif
459 #else
460 #define lws_pollfd pollfd
461 #define LWS_POLLHUP (POLLHUP | POLLERR)
462 #define LWS_POLLIN (POLLIN)
463 #define LWS_POLLOUT (POLLOUT)
464 #endif
465 #endif
466
467
468 #if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
469 /* ... */
470 #define ssize_t SSIZE_T
471 #endif
472
473 #if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
474 #include <sys/types.h>
475 #include <sys/stat.h>
476 #endif
477
478 #if defined(LWS_HAVE_STDINT_H)
479 #include <stdint.h>
480 #else
481 #if defined(WIN32) || defined(_WIN32)
482 /* !!! >:-[  */
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;
490 #else
491 typedef unsigned int uint32_t;
492 typedef unsigned short uint16_t;
493 typedef unsigned char uint8_t;
494 #endif
495 #endif
496
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;
501
502 #define lws_concat_temp(_t, _l) (_t + sizeof(_t) - _l)
503 #define lws_concat_used(_t, _l) (sizeof(_t) - _l)
504
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 */
511 };
512
513 struct lws_extension; /* needed even with ws exts disabled for create context */
514 struct lws_token_limits;
515 struct lws_context;
516 struct lws_tokens;
517 struct lws_vhost;
518 struct lws;
519
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>
555
556 #include <libwebsockets/abstract/abstract.h>
557
558 #include <libwebsockets/lws-test-sequencer.h>
559
560 #if defined(LWS_WITH_TLS)
561
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>
567 #endif
568
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>
574
575 #include <libwebsockets/lws-jwk.h>
576 #include <libwebsockets/lws-jose.h>
577 #include <libwebsockets/lws-jws.h>
578 #include <libwebsockets/lws-jwe.h>
579
580 #endif
581
582 #ifdef __cplusplus
583 }
584 #endif
585
586 #endif