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 ((lws_usec_t)1000000)
54 #define LWS_MS_PER_SEC ((lws_usec_t)1000)
55 #define LWS_US_PER_MS ((lws_usec_t)1000)
56 #define LWS_NS_PER_US ((lws_usec_t)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 #endif
104 #endif
105 
106 #define LWS_INVALID_FILE INVALID_HANDLE_VALUE
107 #define LWS_SOCK_INVALID (INVALID_SOCKET)
108 #define LWS_O_RDONLY _O_RDONLY
109 #define LWS_O_WRONLY _O_WRONLY
110 #define LWS_O_CREAT _O_CREAT
111 #define LWS_O_TRUNC _O_TRUNC
112 
113 #ifndef __func__
114 #define __func__ __FUNCTION__
115 #endif
116 
117 #else /* NOT WIN32 */
118 #include <unistd.h>
119 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
120 #include <sys/capability.h>
121 #endif
122 
123 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__QNX__) || defined(__OpenBSD__)
124 #include <sys/socket.h>
125 #include <netinet/in.h>
126 #endif
127 
128 #define LWS_INLINE inline
129 #define LWS_O_RDONLY O_RDONLY
130 #define LWS_O_WRONLY O_WRONLY
131 #define LWS_O_CREAT O_CREAT
132 #define LWS_O_TRUNC O_TRUNC
133 
134 #if !defined(LWS_PLAT_OPTEE) && !defined(OPTEE_TA) && !defined(LWS_PLAT_FREERTOS)
135 #include <poll.h>
136 #include <netdb.h>
137 #define LWS_INVALID_FILE -1
138 #define LWS_SOCK_INVALID (-1)
139 #else
140 #define getdtablesize() (30)
141 #if defined(LWS_PLAT_FREERTOS)
142 #define LWS_INVALID_FILE NULL
143 #define LWS_SOCK_INVALID (-1)
144 #else
145 #define LWS_INVALID_FILE NULL
146 #define LWS_SOCK_INVALID (-1)
147 #endif
148 #endif
149 
150 #if defined(__FreeBSD__)
151 #include <sys/signal.h>
152 #endif
153 
154 #if defined(__GNUC__)
155 
156 /* warn_unused_result attribute only supported by GCC 3.4 or later */
157 #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
158 #define LWS_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
159 #else
160 #define LWS_WARN_UNUSED_RESULT
161 #endif
162 
163 #define LWS_VISIBLE __attribute__((visibility("default")))
164 #define LWS_WARN_DEPRECATED __attribute__ ((deprecated))
165 #define LWS_FORMAT(string_index) __attribute__ ((format(printf, string_index, string_index+1)))
166 #else
167 #define LWS_VISIBLE
168 #define LWS_WARN_UNUSED_RESULT
169 #define LWS_WARN_DEPRECATED
170 #define LWS_FORMAT(string_index)
171 #endif
172 
173 #if defined(__ANDROID__)
174 #include <netinet/in.h>
175 #include <unistd.h>
176 #endif
177 
178 #endif
179 
180 #ifndef LWS_EXTERN
181 #define LWS_EXTERN extern
182 #endif
183 
184 #ifdef _WIN32
185 #define random rand
186 #else
187 #if !defined(LWS_PLAT_OPTEE)
188 #include <sys/time.h>
189 #include <unistd.h>
190 #endif
191 #endif
192 
193 #if defined(LWS_WITH_LIBUV_INTERNAL)
194 #include <uv.h>
195 
196 #ifdef LWS_HAVE_UV_VERSION_H
197 #include <uv-version.h>
198 #endif
199 
200 #ifdef LWS_HAVE_NEW_UV_VERSION_H
201 #include <uv/version.h>
202 #endif
203 #endif
204 
205 #if defined(LWS_WITH_TLS)
206 
207 #ifdef USE_WOLFSSL
208 #ifdef USE_OLD_CYASSL
209 #ifdef _WIN32
210 /*
211  * Include user-controlled settings for windows from
212  * <wolfssl-root>/IDE/WIN/user_settings.h
213  */
214 #include <IDE/WIN/user_settings.h>
215 #include <cyassl/ctaocrypt/settings.h>
216 #else
217 #include <cyassl/options.h>
218 #endif
219 #include <cyassl/openssl/ssl.h>
220 #include <cyassl/error-ssl.h>
221 
222 #else
223 #ifdef _WIN32
224 /*
225  * Include user-controlled settings for windows from
226  * <wolfssl-root>/IDE/WIN/user_settings.h
227  */
228 #include <IDE/WIN/user_settings.h>
229 #include <wolfssl/wolfcrypt/settings.h>
230 #else
231 #include <wolfssl/options.h>
232 #endif
233 #include <wolfssl/openssl/ssl.h>
234 #include <wolfssl/error-ssl.h>
235 #endif /* not USE_OLD_CYASSL */
236 #else
237 #if defined(LWS_WITH_MBEDTLS)
238 #if defined(LWS_PLAT_FREERTOS)
239 /* this filepath is passed to us but without quotes or <> */
240 #if !defined(LWS_AMAZON_RTOS)
241 /* AMAZON RTOS has its own setting via MTK_MBEDTLS_CONFIG_FILE */
242 #undef MBEDTLS_CONFIG_FILE
243 #define MBEDTLS_CONFIG_FILE <mbedtls/esp_config.h>
244 #endif
245 #endif
246 #if defined(LWS_WITH_TLS)
247 #include <mbedtls/ssl.h>
248 #include <mbedtls/entropy.h>
249 #include <mbedtls/ctr_drbg.h>
250 #endif
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  uint8_t write_blocked;
349 };
350 #define LWS_POLLHUP (FD_CLOSE)
351 #define LWS_POLLIN (FD_READ | FD_ACCEPT)
352 #define LWS_POLLOUT (FD_WRITE)
353 
354 #else
355 
356 
357 #if defined(LWS_PLAT_FREERTOS)
358 #include <libwebsockets/lws-freertos.h>
359 #else
360 typedef int lws_sockfd_type;
361 typedef int lws_filefd_type;
362 #endif
363 
364 #if defined(LWS_PLAT_OPTEE)
365 #include <time.h>
366 struct timeval {
367  time_t tv_sec;
368  unsigned int tv_usec;
369 };
370 #if defined(LWS_WITH_NETWORK)
371 // #include <poll.h>
372 #define lws_pollfd pollfd
373 
374 struct timezone;
375 
376 int gettimeofday(struct timeval *tv, struct timezone *tz);
377 
378  /* Internet address. */
379  struct in_addr {
380  uint32_t s_addr; /* address in network byte order */
381  };
382 
383 typedef unsigned short sa_family_t;
384 typedef unsigned short in_port_t;
385 typedef uint32_t socklen_t;
386 
387 #include <libwebsockets/lws-optee.h>
388 
389 #if !defined(TEE_SE_READER_NAME_MAX)
390  struct addrinfo {
391  int ai_flags;
392  int ai_family;
393  int ai_socktype;
394  int ai_protocol;
395  socklen_t ai_addrlen;
396  struct sockaddr *ai_addr;
397  char *ai_canonname;
398  struct addrinfo *ai_next;
399  };
400 #endif
401 
402 ssize_t recv(int sockfd, void *buf, size_t len, int flags);
403 ssize_t send(int sockfd, const void *buf, size_t len, int flags);
404 ssize_t read(int fd, void *buf, size_t count);
405 int getsockopt(int sockfd, int level, int optname,
406  void *optval, socklen_t *optlen);
407  int setsockopt(int sockfd, int level, int optname,
408  const void *optval, socklen_t optlen);
409 int connect(int sockfd, const struct sockaddr *addr,
410  socklen_t addrlen);
411 
412 extern int errno;
413 
414 uint16_t ntohs(uint16_t netshort);
415 uint16_t htons(uint16_t hostshort);
416 
417 int bind(int sockfd, const struct sockaddr *addr,
418  socklen_t addrlen);
419 
420 
421 #define MSG_NOSIGNAL 0x4000
422 #define EAGAIN 11
423 #define EINTR 4
424 #define EWOULDBLOCK EAGAIN
425 #define EADDRINUSE 98
426 #define INADDR_ANY 0
427 #define AF_INET 2
428 #define SHUT_WR 1
429 #define AF_UNSPEC 0
430 #define PF_UNSPEC 0
431 #define SOCK_STREAM 1
432 #define SOCK_DGRAM 2
433 # define AI_PASSIVE 0x0001
434 #define IPPROTO_UDP 17
435 #define SOL_SOCKET 1
436 #define SO_SNDBUF 7
437 #define EISCONN 106
438 #define EALREADY 114
439 #define EINPROGRESS 115
440 int shutdown(int sockfd, int how);
441 int close(int fd);
442 int atoi(const char *nptr);
443 long long atoll(const char *nptr);
444 
445 int socket(int domain, int type, int protocol);
446  int getaddrinfo(const char *node, const char *service,
447  const struct addrinfo *hints,
448  struct addrinfo **res);
449 
450  void freeaddrinfo(struct addrinfo *res);
451 
452 #if !defined(TEE_SE_READER_NAME_MAX)
453 struct lws_pollfd
454 {
455  int fd; /* File descriptor to poll. */
456  short int events; /* Types of events poller cares about. */
457  short int revents; /* Types of events that actually occurred. */
458 };
459 #endif
460 
461 int poll(struct pollfd *fds, int nfds, int timeout);
462 
463 #define LWS_POLLHUP (0x18)
464 #define LWS_POLLIN (1)
465 #define LWS_POLLOUT (4)
466 #else
467 struct lws_pollfd;
468 struct sockaddr_in;
469 #endif
470 #else
471 #define lws_pollfd pollfd
472 #define LWS_POLLHUP (POLLHUP | POLLERR)
473 #define LWS_POLLIN (POLLIN)
474 #define LWS_POLLOUT (POLLOUT)
475 #endif
476 #endif
477 
478 
479 #if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
480 /* ... */
481 #define ssize_t SSIZE_T
482 #endif
483 
484 #if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
485 #include <sys/types.h>
486 #include <sys/stat.h>
487 #endif
488 
489 #if defined(LWS_HAVE_STDINT_H)
490 #include <stdint.h>
491 #else
492 #if defined(WIN32) || defined(_WIN32)
493 /* !!! >:-[ */
494 typedef __int64 int64_t;
495 typedef unsigned __int64 uint64_t;
496 typedef __int32 int32_t;
497 typedef unsigned __int32 uint32_t;
498 typedef __int16 int16_t;
499 typedef unsigned __int16 uint16_t;
500 typedef unsigned __int8 uint8_t;
501 #else
502 typedef unsigned int uint32_t;
503 typedef unsigned short uint16_t;
504 typedef unsigned char uint8_t;
505 #endif
506 #endif
507 
508 typedef int64_t lws_usec_t;
509 typedef unsigned long long lws_filepos_t;
510 typedef long long lws_fileofs_t;
511 typedef uint32_t lws_fop_flags_t;
512 
513 #define lws_concat_temp(_t, _l) (_t + sizeof(_t) - _l)
514 #define lws_concat_used(_t, _l) (sizeof(_t) - _l)
515 
518 struct lws_pollargs {
519  lws_sockfd_type fd;
520  int events;
522 };
523 
524 struct lws_extension; /* needed even with ws exts disabled for create context */
525 struct lws_token_limits;
526 struct lws_protocols;
527 struct lws_context;
528 struct lws_tokens;
529 struct lws_vhost;
530 struct lws;
531 
532 #include <libwebsockets/lws-dll2.h>
533 #include <libwebsockets/lws-timeout-timer.h>
534 #if defined(LWS_WITH_SYS_SMD)
535 #include <libwebsockets/lws-smd.h>
536 #endif
537 #include <libwebsockets/lws-state.h>
538 #include <libwebsockets/lws-retry.h>
539 #include <libwebsockets/lws-adopt.h>
540 #include <libwebsockets/lws-network-helper.h>
541 #include <libwebsockets/lws-system.h>
542 #include <libwebsockets/lws-detailed-latency.h>
543 #include <libwebsockets/lws-ws-close.h>
544 #include <libwebsockets/lws-callbacks.h>
545 #include <libwebsockets/lws-ws-state.h>
546 #include <libwebsockets/lws-ws-ext.h>
547 #include <libwebsockets/lws-protocols-plugins.h>
548 
549 #include <libwebsockets/lws-context-vhost.h>
550 
551 #if defined(LWS_ROLE_MQTT)
552 #include <libwebsockets/lws-mqtt.h>
553 #endif
554 #include <libwebsockets/lws-client.h>
555 #include <libwebsockets/lws-http.h>
556 #include <libwebsockets/lws-spa.h>
557 #include <libwebsockets/lws-purify.h>
558 #include <libwebsockets/lws-misc.h>
559 #include <libwebsockets/lws-dsh.h>
560 #include <libwebsockets/lws-service.h>
561 #include <libwebsockets/lws-write.h>
562 #include <libwebsockets/lws-writeable.h>
563 #include <libwebsockets/lws-ring.h>
564 #include <libwebsockets/lws-sha1-base64.h>
565 #include <libwebsockets/lws-x509.h>
566 #include <libwebsockets/lws-cgi.h>
567 #if defined(LWS_WITH_FILE_OPS)
568 #include <libwebsockets/lws-vfs.h>
569 #endif
570 #include <libwebsockets/lws-lejp.h>
571 #include <libwebsockets/lws-stats.h>
572 #include <libwebsockets/lws-struct.h>
573 #include <libwebsockets/lws-threadpool.h>
574 #include <libwebsockets/lws-tokenize.h>
575 #include <libwebsockets/lws-lwsac.h>
576 #include <libwebsockets/lws-fts.h>
577 #include <libwebsockets/lws-diskcache.h>
578 #include <libwebsockets/lws-sequencer.h>
579 #include <libwebsockets/lws-secure-streams.h>
580 #include <libwebsockets/lws-secure-streams-policy.h>
581 #include <libwebsockets/lws-secure-streams-client.h>
582 
583 #if !defined(LWS_PLAT_FREERTOS)
584 #include <libwebsockets/abstract/abstract.h>
585 
586 #include <libwebsockets/lws-test-sequencer.h>
587 #endif
588 #include <libwebsockets/lws-async-dns.h>
589 
590 #if defined(LWS_WITH_TLS)
591 
592 #if defined(LWS_WITH_MBEDTLS)
593 #include <mbedtls/md5.h>
594 #include <mbedtls/sha1.h>
595 #include <mbedtls/sha256.h>
596 #include <mbedtls/sha512.h>
597 #endif
598 
599 #include <libwebsockets/lws-gencrypto.h>
600 #include <libwebsockets/lws-genhash.h>
601 #include <libwebsockets/lws-genrsa.h>
602 #include <libwebsockets/lws-genaes.h>
603 #include <libwebsockets/lws-genec.h>
604 
605 #include <libwebsockets/lws-jwk.h>
606 #include <libwebsockets/lws-jose.h>
607 #include <libwebsockets/lws-jws.h>
608 #include <libwebsockets/lws-jwe.h>
609 
610 #endif
611 
612 #include <libwebsockets/lws-eventlib-exports.h>
613 #include <libwebsockets/lws-i2c.h>
614 #include <libwebsockets/lws-spi.h>
615 #include <libwebsockets/lws-gpio.h>
616 #include <libwebsockets/lws-bb-i2c.h>
617 #include <libwebsockets/lws-bb-spi.h>
618 #include <libwebsockets/lws-button.h>
619 #include <libwebsockets/lws-led.h>
620 #include <libwebsockets/lws-pwm.h>
621 #include <libwebsockets/lws-display.h>
622 #include <libwebsockets/lws-ssd1306-i2c.h>
623 #include <libwebsockets/lws-ili9341-spi.h>
624 #include <libwebsockets/lws-settings.h>
625 #include <libwebsockets/lws-netdev.h>
626 
627 #ifdef __cplusplus
628 }
629 #endif
630 
631 #endif
Definition: lws-ws-ext.h:139
Definition: libwebsockets.h:518
int events
Definition: libwebsockets.h:520
int prev_events
Definition: libwebsockets.h:521
lws_sockfd_type fd
Definition: libwebsockets.h:519
Definition: lws-protocols-plugins.h:44
Definition: lws-http.h:368
Definition: lws-http.h:204