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_O_RDONLY _O_RDONLY
89 #define LWS_O_WRONLY _O_WRONLY
90 #define LWS_O_CREAT _O_CREAT
91 #define LWS_O_TRUNC _O_TRUNC
92 
93 #ifndef __func__
94 #define __func__ __FUNCTION__
95 #endif
96 
97 #else /* NOT WIN32 */
98 #include <unistd.h>
99 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
100 #include <sys/capability.h>
101 #endif
102 
103 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__QNX__) || defined(__OpenBSD__)
104 #include <sys/socket.h>
105 #include <netinet/in.h>
106 #endif
107 
108 #define LWS_INLINE inline
109 #define LWS_O_RDONLY O_RDONLY
110 #define LWS_O_WRONLY O_WRONLY
111 #define LWS_O_CREAT O_CREAT
112 #define LWS_O_TRUNC O_TRUNC
113 
114 #if !defined(LWS_PLAT_OPTEE) && !defined(OPTEE_TA) && !defined(LWS_WITH_ESP32)
115 #include <poll.h>
116 #include <netdb.h>
117 #define LWS_INVALID_FILE -1
118 #else
119 #define getdtablesize() (30)
120 #if defined(LWS_WITH_ESP32)
121 #define LWS_INVALID_FILE NULL
122 #else
123 #define LWS_INVALID_FILE NULL
124 #endif
125 #endif
126 
127 #if defined(__GNUC__)
128 
129 /* warn_unused_result attribute only supported by GCC 3.4 or later */
130 #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
131 #define LWS_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
132 #else
133 #define LWS_WARN_UNUSED_RESULT
134 #endif
135 
136 #define LWS_VISIBLE __attribute__((visibility("default")))
137 #define LWS_WARN_DEPRECATED __attribute__ ((deprecated))
138 #define LWS_FORMAT(string_index) __attribute__ ((format(printf, string_index, string_index+1)))
139 #else
140 #define LWS_VISIBLE
141 #define LWS_WARN_UNUSED_RESULT
142 #define LWS_WARN_DEPRECATED
143 #define LWS_FORMAT(string_index)
144 #endif
145 
146 #if defined(__ANDROID__)
147 #include <netinet/in.h>
148 #include <unistd.h>
149 #endif
150 
151 #endif
152 
153 #if defined(LWS_WITH_LIBEV)
154 #include <ev.h>
155 #endif /* LWS_WITH_LIBEV */
156 #ifdef LWS_WITH_LIBUV
157 #include <uv.h>
158 #ifdef LWS_HAVE_UV_VERSION_H
159 #include <uv-version.h>
160 #endif
161 #ifdef LWS_HAVE_NEW_UV_VERSION_H
162 #include <uv/version.h>
163 #endif
164 #endif /* LWS_WITH_LIBUV */
165 #if defined(LWS_WITH_LIBEVENT)
166 #include <event2/event.h>
167 #endif /* LWS_WITH_LIBEVENT */
168 
169 #ifndef LWS_EXTERN
170 #define LWS_EXTERN extern
171 #endif
172 
173 #ifdef _WIN32
174 #define random rand
175 #else
176 #if !defined(OPTEE_TA)
177 #include <sys/time.h>
178 #include <unistd.h>
179 #endif
180 #endif
181 
182 #if defined(LWS_WITH_TLS)
183 
184 #ifdef USE_WOLFSSL
185 #ifdef USE_OLD_CYASSL
186 #ifdef _WIN32
187 /*
188  * Include user-controlled settings for windows from
189  * <wolfssl-root>/IDE/WIN/user_settings.h
190  */
191 #include <IDE/WIN/user_settings.h>
192 #include <cyassl/ctaocrypt/settings.h>
193 #else
194 #include <cyassl/options.h>
195 #endif
196 #include <cyassl/openssl/ssl.h>
197 #include <cyassl/error-ssl.h>
198 
199 #else
200 #ifdef _WIN32
201 /*
202  * Include user-controlled settings for windows from
203  * <wolfssl-root>/IDE/WIN/user_settings.h
204  */
205 #include <IDE/WIN/user_settings.h>
206 #include <wolfssl/wolfcrypt/settings.h>
207 #else
208 #include <wolfssl/options.h>
209 #endif
210 #include <wolfssl/openssl/ssl.h>
211 #include <wolfssl/error-ssl.h>
212 #endif /* not USE_OLD_CYASSL */
213 #else
214 #if defined(LWS_WITH_MBEDTLS)
215 #if defined(LWS_WITH_ESP32)
216 /* this filepath is passed to us but without quotes or <> */
217 #if !defined(LWS_AMAZON_RTOS)
218 /* AMAZON RTOS has its own setting via MTK_MBEDTLS_CONFIG_FILE */
219 #undef MBEDTLS_CONFIG_FILE
220 #define MBEDTLS_CONFIG_FILE <mbedtls/esp_config.h>
221 #endif
222 #endif
223 #include <mbedtls/ssl.h>
224 #include <mbedtls/entropy.h>
225 #include <mbedtls/ctr_drbg.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 /* api change list for user code to test against */
296 
297 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
298 
299 /* the struct lws_protocols has the id field present */
300 #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
301 
302 /* you can call lws_get_peer_write_allowance */
303 #define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
304 
305 /* extra parameter introduced in 917f43ab821 */
306 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
307 
308 /* File operations stuff exists */
309 #define LWS_FEATURE_FOPS
310 
311 
312 #if defined(_WIN32)
313 #if !defined(LWS_WIN32_HANDLE_TYPES)
314 typedef SOCKET lws_sockfd_type;
315 typedef HANDLE lws_filefd_type;
316 #endif
317 
318 struct lws_pollfd {
319  lws_sockfd_type fd;
320  SHORT events;
321  SHORT revents;
322 };
323 #define LWS_POLLHUP (FD_CLOSE)
324 #define LWS_POLLIN (FD_READ | FD_ACCEPT)
325 #define LWS_POLLOUT (FD_WRITE)
326 #else
327 
328 
329 #if defined(LWS_WITH_ESP32)
330 #include <libwebsockets/lws-esp32.h>
331 #else
332 typedef int lws_sockfd_type;
333 typedef int lws_filefd_type;
334 #endif
335 
336 #define lws_pollfd pollfd
337 #define LWS_POLLHUP (POLLHUP|POLLERR)
338 #define LWS_POLLIN (POLLIN)
339 #define LWS_POLLOUT (POLLOUT)
340 #endif
341 
342 
343 #if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
344 /* ... */
345 #define ssize_t SSIZE_T
346 #endif
347 
348 #if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
349 #include <sys/types.h>
350 #include <sys/stat.h>
351 #endif
352 
353 #if defined(LWS_HAVE_STDINT_H)
354 #include <stdint.h>
355 #else
356 #if defined(WIN32) || defined(_WIN32)
357 /* !!! >:-[ */
358 typedef __int64 int64_t;
359 typedef unsigned __int64 uint64_t;
360 typedef __int32 int32_t;
361 typedef unsigned __int32 uint32_t;
362 typedef __int16 int16_t;
363 typedef unsigned __int16 uint16_t;
364 typedef unsigned __int8 uint8_t;
365 #else
366 typedef unsigned int uint32_t;
367 typedef unsigned short uint16_t;
368 typedef unsigned char uint8_t;
369 #endif
370 #endif
371 
372 typedef int64_t lws_usec_t;
373 typedef unsigned long long lws_filepos_t;
374 typedef long long lws_fileofs_t;
375 typedef uint32_t lws_fop_flags_t;
376 
379 struct lws_pollargs {
380  lws_sockfd_type fd;
381  int events;
383 };
384 
385 struct lws_extension; /* needed even with ws exts disabled for create context */
386 struct lws_token_limits;
387 struct lws_context;
388 struct lws_tokens;
389 struct lws_vhost;
390 struct lws;
391 
392 #include <libwebsockets/lws-ws-close.h>
393 #include <libwebsockets/lws-callbacks.h>
394 #include <libwebsockets/lws-ws-state.h>
395 #include <libwebsockets/lws-ws-ext.h>
396 #include <libwebsockets/lws-protocols-plugins.h>
397 #include <libwebsockets/lws-plugin-generic-sessions.h>
398 #include <libwebsockets/lws-context-vhost.h>
399 #include <libwebsockets/lws-client.h>
400 #include <libwebsockets/lws-http.h>
401 #include <libwebsockets/lws-spa.h>
402 #include <libwebsockets/lws-purify.h>
403 #include <libwebsockets/lws-timeout-timer.h>
404 #include <libwebsockets/lws-service.h>
405 #include <libwebsockets/lws-write.h>
406 #include <libwebsockets/lws-writeable.h>
407 #include <libwebsockets/lws-adopt.h>
408 #include <libwebsockets/lws-network-helper.h>
409 #include <libwebsockets/lws-misc.h>
410 #include <libwebsockets/lws-ring.h>
411 #include <libwebsockets/lws-sha1-base64.h>
412 #include <libwebsockets/lws-x509.h>
413 #include <libwebsockets/lws-cgi.h>
414 #include <libwebsockets/lws-vfs.h>
415 #include <libwebsockets/lws-lejp.h>
416 #include <libwebsockets/lws-stats.h>
417 #include <libwebsockets/lws-threadpool.h>
418 #include <libwebsockets/lws-tokenize.h>
419 #include <libwebsockets/lws-lwsac.h>
420 #include <libwebsockets/lws-fts.h>
421 #include <libwebsockets/lws-diskcache.h>
422 
423 #if defined(LWS_WITH_TLS)
424 
425 #if defined(LWS_WITH_MBEDTLS)
426 #include <mbedtls/sha1.h>
427 #include <mbedtls/sha256.h>
428 #include <mbedtls/sha512.h>
429 #endif
430 
431 #include <libwebsockets/lws-genhash.h>
432 #include <libwebsockets/lws-genrsa.h>
433 #include <libwebsockets/lws-jwk.h>
434 #include <libwebsockets/lws-jws.h>
435 
436 #endif
437 
438 #ifdef __cplusplus
439 }
440 #endif
441 
442 #endif
lws_pollargs
Definition: libwebsockets.h:379
lws_pollargs::events
int events
Definition: libwebsockets.h:381
lws_pollargs::prev_events
int prev_events
Definition: libwebsockets.h:382
lws_pollargs::fd
lws_sockfd_type fd
Definition: libwebsockets.h:380
lws_extension
Definition: lws-ws-ext.h:138
lws_tokens
Definition: lws-http.h:203
lws_token_limits
Definition: lws-http.h:332