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