messenger_via_http_server  1.0
mongoose.h
См. документацию.
1 #ifdef MG_MODULE_LINES
2 #line 1 "mongoose/src/common.h"
3 #endif
4 /*
5  * Copyright (c) 2004-2013 Sergey Lyubka
6  * Copyright (c) 2013-2015 Cesanta Software Limited
7  * All rights reserved
8  *
9  * This software is dual-licensed: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation. For the terms of this
12  * license, see <http://www.gnu.org/licenses/>.
13  *
14  * You are free to use this software under the terms of the GNU General
15  * Public License, but WITHOUT ANY WARRANTY; without even the implied
16  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  * See the GNU General Public License for more details.
18  *
19  * Alternatively, you can license this software under a commercial
20  * license, as set out in <https://www.cesanta.com/license>.
21  */
22 
23 #ifndef CS_MONGOOSE_SRC_COMMON_H_
24 #define CS_MONGOOSE_SRC_COMMON_H_
25 
26 #define MG_VERSION "6.7"
27 
28 /* Local tweaks, applied before any of Mongoose's own headers. */
29 #ifdef MG_LOCALS
30 #include <mg_locals.h>
31 #endif
32 
33 #endif /* CS_MONGOOSE_SRC_COMMON_H_ */
34 #ifdef MG_MODULE_LINES
35 #line 1 "common/platform.h"
36 #endif
37 #ifndef CS_COMMON_PLATFORM_H_
38 #define CS_COMMON_PLATFORM_H_
39 
40 /*
41  * For the "custom" platform, includes and dependencies can be
42  * provided through mg_locals.h.
43  */
44 #define CS_P_CUSTOM 0
45 #define CS_P_UNIX 1
46 #define CS_P_WINDOWS 2
47 #define CS_P_ESP32 15
48 #define CS_P_ESP8266 3
49 #define CS_P_CC3200 4
50 #define CS_P_MSP432 5
51 #define CS_P_CC3100 6
52 #define CS_P_TM4C129 14
53 #define CS_P_MBED 7
54 #define CS_P_WINCE 8
55 #define CS_P_NXP_LPC 13
56 #define CS_P_NXP_KINETIS 9
57 #define CS_P_NRF51 12
58 #define CS_P_NRF52 10
59 #define CS_P_PIC32 11
60 #define CS_P_STM32 16
61 /* Next id: 17 */
62 
63 /* If not specified explicitly, we guess platform by defines. */
64 #ifndef CS_PLATFORM
65 
66 #if defined(TARGET_IS_MSP432P4XX) || defined(__MSP432P401R__)
67 #define CS_PLATFORM CS_P_MSP432
68 #elif defined(cc3200)
69 #define CS_PLATFORM CS_P_CC3200
70 #elif defined(__unix__) || defined(__APPLE__)
71 #define CS_PLATFORM CS_P_UNIX
72 #elif defined(WINCE)
73 #define CS_PLATFORM CS_P_WINCE
74 #elif defined(_WIN32)
75 #define CS_PLATFORM CS_P_WINDOWS
76 #elif defined(__MBED__)
77 #define CS_PLATFORM CS_P_MBED
78 #elif defined(__USE_LPCOPEN)
79 #define CS_PLATFORM CS_P_NXP_LPC
80 #elif defined(FRDM_K64F) || defined(FREEDOM)
81 #define CS_PLATFORM CS_P_NXP_KINETIS
82 #elif defined(PIC32)
83 #define CS_PLATFORM CS_P_PIC32
84 #elif defined(ESP_PLATFORM)
85 #define CS_PLATFORM CS_P_ESP32
86 #elif defined(ICACHE_FLASH)
87 #define CS_PLATFORM CS_P_ESP8266
88 #elif defined(TARGET_IS_TM4C129_RA0) || defined(TARGET_IS_TM4C129_RA1) || \
89  defined(TARGET_IS_TM4C129_RA2)
90 #define CS_PLATFORM CS_P_TM4C129
91 #elif defined(STM32)
92 #define CS_PLATFORM CS_P_STM32
93 #endif
94 
95 #ifndef CS_PLATFORM
96 #error "CS_PLATFORM is not specified and we couldn't guess it."
97 #endif
98 
99 #endif /* !defined(CS_PLATFORM) */
100 
101 #define MG_NET_IF_SOCKET 1
102 #define MG_NET_IF_SIMPLELINK 2
103 #define MG_NET_IF_LWIP_LOW_LEVEL 3
104 #define MG_NET_IF_PIC32 4
105 
106 #define MG_SSL_IF_OPENSSL 1
107 #define MG_SSL_IF_MBEDTLS 2
108 #define MG_SSL_IF_SIMPLELINK 3
109 
110 /* Amalgamated: #include "common/platforms/platform_unix.h" */
111 /* Amalgamated: #include "common/platforms/platform_windows.h" */
112 /* Amalgamated: #include "common/platforms/platform_esp32.h" */
113 /* Amalgamated: #include "common/platforms/platform_esp8266.h" */
114 /* Amalgamated: #include "common/platforms/platform_cc3200.h" */
115 /* Amalgamated: #include "common/platforms/platform_cc3100.h" */
116 /* Amalgamated: #include "common/platforms/platform_mbed.h" */
117 /* Amalgamated: #include "common/platforms/platform_nrf51.h" */
118 /* Amalgamated: #include "common/platforms/platform_nrf52.h" */
119 /* Amalgamated: #include "common/platforms/platform_wince.h" */
120 /* Amalgamated: #include "common/platforms/platform_nxp_lpc.h" */
121 /* Amalgamated: #include "common/platforms/platform_nxp_kinetis.h" */
122 /* Amalgamated: #include "common/platforms/platform_pic32.h" */
123 /* Amalgamated: #include "common/platforms/platform_stm32.h" */
124 
125 /* Common stuff */
126 
127 #if !defined(WEAK)
128 #if (defined(__GNUC__) || defined(__TI_COMPILER_VERSION__)) && !defined(_WIN32)
129 #define WEAK __attribute__((weak))
130 #else
131 #define WEAK
132 #endif
133 #endif
134 
135 #ifdef __GNUC__
136 #define NORETURN __attribute__((noreturn))
137 #define NOINLINE __attribute__((noinline))
138 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
139 #define NOINSTR __attribute__((no_instrument_function))
140 #define DO_NOT_WARN_UNUSED __attribute__((unused))
141 #else
142 #define NORETURN
143 #define NOINLINE
144 #define WARN_UNUSED_RESULT
145 #define NOINSTR
146 #define DO_NOT_WARN_UNUSED
147 #endif /* __GNUC__ */
148 
149 #ifndef ARRAY_SIZE
150 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
151 #endif
152 
153 #endif /* CS_COMMON_PLATFORM_H_ */
154 #ifdef MG_MODULE_LINES
155 #line 1 "common/platforms/platform_windows.h"
156 #endif
157 #ifndef CS_COMMON_PLATFORMS_PLATFORM_WINDOWS_H_
158 #define CS_COMMON_PLATFORMS_PLATFORM_WINDOWS_H_
159 #if CS_PLATFORM == CS_P_WINDOWS
160 
161 /*
162  * MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015)
163  * MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013)
164  * MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012)
165  * MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010)
166  * MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008)
167  * MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005)
168  * MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio 2003)
169  * MSVC++ 7.0 _MSC_VER == 1300
170  * MSVC++ 6.0 _MSC_VER == 1200
171  * MSVC++ 5.0 _MSC_VER == 1100
172  */
173 #ifdef _MSC_VER
174 #pragma warning(disable : 4127) /* FD_SET() emits warning, disable it */
175 #pragma warning(disable : 4204) /* missing c99 support */
176 #endif
177 
178 #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
179 #define _WINSOCK_DEPRECATED_NO_WARNINGS 1
180 #endif
181 
182 #ifndef _CRT_SECURE_NO_WARNINGS
183 #define _CRT_SECURE_NO_WARNINGS
184 #endif
185 
186 #include <assert.h>
187 #include <direct.h>
188 #include <errno.h>
189 #include <fcntl.h>
190 #include <io.h>
191 #include <limits.h>
192 #include <signal.h>
193 #include <stddef.h>
194 #include <stdio.h>
195 #include <stdlib.h>
196 #include <sys/stat.h>
197 #include <time.h>
198 
199 #ifdef _MSC_VER
200 #pragma comment(lib, "ws2_32.lib") /* Linking with winsock library */
201 #endif
202 
203 #include <winsock2.h>
204 #include <ws2tcpip.h>
205 #include <windows.h>
206 #include <process.h>
207 
208 #if defined(_MSC_VER) && _MSC_VER >= 1800
209 #define strdup _strdup
210 #endif
211 
212 #ifndef EINPROGRESS
213 #define EINPROGRESS WSAEINPROGRESS
214 #endif
215 #ifndef EWOULDBLOCK
216 #define EWOULDBLOCK WSAEWOULDBLOCK
217 #endif
218 #ifndef __func__
219 #define STRX(x) #x
220 #define STR(x) STRX(x)
221 #define __func__ __FILE__ ":" STR(__LINE__)
222 #endif
223 #define snprintf _snprintf
224 #define fileno _fileno
225 #define vsnprintf _vsnprintf
226 #define sleep(x) Sleep((x) *1000)
227 #define to64(x) _atoi64(x)
228 #if !defined(__MINGW32__) && !defined(__MINGW64__)
229 #define popen(x, y) _popen((x), (y))
230 #define pclose(x) _pclose(x)
231 #endif
232 #define rmdir _rmdir
233 #if defined(_MSC_VER) && _MSC_VER >= 1400
234 #define fseeko(x, y, z) _fseeki64((x), (y), (z))
235 #else
236 #define fseeko(x, y, z) fseek((x), (y), (z))
237 #endif
238 #if defined(_MSC_VER) && _MSC_VER <= 1200
239 typedef unsigned long uintptr_t;
240 typedef long intptr_t;
241 #endif
242 typedef int socklen_t;
243 #if _MSC_VER >= 1700
244 #include <stdint.h>
245 #else
246 typedef signed char int8_t;
247 typedef unsigned char uint8_t;
248 typedef int int32_t;
249 typedef unsigned int uint32_t;
250 typedef short int16_t;
251 typedef unsigned short uint16_t;
252 typedef __int64 int64_t;
253 typedef unsigned __int64 uint64_t;
254 #endif
255 typedef SOCKET sock_t;
256 typedef uint32_t in_addr_t;
257 #ifndef UINT16_MAX
258 #define UINT16_MAX 65535
259 #endif
260 #ifndef UINT32_MAX
261 #define UINT32_MAX 4294967295
262 #endif
263 #ifndef pid_t
264 #define pid_t HANDLE
265 #endif
266 #define INT64_FMT "I64d"
267 #define INT64_X_FMT "I64x"
268 #define SIZE_T_FMT "Iu"
269 typedef struct _stati64 cs_stat_t;
270 #ifndef S_ISDIR
271 #define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR)
272 #endif
273 #ifndef S_ISREG
274 #define S_ISREG(x) (((x) &_S_IFMT) == _S_IFREG)
275 #endif
276 #define DIRSEP '\\'
277 #define CS_DEFINE_DIRENT
278 
279 #ifndef va_copy
280 #ifdef __va_copy
281 #define va_copy __va_copy
282 #else
283 #define va_copy(x, y) (x) = (y)
284 #endif
285 #endif
286 
287 #ifndef MG_MAX_HTTP_REQUEST_SIZE
288 #define MG_MAX_HTTP_REQUEST_SIZE 8192
289 #endif
290 
291 #ifndef MG_MAX_HTTP_SEND_MBUF
292 #define MG_MAX_HTTP_SEND_MBUF 4096
293 #endif
294 
295 #ifndef MG_MAX_HTTP_HEADERS
296 #define MG_MAX_HTTP_HEADERS 40
297 #endif
298 
299 #ifndef CS_ENABLE_STDIO
300 #define CS_ENABLE_STDIO 1
301 #endif
302 
303 #ifndef MG_ENABLE_BROADCAST
304 #define MG_ENABLE_BROADCAST 1
305 #endif
306 
307 #ifndef MG_ENABLE_DIRECTORY_LISTING
308 #define MG_ENABLE_DIRECTORY_LISTING 1
309 #endif
310 
311 #ifndef MG_ENABLE_FILESYSTEM
312 #define MG_ENABLE_FILESYSTEM 1
313 #endif
314 
315 #ifndef MG_ENABLE_HTTP_CGI
316 #define MG_ENABLE_HTTP_CGI MG_ENABLE_FILESYSTEM
317 #endif
318 
319 #ifndef MG_NET_IF
320 #define MG_NET_IF MG_NET_IF_SOCKET
321 #endif
322 
323 #endif /* CS_PLATFORM == CS_P_WINDOWS */
324 #endif /* CS_COMMON_PLATFORMS_PLATFORM_WINDOWS_H_ */
325 #ifdef MG_MODULE_LINES
326 #line 1 "common/platforms/platform_unix.h"
327 #endif
328 #ifndef CS_COMMON_PLATFORMS_PLATFORM_UNIX_H_
329 #define CS_COMMON_PLATFORMS_PLATFORM_UNIX_H_
330 #if CS_PLATFORM == CS_P_UNIX
331 
332 #ifndef _XOPEN_SOURCE
333 #define _XOPEN_SOURCE 600
334 #endif
335 
336 /* <inttypes.h> wants this for C++ */
337 #ifndef __STDC_FORMAT_MACROS
338 #define __STDC_FORMAT_MACROS
339 #endif
340 
341 /* C++ wants that for INT64_MAX */
342 #ifndef __STDC_LIMIT_MACROS
343 #define __STDC_LIMIT_MACROS
344 #endif
345 
346 /* Enable fseeko() and ftello() functions */
347 #ifndef _LARGEFILE_SOURCE
348 #define _LARGEFILE_SOURCE
349 #endif
350 
351 /* Enable 64-bit file offsets */
352 #ifndef _FILE_OFFSET_BITS
353 #define _FILE_OFFSET_BITS 64
354 #endif
355 
356 #include <arpa/inet.h>
357 #include <assert.h>
358 #include <ctype.h>
359 #include <dirent.h>
360 #include <errno.h>
361 #include <fcntl.h>
362 #include <inttypes.h>
363 #include <stdint.h>
364 #include <limits.h>
365 #include <math.h>
366 #include <netdb.h>
367 #include <netinet/in.h>
368 #include <pthread.h>
369 #include <signal.h>
370 #include <stdarg.h>
371 #include <stdio.h>
372 #include <stdlib.h>
373 #include <string.h>
374 #include <sys/param.h>
375 #include <sys/socket.h>
376 #include <sys/select.h>
377 #include <sys/stat.h>
378 #include <sys/time.h>
379 #include <sys/types.h>
380 #include <unistd.h>
381 
382 #ifdef __APPLE__
383 #include <machine/endian.h>
384 #ifndef BYTE_ORDER
385 #define LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
386 #define BIG_ENDIAN __DARWIN_BIG_ENDIAN
387 #define PDP_ENDIAN __DARWIN_PDP_ENDIAN
388 #define BYTE_ORDER __DARWIN_BYTE_ORDER
389 #endif
390 #endif
391 
392 /*
393  * osx correctly avoids defining strtoll when compiling in strict ansi mode.
394  * c++ 11 standard defines strtoll as well.
395  * We require strtoll, and if your embedded pre-c99 compiler lacks one, please
396  * implement a shim.
397  */
398 #if !(defined(__cplusplus) && __cplusplus >= 201103L) && \
399  !(defined(__DARWIN_C_LEVEL) && __DARWIN_C_LEVEL >= 200809L)
400 long long strtoll(const char *, char **, int);
401 #endif
402 
403 typedef int sock_t;
404 #define INVALID_SOCKET (-1)
405 #define SIZE_T_FMT "zu"
406 typedef struct stat cs_stat_t;
407 #define DIRSEP '/'
408 #define to64(x) strtoll(x, NULL, 10)
409 #define INT64_FMT PRId64
410 #define INT64_X_FMT PRIx64
411 
412 #ifndef __cdecl
413 #define __cdecl
414 #endif
415 
416 #ifndef va_copy
417 #ifdef __va_copy
418 #define va_copy __va_copy
419 #else
420 #define va_copy(x, y) (x) = (y)
421 #endif
422 #endif
423 
424 #define closesocket(x) close(x)
425 
426 #ifndef MG_MAX_HTTP_REQUEST_SIZE
427 #define MG_MAX_HTTP_REQUEST_SIZE 8192
428 #endif
429 
430 #ifndef MG_MAX_HTTP_SEND_MBUF
431 #define MG_MAX_HTTP_SEND_MBUF 4096
432 #endif
433 
434 #ifndef MG_MAX_HTTP_HEADERS
435 #define MG_MAX_HTTP_HEADERS 40
436 #endif
437 
438 #ifndef CS_ENABLE_STDIO
439 #define CS_ENABLE_STDIO 1
440 #endif
441 
442 #ifndef MG_ENABLE_BROADCAST
443 #define MG_ENABLE_BROADCAST 1
444 #endif
445 
446 #ifndef MG_ENABLE_DIRECTORY_LISTING
447 #define MG_ENABLE_DIRECTORY_LISTING 1
448 #endif
449 
450 #ifndef MG_ENABLE_FILESYSTEM
451 #define MG_ENABLE_FILESYSTEM 1
452 #endif
453 
454 #ifndef MG_ENABLE_HTTP_CGI
455 #define MG_ENABLE_HTTP_CGI MG_ENABLE_FILESYSTEM
456 #endif
457 
458 #ifndef MG_NET_IF
459 #define MG_NET_IF MG_NET_IF_SOCKET
460 #endif
461 
462 #endif /* CS_PLATFORM == CS_P_UNIX */
463 #endif /* CS_COMMON_PLATFORMS_PLATFORM_UNIX_H_ */
464 #ifdef MG_MODULE_LINES
465 #line 1 "common/platforms/platform_esp32.h"
466 #endif
467 /*
468  * Copyright (c) 2014-2016 Cesanta Software Limited
469  * All rights reserved
470  */
471 
472 #ifndef CS_COMMON_PLATFORMS_PLATFORM_ESP32_H_
473 #define CS_COMMON_PLATFORMS_PLATFORM_ESP32_H_
474 #if CS_PLATFORM == CS_P_ESP32
475 
476 #include <assert.h>
477 #include <ctype.h>
478 #include <dirent.h>
479 #include <fcntl.h>
480 #include <inttypes.h>
481 #include <machine/endian.h>
482 #include <stdint.h>
483 #include <string.h>
484 #include <sys/stat.h>
485 #include <sys/time.h>
486 
487 #define SIZE_T_FMT "u"
488 typedef struct stat cs_stat_t;
489 #define DIRSEP '/'
490 #define to64(x) strtoll(x, NULL, 10)
491 #define INT64_FMT PRId64
492 #define INT64_X_FMT PRIx64
493 #define __cdecl
494 #define _FILE_OFFSET_BITS 32
495 
496 #define MG_LWIP 1
497 
498 #ifndef MG_NET_IF
499 #define MG_NET_IF MG_NET_IF_SOCKET
500 #endif
501 
502 #ifndef CS_ENABLE_STDIO
503 #define CS_ENABLE_STDIO 1
504 #endif
505 
506 #endif /* CS_PLATFORM == CS_P_ESP32 */
507 #endif /* CS_COMMON_PLATFORMS_PLATFORM_ESP32_H_ */
508 #ifdef MG_MODULE_LINES
509 #line 1 "common/platforms/platform_esp8266.h"
510 #endif
511 /*
512  * Copyright (c) 2014-2016 Cesanta Software Limited
513  * All rights reserved
514  */
515 
516 #ifndef CS_COMMON_PLATFORMS_PLATFORM_ESP8266_H_
517 #define CS_COMMON_PLATFORMS_PLATFORM_ESP8266_H_
518 #if CS_PLATFORM == CS_P_ESP8266
519 
520 #include <assert.h>
521 #include <ctype.h>
522 #include <fcntl.h>
523 #include <inttypes.h>
524 #include <machine/endian.h>
525 #include <string.h>
526 #include <sys/stat.h>
527 #include <sys/time.h>
528 
529 #define SIZE_T_FMT "u"
530 typedef struct stat cs_stat_t;
531 #define DIRSEP '/'
532 #define CS_DEFINE_DIRENT
533 
534 #define to64(x) strtoll(x, NULL, 10)
535 #define INT64_FMT PRId64
536 #define INT64_X_FMT PRIx64
537 #define __cdecl
538 #define _FILE_OFFSET_BITS 32
539 
540 #ifndef RTOS_SDK
541 #define fileno(x) -1
542 #endif
543 
544 #define MG_LWIP 1
545 
546 /* struct timeval is defined in sys/time.h. */
547 #define LWIP_TIMEVAL_PRIVATE 0
548 
549 #ifndef MG_NET_IF
550 #include <lwip/opt.h>
551 #if LWIP_SOCKET /* RTOS SDK has LWIP sockets */
552 # define MG_NET_IF MG_NET_IF_SOCKET
553 #else
554 # define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
555 #endif
556 #endif
557 
558 #ifndef CS_ENABLE_STDIO
559 #define CS_ENABLE_STDIO 1
560 #endif
561 
562 #endif /* CS_PLATFORM == CS_P_ESP8266 */
563 #endif /* CS_COMMON_PLATFORMS_PLATFORM_ESP8266_H_ */
564 #ifdef MG_MODULE_LINES
565 #line 1 "common/platforms/platform_cc3100.h"
566 #endif
567 /*
568  * Copyright (c) 2014-2016 Cesanta Software Limited
569  * All rights reserved
570  */
571 
572 #ifndef CS_COMMON_PLATFORMS_PLATFORM_CC3100_H_
573 #define CS_COMMON_PLATFORMS_PLATFORM_CC3100_H_
574 #if CS_PLATFORM == CS_P_CC3100
575 
576 #include <assert.h>
577 #include <ctype.h>
578 #include <errno.h>
579 #include <inttypes.h>
580 #include <stdint.h>
581 #include <string.h>
582 #include <time.h>
583 
584 #define MG_NET_IF MG_NET_IF_SIMPLELINK
585 #define MG_SSL_IF MG_SSL_IF_SIMPLELINK
586 
587 /*
588  * CC3100 SDK and STM32 SDK include headers w/out path, just like
589  * #include "simplelink.h". As result, we have to add all required directories
590  * into Makefile IPATH and do the same thing (include w/out path)
591  */
592 
593 #include <simplelink.h>
594 #include <netapp.h>
595 #undef timeval
596 
597 typedef int sock_t;
598 #define INVALID_SOCKET (-1)
599 
600 #define to64(x) strtoll(x, NULL, 10)
601 #define INT64_FMT PRId64
602 #define INT64_X_FMT PRIx64
603 #define SIZE_T_FMT "u"
604 
605 #define SOMAXCONN 8
606 
607 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
608 char *inet_ntoa(struct in_addr in);
609 int inet_pton(int af, const char *src, void *dst);
610 
611 #endif /* CS_PLATFORM == CS_P_CC3100 */
612 #endif /* CS_COMMON_PLATFORMS_PLATFORM_CC3100_H_ */
613 #ifdef MG_MODULE_LINES
614 #line 1 "common/platforms/platform_cc3200.h"
615 #endif
616 /*
617  * Copyright (c) 2014-2016 Cesanta Software Limited
618  * All rights reserved
619  */
620 
621 #ifndef CS_COMMON_PLATFORMS_PLATFORM_CC3200_H_
622 #define CS_COMMON_PLATFORMS_PLATFORM_CC3200_H_
623 #if CS_PLATFORM == CS_P_CC3200
624 
625 #include <assert.h>
626 #include <ctype.h>
627 #include <errno.h>
628 #include <inttypes.h>
629 #include <stdint.h>
630 #include <string.h>
631 #include <time.h>
632 
633 #ifndef __TI_COMPILER_VERSION__
634 #include <fcntl.h>
635 #include <sys/time.h>
636 #endif
637 
638 #define MG_NET_IF MG_NET_IF_SIMPLELINK
639 #define MG_SSL_IF MG_SSL_IF_SIMPLELINK
640 
641 /* Only SPIFFS supports directories, SLFS does not. */
642 #if defined(CC3200_FS_SPIFFS) && !defined(MG_ENABLE_DIRECTORY_LISTING)
643 #define MG_ENABLE_DIRECTORY_LISTING 1
644 #endif
645 
646 /* Amalgamated: #include "common/platforms/simplelink/cs_simplelink.h" */
647 
648 typedef int sock_t;
649 #define INVALID_SOCKET (-1)
650 #define SIZE_T_FMT "u"
651 typedef struct stat cs_stat_t;
652 #define DIRSEP '/'
653 #define to64(x) strtoll(x, NULL, 10)
654 #define INT64_FMT PRId64
655 #define INT64_X_FMT PRIx64
656 #define __cdecl
657 
658 #define fileno(x) -1
659 
660 /* Some functions we implement for Mongoose. */
661 
662 #ifdef __cplusplus
663 extern "C" {
664 #endif
665 
666 #ifdef __TI_COMPILER_VERSION__
667 struct SlTimeval_t;
668 #define timeval SlTimeval_t
669 int gettimeofday(struct timeval *t, void *tz);
670 
671 int asprintf(char **strp, const char *fmt, ...);
672 
673 #endif
674 
675 /* TI's libc does not have stat & friends, add them. */
676 #ifdef __TI_COMPILER_VERSION__
677 
678 #include <file.h>
679 
680 typedef unsigned int mode_t;
681 typedef size_t _off_t;
682 typedef long ssize_t;
683 
684 struct stat {
685  int st_ino;
686  mode_t st_mode;
687  int st_nlink;
688  time_t st_mtime;
689  off_t st_size;
690 };
691 
692 int _stat(const char *pathname, struct stat *st);
693 #define stat(a, b) _stat(a, b)
694 
695 #define __S_IFMT 0170000
696 
697 #define __S_IFDIR 0040000
698 #define __S_IFCHR 0020000
699 #define __S_IFREG 0100000
700 
701 #define __S_ISTYPE(mode, mask) (((mode) &__S_IFMT) == (mask))
702 
703 #define S_IFDIR __S_IFDIR
704 #define S_IFCHR __S_IFCHR
705 #define S_IFREG __S_IFREG
706 #define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR)
707 #define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG)
708 
709 /* As of 5.2.7, TI compiler does not support va_copy() yet. */
710 #define va_copy(apc, ap) ((apc) = (ap))
711 
712 #endif /* __TI_COMPILER_VERSION__ */
713 
714 #ifdef CC3200_FS_SLFS
715 #define MG_FS_SLFS
716 #endif
717 
718 #if (defined(CC3200_FS_SPIFFS) || defined(CC3200_FS_SLFS)) && \
719  !defined(MG_ENABLE_FILESYSTEM)
720 #define MG_ENABLE_FILESYSTEM 1
721 #define CS_DEFINE_DIRENT
722 #endif
723 
724 #ifndef CS_ENABLE_STDIO
725 #define CS_ENABLE_STDIO 1
726 #endif
727 
728 #ifdef __cplusplus
729 }
730 #endif
731 
732 #endif /* CS_PLATFORM == CS_P_CC3200 */
733 #endif /* CS_COMMON_PLATFORMS_PLATFORM_CC3200_H_ */
734 #ifdef MG_MODULE_LINES
735 #line 1 "common/platforms/platform_msp432.h"
736 #endif
737 /*
738  * Copyright (c) 2014-2016 Cesanta Software Limited
739  * All rights reserved
740  */
741 
742 #ifndef CS_COMMON_PLATFORMS_PLATFORM_MSP432_H_
743 #define CS_COMMON_PLATFORMS_PLATFORM_MSP432_H_
744 #if CS_PLATFORM == CS_P_MSP432
745 
746 #include <assert.h>
747 #include <ctype.h>
748 #include <errno.h>
749 #include <inttypes.h>
750 #include <stdint.h>
751 #include <string.h>
752 #include <time.h>
753 
754 #ifndef __TI_COMPILER_VERSION__
755 #include <fcntl.h>
756 #include <sys/time.h>
757 #endif
758 
759 #define MG_NET_IF MG_NET_IF_SIMPLELINK
760 #define MG_SSL_IF MG_SSL_IF_SIMPLELINK
761 
762 /* Amalgamated: #include "common/platforms/simplelink/cs_simplelink.h" */
763 
764 typedef int sock_t;
765 #define INVALID_SOCKET (-1)
766 #define SIZE_T_FMT "u"
767 typedef struct stat cs_stat_t;
768 #define DIRSEP '/'
769 #define to64(x) strtoll(x, NULL, 10)
770 #define INT64_FMT PRId64
771 #define INT64_X_FMT PRIx64
772 #define __cdecl
773 
774 #define fileno(x) -1
775 
776 /* Some functions we implement for Mongoose. */
777 
778 #ifdef __cplusplus
779 extern "C" {
780 #endif
781 
782 #ifdef __TI_COMPILER_VERSION__
783 struct SlTimeval_t;
784 #define timeval SlTimeval_t
785 int gettimeofday(struct timeval *t, void *tz);
786 #endif
787 
788 /* TI's libc does not have stat & friends, add them. */
789 #ifdef __TI_COMPILER_VERSION__
790 
791 #include <file.h>
792 
793 typedef unsigned int mode_t;
794 typedef size_t _off_t;
795 typedef long ssize_t;
796 
797 struct stat {
798  int st_ino;
799  mode_t st_mode;
800  int st_nlink;
801  time_t st_mtime;
802  off_t st_size;
803 };
804 
805 int _stat(const char *pathname, struct stat *st);
806 #define stat(a, b) _stat(a, b)
807 
808 #define __S_IFMT 0170000
809 
810 #define __S_IFDIR 0040000
811 #define __S_IFCHR 0020000
812 #define __S_IFREG 0100000
813 
814 #define __S_ISTYPE(mode, mask) (((mode) &__S_IFMT) == (mask))
815 
816 #define S_IFDIR __S_IFDIR
817 #define S_IFCHR __S_IFCHR
818 #define S_IFREG __S_IFREG
819 #define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR)
820 #define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG)
821 
822 /* As of 5.2.7, TI compiler does not support va_copy() yet. */
823 #define va_copy(apc, ap) ((apc) = (ap))
824 
825 #endif /* __TI_COMPILER_VERSION__ */
826 
827 #ifndef CS_ENABLE_STDIO
828 #define CS_ENABLE_STDIO 1
829 #endif
830 
831 #if (defined(CC3200_FS_SPIFFS) || defined(CC3200_FS_SLFS)) && !defined(MG_ENABLE_FILESYSTEM)
832 #define MG_ENABLE_FILESYSTEM 1
833 #endif
834 
835 #ifdef __cplusplus
836 }
837 #endif
838 
839 #endif /* CS_PLATFORM == CS_P_MSP432 */
840 #endif /* CS_COMMON_PLATFORMS_PLATFORM_MSP432_H_ */
841 #ifdef MG_MODULE_LINES
842 #line 1 "common/platforms/platform_tm4c129.h"
843 #endif
844 /*
845  * Copyright (c) 2014-2016 Cesanta Software Limited
846  * All rights reserved
847  */
848 
849 #ifndef CS_COMMON_PLATFORMS_PLATFORM_TM4C129_H_
850 #define CS_COMMON_PLATFORMS_PLATFORM_TM4C129_H_
851 #if CS_PLATFORM == CS_P_TM4C129
852 
853 #include <assert.h>
854 #include <ctype.h>
855 #include <errno.h>
856 #include <inttypes.h>
857 #include <stdint.h>
858 #include <string.h>
859 #include <time.h>
860 
861 #ifndef __TI_COMPILER_VERSION__
862 #include <fcntl.h>
863 #include <sys/time.h>
864 #endif
865 
866 #define SIZE_T_FMT "u"
867 typedef struct stat cs_stat_t;
868 #define DIRSEP '/'
869 #define to64(x) strtoll(x, NULL, 10)
870 #define INT64_FMT PRId64
871 #define INT64_X_FMT PRIx64
872 #define __cdecl
873 
874 #ifndef MG_NET_IF
875 # include <lwip/opt.h>
876 # if LWIP_SOCKET
877 # define MG_NET_IF MG_NET_IF_SOCKET
878 # else
879 # define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
880 # endif
881 # define MG_LWIP 1
882 #elif MG_NET_IF == MG_NET_IF_SIMPLELINK
883 # include "common/platforms/simplelink/cs_simplelink.h"
884 #endif
885 
886 #ifndef CS_ENABLE_STDIO
887 #define CS_ENABLE_STDIO 1
888 #endif
889 
890 #ifdef __TI_COMPILER_VERSION__
891 /* As of 5.2.8, TI compiler does not support va_copy() yet. */
892 #define va_copy(apc, ap) ((apc) = (ap))
893 #endif /* __TI_COMPILER_VERSION__ */
894 
895 #ifdef __cplusplus
896 }
897 #endif
898 
899 #endif /* CS_PLATFORM == CS_P_TM4C129 */
900 #endif /* CS_COMMON_PLATFORMS_PLATFORM_TM4C129_H_ */
901 #ifdef MG_MODULE_LINES
902 #line 1 "common/platforms/platform_mbed.h"
903 #endif
904 /*
905  * Copyright (c) 2014-2016 Cesanta Software Limited
906  * All rights reserved
907  */
908 
909 #ifndef CS_COMMON_PLATFORMS_PLATFORM_MBED_H_
910 #define CS_COMMON_PLATFORMS_PLATFORM_MBED_H_
911 #if CS_PLATFORM == CS_P_MBED
912 
913 /*
914  * mbed.h contains C++ code (e.g. templates), thus, it should be processed
915  * only if included directly to startup file (ex: main.cpp)
916  */
917 #ifdef __cplusplus
918 /* Amalgamated: #include "mbed.h" */
919 #endif /* __cplusplus */
920 
921 #include <assert.h>
922 #include <ctype.h>
923 #include <errno.h>
924 #include <inttypes.h>
925 #include <stdint.h>
926 #include <string.h>
927 #include <time.h>
928 #include <sys/stat.h>
929 #include <sys/types.h>
930 #include <fcntl.h>
931 #include <stdio.h>
932 
933 typedef struct stat cs_stat_t;
934 #define DIRSEP '/'
935 
936 #ifndef CS_ENABLE_STDIO
937 #define CS_ENABLE_STDIO 1
938 #endif
939 
940 /*
941  * mbed can be compiled with the ARM compiler which
942  * just doesn't come with a gettimeofday shim
943  * because it's a BSD API and ARM targets embedded
944  * non-unix platforms.
945  */
946 #if defined(__ARMCC_VERSION) || defined(__ICCARM__)
947 #define _TIMEVAL_DEFINED
948 #define gettimeofday _gettimeofday
949 
950 /* copied from GCC on ARM; for some reason useconds are signed */
951 typedef long suseconds_t; /* microseconds (signed) */
952 struct timeval {
953  time_t tv_sec; /* seconds */
954  suseconds_t tv_usec; /* and microseconds */
955 };
956 
957 #endif
958 
959 #if MG_NET_IF == MG_NET_IF_SIMPLELINK
960 
961 #define MG_SIMPLELINK_NO_OSI 1
962 
963 #include <simplelink.h>
964 
965 typedef int sock_t;
966 #define INVALID_SOCKET (-1)
967 
968 #define to64(x) strtoll(x, NULL, 10)
969 #define INT64_FMT PRId64
970 #define INT64_X_FMT PRIx64
971 #define SIZE_T_FMT "u"
972 
973 #define SOMAXCONN 8
974 
975 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
976 char *inet_ntoa(struct in_addr in);
977 int inet_pton(int af, const char *src, void *dst);
978 int inet_aton(const char *cp, struct in_addr *inp);
979 in_addr_t inet_addr(const char *cp);
980 
981 #endif /* MG_NET_IF == MG_NET_IF_SIMPLELINK */
982 
983 #endif /* CS_PLATFORM == CS_P_MBED */
984 #endif /* CS_COMMON_PLATFORMS_PLATFORM_MBED_H_ */
985 #ifdef MG_MODULE_LINES
986 #line 1 "common/platforms/platform_nrf51.h"
987 #endif
988 /*
989  * Copyright (c) 2014-2016 Cesanta Software Limited
990  * All rights reserved
991  */
992 #ifndef CS_COMMON_PLATFORMS_PLATFORM_NRF51_H_
993 #define CS_COMMON_PLATFORMS_PLATFORM_NRF51_H_
994 #if CS_PLATFORM == CS_P_NRF51
995 
996 #include <assert.h>
997 #include <ctype.h>
998 #include <inttypes.h>
999 #include <stdint.h>
1000 #include <string.h>
1001 #include <time.h>
1002 
1003 #define to64(x) strtoll(x, NULL, 10)
1004 
1005 #define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
1006 #define MG_LWIP 1
1007 #define MG_ENABLE_IPV6 1
1008 
1009 /*
1010  * For ARM C Compiler, make lwip to export `struct timeval`; for other
1011  * compilers, suppress it.
1012  */
1013 #if !defined(__ARMCC_VERSION)
1014 # define LWIP_TIMEVAL_PRIVATE 0
1015 #else
1016 struct timeval;
1017 int gettimeofday(struct timeval *tp, void *tzp);
1018 #endif
1019 
1020 #define INT64_FMT PRId64
1021 #define SIZE_T_FMT "u"
1022 
1023 /*
1024  * ARM C Compiler doesn't have strdup, so we provide it
1025  */
1026 #define CS_ENABLE_STRDUP defined(__ARMCC_VERSION)
1027 
1028 #endif /* CS_PLATFORM == CS_P_NRF51 */
1029 #endif /* CS_COMMON_PLATFORMS_PLATFORM_NRF51_H_ */
1030 #ifdef MG_MODULE_LINES
1031 #line 1 "common/platforms/platform_nrf52.h"
1032 #endif
1033 /*
1034  * Copyright (c) 2014-2016 Cesanta Software Limited
1035  * All rights reserved
1036  */
1037 #ifndef CS_COMMON_PLATFORMS_PLATFORM_NRF52_H_
1038 #define CS_COMMON_PLATFORMS_PLATFORM_NRF52_H_
1039 #if CS_PLATFORM == CS_P_NRF52
1040 
1041 #include <assert.h>
1042 #include <ctype.h>
1043 #include <errno.h>
1044 #include <inttypes.h>
1045 #include <stdint.h>
1046 #include <string.h>
1047 #include <time.h>
1048 
1049 #define to64(x) strtoll(x, NULL, 10)
1050 
1051 #define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
1052 #define MG_LWIP 1
1053 #define MG_ENABLE_IPV6 1
1054 
1055 #if !defined(ENOSPC)
1056 # define ENOSPC 28 /* No space left on device */
1057 #endif
1058 
1059 /*
1060  * For ARM C Compiler, make lwip to export `struct timeval`; for other
1061  * compilers, suppress it.
1062  */
1063 #if !defined(__ARMCC_VERSION)
1064 # define LWIP_TIMEVAL_PRIVATE 0
1065 #endif
1066 
1067 #define INT64_FMT PRId64
1068 #define SIZE_T_FMT "u"
1069 
1070 /*
1071  * ARM C Compiler doesn't have strdup, so we provide it
1072  */
1073 #define CS_ENABLE_STRDUP defined(__ARMCC_VERSION)
1074 
1075 #endif /* CS_PLATFORM == CS_P_NRF52 */
1076 #endif /* CS_COMMON_PLATFORMS_PLATFORM_NRF52_H_ */
1077 #ifdef MG_MODULE_LINES
1078 #line 1 "common/platforms/simplelink/cs_simplelink.h"
1079 #endif
1080 /*
1081  * Copyright (c) 2014-2016 Cesanta Software Limited
1082  * All rights reserved
1083  */
1084 
1085 #ifndef CS_COMMON_PLATFORMS_SIMPLELINK_CS_SIMPLELINK_H_
1086 #define CS_COMMON_PLATFORMS_SIMPLELINK_CS_SIMPLELINK_H_
1087 
1088 /* If simplelink.h is already included, all bets are off. */
1089 #if defined(MG_NET_IF) && MG_NET_IF == MG_NET_IF_SIMPLELINK && \
1090  !defined(__SIMPLELINK_H__)
1091 
1092 #include <stdbool.h>
1093 
1094 #ifndef __TI_COMPILER_VERSION__
1095 #undef __CONCAT
1096 #undef FD_CLR
1097 #undef FD_ISSET
1098 #undef FD_SET
1099 #undef FD_SETSIZE
1100 #undef FD_ZERO
1101 #undef fd_set
1102 #endif
1103 
1104 /* We want to disable SL_INC_STD_BSD_API_NAMING, so we include user.h ourselves
1105  * and undef it. */
1106 #define PROVISIONING_API_H_
1107 #include <simplelink/user.h>
1108 #undef PROVISIONING_API_H_
1109 #undef SL_INC_STD_BSD_API_NAMING
1110 
1111 #include <simplelink/include/simplelink.h>
1112 #include <simplelink/include/netapp.h>
1113 
1114 /* Now define only the subset of the BSD API that we use.
1115  * Notably, close(), read() and write() are not defined. */
1116 #define AF_INET SL_AF_INET
1117 
1118 #define socklen_t SlSocklen_t
1119 #define sockaddr SlSockAddr_t
1120 #define sockaddr_in SlSockAddrIn_t
1121 #define in_addr SlInAddr_t
1122 
1123 #define SOCK_STREAM SL_SOCK_STREAM
1124 #define SOCK_DGRAM SL_SOCK_DGRAM
1125 
1126 #define htonl sl_Htonl
1127 #define ntohl sl_Ntohl
1128 #define htons sl_Htons
1129 #define ntohs sl_Ntohs
1130 
1131 #ifndef EACCES
1132 #define EACCES SL_EACCES
1133 #endif
1134 #ifndef EAFNOSUPPORT
1135 #define EAFNOSUPPORT SL_EAFNOSUPPORT
1136 #endif
1137 #ifndef EAGAIN
1138 #define EAGAIN SL_EAGAIN
1139 #endif
1140 #ifndef EBADF
1141 #define EBADF SL_EBADF
1142 #endif
1143 #ifndef EINVAL
1144 #define EINVAL SL_EINVAL
1145 #endif
1146 #ifndef ENOMEM
1147 #define ENOMEM SL_ENOMEM
1148 #endif
1149 #ifndef EWOULDBLOCK
1150 #define EWOULDBLOCK SL_EWOULDBLOCK
1151 #endif
1152 
1153 #define SOMAXCONN 8
1154 
1155 #ifdef __cplusplus
1156 extern "C" {
1157 #endif
1158 
1159 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
1160 char *inet_ntoa(struct in_addr in);
1161 int inet_pton(int af, const char *src, void *dst);
1162 
1163 struct mg_mgr;
1164 struct mg_connection;
1165 
1166 typedef void (*mg_init_cb)(struct mg_mgr *mgr);
1167 bool mg_start_task(int priority, int stack_size, mg_init_cb mg_init);
1168 
1169 void mg_run_in_task(void (*cb)(struct mg_mgr *mgr, void *arg), void *cb_arg);
1170 
1171 int sl_fs_init(void);
1172 
1173 void sl_restart_cb(struct mg_mgr *mgr);
1174 
1175 int sl_set_ssl_opts(struct mg_connection *nc);
1176 
1177 #ifdef __cplusplus
1178 }
1179 #endif
1180 
1181 #endif /* MG_NET_IF == MG_NET_IF_SIMPLELINK && !defined(__SIMPLELINK_H__) */
1182 
1183 #endif /* CS_COMMON_PLATFORMS_SIMPLELINK_CS_SIMPLELINK_H_ */
1184 #ifdef MG_MODULE_LINES
1185 #line 1 "common/platforms/platform_wince.h"
1186 #endif
1187 #ifndef CS_COMMON_PLATFORMS_PLATFORM_WINCE_H_
1188 #define CS_COMMON_PLATFORMS_PLATFORM_WINCE_H_
1189 
1190 #if CS_PLATFORM == CS_P_WINCE
1191 
1192 /*
1193  * MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015)
1194  * MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013)
1195  * MSVC++ 11.0 _MSC_VER == 1700 (Visual Studio 2012)
1196  * MSVC++ 10.0 _MSC_VER == 1600 (Visual Studio 2010)
1197  * MSVC++ 9.0 _MSC_VER == 1500 (Visual Studio 2008)
1198  * MSVC++ 8.0 _MSC_VER == 1400 (Visual Studio 2005)
1199  * MSVC++ 7.1 _MSC_VER == 1310 (Visual Studio 2003)
1200  * MSVC++ 7.0 _MSC_VER == 1300
1201  * MSVC++ 6.0 _MSC_VER == 1200
1202  * MSVC++ 5.0 _MSC_VER == 1100
1203  */
1204 #pragma warning(disable : 4127) /* FD_SET() emits warning, disable it */
1205 #pragma warning(disable : 4204) /* missing c99 support */
1206 
1207 #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
1208 #define _WINSOCK_DEPRECATED_NO_WARNINGS 1
1209 #endif
1210 
1211 #ifndef _CRT_SECURE_NO_WARNINGS
1212 #define _CRT_SECURE_NO_WARNINGS
1213 #endif
1214 
1215 #include <assert.h>
1216 #include <limits.h>
1217 #include <stddef.h>
1218 #include <stdio.h>
1219 #include <stdlib.h>
1220 #include <time.h>
1221 
1222 #pragma comment(lib, "ws2.lib") /* Linking with WinCE winsock library */
1223 
1224 #include <winsock2.h>
1225 #include <ws2tcpip.h>
1226 #include <windows.h>
1227 
1228 #define strdup _strdup
1229 
1230 #ifndef EINPROGRESS
1231 #define EINPROGRESS WSAEINPROGRESS
1232 #endif
1233 
1234 #ifndef EWOULDBLOCK
1235 #define EWOULDBLOCK WSAEWOULDBLOCK
1236 #endif
1237 
1238 #ifndef __func__
1239 #define STRX(x) #x
1240 #define STR(x) STRX(x)
1241 #define __func__ __FILE__ ":" STR(__LINE__)
1242 #endif
1243 
1244 #define snprintf _snprintf
1245 #define fileno _fileno
1246 #define vsnprintf _vsnprintf
1247 #define sleep(x) Sleep((x) *1000)
1248 #define to64(x) _atoi64(x)
1249 #define rmdir _rmdir
1250 
1251 #if defined(_MSC_VER) && _MSC_VER >= 1400
1252 #define fseeko(x, y, z) _fseeki64((x), (y), (z))
1253 #else
1254 #define fseeko(x, y, z) fseek((x), (y), (z))
1255 #endif
1256 
1257 typedef int socklen_t;
1258 
1259 #if _MSC_VER >= 1700
1260 #include <stdint.h>
1261 #else
1262 typedef signed char int8_t;
1263 typedef unsigned char uint8_t;
1264 typedef int int32_t;
1265 typedef unsigned int uint32_t;
1266 typedef short int16_t;
1267 typedef unsigned short uint16_t;
1268 typedef __int64 int64_t;
1269 typedef unsigned __int64 uint64_t;
1270 #endif
1271 
1272 typedef SOCKET sock_t;
1273 typedef uint32_t in_addr_t;
1274 
1275 #ifndef UINT16_MAX
1276 #define UINT16_MAX 65535
1277 #endif
1278 
1279 #ifndef UINT32_MAX
1280 #define UINT32_MAX 4294967295
1281 #endif
1282 
1283 #ifndef pid_t
1284 #define pid_t HANDLE
1285 #endif
1286 
1287 #define INT64_FMT "I64d"
1288 #define INT64_X_FMT "I64x"
1289 /* TODO(alashkin): check if this is correct */
1290 #define SIZE_T_FMT "u"
1291 
1292 #define DIRSEP '\\'
1293 #define CS_DEFINE_DIRENT
1294 
1295 #ifndef va_copy
1296 #ifdef __va_copy
1297 #define va_copy __va_copy
1298 #else
1299 #define va_copy(x, y) (x) = (y)
1300 #endif
1301 #endif
1302 
1303 #ifndef MG_MAX_HTTP_REQUEST_SIZE
1304 #define MG_MAX_HTTP_REQUEST_SIZE 8192
1305 #endif
1306 
1307 #ifndef MG_MAX_HTTP_SEND_MBUF
1308 #define MG_MAX_HTTP_SEND_MBUF 4096
1309 #endif
1310 
1311 #ifndef MG_MAX_HTTP_HEADERS
1312 #define MG_MAX_HTTP_HEADERS 40
1313 #endif
1314 
1315 #ifndef CS_ENABLE_STDIO
1316 #define CS_ENABLE_STDIO 1
1317 #endif
1318 
1319 #define abort() DebugBreak();
1320 
1321 #ifndef BUFSIZ
1322 #define BUFSIZ 4096
1323 #endif
1324 /*
1325  * Explicitly disabling MG_ENABLE_THREADS for WinCE
1326  * because they are enabled for _WIN32 by default
1327  */
1328 #ifndef MG_ENABLE_THREADS
1329 #define MG_ENABLE_THREADS 0
1330 #endif
1331 
1332 #ifndef MG_ENABLE_FILESYSTEM
1333 #define MG_ENABLE_FILESYSTEM 1
1334 #endif
1335 
1336 #ifndef MG_NET_IF
1337 #define MG_NET_IF MG_NET_IF_SOCKET
1338 #endif
1339 
1340 typedef struct _stati64 {
1341  uint32_t st_mtime;
1342  uint32_t st_size;
1343  uint32_t st_mode;
1344 } cs_stat_t;
1345 
1346 /*
1347  * WinCE 6.0 has a lot of useful definitions in ATL (not windows.h) headers
1348  * use #ifdefs to avoid conflicts
1349  */
1350 
1351 #ifndef ENOENT
1352 #define ENOENT ERROR_PATH_NOT_FOUND
1353 #endif
1354 
1355 #ifndef EACCES
1356 #define EACCES ERROR_ACCESS_DENIED
1357 #endif
1358 
1359 #ifndef ENOMEM
1360 #define ENOMEM ERROR_NOT_ENOUGH_MEMORY
1361 #endif
1362 
1363 #ifndef _UINTPTR_T_DEFINED
1364 typedef unsigned int* uintptr_t;
1365 #endif
1366 
1367 #define _S_IFREG 2
1368 #define _S_IFDIR 4
1369 
1370 #ifndef S_ISDIR
1371 #define S_ISDIR(x) (((x) & _S_IFDIR) != 0)
1372 #endif
1373 
1374 #ifndef S_ISREG
1375 #define S_ISREG(x) (((x) & _S_IFREG) != 0)
1376 #endif
1377 
1378 int open(const char *filename, int oflag, int pmode);
1379 int _wstati64(const wchar_t *path, cs_stat_t *st);
1380 const char *strerror();
1381 
1382 #endif /* CS_PLATFORM == CS_P_WINCE */
1383 #endif /* CS_COMMON_PLATFORMS_PLATFORM_WINCE_H_ */
1384 #ifdef MG_MODULE_LINES
1385 #line 1 "common/platforms/platform_nxp_lpc.h"
1386 #endif
1387 /*
1388  * Copyright (c) 2014-2016 Cesanta Software Limited
1389  * All rights reserved
1390  */
1391 
1392 #ifndef CS_COMMON_PLATFORMS_PLATFORM_NXP_LPC_H_
1393 #define CS_COMMON_PLATFORMS_PLATFORM_NXP_LPC_H_
1394 
1395 #if CS_PLATFORM == CS_P_NXP_LPC
1396 
1397 #include <ctype.h>
1398 #include <stdint.h>
1399 #include <string.h>
1400 
1401 #define SIZE_T_FMT "u"
1402 typedef struct stat cs_stat_t;
1403 #define INT64_FMT "lld"
1404 #define INT64_X_FMT "llx"
1405 #define __cdecl
1406 
1407 #define MG_LWIP 1
1408 
1409 #define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
1410 
1411 /*
1412  * LPCXpress comes with 3 C library implementations: Newlib, NewlibNano and Redlib.
1413  * See https://community.nxp.com/message/630860 for more details.
1414  *
1415  * Redlib is the default and lacks certain things, so we provide them.
1416  */
1417 #ifdef __REDLIB_INTERFACE_VERSION__
1418 
1419 /* Let LWIP define timeval for us. */
1420 #define LWIP_TIMEVAL_PRIVATE 1
1421 
1422 #define va_copy(d, s) __builtin_va_copy(d, s)
1423 
1424 #define CS_ENABLE_TO64 1
1425 #define to64(x) cs_to64(x)
1426 
1427 #define CS_ENABLE_STRDUP 1
1428 
1429 #else
1430 
1431 #include <sys/time.h>
1432 #define LWIP_TIMEVAL_PRIVATE 0
1433 #define to64(x) strtoll(x, NULL, 10)
1434 
1435 #endif
1436 
1437 #endif /* CS_PLATFORM == CS_P_NXP_LPC */
1438 #endif /* CS_COMMON_PLATFORMS_PLATFORM_NXP_LPC_H_ */
1439 #ifdef MG_MODULE_LINES
1440 #line 1 "common/platforms/platform_nxp_kinetis.h"
1441 #endif
1442 /*
1443  * Copyright (c) 2014-2016 Cesanta Software Limited
1444  * All rights reserved
1445  */
1446 
1447 #ifndef CS_COMMON_PLATFORMS_PLATFORM_NXP_KINETIS_H_
1448 #define CS_COMMON_PLATFORMS_PLATFORM_NXP_KINETIS_H_
1449 
1450 #if CS_PLATFORM == CS_P_NXP_KINETIS
1451 
1452 #include <ctype.h>
1453 #include <inttypes.h>
1454 #include <string.h>
1455 #include <sys/time.h>
1456 
1457 #define SIZE_T_FMT "u"
1458 typedef struct stat cs_stat_t;
1459 #define to64(x) strtoll(x, NULL, 10)
1460 #define INT64_FMT "lld"
1461 #define INT64_X_FMT "llx"
1462 #define __cdecl
1463 
1464 #define MG_LWIP 1
1465 
1466 #define MG_NET_IF MG_NET_IF_LWIP_LOW_LEVEL
1467 
1468 /* struct timeval is defined in sys/time.h. */
1469 #define LWIP_TIMEVAL_PRIVATE 0
1470 
1471 #endif /* CS_PLATFORM == CS_P_NXP_KINETIS */
1472 #endif /* CS_COMMON_PLATFORMS_PLATFORM_NXP_KINETIS_H_ */
1473 #ifdef MG_MODULE_LINES
1474 #line 1 "common/platforms/platform_pic32.h"
1475 #endif
1476 /*
1477  * Copyright (c) 2014-2016 Cesanta Software Limited
1478  * All rights reserved
1479  */
1480 
1481 #ifndef CS_COMMON_PLATFORMS_PLATFORM_PIC32_H_
1482 #define CS_COMMON_PLATFORMS_PLATFORM_PIC32_H_
1483 
1484 #if CS_PLATFORM == CS_P_PIC32
1485 
1486 #define MG_NET_IF MG_NET_IF_PIC32
1487 
1488 #include <stdint.h>
1489 #include <time.h>
1490 #include <ctype.h>
1491 #include <stdlib.h>
1492 
1493 #include <system_config.h>
1494 #include <system_definitions.h>
1495 
1496 #include <sys/types.h>
1497 
1498 typedef TCP_SOCKET sock_t;
1499 #define to64(x) strtoll(x, NULL, 10)
1500 
1501 #define SIZE_T_FMT "lu"
1502 #define INT64_FMT "lld"
1503 
1504 #ifndef CS_ENABLE_STDIO
1505 #define CS_ENABLE_STDIO 1
1506 #endif
1507 
1508 char* inet_ntoa(struct in_addr in);
1509 
1510 #endif /* CS_PLATFORM == CS_P_PIC32 */
1511 
1512 #endif /* CS_COMMON_PLATFORMS_PLATFORM_PIC32_H_ */
1513 #ifdef MG_MODULE_LINES
1514 #line 1 "common/platforms/platform_stm32.h"
1515 #endif
1516 /*
1517  * Copyright (c) 2014-2016 Cesanta Software Limited
1518  * All rights reserved
1519  */
1520 
1521 #ifndef CS_COMMON_PLATFORMS_PLATFORM_STM32_H_
1522 #define CS_COMMON_PLATFORMS_PLATFORM_STM32_H_
1523 #if CS_PLATFORM == CS_P_STM32
1524 
1525 #include <sys/types.h>
1526 #include <sys/stat.h>
1527 #include <stdint.h>
1528 #include <inttypes.h>
1529 #include <stdio.h>
1530 #include <ctype.h>
1531 #include <errno.h>
1532 #include <memory.h>
1533 #include <fcntl.h>
1534 #include <stm32_sdk_hal.h>
1535 
1536 #define to64(x) strtoll(x, NULL, 10)
1537 #define INT64_FMT PRId64
1538 #define SIZE_T_FMT "u"
1539 typedef struct stat cs_stat_t;
1540 #define DIRSEP '/'
1541 
1542 #ifndef CS_ENABLE_STDIO
1543 #define CS_ENABLE_STDIO 1
1544 #endif
1545 
1546 #ifndef MG_ENABLE_FILESYSTEM
1547 #define MG_ENABLE_FILESYSTEM 1
1548 #endif
1549 
1550 #define CS_DEFINE_DIRENT
1551 
1552 #endif /* CS_PLATFORM == CS_P_STM32 */
1553 #endif /* CS_COMMON_PLATFORMS_PLATFORM_STM32_H_ */
1554 #ifdef MG_MODULE_LINES
1555 #line 1 "common/platforms/lwip/mg_lwip.h"
1556 #endif
1557 /*
1558  * Copyright (c) 2014-2016 Cesanta Software Limited
1559  * All rights reserved
1560  */
1561 
1562 #ifndef CS_COMMON_PLATFORMS_LWIP_MG_LWIP_H_
1563 #define CS_COMMON_PLATFORMS_LWIP_MG_LWIP_H_
1564 
1565 #ifndef MG_LWIP
1566 #define MG_LWIP 0
1567 #endif
1568 
1569 #if MG_LWIP
1570 
1571 /*
1572  * When compiling for nRF5x chips with arm-none-eabi-gcc, it has BYTE_ORDER
1573  * already defined, so in order to avoid warnings in lwip, we have to undefine
1574  * it.
1575  *
1576  * TODO: Check if in the future versions of nRF5 SDK that changes.
1577  * Current version of nRF51 SDK: 0.8.0
1578  * nRF5 SDK: 0.9.0
1579  */
1580 #if CS_PLATFORM == CS_P_NRF51 || CS_PLATFORM == CS_P_NRF52
1581 #undef BYTE_ORDER
1582 #endif
1583 
1584 #include <lwip/opt.h>
1585 #include <lwip/err.h>
1586 #include <lwip/ip_addr.h>
1587 #include <lwip/inet.h>
1588 #include <lwip/netdb.h>
1589 #include <lwip/dns.h>
1590 
1591 #ifndef LWIP_PROVIDE_ERRNO
1592 #include <errno.h>
1593 #endif
1594 
1595 #if LWIP_SOCKET
1596 #include <lwip/sockets.h>
1597 #else
1598 /* We really need the definitions from sockets.h. */
1599 #undef LWIP_SOCKET
1600 #define LWIP_SOCKET 1
1601 #include <lwip/sockets.h>
1602 #undef LWIP_SOCKET
1603 #define LWIP_SOCKET 0
1604 #endif
1605 
1606 #define INVALID_SOCKET (-1)
1607 #define SOMAXCONN 10
1608 typedef int sock_t;
1609 
1610 #if MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL
1611 struct mg_mgr;
1612 struct mg_connection;
1613 uint32_t mg_lwip_get_poll_delay_ms(struct mg_mgr *mgr);
1614 void mg_lwip_set_keepalive_params(struct mg_connection *nc, int idle,
1615  int interval, int count);
1616 #endif
1617 
1618 /* For older version of LWIP */
1619 #ifndef ipX_2_ip
1620 #define ipX_2_ip(x) (x)
1621 #endif
1622 
1623 #endif /* MG_LWIP */
1624 
1625 #endif /* CS_COMMON_PLATFORMS_LWIP_MG_LWIP_H_ */
1626 #ifdef MG_MODULE_LINES
1627 #line 1 "common/cs_time.h"
1628 #endif
1629 /*
1630  * Copyright (c) 2014-2016 Cesanta Software Limited
1631  * All rights reserved
1632  */
1633 
1634 #ifndef CS_COMMON_CS_TIME_H_
1635 #define CS_COMMON_CS_TIME_H_
1636 
1637 /* Amalgamated: #include "common/platform.h" */
1638 
1639 #ifdef __cplusplus
1640 extern "C" {
1641 #endif /* __cplusplus */
1642 
1643 /* Sub-second granularity time(). */
1644 double cs_time(void);
1645 
1646 #ifdef __cplusplus
1647 }
1648 #endif /* __cplusplus */
1649 
1650 #endif /* CS_COMMON_CS_TIME_H_ */
1651 #ifdef MG_MODULE_LINES
1652 #line 1 "common/mg_str.h"
1653 #endif
1654 /*
1655  * Copyright (c) 2014-2016 Cesanta Software Limited
1656  * All rights reserved
1657  */
1658 
1659 #ifndef CS_COMMON_MG_STR_H_
1660 #define CS_COMMON_MG_STR_H_
1661 
1662 #include <stddef.h>
1663 
1664 /* Amalgamated: #include "common/platform.h" */
1665 
1666 #ifdef __cplusplus
1667 extern "C" {
1668 #endif /* __cplusplus */
1669 
1670 /* Describes chunk of memory */
1671 struct mg_str {
1672  const char *p; /* Memory chunk pointer */
1673  size_t len; /* Memory chunk length */
1674 };
1675 
1676 /*
1677  * Helper functions for creating mg_str struct from plain C string.
1678  * `NULL` is allowed and becomes `{NULL, 0}`.
1679  */
1680 struct mg_str mg_mk_str(const char *s);
1681 struct mg_str mg_mk_str_n(const char *s, size_t len);
1682 
1683 /* Macro for initializing mg_str. */
1684 #define MG_MK_STR(str_literal) \
1685  { str_literal, sizeof(str_literal) - 1 }
1686 
1687 /*
1688  * Cross-platform version of `strcmp()` where where first string is
1689  * specified by `struct mg_str`.
1690  */
1691 int mg_vcmp(const struct mg_str *str2, const char *str1);
1692 
1693 /*
1694  * Cross-platform version of `strncasecmp()` where first string is
1695  * specified by `struct mg_str`.
1696  */
1697 int mg_vcasecmp(const struct mg_str *str2, const char *str1);
1698 
1699 struct mg_str mg_strdup(const struct mg_str s);
1700 int mg_strcmp(const struct mg_str str1, const struct mg_str str2);
1701 int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n);
1702 
1703 #ifdef __cplusplus
1704 }
1705 #endif /* __cplusplus */
1706 
1707 #endif /* CS_COMMON_MG_STR_H_ */
1708 #ifdef MG_MODULE_LINES
1709 #line 1 "common/mbuf.h"
1710 #endif
1711 /*
1712  * Copyright (c) 2015 Cesanta Software Limited
1713  * All rights reserved
1714  */
1715 
1716 /*
1717  * === Memory Buffers
1718  *
1719  * Mbufs are mutable/growing memory buffers, like C++ strings.
1720  * Mbuf can append data to the end of a buffer or insert data into arbitrary
1721  * position in the middle of a buffer. The buffer grows automatically when
1722  * needed.
1723  */
1724 
1725 #ifndef CS_COMMON_MBUF_H_
1726 #define CS_COMMON_MBUF_H_
1727 
1728 #include <stdlib.h>
1729 /* Amalgamated: #include "common/platform.h" */
1730 
1731 #if defined(__cplusplus)
1732 extern "C" {
1733 #endif
1734 
1735 #ifndef MBUF_SIZE_MULTIPLIER
1736 #define MBUF_SIZE_MULTIPLIER 1.5
1737 #endif
1738 
1739 /* Memory buffer descriptor */
1740 struct mbuf {
1741  char *buf; /* Buffer pointer */
1742  size_t len; /* Data length. Data is located between offset 0 and len. */
1743  size_t size; /* Buffer size allocated by realloc(1). Must be >= len */
1744 };
1745 
1746 /*
1747  * Initialises an Mbuf.
1748  * `initial_capacity` specifies the initial capacity of the mbuf.
1749  */
1750 void mbuf_init(struct mbuf *, size_t initial_capacity);
1751 
1752 /* Frees the space allocated for the mbuffer and resets the mbuf structure. */
1753 void mbuf_free(struct mbuf *);
1754 
1755 /*
1756  * Appends data to the Mbuf.
1757  *
1758  * Returns the number of bytes appended or 0 if out of memory.
1759  */
1760 size_t mbuf_append(struct mbuf *, const void *data, size_t data_size);
1761 
1762 /*
1763  * Inserts data at a specified offset in the Mbuf.
1764  *
1765  * Existing data will be shifted forwards and the buffer will
1766  * be grown if necessary.
1767  * Returns the number of bytes inserted.
1768  */
1769 size_t mbuf_insert(struct mbuf *, size_t, const void *, size_t);
1770 
1771 /* Removes `data_size` bytes from the beginning of the buffer. */
1772 void mbuf_remove(struct mbuf *, size_t data_size);
1773 
1774 /*
1775  * Resizes an Mbuf.
1776  *
1777  * If `new_size` is smaller than buffer's `len`, the
1778  * resize is not performed.
1779  */
1780 void mbuf_resize(struct mbuf *, size_t new_size);
1781 
1782 /* Shrinks an Mbuf by resizing its `size` to `len`. */
1783 void mbuf_trim(struct mbuf *);
1784 
1785 #if defined(__cplusplus)
1786 }
1787 #endif /* __cplusplus */
1788 
1789 #endif /* CS_COMMON_MBUF_H_ */
1790 #ifdef MG_MODULE_LINES
1791 #line 1 "common/sha1.h"
1792 #endif
1793 /*
1794  * Copyright (c) 2014 Cesanta Software Limited
1795  * All rights reserved
1796  */
1797 
1798 #ifndef CS_COMMON_SHA1_H_
1799 #define CS_COMMON_SHA1_H_
1800 
1801 #ifndef DISABLE_SHA1
1802 #define DISABLE_SHA1 0
1803 #endif
1804 
1805 #if !DISABLE_SHA1
1806 
1807 /* Amalgamated: #include "common/platform.h" */
1808 
1809 #ifdef __cplusplus
1810 extern "C" {
1811 #endif /* __cplusplus */
1812 
1813 typedef struct {
1814  uint32_t state[5];
1815  uint32_t count[2];
1816  unsigned char buffer[64];
1817 } cs_sha1_ctx;
1818 
1819 void cs_sha1_init(cs_sha1_ctx *);
1820 void cs_sha1_update(cs_sha1_ctx *, const unsigned char *data, uint32_t len);
1821 void cs_sha1_final(unsigned char digest[20], cs_sha1_ctx *);
1822 void cs_hmac_sha1(const unsigned char *key, size_t key_len,
1823  const unsigned char *text, size_t text_len,
1824  unsigned char out[20]);
1825 #ifdef __cplusplus
1826 }
1827 #endif /* __cplusplus */
1828 
1829 #endif /* DISABLE_SHA1 */
1830 
1831 #endif /* CS_COMMON_SHA1_H_ */
1832 #ifdef MG_MODULE_LINES
1833 #line 1 "common/md5.h"
1834 #endif
1835 /*
1836  * Copyright (c) 2014 Cesanta Software Limited
1837  * All rights reserved
1838  */
1839 
1840 #ifndef CS_COMMON_MD5_H_
1841 #define CS_COMMON_MD5_H_
1842 
1843 /* Amalgamated: #include "common/platform.h" */
1844 
1845 #ifndef DISABLE_MD5
1846 #define DISABLE_MD5 0
1847 #endif
1848 
1849 #ifdef __cplusplus
1850 extern "C" {
1851 #endif /* __cplusplus */
1852 
1853 typedef struct MD5Context {
1854  uint32_t buf[4];
1855  uint32_t bits[2];
1856  unsigned char in[64];
1857 } MD5_CTX;
1858 
1859 void MD5_Init(MD5_CTX *c);
1860 void MD5_Update(MD5_CTX *c, const unsigned char *data, size_t len);
1861 void MD5_Final(unsigned char *md, MD5_CTX *c);
1862 
1863 /*
1864  * Return stringified MD5 hash for NULL terminated list of pointer/length pairs.
1865  * A length should be specified as size_t variable.
1866  * Example:
1867  *
1868  * char buf[33];
1869  * cs_md5(buf, "foo", (size_t) 3, "bar", (size_t) 3, NULL);
1870  */
1871 char *cs_md5(char buf[33], ...);
1872 
1873 #ifdef __cplusplus
1874 }
1875 #endif /* __cplusplus */
1876 
1877 #endif /* CS_COMMON_MD5_H_ */
1878 #ifdef MG_MODULE_LINES
1879 #line 1 "common/base64.h"
1880 #endif
1881 /*
1882  * Copyright (c) 2014 Cesanta Software Limited
1883  * All rights reserved
1884  */
1885 
1886 #ifndef CS_COMMON_BASE64_H_
1887 #define CS_COMMON_BASE64_H_
1888 
1889 #ifndef DISABLE_BASE64
1890 #define DISABLE_BASE64 0
1891 #endif
1892 
1893 #if !DISABLE_BASE64
1894 
1895 #include <stdio.h>
1896 
1897 #ifdef __cplusplus
1898 extern "C" {
1899 #endif
1900 
1901 typedef void (*cs_base64_putc_t)(char, void *);
1902 
1904  /* cannot call it putc because it's a macro on some environments */
1906  unsigned char chunk[3];
1908  void *user_data;
1909 };
1910 
1911 void cs_base64_init(struct cs_base64_ctx *ctx, cs_base64_putc_t putc,
1912  void *user_data);
1913 void cs_base64_update(struct cs_base64_ctx *ctx, const char *str, size_t len);
1914 void cs_base64_finish(struct cs_base64_ctx *ctx);
1915 
1916 void cs_base64_encode(const unsigned char *src, int src_len, char *dst);
1917 void cs_fprint_base64(FILE *f, const unsigned char *src, int src_len);
1918 int cs_base64_decode(const unsigned char *s, int len, char *dst, int *dec_len);
1919 
1920 #ifdef __cplusplus
1921 }
1922 #endif
1923 
1924 #endif /* DISABLE_BASE64 */
1925 
1926 #endif /* CS_COMMON_BASE64_H_ */
1927 #ifdef MG_MODULE_LINES
1928 #line 1 "common/str_util.h"
1929 #endif
1930 /*
1931  * Copyright (c) 2015 Cesanta Software Limited
1932  * All rights reserved
1933  */
1934 
1935 #ifndef CS_COMMON_STR_UTIL_H_
1936 #define CS_COMMON_STR_UTIL_H_
1937 
1938 #include <stdarg.h>
1939 #include <stdlib.h>
1940 
1941 /* Amalgamated: #include "common/platform.h" */
1942 
1943 #ifndef CS_ENABLE_STRDUP
1944 #define CS_ENABLE_STRDUP 0
1945 #endif
1946 
1947 #ifndef CS_ENABLE_TO64
1948 #define CS_ENABLE_TO64 0
1949 #endif
1950 
1951 /*
1952  * Expands to a string representation of its argument: e.g.
1953  * `CS_STRINGIFY_LIT(5) expands to "5"`
1954  */
1955 #define CS_STRINGIFY_LIT(x) #x
1956 
1957 /*
1958  * Expands to a string representation of its argument, which is allowed
1959  * to be a macro: e.g.
1960  *
1961  * #define FOO 123
1962  * CS_STRINGIFY_MACRO(FOO)
1963  *
1964  * expands to 123.
1965  */
1966 #define CS_STRINGIFY_MACRO(x) CS_STRINGIFY_LIT(x)
1967 
1968 #ifdef __cplusplus
1969 extern "C" {
1970 #endif
1971 
1972 size_t c_strnlen(const char *s, size_t maxlen);
1973 int c_snprintf(char *buf, size_t buf_size, const char *format, ...);
1974 int c_vsnprintf(char *buf, size_t buf_size, const char *format, va_list ap);
1975 /*
1976  * Find the first occurrence of find in s, where the search is limited to the
1977  * first slen characters of s.
1978  */
1979 const char *c_strnstr(const char *s, const char *find, size_t slen);
1980 
1981 /*
1982  * Stringify binary data. Output buffer size must be 2 * size_of_input + 1
1983  * because each byte of input takes 2 bytes in string representation
1984  * plus 1 byte for the terminating \0 character.
1985  */
1986 void cs_to_hex(char *to, const unsigned char *p, size_t len);
1987 
1988 /*
1989  * Convert stringified binary data back to binary.
1990  * Does the reverse of `cs_to_hex()`.
1991  */
1992 void cs_from_hex(char *to, const char *p, size_t len);
1993 
1994 #if CS_ENABLE_STRDUP
1995 char *strdup(const char *src);
1996 #endif
1997 
1998 #if CS_ENABLE_TO64
1999 #include <stdint.h>
2000 /*
2001  * Simple string -> int64 conversion routine.
2002  */
2003 int64_t cs_to64(const char *s);
2004 #endif
2005 
2006 /*
2007  * Cross-platform version of `strncasecmp()`.
2008  */
2009 int mg_ncasecmp(const char *s1, const char *s2, size_t len);
2010 
2011 /*
2012  * Cross-platform version of `strcasecmp()`.
2013  */
2014 int mg_casecmp(const char *s1, const char *s2);
2015 
2016 /*
2017  * Prints message to the buffer. If the buffer is large enough to hold the
2018  * message, it returns buffer. If buffer is to small, it allocates a large
2019  * enough buffer on heap and returns allocated buffer.
2020  * This is a supposed use case:
2021  *
2022  * char buf[5], *p = buf;
2023  * mg_avprintf(&p, sizeof(buf), "%s", "hi there");
2024  * use_p_somehow(p);
2025  * if (p != buf) {
2026  * free(p);
2027  * }
2028  *
2029  * The purpose of this is to avoid malloc-ing if generated strings are small.
2030  */
2031 int mg_asprintf(char **buf, size_t size, const char *fmt, ...);
2032 
2033 /* Same as mg_asprintf, but takes varargs list. */
2034 int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap);
2035 
2036 #ifdef __cplusplus
2037 }
2038 #endif
2039 
2040 #endif /* CS_COMMON_STR_UTIL_H_ */
2041 #ifdef MG_MODULE_LINES
2042 #line 1 "common/queue.h"
2043 #endif
2044 /* clang-format off */
2045 /*-
2046  * Copyright (c) 1991, 1993
2047  * The Regents of the University of California. All rights reserved.
2048  *
2049  * Redistribution and use in source and binary forms, with or without
2050  * modification, are permitted provided that the following conditions
2051  * are met:
2052  * 1. Redistributions of source code must retain the above copyright
2053  * notice, this list of conditions and the following disclaimer.
2054  * 2. Redistributions in binary form must reproduce the above copyright
2055  * notice, this list of conditions and the following disclaimer in the
2056  * documentation and/or other materials provided with the distribution.
2057  * 4. Neither the name of the University nor the names of its contributors
2058  * may be used to endorse or promote products derived from this software
2059  * without specific prior written permission.
2060  *
2061  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2062  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2063  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2064  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2065  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2066  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2067  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2068  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2069  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2070  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2071  * SUCH DAMAGE.
2072  *
2073  * @(#)queue.h 8.5 (Berkeley) 8/20/94
2074  * $FreeBSD$
2075  */
2076 
2077 #ifndef _SYS_QUEUE_H_
2078 #define _SYS_QUEUE_H_
2079 
2080 /*
2081  * This file defines four types of data structures: singly-linked lists,
2082  * singly-linked tail queues, lists and tail queues.
2083  *
2084  * A singly-linked list is headed by a single forward pointer. The elements
2085  * are singly linked for minimum space and pointer manipulation overhead at
2086  * the expense of O(n) removal for arbitrary elements. New elements can be
2087  * added to the list after an existing element or at the head of the list.
2088  * Elements being removed from the head of the list should use the explicit
2089  * macro for this purpose for optimum efficiency. A singly-linked list may
2090  * only be traversed in the forward direction. Singly-linked lists are ideal
2091  * for applications with large datasets and few or no removals or for
2092  * implementing a LIFO queue.
2093  *
2094  * A singly-linked tail queue is headed by a pair of pointers, one to the
2095  * head of the list and the other to the tail of the list. The elements are
2096  * singly linked for minimum space and pointer manipulation overhead at the
2097  * expense of O(n) removal for arbitrary elements. New elements can be added
2098  * to the list after an existing element, at the head of the list, or at the
2099  * end of the list. Elements being removed from the head of the tail queue
2100  * should use the explicit macro for this purpose for optimum efficiency.
2101  * A singly-linked tail queue may only be traversed in the forward direction.
2102  * Singly-linked tail queues are ideal for applications with large datasets
2103  * and few or no removals or for implementing a FIFO queue.
2104  *
2105  * A list is headed by a single forward pointer (or an array of forward
2106  * pointers for a hash table header). The elements are doubly linked
2107  * so that an arbitrary element can be removed without a need to
2108  * traverse the list. New elements can be added to the list before
2109  * or after an existing element or at the head of the list. A list
2110  * may be traversed in either direction.
2111  *
2112  * A tail queue is headed by a pair of pointers, one to the head of the
2113  * list and the other to the tail of the list. The elements are doubly
2114  * linked so that an arbitrary element can be removed without a need to
2115  * traverse the list. New elements can be added to the list before or
2116  * after an existing element, at the head of the list, or at the end of
2117  * the list. A tail queue may be traversed in either direction.
2118  *
2119  * For details on the use of these macros, see the queue(3) manual page.
2120  *
2121  *
2122  * SLIST LIST STAILQ TAILQ
2123  * _HEAD + + + +
2124  * _CLASS_HEAD + + + +
2125  * _HEAD_INITIALIZER + + + +
2126  * _ENTRY + + + +
2127  * _CLASS_ENTRY + + + +
2128  * _INIT + + + +
2129  * _EMPTY + + + +
2130  * _FIRST + + + +
2131  * _NEXT + + + +
2132  * _PREV - + - +
2133  * _LAST - - + +
2134  * _FOREACH + + + +
2135  * _FOREACH_FROM + + + +
2136  * _FOREACH_SAFE + + + +
2137  * _FOREACH_FROM_SAFE + + + +
2138  * _FOREACH_REVERSE - - - +
2139  * _FOREACH_REVERSE_FROM - - - +
2140  * _FOREACH_REVERSE_SAFE - - - +
2141  * _FOREACH_REVERSE_FROM_SAFE - - - +
2142  * _INSERT_HEAD + + + +
2143  * _INSERT_BEFORE - + - +
2144  * _INSERT_AFTER + + + +
2145  * _INSERT_TAIL - - + +
2146  * _CONCAT - - + +
2147  * _REMOVE_AFTER + - + -
2148  * _REMOVE_HEAD + - + -
2149  * _REMOVE + + + +
2150  * _SWAP + + + +
2151  *
2152  */
2153 #ifdef QUEUE_MACRO_DEBUG
2154 /* Store the last 2 places the queue element or head was altered */
2155 struct qm_trace {
2156  unsigned long lastline;
2157  unsigned long prevline;
2158  const char *lastfile;
2159  const char *prevfile;
2160 };
2161 
2162 #define TRACEBUF struct qm_trace trace;
2163 #define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } ,
2164 #define TRASHIT(x) do {(x) = (void *)-1;} while (0)
2165 #define QMD_SAVELINK(name, link) void **name = (void *)&(link)
2166 
2167 #define QMD_TRACE_HEAD(head) do { \
2168  (head)->trace.prevline = (head)->trace.lastline; \
2169  (head)->trace.prevfile = (head)->trace.lastfile; \
2170  (head)->trace.lastline = __LINE__; \
2171  (head)->trace.lastfile = __FILE__; \
2172 } while (0)
2173 
2174 #define QMD_TRACE_ELEM(elem) do { \
2175  (elem)->trace.prevline = (elem)->trace.lastline; \
2176  (elem)->trace.prevfile = (elem)->trace.lastfile; \
2177  (elem)->trace.lastline = __LINE__; \
2178  (elem)->trace.lastfile = __FILE__; \
2179 } while (0)
2180 
2181 #else
2182 #define QMD_TRACE_ELEM(elem)
2183 #define QMD_TRACE_HEAD(head)
2184 #define QMD_SAVELINK(name, link)
2185 #define TRACEBUF
2186 #define TRACEBUF_INITIALIZER
2187 #define TRASHIT(x)
2188 #endif /* QUEUE_MACRO_DEBUG */
2189 
2190 #ifdef __cplusplus
2191 /*
2192  * In C++ there can be structure lists and class lists:
2193  */
2194 #define QUEUE_TYPEOF(type) type
2195 #else
2196 #define QUEUE_TYPEOF(type) struct type
2197 #endif
2198 
2199 /*
2200  * Singly-linked List declarations.
2201  */
2202 #define SLIST_HEAD(name, type) \
2203 struct name { \
2204  struct type *slh_first; /* first element */ \
2205 }
2206 
2207 #define SLIST_CLASS_HEAD(name, type) \
2208 struct name { \
2209  class type *slh_first; /* first element */ \
2210 }
2211 
2212 #define SLIST_HEAD_INITIALIZER(head) \
2213  { NULL }
2214 
2215 #define SLIST_ENTRY(type) \
2216 struct { \
2217  struct type *sle_next; /* next element */ \
2218 }
2219 
2220 #define SLIST_CLASS_ENTRY(type) \
2221 struct { \
2222  class type *sle_next; /* next element */ \
2223 }
2224 
2225 /*
2226  * Singly-linked List functions.
2227  */
2228 #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
2229 
2230 #define SLIST_FIRST(head) ((head)->slh_first)
2231 
2232 #define SLIST_FOREACH(var, head, field) \
2233  for ((var) = SLIST_FIRST((head)); \
2234  (var); \
2235  (var) = SLIST_NEXT((var), field))
2236 
2237 #define SLIST_FOREACH_FROM(var, head, field) \
2238  for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
2239  (var); \
2240  (var) = SLIST_NEXT((var), field))
2241 
2242 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
2243  for ((var) = SLIST_FIRST((head)); \
2244  (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
2245  (var) = (tvar))
2246 
2247 #define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
2248  for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \
2249  (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
2250  (var) = (tvar))
2251 
2252 #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
2253  for ((varp) = &SLIST_FIRST((head)); \
2254  ((var) = *(varp)) != NULL; \
2255  (varp) = &SLIST_NEXT((var), field))
2256 
2257 #define SLIST_INIT(head) do { \
2258  SLIST_FIRST((head)) = NULL; \
2259 } while (0)
2260 
2261 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
2262  SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
2263  SLIST_NEXT((slistelm), field) = (elm); \
2264 } while (0)
2265 
2266 #define SLIST_INSERT_HEAD(head, elm, field) do { \
2267  SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
2268  SLIST_FIRST((head)) = (elm); \
2269 } while (0)
2270 
2271 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
2272 
2273 #define SLIST_REMOVE(head, elm, type, field) do { \
2274  QMD_SAVELINK(oldnext, (elm)->field.sle_next); \
2275  if (SLIST_FIRST((head)) == (elm)) { \
2276  SLIST_REMOVE_HEAD((head), field); \
2277  } \
2278  else { \
2279  QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head); \
2280  while (SLIST_NEXT(curelm, field) != (elm)) \
2281  curelm = SLIST_NEXT(curelm, field); \
2282  SLIST_REMOVE_AFTER(curelm, field); \
2283  } \
2284  TRASHIT(*oldnext); \
2285 } while (0)
2286 
2287 #define SLIST_REMOVE_AFTER(elm, field) do { \
2288  SLIST_NEXT(elm, field) = \
2289  SLIST_NEXT(SLIST_NEXT(elm, field), field); \
2290 } while (0)
2291 
2292 #define SLIST_REMOVE_HEAD(head, field) do { \
2293  SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
2294 } while (0)
2295 
2296 #define SLIST_SWAP(head1, head2, type) do { \
2297  QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1); \
2298  SLIST_FIRST(head1) = SLIST_FIRST(head2); \
2299  SLIST_FIRST(head2) = swap_first; \
2300 } while (0)
2301 
2302 /*
2303  * Singly-linked Tail queue declarations.
2304  */
2305 #define STAILQ_HEAD(name, type) \
2306 struct name { \
2307  struct type *stqh_first;/* first element */ \
2308  struct type **stqh_last;/* addr of last next element */ \
2309 }
2310 
2311 #define STAILQ_CLASS_HEAD(name, type) \
2312 struct name { \
2313  class type *stqh_first; /* first element */ \
2314  class type **stqh_last; /* addr of last next element */ \
2315 }
2316 
2317 #define STAILQ_HEAD_INITIALIZER(head) \
2318  { NULL, &(head).stqh_first }
2319 
2320 #define STAILQ_ENTRY(type) \
2321 struct { \
2322  struct type *stqe_next; /* next element */ \
2323 }
2324 
2325 #define STAILQ_CLASS_ENTRY(type) \
2326 struct { \
2327  class type *stqe_next; /* next element */ \
2328 }
2329 
2330 /*
2331  * Singly-linked Tail queue functions.
2332  */
2333 #define STAILQ_CONCAT(head1, head2) do { \
2334  if (!STAILQ_EMPTY((head2))) { \
2335  *(head1)->stqh_last = (head2)->stqh_first; \
2336  (head1)->stqh_last = (head2)->stqh_last; \
2337  STAILQ_INIT((head2)); \
2338  } \
2339 } while (0)
2340 
2341 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
2342 
2343 #define STAILQ_FIRST(head) ((head)->stqh_first)
2344 
2345 #define STAILQ_FOREACH(var, head, field) \
2346  for((var) = STAILQ_FIRST((head)); \
2347  (var); \
2348  (var) = STAILQ_NEXT((var), field))
2349 
2350 #define STAILQ_FOREACH_FROM(var, head, field) \
2351  for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
2352  (var); \
2353  (var) = STAILQ_NEXT((var), field))
2354 
2355 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
2356  for ((var) = STAILQ_FIRST((head)); \
2357  (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
2358  (var) = (tvar))
2359 
2360 #define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
2361  for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \
2362  (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
2363  (var) = (tvar))
2364 
2365 #define STAILQ_INIT(head) do { \
2366  STAILQ_FIRST((head)) = NULL; \
2367  (head)->stqh_last = &STAILQ_FIRST((head)); \
2368 } while (0)
2369 
2370 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
2371  if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
2372  (head)->stqh_last = &STAILQ_NEXT((elm), field); \
2373  STAILQ_NEXT((tqelm), field) = (elm); \
2374 } while (0)
2375 
2376 #define STAILQ_INSERT_HEAD(head, elm, field) do { \
2377  if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
2378  (head)->stqh_last = &STAILQ_NEXT((elm), field); \
2379  STAILQ_FIRST((head)) = (elm); \
2380 } while (0)
2381 
2382 #define STAILQ_INSERT_TAIL(head, elm, field) do { \
2383  STAILQ_NEXT((elm), field) = NULL; \
2384  *(head)->stqh_last = (elm); \
2385  (head)->stqh_last = &STAILQ_NEXT((elm), field); \
2386 } while (0)
2387 
2388 #define STAILQ_LAST(head, type, field) \
2389  (STAILQ_EMPTY((head)) ? NULL : \
2390  __containerof((head)->stqh_last, \
2391  QUEUE_TYPEOF(type), field.stqe_next))
2392 
2393 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
2394 
2395 #define STAILQ_REMOVE(head, elm, type, field) do { \
2396  QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \
2397  if (STAILQ_FIRST((head)) == (elm)) { \
2398  STAILQ_REMOVE_HEAD((head), field); \
2399  } \
2400  else { \
2401  QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head); \
2402  while (STAILQ_NEXT(curelm, field) != (elm)) \
2403  curelm = STAILQ_NEXT(curelm, field); \
2404  STAILQ_REMOVE_AFTER(head, curelm, field); \
2405  } \
2406  TRASHIT(*oldnext); \
2407 } while (0)
2408 
2409 #define STAILQ_REMOVE_AFTER(head, elm, field) do { \
2410  if ((STAILQ_NEXT(elm, field) = \
2411  STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
2412  (head)->stqh_last = &STAILQ_NEXT((elm), field); \
2413 } while (0)
2414 
2415 #define STAILQ_REMOVE_HEAD(head, field) do { \
2416  if ((STAILQ_FIRST((head)) = \
2417  STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
2418  (head)->stqh_last = &STAILQ_FIRST((head)); \
2419 } while (0)
2420 
2421 #define STAILQ_SWAP(head1, head2, type) do { \
2422  QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1); \
2423  QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last; \
2424  STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
2425  (head1)->stqh_last = (head2)->stqh_last; \
2426  STAILQ_FIRST(head2) = swap_first; \
2427  (head2)->stqh_last = swap_last; \
2428  if (STAILQ_EMPTY(head1)) \
2429  (head1)->stqh_last = &STAILQ_FIRST(head1); \
2430  if (STAILQ_EMPTY(head2)) \
2431  (head2)->stqh_last = &STAILQ_FIRST(head2); \
2432 } while (0)
2433 
2434 
2435 /*
2436  * List declarations.
2437  */
2438 #define LIST_HEAD(name, type) \
2439 struct name { \
2440  struct type *lh_first; /* first element */ \
2441 }
2442 
2443 #define LIST_CLASS_HEAD(name, type) \
2444 struct name { \
2445  class type *lh_first; /* first element */ \
2446 }
2447 
2448 #define LIST_HEAD_INITIALIZER(head) \
2449  { NULL }
2450 
2451 #define LIST_ENTRY(type) \
2452 struct { \
2453  struct type *le_next; /* next element */ \
2454  struct type **le_prev; /* address of previous next element */ \
2455 }
2456 
2457 #define LIST_CLASS_ENTRY(type) \
2458 struct { \
2459  class type *le_next; /* next element */ \
2460  class type **le_prev; /* address of previous next element */ \
2461 }
2462 
2463 /*
2464  * List functions.
2465  */
2466 
2467 #if (defined(_KERNEL) && defined(INVARIANTS))
2468 #define QMD_LIST_CHECK_HEAD(head, field) do { \
2469  if (LIST_FIRST((head)) != NULL && \
2470  LIST_FIRST((head))->field.le_prev != \
2471  &LIST_FIRST((head))) \
2472  panic("Bad list head %p first->prev != head", (head)); \
2473 } while (0)
2474 
2475 #define QMD_LIST_CHECK_NEXT(elm, field) do { \
2476  if (LIST_NEXT((elm), field) != NULL && \
2477  LIST_NEXT((elm), field)->field.le_prev != \
2478  &((elm)->field.le_next)) \
2479  panic("Bad link elm %p next->prev != elm", (elm)); \
2480 } while (0)
2481 
2482 #define QMD_LIST_CHECK_PREV(elm, field) do { \
2483  if (*(elm)->field.le_prev != (elm)) \
2484  panic("Bad link elm %p prev->next != elm", (elm)); \
2485 } while (0)
2486 #else
2487 #define QMD_LIST_CHECK_HEAD(head, field)
2488 #define QMD_LIST_CHECK_NEXT(elm, field)
2489 #define QMD_LIST_CHECK_PREV(elm, field)
2490 #endif /* (_KERNEL && INVARIANTS) */
2491 
2492 #define LIST_EMPTY(head) ((head)->lh_first == NULL)
2493 
2494 #define LIST_FIRST(head) ((head)->lh_first)
2495 
2496 #define LIST_FOREACH(var, head, field) \
2497  for ((var) = LIST_FIRST((head)); \
2498  (var); \
2499  (var) = LIST_NEXT((var), field))
2500 
2501 #define LIST_FOREACH_FROM(var, head, field) \
2502  for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
2503  (var); \
2504  (var) = LIST_NEXT((var), field))
2505 
2506 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
2507  for ((var) = LIST_FIRST((head)); \
2508  (var) && ((tvar) = LIST_NEXT((var), field), 1); \
2509  (var) = (tvar))
2510 
2511 #define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \
2512  for ((var) = ((var) ? (var) : LIST_FIRST((head))); \
2513  (var) && ((tvar) = LIST_NEXT((var), field), 1); \
2514  (var) = (tvar))
2515 
2516 #define LIST_INIT(head) do { \
2517  LIST_FIRST((head)) = NULL; \
2518 } while (0)
2519 
2520 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
2521  QMD_LIST_CHECK_NEXT(listelm, field); \
2522  if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
2523  LIST_NEXT((listelm), field)->field.le_prev = \
2524  &LIST_NEXT((elm), field); \
2525  LIST_NEXT((listelm), field) = (elm); \
2526  (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
2527 } while (0)
2528 
2529 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
2530  QMD_LIST_CHECK_PREV(listelm, field); \
2531  (elm)->field.le_prev = (listelm)->field.le_prev; \
2532  LIST_NEXT((elm), field) = (listelm); \
2533  *(listelm)->field.le_prev = (elm); \
2534  (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
2535 } while (0)
2536 
2537 #define LIST_INSERT_HEAD(head, elm, field) do { \
2538  QMD_LIST_CHECK_HEAD((head), field); \
2539  if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
2540  LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
2541  LIST_FIRST((head)) = (elm); \
2542  (elm)->field.le_prev = &LIST_FIRST((head)); \
2543 } while (0)
2544 
2545 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
2546 
2547 #define LIST_PREV(elm, head, type, field) \
2548  ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \
2549  __containerof((elm)->field.le_prev, \
2550  QUEUE_TYPEOF(type), field.le_next))
2551 
2552 #define LIST_REMOVE(elm, field) do { \
2553  QMD_SAVELINK(oldnext, (elm)->field.le_next); \
2554  QMD_SAVELINK(oldprev, (elm)->field.le_prev); \
2555  QMD_LIST_CHECK_NEXT(elm, field); \
2556  QMD_LIST_CHECK_PREV(elm, field); \
2557  if (LIST_NEXT((elm), field) != NULL) \
2558  LIST_NEXT((elm), field)->field.le_prev = \
2559  (elm)->field.le_prev; \
2560  *(elm)->field.le_prev = LIST_NEXT((elm), field); \
2561  TRASHIT(*oldnext); \
2562  TRASHIT(*oldprev); \
2563 } while (0)
2564 
2565 #define LIST_SWAP(head1, head2, type, field) do { \
2566  QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1); \
2567  LIST_FIRST((head1)) = LIST_FIRST((head2)); \
2568  LIST_FIRST((head2)) = swap_tmp; \
2569  if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
2570  swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
2571  if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
2572  swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
2573 } while (0)
2574 
2575 /*
2576  * Tail queue declarations.
2577  */
2578 #define TAILQ_HEAD(name, type) \
2579 struct name { \
2580  struct type *tqh_first; /* first element */ \
2581  struct type **tqh_last; /* addr of last next element */ \
2582  TRACEBUF \
2583 }
2584 
2585 #define TAILQ_CLASS_HEAD(name, type) \
2586 struct name { \
2587  class type *tqh_first; /* first element */ \
2588  class type **tqh_last; /* addr of last next element */ \
2589  TRACEBUF \
2590 }
2591 
2592 #define TAILQ_HEAD_INITIALIZER(head) \
2593  { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER }
2594 
2595 #define TAILQ_ENTRY(type) \
2596 struct { \
2597  struct type *tqe_next; /* next element */ \
2598  struct type **tqe_prev; /* address of previous next element */ \
2599  TRACEBUF \
2600 }
2601 
2602 #define TAILQ_CLASS_ENTRY(type) \
2603 struct { \
2604  class type *tqe_next; /* next element */ \
2605  class type **tqe_prev; /* address of previous next element */ \
2606  TRACEBUF \
2607 }
2608 
2609 /*
2610  * Tail queue functions.
2611  */
2612 #if (defined(_KERNEL) && defined(INVARIANTS))
2613 #define QMD_TAILQ_CHECK_HEAD(head, field) do { \
2614  if (!TAILQ_EMPTY(head) && \
2615  TAILQ_FIRST((head))->field.tqe_prev != \
2616  &TAILQ_FIRST((head))) \
2617  panic("Bad tailq head %p first->prev != head", (head)); \
2618 } while (0)
2619 
2620 #define QMD_TAILQ_CHECK_TAIL(head, field) do { \
2621  if (*(head)->tqh_last != NULL) \
2622  panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
2623 } while (0)
2624 
2625 #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
2626  if (TAILQ_NEXT((elm), field) != NULL && \
2627  TAILQ_NEXT((elm), field)->field.tqe_prev != \
2628  &((elm)->field.tqe_next)) \
2629  panic("Bad link elm %p next->prev != elm", (elm)); \
2630 } while (0)
2631 
2632 #define QMD_TAILQ_CHECK_PREV(elm, field) do { \
2633  if (*(elm)->field.tqe_prev != (elm)) \
2634  panic("Bad link elm %p prev->next != elm", (elm)); \
2635 } while (0)
2636 #else
2637 #define QMD_TAILQ_CHECK_HEAD(head, field)
2638 #define QMD_TAILQ_CHECK_TAIL(head, headname)
2639 #define QMD_TAILQ_CHECK_NEXT(elm, field)
2640 #define QMD_TAILQ_CHECK_PREV(elm, field)
2641 #endif /* (_KERNEL && INVARIANTS) */
2642 
2643 #define TAILQ_CONCAT(head1, head2, field) do { \
2644  if (!TAILQ_EMPTY(head2)) { \
2645  *(head1)->tqh_last = (head2)->tqh_first; \
2646  (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
2647  (head1)->tqh_last = (head2)->tqh_last; \
2648  TAILQ_INIT((head2)); \
2649  QMD_TRACE_HEAD(head1); \
2650  QMD_TRACE_HEAD(head2); \
2651  } \
2652 } while (0)
2653 
2654 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
2655 
2656 #define TAILQ_FIRST(head) ((head)->tqh_first)
2657 
2658 #define TAILQ_FOREACH(var, head, field) \
2659  for ((var) = TAILQ_FIRST((head)); \
2660  (var); \
2661  (var) = TAILQ_NEXT((var), field))
2662 
2663 #define TAILQ_FOREACH_FROM(var, head, field) \
2664  for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
2665  (var); \
2666  (var) = TAILQ_NEXT((var), field))
2667 
2668 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
2669  for ((var) = TAILQ_FIRST((head)); \
2670  (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
2671  (var) = (tvar))
2672 
2673 #define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \
2674  for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \
2675  (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
2676  (var) = (tvar))
2677 
2678 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
2679  for ((var) = TAILQ_LAST((head), headname); \
2680  (var); \
2681  (var) = TAILQ_PREV((var), headname, field))
2682 
2683 #define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \
2684  for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
2685  (var); \
2686  (var) = TAILQ_PREV((var), headname, field))
2687 
2688 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
2689  for ((var) = TAILQ_LAST((head), headname); \
2690  (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
2691  (var) = (tvar))
2692 
2693 #define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \
2694  for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \
2695  (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
2696  (var) = (tvar))
2697 
2698 #define TAILQ_INIT(head) do { \
2699  TAILQ_FIRST((head)) = NULL; \
2700  (head)->tqh_last = &TAILQ_FIRST((head)); \
2701  QMD_TRACE_HEAD(head); \
2702 } while (0)
2703 
2704 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
2705  QMD_TAILQ_CHECK_NEXT(listelm, field); \
2706  if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
2707  TAILQ_NEXT((elm), field)->field.tqe_prev = \
2708  &TAILQ_NEXT((elm), field); \
2709  else { \
2710  (head)->tqh_last = &TAILQ_NEXT((elm), field); \
2711  QMD_TRACE_HEAD(head); \
2712  } \
2713  TAILQ_NEXT((listelm), field) = (elm); \
2714  (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
2715  QMD_TRACE_ELEM(&(elm)->field); \
2716  QMD_TRACE_ELEM(&(listelm)->field); \
2717 } while (0)
2718 
2719 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
2720  QMD_TAILQ_CHECK_PREV(listelm, field); \
2721  (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
2722  TAILQ_NEXT((elm), field) = (listelm); \
2723  *(listelm)->field.tqe_prev = (elm); \
2724  (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
2725  QMD_TRACE_ELEM(&(elm)->field); \
2726  QMD_TRACE_ELEM(&(listelm)->field); \
2727 } while (0)
2728 
2729 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
2730  QMD_TAILQ_CHECK_HEAD(head, field); \
2731  if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
2732  TAILQ_FIRST((head))->field.tqe_prev = \
2733  &TAILQ_NEXT((elm), field); \
2734  else \
2735  (head)->tqh_last = &TAILQ_NEXT((elm), field); \
2736  TAILQ_FIRST((head)) = (elm); \
2737  (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
2738  QMD_TRACE_HEAD(head); \
2739  QMD_TRACE_ELEM(&(elm)->field); \
2740 } while (0)
2741 
2742 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
2743  QMD_TAILQ_CHECK_TAIL(head, field); \
2744  TAILQ_NEXT((elm), field) = NULL; \
2745  (elm)->field.tqe_prev = (head)->tqh_last; \
2746  *(head)->tqh_last = (elm); \
2747  (head)->tqh_last = &TAILQ_NEXT((elm), field); \
2748  QMD_TRACE_HEAD(head); \
2749  QMD_TRACE_ELEM(&(elm)->field); \
2750 } while (0)
2751 
2752 #define TAILQ_LAST(head, headname) \
2753  (*(((struct headname *)((head)->tqh_last))->tqh_last))
2754 
2755 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
2756 
2757 #define TAILQ_PREV(elm, headname, field) \
2758  (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
2759 
2760 #define TAILQ_REMOVE(head, elm, field) do { \
2761  QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
2762  QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
2763  QMD_TAILQ_CHECK_NEXT(elm, field); \
2764  QMD_TAILQ_CHECK_PREV(elm, field); \
2765  if ((TAILQ_NEXT((elm), field)) != NULL) \
2766  TAILQ_NEXT((elm), field)->field.tqe_prev = \
2767  (elm)->field.tqe_prev; \
2768  else { \
2769  (head)->tqh_last = (elm)->field.tqe_prev; \
2770  QMD_TRACE_HEAD(head); \
2771  } \
2772  *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
2773  TRASHIT(*oldnext); \
2774  TRASHIT(*oldprev); \
2775  QMD_TRACE_ELEM(&(elm)->field); \
2776 } while (0)
2777 
2778 #define TAILQ_SWAP(head1, head2, type, field) do { \
2779  QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first; \
2780  QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last; \
2781  (head1)->tqh_first = (head2)->tqh_first; \
2782  (head1)->tqh_last = (head2)->tqh_last; \
2783  (head2)->tqh_first = swap_first; \
2784  (head2)->tqh_last = swap_last; \
2785  if ((swap_first = (head1)->tqh_first) != NULL) \
2786  swap_first->field.tqe_prev = &(head1)->tqh_first; \
2787  else \
2788  (head1)->tqh_last = &(head1)->tqh_first; \
2789  if ((swap_first = (head2)->tqh_first) != NULL) \
2790  swap_first->field.tqe_prev = &(head2)->tqh_first; \
2791  else \
2792  (head2)->tqh_last = &(head2)->tqh_first; \
2793 } while (0)
2794 
2795 #endif /* !_SYS_QUEUE_H_ */
2796 #ifdef MG_MODULE_LINES
2797 #line 1 "mongoose/src/features.h"
2798 #endif
2799 /*
2800  * Copyright (c) 2014-2016 Cesanta Software Limited
2801  * All rights reserved
2802  */
2803 
2804 #ifndef CS_MONGOOSE_SRC_FEATURES_H_
2805 #define CS_MONGOOSE_SRC_FEATURES_H_
2806 
2807 #ifndef MG_DISABLE_HTTP_DIGEST_AUTH
2808 #define MG_DISABLE_HTTP_DIGEST_AUTH 0
2809 #endif
2810 
2811 #ifndef MG_DISABLE_HTTP_KEEP_ALIVE
2812 #define MG_DISABLE_HTTP_KEEP_ALIVE 0
2813 #endif
2814 
2815 #ifndef MG_DISABLE_PFS
2816 #define MG_DISABLE_PFS 0
2817 #endif
2818 
2819 #ifndef MG_DISABLE_WS_RANDOM_MASK
2820 #define MG_DISABLE_WS_RANDOM_MASK 0
2821 #endif
2822 
2823 #ifndef MG_ENABLE_ASYNC_RESOLVER
2824 #define MG_ENABLE_ASYNC_RESOLVER 1
2825 #endif
2826 
2827 #ifndef MG_ENABLE_BROADCAST
2828 #define MG_ENABLE_BROADCAST 0
2829 #endif
2830 
2831 #ifndef MG_ENABLE_COAP
2832 #define MG_ENABLE_COAP 0
2833 #endif
2834 
2835 #ifndef MG_ENABLE_DEBUG
2836 #define MG_ENABLE_DEBUG 0
2837 #endif
2838 
2839 #ifndef MG_ENABLE_DIRECTORY_LISTING
2840 #define MG_ENABLE_DIRECTORY_LISTING 0
2841 #endif
2842 
2843 #ifndef MG_ENABLE_DNS
2844 #define MG_ENABLE_DNS 1
2845 #endif
2846 
2847 #ifndef MG_ENABLE_DNS_SERVER
2848 #define MG_ENABLE_DNS_SERVER 0
2849 #endif
2850 
2851 #ifndef MG_ENABLE_FAKE_DAVLOCK
2852 #define MG_ENABLE_FAKE_DAVLOCK 0
2853 #endif
2854 
2855 #ifndef MG_ENABLE_FILESYSTEM
2856 #define MG_ENABLE_FILESYSTEM 0
2857 #endif
2858 
2859 #ifndef MG_ENABLE_GETADDRINFO
2860 #define MG_ENABLE_GETADDRINFO 0
2861 #endif
2862 
2863 #ifndef MG_ENABLE_HEXDUMP
2864 #define MG_ENABLE_HEXDUMP CS_ENABLE_STDIO
2865 #endif
2866 
2867 #ifndef MG_ENABLE_HTTP
2868 #define MG_ENABLE_HTTP 1
2869 #endif
2870 
2871 #ifndef MG_ENABLE_HTTP_CGI
2872 #define MG_ENABLE_HTTP_CGI 0
2873 #endif
2874 
2875 #ifndef MG_ENABLE_HTTP_SSI
2876 #define MG_ENABLE_HTTP_SSI MG_ENABLE_FILESYSTEM
2877 #endif
2878 
2879 #ifndef MG_ENABLE_HTTP_SSI_EXEC
2880 #define MG_ENABLE_HTTP_SSI_EXEC 0
2881 #endif
2882 
2883 #ifndef MG_ENABLE_HTTP_STREAMING_MULTIPART
2884 #define MG_ENABLE_HTTP_STREAMING_MULTIPART 0
2885 #endif
2886 
2887 #ifndef MG_ENABLE_HTTP_WEBDAV
2888 #define MG_ENABLE_HTTP_WEBDAV 0
2889 #endif
2890 
2891 #ifndef MG_ENABLE_HTTP_WEBSOCKET
2892 #define MG_ENABLE_HTTP_WEBSOCKET MG_ENABLE_HTTP
2893 #endif
2894 
2895 #ifndef MG_ENABLE_IPV6
2896 #define MG_ENABLE_IPV6 0
2897 #endif
2898 
2899 #ifndef MG_ENABLE_JAVASCRIPT
2900 #define MG_ENABLE_JAVASCRIPT 0
2901 #endif
2902 
2903 #ifndef MG_ENABLE_MQTT
2904 #define MG_ENABLE_MQTT 1
2905 #endif
2906 
2907 #ifndef MG_ENABLE_MQTT_BROKER
2908 #define MG_ENABLE_MQTT_BROKER 0
2909 #endif
2910 
2911 #ifndef MG_ENABLE_SSL
2912 #define MG_ENABLE_SSL 0
2913 #endif
2914 
2915 #ifndef MG_ENABLE_SYNC_RESOLVER
2916 #define MG_ENABLE_SYNC_RESOLVER 0
2917 #endif
2918 
2919 #ifndef MG_ENABLE_STDIO
2920 #define MG_ENABLE_STDIO CS_ENABLE_STDIO
2921 #endif
2922 
2923 #ifndef MG_NET_IF
2924 #define MG_NET_IF MG_NET_IF_SOCKET
2925 #endif
2926 
2927 #ifndef MG_SSL_IF
2928 #define MG_SSL_IF MG_SSL_IF_OPENSSL
2929 #endif
2930 
2931 #ifndef MG_ENABLE_THREADS /* ifdef-ok */
2932 #ifdef _WIN32
2933 #define MG_ENABLE_THREADS 1
2934 #else
2935 #define MG_ENABLE_THREADS 0
2936 #endif
2937 #endif
2938 
2939 #if MG_ENABLE_DEBUG && !defined(CS_ENABLE_DEBUG)
2940 #define CS_ENABLE_DEBUG 1
2941 #endif
2942 
2943 /* MQTT broker requires MQTT */
2944 #if MG_ENABLE_MQTT_BROKER && !MG_ENABLE_MQTT
2945 #undef MG_ENABLE_MQTT
2946 #define MG_ENABLE_MQTT 1
2947 #endif
2948 
2949 #ifndef MG_ENABLE_HTTP_URL_REWRITES
2950 #define MG_ENABLE_HTTP_URL_REWRITES \
2951  (CS_PLATFORM == CS_P_WINDOWS || CS_PLATFORM == CS_P_UNIX)
2952 #endif
2953 
2954 #ifndef MG_ENABLE_TUN
2955 #define MG_ENABLE_TUN MG_ENABLE_HTTP_WEBSOCKET
2956 #endif
2957 
2958 #ifndef MG_ENABLE_SNTP
2959 #define MG_ENABLE_SNTP 0
2960 #endif
2961 
2962 #ifndef MG_ENABLE_EXTRA_ERRORS_DESC
2963 #define MG_ENABLE_EXTRA_ERRORS_DESC 0
2964 #endif
2965 
2966 #endif /* CS_MONGOOSE_SRC_FEATURES_H_ */
2967 #ifdef MG_MODULE_LINES
2968 #line 1 "mongoose/src/net_if.h"
2969 #endif
2970 /*
2971  * Copyright (c) 2014-2016 Cesanta Software Limited
2972  * All rights reserved
2973  */
2974 
2975 #ifndef CS_MONGOOSE_SRC_NET_IF_H_
2976 #define CS_MONGOOSE_SRC_NET_IF_H_
2977 
2978 /* Amalgamated: #include "common/platform.h" */
2979 
2980 /*
2981  * Internal async networking core interface.
2982  * Consists of calls made by the core, which should not block,
2983  * and callbacks back into the core ("..._cb").
2984  * Callbacks may (will) cause methods to be invoked from within,
2985  * but methods are not allowed to invoke callbacks inline.
2986  *
2987  * Implementation must ensure that only one callback is invoked at any time.
2988  */
2989 
2990 #ifdef __cplusplus
2991 extern "C" {
2992 #endif /* __cplusplus */
2993 
2994 #define MG_MAIN_IFACE 0
2995 
2996 struct mg_mgr;
2997 struct mg_connection;
2998 union socket_address;
2999 
3000 struct mg_iface_vtable;
3001 
3002 struct mg_iface {
3003  struct mg_mgr *mgr;
3004  void *data; /* Implementation-specific data */
3006 };
3007 
3009  void (*init)(struct mg_iface *iface);
3010  void (*free)(struct mg_iface *iface);
3011  void (*add_conn)(struct mg_connection *nc);
3012  void (*remove_conn)(struct mg_connection *nc);
3013  time_t (*poll)(struct mg_iface *iface, int timeout_ms);
3014 
3015  /* Set up a listening TCP socket on a given address. rv = 0 -> ok. */
3016  int (*listen_tcp)(struct mg_connection *nc, union socket_address *sa);
3017  /* Request that a "listening" UDP socket be created. */
3018  int (*listen_udp)(struct mg_connection *nc, union socket_address *sa);
3019 
3020  /* Request that a TCP connection is made to the specified address. */
3021  void (*connect_tcp)(struct mg_connection *nc, const union socket_address *sa);
3022  /* Open a UDP socket. Doesn't actually connect anything. */
3023  void (*connect_udp)(struct mg_connection *nc);
3024 
3025  /* Send functions for TCP and UDP. Sent data is copied before return. */
3026  void (*tcp_send)(struct mg_connection *nc, const void *buf, size_t len);
3027  void (*udp_send)(struct mg_connection *nc, const void *buf, size_t len);
3028 
3029  void (*recved)(struct mg_connection *nc, size_t len);
3030 
3031  /* Perform interface-related connection initialization. Return 1 on ok. */
3032  int (*create_conn)(struct mg_connection *nc);
3033  /* Perform interface-related cleanup on connection before destruction. */
3034  void (*destroy_conn)(struct mg_connection *nc);
3035 
3036  /* Associate a socket to a connection. */
3037  void (*sock_set)(struct mg_connection *nc, sock_t sock);
3038 
3039  /* Put connection's address into *sa, local (remote = 0) or remote. */
3040  void (*get_conn_addr)(struct mg_connection *nc, int remote,
3041  union socket_address *sa);
3042 };
3043 
3044 extern struct mg_iface_vtable *mg_ifaces[];
3045 extern int mg_num_ifaces;
3046 
3047 /* Creates a new interface instance. */
3049  struct mg_mgr *mgr);
3050 
3051 /*
3052  * Find an interface with a given implementation. The search is started from
3053  * interface `from`, exclusive. Returns NULL if none is found.
3054  */
3055 struct mg_iface *mg_find_iface(struct mg_mgr *mgr,
3056  struct mg_iface_vtable *vtable,
3057  struct mg_iface *from);
3058 /*
3059  * Deliver a new TCP connection. Returns NULL in case on error (unable to
3060  * create connection, in which case interface state should be discarded.
3061  * This is phase 1 of the two-phase process - MG_EV_ACCEPT will be delivered
3062  * when mg_if_accept_tcp_cb is invoked.
3063  */
3065 void mg_if_accept_tcp_cb(struct mg_connection *nc, union socket_address *sa,
3066  size_t sa_len);
3067 
3068 /* Callback invoked by connect methods. err = 0 -> ok, != 0 -> error. */
3069 void mg_if_connect_cb(struct mg_connection *nc, int err);
3070 /* Callback that reports that data has been put on the wire. */
3071 void mg_if_sent_cb(struct mg_connection *nc, int num_sent);
3072 /*
3073  * Receive callback.
3074  * if `own` is true, buf must be heap-allocated and ownership is transferred
3075  * to the core.
3076  * Core will acknowledge consumption by calling iface::recved.
3077  */
3078 void mg_if_recv_tcp_cb(struct mg_connection *nc, void *buf, int len, int own);
3079 /*
3080  * Receive callback.
3081  * buf must be heap-allocated and ownership is transferred to the core.
3082  * Core will acknowledge consumption by calling iface::recved.
3083  */
3084 void mg_if_recv_udp_cb(struct mg_connection *nc, void *buf, int len,
3085  union socket_address *sa, size_t sa_len);
3086 
3087 /* void mg_if_close_conn(struct mg_connection *nc); */
3088 
3089 /* Deliver a POLL event to the connection. */
3090 void mg_if_poll(struct mg_connection *nc, time_t now);
3091 
3092 /* Deliver a TIMER event to the connection. */
3093 void mg_if_timer(struct mg_connection *c, double now);
3094 
3095 #ifdef __cplusplus
3096 }
3097 #endif /* __cplusplus */
3098 
3099 #endif /* CS_MONGOOSE_SRC_NET_IF_H_ */
3100 #ifdef MG_MODULE_LINES
3101 #line 1 "mongoose/src/ssl_if.h"
3102 #endif
3103 /*
3104  * Copyright (c) 2014-2016 Cesanta Software Limited
3105  * All rights reserved
3106  */
3107 
3108 #ifndef CS_MONGOOSE_SRC_SSL_IF_H_
3109 #define CS_MONGOOSE_SRC_SSL_IF_H_
3110 
3111 #if MG_ENABLE_SSL
3112 
3113 #ifdef __cplusplus
3114 extern "C" {
3115 #endif /* __cplusplus */
3116 
3117 struct mg_ssl_if_ctx;
3118 struct mg_connection;
3119 
3120 void mg_ssl_if_init();
3121 
3122 enum mg_ssl_if_result {
3123  MG_SSL_OK = 0,
3124  MG_SSL_WANT_READ = -1,
3125  MG_SSL_WANT_WRITE = -2,
3126  MG_SSL_ERROR = -3,
3127 };
3128 
3129 struct mg_ssl_if_conn_params {
3130  const char *cert;
3131  const char *key;
3132  const char *ca_cert;
3133  const char *server_name;
3134  const char *cipher_suites;
3135  const char *psk_identity;
3136  const char *psk_key;
3137 };
3138 
3139 enum mg_ssl_if_result mg_ssl_if_conn_init(
3140  struct mg_connection *nc, const struct mg_ssl_if_conn_params *params,
3141  const char **err_msg);
3142 enum mg_ssl_if_result mg_ssl_if_conn_accept(struct mg_connection *nc,
3143  struct mg_connection *lc);
3144 void mg_ssl_if_conn_free(struct mg_connection *nc);
3145 
3146 enum mg_ssl_if_result mg_ssl_if_handshake(struct mg_connection *nc);
3147 int mg_ssl_if_read(struct mg_connection *nc, void *buf, size_t buf_size);
3148 int mg_ssl_if_write(struct mg_connection *nc, const void *data, size_t len);
3149 
3150 #ifdef __cplusplus
3151 }
3152 #endif /* __cplusplus */
3153 
3154 #endif /* MG_ENABLE_SSL */
3155 
3156 #endif /* CS_MONGOOSE_SRC_SSL_IF_H_ */
3157 #ifdef MG_MODULE_LINES
3158 #line 1 "mongoose/src/net.h"
3159 #endif
3160 /*
3161  * Copyright (c) 2014 Cesanta Software Limited
3162  * All rights reserved
3163  * This software is dual-licensed: you can redistribute it and/or modify
3164  * it under the terms of the GNU General Public License version 2 as
3165  * published by the Free Software Foundation. For the terms of this
3166  * license, see <http://www.gnu.org/licenses/>.
3167  *
3168  * You are free to use this software under the terms of the GNU General
3169  * Public License, but WITHOUT ANY WARRANTY; without even the implied
3170  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
3171  * See the GNU General Public License for more details.
3172  *
3173  * Alternatively, you can license this software under a commercial
3174  * license, as set out in <https://www.cesanta.com/license>.
3175  */
3176 
3177 /*
3178  * === Core API: TCP/UDP/SSL
3179  *
3180  * NOTE: Mongoose manager is single threaded. It does not protect
3181  * its data structures by mutexes, therefore all functions that are dealing
3182  * with a particular event manager should be called from the same thread,
3183  * with exception of the `mg_broadcast()` function. It is fine to have different
3184  * event managers handled by different threads.
3185  */
3186 
3187 #ifndef CS_MONGOOSE_SRC_NET_H_
3188 #define CS_MONGOOSE_SRC_NET_H_
3189 
3190 #if MG_ENABLE_JAVASCRIPT
3191 #define EXCLUDE_COMMON
3192 #include <v7.h>
3193 #endif
3194 
3195 /* Amalgamated: #include "mongoose/src/common.h" */
3196 /* Amalgamated: #include "mongoose/src/net_if.h" */
3197 /* Amalgamated: #include "common/mbuf.h" */
3198 
3199 #ifndef MG_VPRINTF_BUFFER_SIZE
3200 #define MG_VPRINTF_BUFFER_SIZE 100
3201 #endif
3202 
3203 #ifdef MG_USE_READ_WRITE
3204 #define MG_RECV_FUNC(s, b, l, f) read(s, b, l)
3205 #define MG_SEND_FUNC(s, b, l, f) write(s, b, l)
3206 #else
3207 #define MG_RECV_FUNC(s, b, l, f) recv(s, b, l, f)
3208 #define MG_SEND_FUNC(s, b, l, f) send(s, b, l, f)
3209 #endif
3210 
3211 #ifdef __cplusplus
3212 extern "C" {
3213 #endif /* __cplusplus */
3214 
3216  struct sockaddr sa;
3217  struct sockaddr_in sin;
3218 #if MG_ENABLE_IPV6
3219  struct sockaddr_in6 sin6;
3220 #else
3221  struct sockaddr sin6;
3222 #endif
3223 };
3224 
3225 struct mg_connection;
3226 
3227 /*
3228  * Callback function (event handler) prototype. Must be defined by the user.
3229  * Mongoose calls the event handler, passing the events defined below.
3230  */
3231 typedef void (*mg_event_handler_t)(struct mg_connection *nc, int ev,
3232  void *ev_data);
3233 
3234 /* Events. Meaning of event parameter (evp) is given in the comment. */
3235 #define MG_EV_POLL 0 /* Sent to each connection on each mg_mgr_poll() call */
3236 #define MG_EV_ACCEPT 1 /* New connection accepted. union socket_address * */
3237 #define MG_EV_CONNECT 2 /* connect() succeeded or failed. int * */
3238 #define MG_EV_RECV 3 /* Data has benn received. int *num_bytes */
3239 #define MG_EV_SEND 4 /* Data has been written to a socket. int *num_bytes */
3240 #define MG_EV_CLOSE 5 /* Connection is closed. NULL */
3241 #define MG_EV_TIMER 6 /* now >= conn->ev_timer_time. double * */
3242 
3243 /*
3244  * Mongoose event manager.
3245  */
3246 struct mg_mgr {
3248 #if MG_ENABLE_HEXDUMP
3249  const char *hexdump_file; /* Debug hexdump file path */
3250 #endif
3251 #if MG_ENABLE_BROADCAST
3252  sock_t ctl[2]; /* Socketpair for mg_broadcast() */
3253 #endif
3254  void *user_data; /* User data */
3256  struct mg_iface **ifaces; /* network interfaces */
3257 #if MG_ENABLE_JAVASCRIPT
3258  struct v7 *v7;
3259 #endif
3260 };
3261 
3262 /*
3263  * Mongoose connection.
3264  */
3266  struct mg_connection *next, *prev; /* mg_mgr::active_connections linkage */
3267  struct mg_connection *listener; /* Set only for accept()-ed connections */
3268  struct mg_mgr *mgr; /* Pointer to containing manager */
3269 
3270  sock_t sock; /* Socket to the remote peer */
3271  int err;
3272  union socket_address sa; /* Remote peer address */
3273  size_t recv_mbuf_limit; /* Max size of recv buffer */
3274  struct mbuf recv_mbuf; /* Received data */
3275  struct mbuf send_mbuf; /* Data scheduled for sending */
3276  time_t last_io_time; /* Timestamp of the last socket IO */
3277  double ev_timer_time; /* Timestamp of the future MG_EV_TIMER */
3278 #if MG_ENABLE_SSL
3279  void *ssl_if_data; /* SSL library data. */
3280 #endif
3281  mg_event_handler_t proto_handler; /* Protocol-specific event handler */
3282  void *proto_data; /* Protocol-specific data */
3283  void (*proto_data_destructor)(void *proto_data);
3284  mg_event_handler_t handler; /* Event handler function */
3285  void *user_data; /* User-specific data */
3286  union {
3287  void *v;
3288  /*
3289  * the C standard is fussy about fitting function pointers into
3290  * void pointers, since some archs might have fat pointers for functions.
3291  */
3293  } priv_1;
3294  void *priv_2;
3295  void *mgr_data; /* Implementation-specific event manager's data. */
3296  struct mg_iface *iface;
3297  unsigned long flags;
3298 /* Flags set by Mongoose */
3299 #define MG_F_LISTENING (1 << 0) /* This connection is listening */
3300 #define MG_F_UDP (1 << 1) /* This connection is UDP */
3301 #define MG_F_RESOLVING (1 << 2) /* Waiting for async resolver */
3302 #define MG_F_CONNECTING (1 << 3) /* connect() call in progress */
3303 #define MG_F_SSL (1 << 4) /* SSL is enabled on the connection */
3304 #define MG_F_SSL_HANDSHAKE_DONE (1 << 5) /* SSL hanshake has completed */
3305 #define MG_F_WANT_READ (1 << 6) /* SSL specific */
3306 #define MG_F_WANT_WRITE (1 << 7) /* SSL specific */
3307 #define MG_F_IS_WEBSOCKET (1 << 8) /* Websocket specific */
3308 
3309 /* Flags that are settable by user */
3310 #define MG_F_SEND_AND_CLOSE (1 << 10) /* Push remaining data and close */
3311 #define MG_F_CLOSE_IMMEDIATELY (1 << 11) /* Disconnect */
3312 #define MG_F_WEBSOCKET_NO_DEFRAG (1 << 12) /* Websocket specific */
3313 #define MG_F_DELETE_CHUNK (1 << 13) /* HTTP specific */
3314 #define MG_F_ENABLE_BROADCAST (1 << 14) /* Allow broadcast address usage */
3315 #define MG_F_TUN_DO_NOT_RECONNECT (1 << 15) /* Don't reconnect tunnel */
3316 
3317 #define MG_F_USER_1 (1 << 20) /* Flags left for application */
3318 #define MG_F_USER_2 (1 << 21)
3319 #define MG_F_USER_3 (1 << 22)
3320 #define MG_F_USER_4 (1 << 23)
3321 #define MG_F_USER_5 (1 << 24)
3322 #define MG_F_USER_6 (1 << 25)
3323 };
3324 
3325 /*
3326  * Initialise Mongoose manager. Side effect: ignores SIGPIPE signal.
3327  * `mgr->user_data` field will be initialised with a `user_data` parameter.
3328  * That is an arbitrary pointer, where the user code can associate some data
3329  * with the particular Mongoose manager. For example, a C++ wrapper class
3330  * could be written in which case `user_data` can hold a pointer to the
3331  * class instance.
3332  */
3333 void mg_mgr_init(struct mg_mgr *mgr, void *user_data);
3334 
3335 /*
3336  * Optional parameters to `mg_mgr_init_opt()`.
3337  *
3338  * If `main_iface` is not NULL, it will be used as the main interface in the
3339  * default interface set. The pointer will be free'd by `mg_mgr_free`.
3340  * Otherwise, the main interface will be autodetected based on the current
3341  * platform.
3342  *
3343  * If `num_ifaces` is 0 and `ifaces` is NULL, the default interface set will be
3344  * used.
3345  * This is an advanced option, as it requires you to construct a full interface
3346  * set, including special networking interfaces required by some optional
3347  * features such as TCP tunneling. Memory backing `ifaces` and each of the
3348  * `num_ifaces` pointers it contains will be reclaimed by `mg_mgr_free`.
3349  */
3354 };
3355 
3356 /*
3357  * Like `mg_mgr_init` but with more options.
3358  *
3359  * Notably, this allows you to create a manger and choose
3360  * dynamically which networking interface implementation to use.
3361  */
3362 void mg_mgr_init_opt(struct mg_mgr *mgr, void *user_data,
3363  struct mg_mgr_init_opts opts);
3364 
3365 /*
3366  * De-initialises Mongoose manager.
3367  *
3368  * Closes and deallocates all active connections.
3369  */
3370 void mg_mgr_free(struct mg_mgr *);
3371 
3372 /*
3373  * This function performs the actual IO and must be called in a loop
3374  * (an event loop). It returns the current timestamp.
3375  * `milli` is the maximum number of milliseconds to sleep.
3376  * `mg_mgr_poll()` checks all connections for IO readiness. If at least one
3377  * of the connections is IO-ready, `mg_mgr_poll()` triggers the respective
3378  * event handlers and returns.
3379  */
3380 time_t mg_mgr_poll(struct mg_mgr *, int milli);
3381 
3382 #if MG_ENABLE_BROADCAST
3383 /*
3384  * Passes a message of a given length to all connections.
3385  *
3386  * Must be called from a thread that does NOT call `mg_mgr_poll()`.
3387  * Note that `mg_broadcast()` is the only function
3388  * that can be, and must be, called from a different (non-IO) thread.
3389  *
3390  * `func` callback function will be called by the IO thread for each
3391  * connection. When called, the event will be `MG_EV_POLL`, and a message will
3392  * be passed as the `ev_data` pointer. Maximum message size is capped
3393  * by `MG_CTL_MSG_MESSAGE_SIZE` which is set to 8192 bytes.
3394  */
3395 void mg_broadcast(struct mg_mgr *, mg_event_handler_t func, void *, size_t);
3396 #endif
3397 
3398 /*
3399  * Iterates over all active connections.
3400  *
3401  * Returns the next connection from the list
3402  * of active connections or `NULL` if there are no more connections. Below
3403  * is the iteration idiom:
3404  *
3405  * ```c
3406  * for (c = mg_next(srv, NULL); c != NULL; c = mg_next(srv, c)) {
3407  * // Do something with connection `c`
3408  * }
3409  * ```
3410  */
3411 struct mg_connection *mg_next(struct mg_mgr *, struct mg_connection *);
3412 
3413 /*
3414  * Optional parameters to `mg_add_sock_opt()`.
3415  *
3416  * `flags` is an initial `struct mg_connection::flags` bitmask to set,
3417  * see `MG_F_*` flags definitions.
3418  */
3420  void *user_data; /* Initial value for connection's user_data */
3421  unsigned int flags; /* Initial connection flags */
3422  const char **error_string; /* Placeholder for the error string */
3423  struct mg_iface *iface; /* Interface instance */
3424 };
3425 
3426 /*
3427  * Creates a connection, associates it with the given socket and event handler
3428  * and adds it to the manager.
3429  *
3430  * For more options see the `mg_add_sock_opt` variant.
3431  */
3432 struct mg_connection *mg_add_sock(struct mg_mgr *, sock_t, mg_event_handler_t);
3433 
3434 /*
3435  * Creates a connection, associates it with the given socket and event handler
3436  * and adds to the manager.
3437  *
3438  * See the `mg_add_sock_opts` structure for a description of the options.
3439  */
3440 struct mg_connection *mg_add_sock_opt(struct mg_mgr *, sock_t,
3442  struct mg_add_sock_opts);
3443 
3444 /*
3445  * Optional parameters to `mg_bind_opt()`.
3446  *
3447  * `flags` is an initial `struct mg_connection::flags` bitmask to set,
3448  * see `MG_F_*` flags definitions.
3449  */
3451  void *user_data; /* Initial value for connection's user_data */
3452  unsigned int flags; /* Extra connection flags */
3453  const char **error_string; /* Placeholder for the error string */
3454  struct mg_iface *iface; /* Interface instance */
3455 #if MG_ENABLE_SSL
3456  /*
3457  * SSL settings.
3458  *
3459  * Server certificate to present to clients or client certificate to
3460  * present to tunnel dispatcher (for tunneled connections).
3461  */
3462  const char *ssl_cert;
3463  /* Private key corresponding to the certificate. If ssl_cert is set but
3464  * ssl_key is not, ssl_cert is used. */
3465  const char *ssl_key;
3466  /* CA bundle used to verify client certificates or tunnel dispatchers. */
3467  const char *ssl_ca_cert;
3468  /* Colon-delimited list of acceptable cipher suites.
3469  * Names depend on the library used, for example:
3470  *
3471  * ECDH-ECDSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256 (OpenSSL)
3472  * TLS-ECDH-ECDSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256
3473  * (mbedTLS)
3474  *
3475  * For OpenSSL the list can be obtained by running "openssl ciphers".
3476  * For mbedTLS, names can be found in library/ssl_ciphersuites.c
3477  * If NULL, a reasonable default is used.
3478  */
3479  const char *ssl_cipher_suites;
3480 #endif
3481 };
3482 
3483 /*
3484  * Creates a listening connection.
3485  *
3486  * See `mg_bind_opt` for full documentation.
3487  */
3488 struct mg_connection *mg_bind(struct mg_mgr *, const char *,
3490 /*
3491  * Creates a listening connection.
3492  *
3493  * The `address` parameter specifies which address to bind to. It's format is
3494  * the same as for the `mg_connect()` call, where `HOST` part is optional.
3495  * `address` can be just a port number, e.g. `:8000`. To bind to a specific
3496  * interface, an IP address can be specified, e.g. `1.2.3.4:8000`. By default,
3497  * a TCP connection is created. To create UDP connection, prepend `udp://`
3498  * prefix, e.g. `udp://:8000`. To summarize, `address` paramer has following
3499  * format: `[PROTO://][IP_ADDRESS]:PORT`, where `PROTO` could be `tcp` or
3500  * `udp`.
3501  *
3502  * See the `mg_bind_opts` structure for a description of the optional
3503  * parameters.
3504  *
3505  * Returns a new listening connection or `NULL` on error.
3506  * NOTE: The connection remains owned by the manager, do not free().
3507  */
3508 struct mg_connection *mg_bind_opt(struct mg_mgr *mgr, const char *address,
3510  struct mg_bind_opts opts);
3511 
3512 /* Optional parameters to `mg_connect_opt()` */
3514  void *user_data; /* Initial value for connection's user_data */
3515  unsigned int flags; /* Extra connection flags */
3516  const char **error_string; /* Placeholder for the error string */
3517  struct mg_iface *iface; /* Interface instance */
3518 #if MG_ENABLE_SSL
3519  /*
3520  * SSL settings.
3521  * Client certificate to present to the server.
3522  */
3523  const char *ssl_cert;
3524  /*
3525  * Private key corresponding to the certificate.
3526  * If ssl_cert is set but ssl_key is not, ssl_cert is used.
3527  */
3528  const char *ssl_key;
3529  /*
3530  * Verify server certificate using this CA bundle. If set to "*", then SSL
3531  * is enabled but no cert verification is performed.
3532  */
3533  const char *ssl_ca_cert;
3534  /* Colon-delimited list of acceptable cipher suites.
3535  * Names depend on the library used, for example:
3536  *
3537  * ECDH-ECDSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256 (OpenSSL)
3538  * TLS-ECDH-ECDSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256
3539  * (mbedTLS)
3540  *
3541  * For OpenSSL the list can be obtained by running "openssl ciphers".
3542  * For mbedTLS, names can be found in library/ssl_ciphersuites.c
3543  * If NULL, a reasonable default is used.
3544  */
3545  const char *ssl_cipher_suites;
3546  /*
3547  * Server name verification. If ssl_ca_cert is set and the certificate has
3548  * passed verification, its subject will be verified against this string.
3549  * By default (if ssl_server_name is NULL) hostname part of the address will
3550  * be used. Wildcard matching is supported. A special value of "*" disables
3551  * name verification.
3552  */
3553  const char *ssl_server_name;
3554  /*
3555  * PSK identity and key. Identity is a NUL-terminated string and key is a hex
3556  * string. Key must be either 16 or 32 bytes (32 or 64 hex digits) for AES-128
3557  * or AES-256 respectively.
3558  * Note: Default list of cipher suites does not include PSK suites, if you
3559  * want to use PSK you will need to set ssl_cipher_suites as well.
3560  */
3561  const char *ssl_psk_identity;
3562  const char *ssl_psk_key;
3563 #endif
3564 };
3565 
3566 /*
3567  * Connects to a remote host.
3568  *
3569  * See `mg_connect_opt()` for full documentation.
3570  */
3571 struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *address,
3573 
3574 /*
3575  * Connects to a remote host.
3576  *
3577  * The `address` format is `[PROTO://]HOST:PORT`. `PROTO` could be `tcp` or
3578  * `udp`. `HOST` could be an IP address,
3579  * IPv6 address (if Mongoose is compiled with `-DMG_ENABLE_IPV6`) or a host
3580  * name. If `HOST` is a name, Mongoose will resolve it asynchronously. Examples
3581  * of valid addresses: `google.com:80`, `udp://1.2.3.4:53`, `10.0.0.1:443`,
3582  * `[::1]:80`
3583  *
3584  * See the `mg_connect_opts` structure for a description of the optional
3585  * parameters.
3586  *
3587  * Returns a new outbound connection or `NULL` on error.
3588  *
3589  * NOTE: The connection remains owned by the manager, do not free().
3590  *
3591  * NOTE: To enable IPv6 addresses `-DMG_ENABLE_IPV6` should be specified
3592  * in the compilation flags.
3593  *
3594  * NOTE: The new connection will receive `MG_EV_CONNECT` as its first event
3595  * which will report the connect success status.
3596  * If the asynchronous resolution fails or the `connect()` syscall fails for
3597  * whatever reason (e.g. with `ECONNREFUSED` or `ENETUNREACH`), then
3598  * `MG_EV_CONNECT` event will report failure. Code example below:
3599  *
3600  * ```c
3601  * static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
3602  * int connect_status;
3603  *
3604  * switch (ev) {
3605  * case MG_EV_CONNECT:
3606  * connect_status = * (int *) ev_data;
3607  * if (connect_status == 0) {
3608  * // Success
3609  * } else {
3610  * // Error
3611  * printf("connect() error: %s\n", strerror(connect_status));
3612  * }
3613  * break;
3614  * ...
3615  * }
3616  * }
3617  *
3618  * ...
3619  * mg_connect(mgr, "my_site.com:80", ev_handler);
3620  * ```
3621  */
3622 struct mg_connection *mg_connect_opt(struct mg_mgr *mgr, const char *address,
3624  struct mg_connect_opts opts);
3625 
3626 #if MG_ENABLE_SSL && MG_NET_IF != MG_NET_IF_SIMPLELINK
3627 /*
3628  * Note: This function is deprecated. Please, use SSL options in
3629  * mg_connect_opt.
3630  *
3631  * Enables SSL for a given connection.
3632  * `cert` is a server certificate file name for a listening connection
3633  * or a client certificate file name for an outgoing connection.
3634  * The certificate files must be in PEM format. The server certificate file
3635  * must contain a certificate, concatenated with a private key, optionally
3636  * concatenated with DH parameters.
3637  * `ca_cert` is a CA certificate or NULL if peer verification is not
3638  * required.
3639  * Return: NULL on success or error message on error.
3640  */
3641 const char *mg_set_ssl(struct mg_connection *nc, const char *cert,
3642  const char *ca_cert);
3643 #endif
3644 
3645 /*
3646  * Sends data to the connection.
3647  *
3648  * Note that sending functions do not actually push data to the socket.
3649  * They just append data to the output buffer. MG_EV_SEND will be delivered when
3650  * the data has actually been pushed out.
3651  */
3652 void mg_send(struct mg_connection *, const void *buf, int len);
3653 
3654 /* Enables format string warnings for mg_printf */
3655 #if defined(__GNUC__)
3656 __attribute__((format(printf, 2, 3)))
3657 #endif
3658 /* don't separate from mg_printf declaration */
3659 
3660 /*
3661  * Sends `printf`-style formatted data to the connection.
3662  *
3663  * See `mg_send` for more details on send semantics.
3664  */
3665 int mg_printf(struct mg_connection *, const char *fmt, ...);
3666 
3667 /* Same as `mg_printf()`, but takes `va_list ap` as an argument. */
3668 int mg_vprintf(struct mg_connection *, const char *fmt, va_list ap);
3669 
3670 /*
3671  * Creates a socket pair.
3672  * `sock_type` can be either `SOCK_STREAM` or `SOCK_DGRAM`.
3673  * Returns 0 on failure and 1 on success.
3674  */
3675 int mg_socketpair(sock_t[2], int sock_type);
3676 
3677 #if MG_ENABLE_SYNC_RESOLVER
3678 /*
3679  * Convert domain name into IP address.
3680  *
3681  * This is a utility function. If compilation flags have
3682  * `-DMG_ENABLE_GETADDRINFO`, then `getaddrinfo()` call is used for name
3683  * resolution. Otherwise, `gethostbyname()` is used.
3684  *
3685  * CAUTION: this function can block.
3686  * Return 1 on success, 0 on failure.
3687  */
3688 int mg_resolve(const char *domain_name, char *ip_addr_buf, size_t buf_len);
3689 #endif
3690 
3691 /*
3692  * Verify given IP address against the ACL.
3693  *
3694  * `remote_ip` - an IPv4 address to check, in host byte order
3695  * `acl` - a comma separated list of IP subnets: `x.x.x.x/x` or `x.x.x.x`.
3696  * Each subnet is
3697  * prepended by either a - or a + sign. A plus sign means allow, where a
3698  * minus sign means deny. If a subnet mask is omitted, such as `-1.2.3.4`,
3699  * it means that only that single IP address is denied.
3700  * Subnet masks may vary from 0 to 32, inclusive. The default setting
3701  * is to allow all access. On each request the full list is traversed,
3702  * and the last match wins. Example:
3703  *
3704  * `-0.0.0.0/0,+192.168/16` - deny all acccesses, only allow 192.168/16 subnet
3705  *
3706  * To learn more about subnet masks, see this
3707  * link:https://en.wikipedia.org/wiki/Subnetwork[Wikipedia page on Subnetwork].
3708  *
3709  * Returns -1 if ACL is malformed, 0 if address is disallowed, 1 if allowed.
3710  */
3711 int mg_check_ip_acl(const char *acl, uint32_t remote_ip);
3712 
3713 #if MG_ENABLE_JAVASCRIPT
3714 /*
3715  * Enables server-side JavaScript scripting.
3716  * Requires a `-DMG_ENABLE_JAVASCRIPT` compilation flag and V7 engine sources.
3717  * V7 instance must not be destroyed during manager's lifetime.
3718  * Returns a V7 error.
3719  */
3720 enum v7_err mg_enable_javascript(struct mg_mgr *m, struct v7 *v7,
3721  const char *init_js_file_name);
3722 #endif
3723 
3724 /*
3725  * Schedules an MG_EV_TIMER event to be delivered at `timestamp` time.
3726  * `timestamp` is UNIX time (the number of seconds since Epoch). It is
3727  * `double` instead of `time_t` to allow for sub-second precision.
3728  * Returns the old timer value.
3729  *
3730  * Example: set the connect timeout to 1.5 seconds:
3731  *
3732  * ```
3733  * c = mg_connect(&mgr, "cesanta.com", ev_handler);
3734  * mg_set_timer(c, mg_time() + 1.5);
3735  * ...
3736  *
3737  * void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
3738  * switch (ev) {
3739  * case MG_EV_CONNECT:
3740  * mg_set_timer(c, 0); // Clear connect timer
3741  * break;
3742  * case MG_EV_TIMER:
3743  * log("Connect timeout");
3744  * c->flags |= MG_F_CLOSE_IMMEDIATELY;
3745  * break;
3746  * ```
3747  */
3748 double mg_set_timer(struct mg_connection *c, double timestamp);
3749 
3750 /*
3751  * A sub-second precision version of time().
3752  */
3753 double mg_time(void);
3754 
3755 #ifdef __cplusplus
3756 }
3757 #endif /* __cplusplus */
3758 
3759 #endif /* CS_MONGOOSE_SRC_NET_H_ */
3760 #ifdef MG_MODULE_LINES
3761 #line 1 "mongoose/src/uri.h"
3762 #endif
3763 /*
3764  * Copyright (c) 2014 Cesanta Software Limited
3765  * All rights reserved
3766  */
3767 
3768 /*
3769  * === URI
3770  */
3771 
3772 #ifndef CS_MONGOOSE_SRC_URI_H_
3773 #define CS_MONGOOSE_SRC_URI_H_
3774 
3775 /* Amalgamated: #include "mongoose/src/net.h" */
3776 
3777 #ifdef __cplusplus
3778 extern "C" {
3779 #endif /* __cplusplus */
3780 
3781 /*
3782  * Parses an URI and fills string chunks with locations of the respective
3783  * uri components within the input uri string. NULL pointers will be
3784  * ignored.
3785  *
3786  * General syntax:
3787  *
3788  * [scheme://[user_info@]]host[:port][/path][?query][#fragment]
3789  *
3790  * Example:
3791  *
3792  * foo.com:80
3793  * tcp://foo.com:1234
3794  * http://foo.com:80/bar?baz=1
3795  * https://user:pw@foo.com:443/blah
3796  *
3797  * `path` will include the leading slash. `query` won't include the leading `?`.
3798  * `host` can contain embedded colons if surrounded by square brackets in order
3799  * to support IPv6 literal addresses.
3800  *
3801  *
3802  * Returns 0 on success, -1 on error.
3803  */
3804 int mg_parse_uri(struct mg_str uri, struct mg_str *scheme,
3805  struct mg_str *user_info, struct mg_str *host,
3806  unsigned int *port, struct mg_str *path, struct mg_str *query,
3807  struct mg_str *fragment);
3808 
3809 int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out);
3810 
3811 #ifdef __cplusplus
3812 }
3813 #endif /* __cplusplus */
3814 #endif /* CS_MONGOOSE_SRC_URI_H_ */
3815 #ifdef MG_MODULE_LINES
3816 #line 1 "mongoose/src/util.h"
3817 #endif
3818 /*
3819  * Copyright (c) 2014 Cesanta Software Limited
3820  * All rights reserved
3821  */
3822 
3823 /*
3824  * === Utility API
3825  */
3826 
3827 #ifndef CS_MONGOOSE_SRC_UTIL_H_
3828 #define CS_MONGOOSE_SRC_UTIL_H_
3829 
3830 #include <stdio.h>
3831 
3832 /* Amalgamated: #include "mongoose/src/common.h" */
3833 /* Amalgamated: #include "mongoose/src/net_if.h" */
3834 
3835 #ifdef __cplusplus
3836 extern "C" {
3837 #endif /* __cplusplus */
3838 
3839 #ifndef MAX_PATH_SIZE
3840 #define MAX_PATH_SIZE 500
3841 #endif
3842 
3843 /*
3844  * Fetches substring from input string `s`, `end` into `v`.
3845  * Skips initial delimiter characters. Records first non-delimiter character
3846  * at the beginning of substring `v`. Then scans the rest of the string
3847  * until a delimiter character or end-of-string is found.
3848  * `delimiters` is a 0-terminated string containing delimiter characters.
3849  * Either one of `delimiters` or `end_string` terminates the search.
3850  * Returns an `s` pointer, advanced forward where parsing has stopped.
3851  */
3852 const char *mg_skip(const char *s, const char *end_string,
3853  const char *delimiters, struct mg_str *v);
3854 
3855 /*
3856  * Decodes base64-encoded string `s`, `len` into the destination `dst`.
3857  * The destination has to have enough space to hold the decoded buffer.
3858  * Decoding stops either when all strings have been decoded or invalid an
3859  * character appeared.
3860  * Destination is '\0'-terminated.
3861  * Returns the number of decoded characters. On success, that should be equal
3862  * to `len`. On error (invalid character) the return value is smaller then
3863  * `len`.
3864  */
3865 int mg_base64_decode(const unsigned char *s, int len, char *dst);
3866 
3867 /*
3868  * Base64-encode chunk of memory `src`, `src_len` into the destination `dst`.
3869  * Destination has to have enough space to hold encoded buffer.
3870  * Destination is '\0'-terminated.
3871  */
3872 void mg_base64_encode(const unsigned char *src, int src_len, char *dst);
3873 
3874 #if MG_ENABLE_FILESYSTEM
3875 /*
3876  * Performs a 64-bit `stat()` call against a given file.
3877  *
3878  * `path` should be UTF8 encoded.
3879  *
3880  * Return value is the same as for `stat()` syscall.
3881  */
3882 int mg_stat(const char *path, cs_stat_t *st);
3883 
3884 /*
3885  * Opens the given file and returns a file stream.
3886  *
3887  * `path` and `mode` should be UTF8 encoded.
3888  *
3889  * Return value is the same as for the `fopen()` call.
3890  */
3891 FILE *mg_fopen(const char *path, const char *mode);
3892 
3893 /*
3894  * Opens the given file and returns a file stream.
3895  *
3896  * `path` should be UTF8 encoded.
3897  *
3898  * Return value is the same as for the `open()` syscall.
3899  */
3900 int mg_open(const char *path, int flag, int mode);
3901 #endif /* MG_ENABLE_FILESYSTEM */
3902 
3903 #if MG_ENABLE_THREADS
3904 /*
3905  * Starts a new detached thread.
3906  * Arguments and semantics are the same as pthead's `pthread_create()`.
3907  * `thread_func` is a thread function, `thread_func_param` is a parameter
3908  * that is passed to the thread function.
3909  */
3910 void *mg_start_thread(void *(*thread_func)(void *), void *thread_func_param);
3911 #endif
3912 
3913 void mg_set_close_on_exec(sock_t);
3914 
3915 #define MG_SOCK_STRINGIFY_IP 1
3916 #define MG_SOCK_STRINGIFY_PORT 2
3917 #define MG_SOCK_STRINGIFY_REMOTE 4
3918 /*
3919  * Converts a connection's local or remote address into string.
3920  *
3921  * The `flags` parameter is a bit mask that controls the behaviour,
3922  * see `MG_SOCK_STRINGIFY_*` definitions.
3923  *
3924  * - MG_SOCK_STRINGIFY_IP - print IP address
3925  * - MG_SOCK_STRINGIFY_PORT - print port number
3926  * - MG_SOCK_STRINGIFY_REMOTE - print remote peer's IP/port, not local address
3927  *
3928  * If both port number and IP address are printed, they are separated by `:`.
3929  * If compiled with `-DMG_ENABLE_IPV6`, IPv6 addresses are supported.
3930  */
3931 void mg_conn_addr_to_str(struct mg_connection *nc, char *buf, size_t len,
3932  int flags);
3933 #if MG_NET_IF == MG_NET_IF_SOCKET
3934 /* Legacy interface. */
3935 void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags);
3936 #endif
3937 
3938 /*
3939  * Convert the socket's address into string.
3940  *
3941  * `flags` is MG_SOCK_STRINGIFY_IP and/or MG_SOCK_STRINGIFY_PORT.
3942  */
3943 void mg_sock_addr_to_str(const union socket_address *sa, char *buf, size_t len,
3944  int flags);
3945 
3946 #if MG_ENABLE_HEXDUMP
3947 /*
3948  * Generates a human-readable hexdump of memory chunk.
3949  *
3950  * Takes a memory buffer `buf` of length `len` and creates a hex dump of that
3951  * buffer in `dst`. The generated output is a-la hexdump(1).
3952  * Returns the length of generated string, excluding terminating `\0`. If
3953  * returned length is bigger than `dst_len`, the overflow bytes are discarded.
3954  */
3955 int mg_hexdump(const void *buf, int len, char *dst, int dst_len);
3956 
3957 /* Same as mg_hexdump, but with output going to file instead of a buffer. */
3958 void mg_hexdumpf(FILE *fp, const void *buf, int len);
3959 
3960 /*
3961  * Generates human-readable hexdump of the data sent or received by the
3962  * connection. `path` is a file name where hexdump should be written.
3963  * `num_bytes` is a number of bytes sent/received. `ev` is one of the `MG_*`
3964  * events sent to an event handler. This function is supposed to be called from
3965  * the event handler.
3966  */
3967 void mg_hexdump_connection(struct mg_connection *nc, const char *path,
3968  const void *buf, int num_bytes, int ev);
3969 #endif
3970 
3971 /*
3972  * Returns true if target platform is big endian.
3973  */
3974 int mg_is_big_endian(void);
3975 
3976 /*
3977  * A helper function for traversing a comma separated list of values.
3978  * It returns a list pointer shifted to the next value or NULL if the end
3979  * of the list found.
3980  * The value is stored in a val vector. If the value has a form "x=y", then
3981  * eq_val vector is initialised to point to the "y" part, and val vector length
3982  * is adjusted to point only to "x".
3983  * If the list is just a comma separated list of entries, like "aa,bb,cc" then
3984  * `eq_val` will contain zero-length string.
3985  *
3986  * The purpose of this function is to parse comma separated string without
3987  * any copying/memory allocation.
3988  */
3989 const char *mg_next_comma_list_entry(const char *list, struct mg_str *val,
3990  struct mg_str *eq_val);
3991 
3992 /*
3993  * Matches 0-terminated string (mg_match_prefix) or string with given length
3994  * mg_match_prefix_n against a glob pattern.
3995  *
3996  * Match is case-insensitive. Returns number of bytes matched, or -1 if no
3997  * match.
3998  */
3999 int mg_match_prefix(const char *pattern, int pattern_len, const char *str);
4000 int mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str);
4001 
4002 /*
4003  * Use with cs_base64_init/update/finish in order to write out base64 in chunks.
4004  */
4005 void mg_mbuf_append_base64_putc(char ch, void *user_data);
4006 
4007 /*
4008  * Encode `len` bytes starting at `data` as base64 and append them to an mbuf.
4009  */
4010 void mg_mbuf_append_base64(struct mbuf *mbuf, const void *data, size_t len);
4011 
4012 /*
4013  * Generate a Basic Auth header and appends it to buf.
4014  * If pass is NULL, then user is expected to contain the credentials pair
4015  * already encoded as `user:pass`.
4016  */
4017 void mg_basic_auth_header(const char *user, const char *pass, struct mbuf *buf);
4018 
4019 #ifdef __cplusplus
4020 }
4021 #endif /* __cplusplus */
4022 #endif /* CS_MONGOOSE_SRC_UTIL_H_ */
4023 #ifdef MG_MODULE_LINES
4024 #line 1 "mongoose/src/http.h"
4025 #endif
4026 /*
4027  * Copyright (c) 2014 Cesanta Software Limited
4028  * All rights reserved
4029  */
4030 
4031 /*
4032  * === Common API reference
4033  */
4034 
4035 #ifndef CS_MONGOOSE_SRC_HTTP_H_
4036 #define CS_MONGOOSE_SRC_HTTP_H_
4037 
4038 #if MG_ENABLE_HTTP
4039 
4040 /* Amalgamated: #include "mongoose/src/net.h" */
4041 /* Amalgamated: #include "common/mg_str.h" */
4042 
4043 #ifdef __cplusplus
4044 extern "C" {
4045 #endif /* __cplusplus */
4046 
4047 #ifndef MG_MAX_HTTP_HEADERS
4048 #define MG_MAX_HTTP_HEADERS 20
4049 #endif
4050 
4051 #ifndef MG_MAX_HTTP_REQUEST_SIZE
4052 #define MG_MAX_HTTP_REQUEST_SIZE 1024
4053 #endif
4054 
4055 #ifndef MG_MAX_PATH
4056 #ifdef PATH_MAX
4057 #define MG_MAX_PATH PATH_MAX
4058 #else
4059 #define MG_MAX_PATH 256
4060 #endif
4061 #endif
4062 
4063 #ifndef MG_MAX_HTTP_SEND_MBUF
4064 #define MG_MAX_HTTP_SEND_MBUF 1024
4065 #endif
4066 
4067 #ifndef MG_CGI_ENVIRONMENT_SIZE
4068 #define MG_CGI_ENVIRONMENT_SIZE 8192
4069 #endif
4070 
4071 /* HTTP message */
4073  struct mg_str message; /* Whole message: request line + headers + body */
4074 
4075  /* HTTP Request line (or HTTP response line) */
4076  struct mg_str method; /* "GET" */
4077  struct mg_str uri; /* "/my_file.html" */
4078  struct mg_str proto; /* "HTTP/1.1" -- for both request and response */
4079 
4080  /* For responses, code and response status message are set */
4082  struct mg_str resp_status_msg;
4083 
4084  /*
4085  * Query-string part of the URI. For example, for HTTP request
4086  * GET /foo/bar?param1=val1&param2=val2
4087  * | uri | query_string |
4088  *
4089  * Note that question mark character doesn't belong neither to the uri,
4090  * nor to the query_string
4091  */
4092  struct mg_str query_string;
4093 
4094  /* Headers */
4095  struct mg_str header_names[MG_MAX_HTTP_HEADERS];
4096  struct mg_str header_values[MG_MAX_HTTP_HEADERS];
4097 
4098  /* Message body */
4099  struct mg_str body; /* Zero-length for requests with no body */
4100 };
4101 
4102 #if MG_ENABLE_HTTP_WEBSOCKET
4103 /* WebSocket message */
4105  unsigned char *data;
4106  size_t size;
4107  unsigned char flags;
4108 };
4109 #endif
4110 
4111 /* HTTP multipart part */
4113  const char *file_name;
4114  const char *var_name;
4115  struct mg_str data;
4116  int status; /* <0 on error */
4117  void *user_data;
4118 };
4119 
4120 /* SSI call context */
4122  struct http_message *req; /* The request being processed. */
4123  struct mg_str file; /* Filesystem path of the file being processed. */
4124  struct mg_str arg; /* The argument passed to the tag: <!-- call arg -->. */
4125 };
4126 
4127 /* HTTP and websocket events. void *ev_data is described in a comment. */
4128 #define MG_EV_HTTP_REQUEST 100 /* struct http_message * */
4129 #define MG_EV_HTTP_REPLY 101 /* struct http_message * */
4130 #define MG_EV_HTTP_CHUNK 102 /* struct http_message * */
4131 #define MG_EV_SSI_CALL 105 /* char * */
4132 #define MG_EV_SSI_CALL_CTX 106 /* struct mg_ssi_call_ctx * */
4133 
4134 #if MG_ENABLE_HTTP_WEBSOCKET
4135 #define MG_EV_WEBSOCKET_HANDSHAKE_REQUEST 111 /* struct http_message * */
4136 #define MG_EV_WEBSOCKET_HANDSHAKE_DONE 112 /* NULL */
4137 #define MG_EV_WEBSOCKET_FRAME 113 /* struct websocket_message * */
4138 #define MG_EV_WEBSOCKET_CONTROL_FRAME 114 /* struct websocket_message * */
4139 #endif
4140 
4141 #if MG_ENABLE_HTTP_STREAMING_MULTIPART
4142 #define MG_EV_HTTP_MULTIPART_REQUEST 121 /* struct http_message */
4143 #define MG_EV_HTTP_PART_BEGIN 122 /* struct mg_http_multipart_part */
4144 #define MG_EV_HTTP_PART_DATA 123 /* struct mg_http_multipart_part */
4145 #define MG_EV_HTTP_PART_END 124 /* struct mg_http_multipart_part */
4146 /* struct mg_http_multipart_part */
4147 #define MG_EV_HTTP_MULTIPART_REQUEST_END 125
4148 #endif
4149 
4150 /*
4151  * Attaches a built-in HTTP event handler to the given connection.
4152  * The user-defined event handler will receive following extra events:
4153  *
4154  * - MG_EV_HTTP_REQUEST: HTTP request has arrived. Parsed HTTP request
4155  * is passed as
4156  * `struct http_message` through the handler's `void *ev_data` pointer.
4157  * - MG_EV_HTTP_REPLY: The HTTP reply has arrived. The parsed HTTP reply is
4158  * passed as `struct http_message` through the handler's `void *ev_data`
4159  * pointer.
4160  * - MG_EV_HTTP_CHUNK: The HTTP chunked-encoding chunk has arrived.
4161  * The parsed HTTP reply is passed as `struct http_message` through the
4162  * handler's `void *ev_data` pointer. `http_message::body` would contain
4163  * incomplete, reassembled HTTP body.
4164  * It will grow with every new chunk that arrives, and it can
4165  * potentially consume a lot of memory. An event handler may process
4166  * the body as chunks are coming, and signal Mongoose to delete processed
4167  * body by setting `MG_F_DELETE_CHUNK` in `mg_connection::flags`. When
4168  * the last zero chunk is received,
4169  * Mongoose sends `MG_EV_HTTP_REPLY` event with
4170  * full reassembled body (if handler did not signal to delete chunks) or
4171  * with empty body (if handler did signal to delete chunks).
4172  * - MG_EV_WEBSOCKET_HANDSHAKE_REQUEST: server has received the WebSocket
4173  * handshake request. `ev_data` contains parsed HTTP request.
4174  * - MG_EV_WEBSOCKET_HANDSHAKE_DONE: server has completed the WebSocket
4175  * handshake. `ev_data` is `NULL`.
4176  * - MG_EV_WEBSOCKET_FRAME: new WebSocket frame has arrived. `ev_data` is
4177  * `struct websocket_message *`
4178  *
4179  * When compiled with MG_ENABLE_HTTP_STREAMING_MULTIPART, Mongoose parses
4180  * multipart requests and splits them into separate events:
4181  * - MG_EV_HTTP_MULTIPART_REQUEST: Start of the request.
4182  * This event is sent before body is parsed. After this, the user
4183  * should expect a sequence of PART_BEGIN/DATA/END requests.
4184  * This is also the last time when headers and other request fields are
4185  * accessible.
4186  * - MG_EV_HTTP_PART_BEGIN: Start of a part of a multipart message.
4187  * Argument: mg_http_multipart_part with var_name and file_name set
4188  * (if present). No data is passed in this message.
4189  * - MG_EV_HTTP_PART_DATA: new portion of data from the multipart message.
4190  * Argument: mg_http_multipart_part. var_name and file_name are preserved,
4191  * data is available in mg_http_multipart_part.data.
4192  * - MG_EV_HTTP_PART_END: End of the current part. var_name, file_name are
4193  * the same, no data in the message. If status is 0, then the part is
4194  * properly terminated with a boundary, status < 0 means that connection
4195  * was terminated.
4196  * - MG_EV_HTTP_MULTIPART_REQUEST_END: End of the multipart request.
4197  * Argument: mg_http_multipart_part, var_name and file_name are NULL,
4198  * status = 0 means request was properly closed, < 0 means connection
4199  * was terminated (note: in this case both PART_END and REQUEST_END are
4200  * delivered).
4201  */
4203 
4204 #if MG_ENABLE_HTTP_WEBSOCKET
4205 /*
4206  * Send websocket handshake to the server.
4207  *
4208  * `nc` must be a valid connection, connected to a server. `uri` is an URI
4209  * to fetch, extra_headers` is extra HTTP headers to send or `NULL`.
4210  *
4211  * This function is intended to be used by websocket client.
4212  *
4213  * Note that the Host header is mandatory in HTTP/1.1 and must be
4214  * included in `extra_headers`. `mg_send_websocket_handshake2` offers
4215  * a better API for that.
4216  *
4217  * Deprecated in favour of `mg_send_websocket_handshake2`
4218  */
4219 void mg_send_websocket_handshake(struct mg_connection *nc, const char *uri,
4220  const char *extra_headers);
4221 
4222 /*
4223  * Send websocket handshake to the server.
4224  *
4225  * `nc` must be a valid connection, connected to a server. `uri` is an URI
4226  * to fetch, `host` goes into the `Host` header, `protocol` goes into the
4227  * `Sec-WebSocket-Proto` header (NULL to omit), extra_headers` is extra HTTP
4228  * headers to send or `NULL`.
4229  *
4230  * This function is intended to be used by websocket client.
4231  */
4232 void mg_send_websocket_handshake2(struct mg_connection *nc, const char *path,
4233  const char *host, const char *protocol,
4234  const char *extra_headers);
4235 
4236 /* Like mg_send_websocket_handshake2 but also passes basic auth header */
4237 void mg_send_websocket_handshake3(struct mg_connection *nc, const char *path,
4238  const char *host, const char *protocol,
4239  const char *extra_headers, const char *user,
4240  const char *pass);
4241 /*
4242  * Helper function that creates an outbound WebSocket connection.
4243  *
4244  * `url` is a URL to connect to. It must be properly URL-encoded, e.g. have
4245  * no spaces, etc. By default, `mg_connect_ws()` sends Connection and
4246  * Host headers. `extra_headers` is an extra HTTP header to send, e.g.
4247  * `"User-Agent: my-app\r\n"`.
4248  * If `protocol` is not NULL, then a `Sec-WebSocket-Protocol` header is sent.
4249  *
4250  * Examples:
4251  *
4252  * ```c
4253  * nc1 = mg_connect_ws(mgr, ev_handler_1, "ws://echo.websocket.org", NULL,
4254  * NULL);
4255  * nc2 = mg_connect_ws(mgr, ev_handler_1, "wss://echo.websocket.org", NULL,
4256  * NULL);
4257  * nc3 = mg_connect_ws(mgr, ev_handler_1, "ws://api.cesanta.com",
4258  * "clubby.cesanta.com", NULL);
4259  * ```
4260  */
4261 struct mg_connection *mg_connect_ws(struct mg_mgr *mgr,
4262  mg_event_handler_t event_handler,
4263  const char *url, const char *protocol,
4264  const char *extra_headers);
4265 
4266 /*
4267  * Helper function that creates an outbound WebSocket connection
4268  *
4269  * Mostly identical to `mg_connect_ws`, but allows to provide extra parameters
4270  * (for example, SSL parameters)
4271  */
4272 struct mg_connection *mg_connect_ws_opt(struct mg_mgr *mgr,
4274  struct mg_connect_opts opts,
4275  const char *url, const char *protocol,
4276  const char *extra_headers);
4277 
4278 /*
4279  * Send WebSocket frame to the remote end.
4280  *
4281  * `op_and_flags` specifies the frame's type. It's one of:
4282  *
4283  * - WEBSOCKET_OP_CONTINUE
4284  * - WEBSOCKET_OP_TEXT
4285  * - WEBSOCKET_OP_BINARY
4286  * - WEBSOCKET_OP_CLOSE
4287  * - WEBSOCKET_OP_PING
4288  * - WEBSOCKET_OP_PONG
4289  *
4290  * Orred with one of the flags:
4291  *
4292  * - WEBSOCKET_DONT_FIN: Don't set the FIN flag on the frame to be sent.
4293  *
4294  * `data` and `data_len` contain frame data.
4295  */
4296 void mg_send_websocket_frame(struct mg_connection *nc, int op_and_flags,
4297  const void *data, size_t data_len);
4298 
4299 /*
4300  * Sends multiple websocket frames.
4301  *
4302  * Like `mg_send_websocket_frame()`, but composes a frame from multiple buffers.
4303  */
4304 void mg_send_websocket_framev(struct mg_connection *nc, int op_and_flags,
4305  const struct mg_str *strings, int num_strings);
4306 
4307 /*
4308  * Sends WebSocket frame to the remote end.
4309  *
4310  * Like `mg_send_websocket_frame()`, but allows to create formatted messages
4311  * with `printf()`-like semantics.
4312  */
4313 void mg_printf_websocket_frame(struct mg_connection *nc, int op_and_flags,
4314  const char *fmt, ...);
4315 
4316 /* Websocket opcodes, from http://tools.ietf.org/html/rfc6455 */
4317 #define WEBSOCKET_OP_CONTINUE 0
4318 #define WEBSOCKET_OP_TEXT 1
4319 #define WEBSOCKET_OP_BINARY 2
4320 #define WEBSOCKET_OP_CLOSE 8
4321 #define WEBSOCKET_OP_PING 9
4322 #define WEBSOCKET_OP_PONG 10
4323 
4324 /*
4325  * If set causes the FIN flag to not be set on outbound
4326  * frames. This enables sending multiple fragments of a single
4327  * logical message.
4328  *
4329  * The WebSocket protocol mandates that if the FIN flag of a data
4330  * frame is not set, the next frame must be a WEBSOCKET_OP_CONTINUE.
4331  * The last frame must have the FIN bit set.
4332  *
4333  * Note that mongoose will automatically defragment incoming messages,
4334  * so this flag is used only on outbound messages.
4335  */
4336 #define WEBSOCKET_DONT_FIN 0x100
4337 
4338 #endif /* MG_ENABLE_HTTP_WEBSOCKET */
4339 
4340 /*
4341  * Decodes a URL-encoded string.
4342  *
4343  * Source string is specified by (`src`, `src_len`), and destination is
4344  * (`dst`, `dst_len`). If `is_form_url_encoded` is non-zero, then
4345  * `+` character is decoded as a blank space character. This function
4346  * guarantees to NUL-terminate the destination. If destination is too small,
4347  * then the source string is partially decoded and `-1` is returned. Otherwise,
4348  * a length of the decoded string is returned, not counting final NUL.
4349  */
4350 int mg_url_decode(const char *src, int src_len, char *dst, int dst_len,
4351  int is_form_url_encoded);
4352 
4353 #ifdef __cplusplus
4354 }
4355 #endif /* __cplusplus */
4356 
4357 #endif /* MG_ENABLE_HTTP */
4358 
4359 #endif /* CS_MONGOOSE_SRC_HTTP_H_ */
4360 #ifdef MG_MODULE_LINES
4361 #line 1 "mongoose/src/http_server.h"
4362 #endif
4363 /*
4364  * === Server API reference
4365  */
4366 
4367 #ifndef CS_MONGOOSE_SRC_HTTP_SERVER_H_
4368 #define CS_MONGOOSE_SRC_HTTP_SERVER_H_
4369 
4370 #if MG_ENABLE_HTTP
4371 
4372 #ifdef __cplusplus
4373 extern "C" {
4374 #endif /* __cplusplus */
4375 
4376 /*
4377  * Parses a HTTP message.
4378  *
4379  * `is_req` should be set to 1 if parsing a request, 0 if reply.
4380  *
4381  * Returns the number of bytes parsed. If HTTP message is
4382  * incomplete `0` is returned. On parse error, a negative number is returned.
4383  */
4384 int mg_parse_http(const char *s, int n, struct http_message *hm, int is_req);
4385 
4386 /*
4387  * Searches and returns the header `name` in parsed HTTP message `hm`.
4388  * If header is not found, NULL is returned. Example:
4389  *
4390  * struct mg_str *host_hdr = mg_get_http_header(hm, "Host");
4391  */
4392 struct mg_str *mg_get_http_header(struct http_message *hm, const char *name);
4393 
4394 /*
4395  * Parses the HTTP header `hdr`. Finds variable `var_name` and stores its value
4396  * in the buffer `buf`, `buf_size`. Returns 0 if variable not found, non-zero
4397  * otherwise.
4398  *
4399  * This function is supposed to parse cookies, authentication headers, etc.
4400  * Example (error handling omitted):
4401  *
4402  * char user[20];
4403  * struct mg_str *hdr = mg_get_http_header(hm, "Authorization");
4404  * mg_http_parse_header(hdr, "username", user, sizeof(user));
4405  *
4406  * Returns the length of the variable's value. If buffer is not large enough,
4407  * or variable not found, 0 is returned.
4408  */
4409 int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf,
4410  size_t buf_size);
4411 
4412 /*
4413  * Gets and parses the Authorization: Basic header
4414  * Returns -1 if no Authorization header is found, or if
4415  * mg_parse_http_basic_auth
4416  * fails parsing the resulting header.
4417  */
4418 int mg_get_http_basic_auth(struct http_message *hm, char *user, size_t user_len,
4419  char *pass, size_t pass_len);
4420 
4421 /*
4422  * Parses the Authorization: Basic header
4423  * Returns -1 iif the authorization type is not "Basic" or any other error such
4424  * as incorrectly encoded base64 user password pair.
4425  */
4426 int mg_parse_http_basic_auth(struct mg_str *hdr, char *user, size_t user_len,
4427  char *pass, size_t pass_len);
4428 
4429 /*
4430  * Parses the buffer `buf`, `buf_len` that contains multipart form data chunks.
4431  * Stores the chunk name in a `var_name`, `var_name_len` buffer.
4432  * If a chunk is an uploaded file, then `file_name`, `file_name_len` is
4433  * filled with an uploaded file name. `chunk`, `chunk_len`
4434  * points to the chunk data.
4435  *
4436  * Return: number of bytes to skip to the next chunk or 0 if there are
4437  * no more chunks.
4438  *
4439  * Usage example:
4440  *
4441  * ```c
4442  * static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
4443  * switch(ev) {
4444  * case MG_EV_HTTP_REQUEST: {
4445  * struct http_message *hm = (struct http_message *) ev_data;
4446  * char var_name[100], file_name[100];
4447  * const char *chunk;
4448  * size_t chunk_len, n1, n2;
4449  *
4450  * n1 = n2 = 0;
4451  * while ((n2 = mg_parse_multipart(hm->body.p + n1,
4452  * hm->body.len - n1,
4453  * var_name, sizeof(var_name),
4454  * file_name, sizeof(file_name),
4455  * &chunk, &chunk_len)) > 0) {
4456  * printf("var: %s, file_name: %s, size: %d, chunk: [%.*s]\n",
4457  * var_name, file_name, (int) chunk_len,
4458  * (int) chunk_len, chunk);
4459  * n1 += n2;
4460  * }
4461  * }
4462  * break;
4463  * ```
4464  */
4465 size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name,
4466  size_t var_name_len, char *file_name,
4467  size_t file_name_len, const char **chunk,
4468  size_t *chunk_len);
4469 
4470 /*
4471  * Fetches a HTTP form variable.
4472  *
4473  * Fetches a variable `name` from a `buf` into a buffer specified by `dst`,
4474  * `dst_len`. The destination is always zero-terminated. Returns the length of
4475  * a fetched variable. If not found, 0 is returned. `buf` must be valid
4476  * url-encoded buffer. If destination is too small, `-1` is returned.
4477  */
4478 int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst,
4479  size_t dst_len);
4480 
4481 #if MG_ENABLE_FILESYSTEM
4482 /*
4483  * This structure defines how `mg_serve_http()` works.
4484  * Best practice is to set only required settings, and leave the rest as NULL.
4485  */
4486 struct mg_serve_http_opts {
4487  /* Path to web root directory */
4488  const char *document_root;
4489 
4490  /* List of index files. Default is "" */
4491  const char *index_files;
4492 
4493  /*
4494  * Leave as NULL to disable authentication.
4495  * To enable directory protection with authentication, set this to ".htpasswd"
4496  * Then, creating ".htpasswd" file in any directory automatically protects
4497  * it with digest authentication.
4498  * Use `mongoose` web server binary, or `htdigest` Apache utility to
4499  * create/manipulate passwords file.
4500  * Make sure `auth_domain` is set to a valid domain name.
4501  */
4502  const char *per_directory_auth_file;
4503 
4504  /* Authorization domain (domain name of this web server) */
4505  const char *auth_domain;
4506 
4507  /*
4508  * Leave as NULL to disable authentication.
4509  * Normally, only selected directories in the document root are protected.
4510  * If absolutely every access to the web server needs to be authenticated,
4511  * regardless of the URI, set this option to the path to the passwords file.
4512  * Format of that file is the same as ".htpasswd" file. Make sure that file
4513  * is located outside document root to prevent people fetching it.
4514  */
4515  const char *global_auth_file;
4516 
4517  /* Set to "no" to disable directory listing. Enabled by default. */
4518  const char *enable_directory_listing;
4519 
4520  /*
4521  * SSI files pattern. If not set, "**.shtml$|**.shtm$" is used.
4522  *
4523  * All files that match ssi_pattern are treated as SSI.
4524  *
4525  * Server Side Includes (SSI) is a simple interpreted server-side scripting
4526  * language which is most commonly used to include the contents of a file
4527  * into a web page. It can be useful when it is desirable to include a common
4528  * piece of code throughout a website, for example, headers and footers.
4529  *
4530  * In order for a webpage to recognize an SSI-enabled HTML file, the
4531  * filename should end with a special extension, by default the extension
4532  * should be either .shtml or .shtm
4533  *
4534  * Unknown SSI directives are silently ignored by Mongoose. Currently,
4535  * the following SSI directives are supported:
4536  * &lt;!--#include FILE_TO_INCLUDE --&gt;
4537  * &lt;!--#exec "COMMAND_TO_EXECUTE" --&gt;
4538  * &lt;!--#call COMMAND --&gt;
4539  *
4540  * Note that &lt;!--#include ...> directive supports three path
4541  *specifications:
4542  *
4543  * &lt;!--#include virtual="path" --&gt; Path is relative to web server root
4544  * &lt;!--#include abspath="path" --&gt; Path is absolute or relative to the
4545  * web server working dir
4546  * &lt;!--#include file="path" --&gt;, Path is relative to current document
4547  * &lt;!--#include "path" --&gt;
4548  *
4549  * The include directive may be used to include the contents of a file or
4550  * the result of running a CGI script.
4551  *
4552  * The exec directive is used to execute
4553  * a command on a server, and show command's output. Example:
4554  *
4555  * &lt;!--#exec "ls -l" --&gt;
4556  *
4557  * The call directive is a way to invoke a C handler from the HTML page.
4558  * On each occurence of &lt;!--#call COMMAND OPTIONAL_PARAMS> directive,
4559  * Mongoose calls a registered event handler with MG_EV_SSI_CALL event,
4560  * and event parameter will point to the COMMAND OPTIONAL_PARAMS string.
4561  * An event handler can output any text, for example by calling
4562  * `mg_printf()`. This is a flexible way of generating a web page on
4563  * server side by calling a C event handler. Example:
4564  *
4565  * &lt;!--#call foo --&gt; ... &lt;!--#call bar --&gt;
4566  *
4567  * In the event handler:
4568  * case MG_EV_SSI_CALL: {
4569  * const char *param = (const char *) ev_data;
4570  * if (strcmp(param, "foo") == 0) {
4571  * mg_printf(c, "hello from foo");
4572  * } else if (strcmp(param, "bar") == 0) {
4573  * mg_printf(c, "hello from bar");
4574  * }
4575  * break;
4576  * }
4577  */
4578  const char *ssi_pattern;
4579 
4580  /* IP ACL. By default, NULL, meaning all IPs are allowed to connect */
4581  const char *ip_acl;
4582 
4583 #if MG_ENABLE_HTTP_URL_REWRITES
4584  /* URL rewrites.
4585  *
4586  * Comma-separated list of `uri_pattern=url_file_or_directory_path` rewrites.
4587  * When HTTP request is received, Mongoose constructs a file name from the
4588  * requested URI by combining `document_root` and the URI. However, if the
4589  * rewrite option is used and `uri_pattern` matches requested URI, then
4590  * `document_root` is ignored. Instead, `url_file_or_directory_path` is used,
4591  * which should be a full path name or a path relative to the web server's
4592  * current working directory. It can also be an URI (http:// or https://)
4593  * in which case mongoose will behave as a reverse proxy for that destination.
4594  *
4595  * Note that `uri_pattern`, as all Mongoose patterns, is a prefix pattern.
4596  *
4597  * If uri_pattern starts with `@` symbol, then Mongoose compares it with the
4598  * HOST header of the request. If they are equal, Mongoose sets document root
4599  * to `file_or_directory_path`, implementing virtual hosts support.
4600  * Example: `@foo.com=/document/root/for/foo.com`
4601  *
4602  * If `uri_pattern` starts with `%` symbol, then Mongoose compares it with
4603  * the listening port. If they match, then Mongoose issues a 301 redirect.
4604  * For example, to redirect all HTTP requests to the
4605  * HTTPS port, do `%80=https://my.site.com`. Note that the request URI is
4606  * automatically appended to the redirect location.
4607  */
4608  const char *url_rewrites;
4609 #endif
4610 
4611  /* DAV document root. If NULL, DAV requests are going to fail. */
4612  const char *dav_document_root;
4613 
4614  /*
4615  * DAV passwords file. If NULL, DAV requests are going to fail.
4616  * If passwords file is set to "-", then DAV auth is disabled.
4617  */
4618  const char *dav_auth_file;
4619 
4620  /* Glob pattern for the files to hide. */
4621  const char *hidden_file_pattern;
4622 
4623  /* Set to non-NULL to enable CGI, e.g. **.cgi$|**.php$" */
4624  const char *cgi_file_pattern;
4625 
4626  /* If not NULL, ignore CGI script hashbang and use this interpreter */
4627  const char *cgi_interpreter;
4628 
4629  /*
4630  * Comma-separated list of Content-Type overrides for path suffixes, e.g.
4631  * ".txt=text/plain; charset=utf-8,.c=text/plain"
4632  */
4633  const char *custom_mime_types;
4634 
4635  /*
4636  * Extra HTTP headers to add to each server response.
4637  * Example: to enable CORS, set this to "Access-Control-Allow-Origin: *".
4638  */
4639  const char *extra_headers;
4640 };
4641 
4642 /*
4643  * Serves given HTTP request according to the `options`.
4644  *
4645  * Example code snippet:
4646  *
4647  * ```c
4648  * static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
4649  * struct http_message *hm = (struct http_message *) ev_data;
4650  * struct mg_serve_http_opts opts = { .document_root = "/var/www" }; // C99
4651  *
4652  * switch (ev) {
4653  * case MG_EV_HTTP_REQUEST:
4654  * mg_serve_http(nc, hm, opts);
4655  * break;
4656  * default:
4657  * break;
4658  * }
4659  * }
4660  * ```
4661  */
4662 void mg_serve_http(struct mg_connection *nc, struct http_message *hm,
4663  struct mg_serve_http_opts opts);
4664 
4665 /*
4666  * Serves a specific file with a given MIME type and optional extra headers.
4667  *
4668  * Example code snippet:
4669  *
4670  * ```c
4671  * static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
4672  * switch (ev) {
4673  * case MG_EV_HTTP_REQUEST: {
4674  * struct http_message *hm = (struct http_message *) ev_data;
4675  * mg_http_serve_file(nc, hm, "file.txt",
4676  * mg_mk_str("text/plain"), mg_mk_str(""));
4677  * break;
4678  * }
4679  * ...
4680  * }
4681  * }
4682  * ```
4683  */
4684 void mg_http_serve_file(struct mg_connection *nc, struct http_message *hm,
4685  const char *path, const struct mg_str mime_type,
4686  const struct mg_str extra_headers);
4687 
4688 #if MG_ENABLE_HTTP_STREAMING_MULTIPART
4689 
4690 /* Callback prototype for `mg_file_upload_handler()`. */
4691 typedef struct mg_str (*mg_fu_fname_fn)(struct mg_connection *nc,
4692  struct mg_str fname);
4693 
4694 /*
4695  * File upload handler.
4696  * This handler can be used to implement file uploads with minimum code.
4697  * This handler will process MG_EV_HTTP_PART_* events and store file data into
4698  * a local file.
4699  * `local_name_fn` will be invoked with whatever name was provided by the client
4700  * and will expect the name of the local file to open. A return value of NULL
4701  * will abort file upload (client will get a "403 Forbidden" response). If
4702  * non-null, the returned string must be heap-allocated and will be freed by
4703  * the caller.
4704  * Exception: it is ok to return the same string verbatim.
4705  *
4706  * Example:
4707  *
4708  * ```c
4709  * struct mg_str upload_fname(struct mg_connection *nc, struct mg_str fname) {
4710  * // Just return the same filename. Do not actually do this except in test!
4711  * // fname is user-controlled and needs to be sanitized.
4712  * return fname;
4713  * }
4714  * void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
4715  * switch (ev) {
4716  * ...
4717  * case MG_EV_HTTP_PART_BEGIN:
4718  * case MG_EV_HTTP_PART_DATA:
4719  * case MG_EV_HTTP_PART_END:
4720  * mg_file_upload_handler(nc, ev, ev_data, upload_fname);
4721  * break;
4722  * }
4723  * }
4724  * ```
4725  */
4726 void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data,
4727  mg_fu_fname_fn local_name_fn);
4728 #endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */
4729 #endif /* MG_ENABLE_FILESYSTEM */
4730 
4731 /*
4732  * Registers a callback for a specified http endpoint
4733  * Note: if callback is registered it is called instead of the
4734  * callback provided in mg_bind
4735  *
4736  * Example code snippet:
4737  *
4738  * ```c
4739  * static void handle_hello1(struct mg_connection *nc, int ev, void *ev_data) {
4740  * (void) ev; (void) ev_data;
4741  * mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[I am Hello1]");
4742  * nc->flags |= MG_F_SEND_AND_CLOSE;
4743  * }
4744  *
4745  * static void handle_hello1(struct mg_connection *nc, int ev, void *ev_data) {
4746  * (void) ev; (void) ev_data;
4747  * mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[I am Hello2]");
4748  * nc->flags |= MG_F_SEND_AND_CLOSE;
4749  * }
4750  *
4751  * void init() {
4752  * nc = mg_bind(&mgr, local_addr, cb1);
4753  * mg_register_http_endpoint(nc, "/hello1", handle_hello1);
4754  * mg_register_http_endpoint(nc, "/hello1/hello2", handle_hello2);
4755  * }
4756  * ```
4757  */
4758 void mg_register_http_endpoint(struct mg_connection *nc, const char *uri_path,
4759  mg_event_handler_t handler);
4760 
4761 /*
4762  * Authenticates a HTTP request against an opened password file.
4763  * Returns 1 if authenticated, 0 otherwise.
4764  */
4765 int mg_http_check_digest_auth(struct http_message *hm, const char *auth_domain,
4766  FILE *fp);
4767 
4768 /*
4769  * Sends buffer `buf` of size `len` to the client using chunked HTTP encoding.
4770  * This function sends the buffer size as hex number + newline first, then
4771  * the buffer itself, then the newline. For example,
4772  * `mg_send_http_chunk(nc, "foo", 3)` whill append the `3\r\nfoo\r\n` string
4773  * to the `nc->send_mbuf` output IO buffer.
4774  *
4775  * NOTE: The HTTP header "Transfer-Encoding: chunked" should be sent prior to
4776  * using this function.
4777  *
4778  * NOTE: do not forget to send an empty chunk at the end of the response,
4779  * to tell the client that everything was sent. Example:
4780  *
4781  * ```
4782  * mg_printf_http_chunk(nc, "%s", "my response!");
4783  * mg_send_http_chunk(nc, "", 0); // Tell the client we're finished
4784  * ```
4785  */
4786 void mg_send_http_chunk(struct mg_connection *nc, const char *buf, size_t len);
4787 
4788 /*
4789  * Sends a printf-formatted HTTP chunk.
4790  * Functionality is similar to `mg_send_http_chunk()`.
4791  */
4792 void mg_printf_http_chunk(struct mg_connection *nc, const char *fmt, ...);
4793 
4794 /*
4795  * Sends the response status line.
4796  * If `extra_headers` is not NULL, then `extra_headers` are also sent
4797  * after the reponse line. `extra_headers` must NOT end end with new line.
4798  * Example:
4799  *
4800  * mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *");
4801  *
4802  * Will result in:
4803  *
4804  * HTTP/1.1 200 OK\r\n
4805  * Access-Control-Allow-Origin: *\r\n
4806  */
4807 void mg_send_response_line(struct mg_connection *nc, int status_code,
4808  const char *extra_headers);
4809 
4810 /*
4811  * Sends an error response. If reason is NULL, the message will be inferred
4812  * from the error code (if supported).
4813  */
4814 void mg_http_send_error(struct mg_connection *nc, int code, const char *reason);
4815 
4816 /*
4817  * Sends a redirect response.
4818  * `status_code` should be either 301 or 302 and `location` point to the
4819  * new location.
4820  * If `extra_headers` is not empty, then `extra_headers` are also sent
4821  * after the reponse line. `extra_headers` must NOT end end with new line.
4822  *
4823  * Example:
4824  *
4825  * mg_http_send_redirect(nc, 302, mg_mk_str("/login"), mg_mk_str(NULL));
4826  */
4827 void mg_http_send_redirect(struct mg_connection *nc, int status_code,
4828  const struct mg_str location,
4829  const struct mg_str extra_headers);
4830 
4831 /*
4832  * Sends the response line and headers.
4833  * This function sends the response line with the `status_code`, and
4834  * automatically
4835  * sends one header: either "Content-Length" or "Transfer-Encoding".
4836  * If `content_length` is negative, then "Transfer-Encoding: chunked" header
4837  * is sent, otherwise, "Content-Length" header is sent.
4838  *
4839  * NOTE: If `Transfer-Encoding` is `chunked`, then message body must be sent
4840  * using `mg_send_http_chunk()` or `mg_printf_http_chunk()` functions.
4841  * Otherwise, `mg_send()` or `mg_printf()` must be used.
4842  * Extra headers could be set through `extra_headers`. Note `extra_headers`
4843  * must NOT be terminated by a new line.
4844  */
4845 void mg_send_head(struct mg_connection *n, int status_code,
4846  int64_t content_length, const char *extra_headers);
4847 
4848 /*
4849  * Sends a printf-formatted HTTP chunk, escaping HTML tags.
4850  */
4851 void mg_printf_html_escape(struct mg_connection *nc, const char *fmt, ...);
4852 
4853 #if MG_ENABLE_HTTP_URL_REWRITES
4854 /*
4855  * Proxies a given request to a given upstream http server. The path prefix
4856  * in `mount` will be stripped of the path requested to the upstream server,
4857  * e.g. if mount is /api and upstream is http://localhost:8001/foo
4858  * then an incoming request to /api/bar will cause a request to
4859  * http://localhost:8001/foo/bar
4860  *
4861  * EXPERIMENTAL API. Please use http_serve_http + url_rewrites if a static
4862  * mapping is good enough.
4863  */
4864 void mg_http_reverse_proxy(struct mg_connection *nc,
4865  const struct http_message *hm, struct mg_str mount,
4866  struct mg_str upstream);
4867 #endif
4868 
4869 #ifdef __cplusplus
4870 }
4871 #endif /* __cplusplus */
4872 
4873 #endif /* MG_ENABLE_HTTP */
4874 
4875 #endif /* CS_MONGOOSE_SRC_HTTP_SERVER_H_ */
4876 #ifdef MG_MODULE_LINES
4877 #line 1 "mongoose/src/http_client.h"
4878 #endif
4879 /*
4880  * === Client API reference
4881  */
4882 
4883 #ifndef CS_MONGOOSE_SRC_HTTP_CLIENT_H_
4884 #define CS_MONGOOSE_SRC_HTTP_CLIENT_H_
4885 
4886 #ifdef __cplusplus
4887 extern "C" {
4888 #endif /* __cplusplus */
4889 
4890 /*
4891  * Helper function that creates an outbound HTTP connection.
4892  *
4893  * `url` is the URL to fetch. It must be properly URL-encoded, e.g. have
4894  * no spaces, etc. By default, `mg_connect_http()` sends the Connection and
4895  * Host headers. `extra_headers` is an extra HTTP header to send, e.g.
4896  * `"User-Agent: my-app\r\n"`.
4897  * If `post_data` is NULL, then a GET request is created. Otherwise, a POST
4898  * request is created with the specified POST data. Note that if the data being
4899  * posted is a form submission, the `Content-Type` header should be set
4900  * accordingly (see example below).
4901  *
4902  * Examples:
4903  *
4904  * ```c
4905  * nc1 = mg_connect_http(mgr, ev_handler_1, "http://www.google.com", NULL,
4906  * NULL);
4907  * nc2 = mg_connect_http(mgr, ev_handler_1, "https://github.com", NULL, NULL);
4908  * nc3 = mg_connect_http(
4909  * mgr, ev_handler_1, "my_server:8000/form_submit/",
4910  * "Content-Type: application/x-www-form-urlencoded\r\n",
4911  * "var_1=value_1&var_2=value_2");
4912  * ```
4913  */
4914 struct mg_connection *mg_connect_http(struct mg_mgr *mgr,
4915  mg_event_handler_t event_handler,
4916  const char *url,
4917  const char *extra_headers,
4918  const char *post_data);
4919 
4920 /*
4921  * Helper function that creates an outbound HTTP connection.
4922  *
4923  * Mostly identical to mg_connect_http, but allows you to provide extra
4924  *parameters
4925  * (for example, SSL parameters)
4926  */
4927 struct mg_connection *mg_connect_http_opt(struct mg_mgr *mgr,
4929  struct mg_connect_opts opts,
4930  const char *url,
4931  const char *extra_headers,
4932  const char *post_data);
4933 
4934 /* Creates digest authentication header for a client request. */
4935 int mg_http_create_digest_auth_header(char *buf, size_t buf_len,
4936  const char *method, const char *uri,
4937  const char *auth_domain, const char *user,
4938  const char *passwd);
4939 
4940 #ifdef __cplusplus
4941 }
4942 #endif /* __cplusplus */
4943 #endif /* CS_MONGOOSE_SRC_HTTP_CLIENT_H_ */
4944 #ifdef MG_MODULE_LINES
4945 #line 1 "mongoose/src/mqtt.h"
4946 #endif
4947 /*
4948  * Copyright (c) 2014 Cesanta Software Limited
4949  * All rights reserved
4950  * This software is dual-licensed: you can redistribute it and/or modify
4951  * it under the terms of the GNU General Public License version 2 as
4952  * published by the Free Software Foundation. For the terms of this
4953  * license, see <http://www.gnu.org/licenses/>.
4954  *
4955  * You are free to use this software under the terms of the GNU General
4956  * Public License, but WITHOUT ANY WARRANTY; without even the implied
4957  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
4958  * See the GNU General Public License for more details.
4959  *
4960  * Alternatively, you can license this software under a commercial
4961  * license, as set out in <https://www.cesanta.com/license>.
4962  */
4963 
4964 /*
4965  * === MQTT API reference
4966  */
4967 
4968 #ifndef CS_MONGOOSE_SRC_MQTT_H_
4969 #define CS_MONGOOSE_SRC_MQTT_H_
4970 
4971 /* Amalgamated: #include "mongoose/src/net.h" */
4972 
4974  int cmd;
4975  int qos;
4976  struct mg_str topic;
4977  struct mg_str payload;
4978 
4979  uint8_t connack_ret_code; /* connack */
4980  uint16_t message_id; /* puback */
4981 
4982  /* connect */
4984  uint8_t connect_flags;
4986  struct mg_str protocol_name;
4987  struct mg_str client_id;
4988  struct mg_str will_topic;
4989  struct mg_str will_message;
4990  struct mg_str user_name;
4991  struct mg_str password;
4992 };
4993 
4995  const char *topic;
4996  uint8_t qos;
4997 };
4998 
5000  unsigned char flags; /* connection flags */
5001  uint16_t keep_alive;
5002  const char *will_topic;
5003  const char *will_message;
5004  const char *user_name;
5005  const char *password;
5006 };
5007 
5008 /* mg_mqtt_proto_data should be in header to allow external access to it */
5010  uint16_t keep_alive;
5011 };
5012 
5013 /* Message types */
5014 #define MG_MQTT_CMD_CONNECT 1
5015 #define MG_MQTT_CMD_CONNACK 2
5016 #define MG_MQTT_CMD_PUBLISH 3
5017 #define MG_MQTT_CMD_PUBACK 4
5018 #define MG_MQTT_CMD_PUBREC 5
5019 #define MG_MQTT_CMD_PUBREL 6
5020 #define MG_MQTT_CMD_PUBCOMP 7
5021 #define MG_MQTT_CMD_SUBSCRIBE 8
5022 #define MG_MQTT_CMD_SUBACK 9
5023 #define MG_MQTT_CMD_UNSUBSCRIBE 10
5024 #define MG_MQTT_CMD_UNSUBACK 11
5025 #define MG_MQTT_CMD_PINGREQ 12
5026 #define MG_MQTT_CMD_PINGRESP 13
5027 #define MG_MQTT_CMD_DISCONNECT 14
5028 
5029 /* MQTT event types */
5030 #define MG_MQTT_EVENT_BASE 200
5031 #define MG_EV_MQTT_CONNECT (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_CONNECT)
5032 #define MG_EV_MQTT_CONNACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_CONNACK)
5033 #define MG_EV_MQTT_PUBLISH (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBLISH)
5034 #define MG_EV_MQTT_PUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBACK)
5035 #define MG_EV_MQTT_PUBREC (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBREC)
5036 #define MG_EV_MQTT_PUBREL (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBREL)
5037 #define MG_EV_MQTT_PUBCOMP (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBCOMP)
5038 #define MG_EV_MQTT_SUBSCRIBE (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_SUBSCRIBE)
5039 #define MG_EV_MQTT_SUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_SUBACK)
5040 #define MG_EV_MQTT_UNSUBSCRIBE (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_UNSUBSCRIBE)
5041 #define MG_EV_MQTT_UNSUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_UNSUBACK)
5042 #define MG_EV_MQTT_PINGREQ (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PINGREQ)
5043 #define MG_EV_MQTT_PINGRESP (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PINGRESP)
5044 #define MG_EV_MQTT_DISCONNECT (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_DISCONNECT)
5045 
5046 /* Message flags */
5047 #define MG_MQTT_RETAIN 0x1
5048 #define MG_MQTT_DUP 0x4
5049 #define MG_MQTT_QOS(qos) ((qos) << 1)
5050 #define MG_MQTT_GET_QOS(flags) (((flags) &0x6) >> 1)
5051 #define MG_MQTT_SET_QOS(flags, qos) (flags) = ((flags) & ~0x6) | ((qos) << 1)
5052 
5053 /* Connection flags */
5054 #define MG_MQTT_CLEAN_SESSION 0x02
5055 #define MG_MQTT_HAS_WILL 0x04
5056 #define MG_MQTT_WILL_RETAIN 0x20
5057 #define MG_MQTT_HAS_PASSWORD 0x40
5058 #define MG_MQTT_HAS_USER_NAME 0x80
5059 #define MG_MQTT_GET_WILL_QOS(flags) (((flags) &0x18) >> 3)
5060 #define MG_MQTT_SET_WILL_QOS(flags, qos) \
5061  (flags) = ((flags) & ~0x18) | ((qos) << 3)
5062 
5063 /* CONNACK return codes */
5064 #define MG_EV_MQTT_CONNACK_ACCEPTED 0
5065 #define MG_EV_MQTT_CONNACK_UNACCEPTABLE_VERSION 1
5066 #define MG_EV_MQTT_CONNACK_IDENTIFIER_REJECTED 2
5067 #define MG_EV_MQTT_CONNACK_SERVER_UNAVAILABLE 3
5068 #define MG_EV_MQTT_CONNACK_BAD_AUTH 4
5069 #define MG_EV_MQTT_CONNACK_NOT_AUTHORIZED 5
5070 
5071 #ifdef __cplusplus
5072 extern "C" {
5073 #endif /* __cplusplus */
5074 
5075 /*
5076  * Attaches a built-in MQTT event handler to the given connection.
5077  *
5078  * The user-defined event handler will receive following extra events:
5079  *
5080  * - MG_EV_MQTT_CONNACK
5081  * - MG_EV_MQTT_PUBLISH
5082  * - MG_EV_MQTT_PUBACK
5083  * - MG_EV_MQTT_PUBREC
5084  * - MG_EV_MQTT_PUBREL
5085  * - MG_EV_MQTT_PUBCOMP
5086  * - MG_EV_MQTT_SUBACK
5087  */
5088 void mg_set_protocol_mqtt(struct mg_connection *nc);
5089 
5090 /* Sends an MQTT handshake. */
5091 void mg_send_mqtt_handshake(struct mg_connection *nc, const char *client_id);
5092 
5093 /* Sends an MQTT handshake with optional parameters. */
5094 void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id,
5096 
5097 /* Publishes a message to a given topic. */
5098 void mg_mqtt_publish(struct mg_connection *nc, const char *topic,
5099  uint16_t message_id, int flags, const void *data,
5100  size_t len);
5101 
5102 /* Subscribes to a bunch of topics. */
5103 void mg_mqtt_subscribe(struct mg_connection *nc,
5104  const struct mg_mqtt_topic_expression *topics,
5105  size_t topics_len, uint16_t message_id);
5106 
5107 /* Unsubscribes from a bunch of topics. */
5108 void mg_mqtt_unsubscribe(struct mg_connection *nc, char **topics,
5109  size_t topics_len, uint16_t message_id);
5110 
5111 /* Sends a DISCONNECT command. */
5112 void mg_mqtt_disconnect(struct mg_connection *nc);
5113 
5114 /* Sends a CONNACK command with a given `return_code`. */
5115 void mg_mqtt_connack(struct mg_connection *nc, uint8_t return_code);
5116 
5117 /* Sends a PUBACK command with a given `message_id`. */
5118 void mg_mqtt_puback(struct mg_connection *nc, uint16_t message_id);
5119 
5120 /* Sends a PUBREC command with a given `message_id`. */
5121 void mg_mqtt_pubrec(struct mg_connection *nc, uint16_t message_id);
5122 
5123 /* Sends a PUBREL command with a given `message_id`. */
5124 void mg_mqtt_pubrel(struct mg_connection *nc, uint16_t message_id);
5125 
5126 /* Sends a PUBCOMP command with a given `message_id`. */
5127 void mg_mqtt_pubcomp(struct mg_connection *nc, uint16_t message_id);
5128 
5129 /*
5130  * Sends a SUBACK command with a given `message_id`
5131  * and a sequence of granted QoSs.
5132  */
5133 void mg_mqtt_suback(struct mg_connection *nc, uint8_t *qoss, size_t qoss_len,
5134  uint16_t message_id);
5135 
5136 /* Sends a UNSUBACK command with a given `message_id`. */
5137 void mg_mqtt_unsuback(struct mg_connection *nc, uint16_t message_id);
5138 
5139 /* Sends a PINGREQ command. */
5140 void mg_mqtt_ping(struct mg_connection *nc);
5141 
5142 /* Sends a PINGRESP command. */
5143 void mg_mqtt_pong(struct mg_connection *nc);
5144 
5145 /*
5146  * Extracts the next topic expression from a SUBSCRIBE command payload.
5147  *
5148  * The topic expression name will point to a string in the payload buffer.
5149  * Returns the pos of the next topic expression or -1 when the list
5150  * of topics is exhausted.
5151  */
5153  struct mg_str *topic, uint8_t *qos, int pos);
5154 
5155 #ifdef __cplusplus
5156 }
5157 #endif /* __cplusplus */
5158 
5159 #endif /* CS_MONGOOSE_SRC_MQTT_H_ */
5160 #ifdef MG_MODULE_LINES
5161 #line 1 "mongoose/src/mqtt_server.h"
5162 #endif
5163 /*
5164  * Copyright (c) 2014 Cesanta Software Limited
5165  * All rights reserved
5166  * This software is dual-licensed: you can redistribute it and/or modify
5167  * it under the terms of the GNU General Public License version 2 as
5168  * published by the Free Software Foundation. For the terms of this
5169  * license, see <http://www.gnu.org/licenses/>.
5170  *
5171  * You are free to use this software under the terms of the GNU General
5172  * Public License, but WITHOUT ANY WARRANTY; without even the implied
5173  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5174  * See the GNU General Public License for more details.
5175  *
5176  * Alternatively, you can license this software under a commercial
5177  * license, as set out in <https://www.cesanta.com/license>.
5178  */
5179 
5180 /*
5181  * === MQTT Server API reference
5182  */
5183 
5184 #ifndef CS_MONGOOSE_SRC_MQTT_BROKER_H_
5185 #define CS_MONGOOSE_SRC_MQTT_BROKER_H_
5186 
5187 #if MG_ENABLE_MQTT_BROKER
5188 
5189 /* Amalgamated: #include "common/queue.h" */
5190 /* Amalgamated: #include "mongoose/src/mqtt.h" */
5191 
5192 #ifdef __cplusplus
5193 extern "C" {
5194 #endif /* __cplusplus */
5195 
5196 #define MG_MQTT_MAX_SESSION_SUBSCRIPTIONS 512;
5197 
5198 struct mg_mqtt_broker;
5199 
5200 /* MQTT session (Broker side). */
5201 struct mg_mqtt_session {
5202  struct mg_mqtt_broker *brk; /* Broker */
5203  LIST_ENTRY(mg_mqtt_session) link; /* mg_mqtt_broker::sessions linkage */
5204  struct mg_connection *nc; /* Connection with the client */
5205  size_t num_subscriptions; /* Size of `subscriptions` array */
5206  void *user_data; /* User data */
5207  struct mg_mqtt_topic_expression *subscriptions;
5208 };
5209 
5210 /* MQTT broker. */
5211 struct mg_mqtt_broker {
5212  LIST_HEAD(_mg_sesshead, mg_mqtt_session) sessions; /* Session list */
5213  void *user_data; /* User data */
5214 };
5215 
5216 /* Initialises a MQTT broker. */
5217 void mg_mqtt_broker_init(struct mg_mqtt_broker *brk, void *user_data);
5218 
5219 /*
5220  * Processes a MQTT broker message.
5221  *
5222  * The listening connection expects a pointer to an initialised
5223  * `mg_mqtt_broker` structure in the `user_data` field.
5224  *
5225  * Basic usage:
5226  *
5227  * ```c
5228  * mg_mqtt_broker_init(&brk, NULL);
5229  *
5230  * if ((nc = mg_bind(&mgr, address, mg_mqtt_broker)) == NULL) {
5231  * // fail;
5232  * }
5233  * nc->user_data = &brk;
5234  * ```
5235  *
5236  * New incoming connections will receive a `mg_mqtt_session` structure
5237  * in the connection `user_data`. The original `user_data` will be stored
5238  * in the `user_data` field of the session structure. This allows the user
5239  * handler to store user data before `mg_mqtt_broker` creates the session.
5240  *
5241  * Since only the MG_EV_ACCEPT message is processed by the listening socket,
5242  * for most events the `user_data` will thus point to a `mg_mqtt_session`.
5243  */
5244 void mg_mqtt_broker(struct mg_connection *brk, int ev, void *data);
5245 
5246 /*
5247  * Iterates over all MQTT session connections. Example:
5248  *
5249  * ```c
5250  * struct mg_mqtt_session *s;
5251  * for (s = mg_mqtt_next(brk, NULL); s != NULL; s = mg_mqtt_next(brk, s)) {
5252  * // Do something
5253  * }
5254  * ```
5255  */
5256 struct mg_mqtt_session *mg_mqtt_next(struct mg_mqtt_broker *brk,
5257  struct mg_mqtt_session *s);
5258 
5259 #ifdef __cplusplus
5260 }
5261 #endif /* __cplusplus */
5262 
5263 #endif /* MG_ENABLE_MQTT_BROKER */
5264 #endif /* CS_MONGOOSE_SRC_MQTT_BROKER_H_ */
5265 #ifdef MG_MODULE_LINES
5266 #line 1 "mongoose/src/dns.h"
5267 #endif
5268 /*
5269  * Copyright (c) 2014 Cesanta Software Limited
5270  * All rights reserved
5271  */
5272 
5273 /*
5274  * === DNS API reference
5275  */
5276 
5277 #ifndef CS_MONGOOSE_SRC_DNS_H_
5278 #define CS_MONGOOSE_SRC_DNS_H_
5279 
5280 /* Amalgamated: #include "mongoose/src/net.h" */
5281 
5282 #ifdef __cplusplus
5283 extern "C" {
5284 #endif /* __cplusplus */
5285 
5286 #define MG_DNS_A_RECORD 0x01 /* Lookup IP address */
5287 #define MG_DNS_CNAME_RECORD 0x05 /* Lookup CNAME */
5288 #define MG_DNS_PTR_RECORD 0x0c /* Lookup PTR */
5289 #define MG_DNS_TXT_RECORD 0x10 /* Lookup TXT */
5290 #define MG_DNS_AAAA_RECORD 0x1c /* Lookup IPv6 address */
5291 #define MG_DNS_SRV_RECORD 0x21 /* Lookup SRV */
5292 #define MG_DNS_MX_RECORD 0x0f /* Lookup mail server for domain */
5293 
5294 #define MG_MAX_DNS_QUESTIONS 32
5295 #define MG_MAX_DNS_ANSWERS 32
5296 
5297 #define MG_DNS_MESSAGE 100 /* High-level DNS message event */
5298 
5303 };
5304 
5305 /* DNS resource record. */
5307  struct mg_str name; /* buffer with compressed name */
5308  int rtype;
5309  int rclass;
5310  int ttl;
5312  struct mg_str rdata; /* protocol data (can be a compressed name) */
5313 };
5314 
5315 /* DNS message (request and response). */
5317  struct mg_str pkt; /* packet body */
5318  uint16_t flags;
5319  uint16_t transaction_id;
5324 };
5325 
5327  struct mg_dns_message *msg, int query, struct mg_dns_resource_record *prev);
5328 
5329 /*
5330  * Parses the record data from a DNS resource record.
5331  *
5332  * - A: struct in_addr *ina
5333  * - AAAA: struct in6_addr *ina
5334  * - CNAME: char buffer
5335  *
5336  * Returns -1 on error.
5337  *
5338  * TODO(mkm): MX
5339  */
5341  struct mg_dns_resource_record *rr, void *data,
5342  size_t data_len);
5343 
5344 /*
5345  * Sends a DNS query to the remote end.
5346  */
5347 void mg_send_dns_query(struct mg_connection *nc, const char *name,
5348  int query_type);
5349 
5350 /*
5351  * Inserts a DNS header to an IO buffer.
5352  *
5353  * Returns the number of bytes inserted.
5354  */
5355 int mg_dns_insert_header(struct mbuf *io, size_t pos,
5356  struct mg_dns_message *msg);
5357 
5358 /*
5359  * Appends already encoded questions from an existing message.
5360  *
5361  * This is useful when generating a DNS reply message which includes
5362  * all question records.
5363  *
5364  * Returns the number of appended bytes.
5365  */
5366 int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg);
5367 
5368 /*
5369  * Encodes and appends a DNS resource record to an IO buffer.
5370  *
5371  * The record metadata is taken from the `rr` parameter, while the name and data
5372  * are taken from the parameters, encoded in the appropriate format depending on
5373  * record type and stored in the IO buffer. The encoded values might contain
5374  * offsets within the IO buffer. It's thus important that the IO buffer doesn't
5375  * get trimmed while a sequence of records are encoded while preparing a DNS
5376  * reply.
5377  *
5378  * This function doesn't update the `name` and `rdata` pointers in the `rr`
5379  * struct because they might be invalidated as soon as the IO buffer grows
5380  * again.
5381  *
5382  * Returns the number of bytes appened or -1 in case of error.
5383  */
5384 int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr,
5385  const char *name, size_t nlen, const void *rdata,
5386  size_t rlen);
5387 
5388 /*
5389  * Encodes a DNS name.
5390  */
5391 int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len);
5392 
5393 /* Low-level: parses a DNS response. */
5394 int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg);
5395 
5396 /*
5397  * Uncompresses a DNS compressed name.
5398  *
5399  * The containing DNS message is required because of the compressed encoding
5400  * and reference suffixes present elsewhere in the packet.
5401  *
5402  * If the name is less than `dst_len` characters long, the remainder
5403  * of `dst` is terminated with `\0` characters. Otherwise, `dst` is not
5404  * terminated.
5405  *
5406  * If `dst_len` is 0 `dst` can be NULL.
5407  * Returns the uncompressed name length.
5408  */
5409 size_t mg_dns_uncompress_name(struct mg_dns_message *msg, struct mg_str *name,
5410  char *dst, int dst_len);
5411 
5412 /*
5413  * Attaches a built-in DNS event handler to the given listening connection.
5414  *
5415  * The DNS event handler parses the incoming UDP packets, treating them as DNS
5416  * requests. If an incoming packet gets successfully parsed by the DNS event
5417  * handler, a user event handler will receive an `MG_DNS_REQUEST` event, with
5418  * `ev_data` pointing to the parsed `struct mg_dns_message`.
5419  *
5420  * See
5421  * [captive_dns_server](https://github.com/cesanta/mongoose/tree/master/examples/captive_dns_server)
5422  * example on how to handle DNS request and send DNS reply.
5423  */
5424 void mg_set_protocol_dns(struct mg_connection *nc);
5425 
5426 #ifdef __cplusplus
5427 }
5428 #endif /* __cplusplus */
5429 #endif /* CS_MONGOOSE_SRC_DNS_H_ */
5430 #ifdef MG_MODULE_LINES
5431 #line 1 "mongoose/src/dns_server.h"
5432 #endif
5433 /*
5434  * Copyright (c) 2014 Cesanta Software Limited
5435  * All rights reserved
5436  */
5437 
5438 /*
5439  * === DNS server API reference
5440  *
5441  * Disabled by default; enable with `-DMG_ENABLE_DNS_SERVER`.
5442  */
5443 
5444 #ifndef CS_MONGOOSE_SRC_DNS_SERVER_H_
5445 #define CS_MONGOOSE_SRC_DNS_SERVER_H_
5446 
5447 #if MG_ENABLE_DNS_SERVER
5448 
5449 /* Amalgamated: #include "mongoose/src/dns.h" */
5450 
5451 #ifdef __cplusplus
5452 extern "C" {
5453 #endif /* __cplusplus */
5454 
5455 #define MG_DNS_SERVER_DEFAULT_TTL 3600
5456 
5457 struct mg_dns_reply {
5458  struct mg_dns_message *msg;
5459  struct mbuf *io;
5460  size_t start;
5461 };
5462 
5463 /*
5464  * Creates a DNS reply.
5465  *
5466  * The reply will be based on an existing query message `msg`.
5467  * The query body will be appended to the output buffer.
5468  * "reply + recursion allowed" will be added to the message flags and the
5469  * message's num_answers will be set to 0.
5470  *
5471  * Answer records can be appended with `mg_dns_send_reply` or by lower
5472  * level function defined in the DNS API.
5473  *
5474  * In order to send a reply use `mg_dns_send_reply`.
5475  * It's possible to use a connection's send buffer as reply buffer,
5476  * and it will work for both UDP and TCP connections.
5477  *
5478  * Example:
5479  *
5480  * ```c
5481  * reply = mg_dns_create_reply(&nc->send_mbuf, msg);
5482  * for (i = 0; i < msg->num_questions; i++) {
5483  * rr = &msg->questions[i];
5484  * if (rr->rtype == MG_DNS_A_RECORD) {
5485  * mg_dns_reply_record(&reply, rr, 3600, &dummy_ip_addr, 4);
5486  * }
5487  * }
5488  * mg_dns_send_reply(nc, &reply);
5489  * ```
5490  */
5491 struct mg_dns_reply mg_dns_create_reply(struct mbuf *io,
5492  struct mg_dns_message *msg);
5493 
5494 /*
5495  * Appends a DNS reply record to the IO buffer and to the DNS message.
5496  *
5497  * The message's num_answers field will be incremented. It's the caller's duty
5498  * to ensure num_answers is properly initialised.
5499  *
5500  * Returns -1 on error.
5501  */
5502 int mg_dns_reply_record(struct mg_dns_reply *reply,
5503  struct mg_dns_resource_record *question,
5504  const char *name, int rtype, int ttl, const void *rdata,
5505  size_t rdata_len);
5506 
5507 /*
5508  * Sends a DNS reply through a connection.
5509  *
5510  * The DNS data is stored in an IO buffer pointed by reply structure in `r`.
5511  * This function mutates the content of that buffer in order to ensure that
5512  * the DNS header reflects the size and flags of the message, that might have
5513  * been updated either with `mg_dns_reply_record` or by direct manipulation of
5514  * `r->message`.
5515  *
5516  * Once sent, the IO buffer will be trimmed unless the reply IO buffer
5517  * is the connection's send buffer and the connection is not in UDP mode.
5518  */
5519 void mg_dns_send_reply(struct mg_connection *nc, struct mg_dns_reply *r);
5520 
5521 #ifdef __cplusplus
5522 }
5523 #endif /* __cplusplus */
5524 
5525 #endif /* MG_ENABLE_DNS_SERVER */
5526 #endif /* CS_MONGOOSE_SRC_DNS_SERVER_H_ */
5527 #ifdef MG_MODULE_LINES
5528 #line 1 "mongoose/src/resolv.h"
5529 #endif
5530 /*
5531  * Copyright (c) 2014 Cesanta Software Limited
5532  * All rights reserved
5533  */
5534 
5535 /*
5536  * === API reference
5537  */
5538 
5539 #ifndef CS_MONGOOSE_SRC_RESOLV_H_
5540 #define CS_MONGOOSE_SRC_RESOLV_H_
5541 
5542 /* Amalgamated: #include "mongoose/src/dns.h" */
5543 
5544 #ifdef __cplusplus
5545 extern "C" {
5546 #endif /* __cplusplus */
5547 
5553 };
5554 
5555 typedef void (*mg_resolve_callback_t)(struct mg_dns_message *dns_message,
5556  void *user_data, enum mg_resolve_err);
5557 
5558 /* Options for `mg_resolve_async_opt`. */
5560  const char *nameserver_url;
5561  int max_retries; /* defaults to 2 if zero */
5562  int timeout; /* in seconds; defaults to 5 if zero */
5563  int accept_literal; /* pseudo-resolve literal ipv4 and ipv6 addrs */
5564  int only_literal; /* only resolves literal addrs; sync cb invocation */
5565  struct mg_connection **dns_conn; /* return DNS connection */
5566 };
5567 
5568 /* See `mg_resolve_async_opt()` */
5569 int mg_resolve_async(struct mg_mgr *mgr, const char *name, int query,
5570  mg_resolve_callback_t cb, void *data);
5571 
5572 /*
5573  * Resolved a DNS name asynchronously.
5574  *
5575  * Upon successful resolution, the user callback will be invoked
5576  * with the full DNS response message and a pointer to the user's
5577  * context `data`.
5578  *
5579  * In case of timeout while performing the resolution the callback
5580  * will receive a NULL `msg`.
5581  *
5582  * The DNS answers can be extracted with `mg_next_record` and
5583  * `mg_dns_parse_record_data`:
5584  *
5585  * [source,c]
5586  * ----
5587  * struct in_addr ina;
5588  * struct mg_dns_resource_record *rr = mg_next_record(msg, MG_DNS_A_RECORD,
5589  * NULL);
5590  * mg_dns_parse_record_data(msg, rr, &ina, sizeof(ina));
5591  * ----
5592  */
5593 int mg_resolve_async_opt(struct mg_mgr *mgr, const char *name, int query,
5594  mg_resolve_callback_t cb, void *data,
5595  struct mg_resolve_async_opts opts);
5596 
5597 /*
5598  * Resolve a name from `/etc/hosts`.
5599  *
5600  * Returns 0 on success, -1 on failure.
5601  */
5602 int mg_resolve_from_hosts_file(const char *host, union socket_address *usa);
5603 
5604 #ifdef __cplusplus
5605 }
5606 #endif /* __cplusplus */
5607 #endif /* CS_MONGOOSE_SRC_RESOLV_H_ */
5608 #ifdef MG_MODULE_LINES
5609 #line 1 "mongoose/src/coap.h"
5610 #endif
5611 /*
5612  * Copyright (c) 2015 Cesanta Software Limited
5613  * All rights reserved
5614  * This software is dual-licensed: you can redistribute it and/or modify
5615  * it under the terms of the GNU General Public License version 2 as
5616  * published by the Free Software Foundation. For the terms of this
5617  * license, see <http://www.gnu.org/licenses/>.
5618  *
5619  * You are free to use this software under the terms of the GNU General
5620  * Public License, but WITHOUT ANY WARRANTY; without even the implied
5621  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5622  * See the GNU General Public License for more details.
5623  *
5624  * Alternatively, you can license this software under a commercial
5625  * license, as set out in <https://www.cesanta.com/license>.
5626  */
5627 
5628 /*
5629  * === CoAP API reference
5630  *
5631  * CoAP message format:
5632  *
5633  * ```
5634  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5635  * |Ver| T | TKL | Code | Message ID | Token (if any, TKL bytes) ...
5636  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5637  * | Options (if any) ... |1 1 1 1 1 1 1 1| Payload (if any) ...
5638  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
5639  * ```
5640  */
5641 
5642 #ifndef CS_MONGOOSE_SRC_COAP_H_
5643 #define CS_MONGOOSE_SRC_COAP_H_
5644 
5645 #if MG_ENABLE_COAP
5646 
5647 #define MG_COAP_MSG_TYPE_FIELD 0x2
5648 #define MG_COAP_CODE_CLASS_FIELD 0x4
5649 #define MG_COAP_CODE_DETAIL_FIELD 0x8
5650 #define MG_COAP_MSG_ID_FIELD 0x10
5651 #define MG_COAP_TOKEN_FIELD 0x20
5652 #define MG_COAP_OPTIOMG_FIELD 0x40
5653 #define MG_COAP_PAYLOAD_FIELD 0x80
5654 
5655 #define MG_COAP_ERROR 0x10000
5656 #define MG_COAP_FORMAT_ERROR (MG_COAP_ERROR | 0x20000)
5657 #define MG_COAP_IGNORE (MG_COAP_ERROR | 0x40000)
5658 #define MG_COAP_NOT_ENOUGH_DATA (MG_COAP_ERROR | 0x80000)
5659 #define MG_COAP_NETWORK_ERROR (MG_COAP_ERROR | 0x100000)
5660 
5661 #define MG_COAP_MSG_CON 0
5662 #define MG_COAP_MSG_NOC 1
5663 #define MG_COAP_MSG_ACK 2
5664 #define MG_COAP_MSG_RST 3
5665 #define MG_COAP_MSG_MAX 3
5666 
5667 #define MG_COAP_CODECLASS_REQUEST 0
5668 #define MG_COAP_CODECLASS_RESP_OK 2
5669 #define MG_COAP_CODECLASS_CLIENT_ERR 4
5670 #define MG_COAP_CODECLASS_SRV_ERR 5
5671 
5672 #define MG_COAP_EVENT_BASE 300
5673 #define MG_EV_COAP_CON (MG_COAP_EVENT_BASE + MG_COAP_MSG_CON)
5674 #define MG_EV_COAP_NOC (MG_COAP_EVENT_BASE + MG_COAP_MSG_NOC)
5675 #define MG_EV_COAP_ACK (MG_COAP_EVENT_BASE + MG_COAP_MSG_ACK)
5676 #define MG_EV_COAP_RST (MG_COAP_EVENT_BASE + MG_COAP_MSG_RST)
5677 
5678 /*
5679  * CoAP options.
5680  * Use mg_coap_add_option and mg_coap_free_options
5681  * for creation and destruction.
5682  */
5683 struct mg_coap_option {
5684  struct mg_coap_option *next;
5685  uint32_t number;
5686  struct mg_str value;
5687 };
5688 
5689 /* CoAP message. See RFC 7252 for details. */
5690 struct mg_coap_message {
5691  uint32_t flags;
5692  uint8_t msg_type;
5693  uint8_t code_class;
5694  uint8_t code_detail;
5695  uint16_t msg_id;
5696  struct mg_str token;
5697  struct mg_coap_option *options;
5698  struct mg_str payload;
5699  struct mg_coap_option *optiomg_tail;
5700 };
5701 
5702 #ifdef __cplusplus
5703 extern "C" {
5704 #endif /* __cplusplus */
5705 
5706 /* Sets CoAP protocol handler - triggers CoAP specific events. */
5707 int mg_set_protocol_coap(struct mg_connection *nc);
5708 
5709 /*
5710  * Adds a new option to mg_coap_message structure.
5711  * Returns pointer to the newly created option.
5712  * Note: options must be freed by using mg_coap_free_options
5713  */
5714 struct mg_coap_option *mg_coap_add_option(struct mg_coap_message *cm,
5715  uint32_t number, char *value,
5716  size_t len);
5717 
5718 /*
5719  * Frees the memory allocated for options.
5720  * If the cm paramater doesn't contain any option it does nothing.
5721  */
5722 void mg_coap_free_options(struct mg_coap_message *cm);
5723 
5724 /*
5725  * Composes a CoAP message from `mg_coap_message`
5726  * and sends it into `nc` connection.
5727  * Returns 0 on success. On error, it is a bitmask:
5728  *
5729  * - `#define MG_COAP_ERROR 0x10000`
5730  * - `#define MG_COAP_FORMAT_ERROR (MG_COAP_ERROR | 0x20000)`
5731  * - `#define MG_COAP_IGNORE (MG_COAP_ERROR | 0x40000)`
5732  * - `#define MG_COAP_NOT_ENOUGH_DATA (MG_COAP_ERROR | 0x80000)`
5733  * - `#define MG_COAP_NETWORK_ERROR (MG_COAP_ERROR | 0x100000)`
5734  */
5735 uint32_t mg_coap_send_message(struct mg_connection *nc,
5736  struct mg_coap_message *cm);
5737 
5738 /*
5739  * Composes CoAP acknowledgement from `mg_coap_message`
5740  * and sends it into `nc` connection.
5741  * Return value: see `mg_coap_send_message()`
5742  */
5743 uint32_t mg_coap_send_ack(struct mg_connection *nc, uint16_t msg_id);
5744 
5745 /*
5746  * Parses CoAP message and fills mg_coap_message and returns cm->flags.
5747  * This is a helper function.
5748  *
5749  * NOTE: usually CoAP works over UDP, so lack of data means format error.
5750  * But, in theory, it is possible to use CoAP over TCP (according to RFC)
5751  *
5752  * The caller has to check results and treat COAP_NOT_ENOUGH_DATA according to
5753  * underlying protocol:
5754  *
5755  * - in case of UDP COAP_NOT_ENOUGH_DATA means COAP_FORMAT_ERROR,
5756  * - in case of TCP client can try to receive more data
5757  *
5758  * Return value: see `mg_coap_send_message()`
5759  */
5760 uint32_t mg_coap_parse(struct mbuf *io, struct mg_coap_message *cm);
5761 
5762 /*
5763  * Composes CoAP message from mg_coap_message structure.
5764  * This is a helper function.
5765  * Return value: see `mg_coap_send_message()`
5766  */
5767 uint32_t mg_coap_compose(struct mg_coap_message *cm, struct mbuf *io);
5768 
5769 #ifdef __cplusplus
5770 }
5771 #endif /* __cplusplus */
5772 
5773 #endif /* MG_ENABLE_COAP */
5774 
5775 #endif /* CS_MONGOOSE_SRC_COAP_H_ */
5776 #ifdef MG_MODULE_LINES
5777 #line 1 "mongoose/src/sntp.h"
5778 #endif
5779 /*
5780  * Copyright (c) 2016 Cesanta Software Limited
5781  * All rights reserved
5782  */
5783 
5784 #ifndef CS_MONGOOSE_SRC_SNTP_H_
5785 #define CS_MONGOOSE_SRC_SNTP_H_
5786 
5787 #if MG_ENABLE_SNTP
5788 
5789 #define MG_SNTP_EVENT_BASE 500
5790 
5791 /*
5792  * Received reply from time server. Event handler parameter contains
5793  * pointer to mg_sntp_message structure
5794  */
5795 #define MG_SNTP_REPLY (MG_SNTP_EVENT_BASE + 1)
5796 
5797 /* Received malformed SNTP packet */
5798 #define MG_SNTP_MALFORMED_REPLY (MG_SNTP_EVENT_BASE + 2)
5799 
5800 /* Failed to get time from server (timeout etc) */
5801 #define MG_SNTP_FAILED (MG_SNTP_EVENT_BASE + 3)
5802 
5803 struct mg_sntp_message {
5804  /* if server sends this flags, user should not send requests to it */
5805  int kiss_of_death;
5806  /* usual mg_time */
5807  double time;
5808 };
5809 
5810 /* Establishes connection to given sntp server */
5811 struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr,
5812  mg_event_handler_t event_handler,
5813  const char *sntp_server_name);
5814 
5815 /* Sends time request to given connection */
5816 void mg_sntp_send_request(struct mg_connection *c);
5817 
5818 /*
5819  * Helper function
5820  * Establishes connection to time server, tries to send request
5821  * repeats sending SNTP_ATTEMPTS times every SNTP_TIMEOUT sec
5822  * (if needed)
5823  * See sntp_client example
5824  */
5825 struct mg_connection *mg_sntp_get_time(struct mg_mgr *mgr,
5826  mg_event_handler_t event_handler,
5827  const char *sntp_server_name);
5828 
5829 #endif
5830 
5831 #endif /* CS_MONGOOSE_SRC_SNTP_H_ */
uint16_t keep_alive
Definition: mongoose.h:5001
const char * will_topic
Definition: mongoose.h:5002
struct mg_connection ** dns_conn
Definition: mongoose.h:5565
Definition: mongoose.h:5316
int num_ifaces
Definition: mongoose.h:3352
struct mg_connection * next
Definition: mongoose.h:3266
Definition: mongoose.h:5306
void mg_mbuf_append_base64_putc(char ch, void *user_data)
Definition: mongoose.c:9544
struct mg_str mg_strdup(const struct mg_str s)
Definition: mongoose.c:1158
Definition: mongoose.h:4104
void mg_set_protocol_http_websocket(struct mg_connection *nc)
Definition: mongoose.c:6050
int mg_vprintf(struct mg_connection *, const char *fmt, va_list ap)
Definition: mongoose.c:2238
struct mg_connection * mg_connect_opt(struct mg_mgr *mgr, const char *address, mg_event_handler_t handler, struct mg_connect_opts opts)
Definition: mongoose.c:2652
double ev_timer_time
Definition: mongoose.h:3277
uint16_t message_id
Definition: mongoose.h:4980
void mg_if_connect_cb(struct mg_connection *nc, int err)
Definition: mongoose.c:2589
void mg_if_poll(struct mg_connection *nc, time_t now)
Definition: mongoose.c:2084
void cs_sha1_init(cs_sha1_ctx *)
Definition: mongoose.c:1363
void mg_send_websocket_handshake(struct mg_connection *nc, const char *uri, const char *extra_headers)
Definition: mongoose.c:9151
void mg_if_timer(struct mg_connection *c, double now)
Definition: mongoose.c:2070
uint8_t protocol_version
Definition: mongoose.h:4983
void * data
Definition: mongoose.h:3004
void mbuf_free(struct mbuf *)
Definition: mongoose.c:1035
size_t len
Definition: mongoose.h:1742
Definition: mongoose.h:4999
#define MG_MAX_DNS_QUESTIONS
Definition: mongoose.h:5294
int mg_resolve_async(struct mg_mgr *mgr, const char *name, int query, mg_resolve_callback_t cb, void *data)
Definition: mongoose.c:10797
struct mg_connection * active_connections
Definition: mongoose.h:3247
int mg_dns_parse_record_data(struct mg_dns_message *msg, struct mg_dns_resource_record *rr, void *data, size_t data_len)
Definition: mongoose.c:10170
void cs_base64_finish(struct cs_base64_ctx *ctx)
Definition: mongoose.c:412
Definition: mongoose.h:5549
Definition: mongoose.h:5302
struct mg_connection * mg_connect(struct mg_mgr *mgr, const char *address, mg_event_handler_t handler)
Definition: mongoose.c:2645
size_t mbuf_insert(struct mbuf *, size_t, const void *, size_t)
Definition: mongoose.c:1063
void mg_mqtt_unsubscribe(struct mg_connection *nc, char **topics, size_t topics_len, uint16_t message_id)
Definition: mongoose.c:9870
mg_event_handler_t handler
Definition: mongoose.h:3284
const char * file_name
Definition: mongoose.h:4113
struct mg_iface * iface
Definition: mongoose.h:3296
int cs_base64_decode(const unsigned char *s, int len, char *dst, int *dec_len)
Definition: mongoose.c:522
void MD5_Init(MD5_CTX *c)
Definition: mongoose.c:825
void mbuf_init(struct mbuf *, size_t initial_capacity)
Definition: mongoose.c:1028
void cs_base64_update(struct cs_base64_ctx *ctx, const char *str, size_t len)
Definition: mongoose.c:400
void mg_mqtt_connack(struct mg_connection *nc, uint8_t return_code)
Definition: mongoose.c:9888
const char * topic
Definition: mongoose.h:4995
Definition: mongoose.h:3008
struct MD5Context MD5_CTX
struct mg_connection * mg_connect_ws_opt(struct mg_mgr *mgr, mg_event_handler_t ev_handler, struct mg_connect_opts opts, const char *url, const char *protocol, const char *extra_headers)
Definition: mongoose.c:9157
int c_snprintf(char *buf, size_t buf_size, const char *format,...)
Definition: mongoose.c:1679
struct mg_str name
Definition: mongoose.h:5307
void mg_if_recv_tcp_cb(struct mg_connection *nc, void *buf, int len, int own)
Definition: mongoose.c:2506
int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len)
Definition: mongoose.c:10222
void mg_if_recv_udp_cb(struct mg_connection *nc, void *buf, int len, union socket_address *sa, size_t sa_len)
Definition: mongoose.c:2510
Definition: mongoose.h:1903
void mg_mqtt_puback(struct mg_connection *nc, uint16_t message_id)
Definition: mongoose.c:9907
const char * nameserver_url
Definition: mongoose.h:5560
uint8_t connect_flags
Definition: mongoose.h:4984
Definition: mongoose.h:3419
double cs_time(void)
Definition: mongoose.c:716
double mg_time(void)
Definition: mongoose.c:2954
struct mg_str mg_mk_str(const char *s)
Definition: mongoose.c:1125
struct mg_mgr * mgr
Definition: mongoose.h:3268
int num_ifaces
Definition: mongoose.h:3255
Definition: mongoose.h:3350
void MD5_Final(unsigned char *md, MD5_CTX *c)
Definition: mongoose.h:5009
Definition: mongoose.h:5551
uint16_t keep_alive_timer
Definition: mongoose.h:4985
void * mgr_data
Definition: mongoose.h:3295
void mg_mqtt_suback(struct mg_connection *nc, uint8_t *qoss, size_t qoss_len, uint16_t message_id)
Definition: mongoose.c:9923
int mg_match_prefix(const char *pattern, int pattern_len, const char *str)
Definition: mongoose.c:9530
void mg_if_accept_tcp_cb(struct mg_connection *nc, union socket_address *sa, size_t sa_len)
Definition: mongoose.c:2449
void mg_send_head(struct mg_connection *n, int status_code, int64_t content_length, const char *extra_headers)
Definition: mongoose.c:6221
void mg_mqtt_pong(struct mg_connection *nc)
Definition: mongoose.c:9942
Definition: mongoose.h:3450
struct mg_iface_vtable * mg_ifaces[]
Definition: mongoose.c:3029
void mg_send_mqtt_handshake(struct mg_connection *nc, const char *client_id)
Definition: mongoose.c:9718
int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap)
Definition: mongoose.c:1831
Definition: mongoose.h:1813
mg_event_handler_t f
Definition: mongoose.h:3292
size_t size
Definition: mongoose.h:1743
Definition: mongoose.h:1671
struct mg_connection * mg_add_sock_opt(struct mg_mgr *, sock_t, mg_event_handler_t, struct mg_add_sock_opts)
Definition: mongoose.c:2936
int mg_parse_http(const char *s, int n, struct http_message *hm, int is_req)
Definition: mongoose.c:5283
void mg_send_websocket_handshake2(struct mg_connection *nc, const char *path, const char *host, const char *protocol, const char *extra_headers)
Definition: mongoose.c:9096
int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg)
Definition: mongoose.c:10212
void * user_data
Definition: mongoose.h:3285
void mg_mqtt_pubcomp(struct mg_connection *nc, uint16_t message_id)
Definition: mongoose.c:9919
void * user_data
Definition: mongoose.h:3451
struct mg_connection * mg_bind_opt(struct mg_mgr *mgr, const char *address, mg_event_handler_t handler, struct mg_bind_opts opts)
Definition: mongoose.c:2754
unsigned long flags
Definition: mongoose.h:3297
int only_literal
Definition: mongoose.h:5564
int c_vsnprintf(char *buf, size_t buf_size, const char *format, va_list ap)
Definition: mongoose.c:1541
void mg_printf_http_chunk(struct mg_connection *nc, const char *fmt,...)
Definition: mongoose.c:6461
int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf, size_t buf_size)
Definition: mongoose.c:6508
int timeout
Definition: mongoose.h:5562
void * user_data
Definition: mongoose.h:1908
Definition: mongoose.h:5552
int mg_socketpair(sock_t[2], int sock_type)
Definition: mongoose.h:5300
void mg_printf_websocket_frame(struct mg_connection *nc, int op_and_flags, const char *fmt,...)
Definition: mongoose.c:9020
size_t recv_mbuf_limit
Definition: mongoose.h:3273
struct mg_connection * mg_connect_http_opt(struct mg_mgr *mgr, mg_event_handler_t ev_handler, struct mg_connect_opts opts, const char *url, const char *extra_headers, const char *post_data)
Definition: mongoose.c:7720
sock_t sock
Definition: mongoose.h:3270
struct mg_connection * mg_bind(struct mg_mgr *, const char *, mg_event_handler_t)
Definition: mongoose.c:2747
size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name, size_t var_name_len, char *file_name, size_t file_name_len, const char **chunk, size_t *chunk_len)
Definition: mongoose.c:7768
void(* cs_base64_putc_t)(char, void *)
Definition: mongoose.h:1901
void cs_base64_encode(const unsigned char *src, int src_len, char *dst)
Definition: mongoose.c:458
struct mg_connection * mg_add_sock(struct mg_mgr *, sock_t, mg_event_handler_t)
Definition: mongoose.c:2947
void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id, struct mg_send_mqtt_handshake_opts)
Definition: mongoose.c:9723
Definition: mongoose.h:1853
int accept_literal
Definition: mongoose.h:5563
mg_resolve_err
Definition: mongoose.h:5548
struct mg_iface_vtable * vtable
Definition: mongoose.h:3005
union socket_address sa
Definition: mongoose.h:3272
void mbuf_trim(struct mbuf *)
Definition: mongoose.c:1058
int mg_dns_insert_header(struct mbuf *io, size_t pos, struct mg_dns_message *msg)
Definition: mongoose.c:10199
void cs_hmac_sha1(const unsigned char *key, size_t key_len, const unsigned char *text, size_t text_len, unsigned char out[20])
Definition: mongoose.c:1416
int err
Definition: mongoose.h:3271
size_t mg_dns_uncompress_name(struct mg_dns_message *msg, struct mg_str *name, char *dst, int dst_len)
Definition: mongoose.c:10419
void mg_basic_auth_header(const char *user, const char *pass, struct mbuf *buf)
Definition: mongoose.c:9556
unsigned char flags
Definition: mongoose.h:4107
struct http_message * req
Definition: mongoose.h:4122
struct mg_iface_vtable ** ifaces
Definition: mongoose.h:3353
void(* mg_event_handler_t)(struct mg_connection *nc, int ev, void *ev_data)
Definition: mongoose.h:3231
void mg_register_http_endpoint(struct mg_connection *nc, const char *uri_path, mg_event_handler_t handler)
Definition: mongoose.c:7806
const char * password
Definition: mongoose.h:5005
void mg_set_protocol_mqtt(struct mg_connection *nc)
Definition: mongoose.c:9712
mg_dns_resource_record_kind
Definition: mongoose.h:5299
void mg_mbuf_append_base64(struct mbuf *mbuf, const void *data, size_t len)
Definition: mongoose.c:9549
void mg_conn_addr_to_str(struct mg_connection *nc, char *buf, size_t len, int flags)
Definition: mongoose.c:9355
void MD5_Update(MD5_CTX *c, const unsigned char *data, size_t len)
Definition: mongoose.c:917
Definition: mongoose.h:3215
char * buf
Definition: mongoose.h:1741
Definition: mongoose.h:3246
struct mg_dns_resource_record * mg_dns_next_record(struct mg_dns_message *msg, int query, struct mg_dns_resource_record *prev)
Definition: mongoose.c:10156
unsigned char flags
Definition: mongoose.h:5000
size_t mbuf_append(struct mbuf *, const void *data, size_t data_size)
Definition: mongoose.c:1096
Definition: mongoose.h:4121
size_t size
Definition: mongoose.h:4106
void mg_if_sent_cb(struct mg_connection *nc, int num_sent)
Definition: mongoose.c:2470
struct mg_iface * iface
Definition: mongoose.h:3423
int mg_parse_uri(struct mg_str uri, struct mg_str *scheme, struct mg_str *user_info, struct mg_str *host, unsigned int *port, struct mg_str *path, struct mg_str *query, struct mg_str *fragment)
Definition: mongoose.c:4813
void mg_http_send_error(struct mg_connection *nc, int code, const char *reason)
Definition: mongoose.c:6232
void * user_data
Definition: mongoose.h:4117
int mg_casecmp(const char *s1, const char *s2)
Definition: mongoose.c:1816
void * user_data
Definition: mongoose.h:3514
int mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str)
Definition: mongoose.c:9484
void(* mg_resolve_callback_t)(struct mg_dns_message *dns_message, void *user_data, enum mg_resolve_err)
Definition: mongoose.h:5555
int mg_get_http_basic_auth(struct http_message *hm, char *user, size_t user_len, char *pass, size_t pass_len)
Definition: mongoose.c:6545
int mg_resolve_from_hosts_file(const char *host, union socket_address *usa)
Definition: mongoose.c:10691
struct mg_iface * mg_find_iface(struct mg_mgr *mgr, struct mg_iface_vtable *vtable, struct mg_iface *from)
Definition: mongoose.c:3046
Definition: mongoose.h:4072
void mg_base64_encode(const unsigned char *src, int src_len, char *dst)
Definition: mongoose.c:9252
int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out)
Definition: mongoose.c:4929
int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr, const char *name, size_t nlen, const void *rdata, size_t rlen)
Definition: mongoose.c:10251
int mg_http_check_digest_auth(struct http_message *hm, const char *auth_domain, FILE *fp)
size_t c_strnlen(const char *s, size_t maxlen)
Definition: mongoose.c:1478
Definition: mongoose.h:3513
int mg_ncasecmp(const char *s1, const char *s2, size_t len)
Definition: mongoose.c:1805
const char * var_name
Definition: mongoose.h:4114
int mg_mqtt_next_subscribe_topic(struct mg_mqtt_message *msg, struct mg_str *topic, uint8_t *qos, int pos)
Definition: mongoose.c:9856
#define intptr_t
Definition: mongoose.c:1998
time_t mg_mgr_poll(struct mg_mgr *, int milli)
Definition: mongoose.c:2223
struct mg_mgr * mgr
Definition: mongoose.h:3003
char * cs_md5(char buf[33],...)
Definition: mongoose.c:983
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded)
Definition: mongoose.c:6386
struct mg_connection * mg_connect_ws(struct mg_mgr *mgr, mg_event_handler_t event_handler, const char *url, const char *protocol, const char *extra_headers)
Definition: mongoose.c:9179
void mg_mqtt_pubrel(struct mg_connection *nc, uint16_t message_id)
Definition: mongoose.c:9915
void cs_sha1_final(unsigned char digest[20], cs_sha1_ctx *)
Definition: mongoose.c:1392
int chunk_size
Definition: mongoose.h:1907
void mg_mqtt_subscribe(struct mg_connection *nc, const struct mg_mqtt_topic_expression *topics, size_t topics_len, uint16_t message_id)
Definition: mongoose.c:9836
void * user_data
Definition: mongoose.h:3254
int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg)
Definition: mongoose.c:10383
struct mg_iface_vtable * main_iface
Definition: mongoose.h:3351
int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst, size_t dst_len)
Definition: mongoose.c:6414
struct mg_connection * listener
Definition: mongoose.h:3267
int mg_base64_decode(const unsigned char *s, int len, char *dst)
Definition: mongoose.c:9256
void mg_send_websocket_frame(struct mg_connection *nc, int op_and_flags, const void *data, size_t data_len)
Definition: mongoose.c:8984
Definition: mongoose.h:5550
#define MG_MAX_DNS_ANSWERS
Definition: mongoose.h:5295
int mg_asprintf(char **buf, size_t size, const char *fmt,...)
Definition: mongoose.c:1821
void mg_http_send_redirect(struct mg_connection *nc, int status_code, const struct mg_str location, const struct mg_str extra_headers)
Definition: mongoose.c:6199
Definition: mongoose.h:1740
void mg_send(struct mg_connection *, const void *buf, int len)
Definition: mongoose.c:2456
void cs_base64_init(struct cs_base64_ctx *ctx, cs_base64_putc_t putc, void *user_data)
Definition: mongoose.c:393
uint16_t transaction_id
Definition: mongoose.h:5319
int mg_is_big_endian(void)
Definition: mongoose.c:9445
int num_questions
Definition: mongoose.h:5320
void cs_to_hex(char *to, const unsigned char *p, size_t len)
Definition: mongoose.c:1750
double mg_set_timer(struct mg_connection *c, double timestamp)
Definition: mongoose.c:2909
struct mg_str mg_mk_str_n(const char *s, size_t len)
Definition: mongoose.c:1132
int status
Definition: mongoose.h:4116
int mg_check_ip_acl(const char *acl, uint32_t remote_ip)
Definition: mongoose.c:2879
size_t len
Definition: mongoose.h:1673
int resp_code
Definition: mongoose.h:4081
struct sockaddr sa
Definition: mongoose.h:3216
uint16_t keep_alive
Definition: mongoose.h:5010
void cs_sha1_update(cs_sha1_ctx *, const unsigned char *data, uint32_t len)
Definition: mongoose.c:1372
unsigned int flags
Definition: mongoose.h:3421
void mg_set_close_on_exec(sock_t)
Definition: mongoose.c:9286
struct mg_str * mg_get_http_header(struct http_message *hm, const char *name)
Definition: mongoose.c:5347
struct mg_connection * mg_if_accept_new_conn(struct mg_connection *lc)
Definition: mongoose.c:2432
int mg_parse_http_basic_auth(struct mg_str *hdr, char *user, size_t user_len, char *pass, size_t pass_len)
Definition: mongoose.c:6552
void mg_mqtt_ping(struct mg_connection *nc)
Definition: mongoose.c:9938
void mbuf_resize(struct mbuf *, size_t new_size)
Definition: mongoose.c:1043
int max_retries
Definition: mongoose.h:5561
struct mg_connection * prev
Definition: mongoose.h:3266
Definition: mongoose.h:4973
void * proto_data
Definition: mongoose.h:3282
mg_event_handler_t proto_handler
Definition: mongoose.h:3281
void * priv_2
Definition: mongoose.h:3294
struct mg_iface * iface
Definition: mongoose.h:3454
struct mg_iface * iface
Definition: mongoose.h:3517
int mg_vcmp(const struct mg_str *str2, const char *str1)
Definition: mongoose.c:1138
int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n)
Definition: mongoose.c:1184
int ttl
Definition: mongoose.h:5310
const char * c_strnstr(const char *s, const char *find, size_t slen)
Definition: mongoose.c:1720
void mg_set_protocol_dns(struct mg_connection *nc)
Definition: mongoose.c:10499
Definition: mongoose.h:3265
const char * user_name
Definition: mongoose.h:5004
unsigned int flags
Definition: mongoose.h:3515
uint8_t qos
Definition: mongoose.h:4996
const char * will_message
Definition: mongoose.h:5003
void mg_mgr_init_opt(struct mg_mgr *mgr, void *user_data, struct mg_mgr_init_opts opts)
Definition: mongoose.c:2119
int rclass
Definition: mongoose.h:5309
int mg_http_create_digest_auth_header(char *buf, size_t buf_len, const char *method, const char *uri, const char *auth_domain, const char *user, const char *passwd)
void mg_mgr_init(struct mg_mgr *mgr, void *user_data)
Definition: mongoose.c:2113
void mg_sock_addr_to_str(const union socket_address *sa, char *buf, size_t len, int flags)
Definition: mongoose.c:9296
void mg_send_http_chunk(struct mg_connection *nc, const char *buf, size_t len)
Definition: mongoose.c:6451
unsigned int flags
Definition: mongoose.h:3452
void mg_mqtt_publish(struct mg_connection *nc, const char *topic, uint16_t message_id, int flags, const void *data, size_t len)
Definition: mongoose.c:9817
const char ** error_string
Definition: mongoose.h:3422
int mg_resolve_async_opt(struct mg_mgr *mgr, const char *name, int query, mg_resolve_callback_t cb, void *data, struct mg_resolve_async_opts opts)
Definition: mongoose.c:10804
int qos
Definition: mongoose.h:4975
void mg_printf_html_escape(struct mg_connection *nc, const char *fmt,...)
Definition: mongoose.c:6481
Definition: mongoose.h:5559
unsigned char * data
Definition: mongoose.h:4105
const char * mg_next_comma_list_entry(const char *list, struct mg_str *val, struct mg_str *eq_val)
Definition: mongoose.c:9451
int num_answers
Definition: mongoose.h:5321
int mg_num_ifaces
Definition: mongoose.c:3035
void mg_mqtt_unsuback(struct mg_connection *nc, uint16_t message_id)
Definition: mongoose.c:9934
Definition: mongoose.h:4112
void cs_from_hex(char *to, const char *p, size_t len)
Definition: mongoose.c:1772
void mg_mqtt_pubrec(struct mg_connection *nc, uint16_t message_id)
Definition: mongoose.c:9911
Definition: mongoose.h:3002
Definition: mongoose.h:4994
cs_base64_putc_t b64_putc
Definition: mongoose.h:1905
void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags)
Definition: mongoose.c:3760
int mg_strcmp(const struct mg_str str1, const struct mg_str str2)
Definition: mongoose.c:1171
struct mg_iface * mg_if_create_iface(struct mg_iface_vtable *vtable, struct mg_mgr *mgr)
Definition: mongoose.c:3037
const char * p
Definition: mongoose.h:1672
struct mg_iface ** ifaces
Definition: mongoose.h:3256
uint8_t connack_ret_code
Definition: mongoose.h:4979
struct mg_connection * mg_connect_http(struct mg_mgr *mgr, mg_event_handler_t event_handler, const char *url, const char *extra_headers, const char *post_data)
Definition: mongoose.c:7757
void mg_send_websocket_framev(struct mg_connection *nc, int op_and_flags, const struct mg_str *strings, int num_strings)
Definition: mongoose.c:8998
const char * mg_skip(const char *s, const char *end_string, const char *delimiters, struct mg_str *v)
Definition: mongoose.c:9205
uint16_t flags
Definition: mongoose.h:5318
void * v
Definition: mongoose.h:3287
void mg_send_dns_query(struct mg_connection *nc, const char *name, int query_type)
Definition: mongoose.c:10296
int mg_vcasecmp(const struct mg_str *str2, const char *str1)
Definition: mongoose.c:1148
time_t last_io_time
Definition: mongoose.h:3276
void mbuf_remove(struct mbuf *, size_t data_size)
Definition: mongoose.c:1101
#define LIST_ENTRY(type)
Definition: mongoose.h:2451
void mg_send_response_line(struct mg_connection *nc, int status_code, const char *extra_headers)
Definition: mongoose.c:6194
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data)
Функция-обработчик событий
Definition: messenger_via_http_server.c:64
void mg_send_websocket_handshake3(struct mg_connection *nc, const char *path, const char *host, const char *protocol, const char *extra_headers, const char *user, const char *pass)
Definition: mongoose.c:9103
int mg_printf(struct mg_connection *, const char *fmt,...)
Definition: mongoose.c:2252
#define MG_MAX_HTTP_HEADERS
Definition: mongoose.h:4048
int rtype
Definition: mongoose.h:5308
int cmd
Definition: mongoose.h:4974
const char ** error_string
Definition: mongoose.h:3516
void mg_mqtt_disconnect(struct mg_connection *nc)
Definition: mongoose.c:9946
Definition: mongoose.h:5301
const char ** error_string
Definition: mongoose.h:3453
void cs_fprint_base64(FILE *f, const unsigned char *src, int src_len)
void * user_data
Definition: mongoose.h:3420
#define LIST_HEAD(name, type)
Definition: mongoose.h:2438
struct mg_connection * mg_next(struct mg_mgr *, struct mg_connection *)
Definition: mongoose.c:2829
void mg_mgr_free(struct mg_mgr *)
Definition: mongoose.c:2194