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