File indexing completed on 2025-08-02 08:21:06
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #ifndef CS_MONGOOSE_SRC_COMMON_H_
0021 #define CS_MONGOOSE_SRC_COMMON_H_
0022
0023 #define MG_VERSION "6.4"
0024
0025
0026 #ifdef MG_LOCALS
0027 #include <mg_locals.h>
0028 #endif
0029
0030 #if defined(MG_ENABLE_DEBUG) && !defined(CS_ENABLE_DEBUG)
0031 #define CS_ENABLE_DEBUG
0032 #endif
0033 #if defined(MG_DISABLE_STDIO) && !defined(CS_DISABLE_STDIO)
0034 #define CS_DISABLE_STDIO
0035 #elif defined(CS_DISABLE_STDIO) && !defined(MG_DISABLE_STDIO)
0036 #define MG_DISABLE_STDIO
0037 #endif
0038
0039
0040 #ifdef MG_DISABLE_FILESYSTEM
0041 #ifndef MG_DISABLE_DAV
0042 #define MG_DISABLE_DAV
0043 #endif
0044 #ifndef MG_DISABLE_CGI
0045 #define MG_DISABLE_CGI
0046 #endif
0047 #ifndef MG_DISABLE_DIRECTORY_LISTING
0048 #define MG_DISABLE_DIRECTORY_LISTING
0049 #endif
0050 #ifndef MG_DISABLE_DAV
0051 #define MG_DISABLE_DAV
0052 #endif
0053 #endif
0054
0055 #ifdef MG_NO_BSD_SOCKETS
0056 #ifndef MG_DISABLE_SYNC_RESOLVER
0057 #define MG_DISABLE_SYNC_RESOLVER
0058 #endif
0059 #ifndef MG_DISABLE_SOCKETPAIR
0060 #define MG_DISABLE_SOCKETPAIR
0061 #endif
0062 #endif
0063
0064
0065 #endif
0066 #ifndef CS_COMMON_PLATFORM_H_
0067 #define CS_COMMON_PLATFORM_H_
0068
0069
0070
0071
0072
0073 #define CS_P_CUSTOM 0
0074 #define CS_P_UNIX 1
0075 #define CS_P_WINDOWS 2
0076 #define CS_P_ESP_LWIP 3
0077 #define CS_P_CC3200 4
0078 #define CS_P_MSP432 5
0079
0080
0081 #ifndef CS_PLATFORM
0082
0083 #if defined(TARGET_IS_MSP432P4XX) || defined(__MSP432P401R__)
0084
0085 #define CS_PLATFORM CS_P_MSP432
0086 #elif defined(cc3200)
0087 #define CS_PLATFORM CS_P_CC3200
0088 #elif defined(__unix__) || defined(__APPLE__)
0089 #define CS_PLATFORM CS_P_UNIX
0090 #elif defined(_WIN32)
0091 #define CS_PLATFORM CS_P_WINDOWS
0092 #endif
0093
0094 #ifndef CS_PLATFORM
0095 #error "CS_PLATFORM is not specified and we couldn't guess it."
0096 #endif
0097
0098 #endif
0099
0100
0101
0102
0103 #ifdef __GNUC__
0104 #define NORETURN __attribute__((noreturn))
0105 #define NOINLINE __attribute__((noinline))
0106 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
0107 #else
0108 #define NORETURN
0109 #define NOINLINE
0110 #define WARN_UNUSED_RESULT
0111 #endif
0112
0113 #ifndef ARRAY_SIZE
0114 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
0115 #endif
0116
0117 #endif
0118 #ifndef CS_COMMON_PLATFORMS_PLATFORM_WINDOWS_H_
0119 #define CS_COMMON_PLATFORMS_PLATFORM_WINDOWS_H_
0120 #if CS_PLATFORM == CS_P_WINDOWS
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134 #ifdef _MSC_VER
0135 #pragma warning(disable : 4127)
0136 #pragma warning(disable : 4204)
0137 #endif
0138
0139 #include <assert.h>
0140 #include <direct.h>
0141 #include <errno.h>
0142 #include <fcntl.h>
0143 #include <io.h>
0144 #include <limits.h>
0145 #include <signal.h>
0146 #include <stddef.h>
0147 #include <stdio.h>
0148 #include <stdlib.h>
0149 #include <sys/stat.h>
0150 #include <time.h>
0151
0152 #define random() rand()
0153 #ifdef _MSC_VER
0154 #pragma comment(lib, "ws2_32.lib")
0155 #endif
0156
0157 #include <winsock2.h>
0158 #include <ws2tcpip.h>
0159 #include <windows.h>
0160 #include <process.h>
0161
0162 #ifndef EINPROGRESS
0163 #define EINPROGRESS WSAEINPROGRESS
0164 #endif
0165 #ifndef EWOULDBLOCK
0166 #define EWOULDBLOCK WSAEWOULDBLOCK
0167 #endif
0168 #ifndef __func__
0169 #define STRX(x) #x
0170 #define STR(x) STRX(x)
0171 #define __func__ __FILE__ ":" STR(__LINE__)
0172 #endif
0173 #define snprintf _snprintf
0174 #define fileno _fileno
0175 #define vsnprintf _vsnprintf
0176 #define sleep(x) Sleep((x) *1000)
0177 #define to64(x) _atoi64(x)
0178 #if !defined(__MINGW32__) && !defined(__MINGW64__)
0179 #define popen(x, y) _popen((x), (y))
0180 #define pclose(x) _pclose(x)
0181 #endif
0182 #define rmdir _rmdir
0183 #if defined(_MSC_VER) && _MSC_VER >= 1400
0184 #define fseeko(x, y, z) _fseeki64((x), (y), (z))
0185 #else
0186 #define fseeko(x, y, z) fseek((x), (y), (z))
0187 #endif
0188 #define random() rand()
0189 typedef int socklen_t;
0190 #if _MSC_VER >= 1700
0191 #include <stdint.h>
0192 #else
0193 typedef signed char int8_t;
0194 typedef unsigned char uint8_t;
0195 typedef int int32_t;
0196 typedef unsigned int uint32_t;
0197 typedef short int16_t;
0198 typedef unsigned short uint16_t;
0199 typedef __int64 int64_t;
0200 typedef unsigned __int64 uint64_t;
0201 #endif
0202 typedef SOCKET sock_t;
0203 typedef uint32_t in_addr_t;
0204 #ifndef UINT16_MAX
0205 #define UINT16_MAX 65535
0206 #endif
0207 #ifndef UINT32_MAX
0208 #define UINT32_MAX 4294967295
0209 #endif
0210 #ifndef pid_t
0211 #define pid_t HANDLE
0212 #endif
0213 #define INT64_FMT "I64d"
0214 #define INT64_X_FMT "I64x"
0215 #define SIZE_T_FMT "Iu"
0216 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
0217 typedef struct stat cs_stat_t;
0218 #else
0219 typedef struct _stati64 cs_stat_t;
0220 #endif
0221 #ifndef S_ISDIR
0222 #define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR)
0223 #endif
0224 #ifndef S_ISREG
0225 #define S_ISREG(x) (((x) &_S_IFMT) == _S_IFREG)
0226 #endif
0227 #define DIRSEP '\\'
0228
0229 #ifndef va_copy
0230 #ifdef __va_copy
0231 #define va_copy __va_copy
0232 #else
0233 #define va_copy(x, y) (x) = (y)
0234 #endif
0235 #endif
0236
0237 #ifndef MG_MAX_HTTP_REQUEST_SIZE
0238 #define MG_MAX_HTTP_REQUEST_SIZE 8192
0239 #endif
0240
0241 #ifndef MG_MAX_HTTP_SEND_MBUF
0242 #define MG_MAX_HTTP_SEND_MBUF 4096
0243 #endif
0244
0245 #ifndef MG_MAX_HTTP_HEADERS
0246 #define MG_MAX_HTTP_HEADERS 40
0247 #endif
0248
0249 #endif
0250 #endif
0251 #ifndef CS_COMMON_PLATFORMS_PLATFORM_UNIX_H_
0252 #define CS_COMMON_PLATFORMS_PLATFORM_UNIX_H_
0253 #if CS_PLATFORM == CS_P_UNIX
0254
0255 #ifndef _XOPEN_SOURCE
0256 #define _XOPEN_SOURCE 600
0257 #endif
0258
0259
0260 #ifndef __STDC_FORMAT_MACROS
0261 #define __STDC_FORMAT_MACROS
0262 #endif
0263
0264
0265 #ifndef __STDC_LIMIT_MACROS
0266 #define __STDC_LIMIT_MACROS
0267 #endif
0268
0269
0270 #ifndef _LARGEFILE_SOURCE
0271 #define _LARGEFILE_SOURCE
0272 #endif
0273
0274
0275 #ifndef _FILE_OFFSET_BITS
0276 #define _FILE_OFFSET_BITS 64
0277 #endif
0278
0279 #include <arpa/inet.h>
0280 #include <assert.h>
0281 #include <ctype.h>
0282 #include <dirent.h>
0283 #include <errno.h>
0284 #include <fcntl.h>
0285 #include <inttypes.h>
0286 #include <limits.h>
0287 #include <math.h>
0288 #include <netdb.h>
0289 #include <netinet/in.h>
0290 #include <pthread.h>
0291 #include <signal.h>
0292 #include <stdarg.h>
0293 #include <stdio.h>
0294 #include <stdlib.h>
0295 #include <string.h>
0296 #include <sys/socket.h>
0297 #include <sys/select.h>
0298 #include <sys/stat.h>
0299 #include <sys/time.h>
0300 #include <sys/types.h>
0301 #include <unistd.h>
0302
0303
0304
0305
0306
0307
0308 #if !(defined(__DARWIN_C_LEVEL) && __DARWIN_C_LEVEL >= 200809L)
0309 long long strtoll(const char *, char **, int);
0310 #endif
0311
0312 typedef int sock_t;
0313 #define INVALID_SOCKET (-1)
0314 #define SIZE_T_FMT "zu"
0315 typedef struct stat cs_stat_t;
0316 #define DIRSEP '/'
0317 #define to64(x) strtoll(x, NULL, 10)
0318 #define INT64_FMT PRId64
0319 #define INT64_X_FMT PRIx64
0320
0321 #ifndef __cdecl
0322 #define __cdecl
0323 #endif
0324
0325 #ifndef va_copy
0326 #ifdef __va_copy
0327 #define va_copy __va_copy
0328 #else
0329 #define va_copy(x, y) (x) = (y)
0330 #endif
0331 #endif
0332
0333 #define closesocket(x) close(x)
0334
0335 #ifndef MG_MAX_HTTP_REQUEST_SIZE
0336 #define MG_MAX_HTTP_REQUEST_SIZE 8192
0337 #endif
0338
0339 #ifndef MG_MAX_HTTP_SEND_MBUF
0340 #define MG_MAX_HTTP_SEND_MBUF 4096
0341 #endif
0342
0343 #ifndef MG_MAX_HTTP_HEADERS
0344 #define MG_MAX_HTTP_HEADERS 40
0345 #endif
0346
0347 #endif
0348 #endif
0349 #ifndef CS_COMMON_PLATFORMS_PLATFORM_ESP_LWIP_H_
0350 #define CS_COMMON_PLATFORMS_PLATFORM_ESP_LWIP_H_
0351 #if CS_PLATFORM == CS_P_ESP_LWIP
0352
0353 #include <assert.h>
0354 #include <ctype.h>
0355 #include <fcntl.h>
0356 #include <inttypes.h>
0357 #include <string.h>
0358 #include <sys/stat.h>
0359 #include <sys/time.h>
0360
0361 #include <lwip/err.h>
0362 #include <lwip/ip_addr.h>
0363 #include <lwip/inet.h>
0364 #include <lwip/netdb.h>
0365 #include <lwip/dns.h>
0366
0367 #ifndef LWIP_PROVIDE_ERRNO
0368 #include <errno.h>
0369 #endif
0370
0371 #define LWIP_TIMEVAL_PRIVATE 0
0372
0373 #if LWIP_SOCKET
0374 #include <lwip/sockets.h>
0375 #define SOMAXCONN 10
0376 #else
0377
0378 #undef LWIP_SOCKET
0379 #define LWIP_SOCKET 1
0380 #include <lwip/sockets.h>
0381 #undef LWIP_SOCKET
0382 #define LWIP_SOCKET 0
0383 #endif
0384
0385 typedef int sock_t;
0386 #define INVALID_SOCKET (-1)
0387 #define SIZE_T_FMT "u"
0388 typedef struct stat cs_stat_t;
0389 #define DIRSEP '/'
0390 #define to64(x) strtoll(x, NULL, 10)
0391 #define INT64_FMT PRId64
0392 #define INT64_X_FMT PRIx64
0393 #define __cdecl
0394
0395 unsigned long os_random(void);
0396 #define random os_random
0397
0398 #endif
0399 #endif
0400
0401
0402
0403
0404
0405 #ifndef CS_COMMON_PLATFORMS_PLATFORM_CC3200_H_
0406 #define CS_COMMON_PLATFORMS_PLATFORM_CC3200_H_
0407 #if CS_PLATFORM == CS_P_CC3200
0408
0409 #include <assert.h>
0410 #include <ctype.h>
0411 #include <errno.h>
0412 #include <inttypes.h>
0413 #include <stdint.h>
0414 #include <string.h>
0415 #include <time.h>
0416
0417 #ifndef __TI_COMPILER_VERSION__
0418 #include <fcntl.h>
0419 #include <sys/time.h>
0420 #endif
0421
0422 #define MG_SOCKET_SIMPLELINK 1
0423 #define MG_DISABLE_SOCKETPAIR 1
0424 #define MG_DISABLE_SYNC_RESOLVER 1
0425 #define MG_DISABLE_POPEN 1
0426 #define MG_DISABLE_CGI 1
0427
0428 #ifndef CC3200_FS_SPIFFS
0429 #define MG_DISABLE_DAV 1
0430 #define MG_DISABLE_DIRECTORY_LISTING 1
0431 #endif
0432
0433
0434 typedef int sock_t;
0435 #define INVALID_SOCKET (-1)
0436 #define SIZE_T_FMT "u"
0437 typedef struct stat cs_stat_t;
0438 #define DIRSEP '/'
0439 #define to64(x) strtoll(x, NULL, 10)
0440 #define INT64_FMT PRId64
0441 #define INT64_X_FMT PRIx64
0442 #define __cdecl
0443
0444 #define fileno(x) -1
0445
0446
0447
0448 #ifdef __cplusplus
0449 extern "C" {
0450 #endif
0451
0452 #ifdef __TI_COMPILER_VERSION__
0453 struct SlTimeval_t;
0454 #define timeval SlTimeval_t
0455 int gettimeofday(struct timeval *t, void *tz);
0456
0457 int asprintf(char **strp, const char *fmt, ...);
0458
0459 #endif
0460
0461 long int random(void);
0462
0463
0464 #ifdef __TI_COMPILER_VERSION__
0465
0466 #include <file.h>
0467
0468 typedef unsigned int mode_t;
0469 typedef size_t _off_t;
0470 typedef long ssize_t;
0471
0472 struct stat {
0473 int st_ino;
0474 mode_t st_mode;
0475 int st_nlink;
0476 time_t st_mtime;
0477 off_t st_size;
0478 };
0479
0480 int _stat(const char *pathname, struct stat *st);
0481 #define stat(a, b) _stat(a, b)
0482
0483 #define __S_IFMT 0170000
0484
0485 #define __S_IFDIR 0040000
0486 #define __S_IFCHR 0020000
0487 #define __S_IFREG 0100000
0488
0489 #define __S_ISTYPE(mode, mask) (((mode) &__S_IFMT) == (mask))
0490
0491 #define S_IFDIR __S_IFDIR
0492 #define S_IFCHR __S_IFCHR
0493 #define S_IFREG __S_IFREG
0494 #define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR)
0495 #define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG)
0496
0497
0498 #define va_copy(apc, ap) ((apc) = (ap))
0499
0500 #endif
0501
0502 #ifdef CC3200_FS_SPIFFS
0503 #include <common/spiffs/spiffs.h>
0504
0505 typedef struct {
0506 spiffs_DIR dh;
0507 struct spiffs_dirent de;
0508 } DIR;
0509
0510 #define d_name name
0511 #define dirent spiffs_dirent
0512
0513 DIR *opendir(const char *dir_name);
0514 int closedir(DIR *dir);
0515 struct dirent *readdir(DIR *dir);
0516 #endif
0517
0518 #ifdef CC3200_FS_SLFS
0519 #define MG_FS_SLFS
0520 #endif
0521
0522 #ifdef __cplusplus
0523 }
0524 #endif
0525
0526 #endif
0527 #endif
0528
0529
0530
0531
0532
0533 #ifndef CS_COMMON_PLATFORMS_PLATFORM_MSP432_H_
0534 #define CS_COMMON_PLATFORMS_PLATFORM_MSP432_H_
0535 #if CS_PLATFORM == CS_P_MSP432
0536
0537 #include <assert.h>
0538 #include <ctype.h>
0539 #include <errno.h>
0540 #include <inttypes.h>
0541 #include <stdint.h>
0542 #include <string.h>
0543 #include <time.h>
0544
0545 #ifndef __TI_COMPILER_VERSION__
0546 #include <fcntl.h>
0547 #include <sys/time.h>
0548 #endif
0549
0550 #define MG_SOCKET_SIMPLELINK 1
0551 #define MG_DISABLE_SOCKETPAIR 1
0552 #define MG_DISABLE_SYNC_RESOLVER 1
0553 #define MG_DISABLE_POPEN 1
0554 #define MG_DISABLE_CGI 1
0555 #define MG_DISABLE_DAV 1
0556 #define MG_DISABLE_DIRECTORY_LISTING 1
0557
0558
0559 typedef int sock_t;
0560 #define INVALID_SOCKET (-1)
0561 #define SIZE_T_FMT "u"
0562 typedef struct stat cs_stat_t;
0563 #define DIRSEP '/'
0564 #define to64(x) strtoll(x, NULL, 10)
0565 #define INT64_FMT PRId64
0566 #define INT64_X_FMT PRIx64
0567 #define __cdecl
0568
0569 #define fileno(x) -1
0570
0571
0572
0573 #ifdef __cplusplus
0574 extern "C" {
0575 #endif
0576
0577 #ifdef __TI_COMPILER_VERSION__
0578 struct SlTimeval_t;
0579 #define timeval SlTimeval_t
0580 int gettimeofday(struct timeval *t, void *tz);
0581 #endif
0582
0583 long int random(void);
0584
0585
0586 #ifdef __TI_COMPILER_VERSION__
0587
0588 #include <file.h>
0589
0590 typedef unsigned int mode_t;
0591 typedef size_t _off_t;
0592 typedef long ssize_t;
0593
0594 struct stat {
0595 int st_ino;
0596 mode_t st_mode;
0597 int st_nlink;
0598 time_t st_mtime;
0599 off_t st_size;
0600 };
0601
0602 int _stat(const char *pathname, struct stat *st);
0603 #define stat(a, b) _stat(a, b)
0604
0605 #define __S_IFMT 0170000
0606
0607 #define __S_IFDIR 0040000
0608 #define __S_IFCHR 0020000
0609 #define __S_IFREG 0100000
0610
0611 #define __S_ISTYPE(mode, mask) (((mode) &__S_IFMT) == (mask))
0612
0613 #define S_IFDIR __S_IFDIR
0614 #define S_IFCHR __S_IFCHR
0615 #define S_IFREG __S_IFREG
0616 #define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR)
0617 #define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG)
0618
0619
0620 #define va_copy(apc, ap) ((apc) = (ap))
0621
0622 #endif
0623
0624 #ifdef __cplusplus
0625 }
0626 #endif
0627
0628 #endif
0629 #endif
0630
0631
0632
0633
0634
0635 #ifndef CS_COMMON_PLATFORMS_SIMPLELINK_CS_SIMPLELINK_H_
0636 #define CS_COMMON_PLATFORMS_SIMPLELINK_CS_SIMPLELINK_H_
0637
0638
0639 #if defined(MG_SOCKET_SIMPLELINK) && !defined(__SIMPLELINK_H__)
0640
0641 #include <stdbool.h>
0642
0643 #ifndef __TI_COMPILER_VERSION__
0644 #undef __CONCAT
0645 #undef FD_CLR
0646 #undef FD_ISSET
0647 #undef FD_SET
0648 #undef FD_SETSIZE
0649 #undef FD_ZERO
0650 #undef fd_set
0651 #endif
0652
0653
0654
0655 #define PROVISIONING_API_H_
0656 #include <simplelink/user.h>
0657 #undef PROVISIONING_API_H_
0658 #undef SL_INC_STD_BSD_API_NAMING
0659
0660 #include <simplelink/include/simplelink.h>
0661
0662
0663
0664 #define AF_INET SL_AF_INET
0665
0666 #define socklen_t SlSocklen_t
0667 #define sockaddr SlSockAddr_t
0668 #define sockaddr_in SlSockAddrIn_t
0669 #define in_addr SlInAddr_t
0670
0671 #define SOCK_STREAM SL_SOCK_STREAM
0672 #define SOCK_DGRAM SL_SOCK_DGRAM
0673
0674 #define FD_SET SL_FD_SET
0675 #define FD_CLR SL_FD_CLR
0676 #define FD_ISSET SL_FD_ISSET
0677 #define FD_ZERO SL_FD_ZERO
0678 #define fd_set SlFdSet_t
0679
0680 #define htonl sl_Htonl
0681 #define ntohl sl_Ntohl
0682 #define htons sl_Htons
0683 #define ntohs sl_Ntohs
0684
0685 #define accept sl_Accept
0686 #define closesocket sl_Close
0687 #define bind sl_Bind
0688 #define connect sl_Connect
0689 #define listen sl_Listen
0690 #define recv sl_Recv
0691 #define recvfrom sl_RecvFrom
0692 #define send sl_Send
0693 #define sendto sl_SendTo
0694 #define socket sl_Socket
0695
0696 #define select(nfds, rfds, wfds, efds, tout) \
0697 sl_Select((nfds), (rfds), (wfds), (efds), (struct SlTimeval_t *)(tout))
0698
0699 #ifndef EACCES
0700 #define EACCES SL_EACCES
0701 #endif
0702 #ifndef EAFNOSUPPORT
0703 #define EAFNOSUPPORT SL_EAFNOSUPPORT
0704 #endif
0705 #ifndef EAGAIN
0706 #define EAGAIN SL_EAGAIN
0707 #endif
0708 #ifndef EBADF
0709 #define EBADF SL_EBADF
0710 #endif
0711 #ifndef EINVAL
0712 #define EINVAL SL_EINVAL
0713 #endif
0714 #ifndef ENOMEM
0715 #define ENOMEM SL_ENOMEM
0716 #endif
0717 #ifndef EWOULDBLOCK
0718 #define EWOULDBLOCK SL_EWOULDBLOCK
0719 #endif
0720
0721 #define SOMAXCONN 8
0722
0723 #ifdef __cplusplus
0724 extern "C" {
0725 #endif
0726
0727 const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
0728 char *inet_ntoa(struct in_addr in);
0729 int inet_pton(int af, const char *src, void *dst);
0730
0731 struct mg_mgr;
0732
0733 typedef void (*mg_init_cb)(struct mg_mgr *mgr);
0734 bool mg_start_task(int priority, int stack_size, mg_init_cb mg_init);
0735
0736 void mg_run_in_task(void (*cb)(struct mg_mgr *mgr, void *arg), void *cb_arg);
0737
0738 int sl_fs_init();
0739
0740 #ifdef __cplusplus
0741 }
0742 #endif
0743
0744 #endif
0745
0746 #endif
0747
0748
0749
0750
0751
0752 #ifndef CS_COMMON_CS_DBG_H_
0753 #define CS_COMMON_CS_DBG_H_
0754
0755 #ifndef CS_DISABLE_STDIO
0756 #include <stdio.h>
0757 #endif
0758
0759 #ifdef __cplusplus
0760 extern "C" {
0761 #endif
0762
0763 enum cs_log_level {
0764 LL_NONE = -1,
0765 LL_ERROR = 0,
0766 LL_WARN = 1,
0767 LL_INFO = 2,
0768 LL_DEBUG = 3,
0769 LL_VERBOSE_DEBUG = 4,
0770
0771 _LL_MIN = -2,
0772 _LL_MAX = 5,
0773 };
0774
0775 void cs_log_set_level(enum cs_log_level level);
0776
0777 #ifndef CS_DISABLE_STDIO
0778
0779 void cs_log_set_file(FILE *file);
0780
0781 extern enum cs_log_level cs_log_level;
0782 void cs_log_print_prefix(const char *func);
0783 void cs_log_printf(const char *fmt, ...);
0784
0785 #define LOG(l, x) \
0786 if (cs_log_level >= l) { \
0787 cs_log_print_prefix(__func__); \
0788 cs_log_printf x; \
0789 }
0790
0791 #ifndef CS_NDEBUG
0792
0793 #define DBG(x) \
0794 if (cs_log_level >= LL_VERBOSE_DEBUG) { \
0795 cs_log_print_prefix(__func__); \
0796 cs_log_printf x; \
0797 }
0798
0799 #else
0800
0801 #define DBG(x)
0802
0803 #endif
0804
0805 #else
0806
0807 #define LOG(l, x)
0808 #define DBG(x)
0809
0810 #endif
0811
0812 #ifdef __cplusplus
0813 }
0814 #endif
0815
0816 #endif
0817
0818
0819
0820
0821
0822 #ifndef CS_COMMON_CS_TIME_H_
0823 #define CS_COMMON_CS_TIME_H_
0824
0825 #ifdef __cplusplus
0826 extern "C" {
0827 #endif
0828
0829
0830 double cs_time();
0831
0832 #ifdef __cplusplus
0833 }
0834 #endif
0835
0836 #endif
0837
0838
0839
0840
0841
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851 #ifndef CS_COMMON_MBUF_H_
0852 #define CS_COMMON_MBUF_H_
0853
0854 #if defined(__cplusplus)
0855 extern "C" {
0856 #endif
0857
0858 #include <stdlib.h>
0859
0860 #ifndef MBUF_SIZE_MULTIPLIER
0861 #define MBUF_SIZE_MULTIPLIER 1.5
0862 #endif
0863
0864
0865 struct mbuf {
0866 char *buf;
0867 size_t len;
0868 size_t size;
0869 };
0870
0871
0872
0873
0874
0875 void mbuf_init(struct mbuf *, size_t initial_capacity);
0876
0877
0878 void mbuf_free(struct mbuf *);
0879
0880
0881
0882
0883
0884
0885 size_t mbuf_append(struct mbuf *, const void *data, size_t data_size);
0886
0887
0888
0889
0890
0891
0892
0893
0894 size_t mbuf_insert(struct mbuf *, size_t, const void *, size_t);
0895
0896
0897 void mbuf_remove(struct mbuf *, size_t data_size);
0898
0899
0900
0901
0902
0903
0904
0905 void mbuf_resize(struct mbuf *, size_t new_size);
0906
0907
0908 void mbuf_trim(struct mbuf *);
0909
0910 #if defined(__cplusplus)
0911 }
0912 #endif
0913
0914 #endif
0915
0916
0917
0918
0919
0920 #ifndef CS_COMMON_SHA1_H_
0921 #define CS_COMMON_SHA1_H_
0922
0923 #ifndef DISABLE_SHA1
0924
0925
0926 #ifdef __cplusplus
0927 extern "C" {
0928 #endif
0929
0930 typedef struct {
0931 uint32_t state[5];
0932 uint32_t count[2];
0933 unsigned char buffer[64];
0934 } cs_sha1_ctx;
0935
0936 void cs_sha1_init(cs_sha1_ctx *);
0937 void cs_sha1_update(cs_sha1_ctx *, const unsigned char *data, uint32_t len);
0938 void cs_sha1_final(unsigned char digest[20], cs_sha1_ctx *);
0939 void cs_hmac_sha1(const unsigned char *key, size_t key_len,
0940 const unsigned char *text, size_t text_len,
0941 unsigned char out[20]);
0942 #ifdef __cplusplus
0943 }
0944 #endif
0945
0946 #endif
0947
0948 #endif
0949
0950
0951
0952
0953
0954 #ifndef CS_COMMON_MD5_H_
0955 #define CS_COMMON_MD5_H_
0956
0957
0958 #ifdef __cplusplus
0959 extern "C" {
0960 #endif
0961
0962 typedef struct MD5Context {
0963 uint32_t buf[4];
0964 uint32_t bits[2];
0965 unsigned char in[64];
0966 } MD5_CTX;
0967
0968 void MD5_Init(MD5_CTX *c);
0969 void MD5_Update(MD5_CTX *c, const unsigned char *data, size_t len);
0970 void MD5_Final(unsigned char *md, MD5_CTX *c);
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980 char *cs_md5(char buf[33], ...);
0981
0982
0983
0984
0985
0986
0987 void cs_to_hex(char *to, const unsigned char *p, size_t len);
0988
0989 #ifdef __cplusplus
0990 }
0991 #endif
0992
0993 #endif
0994
0995
0996
0997
0998
0999 #ifndef CS_COMMON_BASE64_H_
1000 #define CS_COMMON_BASE64_H_
1001
1002 #ifndef DISABLE_BASE64
1003
1004 #include <stdio.h>
1005
1006 #ifdef __cplusplus
1007 extern "C" {
1008 #endif
1009
1010 typedef void (*cs_base64_putc_t)(char, void *);
1011
1012 struct cs_base64_ctx {
1013
1014 cs_base64_putc_t b64_putc;
1015 unsigned char chunk[3];
1016 int chunk_size;
1017 void *user_data;
1018 };
1019
1020 void cs_base64_init(struct cs_base64_ctx *ctx, cs_base64_putc_t putc,
1021 void *user_data);
1022 void cs_base64_update(struct cs_base64_ctx *ctx, const char *str, size_t len);
1023 void cs_base64_finish(struct cs_base64_ctx *ctx);
1024
1025 void cs_base64_encode(const unsigned char *src, int src_len, char *dst);
1026 void cs_fprint_base64(FILE *f, const unsigned char *src, int src_len);
1027 int cs_base64_decode(const unsigned char *s, int len, char *dst);
1028
1029 #ifdef __cplusplus
1030 }
1031 #endif
1032
1033 #endif
1034
1035 #endif
1036
1037
1038
1039
1040
1041 #ifndef CS_COMMON_STR_UTIL_H_
1042 #define CS_COMMON_STR_UTIL_H_
1043
1044 #include <stdarg.h>
1045 #include <stdlib.h>
1046
1047 #ifdef __cplusplus
1048 extern "C" {
1049 #endif
1050
1051 size_t c_strnlen(const char *s, size_t maxlen);
1052 int c_snprintf(char *buf, size_t buf_size, const char *format, ...);
1053 int c_vsnprintf(char *buf, size_t buf_size, const char *format, va_list ap);
1054
1055
1056
1057
1058 const char *c_strnstr(const char *s, const char *find, size_t slen);
1059
1060 #ifdef __cplusplus
1061 }
1062 #endif
1063
1064 #endif
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084 #ifndef CS_MONGOOSE_DEPS_FROZEN_FROZEN_H_
1085 #define CS_MONGOOSE_DEPS_FROZEN_FROZEN_H_
1086
1087 #ifdef __cplusplus
1088 extern "C" {
1089 #endif
1090
1091 #include <stdarg.h>
1092
1093 enum json_type {
1094 JSON_TYPE_EOF = 0,
1095 JSON_TYPE_STRING = 1,
1096 JSON_TYPE_NUMBER = 2,
1097 JSON_TYPE_OBJECT = 3,
1098 JSON_TYPE_TRUE = 4,
1099 JSON_TYPE_FALSE = 5,
1100 JSON_TYPE_NULL = 6,
1101 JSON_TYPE_ARRAY = 7
1102 };
1103
1104 struct json_token {
1105 const char *ptr;
1106 int len;
1107 int num_desc;
1108 enum json_type type;
1109 };
1110
1111
1112 #define JSON_STRING_INVALID -1
1113 #define JSON_STRING_INCOMPLETE -2
1114 #define JSON_TOKEN_ARRAY_TOO_SMALL -3
1115
1116 int parse_json(const char *json_string, int json_string_length,
1117 struct json_token *tokens_array, int size_of_tokens_array);
1118 struct json_token *parse_json2(const char *json_string, int string_length);
1119 struct json_token *find_json_token(struct json_token *toks, const char *path);
1120
1121 int json_emit_long(char *buf, int buf_len, long value);
1122 int json_emit_double(char *buf, int buf_len, double value);
1123 int json_emit_quoted_str(char *buf, int buf_len, const char *str, int len);
1124 int json_emit_unquoted_str(char *buf, int buf_len, const char *str, int len);
1125 int json_emit(char *buf, int buf_len, const char *fmt, ...);
1126 int json_emit_va(char *buf, int buf_len, const char *fmt, va_list);
1127
1128 #ifdef __cplusplus
1129 }
1130 #endif
1131
1132 #endif
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160 #ifndef CS_MONGOOSE_SRC_NET_H_
1161 #define CS_MONGOOSE_SRC_NET_H_
1162
1163 #ifdef MG_ENABLE_JAVASCRIPT
1164 #define EXCLUDE_COMMON
1165 #include <v7.h>
1166 #endif
1167
1168
1169 #ifdef MG_ENABLE_SSL
1170 #ifdef __APPLE__
1171 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1172 #endif
1173 #include <openssl/ssl.h>
1174 #else
1175 typedef void *SSL;
1176 typedef void *SSL_CTX;
1177 #endif
1178
1179 #ifndef MG_VPRINTF_BUFFER_SIZE
1180 #define MG_VPRINTF_BUFFER_SIZE 100
1181 #endif
1182
1183 #ifdef MG_USE_READ_WRITE
1184 #define MG_RECV_FUNC(s, b, l, f) read(s, b, l)
1185 #define MG_SEND_FUNC(s, b, l, f) write(s, b, l)
1186 #else
1187 #define MG_RECV_FUNC(s, b, l, f) recv(s, b, l, f)
1188 #define MG_SEND_FUNC(s, b, l, f) send(s, b, l, f)
1189 #endif
1190
1191 #ifdef __cplusplus
1192 extern "C" {
1193 #endif
1194
1195 union socket_address {
1196 struct sockaddr sa;
1197 struct sockaddr_in sin;
1198 #ifdef MG_ENABLE_IPV6
1199 struct sockaddr_in6 sin6;
1200 #else
1201 struct sockaddr sin6;
1202 #endif
1203 };
1204
1205
1206 struct mg_str {
1207 const char *p;
1208 size_t len;
1209 };
1210
1211 struct mg_connection;
1212
1213
1214
1215
1216
1217 typedef void (*mg_event_handler_t)(struct mg_connection *, int ev, void *);
1218
1219
1220 #define MG_EV_POLL 0
1221 #define MG_EV_ACCEPT 1
1222 #define MG_EV_CONNECT 2
1223 #define MG_EV_RECV 3
1224 #define MG_EV_SEND 4
1225 #define MG_EV_CLOSE 5
1226 #define MG_EV_TIMER 6
1227
1228
1229
1230
1231 struct mg_mgr {
1232 struct mg_connection *active_connections;
1233 const char *hexdump_file;
1234 #ifndef MG_DISABLE_SOCKETPAIR
1235 sock_t ctl[2];
1236 #endif
1237 void *user_data;
1238 void *mgr_data;
1239 #ifdef MG_ENABLE_JAVASCRIPT
1240 struct v7 *v7;
1241 #endif
1242 };
1243
1244
1245
1246
1247 struct mg_connection {
1248 struct mg_connection *next, *prev;
1249 struct mg_connection *listener;
1250 struct mg_mgr *mgr;
1251
1252 sock_t sock;
1253 int err;
1254 union socket_address sa;
1255 size_t recv_mbuf_limit;
1256 struct mbuf recv_mbuf;
1257 struct mbuf send_mbuf;
1258 SSL *ssl;
1259 SSL_CTX *ssl_ctx;
1260 time_t last_io_time;
1261 double ev_timer_time;
1262 mg_event_handler_t proto_handler;
1263 void *proto_data;
1264 void (*proto_data_destructor)(void *proto_data);
1265 mg_event_handler_t handler;
1266 void *user_data;
1267 union {
1268 void *v;
1269
1270
1271
1272
1273 mg_event_handler_t f;
1274 } priv_1;
1275 void *priv_2;
1276 void *mgr_data;
1277 unsigned long flags;
1278
1279 #define MG_F_LISTENING (1 << 0)
1280 #define MG_F_UDP (1 << 1)
1281 #define MG_F_RESOLVING (1 << 2)
1282 #define MG_F_CONNECTING (1 << 3)
1283 #define MG_F_SSL_HANDSHAKE_DONE (1 << 4)
1284 #define MG_F_WANT_READ (1 << 5)
1285 #define MG_F_WANT_WRITE (1 << 6)
1286 #define MG_F_IS_WEBSOCKET (1 << 7)
1287
1288
1289 #define MG_F_SEND_AND_CLOSE (1 << 10)
1290 #define MG_F_CLOSE_IMMEDIATELY (1 << 11)
1291 #define MG_F_WEBSOCKET_NO_DEFRAG (1 << 12)
1292 #define MG_F_DELETE_CHUNK (1 << 13)
1293
1294 #define MG_F_USER_1 (1 << 20)
1295 #define MG_F_USER_2 (1 << 21)
1296 #define MG_F_USER_3 (1 << 22)
1297 #define MG_F_USER_4 (1 << 23)
1298 #define MG_F_USER_5 (1 << 24)
1299 #define MG_F_USER_6 (1 << 25)
1300 };
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310 void mg_mgr_init(struct mg_mgr *mgr, void *user_data);
1311
1312
1313
1314
1315
1316
1317 void mg_mgr_free(struct mg_mgr *);
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327 time_t mg_mgr_poll(struct mg_mgr *, int milli);
1328
1329 #ifndef MG_DISABLE_SOCKETPAIR
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342 void mg_broadcast(struct mg_mgr *, mg_event_handler_t func, void *, size_t);
1343 #endif
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358 struct mg_connection *mg_next(struct mg_mgr *, struct mg_connection *);
1359
1360
1361
1362
1363
1364
1365
1366 struct mg_add_sock_opts {
1367 void *user_data;
1368 unsigned int flags;
1369 const char **error_string;
1370 };
1371
1372
1373
1374
1375
1376
1377
1378 struct mg_connection *mg_add_sock(struct mg_mgr *, sock_t, mg_event_handler_t);
1379
1380
1381
1382
1383
1384
1385
1386 struct mg_connection *mg_add_sock_opt(struct mg_mgr *, sock_t,
1387 mg_event_handler_t,
1388 struct mg_add_sock_opts);
1389
1390
1391
1392
1393
1394
1395
1396 struct mg_bind_opts {
1397 void *user_data;
1398 unsigned int flags;
1399 const char **error_string;
1400 #ifdef MG_ENABLE_SSL
1401
1402 const char *ssl_cert;
1403 const char *ssl_ca_cert;
1404 #endif
1405 };
1406
1407
1408
1409
1410
1411
1412 struct mg_connection *mg_bind(struct mg_mgr *, const char *,
1413 mg_event_handler_t);
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431 struct mg_connection *mg_bind_opt(struct mg_mgr *mgr, const char *address,
1432 mg_event_handler_t handler,
1433 struct mg_bind_opts opts);
1434
1435
1436 struct mg_connect_opts {
1437 void *user_data;
1438 unsigned int flags;
1439 const char **error_string;
1440 #ifdef MG_ENABLE_SSL
1441
1442 const char *ssl_cert;
1443 const char *ssl_ca_cert;
1444
1445
1446
1447
1448
1449
1450
1451
1452 const char *ssl_server_name;
1453 #endif
1454 };
1455
1456
1457
1458
1459
1460
1461 struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *address,
1462 mg_event_handler_t handler);
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512 struct mg_connection *mg_connect_opt(struct mg_mgr *mgr, const char *address,
1513 mg_event_handler_t handler,
1514 struct mg_connect_opts opts);
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527 const char *mg_set_ssl(struct mg_connection *nc, const char *cert,
1528 const char *ca_cert);
1529
1530
1531
1532
1533
1534
1535
1536
1537 void mg_send(struct mg_connection *, const void *buf, int len);
1538
1539
1540 #if defined(__GNUC__)
1541 __attribute__((format(printf, 2, 3)))
1542 #endif
1543
1544
1545
1546
1547
1548
1549
1550 int mg_printf(struct mg_connection *, const char *fmt, ...);
1551
1552
1553 int mg_vprintf(struct mg_connection *, const char *fmt, va_list ap);
1554
1555
1556
1557
1558
1559
1560 int mg_socketpair(sock_t[2], int sock_type);
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572 #ifndef MG_DISABLE_SYNC_RESOLVER
1573 int mg_resolve(const char *domain_name, char *ip_addr_buf, size_t buf_len);
1574 #endif
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596 int mg_check_ip_acl(const char *acl, uint32_t remote_ip);
1597
1598
1599
1600
1601
1602
1603
1604
1605 void mg_enable_multithreading(struct mg_connection *nc);
1606
1607 #ifdef MG_ENABLE_JAVASCRIPT
1608
1609
1610
1611
1612
1613
1614 enum v7_err mg_enable_javascript(struct mg_mgr *m, struct v7 *v7,
1615 const char *init_js_file_name);
1616 #endif
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642 double mg_set_timer(struct mg_connection *c, double timestamp);
1643
1644
1645
1646
1647 double mg_time();
1648
1649 #ifdef __cplusplus
1650 }
1651 #endif
1652
1653 #endif
1654
1655
1656
1657
1658
1659 #ifndef CS_MONGOOSE_SRC_NET_IF_H_
1660 #define CS_MONGOOSE_SRC_NET_IF_H_
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672 #ifdef __cplusplus
1673 extern "C" {
1674 #endif
1675
1676
1677 void mg_if_connect_tcp(struct mg_connection *nc,
1678 const union socket_address *sa);
1679
1680 void mg_if_connect_udp(struct mg_connection *nc);
1681
1682 void mg_if_connect_cb(struct mg_connection *nc, int err);
1683
1684
1685 int mg_if_listen_tcp(struct mg_connection *nc, union socket_address *sa);
1686
1687
1688
1689
1690
1691
1692
1693 struct mg_connection *mg_if_accept_new_conn(struct mg_connection *lc);
1694 void mg_if_accept_tcp_cb(struct mg_connection *nc, union socket_address *sa,
1695 size_t sa_len);
1696
1697
1698 int mg_if_listen_udp(struct mg_connection *nc, union socket_address *sa);
1699
1700
1701 void mg_if_tcp_send(struct mg_connection *nc, const void *buf, size_t len);
1702 void mg_if_udp_send(struct mg_connection *nc, const void *buf, size_t len);
1703
1704 void mg_if_sent_cb(struct mg_connection *nc, int num_sent);
1705
1706
1707
1708
1709
1710
1711 void mg_if_recv_tcp_cb(struct mg_connection *nc, void *buf, int len);
1712 void mg_if_recv_udp_cb(struct mg_connection *nc, void *buf, int len,
1713 union socket_address *sa, size_t sa_len);
1714 void mg_if_recved(struct mg_connection *nc, size_t len);
1715
1716
1717 void mg_if_poll(struct mg_connection *nc, time_t now);
1718
1719
1720 void mg_if_timer(struct mg_connection *c, double now);
1721
1722
1723 int mg_if_create_conn(struct mg_connection *nc);
1724
1725
1726 void mg_if_destroy_conn(struct mg_connection *nc);
1727
1728 void mg_close_conn(struct mg_connection *nc);
1729
1730
1731 void mg_if_get_conn_addr(struct mg_connection *nc, int remote,
1732 union socket_address *sa);
1733
1734
1735 void mg_sock_set(struct mg_connection *nc, sock_t sock);
1736
1737 #ifdef __cplusplus
1738 }
1739 #endif
1740
1741 #endif
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751 #ifndef CS_MONGOOSE_SRC_URI_H_
1752 #define CS_MONGOOSE_SRC_URI_H_
1753
1754
1755 #ifdef __cplusplus
1756 extern "C" {
1757 #endif
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782 int mg_parse_uri(struct mg_str uri, struct mg_str *scheme,
1783 struct mg_str *user_info, struct mg_str *host,
1784 unsigned int *port, struct mg_str *path, struct mg_str *query,
1785 struct mg_str *fragment);
1786
1787 int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out);
1788
1789 #ifdef __cplusplus
1790 }
1791 #endif
1792 #endif
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802 #ifndef CS_MONGOOSE_SRC_UTIL_H_
1803 #define CS_MONGOOSE_SRC_UTIL_H_
1804
1805 #include <stdio.h>
1806
1807
1808 #ifdef __cplusplus
1809 extern "C" {
1810 #endif
1811
1812 #ifndef MAX_PATH_SIZE
1813 #define MAX_PATH_SIZE 500
1814 #endif
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825 const char *mg_skip(const char *s, const char *end_string,
1826 const char *delimiters, struct mg_str *v);
1827
1828
1829
1830
1831 int mg_ncasecmp(const char *s1, const char *s2, size_t len);
1832
1833
1834
1835
1836 int mg_casecmp(const char *s1, const char *s2);
1837
1838
1839
1840
1841
1842 int mg_vcmp(const struct mg_str *str2, const char *str1);
1843
1844
1845
1846
1847
1848 int mg_vcasecmp(const struct mg_str *str2, const char *str1);
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859 int mg_base64_decode(const unsigned char *s, int len, char *dst);
1860
1861
1862
1863
1864
1865
1866 void mg_base64_encode(const unsigned char *src, int src_len, char *dst);
1867
1868 #ifndef MG_DISABLE_FILESYSTEM
1869
1870
1871
1872
1873
1874
1875
1876 int mg_stat(const char *path, cs_stat_t *st);
1877
1878
1879
1880
1881
1882
1883
1884
1885 FILE *mg_fopen(const char *path, const char *mode);
1886
1887
1888
1889
1890
1891
1892
1893
1894 int mg_open(const char *path, int flag, int mode);
1895 #endif
1896
1897 #if defined(_WIN32) && !defined(MG_ENABLE_THREADS)
1898 #define MG_ENABLE_THREADS
1899 #endif
1900
1901 #ifdef MG_ENABLE_THREADS
1902
1903
1904
1905
1906
1907
1908 void *mg_start_thread(void *(*thread_func)(void *), void *thread_func_param);
1909 #endif
1910
1911 void mg_set_close_on_exec(sock_t);
1912
1913 #define MG_SOCK_STRINGIFY_IP 1
1914 #define MG_SOCK_STRINGIFY_PORT 2
1915 #define MG_SOCK_STRINGIFY_REMOTE 4
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929 void mg_conn_addr_to_str(struct mg_connection *nc, char *buf, size_t len,
1930 int flags);
1931 #ifndef MG_DISABLE_SOCKET_IF
1932 void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags);
1933 #endif
1934
1935
1936
1937
1938
1939
1940 void mg_sock_addr_to_str(const union socket_address *sa, char *buf, size_t len,
1941 int flags);
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951 int mg_hexdump(const void *buf, int len, char *dst, int dst_len);
1952
1953
1954
1955
1956
1957
1958
1959
1960 void mg_hexdump_connection(struct mg_connection *nc, const char *path,
1961 const void *buf, int num_bytes, int ev);
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977 int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap);
1978
1979
1980
1981
1982 int mg_is_big_endian(void);
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997 const char *mg_next_comma_list_entry(const char *list, struct mg_str *val,
1998 struct mg_str *eq_val);
1999
2000
2001
2002
2003
2004
2005 int mg_match_prefix(const char *pattern, int pattern_len, const char *str);
2006 int mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str);
2007
2008
2009
2010
2011
2012 struct mg_str mg_mk_str(const char *s);
2013
2014
2015 #define MG_MK_STR(str_literal) \
2016 { str_literal, sizeof(str_literal) - 1 }
2017
2018 #ifdef __cplusplus
2019 }
2020 #endif
2021 #endif
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031 #ifndef CS_MONGOOSE_SRC_HTTP_H_
2032 #define CS_MONGOOSE_SRC_HTTP_H_
2033
2034
2035 #ifdef __cplusplus
2036 extern "C" {
2037 #endif
2038
2039 #ifndef MG_MAX_HTTP_HEADERS
2040 #define MG_MAX_HTTP_HEADERS 20
2041 #endif
2042
2043 #ifndef MG_MAX_HTTP_REQUEST_SIZE
2044 #define MG_MAX_HTTP_REQUEST_SIZE 1024
2045 #endif
2046
2047 #ifndef MG_MAX_PATH
2048 #ifdef PATH_MAX
2049 #define MG_MAX_PATH PATH_MAX
2050 #else
2051 #define MG_MAX_PATH 256
2052 #endif
2053 #endif
2054
2055 #ifndef MG_MAX_HTTP_SEND_MBUF
2056 #define MG_MAX_HTTP_SEND_MBUF 1024
2057 #endif
2058
2059 #ifndef MG_WEBSOCKET_PING_INTERVAL_SECONDS
2060 #define MG_WEBSOCKET_PING_INTERVAL_SECONDS 5
2061 #endif
2062
2063 #ifndef MG_CGI_ENVIRONMENT_SIZE
2064 #define MG_CGI_ENVIRONMENT_SIZE 8192
2065 #endif
2066
2067 #ifndef MG_MAX_CGI_ENVIR_VARS
2068 #define MG_MAX_CGI_ENVIR_VARS 64
2069 #endif
2070
2071 #ifndef MG_ENV_EXPORT_TO_CGI
2072 #define MG_ENV_EXPORT_TO_CGI "MONGOOSE_CGI"
2073 #endif
2074
2075
2076 struct http_message {
2077 struct mg_str message;
2078
2079
2080 struct mg_str method;
2081 struct mg_str uri;
2082 struct mg_str proto;
2083
2084
2085 int resp_code;
2086 struct mg_str resp_status_msg;
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096 struct mg_str query_string;
2097
2098
2099 struct mg_str header_names[MG_MAX_HTTP_HEADERS];
2100 struct mg_str header_values[MG_MAX_HTTP_HEADERS];
2101
2102
2103 struct mg_str body;
2104 };
2105
2106
2107 struct websocket_message {
2108 unsigned char *data;
2109 size_t size;
2110 unsigned char flags;
2111 };
2112
2113
2114 struct mg_http_multipart_part {
2115 const char *file_name;
2116 const char *var_name;
2117 struct mg_str data;
2118 int status;
2119 void *user_data;
2120 };
2121
2122
2123 #define MG_EV_HTTP_REQUEST 100
2124 #define MG_EV_HTTP_REPLY 101
2125 #define MG_EV_HTTP_CHUNK 102
2126 #define MG_EV_SSI_CALL 105
2127
2128 #define MG_EV_WEBSOCKET_HANDSHAKE_REQUEST 111
2129 #define MG_EV_WEBSOCKET_HANDSHAKE_DONE 112
2130 #define MG_EV_WEBSOCKET_FRAME 113
2131 #define MG_EV_WEBSOCKET_CONTROL_FRAME 114
2132
2133 #ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART
2134 #define MG_EV_HTTP_MULTIPART_REQUEST 121
2135 #define MG_EV_HTTP_PART_BEGIN 122
2136 #define MG_EV_HTTP_PART_DATA 123
2137 #define MG_EV_HTTP_PART_END 124
2138 #endif
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183 void mg_set_protocol_http_websocket(struct mg_connection *nc);
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199 void mg_send_websocket_handshake(struct mg_connection *nc, const char *uri,
2200 const char *extra_headers);
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212 void mg_send_websocket_handshake2(struct mg_connection *nc, const char *path,
2213 const char *host, const char *protocol,
2214 const char *extra_headers);
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236 struct mg_connection *mg_connect_ws(struct mg_mgr *mgr,
2237 mg_event_handler_t event_handler,
2238 const char *url, const char *protocol,
2239 const char *extra_headers);
2240
2241
2242
2243
2244
2245
2246
2247 struct mg_connection *mg_connect_ws_opt(struct mg_mgr *mgr,
2248 mg_event_handler_t ev_handler,
2249 struct mg_connect_opts opts,
2250 const char *url, const char *protocol,
2251 const char *extra_headers);
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271 void mg_send_websocket_frame(struct mg_connection *nc, int op_and_flags,
2272 const void *data, size_t data_len);
2273
2274
2275
2276
2277
2278
2279 void mg_send_websocket_framev(struct mg_connection *nc, int op_and_flags,
2280 const struct mg_str *strings, int num_strings);
2281
2282
2283
2284
2285
2286
2287
2288 void mg_printf_websocket_frame(struct mg_connection *nc, int op_and_flags,
2289 const char *fmt, ...);
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309 void mg_send_http_chunk(struct mg_connection *nc, const char *buf, size_t len);
2310
2311
2312
2313
2314
2315 void mg_printf_http_chunk(struct mg_connection *nc, const char *fmt, ...);
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330 void mg_send_response_line(struct mg_connection *c, int status_code,
2331 const char *extra_headers);
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346 void mg_send_head(struct mg_connection *n, int status_code,
2347 int64_t content_length, const char *extra_headers);
2348
2349
2350
2351
2352 void mg_printf_html_escape(struct mg_connection *nc, const char *fmt, ...);
2353
2354
2355 #define WEBSOCKET_OP_CONTINUE 0
2356 #define WEBSOCKET_OP_TEXT 1
2357 #define WEBSOCKET_OP_BINARY 2
2358 #define WEBSOCKET_OP_CLOSE 8
2359 #define WEBSOCKET_OP_PING 9
2360 #define WEBSOCKET_OP_PONG 10
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374 #define WEBSOCKET_DONT_FIN 0x100
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384 int mg_parse_http(const char *s, int n, struct http_message *hm, int is_req);
2385
2386
2387
2388
2389
2390
2391
2392 struct mg_str *mg_get_http_header(struct http_message *hm, const char *name);
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409 int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf,
2410 size_t buf_size);
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448 size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name,
2449 size_t var_name_len, char *file_name,
2450 size_t file_name_len, const char **chunk,
2451 size_t *chunk_len);
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461 int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst,
2462 size_t dst_len);
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474 int mg_url_decode(const char *src, int src_len, char *dst, int dst_len,
2475 int is_form_url_encoded);
2476
2477
2478 int mg_http_create_digest_auth_header(char *buf, size_t buf_len,
2479 const char *method, const char *uri,
2480 const char *auth_domain, const char *user,
2481 const char *passwd);
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507 struct mg_connection *mg_connect_http(struct mg_mgr *mgr,
2508 mg_event_handler_t event_handler,
2509 const char *url,
2510 const char *extra_headers,
2511 const char *post_data);
2512
2513
2514
2515
2516
2517
2518
2519 struct mg_connection *mg_connect_http_opt(struct mg_mgr *mgr,
2520 mg_event_handler_t ev_handler,
2521 struct mg_connect_opts opts,
2522 const char *url,
2523 const char *extra_headers,
2524 const char *post_data);
2525
2526
2527
2528
2529 struct mg_serve_http_opts {
2530
2531 const char *document_root;
2532
2533
2534 const char *index_files;
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545 const char *per_directory_auth_file;
2546
2547
2548 const char *auth_domain;
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558 const char *global_auth_file;
2559
2560
2561 const char *enable_directory_listing;
2562
2563
2564 const char *ssi_pattern;
2565
2566
2567 const char *ip_acl;
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591 const char *url_rewrites;
2592
2593
2594 const char *dav_document_root;
2595
2596
2597
2598
2599
2600 const char *dav_auth_file;
2601
2602
2603 const char *hidden_file_pattern;
2604
2605
2606 const char *cgi_file_pattern;
2607
2608
2609 const char *cgi_interpreter;
2610
2611
2612
2613
2614
2615 const char *custom_mime_types;
2616
2617
2618
2619
2620
2621 const char *extra_headers;
2622 };
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644 void mg_serve_http(struct mg_connection *nc, struct http_message *hm,
2645 struct mg_serve_http_opts opts);
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674 void mg_register_http_endpoint(struct mg_connection *nc, const char *uri_path,
2675 mg_event_handler_t handler);
2676
2677 #ifdef MG_ENABLE_HTTP_STREAMING_MULTIPART
2678
2679
2680 typedef struct mg_str (*mg_fu_fname_fn)(struct mg_connection *nc,
2681 struct mg_str fname);
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714 void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data,
2715 mg_fu_fname_fn local_name_fn);
2716 #endif
2717
2718
2719
2720
2721
2722 int mg_http_check_digest_auth(struct http_message *hm, const char *auth_domain,
2723 FILE *fp);
2724
2725 #ifdef __cplusplus
2726 }
2727 #endif
2728 #endif
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738 #ifndef CS_MONGOOSE_SRC_JSON_RPC_H_
2739 #define CS_MONGOOSE_SRC_JSON_RPC_H_
2740
2741 #ifdef __cplusplus
2742 extern "C" {
2743 #endif
2744
2745
2746 struct mg_rpc_request {
2747 struct json_token *message;
2748 struct json_token *id;
2749 struct json_token *method;
2750 struct json_token *params;
2751 };
2752
2753
2754 struct mg_rpc_reply {
2755 struct json_token *message;
2756 struct json_token *id;
2757 struct json_token *result;
2758 };
2759
2760
2761 struct mg_rpc_error {
2762 struct json_token *message;
2763 struct json_token *id;
2764 struct json_token *error_code;
2765 struct json_token *error_message;
2766 struct json_token *error_data;
2767 };
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784 int mg_rpc_parse_reply(const char *buf, int len, struct json_token *toks,
2785 int max_toks, struct mg_rpc_reply *,
2786 struct mg_rpc_error *);
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796 int mg_rpc_create_request(char *buf, int len, const char *method,
2797 const char *id, const char *params_fmt, ...);
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807 int mg_rpc_create_reply(char *buf, int len, const struct mg_rpc_request *req,
2808 const char *result_fmt, ...);
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818 int mg_rpc_create_error(char *buf, int len, struct mg_rpc_request *req,
2819 int code, const char *message, const char *fmt, ...);
2820
2821
2822 #define JSON_RPC_PARSE_ERROR (-32700)
2823 #define JSON_RPC_INVALID_REQUEST_ERROR (-32600)
2824 #define JSON_RPC_METHOD_NOT_FOUND_ERROR (-32601)
2825 #define JSON_RPC_INVALID_PARAMS_ERROR (-32602)
2826 #define JSON_RPC_INTERNAL_ERROR (-32603)
2827 #define JSON_RPC_SERVER_ERROR (-32000)
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843 int mg_rpc_create_std_error(char *buf, int len, struct mg_rpc_request *req,
2844 int code);
2845
2846 typedef int (*mg_rpc_handler_t)(char *buf, int len, struct mg_rpc_request *req);
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860 int mg_rpc_dispatch(const char *buf, int, char *dst, int dst_len,
2861 const char **methods, mg_rpc_handler_t *handlers);
2862
2863 #ifdef __cplusplus
2864 }
2865 #endif
2866 #endif
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888 #ifndef CS_MONGOOSE_SRC_MQTT_H_
2889 #define CS_MONGOOSE_SRC_MQTT_H_
2890
2891
2892 struct mg_mqtt_message {
2893 int cmd;
2894 struct mg_str payload;
2895 int qos;
2896 uint8_t connack_ret_code;
2897 uint16_t message_id;
2898 char *topic;
2899 };
2900
2901 struct mg_mqtt_topic_expression {
2902 const char *topic;
2903 uint8_t qos;
2904 };
2905
2906 struct mg_send_mqtt_handshake_opts {
2907 unsigned char flags;
2908 uint16_t keep_alive;
2909 const char *will_topic;
2910 const char *will_message;
2911 const char *user_name;
2912 const char *password;
2913 };
2914
2915
2916 #define MG_MQTT_CMD_CONNECT 1
2917 #define MG_MQTT_CMD_CONNACK 2
2918 #define MG_MQTT_CMD_PUBLISH 3
2919 #define MG_MQTT_CMD_PUBACK 4
2920 #define MG_MQTT_CMD_PUBREC 5
2921 #define MG_MQTT_CMD_PUBREL 6
2922 #define MG_MQTT_CMD_PUBCOMP 7
2923 #define MG_MQTT_CMD_SUBSCRIBE 8
2924 #define MG_MQTT_CMD_SUBACK 9
2925 #define MG_MQTT_CMD_UNSUBSCRIBE 10
2926 #define MG_MQTT_CMD_UNSUBACK 11
2927 #define MG_MQTT_CMD_PINGREQ 12
2928 #define MG_MQTT_CMD_PINGRESP 13
2929 #define MG_MQTT_CMD_DISCONNECT 14
2930
2931
2932 #define MG_MQTT_EVENT_BASE 200
2933 #define MG_EV_MQTT_CONNECT (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_CONNECT)
2934 #define MG_EV_MQTT_CONNACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_CONNACK)
2935 #define MG_EV_MQTT_PUBLISH (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBLISH)
2936 #define MG_EV_MQTT_PUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBACK)
2937 #define MG_EV_MQTT_PUBREC (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBREC)
2938 #define MG_EV_MQTT_PUBREL (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBREL)
2939 #define MG_EV_MQTT_PUBCOMP (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBCOMP)
2940 #define MG_EV_MQTT_SUBSCRIBE (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_SUBSCRIBE)
2941 #define MG_EV_MQTT_SUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_SUBACK)
2942 #define MG_EV_MQTT_UNSUBSCRIBE (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_UNSUBSCRIBE)
2943 #define MG_EV_MQTT_UNSUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_UNSUBACK)
2944 #define MG_EV_MQTT_PINGREQ (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PINGREQ)
2945 #define MG_EV_MQTT_PINGRESP (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PINGRESP)
2946 #define MG_EV_MQTT_DISCONNECT (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_DISCONNECT)
2947
2948
2949 #define MG_MQTT_RETAIN 0x1
2950 #define MG_MQTT_DUP 0x4
2951 #define MG_MQTT_QOS(qos) ((qos) << 1)
2952 #define MG_MQTT_GET_QOS(flags) (((flags) &0x6) >> 1)
2953 #define MG_MQTT_SET_QOS(flags, qos) (flags) = ((flags) & ~0x6) | ((qos) << 1)
2954
2955
2956 #define MG_MQTT_CLEAN_SESSION 0x02
2957 #define MG_MQTT_HAS_WILL 0x04
2958 #define MG_MQTT_WILL_RETAIN 0x20
2959 #define MG_MQTT_HAS_PASSWORD 0x40
2960 #define MG_MQTT_HAS_USER_NAME 0x80
2961 #define MG_MQTT_GET_WILL_QOS(flags) (((flags) &0x18) >> 3)
2962 #define MG_MQTT_SET_WILL_QOS(flags, qos) \
2963 (flags) = ((flags) & ~0x18) | ((qos) << 3)
2964
2965
2966 #define MG_EV_MQTT_CONNACK_ACCEPTED 0
2967 #define MG_EV_MQTT_CONNACK_UNACCEPTABLE_VERSION 1
2968 #define MG_EV_MQTT_CONNACK_IDENTIFIER_REJECTED 2
2969 #define MG_EV_MQTT_CONNACK_SERVER_UNAVAILABLE 3
2970 #define MG_EV_MQTT_CONNACK_BAD_AUTH 4
2971 #define MG_EV_MQTT_CONNACK_NOT_AUTHORIZED 5
2972
2973 #ifdef __cplusplus
2974 extern "C" {
2975 #endif
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990 void mg_set_protocol_mqtt(struct mg_connection *nc);
2991
2992
2993 void mg_send_mqtt_handshake(struct mg_connection *nc, const char *client_id);
2994
2995
2996 void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id,
2997 struct mg_send_mqtt_handshake_opts);
2998
2999
3000 void mg_mqtt_publish(struct mg_connection *nc, const char *topic,
3001 uint16_t message_id, int flags, const void *data,
3002 size_t len);
3003
3004
3005 void mg_mqtt_subscribe(struct mg_connection *nc,
3006 const struct mg_mqtt_topic_expression *topics,
3007 size_t topics_len, uint16_t message_id);
3008
3009
3010 void mg_mqtt_unsubscribe(struct mg_connection *nc, char **topics,
3011 size_t topics_len, uint16_t message_id);
3012
3013
3014 void mg_mqtt_disconnect(struct mg_connection *nc);
3015
3016
3017 void mg_mqtt_connack(struct mg_connection *nc, uint8_t return_code);
3018
3019
3020 void mg_mqtt_puback(struct mg_connection *nc, uint16_t message_id);
3021
3022
3023 void mg_mqtt_pubrec(struct mg_connection *nc, uint16_t message_id);
3024
3025
3026 void mg_mqtt_pubrel(struct mg_connection *nc, uint16_t message_id);
3027
3028
3029 void mg_mqtt_pubcomp(struct mg_connection *nc, uint16_t message_id);
3030
3031
3032
3033
3034
3035 void mg_mqtt_suback(struct mg_connection *nc, uint8_t *qoss, size_t qoss_len,
3036 uint16_t message_id);
3037
3038
3039 void mg_mqtt_unsuback(struct mg_connection *nc, uint16_t message_id);
3040
3041
3042 void mg_mqtt_ping(struct mg_connection *nc);
3043
3044
3045 void mg_mqtt_pong(struct mg_connection *nc);
3046
3047
3048
3049
3050
3051
3052
3053
3054 int mg_mqtt_next_subscribe_topic(struct mg_mqtt_message *msg,
3055 struct mg_str *topic, uint8_t *qos, int pos);
3056
3057 #ifdef __cplusplus
3058 }
3059 #endif
3060
3061 #endif
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083 #ifndef CS_MONGOOSE_SRC_MQTT_BROKER_H_
3084 #define CS_MONGOOSE_SRC_MQTT_BROKER_H_
3085
3086 #ifdef MG_ENABLE_MQTT_BROKER
3087
3088
3089 #ifdef __cplusplus
3090 extern "C" {
3091 #endif
3092
3093 #define MG_MQTT_MAX_SESSION_SUBSCRIPTIONS 512;
3094
3095 struct mg_mqtt_broker;
3096
3097
3098 struct mg_mqtt_session {
3099 struct mg_mqtt_broker *brk;
3100 struct mg_mqtt_session *next, *prev;
3101 struct mg_connection *nc;
3102 size_t num_subscriptions;
3103 struct mg_mqtt_topic_expression *subscriptions;
3104 void *user_data;
3105 };
3106
3107
3108 struct mg_mqtt_broker {
3109 struct mg_mqtt_session *sessions;
3110 void *user_data;
3111 };
3112
3113
3114 void mg_mqtt_broker_init(struct mg_mqtt_broker *brk, void *user_data);
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141 void mg_mqtt_broker(struct mg_connection *brk, int ev, void *data);
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153 struct mg_mqtt_session *mg_mqtt_next(struct mg_mqtt_broker *brk,
3154 struct mg_mqtt_session *s);
3155
3156 #ifdef __cplusplus
3157 }
3158 #endif
3159
3160 #endif
3161 #endif
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171 #ifndef CS_MONGOOSE_SRC_DNS_H_
3172 #define CS_MONGOOSE_SRC_DNS_H_
3173
3174
3175 #ifdef __cplusplus
3176 extern "C" {
3177 #endif
3178
3179 #define MG_DNS_A_RECORD 0x01
3180 #define MG_DNS_CNAME_RECORD 0x05
3181 #define MG_DNS_AAAA_RECORD 0x1c
3182 #define MG_DNS_MX_RECORD 0x0f
3183
3184 #define MG_MAX_DNS_QUESTIONS 32
3185 #define MG_MAX_DNS_ANSWERS 32
3186
3187 #define MG_DNS_MESSAGE 100
3188
3189 enum mg_dns_resource_record_kind {
3190 MG_DNS_INVALID_RECORD = 0,
3191 MG_DNS_QUESTION,
3192 MG_DNS_ANSWER
3193 };
3194
3195
3196 struct mg_dns_resource_record {
3197 struct mg_str name;
3198 int rtype;
3199 int rclass;
3200 int ttl;
3201 enum mg_dns_resource_record_kind kind;
3202 struct mg_str rdata;
3203 };
3204
3205
3206 struct mg_dns_message {
3207 struct mg_str pkt;
3208 uint16_t flags;
3209 uint16_t transaction_id;
3210 int num_questions;
3211 int num_answers;
3212 struct mg_dns_resource_record questions[MG_MAX_DNS_QUESTIONS];
3213 struct mg_dns_resource_record answers[MG_MAX_DNS_ANSWERS];
3214 };
3215
3216 struct mg_dns_resource_record *mg_dns_next_record(
3217 struct mg_dns_message *msg, int query, struct mg_dns_resource_record *prev);
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230 int mg_dns_parse_record_data(struct mg_dns_message *msg,
3231 struct mg_dns_resource_record *rr, void *data,
3232 size_t data_len);
3233
3234
3235
3236
3237 void mg_send_dns_query(struct mg_connection *nc, const char *name,
3238 int query_type);
3239
3240
3241
3242
3243
3244
3245 int mg_dns_insert_header(struct mbuf *io, size_t pos,
3246 struct mg_dns_message *msg);
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256 int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg);
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274 int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr,
3275 const char *name, size_t nlen, const void *rdata,
3276 size_t rlen);
3277
3278
3279 int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg);
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294 size_t mg_dns_uncompress_name(struct mg_dns_message *msg, struct mg_str *name,
3295 char *dst, int dst_len);
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309 void mg_set_protocol_dns(struct mg_connection *nc);
3310
3311 #ifdef __cplusplus
3312 }
3313 #endif
3314 #endif
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326 #ifndef CS_MONGOOSE_SRC_DNS_SERVER_H_
3327 #define CS_MONGOOSE_SRC_DNS_SERVER_H_
3328
3329 #ifdef MG_ENABLE_DNS_SERVER
3330
3331
3332 #ifdef __cplusplus
3333 extern "C" {
3334 #endif
3335
3336 #define MG_DNS_SERVER_DEFAULT_TTL 3600
3337
3338 struct mg_dns_reply {
3339 struct mg_dns_message *msg;
3340 struct mbuf *io;
3341 size_t start;
3342 };
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372 struct mg_dns_reply mg_dns_create_reply(struct mbuf *io,
3373 struct mg_dns_message *msg);
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383 int mg_dns_reply_record(struct mg_dns_reply *reply,
3384 struct mg_dns_resource_record *question,
3385 const char *name, int rtype, int ttl, const void *rdata,
3386 size_t rdata_len);
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400 void mg_dns_send_reply(struct mg_connection *nc, struct mg_dns_reply *r);
3401
3402 #ifdef __cplusplus
3403 }
3404 #endif
3405
3406 #endif
3407 #endif
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417 #ifndef CS_MONGOOSE_SRC_RESOLV_H_
3418 #define CS_MONGOOSE_SRC_RESOLV_H_
3419
3420
3421 #ifdef __cplusplus
3422 extern "C" {
3423 #endif
3424
3425 enum mg_resolve_err {
3426 MG_RESOLVE_OK = 0,
3427 MG_RESOLVE_NO_ANSWERS = 1,
3428 MG_RESOLVE_EXCEEDED_RETRY_COUNT = 2,
3429 MG_RESOLVE_TIMEOUT = 3
3430 };
3431
3432 typedef void (*mg_resolve_callback_t)(struct mg_dns_message *dns_message,
3433 void *user_data, enum mg_resolve_err);
3434
3435
3436 struct mg_resolve_async_opts {
3437 const char *nameserver_url;
3438 int max_retries;
3439 int timeout;
3440 int accept_literal;
3441 int only_literal;
3442 struct mg_connection **dns_conn;
3443 };
3444
3445
3446 int mg_resolve_async(struct mg_mgr *mgr, const char *name, int query,
3447 mg_resolve_callback_t cb, void *data);
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470 int mg_resolve_async_opt(struct mg_mgr *mgr, const char *name, int query,
3471 mg_resolve_callback_t cb, void *data,
3472 struct mg_resolve_async_opts opts);
3473
3474
3475
3476
3477
3478
3479 int mg_resolve_from_hosts_file(const char *host, union socket_address *usa);
3480
3481 #ifdef __cplusplus
3482 }
3483 #endif
3484 #endif
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516 #ifndef CS_MONGOOSE_SRC_COAP_H_
3517 #define CS_MONGOOSE_SRC_COAP_H_
3518
3519 #ifdef MG_ENABLE_COAP
3520
3521 #define MG_COAP_MSG_TYPE_FIELD 0x2
3522 #define MG_COAP_CODE_CLASS_FIELD 0x4
3523 #define MG_COAP_CODE_DETAIL_FIELD 0x8
3524 #define MG_COAP_MSG_ID_FIELD 0x10
3525 #define MG_COAP_TOKEN_FIELD 0x20
3526 #define MG_COAP_OPTIOMG_FIELD 0x40
3527 #define MG_COAP_PAYLOAD_FIELD 0x80
3528
3529 #define MG_COAP_ERROR 0x10000
3530 #define MG_COAP_FORMAT_ERROR (MG_COAP_ERROR | 0x20000)
3531 #define MG_COAP_IGNORE (MG_COAP_ERROR | 0x40000)
3532 #define MG_COAP_NOT_ENOUGH_DATA (MG_COAP_ERROR | 0x80000)
3533 #define MG_COAP_NETWORK_ERROR (MG_COAP_ERROR | 0x100000)
3534
3535 #define MG_COAP_MSG_CON 0
3536 #define MG_COAP_MSG_NOC 1
3537 #define MG_COAP_MSG_ACK 2
3538 #define MG_COAP_MSG_RST 3
3539 #define MG_COAP_MSG_MAX 3
3540
3541 #define MG_COAP_CODECLASS_REQUEST 0
3542 #define MG_COAP_CODECLASS_RESP_OK 2
3543 #define MG_COAP_CODECLASS_CLIENT_ERR 4
3544 #define MG_COAP_CODECLASS_SRV_ERR 5
3545
3546 #define MG_COAP_EVENT_BASE 300
3547 #define MG_EV_COAP_CON (MG_COAP_EVENT_BASE + MG_COAP_MSG_CON)
3548 #define MG_EV_COAP_NOC (MG_COAP_EVENT_BASE + MG_COAP_MSG_NOC)
3549 #define MG_EV_COAP_ACK (MG_COAP_EVENT_BASE + MG_COAP_MSG_ACK)
3550 #define MG_EV_COAP_RST (MG_COAP_EVENT_BASE + MG_COAP_MSG_RST)
3551
3552
3553
3554
3555
3556
3557 struct mg_coap_option {
3558 struct mg_coap_option *next;
3559 uint32_t number;
3560 struct mg_str value;
3561 };
3562
3563
3564 struct mg_coap_message {
3565 uint32_t flags;
3566 uint8_t msg_type;
3567 uint8_t code_class;
3568 uint8_t code_detail;
3569 uint16_t msg_id;
3570 struct mg_str token;
3571 struct mg_coap_option *options;
3572 struct mg_str payload;
3573 struct mg_coap_option *optiomg_tail;
3574 };
3575
3576 #ifdef __cplusplus
3577 extern "C" {
3578 #endif
3579
3580
3581 int mg_set_protocol_coap(struct mg_connection *nc);
3582
3583
3584
3585
3586
3587 struct mg_coap_option *mg_coap_add_option(struct mg_coap_message *cm,
3588 uint32_t number, char *value,
3589 size_t len);
3590
3591
3592
3593
3594
3595 void mg_coap_free_options(struct mg_coap_message *cm);
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608 uint32_t mg_coap_send_message(struct mg_connection *nc,
3609 struct mg_coap_message *cm);
3610
3611
3612
3613
3614
3615
3616 uint32_t mg_coap_send_ack(struct mg_connection *nc, uint16_t msg_id);
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633 uint32_t mg_coap_parse(struct mbuf *io, struct mg_coap_message *cm);
3634
3635
3636
3637
3638
3639
3640 uint32_t mg_coap_compose(struct mg_coap_message *cm, struct mbuf *io);
3641
3642 #ifdef __cplusplus
3643 }
3644 #endif
3645
3646 #endif
3647
3648 #endif