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