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 #define getdtablesize() sysconf(_SC_OPEN_MAX)
150 #endif
151 
152 #endif
153 
154 #if defined(LWS_WITH_LIBEV)
155 #include <ev.h>
156 #endif /* LWS_WITH_LIBEV */
157 #ifdef LWS_WITH_LIBUV
158 #include <uv.h>
159 #ifdef LWS_HAVE_UV_VERSION_H
160 #include <uv-version.h>
161 #endif
162 #ifdef LWS_HAVE_NEW_UV_VERSION_H
163 #include <uv/version.h>
164 #endif
165 #endif /* LWS_WITH_LIBUV */
166 #if defined(LWS_WITH_LIBEVENT)
167 #include <event2/event.h>
168 #endif /* LWS_WITH_LIBEVENT */
169 
170 #ifndef LWS_EXTERN
171 #define LWS_EXTERN extern
172 #endif
173 
174 #ifdef _WIN32
175 #define random rand
176 #else
177 #if !defined(OPTEE_TA)
178 #include <sys/time.h>
179 #include <unistd.h>
180 #endif
181 #endif
182 
183 #if defined(LWS_WITH_TLS)
184 
185 #ifdef USE_WOLFSSL
186 #ifdef USE_OLD_CYASSL
187 #ifdef _WIN32
188 /*
189  * Include user-controlled settings for windows from
190  * <wolfssl-root>/IDE/WIN/user_settings.h
191  */
192 #include <IDE/WIN/user_settings.h>
193 #include <cyassl/ctaocrypt/settings.h>
194 #else
195 #include <cyassl/options.h>
196 #endif
197 #include <cyassl/openssl/ssl.h>
198 #include <cyassl/error-ssl.h>
199 
200 #else
201 #ifdef _WIN32
202 /*
203  * Include user-controlled settings for windows from
204  * <wolfssl-root>/IDE/WIN/user_settings.h
205  */
206 #include <IDE/WIN/user_settings.h>
207 #include <wolfssl/wolfcrypt/settings.h>
208 #else
209 #include <wolfssl/options.h>
210 #endif
211 #include <wolfssl/openssl/ssl.h>
212 #include <wolfssl/error-ssl.h>
213 #endif /* not USE_OLD_CYASSL */
214 #else
215 #if defined(LWS_WITH_MBEDTLS)
216 #if defined(LWS_WITH_ESP32)
217 /* this filepath is passed to us but without quotes or <> */
218 #undef MBEDTLS_CONFIG_FILE
219 #define MBEDTLS_CONFIG_FILE <mbedtls/esp_config.h>
220 #endif
221 #include <mbedtls/ssl.h>
222 #else
223 #include <openssl/ssl.h>
224 #if !defined(LWS_WITH_MBEDTLS)
225 #include <openssl/err.h>
226 #endif
227 #endif
228 #endif /* not USE_WOLFSSL */
229 #endif
230 
231 /*
232  * Helpers for pthread mutex in user code... if lws is built for
233  * multiple service threads, these resolve to pthread mutex
234  * operations. In the case LWS_MAX_SMP is 1 (the default), they
235  * are all NOPs and no pthread type or api is referenced.
236  */
237 
238 #if LWS_MAX_SMP > 1
239 
240 #include <pthread.h>
241 
242 #define lws_pthread_mutex(name) pthread_mutex_t name;
243 
244 static LWS_INLINE void
245 lws_pthread_mutex_init(pthread_mutex_t *lock)
246 {
247  pthread_mutex_init(lock, NULL);
248 }
249 
250 static LWS_INLINE void
251 lws_pthread_mutex_destroy(pthread_mutex_t *lock)
252 {
253  pthread_mutex_destroy(lock);
254 }
255 
256 static LWS_INLINE void
257 lws_pthread_mutex_lock(pthread_mutex_t *lock)
258 {
259  pthread_mutex_lock(lock);
260 }
261 
262 static LWS_INLINE void
263 lws_pthread_mutex_unlock(pthread_mutex_t *lock)
264 {
265  pthread_mutex_unlock(lock);
266 }
267 
268 #else
269 #define lws_pthread_mutex(name)
270 #define lws_pthread_mutex_init(_a)
271 #define lws_pthread_mutex_destroy(_a)
272 #define lws_pthread_mutex_lock(_a)
273 #define lws_pthread_mutex_unlock(_a)
274 #endif
275 
276 
277 #define CONTEXT_PORT_NO_LISTEN -1
278 #define CONTEXT_PORT_NO_LISTEN_SERVER -2
279 
280 #include <libwebsockets/lws-logs.h>
281 
282 
283 #include <stddef.h>
284 
285 #ifndef lws_container_of
286 #define lws_container_of(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
287 #endif
288 
289 struct lws;
290 
291 typedef int64_t lws_usec_t;
292 
293 /* api change list for user code to test against */
294 
295 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG
296 
297 /* the struct lws_protocols has the id field present */
298 #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD
299 
300 /* you can call lws_get_peer_write_allowance */
301 #define LWS_FEATURE_PROTOCOLS_HAS_PEER_WRITE_ALLOWANCE
302 
303 /* extra parameter introduced in 917f43ab821 */
304 #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_LEN
305 
306 /* File operations stuff exists */
307 #define LWS_FEATURE_FOPS
308 
309 
310 #if defined(_WIN32)
311 #if !defined(LWS_WIN32_HANDLE_TYPES)
312 typedef SOCKET lws_sockfd_type;
313 typedef HANDLE lws_filefd_type;
314 #endif
315 
316 struct lws_pollfd {
317  lws_sockfd_type fd;
318  SHORT events;
319  SHORT revents;
320 };
321 #define LWS_POLLHUP (FD_CLOSE)
322 #define LWS_POLLIN (FD_READ | FD_ACCEPT)
323 #define LWS_POLLOUT (FD_WRITE)
324 #else
325 
326 
327 #if defined(LWS_WITH_ESP32)
328 #include <libwebsockets/lws-esp32.h>
329 #else
330 typedef int lws_sockfd_type;
331 typedef int lws_filefd_type;
332 #endif
333 
334 #define lws_pollfd pollfd
335 #define LWS_POLLHUP (POLLHUP|POLLERR)
336 #define LWS_POLLIN (POLLIN)
337 #define LWS_POLLOUT (POLLOUT)
338 #endif
339 
340 
341 #if (defined(WIN32) || defined(_WIN32)) && !defined(__MINGW32__)
342 /* ... */
343 #define ssize_t SSIZE_T
344 #endif
345 
346 #if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
347 #include <sys/types.h>
348 #include <sys/stat.h>
349 #endif
350 
351 #if defined(LWS_HAVE_STDINT_H)
352 #include <stdint.h>
353 #else
354 #if defined(WIN32) || defined(_WIN32)
355 /* !!! >:-[ */
356 typedef unsigned __int32 uint32_t;
357 typedef unsigned __int16 uint16_t;
358 typedef unsigned __int8 uint8_t;
359 #else
360 typedef unsigned int uint32_t;
361 typedef unsigned short uint16_t;
362 typedef unsigned char uint8_t;
363 #endif
364 #endif
365 
366 typedef unsigned long long lws_filepos_t;
367 typedef long long lws_fileofs_t;
368 typedef uint32_t lws_fop_flags_t;
369 
372 struct lws_pollargs {
373  lws_sockfd_type fd;
374  int events;
376 };
377 
378 struct lws_extension; /* needed even with ws exts disabled for create context */
379 struct lws_token_limits;
380 struct lws_context;
381 struct lws_tokens;
382 struct lws_vhost;
383 struct lws;
384 
385 #include <libwebsockets/lws-ws-close.h>
386 #include <libwebsockets/lws-callbacks.h>
387 #include <libwebsockets/lws-ws-state.h>
388 #include <libwebsockets/lws-ws-ext.h>
389 #include <libwebsockets/lws-protocols-plugins.h>
390 #include <libwebsockets/lws-plugin-generic-sessions.h>
391 #include <libwebsockets/lws-context-vhost.h>
392 #include <libwebsockets/lws-client.h>
393 #include <libwebsockets/lws-http.h>
394 #include <libwebsockets/lws-spa.h>
395 #include <libwebsockets/lws-purify.h>
396 #include <libwebsockets/lws-timeout-timer.h>
397 #include <libwebsockets/lws-service.h>
398 #include <libwebsockets/lws-write.h>
399 #include <libwebsockets/lws-writeable.h>
400 #include <libwebsockets/lws-adopt.h>
401 #include <libwebsockets/lws-network-helper.h>
402 #include <libwebsockets/lws-misc.h>
403 #include <libwebsockets/lws-ring.h>
404 #include <libwebsockets/lws-sha1-base64.h>
405 #include <libwebsockets/lws-x509.h>
406 #include <libwebsockets/lws-cgi.h>
407 #include <libwebsockets/lws-vfs.h>
408 #include <libwebsockets/lws-lejp.h>
409 #include <libwebsockets/lws-stats.h>
410 #include <libwebsockets/lws-threadpool.h>
411 #include <libwebsockets/lws-tokenize.h>
412 #include <libwebsockets/lws-lwsac.h>
413 #include <libwebsockets/lws-fts.h>
414 #include <libwebsockets/lws-diskcache.h>
415 
416 #if defined(LWS_WITH_TLS)
417 
418 #if defined(LWS_WITH_MBEDTLS)
419 #include <mbedtls/sha1.h>
420 #include <mbedtls/sha256.h>
421 #include <mbedtls/sha512.h>
422 #endif
423 
424 #include <libwebsockets/lws-genhash.h>
425 #include <libwebsockets/lws-genrsa.h>
426 #include <libwebsockets/lws-jwk.h>
427 #include <libwebsockets/lws-jws.h>
428 
429 #endif
430 
431 #ifdef __cplusplus
432 }
433 #endif
434 
435 #endif
lws_sockfd_type fd
Definition: libwebsockets.h:373
Definition: lws-ws-ext.h:138
Definition: libwebsockets.h:372
int prev_events
Definition: libwebsockets.h:375
Definition: lws-http.h:332
Definition: lws-http.h:203
int events
Definition: libwebsockets.h:374