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 #endif
154 
155 #endif
156 
157 #if defined(LWS_WITH_LIBEV)
158 #include <ev.h>
159 #endif /* LWS_WITH_LIBEV */
160 #ifdef LWS_WITH_LIBUV
161 #include <uv.h>
162 #ifdef LWS_HAVE_UV_VERSION_H
163 #include <uv-version.h>
164 #endif
165 #ifdef LWS_HAVE_NEW_UV_VERSION_H
166 #include <uv/version.h>
167 #endif
168 #endif /* LWS_WITH_LIBUV */
169 #if defined(LWS_WITH_LIBEVENT)
170 #include <event2/event.h>
171 #endif /* LWS_WITH_LIBEVENT */
172 
173 #ifndef LWS_EXTERN
174 #define LWS_EXTERN extern
175 #endif
176 
177 #ifdef _WIN32
178 #define random rand
179 #else
180 #if !defined(LWS_PLAT_OPTEE)
181 #include <sys/time.h>
182 #include <unistd.h>
183 #endif
184 #endif
185 
186 #if defined(LWS_WITH_TLS)
187 
188 #ifdef USE_WOLFSSL
189 #ifdef USE_OLD_CYASSL
190 #ifdef _WIN32
191 /*
192  * Include user-controlled settings for windows from
193  * <wolfssl-root>/IDE/WIN/user_settings.h
194  */
195 #include <IDE/WIN/user_settings.h>
196 #include <cyassl/ctaocrypt/settings.h>
197 #else
198 #include <cyassl/options.h>
199 #endif
200 #include <cyassl/openssl/ssl.h>
201 #include <cyassl/error-ssl.h>
202 
203 #else
204 #ifdef _WIN32
205 /*
206  * Include user-controlled settings for windows from
207  * <wolfssl-root>/IDE/WIN/user_settings.h
208  */
209 #include <IDE/WIN/user_settings.h>
210 #include <wolfssl/wolfcrypt/settings.h>
211 #else
212 #include <wolfssl/options.h>
213 #endif
214 #include <wolfssl/openssl/ssl.h>
215 #include <wolfssl/error-ssl.h>
216 #endif /* not USE_OLD_CYASSL */
217 #else
218 #if defined(LWS_WITH_MBEDTLS)
219 #if defined(LWS_WITH_ESP32)
220 /* this filepath is passed to us but without quotes or <> */
221 #if !defined(LWS_AMAZON_RTOS)
222 /* AMAZON RTOS has its own setting via MTK_MBEDTLS_CONFIG_FILE */
223 #undef MBEDTLS_CONFIG_FILE
224 #define MBEDTLS_CONFIG_FILE <mbedtls/esp_config.h>
225 #endif
226 #endif
227 #include <mbedtls/ssl.h>
228 #else
229 #include <openssl/ssl.h>
230 #if !defined(LWS_WITH_MBEDTLS)
231 #include <openssl/err.h>
232 #endif
233 #endif
234 #endif /* not USE_WOLFSSL */
235 #endif
236 
237 /*
238  * Helpers for pthread mutex in user code... if lws is built for
239  * multiple service threads, these resolve to pthread mutex
240  * operations. In the case LWS_MAX_SMP is 1 (the default), they
241  * are all NOPs and no pthread type or api is referenced.
242  */
243 
244 #if LWS_MAX_SMP > 1
245 
246 #include <pthread.h>
247 
248 #define lws_pthread_mutex(name) pthread_mutex_t name;
249 
250 static LWS_INLINE void
251 lws_pthread_mutex_init(pthread_mutex_t *lock)
252 {
253  pthread_mutex_init(lock, NULL);
254 }
255 
256 static LWS_INLINE void
257 lws_pthread_mutex_destroy(pthread_mutex_t *lock)
258 {
259  pthread_mutex_destroy(lock);
260 }
261 
262 static LWS_INLINE void
263 lws_pthread_mutex_lock(pthread_mutex_t *lock)
264 {
265  pthread_mutex_lock(lock);
266 }
267 
268 static LWS_INLINE void
269 lws_pthread_mutex_unlock(pthread_mutex_t *lock)
270 {
271  pthread_mutex_unlock(lock);
272 }
273 
274 #else
275 #define lws_pthread_mutex(name)
276 #define lws_pthread_mutex_init(_a)
277 #define lws_pthread_mutex_destroy(_a)
278 #define lws_pthread_mutex_lock(_a)
279 #define lws_pthread_mutex_unlock(_a)
280 #endif
281 
282 
283 #define CONTEXT_PORT_NO_LISTEN -1
284 #define CONTEXT_PORT_NO_LISTEN_SERVER -2
285 
286 #include <libwebsockets/lws-logs.h>
287 
288 
289 #include <stddef.h>
290 
291 #ifndef lws_container_of
292 #define lws_container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
293 #endif
294 
295 struct lws;
296 
297 typedef int64_t lws_usec_t;
298 
299 /* api change list for user code to test against */
300 
301 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
302 
303 /* the struct lws_protocols has the id field present */
304 #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
305 
306 /* you can call lws_get_peer_write_allowance */
307 #define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
308 
309 /* extra parameter introduced in 917f43ab821 */
310 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
311 
312 /* File operations stuff exists */
313 #define LWS_FEATURE_FOPS
314 
315 
316 #if defined(_WIN32)
317 #if !defined(LWS_WIN32_HANDLE_TYPES)
318 typedef SOCKET lws_sockfd_type;
319 typedef HANDLE lws_filefd_type;
320 #endif
321 
322 struct lws_pollfd {
323  lws_sockfd_type fd;
324  SHORT events;
325  SHORT revents;
326 };
327 #define LWS_POLLHUP (FD_CLOSE)
328 #define LWS_POLLIN (FD_READ | FD_ACCEPT)
329 #define LWS_POLLOUT (FD_WRITE)
330 #else
331 
332 
333 #if defined(LWS_WITH_ESP32)
334 #include <libwebsockets/lws-esp32.h>
335 #else
336 typedef int lws_sockfd_type;
337 typedef int lws_filefd_type;
338 #endif
339 
340 #if defined(LWS_PLAT_OPTEE)
341 #include <time.h>
342 struct timeval {
343  time_t tv_sec;
344  unsigned int tv_usec;
345 };
346 #if defined(LWS_WITH_NETWORK)
347 // #include <poll.h>
348 #define lws_pollfd pollfd
349 
350 struct timezone;
351 
352 int gettimeofday(struct timeval *tv, struct timezone *tz);
353 
354  /* Internet address. */
355  struct in_addr {
356  uint32_t s_addr; /* address in network byte order */
357  };
358 
359 typedef unsigned short sa_family_t;
360 typedef unsigned short in_port_t;
361 typedef uint32_t socklen_t;
362 
363 #include <libwebsockets/lws-optee.h>
364 
365 #if !defined(TEE_SE_READER_NAME_MAX)
366  struct addrinfo {
367  int ai_flags;
368  int ai_family;
369  int ai_socktype;
370  int ai_protocol;
371  socklen_t ai_addrlen;
372  struct sockaddr *ai_addr;
373  char *ai_canonname;
374  struct addrinfo *ai_next;
375  };
376 #endif
377 
378 ssize_t recv(int sockfd, void *buf, size_t len, int flags);
379 ssize_t send(int sockfd, const void *buf, size_t len, int flags);
380 ssize_t read(int fd, void *buf, size_t count);
381 int getsockopt(int sockfd, int level, int optname,
382  void *optval, socklen_t *optlen);
383  int setsockopt(int sockfd, int level, int optname,
384  const void *optval, socklen_t optlen);
385 int connect(int sockfd, const struct sockaddr *addr,
386  socklen_t addrlen);
387 
388 extern int errno;
389 
390 uint16_t ntohs(uint16_t netshort);
391 uint16_t htons(uint16_t hostshort);
392 
393 int bind(int sockfd, const struct sockaddr *addr,
394  socklen_t addrlen);
395 
396 
397 #define MSG_NOSIGNAL 0x4000
398 #define EAGAIN 11
399 #define EINTR 4
400 #define EWOULDBLOCK EAGAIN
401 #define EADDRINUSE 98
402 #define INADDR_ANY 0
403 #define AF_INET 2
404 #define SHUT_WR 1
405 #define AF_UNSPEC 0
406 #define PF_UNSPEC 0
407 #define SOCK_STREAM 1
408 #define SOCK_DGRAM 2
409 # define AI_PASSIVE 0x0001
410 #define IPPROTO_UDP 17
411 #define SOL_SOCKET 1
412 #define SO_SNDBUF 7
413 #define EISCONN 106
414 #define EALREADY 114
415 #define EINPROGRESS 115
416 int shutdown(int sockfd, int how);
417 int close(int fd);
418 int atoi(const char *nptr);
419 long long atoll(const char *nptr);
420 
421 int socket(int domain, int type, int protocol);
422  int getaddrinfo(const char *node, const char *service,
423  const struct addrinfo *hints,
424  struct addrinfo **res);
425 
426  void freeaddrinfo(struct addrinfo *res);
427 
428 #if !defined(TEE_SE_READER_NAME_MAX)
429 struct lws_pollfd
430 {
431  int fd; /* File descriptor to poll. */
432  short int events; /* Types of events poller cares about. */
433  short int revents; /* Types of events that actually occurred. */
434 };
435 #endif
436 
437 int poll(struct pollfd *fds, int nfds, int timeout);
438 
439 #define LWS_POLLHUP (0x18)
440 #define LWS_POLLIN (1)
441 #define LWS_POLLOUT (4)
442 #else
443 struct lws_pollfd;
444 struct sockaddr_in;
445 #endif
446 #else
447 #define lws_pollfd pollfd
448 #define LWS_POLLHUP (POLLHUP | POLLERR)
449 #define LWS_POLLIN (POLLIN)
450 #define LWS_POLLOUT (POLLOUT)
451 #endif
452 #endif
453 
454 
455 #if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
456 /* ... */
457 #define ssize_t SSIZE_T
458 #endif
459 
460 #if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
461 #include <sys/types.h>
462 #include <sys/stat.h>
463 #endif
464 
465 #if defined(LWS_HAVE_STDINT_H)
466 #include <stdint.h>
467 #else
468 #if defined(WIN32) || defined(_WIN32)
469 /* !!! >:-[ */
470 typedef unsigned __int32 uint32_t;
471 typedef unsigned __int16 uint16_t;
472 typedef unsigned __int8 uint8_t;
473 #else
474 typedef unsigned int uint32_t;
475 typedef unsigned short uint16_t;
476 typedef unsigned char uint8_t;
477 #endif
478 #endif
479 
480 typedef unsigned long long lws_filepos_t;
481 typedef long long lws_fileofs_t;
482 typedef uint32_t lws_fop_flags_t;
483 
484 #define lws_concat_temp(_t, _l) (_t + sizeof(_t) - _l)
485 #define lws_concat_used(_t, _l) (sizeof(_t) - _l)
486 
489 struct lws_pollargs {
490  lws_sockfd_type fd;
491  int events;
493 };
494 
495 struct lws_extension; /* needed even with ws exts disabled for create context */
496 struct lws_token_limits;
497 struct lws_context;
498 struct lws_tokens;
499 struct lws_vhost;
500 struct lws;
501 
502 #include <libwebsockets/lws-ws-close.h>
503 #include <libwebsockets/lws-callbacks.h>
504 #include <libwebsockets/lws-ws-state.h>
505 #include <libwebsockets/lws-ws-ext.h>
506 #include <libwebsockets/lws-protocols-plugins.h>
507 #include <libwebsockets/lws-plugin-generic-sessions.h>
508 #include <libwebsockets/lws-context-vhost.h>
509 #include <libwebsockets/lws-client.h>
510 #include <libwebsockets/lws-http.h>
511 #include <libwebsockets/lws-spa.h>
512 #include <libwebsockets/lws-purify.h>
513 #include <libwebsockets/lws-timeout-timer.h>
514 #include <libwebsockets/lws-service.h>
515 #include <libwebsockets/lws-write.h>
516 #include <libwebsockets/lws-writeable.h>
517 #include <libwebsockets/lws-adopt.h>
518 #include <libwebsockets/lws-network-helper.h>
519 #include <libwebsockets/lws-misc.h>
520 #include <libwebsockets/lws-ring.h>
521 #include <libwebsockets/lws-sha1-base64.h>
522 #include <libwebsockets/lws-x509.h>
523 #include <libwebsockets/lws-cgi.h>
524 #include <libwebsockets/lws-vfs.h>
525 #include <libwebsockets/lws-lejp.h>
526 #include <libwebsockets/lws-stats.h>
527 #include <libwebsockets/lws-struct.h>
528 #include <libwebsockets/lws-threadpool.h>
529 #include <libwebsockets/lws-tokenize.h>
530 #include <libwebsockets/lws-lwsac.h>
531 #include <libwebsockets/lws-fts.h>
532 #include <libwebsockets/lws-diskcache.h>
533 
534 #include <libwebsockets/abstract/transports.h>
535 #include <libwebsockets/abstract/smtp.h>
536 
537 #if defined(LWS_WITH_TLS)
538 
539 #if defined(LWS_WITH_MBEDTLS)
540 #include <mbedtls/sha1.h>
541 #include <mbedtls/sha256.h>
542 #include <mbedtls/sha512.h>
543 #endif
544 
545 #include <libwebsockets/lws-gencrypto.h>
546 #include <libwebsockets/lws-genhash.h>
547 #include <libwebsockets/lws-genrsa.h>
548 #include <libwebsockets/lws-genaes.h>
549 #include <libwebsockets/lws-genec.h>
550 
551 #include <libwebsockets/lws-jwk.h>
552 #include <libwebsockets/lws-jose.h>
553 #include <libwebsockets/lws-jws.h>
554 #include <libwebsockets/lws-jwe.h>
555 
556 #endif
557 
558 #ifdef __cplusplus
559 }
560 #endif
561 
562 #endif
lws_sockfd_type fd
Definition: libwebsockets.h:490
Definition: lws-ws-ext.h:138
Definition: libwebsockets.h:489
Definition: lws-esp32.h:27
int prev_events
Definition: libwebsockets.h:492
Definition: lws-http.h:335
Definition: lws-http.h:203
int events
Definition: libwebsockets.h:491