Project

General

Profile

Feature #2953 » mongoose.c

yan hong, 04/18/2023 10:43 AM

 
1
#include "mongoose.h"
2
#ifdef MG_MODULE_LINES
3
#line 1 "mongoose/src/mg_internal.h"
4
#endif
5
/*
6
 * Copyright (c) 2014 Cesanta Software Limited
7
 * All rights reserved
8
 */
9

    
10
#ifndef CS_MONGOOSE_SRC_INTERNAL_H_
11
#define CS_MONGOOSE_SRC_INTERNAL_H_
12

    
13
/* Amalgamated: #include "common/mg_mem.h" */
14

    
15
#ifndef MBUF_REALLOC
16
#define MBUF_REALLOC MG_REALLOC
17
#endif
18

    
19
#ifndef MBUF_FREE
20
#define MBUF_FREE MG_FREE
21
#endif
22

    
23
#define MG_SET_PTRPTR(_ptr, _v) \
24
  do {                          \
25
    if (_ptr) *(_ptr) = _v;     \
26
  } while (0)
27

    
28
#ifndef MG_INTERNAL
29
#define MG_INTERNAL static
30
#endif
31

    
32
#ifdef PICOTCP
33
#define NO_LIBC
34
#define MG_DISABLE_PFS
35
#endif
36

    
37
/* Amalgamated: #include "common/cs_dbg.h" */
38
/* Amalgamated: #include "mg_http.h" */
39
/* Amalgamated: #include "mg_net.h" */
40

    
41
#define MG_CTL_MSG_MESSAGE_SIZE 8192
42

    
43
/* internals that need to be accessible in unit tests */
44
MG_INTERNAL struct mg_connection *mg_do_connect(struct mg_connection *nc,
45
                                                int proto,
46
                                                union socket_address *sa);
47

    
48
MG_INTERNAL int mg_parse_address(const char *str, union socket_address *sa,
49
                                 int *proto, char *host, size_t host_len);
50
MG_INTERNAL void mg_call(struct mg_connection *nc,
51
                         mg_event_handler_t ev_handler, void *user_data, int ev,
52
                         void *ev_data);
53
void mg_forward(struct mg_connection *from, struct mg_connection *to);
54
MG_INTERNAL void mg_add_conn(struct mg_mgr *mgr, struct mg_connection *c);
55
MG_INTERNAL void mg_remove_conn(struct mg_connection *c);
56
MG_INTERNAL struct mg_connection *mg_create_connection(
57
    struct mg_mgr *mgr, mg_event_handler_t callback,
58
    struct mg_add_sock_opts opts);
59
#ifdef _WIN32
60
/* Retur value is the same as for MultiByteToWideChar. */
61
int to_wchar(const char *path, wchar_t *wbuf, size_t wbuf_len);
62
#endif
63

    
64
struct ctl_msg {
65
  mg_event_handler_t callback;
66
  char message[MG_CTL_MSG_MESSAGE_SIZE];
67
};
68

    
69
#if MG_ENABLE_MQTT
70
struct mg_mqtt_message;
71

    
72
#define MG_MQTT_ERROR_INCOMPLETE_MSG -1
73
#define MG_MQTT_ERROR_MALFORMED_MSG -2
74

    
75
MG_INTERNAL int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm);
76
#endif
77

    
78
/* Forward declarations for testing. */
79
extern void *(*test_malloc)(size_t size);
80
extern void *(*test_calloc)(size_t count, size_t size);
81

    
82
#ifndef MIN
83
#define MIN(a, b) ((a) < (b) ? (a) : (b))
84
#endif
85

    
86
#if MG_ENABLE_HTTP
87
struct mg_serve_http_opts;
88

    
89
/*
90
 * Reassemble the content of the buffer (buf, blen) which should be
91
 * in the HTTP chunked encoding, by collapsing data chunks to the
92
 * beginning of the buffer.
93
 *
94
 * If chunks get reassembled, modify hm->body to point to the reassembled
95
 * body and fire MG_EV_HTTP_CHUNK event. If handler sets MG_F_DELETE_CHUNK
96
 * in nc->flags, delete reassembled body from the mbuf.
97
 *
98
 * Return reassembled body size.
99
 */
100
MG_INTERNAL size_t mg_handle_chunked(struct mg_connection *nc,
101
                                     struct http_message *hm, char *buf,
102
                                     size_t blen);
103

    
104
#if MG_ENABLE_FILESYSTEM
105
MG_INTERNAL int mg_uri_to_local_path(struct http_message *hm,
106
                                     const struct mg_serve_http_opts *opts,
107
                                     char **local_path,
108
                                     struct mg_str *remainder);
109
MG_INTERNAL time_t mg_parse_date_string(const char *datetime);
110
MG_INTERNAL int mg_is_not_modified(struct http_message *hm, cs_stat_t *st);
111
#endif
112
#if MG_ENABLE_HTTP_CGI
113
MG_INTERNAL void mg_handle_cgi(struct mg_connection *nc, const char *prog,
114
                               const struct mg_str *path_info,
115
                               const struct http_message *hm,
116
                               const struct mg_serve_http_opts *opts);
117
struct mg_http_proto_data_cgi;
118
MG_INTERNAL void mg_http_free_proto_data_cgi(struct mg_http_proto_data_cgi *d);
119
#endif
120
#if MG_ENABLE_HTTP_SSI
121
MG_INTERNAL void mg_handle_ssi_request(struct mg_connection *nc,
122
                                       struct http_message *hm,
123
                                       const char *path,
124
                                       const struct mg_serve_http_opts *opts);
125
#endif
126
#if MG_ENABLE_HTTP_WEBDAV
127
MG_INTERNAL int mg_is_dav_request(const struct mg_str *s);
128
MG_INTERNAL void mg_handle_propfind(struct mg_connection *nc, const char *path,
129
                                    cs_stat_t *stp, struct http_message *hm,
130
                                    struct mg_serve_http_opts *opts);
131
MG_INTERNAL void mg_handle_lock(struct mg_connection *nc, const char *path);
132
MG_INTERNAL void mg_handle_mkcol(struct mg_connection *nc, const char *path,
133
                                 struct http_message *hm);
134
MG_INTERNAL void mg_handle_move(struct mg_connection *c,
135
                                const struct mg_serve_http_opts *opts,
136
                                const char *path, struct http_message *hm);
137
MG_INTERNAL void mg_handle_delete(struct mg_connection *nc,
138
                                  const struct mg_serve_http_opts *opts,
139
                                  const char *path);
140
MG_INTERNAL void mg_handle_put(struct mg_connection *nc, const char *path,
141
                               struct http_message *hm);
142
#endif
143
#if MG_ENABLE_HTTP_WEBSOCKET
144
MG_INTERNAL void mg_ws_handler(struct mg_connection *nc, int ev,
145
                               void *ev_data MG_UD_ARG(void *user_data));
146
MG_INTERNAL void mg_ws_handshake(struct mg_connection *nc,
147
                                 const struct mg_str *key,
148
                                 struct http_message *);
149
#endif
150
#endif /* MG_ENABLE_HTTP */
151

    
152
MG_INTERNAL int mg_get_errno(void);
153

    
154
MG_INTERNAL void mg_close_conn(struct mg_connection *conn);
155

    
156
#if MG_ENABLE_SNTP
157
MG_INTERNAL int mg_sntp_parse_reply(const char *buf, int len,
158
                                    struct mg_sntp_message *msg);
159
#endif
160

    
161
#endif /* CS_MONGOOSE_SRC_INTERNAL_H_ */
162
#ifdef MG_MODULE_LINES
163
#line 1 "common/mg_mem.h"
164
#endif
165
/*
166
 * Copyright (c) 2014-2016 Cesanta Software Limited
167
 * All rights reserved
168
 */
169

    
170
#ifndef CS_COMMON_MG_MEM_H_
171
#define CS_COMMON_MG_MEM_H_
172

    
173
#ifdef __cplusplus
174
extern "C" {
175
#endif
176

    
177
#ifndef MG_MALLOC
178
#define MG_MALLOC malloc
179
#endif
180

    
181
#ifndef MG_CALLOC
182
#define MG_CALLOC calloc
183
#endif
184

    
185
#ifndef MG_REALLOC
186
#define MG_REALLOC realloc
187
#endif
188

    
189
#ifndef MG_FREE
190
#define MG_FREE free
191
#endif
192

    
193
#ifdef __cplusplus
194
}
195
#endif
196

    
197
#endif /* CS_COMMON_MG_MEM_H_ */
198
#ifdef MG_MODULE_LINES
199
#line 1 "common/cs_base64.c"
200
#endif
201
/*
202
 * Copyright (c) 2014 Cesanta Software Limited
203
 * All rights reserved
204
 */
205

    
206
#ifndef EXCLUDE_COMMON
207

    
208
/* Amalgamated: #include "common/cs_base64.h" */
209

    
210
#include <string.h>
211

    
212
/* Amalgamated: #include "common/cs_dbg.h" */
213

    
214
/* ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ */
215

    
216
#define NUM_UPPERCASES ('Z' - 'A' + 1)
217
#define NUM_LETTERS (NUM_UPPERCASES * 2)
218
#define NUM_DIGITS ('9' - '0' + 1)
219

    
220
/*
221
 * Emit a base64 code char.
222
 *
223
 * Doesn't use memory, thus it's safe to use to safely dump memory in crashdumps
224
 */
225
static void cs_base64_emit_code(struct cs_base64_ctx *ctx, int v) {
226
  if (v < NUM_UPPERCASES) {
227
    ctx->b64_putc(v + 'A', ctx->user_data);
228
  } else if (v < (NUM_LETTERS)) {
229
    ctx->b64_putc(v - NUM_UPPERCASES + 'a', ctx->user_data);
230
  } else if (v < (NUM_LETTERS + NUM_DIGITS)) {
231
    ctx->b64_putc(v - NUM_LETTERS + '0', ctx->user_data);
232
  } else {
233
    ctx->b64_putc(v - NUM_LETTERS - NUM_DIGITS == 0 ? '+' : '/',
234
                  ctx->user_data);
235
  }
236
}
237

    
238
static void cs_base64_emit_chunk(struct cs_base64_ctx *ctx) {
239
  int a, b, c;
240

    
241
  a = ctx->chunk[0];
242
  b = ctx->chunk[1];
243
  c = ctx->chunk[2];
244

    
245
  cs_base64_emit_code(ctx, a >> 2);
246
  cs_base64_emit_code(ctx, ((a & 3) << 4) | (b >> 4));
247
  if (ctx->chunk_size > 1) {
248
    cs_base64_emit_code(ctx, (b & 15) << 2 | (c >> 6));
249
  }
250
  if (ctx->chunk_size > 2) {
251
    cs_base64_emit_code(ctx, c & 63);
252
  }
253
}
254

    
255
void cs_base64_init(struct cs_base64_ctx *ctx, cs_base64_putc_t b64_putc,
256
                    void *user_data) {
257
  ctx->chunk_size = 0;
258
  ctx->b64_putc = b64_putc;
259
  ctx->user_data = user_data;
260
}
261

    
262
void cs_base64_update(struct cs_base64_ctx *ctx, const char *str, size_t len) {
263
  const unsigned char *src = (const unsigned char *) str;
264
  size_t i;
265
  for (i = 0; i < len; i++) {
266
    ctx->chunk[ctx->chunk_size++] = src[i];
267
    if (ctx->chunk_size == 3) {
268
      cs_base64_emit_chunk(ctx);
269
      ctx->chunk_size = 0;
270
    }
271
  }
272
}
273

    
274
void cs_base64_finish(struct cs_base64_ctx *ctx) {
275
  if (ctx->chunk_size > 0) {
276
    int i;
277
    memset(&ctx->chunk[ctx->chunk_size], 0, 3 - ctx->chunk_size);
278
    cs_base64_emit_chunk(ctx);
279
    for (i = 0; i < (3 - ctx->chunk_size); i++) {
280
      ctx->b64_putc('=', ctx->user_data);
281
    }
282
  }
283
}
284

    
285
#define BASE64_ENCODE_BODY                                                \
286
  static const char *b64 =                                                \
287
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; \
288
  int i, j, a, b, c;                                                      \
289
                                                                          \
290
  for (i = j = 0; i < src_len; i += 3) {                                  \
291
    a = src[i];                                                           \
292
    b = i + 1 >= src_len ? 0 : src[i + 1];                                \
293
    c = i + 2 >= src_len ? 0 : src[i + 2];                                \
294
                                                                          \
295
    BASE64_OUT(b64[a >> 2]);                                              \
296
    BASE64_OUT(b64[((a & 3) << 4) | (b >> 4)]);                           \
297
    if (i + 1 < src_len) {                                                \
298
      BASE64_OUT(b64[(b & 15) << 2 | (c >> 6)]);                          \
299
    }                                                                     \
300
    if (i + 2 < src_len) {                                                \
301
      BASE64_OUT(b64[c & 63]);                                            \
302
    }                                                                     \
303
  }                                                                       \
304
                                                                          \
305
  while (j % 4 != 0) {                                                    \
306
    BASE64_OUT('=');                                                      \
307
  }                                                                       \
308
  BASE64_FLUSH()
309

    
310
#define BASE64_OUT(ch) \
311
  do {                 \
312
    dst[j++] = (ch);   \
313
  } while (0)
314

    
315
#define BASE64_FLUSH() \
316
  do {                 \
317
    dst[j++] = '\0';   \
318
  } while (0)
319

    
320
void cs_base64_encode(const unsigned char *src, int src_len, char *dst) {
321
  BASE64_ENCODE_BODY;
322
}
323

    
324
#undef BASE64_OUT
325
#undef BASE64_FLUSH
326

    
327
#if CS_ENABLE_STDIO
328
#define BASE64_OUT(ch)      \
329
  do {                      \
330
    fprintf(f, "%c", (ch)); \
331
    j++;                    \
332
  } while (0)
333

    
334
#define BASE64_FLUSH()
335

    
336
void cs_fprint_base64(FILE *f, const unsigned char *src, int src_len) {
337
  BASE64_ENCODE_BODY;
338
}
339

    
340
#undef BASE64_OUT
341
#undef BASE64_FLUSH
342
#endif /* CS_ENABLE_STDIO */
343

    
344
/* Convert one byte of encoded base64 input stream to 6-bit chunk */
345
static unsigned char from_b64(unsigned char ch) {
346
  /* Inverse lookup map */
347
  static const unsigned char tab[128] = {
348
      255, 255, 255, 255,
349
      255, 255, 255, 255, /*  0 */
350
      255, 255, 255, 255,
351
      255, 255, 255, 255, /*  8 */
352
      255, 255, 255, 255,
353
      255, 255, 255, 255, /*  16 */
354
      255, 255, 255, 255,
355
      255, 255, 255, 255, /*  24 */
356
      255, 255, 255, 255,
357
      255, 255, 255, 255, /*  32 */
358
      255, 255, 255, 62,
359
      255, 255, 255, 63, /*  40 */
360
      52,  53,  54,  55,
361
      56,  57,  58,  59, /*  48 */
362
      60,  61,  255, 255,
363
      255, 200, 255, 255, /*  56   '=' is 200, on index 61 */
364
      255, 0,   1,   2,
365
      3,   4,   5,   6, /*  64 */
366
      7,   8,   9,   10,
367
      11,  12,  13,  14, /*  72 */
368
      15,  16,  17,  18,
369
      19,  20,  21,  22, /*  80 */
370
      23,  24,  25,  255,
371
      255, 255, 255, 255, /*  88 */
372
      255, 26,  27,  28,
373
      29,  30,  31,  32, /*  96 */
374
      33,  34,  35,  36,
375
      37,  38,  39,  40, /*  104 */
376
      41,  42,  43,  44,
377
      45,  46,  47,  48, /*  112 */
378
      49,  50,  51,  255,
379
      255, 255, 255, 255, /*  120 */
380
  };
381
  return tab[ch & 127];
382
}
383

    
384
int cs_base64_decode(const unsigned char *s, int len, char *dst, int *dec_len) {
385
  unsigned char a, b, c, d;
386
  int orig_len = len;
387
  char *orig_dst = dst;
388
  while (len >= 4 && (a = from_b64(s[0])) != 255 &&
389
         (b = from_b64(s[1])) != 255 && (c = from_b64(s[2])) != 255 &&
390
         (d = from_b64(s[3])) != 255) {
391
    s += 4;
392
    len -= 4;
393
    if (a == 200 || b == 200) break; /* '=' can't be there */
394
    *dst++ = a << 2 | b >> 4;
395
    if (c == 200) break;
396
    *dst++ = b << 4 | c >> 2;
397
    if (d == 200) break;
398
    *dst++ = c << 6 | d;
399
  }
400
  *dst = 0;
401
  if (dec_len != NULL) *dec_len = (dst - orig_dst);
402
  return orig_len - len;
403
}
404

    
405
#endif /* EXCLUDE_COMMON */
406
#ifdef MG_MODULE_LINES
407
#line 1 "common/cs_dbg.h"
408
#endif
409
/*
410
 * Copyright (c) 2014-2016 Cesanta Software Limited
411
 * All rights reserved
412
 */
413

    
414
#ifndef CS_COMMON_CS_DBG_H_
415
#define CS_COMMON_CS_DBG_H_
416

    
417
/* Amalgamated: #include "common/platform.h" */
418

    
419
#if CS_ENABLE_STDIO
420
#include <stdio.h>
421
#endif
422

    
423
#ifndef CS_ENABLE_DEBUG
424
#define CS_ENABLE_DEBUG 0
425
#endif
426

    
427
#ifndef CS_LOG_ENABLE_TS_DIFF
428
#define CS_LOG_ENABLE_TS_DIFF 0
429
#endif
430

    
431
#ifdef __cplusplus
432
extern "C" {
433
#endif /* __cplusplus */
434

    
435
/*
436
 * Log level; `LL_INFO` is the default. Use `cs_log_set_level()` to change it.
437
 */
438
enum cs_log_level {
439
  LL_NONE = -1,
440
  LL_ERROR = 0,
441
  LL_WARN = 1,
442
  LL_INFO = 2,
443
  LL_DEBUG = 3,
444
  LL_VERBOSE_DEBUG = 4,
445

    
446
  _LL_MIN = -2,
447
  _LL_MAX = 5,
448
};
449

    
450
/*
451
 * Set max log level to print; messages with the level above the given one will
452
 * not be printed.
453
 */
454
void cs_log_set_level(enum cs_log_level level);
455

    
456
/*
457
 * Set log filter. NULL (a default) logs everything.
458
 * Otherwise, function name and file name will be tested against the given
459
 * pattern, and only matching messages will be printed.
460
 *
461
 * For the pattern syntax, refer to `mg_match_prefix()` in `str_util.h`.
462
 *
463
 * Example:
464
 * ```c
465
 * void foo(void) {
466
 *   LOG(LL_INFO, ("hello from foo"));
467
 * }
468
 *
469
 * void bar(void) {
470
 *   LOG(LL_INFO, ("hello from bar"));
471
 * }
472
 *
473
 * void test(void) {
474
 *   cs_log_set_filter(NULL);
475
 *   foo();
476
 *   bar();
477
 *
478
 *   cs_log_set_filter("f*");
479
 *   foo();
480
 *   bar(); // Will NOT print anything
481
 *
482
 *   cs_log_set_filter("bar");
483
 *   foo(); // Will NOT print anything
484
 *   bar();
485
 * }
486
 * ```
487
 */
488
void cs_log_set_filter(const char *pattern);
489

    
490
/*
491
 * Helper function which prints message prefix with the given `level`, function
492
 * name `func` and `filename`. If message should be printed (accordingly to the
493
 * current log level and filter), prints the prefix and returns 1, otherwise
494
 * returns 0.
495
 *
496
 * Clients should typically just use `LOG()` macro.
497
 */
498
int cs_log_print_prefix(enum cs_log_level level, const char *func,
499
                        const char *filename);
500

    
501
extern enum cs_log_level cs_log_threshold;
502

    
503
#if CS_ENABLE_STDIO
504

    
505
/*
506
 * Set file to write logs into. If `NULL`, logs go to `stderr`.
507
 */
508
void cs_log_set_file(FILE *file);
509

    
510
/*
511
 * Prints log to the current log file, appends "\n" in the end and flushes the
512
 * stream.
513
 */
514
void cs_log_printf(const char *fmt, ...)
515
#ifdef __GNUC__
516
    __attribute__((format(printf, 1, 2)))
517
#endif
518
    ;
519

    
520
/*
521
 * Format and print message `x` with the given level `l`. Example:
522
 *
523
 * ```c
524
 * LOG(LL_INFO, ("my info message: %d", 123));
525
 * LOG(LL_DEBUG, ("my debug message: %d", 123));
526
 * ```
527
 */
528
#define LOG(l, x)                                                    \
529
  do {                                                               \
530
    if (cs_log_print_prefix(l, __func__, __FILE__)) cs_log_printf x; \
531
  } while (0)
532

    
533
#ifndef CS_NDEBUG
534

    
535
/*
536
 * Shortcut for `LOG(LL_VERBOSE_DEBUG, (...))`
537
 */
538
#define DBG(x) LOG(LL_VERBOSE_DEBUG, x)
539

    
540
#else /* NDEBUG */
541

    
542
#define DBG(x)
543

    
544
#endif
545

    
546
#else /* CS_ENABLE_STDIO */
547

    
548
#define LOG(l, x)
549
#define DBG(x)
550

    
551
#endif
552

    
553
#ifdef __cplusplus
554
}
555
#endif /* __cplusplus */
556

    
557
#endif /* CS_COMMON_CS_DBG_H_ */
558
#ifdef MG_MODULE_LINES
559
#line 1 "common/cs_dbg.c"
560
#endif
561
/*
562
 * Copyright (c) 2014-2016 Cesanta Software Limited
563
 * All rights reserved
564
 */
565

    
566
/* Amalgamated: #include "common/cs_dbg.h" */
567

    
568
#include <stdarg.h>
569
#include <stdio.h>
570
#include <string.h>
571

    
572
/* Amalgamated: #include "common/cs_time.h" */
573
/* Amalgamated: #include "common/str_util.h" */
574

    
575
enum cs_log_level cs_log_threshold WEAK =
576
#if CS_ENABLE_DEBUG
577
    LL_VERBOSE_DEBUG;
578
#else
579
    LL_ERROR;
580
#endif
581

    
582
static char *s_filter_pattern = NULL;
583
static size_t s_filter_pattern_len;
584

    
585
void cs_log_set_filter(const char *pattern) WEAK;
586

    
587
#if CS_ENABLE_STDIO
588

    
589
FILE *cs_log_file WEAK = NULL;
590

    
591
#if CS_LOG_ENABLE_TS_DIFF
592
double cs_log_ts WEAK;
593
#endif
594

    
595
enum cs_log_level cs_log_cur_msg_level WEAK = LL_NONE;
596

    
597
void cs_log_set_filter(const char *pattern) {
598
  free(s_filter_pattern);
599
  if (pattern != NULL) {
600
    s_filter_pattern = strdup(pattern);
601
    s_filter_pattern_len = strlen(pattern);
602
  } else {
603
    s_filter_pattern = NULL;
604
    s_filter_pattern_len = 0;
605
  }
606
}
607

    
608
int cs_log_print_prefix(enum cs_log_level, const char *, const char *) WEAK;
609
int cs_log_print_prefix(enum cs_log_level level, const char *func,
610
                        const char *filename) {
611
  char prefix[21];
612

    
613
  if (level > cs_log_threshold) return 0;
614
  if (s_filter_pattern != NULL &&
615
      mg_match_prefix(s_filter_pattern, s_filter_pattern_len, func) == 0 &&
616
      mg_match_prefix(s_filter_pattern, s_filter_pattern_len, filename) == 0) {
617
    return 0;
618
  }
619

    
620
  strncpy(prefix, func, 20);
621
  prefix[20] = '\0';
622
  if (cs_log_file == NULL) cs_log_file = stderr;
623
  cs_log_cur_msg_level = level;
624
  fprintf(cs_log_file, "%-20s ", prefix);
625
#if CS_LOG_ENABLE_TS_DIFF
626
  {
627
    double now = cs_time();
628
    fprintf(cs_log_file, "%7u ", (unsigned int) ((now - cs_log_ts) * 1000000));
629
    cs_log_ts = now;
630
  }
631
#endif
632
  return 1;
633
}
634

    
635
void cs_log_printf(const char *fmt, ...) WEAK;
636
void cs_log_printf(const char *fmt, ...) {
637
  va_list ap;
638
  va_start(ap, fmt);
639
  vfprintf(cs_log_file, fmt, ap);
640
  va_end(ap);
641
  fputc('\n', cs_log_file);
642
  fflush(cs_log_file);
643
  cs_log_cur_msg_level = LL_NONE;
644
}
645

    
646
void cs_log_set_file(FILE *file) WEAK;
647
void cs_log_set_file(FILE *file) {
648
  cs_log_file = file;
649
}
650

    
651
#else
652

    
653
void cs_log_set_filter(const char *pattern) {
654
  (void) pattern;
655
}
656

    
657
#endif /* CS_ENABLE_STDIO */
658

    
659
void cs_log_set_level(enum cs_log_level level) WEAK;
660
void cs_log_set_level(enum cs_log_level level) {
661
  cs_log_threshold = level;
662
#if CS_LOG_ENABLE_TS_DIFF && CS_ENABLE_STDIO
663
  cs_log_ts = cs_time();
664
#endif
665
}
666
#ifdef MG_MODULE_LINES
667
#line 1 "common/cs_dirent.h"
668
#endif
669
/*
670
 * Copyright (c) 2014-2016 Cesanta Software Limited
671
 * All rights reserved
672
 */
673

    
674
#ifndef CS_COMMON_CS_DIRENT_H_
675
#define CS_COMMON_CS_DIRENT_H_
676

    
677
#include <limits.h>
678

    
679
/* Amalgamated: #include "common/platform.h" */
680

    
681
#ifdef __cplusplus
682
extern "C" {
683
#endif /* __cplusplus */
684

    
685
#ifdef CS_DEFINE_DIRENT
686
typedef struct { int dummy; } DIR;
687

    
688
struct dirent {
689
  int d_ino;
690
#ifdef _WIN32
691
  char d_name[MAX_PATH];
692
#else
693
  /* TODO(rojer): Use PATH_MAX but make sure it's sane on every platform */
694
  char d_name[256];
695
#endif
696
};
697

    
698
DIR *opendir(const char *dir_name);
699
int closedir(DIR *dir);
700
struct dirent *readdir(DIR *dir);
701
#endif /* CS_DEFINE_DIRENT */
702

    
703
#ifdef __cplusplus
704
}
705
#endif /* __cplusplus */
706

    
707
#endif /* CS_COMMON_CS_DIRENT_H_ */
708
#ifdef MG_MODULE_LINES
709
#line 1 "common/cs_dirent.c"
710
#endif
711
/*
712
 * Copyright (c) 2015 Cesanta Software Limited
713
 * All rights reserved
714
 */
715

    
716
#ifndef EXCLUDE_COMMON
717

    
718
/* Amalgamated: #include "common/mg_mem.h" */
719
/* Amalgamated: #include "common/cs_dirent.h" */
720

    
721
/*
722
 * This file contains POSIX opendir/closedir/readdir API implementation
723
 * for systems which do not natively support it (e.g. Windows).
724
 */
725

    
726
#ifdef _WIN32
727
struct win32_dir {
728
  DIR d;
729
  HANDLE handle;
730
  WIN32_FIND_DATAW info;
731
  struct dirent result;
732
};
733

    
734
DIR *opendir(const char *name) {
735
  struct win32_dir *dir = NULL;
736
  wchar_t wpath[MAX_PATH];
737
  DWORD attrs;
738

    
739
  if (name == NULL) {
740
    SetLastError(ERROR_BAD_ARGUMENTS);
741
  } else if ((dir = (struct win32_dir *) MG_MALLOC(sizeof(*dir))) == NULL) {
742
    SetLastError(ERROR_NOT_ENOUGH_MEMORY);
743
  } else {
744
    to_wchar(name, wpath, ARRAY_SIZE(wpath));
745
    attrs = GetFileAttributesW(wpath);
746
    if (attrs != 0xFFFFFFFF && (attrs & FILE_ATTRIBUTE_DIRECTORY)) {
747
      (void) wcscat(wpath, L"\\*");
748
      dir->handle = FindFirstFileW(wpath, &dir->info);
749
      dir->result.d_name[0] = '\0';
750
    } else {
751
      MG_FREE(dir);
752
      dir = NULL;
753
    }
754
  }
755

    
756
  return (DIR *) dir;
757
}
758

    
759
int closedir(DIR *d) {
760
  struct win32_dir *dir = (struct win32_dir *) d;
761
  int result = 0;
762

    
763
  if (dir != NULL) {
764
    if (dir->handle != INVALID_HANDLE_VALUE)
765
      result = FindClose(dir->handle) ? 0 : -1;
766
    MG_FREE(dir);
767
  } else {
768
    result = -1;
769
    SetLastError(ERROR_BAD_ARGUMENTS);
770
  }
771

    
772
  return result;
773
}
774

    
775
struct dirent *readdir(DIR *d) {
776
  struct win32_dir *dir = (struct win32_dir *) d;
777
  struct dirent *result = NULL;
778

    
779
  if (dir) {
780
    memset(&dir->result, 0, sizeof(dir->result));
781
    if (dir->handle != INVALID_HANDLE_VALUE) {
782
      result = &dir->result;
783
      (void) WideCharToMultiByte(CP_UTF8, 0, dir->info.cFileName, -1,
784
                                 result->d_name, sizeof(result->d_name), NULL,
785
                                 NULL);
786

    
787
      if (!FindNextFileW(dir->handle, &dir->info)) {
788
        (void) FindClose(dir->handle);
789
        dir->handle = INVALID_HANDLE_VALUE;
790
      }
791

    
792
    } else {
793
      SetLastError(ERROR_FILE_NOT_FOUND);
794
    }
795
  } else {
796
    SetLastError(ERROR_BAD_ARGUMENTS);
797
  }
798

    
799
  return result;
800
}
801
#endif
802

    
803
#endif /* EXCLUDE_COMMON */
804

    
805
/* ISO C requires a translation unit to contain at least one declaration */
806
typedef int cs_dirent_dummy;
807
#ifdef MG_MODULE_LINES
808
#line 1 "common/cs_time.c"
809
#endif
810
/*
811
 * Copyright (c) 2014-2016 Cesanta Software Limited
812
 * All rights reserved
813
 */
814

    
815
/* Amalgamated: #include "common/cs_time.h" */
816

    
817
#ifndef _WIN32
818
#include <stddef.h>
819
/*
820
 * There is no sys/time.h on ARMCC.
821
 */
822
#if !(defined(__ARMCC_VERSION) || defined(__ICCARM__)) && \
823
    !defined(__TI_COMPILER_VERSION__) &&                  \
824
    (!defined(CS_PLATFORM) || CS_PLATFORM != CS_P_NXP_LPC)
825
#include <sys/time.h>
826
#endif
827
#else
828
#include <windows.h>
829
#endif
830

    
831
double cs_time(void) WEAK;
832
double cs_time(void) {
833
  double now;
834
#ifndef _WIN32
835
  struct timeval tv;
836
  if (gettimeofday(&tv, NULL /* tz */) != 0) return 0;
837
  now = (double) tv.tv_sec + (((double) tv.tv_usec) / 1000000.0);
838
#else
839
  SYSTEMTIME sysnow;
840
  FILETIME ftime;
841
  GetLocalTime(&sysnow);
842
  SystemTimeToFileTime(&sysnow, &ftime);
843
  /*
844
   * 1. VC 6.0 doesn't support conversion uint64 -> double, so, using int64
845
   * This should not cause a problems in this (21th) century
846
   * 2. Windows FILETIME is a number of 100-nanosecond intervals since January
847
   * 1, 1601 while time_t is a number of _seconds_ since January 1, 1970 UTC,
848
   * thus, we need to convert to seconds and adjust amount (subtract 11644473600
849
   * seconds)
850
   */
851
  now = (double) (((int64_t) ftime.dwLowDateTime +
852
                   ((int64_t) ftime.dwHighDateTime << 32)) /
853
                  10000000.0) -
854
        11644473600;
855
#endif /* _WIN32 */
856
  return now;
857
}
858

    
859
double cs_timegm(const struct tm *tm) {
860
  /* Month-to-day offset for non-leap-years. */
861
  static const int month_day[12] = {0,   31,  59,  90,  120, 151,
862
                                    181, 212, 243, 273, 304, 334};
863

    
864
  /* Most of the calculation is easy; leap years are the main difficulty. */
865
  int month = tm->tm_mon % 12;
866
  int year = tm->tm_year + tm->tm_mon / 12;
867
  int year_for_leap;
868
  int64_t rt;
869

    
870
  if (month < 0) { /* Negative values % 12 are still negative. */
871
    month += 12;
872
    --year;
873
  }
874

    
875
  /* This is the number of Februaries since 1900. */
876
  year_for_leap = (month > 1) ? year + 1 : year;
877

    
878
  rt =
879
      tm->tm_sec /* Seconds */
880
      +
881
      60 *
882
          (tm->tm_min /* Minute = 60 seconds */
883
           +
884
           60 * (tm->tm_hour /* Hour = 60 minutes */
885
                 +
886
                 24 * (month_day[month] + tm->tm_mday - 1 /* Day = 24 hours */
887
                       + 365 * (year - 70)                /* Year = 365 days */
888
                       + (year_for_leap - 69) / 4 /* Every 4 years is leap... */
889
                       - (year_for_leap - 1) / 100 /* Except centuries... */
890
                       + (year_for_leap + 299) / 400))); /* Except 400s. */
891
  return rt < 0 ? -1 : (double) rt;
892
}
893
#ifdef MG_MODULE_LINES
894
#line 1 "common/cs_endian.h"
895
#endif
896
/*
897
 * Copyright (c) 2014-2016 Cesanta Software Limited
898
 * All rights reserved
899
 */
900

    
901
#ifndef CS_COMMON_CS_ENDIAN_H_
902
#define CS_COMMON_CS_ENDIAN_H_
903

    
904
#ifdef __cplusplus
905
extern "C" {
906
#endif
907

    
908
/*
909
 * clang with std=-c99 uses __LITTLE_ENDIAN, by default
910
 * while for ex, RTOS gcc - LITTLE_ENDIAN, by default
911
 * it depends on __USE_BSD, but let's have everything
912
 */
913
#if !defined(BYTE_ORDER) && defined(__BYTE_ORDER)
914
#define BYTE_ORDER __BYTE_ORDER
915
#ifndef LITTLE_ENDIAN
916
#define LITTLE_ENDIAN __LITTLE_ENDIAN
917
#endif /* LITTLE_ENDIAN */
918
#ifndef BIG_ENDIAN
919
#define BIG_ENDIAN __LITTLE_ENDIAN
920
#endif /* BIG_ENDIAN */
921
#endif /* BYTE_ORDER */
922

    
923
#ifdef __cplusplus
924
}
925
#endif
926

    
927
#endif /* CS_COMMON_CS_ENDIAN_H_ */
928
#ifdef MG_MODULE_LINES
929
#line 1 "common/cs_md5.c"
930
#endif
931
/*
932
 * This code implements the MD5 message-digest algorithm.
933
 * The algorithm is due to Ron Rivest.  This code was
934
 * written by Colin Plumb in 1993, no copyright is claimed.
935
 * This code is in the public domain; do with it what you wish.
936
 *
937
 * Equivalent code is available from RSA Data Security, Inc.
938
 * This code has been tested against that, and is equivalent,
939
 * except that you don't need to include two pages of legalese
940
 * with every copy.
941
 *
942
 * To compute the message digest of a chunk of bytes, declare an
943
 * MD5Context structure, pass it to MD5Init, call MD5Update as
944
 * needed on buffers full of bytes, and then call MD5Final, which
945
 * will fill a supplied 16-byte array with the digest.
946
 */
947

    
948
/* Amalgamated: #include "common/cs_md5.h" */
949
/* Amalgamated: #include "common/str_util.h" */
950

    
951
#if !defined(EXCLUDE_COMMON)
952
#if !CS_DISABLE_MD5
953

    
954
/* Amalgamated: #include "common/cs_endian.h" */
955

    
956
static void byteReverse(unsigned char *buf, unsigned longs) {
957
/* Forrest: MD5 expect LITTLE_ENDIAN, swap if BIG_ENDIAN */
958
#if BYTE_ORDER == BIG_ENDIAN
959
  do {
960
    uint32_t t = (uint32_t)((unsigned) buf[3] << 8 | buf[2]) << 16 |
961
                 ((unsigned) buf[1] << 8 | buf[0]);
962
    *(uint32_t *) buf = t;
963
    buf += 4;
964
  } while (--longs);
965
#else
966
  (void) buf;
967
  (void) longs;
968
#endif
969
}
970

    
971
#define F1(x, y, z) (z ^ (x & (y ^ z)))
972
#define F2(x, y, z) F1(z, x, y)
973
#define F3(x, y, z) (x ^ y ^ z)
974
#define F4(x, y, z) (y ^ (x | ~z))
975

    
976
#define MD5STEP(f, w, x, y, z, data, s) \
977
  (w += f(x, y, z) + data, w = w << s | w >> (32 - s), w += x)
978

    
979
/*
980
 * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
981
 * initialization constants.
982
 */
983
void cs_md5_init(cs_md5_ctx *ctx) {
984
  ctx->buf[0] = 0x67452301;
985
  ctx->buf[1] = 0xefcdab89;
986
  ctx->buf[2] = 0x98badcfe;
987
  ctx->buf[3] = 0x10325476;
988

    
989
  ctx->bits[0] = 0;
990
  ctx->bits[1] = 0;
991
}
992

    
993
static void cs_md5_transform(uint32_t buf[4], uint32_t const in[16]) {
994
  register uint32_t a, b, c, d;
995

    
996
  a = buf[0];
997
  b = buf[1];
998
  c = buf[2];
999
  d = buf[3];
1000

    
1001
  MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
1002
  MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
1003
  MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
1004
  MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
1005
  MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
1006
  MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
1007
  MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
1008
  MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
1009
  MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
1010
  MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
1011
  MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1012
  MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1013
  MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
1014
  MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1015
  MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1016
  MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1017

    
1018
  MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
1019
  MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
1020
  MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1021
  MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
1022
  MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
1023
  MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
1024
  MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1025
  MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
1026
  MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
1027
  MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
1028
  MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
1029
  MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
1030
  MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
1031
  MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
1032
  MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
1033
  MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1034

    
1035
  MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
1036
  MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
1037
  MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1038
  MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1039
  MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
1040
  MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
1041
  MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
1042
  MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1043
  MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
1044
  MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
1045
  MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
1046
  MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
1047
  MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
1048
  MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1049
  MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1050
  MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
1051

    
1052
  MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
1053
  MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
1054
  MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1055
  MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
1056
  MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
1057
  MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
1058
  MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1059
  MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
1060
  MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
1061
  MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1062
  MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
1063
  MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1064
  MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
1065
  MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1066
  MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
1067
  MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
1068

    
1069
  buf[0] += a;
1070
  buf[1] += b;
1071
  buf[2] += c;
1072
  buf[3] += d;
1073
}
1074

    
1075
void cs_md5_update(cs_md5_ctx *ctx, const unsigned char *buf, size_t len) {
1076
  uint32_t t;
1077

    
1078
  t = ctx->bits[0];
1079
  if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t) ctx->bits[1]++;
1080
  ctx->bits[1] += (uint32_t) len >> 29;
1081

    
1082
  t = (t >> 3) & 0x3f;
1083

    
1084
  if (t) {
1085
    unsigned char *p = (unsigned char *) ctx->in + t;
1086

    
1087
    t = 64 - t;
1088
    if (len < t) {
1089
      memcpy(p, buf, len);
1090
      return;
1091
    }
1092
    memcpy(p, buf, t);
1093
    byteReverse(ctx->in, 16);
1094
    cs_md5_transform(ctx->buf, (uint32_t *) ctx->in);
1095
    buf += t;
1096
    len -= t;
1097
  }
1098

    
1099
  while (len >= 64) {
1100
    memcpy(ctx->in, buf, 64);
1101
    byteReverse(ctx->in, 16);
1102
    cs_md5_transform(ctx->buf, (uint32_t *) ctx->in);
1103
    buf += 64;
1104
    len -= 64;
1105
  }
1106

    
1107
  memcpy(ctx->in, buf, len);
1108
}
1109

    
1110
void cs_md5_final(unsigned char digest[16], cs_md5_ctx *ctx) {
1111
  unsigned count;
1112
  unsigned char *p;
1113
  uint32_t *a;
1114

    
1115
  count = (ctx->bits[0] >> 3) & 0x3F;
1116

    
1117
  p = ctx->in + count;
1118
  *p++ = 0x80;
1119
  count = 64 - 1 - count;
1120
  if (count < 8) {
1121
    memset(p, 0, count);
1122
    byteReverse(ctx->in, 16);
1123
    cs_md5_transform(ctx->buf, (uint32_t *) ctx->in);
1124
    memset(ctx->in, 0, 56);
1125
  } else {
1126
    memset(p, 0, count - 8);
1127
  }
1128
  byteReverse(ctx->in, 14);
1129

    
1130
  a = (uint32_t *) ctx->in;
1131
  a[14] = ctx->bits[0];
1132
  a[15] = ctx->bits[1];
1133

    
1134
  cs_md5_transform(ctx->buf, (uint32_t *) ctx->in);
1135
  byteReverse((unsigned char *) ctx->buf, 4);
1136
  memcpy(digest, ctx->buf, 16);
1137
  memset((char *) ctx, 0, sizeof(*ctx));
1138
}
1139

    
1140
#endif /* CS_DISABLE_MD5 */
1141
#endif /* EXCLUDE_COMMON */
1142
#ifdef MG_MODULE_LINES
1143
#line 1 "common/cs_sha1.c"
1144
#endif
1145
/* Copyright(c) By Steve Reid <steve@edmweb.com> */
1146
/* 100% Public Domain */
1147

    
1148
/* Amalgamated: #include "common/cs_sha1.h" */
1149

    
1150
#if !CS_DISABLE_SHA1 && !defined(EXCLUDE_COMMON)
1151

    
1152
/* Amalgamated: #include "common/cs_endian.h" */
1153

    
1154
#define SHA1HANDSOFF
1155
#if defined(__sun)
1156
/* Amalgamated: #include "common/solarisfixes.h" */
1157
#endif
1158

    
1159
union char64long16 {
1160
  unsigned char c[64];
1161
  uint32_t l[16];
1162
};
1163

    
1164
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
1165

    
1166
static uint32_t blk0(union char64long16 *block, int i) {
1167
/* Forrest: SHA expect BIG_ENDIAN, swap if LITTLE_ENDIAN */
1168
#if BYTE_ORDER == LITTLE_ENDIAN
1169
  block->l[i] =
1170
      (rol(block->l[i], 24) & 0xFF00FF00) | (rol(block->l[i], 8) & 0x00FF00FF);
1171
#endif
1172
  return block->l[i];
1173
}
1174

    
1175
/* Avoid redefine warning (ARM /usr/include/sys/ucontext.h define R0~R4) */
1176
#undef blk
1177
#undef R0
1178
#undef R1
1179
#undef R2
1180
#undef R3
1181
#undef R4
1182

    
1183
#define blk(i)                                                               \
1184
  (block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] ^ \
1185
                              block->l[(i + 2) & 15] ^ block->l[i & 15],     \
1186
                          1))
1187
#define R0(v, w, x, y, z, i)                                          \
1188
  z += ((w & (x ^ y)) ^ y) + blk0(block, i) + 0x5A827999 + rol(v, 5); \
1189
  w = rol(w, 30);
1190
#define R1(v, w, x, y, z, i)                                  \
1191
  z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
1192
  w = rol(w, 30);
1193
#define R2(v, w, x, y, z, i)                          \
1194
  z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
1195
  w = rol(w, 30);
1196
#define R3(v, w, x, y, z, i)                                        \
1197
  z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
1198
  w = rol(w, 30);
1199
#define R4(v, w, x, y, z, i)                          \
1200
  z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
1201
  w = rol(w, 30);
1202

    
1203
void cs_sha1_transform(uint32_t state[5], const unsigned char buffer[64]) {
1204
  uint32_t a, b, c, d, e;
1205
  union char64long16 block[1];
1206

    
1207
  memcpy(block, buffer, 64);
1208
  a = state[0];
1209
  b = state[1];
1210
  c = state[2];
1211
  d = state[3];
1212
  e = state[4];
1213
  R0(a, b, c, d, e, 0);
1214
  R0(e, a, b, c, d, 1);
1215
  R0(d, e, a, b, c, 2);
1216
  R0(c, d, e, a, b, 3);
1217
  R0(b, c, d, e, a, 4);
1218
  R0(a, b, c, d, e, 5);
1219
  R0(e, a, b, c, d, 6);
1220
  R0(d, e, a, b, c, 7);
1221
  R0(c, d, e, a, b, 8);
1222
  R0(b, c, d, e, a, 9);
1223
  R0(a, b, c, d, e, 10);
1224
  R0(e, a, b, c, d, 11);
1225
  R0(d, e, a, b, c, 12);
1226
  R0(c, d, e, a, b, 13);
1227
  R0(b, c, d, e, a, 14);
1228
  R0(a, b, c, d, e, 15);
1229
  R1(e, a, b, c, d, 16);
1230
  R1(d, e, a, b, c, 17);
1231
  R1(c, d, e, a, b, 18);
1232
  R1(b, c, d, e, a, 19);
1233
  R2(a, b, c, d, e, 20);
1234
  R2(e, a, b, c, d, 21);
1235
  R2(d, e, a, b, c, 22);
1236
  R2(c, d, e, a, b, 23);
1237
  R2(b, c, d, e, a, 24);
1238
  R2(a, b, c, d, e, 25);
1239
  R2(e, a, b, c, d, 26);
1240
  R2(d, e, a, b, c, 27);
1241
  R2(c, d, e, a, b, 28);
1242
  R2(b, c, d, e, a, 29);
1243
  R2(a, b, c, d, e, 30);
1244
  R2(e, a, b, c, d, 31);
1245
  R2(d, e, a, b, c, 32);
1246
  R2(c, d, e, a, b, 33);
1247
  R2(b, c, d, e, a, 34);
1248
  R2(a, b, c, d, e, 35);
1249
  R2(e, a, b, c, d, 36);
1250
  R2(d, e, a, b, c, 37);
1251
  R2(c, d, e, a, b, 38);
1252
  R2(b, c, d, e, a, 39);
1253
  R3(a, b, c, d, e, 40);
1254
  R3(e, a, b, c, d, 41);
1255
  R3(d, e, a, b, c, 42);
1256
  R3(c, d, e, a, b, 43);
1257
  R3(b, c, d, e, a, 44);
1258
  R3(a, b, c, d, e, 45);
1259
  R3(e, a, b, c, d, 46);
1260
  R3(d, e, a, b, c, 47);
1261
  R3(c, d, e, a, b, 48);
1262
  R3(b, c, d, e, a, 49);
1263
  R3(a, b, c, d, e, 50);
1264
  R3(e, a, b, c, d, 51);
1265
  R3(d, e, a, b, c, 52);
1266
  R3(c, d, e, a, b, 53);
1267
  R3(b, c, d, e, a, 54);
1268
  R3(a, b, c, d, e, 55);
1269
  R3(e, a, b, c, d, 56);
1270
  R3(d, e, a, b, c, 57);
1271
  R3(c, d, e, a, b, 58);
1272
  R3(b, c, d, e, a, 59);
1273
  R4(a, b, c, d, e, 60);
1274
  R4(e, a, b, c, d, 61);
1275
  R4(d, e, a, b, c, 62);
1276
  R4(c, d, e, a, b, 63);
1277
  R4(b, c, d, e, a, 64);
1278
  R4(a, b, c, d, e, 65);
1279
  R4(e, a, b, c, d, 66);
1280
  R4(d, e, a, b, c, 67);
1281
  R4(c, d, e, a, b, 68);
1282
  R4(b, c, d, e, a, 69);
1283
  R4(a, b, c, d, e, 70);
1284
  R4(e, a, b, c, d, 71);
1285
  R4(d, e, a, b, c, 72);
1286
  R4(c, d, e, a, b, 73);
1287
  R4(b, c, d, e, a, 74);
1288
  R4(a, b, c, d, e, 75);
1289
  R4(e, a, b, c, d, 76);
1290
  R4(d, e, a, b, c, 77);
1291
  R4(c, d, e, a, b, 78);
1292
  R4(b, c, d, e, a, 79);
1293
  state[0] += a;
1294
  state[1] += b;
1295
  state[2] += c;
1296
  state[3] += d;
1297
  state[4] += e;
1298
  /* Erase working structures. The order of operations is important,
1299
   * used to ensure that compiler doesn't optimize those out. */
1300
  memset(block, 0, sizeof(block));
1301
  a = b = c = d = e = 0;
1302
  (void) a;
1303
  (void) b;
1304
  (void) c;
1305
  (void) d;
1306
  (void) e;
1307
}
1308

    
1309
void cs_sha1_init(cs_sha1_ctx *context) {
1310
  context->state[0] = 0x67452301;
1311
  context->state[1] = 0xEFCDAB89;
1312
  context->state[2] = 0x98BADCFE;
1313
  context->state[3] = 0x10325476;
1314
  context->state[4] = 0xC3D2E1F0;
1315
  context->count[0] = context->count[1] = 0;
1316
}
1317

    
1318
void cs_sha1_update(cs_sha1_ctx *context, const unsigned char *data,
1319
                    uint32_t len) {
1320
  uint32_t i, j;
1321

    
1322
  j = context->count[0];
1323
  if ((context->count[0] += len << 3) < j) context->count[1]++;
1324
  context->count[1] += (len >> 29);
1325
  j = (j >> 3) & 63;
1326
  if ((j + len) > 63) {
1327
    memcpy(&context->buffer[j], data, (i = 64 - j));
1328
    cs_sha1_transform(context->state, context->buffer);
1329
    for (; i + 63 < len; i += 64) {
1330
      cs_sha1_transform(context->state, &data[i]);
1331
    }
1332
    j = 0;
1333
  } else
1334
    i = 0;
1335
  memcpy(&context->buffer[j], &data[i], len - i);
1336
}
1337

    
1338
void cs_sha1_final(unsigned char digest[20], cs_sha1_ctx *context) {
1339
  unsigned i;
1340
  unsigned char finalcount[8], c;
1341

    
1342
  for (i = 0; i < 8; i++) {
1343
    finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)] >>
1344
                                      ((3 - (i & 3)) * 8)) &
1345
                                     255);
1346
  }
1347
  c = 0200;
1348
  cs_sha1_update(context, &c, 1);
1349
  while ((context->count[0] & 504) != 448) {
1350
    c = 0000;
1351
    cs_sha1_update(context, &c, 1);
1352
  }
1353
  cs_sha1_update(context, finalcount, 8);
1354
  for (i = 0; i < 20; i++) {
1355
    digest[i] =
1356
        (unsigned char) ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
1357
  }
1358
  memset(context, '\0', sizeof(*context));
1359
  memset(&finalcount, '\0', sizeof(finalcount));
1360
}
1361

    
1362
void cs_hmac_sha1(const unsigned char *key, size_t keylen,
1363
                  const unsigned char *data, size_t datalen,
1364
                  unsigned char out[20]) {
1365
  cs_sha1_ctx ctx;
1366
  unsigned char buf1[64], buf2[64], tmp_key[20], i;
1367

    
1368
  if (keylen > sizeof(buf1)) {
1369
    cs_sha1_init(&ctx);
1370
    cs_sha1_update(&ctx, key, keylen);
1371
    cs_sha1_final(tmp_key, &ctx);
1372
    key = tmp_key;
1373
    keylen = sizeof(tmp_key);
1374
  }
1375

    
1376
  memset(buf1, 0, sizeof(buf1));
1377
  memset(buf2, 0, sizeof(buf2));
1378
  memcpy(buf1, key, keylen);
1379
  memcpy(buf2, key, keylen);
1380

    
1381
  for (i = 0; i < sizeof(buf1); i++) {
1382
    buf1[i] ^= 0x36;
1383
    buf2[i] ^= 0x5c;
1384
  }
1385

    
1386
  cs_sha1_init(&ctx);
1387
  cs_sha1_update(&ctx, buf1, sizeof(buf1));
1388
  cs_sha1_update(&ctx, data, datalen);
1389
  cs_sha1_final(out, &ctx);
1390

    
1391
  cs_sha1_init(&ctx);
1392
  cs_sha1_update(&ctx, buf2, sizeof(buf2));
1393
  cs_sha1_update(&ctx, out, 20);
1394
  cs_sha1_final(out, &ctx);
1395
}
1396

    
1397
#endif /* EXCLUDE_COMMON */
1398
#ifdef MG_MODULE_LINES
1399
#line 1 "common/mbuf.c"
1400
#endif
1401
/*
1402
 * Copyright (c) 2014 Cesanta Software Limited
1403
 * All rights reserved
1404
 */
1405

    
1406
#ifndef EXCLUDE_COMMON
1407

    
1408
#include <assert.h>
1409
#include <string.h>
1410
/* Amalgamated: #include "common/mbuf.h" */
1411

    
1412
#ifndef MBUF_REALLOC
1413
#define MBUF_REALLOC realloc
1414
#endif
1415

    
1416
#ifndef MBUF_FREE
1417
#define MBUF_FREE free
1418
#endif
1419

    
1420
void mbuf_init(struct mbuf *mbuf, size_t initial_size) WEAK;
1421
void mbuf_init(struct mbuf *mbuf, size_t initial_size) {
1422
  mbuf->len = mbuf->size = 0;
1423
  mbuf->buf = NULL;
1424
  mbuf_resize(mbuf, initial_size);
1425
}
1426

    
1427
void mbuf_free(struct mbuf *mbuf) WEAK;
1428
void mbuf_free(struct mbuf *mbuf) {
1429
  if (mbuf->buf != NULL) {
1430
    MBUF_FREE(mbuf->buf);
1431
    mbuf_init(mbuf, 0);
1432
  }
1433
}
1434

    
1435
void mbuf_resize(struct mbuf *a, size_t new_size) WEAK;
1436
void mbuf_resize(struct mbuf *a, size_t new_size) {
1437
  if (new_size > a->size || (new_size < a->size && new_size >= a->len)) {
1438
    char *buf = (char *) MBUF_REALLOC(a->buf, new_size);
1439
    /*
1440
     * In case realloc fails, there's not much we can do, except keep things as
1441
     * they are. Note that NULL is a valid return value from realloc when
1442
     * size == 0, but that is covered too.
1443
     */
1444
    if (buf == NULL && new_size != 0) return;
1445
    a->buf = buf;
1446
    a->size = new_size;
1447
  }
1448
}
1449

    
1450
void mbuf_trim(struct mbuf *mbuf) WEAK;
1451
void mbuf_trim(struct mbuf *mbuf) {
1452
  mbuf_resize(mbuf, mbuf->len);
1453
}
1454

    
1455
size_t mbuf_insert(struct mbuf *a, size_t off, const void *buf, size_t) WEAK;
1456
size_t mbuf_insert(struct mbuf *a, size_t off, const void *buf, size_t len) {
1457
  char *p = NULL;
1458

    
1459
  assert(a != NULL);
1460
  assert(a->len <= a->size);
1461
  assert(off <= a->len);
1462

    
1463
  /* check overflow */
1464
  if (~(size_t) 0 - (size_t) a->buf < len) return 0;
1465

    
1466
  if (a->len + len <= a->size) {
1467
    memmove(a->buf + off + len, a->buf + off, a->len - off);
1468
    if (buf != NULL) {
1469
      memcpy(a->buf + off, buf, len);
1470
    }
1471
    a->len += len;
1472
  } else {
1473
    size_t new_size = (size_t)((a->len + len) * MBUF_SIZE_MULTIPLIER);
1474
    if ((p = (char *) MBUF_REALLOC(a->buf, new_size)) != NULL) {
1475
      a->buf = p;
1476
      memmove(a->buf + off + len, a->buf + off, a->len - off);
1477
      if (buf != NULL) memcpy(a->buf + off, buf, len);
1478
      a->len += len;
1479
      a->size = new_size;
1480
    } else {
1481
      len = 0;
1482
    }
1483
  }
1484

    
1485
  return len;
1486
}
1487

    
1488
size_t mbuf_append(struct mbuf *a, const void *buf, size_t len) WEAK;
1489
size_t mbuf_append(struct mbuf *a, const void *buf, size_t len) {
1490
  return mbuf_insert(a, a->len, buf, len);
1491
}
1492

    
1493
void mbuf_remove(struct mbuf *mb, size_t n) WEAK;
1494
void mbuf_remove(struct mbuf *mb, size_t n) {
1495
  if (n > 0 && n <= mb->len) {
1496
    memmove(mb->buf, mb->buf + n, mb->len - n);
1497
    mb->len -= n;
1498
  }
1499
}
1500

    
1501
#endif /* EXCLUDE_COMMON */
1502
#ifdef MG_MODULE_LINES
1503
#line 1 "common/mg_str.c"
1504
#endif
1505
/*
1506
 * Copyright (c) 2014-2016 Cesanta Software Limited
1507
 * All rights reserved
1508
 */
1509

    
1510
/* Amalgamated: #include "common/mg_mem.h" */
1511
/* Amalgamated: #include "common/mg_str.h" */
1512

    
1513
#include <stdlib.h>
1514
#include <string.h>
1515

    
1516
int mg_ncasecmp(const char *s1, const char *s2, size_t len) WEAK;
1517

    
1518
struct mg_str mg_mk_str(const char *s) WEAK;
1519
struct mg_str mg_mk_str(const char *s) {
1520
  struct mg_str ret = {s, 0};
1521
  if (s != NULL) ret.len = strlen(s);
1522
  return ret;
1523
}
1524

    
1525
struct mg_str mg_mk_str_n(const char *s, size_t len) WEAK;
1526
struct mg_str mg_mk_str_n(const char *s, size_t len) {
1527
  struct mg_str ret = {s, len};
1528
  return ret;
1529
}
1530

    
1531
int mg_vcmp(const struct mg_str *str1, const char *str2) WEAK;
1532
int mg_vcmp(const struct mg_str *str1, const char *str2) {
1533
  size_t n2 = strlen(str2), n1 = str1->len;
1534
  int r = strncmp(str1->p, str2, (n1 < n2) ? n1 : n2);
1535
  if (r == 0) {
1536
    return n1 - n2;
1537
  }
1538
  return r;
1539
}
1540

    
1541
int mg_vcasecmp(const struct mg_str *str1, const char *str2) WEAK;
1542
int mg_vcasecmp(const struct mg_str *str1, const char *str2) {
1543
  size_t n2 = strlen(str2), n1 = str1->len;
1544
  int r = mg_ncasecmp(str1->p, str2, (n1 < n2) ? n1 : n2);
1545
  if (r == 0) {
1546
    return n1 - n2;
1547
  }
1548
  return r;
1549
}
1550

    
1551
static struct mg_str mg_strdup_common(const struct mg_str s,
1552
                                      int nul_terminate) {
1553
  struct mg_str r = {NULL, 0};
1554
  if (s.len > 0 && s.p != NULL) {
1555
    char *sc = (char *) MG_MALLOC(s.len + (nul_terminate ? 1 : 0));
1556
    if (sc != NULL) {
1557
      memcpy(sc, s.p, s.len);
1558
      if (nul_terminate) sc[s.len] = '\0';
1559
      r.p = sc;
1560
      r.len = s.len;
1561
    }
1562
  }
1563
  return r;
1564
}
1565

    
1566
struct mg_str mg_strdup(const struct mg_str s) WEAK;
1567
struct mg_str mg_strdup(const struct mg_str s) {
1568
  return mg_strdup_common(s, 0 /* NUL-terminate */);
1569
}
1570

    
1571
struct mg_str mg_strdup_nul(const struct mg_str s) WEAK;
1572
struct mg_str mg_strdup_nul(const struct mg_str s) {
1573
  return mg_strdup_common(s, 1 /* NUL-terminate */);
1574
}
1575

    
1576
const char *mg_strchr(const struct mg_str s, int c) WEAK;
1577
const char *mg_strchr(const struct mg_str s, int c) {
1578
  size_t i;
1579
  for (i = 0; i < s.len; i++) {
1580
    if (s.p[i] == c) return &s.p[i];
1581
  }
1582
  return NULL;
1583
}
1584

    
1585
int mg_strcmp(const struct mg_str str1, const struct mg_str str2) WEAK;
1586
int mg_strcmp(const struct mg_str str1, const struct mg_str str2) {
1587
  size_t i = 0;
1588
  while (i < str1.len && i < str2.len) {
1589
    if (str1.p[i] < str2.p[i]) return -1;
1590
    if (str1.p[i] > str2.p[i]) return 1;
1591
    i++;
1592
  }
1593
  if (i < str1.len) return 1;
1594
  if (i < str2.len) return -1;
1595
  return 0;
1596
}
1597

    
1598
int mg_strncmp(const struct mg_str, const struct mg_str, size_t n) WEAK;
1599
int mg_strncmp(const struct mg_str str1, const struct mg_str str2, size_t n) {
1600
  struct mg_str s1 = str1;
1601
  struct mg_str s2 = str2;
1602

    
1603
  if (s1.len > n) {
1604
    s1.len = n;
1605
  }
1606
  if (s2.len > n) {
1607
    s2.len = n;
1608
  }
1609
  return mg_strcmp(s1, s2);
1610
}
1611

    
1612
const char *mg_strstr(const struct mg_str haystack,
1613
                      const struct mg_str needle) WEAK;
1614
const char *mg_strstr(const struct mg_str haystack,
1615
                      const struct mg_str needle) {
1616
  size_t i;
1617
  if (needle.len > haystack.len) return NULL;
1618
  for (i = 0; i <= haystack.len - needle.len; i++) {
1619
    if (memcmp(haystack.p + i, needle.p, needle.len) == 0) {
1620
      return haystack.p + i;
1621
    }
1622
  }
1623
  return NULL;
1624
}
1625
#ifdef MG_MODULE_LINES
1626
#line 1 "common/str_util.c"
1627
#endif
1628
/*
1629
 * Copyright (c) 2015 Cesanta Software Limited
1630
 * All rights reserved
1631
 */
1632

    
1633
#ifndef EXCLUDE_COMMON
1634

    
1635
/* Amalgamated: #include "common/str_util.h" */
1636
/* Amalgamated: #include "common/mg_mem.h" */
1637
/* Amalgamated: #include "common/platform.h" */
1638

    
1639
#ifndef C_DISABLE_BUILTIN_SNPRINTF
1640
#define C_DISABLE_BUILTIN_SNPRINTF 0
1641
#endif
1642

    
1643
/* Amalgamated: #include "common/mg_mem.h" */
1644

    
1645
size_t c_strnlen(const char *s, size_t maxlen) WEAK;
1646
size_t c_strnlen(const char *s, size_t maxlen) {
1647
  size_t l = 0;
1648
  for (; l < maxlen && s[l] != '\0'; l++) {
1649
  }
1650
  return l;
1651
}
1652

    
1653
#define C_SNPRINTF_APPEND_CHAR(ch)       \
1654
  do {                                   \
1655
    if (i < (int) buf_size) buf[i] = ch; \
1656
    i++;                                 \
1657
  } while (0)
1658

    
1659
#define C_SNPRINTF_FLAG_ZERO 1
1660

    
1661
#if C_DISABLE_BUILTIN_SNPRINTF
1662
int c_vsnprintf(char *buf, size_t buf_size, const char *fmt, va_list ap) WEAK;
1663
int c_vsnprintf(char *buf, size_t buf_size, const char *fmt, va_list ap) {
1664
  return vsnprintf(buf, buf_size, fmt, ap);
1665
}
1666
#else
1667
static int c_itoa(char *buf, size_t buf_size, int64_t num, int base, int flags,
1668
                  int field_width) {
1669
  char tmp[40];
1670
  int i = 0, k = 0, neg = 0;
1671

    
1672
  if (num < 0) {
1673
    neg++;
1674
    num = -num;
1675
  }
1676

    
1677
  /* Print into temporary buffer - in reverse order */
1678
  do {
1679
    int rem = num % base;
1680
    if (rem < 10) {
1681
      tmp[k++] = '0' + rem;
1682
    } else {
1683
      tmp[k++] = 'a' + (rem - 10);
1684
    }
1685
    num /= base;
1686
  } while (num > 0);
1687

    
1688
  /* Zero padding */
1689
  if (flags && C_SNPRINTF_FLAG_ZERO) {
1690
    while (k < field_width && k < (int) sizeof(tmp) - 1) {
1691
      tmp[k++] = '0';
1692
    }
1693
  }
1694

    
1695
  /* And sign */
1696
  if (neg) {
1697
    tmp[k++] = '-';
1698
  }
1699

    
1700
  /* Now output */
1701
  while (--k >= 0) {
1702
    C_SNPRINTF_APPEND_CHAR(tmp[k]);
1703
  }
1704

    
1705
  return i;
1706
}
1707

    
1708
int c_vsnprintf(char *buf, size_t buf_size, const char *fmt, va_list ap) WEAK;
1709
int c_vsnprintf(char *buf, size_t buf_size, const char *fmt, va_list ap) {
1710
  int ch, i = 0, len_mod, flags, precision, field_width;
1711

    
1712
  while ((ch = *fmt++) != '\0') {
1713
    if (ch != '%') {
1714
      C_SNPRINTF_APPEND_CHAR(ch);
1715
    } else {
1716
      /*
1717
       * Conversion specification:
1718
       *   zero or more flags (one of: # 0 - <space> + ')
1719
       *   an optional minimum  field  width (digits)
1720
       *   an  optional precision (. followed by digits, or *)
1721
       *   an optional length modifier (one of: hh h l ll L q j z t)
1722
       *   conversion specifier (one of: d i o u x X e E f F g G a A c s p n)
1723
       */
1724
      flags = field_width = precision = len_mod = 0;
1725

    
1726
      /* Flags. only zero-pad flag is supported. */
1727
      if (*fmt == '0') {
1728
        flags |= C_SNPRINTF_FLAG_ZERO;
1729
      }
1730

    
1731
      /* Field width */
1732
      while (*fmt >= '0' && *fmt <= '9') {
1733
        field_width *= 10;
1734
        field_width += *fmt++ - '0';
1735
      }
1736
      /* Dynamic field width */
1737
      if (*fmt == '*') {
1738
        field_width = va_arg(ap, int);
1739
        fmt++;
1740
      }
1741

    
1742
      /* Precision */
1743
      if (*fmt == '.') {
1744
        fmt++;
1745
        if (*fmt == '*') {
1746
          precision = va_arg(ap, int);
1747
          fmt++;
1748
        } else {
1749
          while (*fmt >= '0' && *fmt <= '9') {
1750
            precision *= 10;
1751
            precision += *fmt++ - '0';
1752
          }
1753
        }
1754
      }
1755

    
1756
      /* Length modifier */
1757
      switch (*fmt) {
1758
        case 'h':
1759
        case 'l':
1760
        case 'L':
1761
        case 'I':
1762
        case 'q':
1763
        case 'j':
1764
        case 'z':
1765
        case 't':
1766
          len_mod = *fmt++;
1767
          if (*fmt == 'h') {
1768
            len_mod = 'H';
1769
            fmt++;
1770
          }
1771
          if (*fmt == 'l') {
1772
            len_mod = 'q';
1773
            fmt++;
1774
          }
1775
          break;
1776
      }
1777

    
1778
      ch = *fmt++;
1779
      if (ch == 's') {
1780
        const char *s = va_arg(ap, const char *); /* Always fetch parameter */
1781
        int j;
1782
        int pad = field_width - (precision >= 0 ? c_strnlen(s, precision) : 0);
1783
        for (j = 0; j < pad; j++) {
1784
          C_SNPRINTF_APPEND_CHAR(' ');
1785
        }
1786

    
1787
        /* `s` may be NULL in case of %.*s */
1788
        if (s != NULL) {
1789
          /* Ignore negative and 0 precisions */
1790
          for (j = 0; (precision <= 0 || j < precision) && s[j] != '\0'; j++) {
1791
            C_SNPRINTF_APPEND_CHAR(s[j]);
1792
          }
1793
        }
1794
      } else if (ch == 'c') {
1795
        ch = va_arg(ap, int); /* Always fetch parameter */
1796
        C_SNPRINTF_APPEND_CHAR(ch);
1797
      } else if (ch == 'd' && len_mod == 0) {
1798
        i += c_itoa(buf + i, buf_size - i, va_arg(ap, int), 10, flags,
1799
                    field_width);
1800
      } else if (ch == 'd' && len_mod == 'l') {
1801
        i += c_itoa(buf + i, buf_size - i, va_arg(ap, long), 10, flags,
1802
                    field_width);
1803
#ifdef SSIZE_MAX
1804
      } else if (ch == 'd' && len_mod == 'z') {
1805
        i += c_itoa(buf + i, buf_size - i, va_arg(ap, ssize_t), 10, flags,
1806
                    field_width);
1807
#endif
1808
      } else if (ch == 'd' && len_mod == 'q') {
1809
        i += c_itoa(buf + i, buf_size - i, va_arg(ap, int64_t), 10, flags,
1810
                    field_width);
1811
      } else if ((ch == 'x' || ch == 'u') && len_mod == 0) {
1812
        i += c_itoa(buf + i, buf_size - i, va_arg(ap, unsigned),
1813
                    ch == 'x' ? 16 : 10, flags, field_width);
1814
      } else if ((ch == 'x' || ch == 'u') && len_mod == 'l') {
1815
        i += c_itoa(buf + i, buf_size - i, va_arg(ap, unsigned long),
1816
                    ch == 'x' ? 16 : 10, flags, field_width);
1817
      } else if ((ch == 'x' || ch == 'u') && len_mod == 'z') {
1818
        i += c_itoa(buf + i, buf_size - i, va_arg(ap, size_t),
1819
                    ch == 'x' ? 16 : 10, flags, field_width);
1820
      } else if (ch == 'p') {
1821
        unsigned long num = (unsigned long) (uintptr_t) va_arg(ap, void *);
1822
        C_SNPRINTF_APPEND_CHAR('0');
1823
        C_SNPRINTF_APPEND_CHAR('x');
1824
        i += c_itoa(buf + i, buf_size - i, num, 16, flags, 0);
1825
      } else {
1826
#ifndef NO_LIBC
1827
        /*
1828
         * TODO(lsm): abort is not nice in a library, remove it
1829
         * Also, ESP8266 SDK doesn't have it
1830
         */
1831
        abort();
1832
#endif
1833
      }
1834
    }
1835
  }
1836

    
1837
  /* Zero-terminate the result */
1838
  if (buf_size > 0) {
1839
    buf[i < (int) buf_size ? i : (int) buf_size - 1] = '\0';
1840
  }
1841

    
1842
  return i;
1843
}
1844
#endif
1845

    
1846
int c_snprintf(char *buf, size_t buf_size, const char *fmt, ...) WEAK;
1847
int c_snprintf(char *buf, size_t buf_size, const char *fmt, ...) {
1848
  int result;
1849
  va_list ap;
1850
  va_start(ap, fmt);
1851
  result = c_vsnprintf(buf, buf_size, fmt, ap);
1852
  va_end(ap);
1853
  return result;
1854
}
1855

    
1856
#ifdef _WIN32
1857
int to_wchar(const char *path, wchar_t *wbuf, size_t wbuf_len) {
1858
  int ret;
1859
  char buf[MAX_PATH * 2], buf2[MAX_PATH * 2], *p;
1860

    
1861
  strncpy(buf, path, sizeof(buf));
1862
  buf[sizeof(buf) - 1] = '\0';
1863

    
1864
  /* Trim trailing slashes. Leave backslash for paths like "X:\" */
1865
  p = buf + strlen(buf) - 1;
1866
  while (p > buf && p[-1] != ':' && (p[0] == '\\' || p[0] == '/')) *p-- = '\0';
1867

    
1868
  memset(wbuf, 0, wbuf_len * sizeof(wchar_t));
1869
  ret = MultiByteToWideChar(CP_UTF8, 0, buf, -1, wbuf, (int) wbuf_len);
1870

    
1871
  /*
1872
   * Convert back to Unicode. If doubly-converted string does not match the
1873
   * original, something is fishy, reject.
1874
   */
1875
  WideCharToMultiByte(CP_UTF8, 0, wbuf, (int) wbuf_len, buf2, sizeof(buf2),
1876
                      NULL, NULL);
1877
  if (strcmp(buf, buf2) != 0) {
1878
    wbuf[0] = L'\0';
1879
    ret = 0;
1880
  }
1881

    
1882
  return ret;
1883
}
1884
#endif /* _WIN32 */
1885

    
1886
/* The simplest O(mn) algorithm. Better implementation are GPLed */
1887
const char *c_strnstr(const char *s, const char *find, size_t slen) WEAK;
1888
const char *c_strnstr(const char *s, const char *find, size_t slen) {
1889
  size_t find_length = strlen(find);
1890
  size_t i;
1891

    
1892
  for (i = 0; i < slen; i++) {
1893
    if (i + find_length > slen) {
1894
      return NULL;
1895
    }
1896

    
1897
    if (strncmp(&s[i], find, find_length) == 0) {
1898
      return &s[i];
1899
    }
1900
  }
1901

    
1902
  return NULL;
1903
}
1904

    
1905
#if CS_ENABLE_STRDUP
1906
char *strdup(const char *src) WEAK;
1907
char *strdup(const char *src) {
1908
  size_t len = strlen(src) + 1;
1909
  char *ret = MG_MALLOC(len);
1910
  if (ret != NULL) {
1911
    strcpy(ret, src);
1912
  }
1913
  return ret;
1914
}
1915
#endif
1916

    
1917
void cs_to_hex(char *to, const unsigned char *p, size_t len) WEAK;
1918
void cs_to_hex(char *to, const unsigned char *p, size_t len) {
1919
  static const char *hex = "0123456789abcdef";
1920

    
1921
  for (; len--; p++) {
1922
    *to++ = hex[p[0] >> 4];
1923
    *to++ = hex[p[0] & 0x0f];
1924
  }
1925
  *to = '\0';
1926
}
1927

    
1928
static int fourbit(int ch) {
1929
  if (ch >= '0' && ch <= '9') {
1930
    return ch - '0';
1931
  } else if (ch >= 'a' && ch <= 'f') {
1932
    return ch - 'a' + 10;
1933
  } else if (ch >= 'A' && ch <= 'F') {
1934
    return ch - 'A' + 10;
1935
  }
1936
  return 0;
1937
}
1938

    
1939
void cs_from_hex(char *to, const char *p, size_t len) WEAK;
1940
void cs_from_hex(char *to, const char *p, size_t len) {
1941
  size_t i;
1942

    
1943
  for (i = 0; i < len; i += 2) {
1944
    *to++ = (fourbit(p[i]) << 4) + fourbit(p[i + 1]);
1945
  }
1946
  *to = '\0';
1947
}
1948

    
1949
#if CS_ENABLE_TO64
1950
int64_t cs_to64(const char *s) WEAK;
1951
int64_t cs_to64(const char *s) {
1952
  int64_t result = 0;
1953
  int64_t neg = 1;
1954
  while (*s && isspace((unsigned char) *s)) s++;
1955
  if (*s == '-') {
1956
    neg = -1;
1957
    s++;
1958
  }
1959
  while (isdigit((unsigned char) *s)) {
1960
    result *= 10;
1961
    result += (*s - '0');
1962
    s++;
1963
  }
1964
  return result * neg;
1965
}
1966
#endif
1967

    
1968
static int str_util_lowercase(const char *s) {
1969
  return tolower(*(const unsigned char *) s);
1970
}
1971

    
1972
int mg_ncasecmp(const char *s1, const char *s2, size_t len) WEAK;
1973
int mg_ncasecmp(const char *s1, const char *s2, size_t len) {
1974
  int diff = 0;
1975

    
1976
  if (len > 0) do {
1977
      diff = str_util_lowercase(s1++) - str_util_lowercase(s2++);
1978
    } while (diff == 0 && s1[-1] != '\0' && --len > 0);
1979

    
1980
  return diff;
1981
}
1982

    
1983
int mg_casecmp(const char *s1, const char *s2) WEAK;
1984
int mg_casecmp(const char *s1, const char *s2) {
1985
  return mg_ncasecmp(s1, s2, (size_t) ~0);
1986
}
1987

    
1988
int mg_asprintf(char **buf, size_t size, const char *fmt, ...) WEAK;
1989
int mg_asprintf(char **buf, size_t size, const char *fmt, ...) {
1990
  int ret;
1991
  va_list ap;
1992
  va_start(ap, fmt);
1993
  ret = mg_avprintf(buf, size, fmt, ap);
1994
  va_end(ap);
1995
  return ret;
1996
}
1997

    
1998
int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap) WEAK;
1999
int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap) {
2000
  va_list ap_copy;
2001
  int len;
2002

    
2003
  va_copy(ap_copy, ap);
2004
  len = vsnprintf(*buf, size, fmt, ap_copy);
2005
  va_end(ap_copy);
2006

    
2007
  if (len < 0) {
2008
    /* eCos and Windows are not standard-compliant and return -1 when
2009
     * the buffer is too small. Keep allocating larger buffers until we
2010
     * succeed or out of memory. */
2011
    *buf = NULL; /* LCOV_EXCL_START */
2012
    while (len < 0) {
2013
      MG_FREE(*buf);
2014
      if (size == 0) {
2015
        size = 5;
2016
      }
2017
      size *= 2;
2018
      if ((*buf = (char *) MG_MALLOC(size)) == NULL) {
2019
        len = -1;
2020
        break;
2021
      }
2022
      va_copy(ap_copy, ap);
2023
      len = vsnprintf(*buf, size - 1, fmt, ap_copy);
2024
      va_end(ap_copy);
2025
    }
2026

    
2027
    /*
2028
     * Microsoft version of vsnprintf() is not always null-terminated, so put
2029
     * the terminator manually
2030
     */
2031
    (*buf)[len] = 0;
2032
    /* LCOV_EXCL_STOP */
2033
  } else if (len >= (int) size) {
2034
    /* Standard-compliant code path. Allocate a buffer that is large enough. */
2035
    if ((*buf = (char *) MG_MALLOC(len + 1)) == NULL) {
2036
      len = -1; /* LCOV_EXCL_LINE */
2037
    } else {    /* LCOV_EXCL_LINE */
2038
      va_copy(ap_copy, ap);
2039
      len = vsnprintf(*buf, len + 1, fmt, ap_copy);
2040
      va_end(ap_copy);
2041
    }
2042
  }
2043

    
2044
  return len;
2045
}
2046

    
2047
const char *mg_next_comma_list_entry(const char *, struct mg_str *,
2048
                                     struct mg_str *) WEAK;
2049
const char *mg_next_comma_list_entry(const char *list, struct mg_str *val,
2050
                                     struct mg_str *eq_val) {
2051
  struct mg_str ret = mg_next_comma_list_entry_n(mg_mk_str(list), val, eq_val);
2052
  return ret.p;
2053
}
2054

    
2055
struct mg_str mg_next_comma_list_entry_n(struct mg_str list, struct mg_str *val,
2056
                                         struct mg_str *eq_val) WEAK;
2057
struct mg_str mg_next_comma_list_entry_n(struct mg_str list, struct mg_str *val,
2058
                                         struct mg_str *eq_val) {
2059
  if (list.len == 0) {
2060
    /* End of the list */
2061
    list = mg_mk_str(NULL);
2062
  } else {
2063
    const char *chr = NULL;
2064
    *val = list;
2065

    
2066
    if ((chr = mg_strchr(*val, ',')) != NULL) {
2067
      /* Comma found. Store length and shift the list ptr */
2068
      val->len = chr - val->p;
2069
      chr++;
2070
      list.len -= (chr - list.p);
2071
      list.p = chr;
2072
    } else {
2073
      /* This value is the last one */
2074
      list = mg_mk_str_n(list.p + list.len, 0);
2075
    }
2076

    
2077
    if (eq_val != NULL) {
2078
      /* Value has form "x=y", adjust pointers and lengths */
2079
      /* so that val points to "x", and eq_val points to "y". */
2080
      eq_val->len = 0;
2081
      eq_val->p = (const char *) memchr(val->p, '=', val->len);
2082
      if (eq_val->p != NULL) {
2083
        eq_val->p++; /* Skip over '=' character */
2084
        eq_val->len = val->p + val->len - eq_val->p;
2085
        val->len = (eq_val->p - val->p) - 1;
2086
      }
2087
    }
2088
  }
2089

    
2090
  return list;
2091
}
2092

    
2093
size_t mg_match_prefix_n(const struct mg_str, const struct mg_str) WEAK;
2094
size_t mg_match_prefix_n(const struct mg_str pattern, const struct mg_str str) {
2095
  const char *or_str;
2096
  size_t res = 0, len = 0, i = 0, j = 0;
2097

    
2098
  if ((or_str = (const char *) memchr(pattern.p, '|', pattern.len)) != NULL ||
2099
      (or_str = (const char *) memchr(pattern.p, ',', pattern.len)) != NULL) {
2100
    struct mg_str pstr = {pattern.p, (size_t)(or_str - pattern.p)};
2101
    res = mg_match_prefix_n(pstr, str);
2102
    if (res > 0) return res;
2103
    pstr.p = or_str + 1;
2104
    pstr.len = (pattern.p + pattern.len) - (or_str + 1);
2105
    return mg_match_prefix_n(pstr, str);
2106
  }
2107

    
2108
  for (; i < pattern.len && j < str.len; i++, j++) {
2109
    if (pattern.p[i] == '?') {
2110
      continue;
2111
    } else if (pattern.p[i] == '*') {
2112
      i++;
2113
      if (i < pattern.len && pattern.p[i] == '*') {
2114
        i++;
2115
        len = str.len - j;
2116
      } else {
2117
        len = 0;
2118
        while (j + len < str.len && str.p[j + len] != '/') len++;
2119
      }
2120
      if (i == pattern.len || (pattern.p[i] == '$' && i == pattern.len - 1))
2121
        return j + len;
2122
      do {
2123
        const struct mg_str pstr = {pattern.p + i, pattern.len - i};
2124
        const struct mg_str sstr = {str.p + j + len, str.len - j - len};
2125
        res = mg_match_prefix_n(pstr, sstr);
2126
      } while (res == 0 && len != 0 && len-- > 0);
2127
      return res == 0 ? 0 : j + res + len;
2128
    } else if (str_util_lowercase(&pattern.p[i]) !=
2129
               str_util_lowercase(&str.p[j])) {
2130
      break;
2131
    }
2132
  }
2133
  if (i < pattern.len && pattern.p[i] == '$') {
2134
    return j == str.len ? str.len : 0;
2135
  }
2136
  return i == pattern.len ? j : 0;
2137
}
2138

    
2139
size_t mg_match_prefix(const char *, int, const char *) WEAK;
2140
size_t mg_match_prefix(const char *pattern, int pattern_len, const char *str) {
2141
  const struct mg_str pstr = {pattern, (size_t) pattern_len};
2142
  struct mg_str s = {str, 0};
2143
  if (str != NULL) s.len = strlen(str);
2144
  return mg_match_prefix_n(pstr, s);
2145
}
2146

    
2147
#endif /* EXCLUDE_COMMON */
2148
#ifdef MG_MODULE_LINES
2149
#line 1 "mongoose/src/mg_net.c"
2150
#endif
2151
/*
2152
 * Copyright (c) 2014 Cesanta Software Limited
2153
 * All rights reserved
2154
 *
2155
 * This software is dual-licensed: you can redistribute it and/or modify
2156
 * it under the terms of the GNU General Public License version 2 as
2157
 * published by the Free Software Foundation. For the terms of this
2158
 * license, see <http://www.gnu.org/licenses/>.
2159
 *
2160
 * You are free to use this software under the terms of the GNU General
2161
 * Public License, but WITHOUT ANY WARRANTY; without even the implied
2162
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
2163
 * See the GNU General Public License for more details.
2164
 *
2165
 * Alternatively, you can license this software under a commercial
2166
 * license, as set out in <https://www.cesanta.com/license>.
2167
 */
2168

    
2169
/* Amalgamated: #include "common/cs_time.h" */
2170
/* Amalgamated: #include "mg_dns.h" */
2171
/* Amalgamated: #include "mg_internal.h" */
2172
/* Amalgamated: #include "mg_resolv.h" */
2173
/* Amalgamated: #include "mg_util.h" */
2174

    
2175
#define MG_MAX_HOST_LEN 200
2176

    
2177
#define MG_COPY_COMMON_CONNECTION_OPTIONS(dst, src) \
2178
  memcpy(dst, src, sizeof(*dst));
2179

    
2180
/* Which flags can be pre-set by the user at connection creation time. */
2181
#define _MG_ALLOWED_CONNECT_FLAGS_MASK                                   \
2182
  (MG_F_USER_1 | MG_F_USER_2 | MG_F_USER_3 | MG_F_USER_4 | MG_F_USER_5 | \
2183
   MG_F_USER_6 | MG_F_WEBSOCKET_NO_DEFRAG | MG_F_ENABLE_BROADCAST)
2184
/* Which flags should be modifiable by user's callbacks. */
2185
#define _MG_CALLBACK_MODIFIABLE_FLAGS_MASK                               \
2186
  (MG_F_USER_1 | MG_F_USER_2 | MG_F_USER_3 | MG_F_USER_4 | MG_F_USER_5 | \
2187
   MG_F_USER_6 | MG_F_WEBSOCKET_NO_DEFRAG | MG_F_SEND_AND_CLOSE |        \
2188
   MG_F_CLOSE_IMMEDIATELY | MG_F_IS_WEBSOCKET | MG_F_DELETE_CHUNK)
2189

    
2190
#ifndef intptr_t
2191
#define intptr_t long
2192
#endif
2193

    
2194
MG_INTERNAL void mg_add_conn(struct mg_mgr *mgr, struct mg_connection *c) {
2195
  DBG(("%p %p", mgr, c));
2196
  c->mgr = mgr;
2197
  c->next = mgr->active_connections;
2198
  mgr->active_connections = c;
2199
  c->prev = NULL;
2200
  if (c->next != NULL) c->next->prev = c;
2201
  if (c->sock != INVALID_SOCKET) {
2202
    c->iface->vtable->add_conn(c);
2203
  }
2204
}
2205

    
2206
MG_INTERNAL void mg_remove_conn(struct mg_connection *conn) {
2207
  if (conn->prev == NULL) conn->mgr->active_connections = conn->next;
2208
  if (conn->prev) conn->prev->next = conn->next;
2209
  if (conn->next) conn->next->prev = conn->prev;
2210
  conn->prev = conn->next = NULL;
2211
  conn->iface->vtable->remove_conn(conn);
2212
}
2213

    
2214
MG_INTERNAL void mg_call(struct mg_connection *nc,
2215
                         mg_event_handler_t ev_handler, void *user_data, int ev,
2216
                         void *ev_data) {
2217
  static int nesting_level = 0;
2218
  nesting_level++;
2219
  if (ev_handler == NULL) {
2220
    /*
2221
     * If protocol handler is specified, call it. Otherwise, call user-specified
2222
     * event handler.
2223
     */
2224
    ev_handler = nc->proto_handler ? nc->proto_handler : nc->handler;
2225
  }
2226
  if (ev != MG_EV_POLL) {
2227
    DBG(("%p %s ev=%d ev_data=%p flags=%lu rmbl=%d smbl=%d", nc,
2228
         ev_handler == nc->handler ? "user" : "proto", ev, ev_data, nc->flags,
2229
         (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len));
2230
  }
2231

    
2232
#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP
2233
  if (nc->mgr->hexdump_file != NULL && ev != MG_EV_POLL && ev != MG_EV_RECV &&
2234
      ev != MG_EV_SEND /* handled separately */) {
2235
    mg_hexdump_connection(nc, nc->mgr->hexdump_file, NULL, 0, ev);
2236
  }
2237
#endif
2238
  if (ev_handler != NULL) {
2239
    unsigned long flags_before = nc->flags;
2240
    size_t recv_mbuf_before = nc->recv_mbuf.len, recved;
2241
    ev_handler(nc, ev, ev_data MG_UD_ARG(user_data));
2242
    recved = (recv_mbuf_before - nc->recv_mbuf.len);
2243
    /* Prevent user handler from fiddling with system flags. */
2244
    if (ev_handler == nc->handler && nc->flags != flags_before) {
2245
      nc->flags = (flags_before & ~_MG_CALLBACK_MODIFIABLE_FLAGS_MASK) |
2246
                  (nc->flags & _MG_CALLBACK_MODIFIABLE_FLAGS_MASK);
2247
    }
2248
    /* It's important to not double-count recved bytes, and since mg_call can be
2249
     * called recursively (e.g. proto_handler invokes user handler), we keep
2250
     * track of recursion and only report received bytes at the top level. */
2251
    if (nesting_level == 1 && recved > 0 && !(nc->flags & MG_F_UDP)) {
2252
      nc->iface->vtable->recved(nc, recved);
2253
    }
2254
  }
2255
  if (ev != MG_EV_POLL) {
2256
    DBG(("%p after %s flags=%lu rmbl=%d smbl=%d", nc,
2257
         ev_handler == nc->handler ? "user" : "proto", nc->flags,
2258
         (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len));
2259
  }
2260
  nesting_level--;
2261
#if !MG_ENABLE_CALLBACK_USERDATA
2262
  (void) user_data;
2263
#endif
2264
}
2265

    
2266
void mg_if_timer(struct mg_connection *c, double now) {
2267
  if (c->ev_timer_time > 0 && now >= c->ev_timer_time) {
2268
    double old_value = c->ev_timer_time;
2269
    c->ev_timer_time = 0;
2270
    mg_call(c, NULL, c->user_data, MG_EV_TIMER, &old_value);
2271
  }
2272
}
2273

    
2274
void mg_if_poll(struct mg_connection *nc, time_t now) {
2275
  if (!(nc->flags & MG_F_SSL) || (nc->flags & MG_F_SSL_HANDSHAKE_DONE)) {
2276
    mg_call(nc, NULL, nc->user_data, MG_EV_POLL, &now);
2277
  }
2278
}
2279

    
2280
void mg_destroy_conn(struct mg_connection *conn, int destroy_if) {
2281
  if (destroy_if) conn->iface->vtable->destroy_conn(conn);
2282
  if (conn->proto_data != NULL && conn->proto_data_destructor != NULL) {
2283
    conn->proto_data_destructor(conn->proto_data);
2284
  }
2285
#if MG_ENABLE_SSL
2286
  mg_ssl_if_conn_free(conn);
2287
#endif
2288
  mbuf_free(&conn->recv_mbuf);
2289
  mbuf_free(&conn->send_mbuf);
2290

    
2291
  memset(conn, 0, sizeof(*conn));
2292
  MG_FREE(conn);
2293
}
2294

    
2295
void mg_close_conn(struct mg_connection *conn) {
2296
  DBG(("%p %lu %d", conn, conn->flags, conn->sock));
2297
#if MG_ENABLE_SSL
2298
  if (conn->flags & MG_F_SSL_HANDSHAKE_DONE) {
2299
    mg_ssl_if_conn_close_notify(conn);
2300
  }
2301
#endif
2302
  mg_remove_conn(conn);
2303
  conn->iface->vtable->destroy_conn(conn);
2304
  mg_call(conn, NULL, conn->user_data, MG_EV_CLOSE, NULL);
2305
  mg_destroy_conn(conn, 0 /* destroy_if */);
2306
}
2307

    
2308
void mg_mgr_init(struct mg_mgr *m, void *user_data) {
2309
  struct mg_mgr_init_opts opts;
2310
  memset(&opts, 0, sizeof(opts));
2311
  mg_mgr_init_opt(m, user_data, opts);
2312
}
2313

    
2314
void mg_mgr_init_opt(struct mg_mgr *m, void *user_data,
2315
                     struct mg_mgr_init_opts opts) {
2316
  memset(m, 0, sizeof(*m));
2317
#if MG_ENABLE_BROADCAST
2318
  m->ctl[0] = m->ctl[1] = INVALID_SOCKET;
2319
#endif
2320
  m->user_data = user_data;
2321

    
2322
#ifdef _WIN32
2323
  {
2324
    WSADATA data;
2325
    WSAStartup(MAKEWORD(2, 2), &data);
2326
  }
2327
#elif defined(__unix__)
2328
  /* Ignore SIGPIPE signal, so if client cancels the request, it
2329
   * won't kill the whole process. */
2330
  signal(SIGPIPE, SIG_IGN);
2331
#endif
2332

    
2333
#if MG_ENABLE_SSL
2334
  {
2335
    static int init_done;
2336
    if (!init_done) {
2337
      mg_ssl_if_init();
2338
      init_done++;
2339
    }
2340
  }
2341
#endif
2342
  {
2343
    int i;
2344
    if (opts.num_ifaces == 0) {
2345
      opts.num_ifaces = mg_num_ifaces;
2346
      opts.ifaces = mg_ifaces;
2347
    }
2348
    if (opts.main_iface != NULL) {
2349
      opts.ifaces[MG_MAIN_IFACE] = opts.main_iface;
2350
    }
2351
    m->num_ifaces = opts.num_ifaces;
2352
    m->ifaces =
2353
        (struct mg_iface **) MG_MALLOC(sizeof(*m->ifaces) * opts.num_ifaces);
2354
    for (i = 0; i < mg_num_ifaces; i++) {
2355
      m->ifaces[i] = mg_if_create_iface(opts.ifaces[i], m);
2356
      m->ifaces[i]->vtable->init(m->ifaces[i]);
2357
    }
2358
  }
2359
  if (opts.nameserver != NULL) {
2360
    m->nameserver = strdup(opts.nameserver);
2361
  }
2362
  DBG(("=================================="));
2363
  DBG(("init mgr=%p", m));
2364
}
2365

    
2366
void mg_mgr_free(struct mg_mgr *m) {
2367
  struct mg_connection *conn, *tmp_conn;
2368

    
2369
  DBG(("%p", m));
2370
  if (m == NULL) return;
2371
  /* Do one last poll, see https://github.com/cesanta/mongoose/issues/286 */
2372
  mg_mgr_poll(m, 0);
2373

    
2374
#if MG_ENABLE_BROADCAST
2375
  if (m->ctl[0] != INVALID_SOCKET) closesocket(m->ctl[0]);
2376
  if (m->ctl[1] != INVALID_SOCKET) closesocket(m->ctl[1]);
2377
  m->ctl[0] = m->ctl[1] = INVALID_SOCKET;
2378
#endif
2379

    
2380
  for (conn = m->active_connections; conn != NULL; conn = tmp_conn) {
2381
    tmp_conn = conn->next;
2382
    mg_close_conn(conn);
2383
  }
2384

    
2385
  {
2386
    int i;
2387
    for (i = 0; i < m->num_ifaces; i++) {
2388
      m->ifaces[i]->vtable->free(m->ifaces[i]);
2389
      MG_FREE(m->ifaces[i]);
2390
    }
2391
    MG_FREE(m->ifaces);
2392
  }
2393

    
2394
  MG_FREE((char *) m->nameserver);
2395
}
2396

    
2397
time_t mg_mgr_poll(struct mg_mgr *m, int timeout_ms) {
2398
  int i;
2399
  time_t now = 0; /* oh GCC, seriously ? */
2400

    
2401
  if (m->num_ifaces == 0) {
2402
    LOG(LL_ERROR, ("cannot poll: no interfaces"));
2403
    return 0;
2404
  }
2405

    
2406
  for (i = 0; i < m->num_ifaces; i++) {
2407
    now = m->ifaces[i]->vtable->poll(m->ifaces[i], timeout_ms);
2408
  }
2409
  return now;
2410
}
2411

    
2412
int mg_vprintf(struct mg_connection *nc, const char *fmt, va_list ap) {
2413
  char mem[MG_VPRINTF_BUFFER_SIZE], *buf = mem;
2414
  int len;
2415

    
2416
  if ((len = mg_avprintf(&buf, sizeof(mem), fmt, ap)) > 0) {
2417
    mg_send(nc, buf, len);
2418
  }
2419
  if (buf != mem && buf != NULL) {
2420
    MG_FREE(buf); /* LCOV_EXCL_LINE */
2421
  }               /* LCOV_EXCL_LINE */
2422

    
2423
  return len;
2424
}
2425

    
2426
int mg_printf(struct mg_connection *conn, const char *fmt, ...) {
2427
  int len;
2428
  va_list ap;
2429
  va_start(ap, fmt);
2430
  len = mg_vprintf(conn, fmt, ap);
2431
  va_end(ap);
2432
  return len;
2433
}
2434

    
2435
#if MG_ENABLE_SYNC_RESOLVER
2436
/* TODO(lsm): use non-blocking resolver */
2437
static int mg_resolve2(const char *host, struct in_addr *ina) {
2438
#if MG_ENABLE_GETADDRINFO
2439
  int rv = 0;
2440
  struct addrinfo hints, *servinfo, *p;
2441
  struct sockaddr_in *h = NULL;
2442
  memset(&hints, 0, sizeof hints);
2443
  hints.ai_family = AF_INET;
2444
  hints.ai_socktype = SOCK_STREAM;
2445
  if ((rv = getaddrinfo(host, NULL, NULL, &servinfo)) != 0) {
2446
    DBG(("getaddrinfo(%s) failed: %s", host, strerror(mg_get_errno())));
2447
    return 0;
2448
  }
2449
  for (p = servinfo; p != NULL; p = p->ai_next) {
2450
    memcpy(&h, &p->ai_addr, sizeof(struct sockaddr_in *));
2451
    memcpy(ina, &h->sin_addr, sizeof(ina));
2452
  }
2453
  freeaddrinfo(servinfo);
2454
  return 1;
2455
#else
2456
  struct hostent *he;
2457
  if ((he = gethostbyname(host)) == NULL) {
2458
    DBG(("gethostbyname(%s) failed: %s", host, strerror(mg_get_errno())));
2459
  } else {
2460
    memcpy(ina, he->h_addr_list[0], sizeof(*ina));
2461
    return 1;
2462
  }
2463
  return 0;
2464
#endif /* MG_ENABLE_GETADDRINFO */
2465
}
2466

    
2467
int mg_resolve(const char *host, char *buf, size_t n) {
2468
  struct in_addr ad;
2469
  return mg_resolve2(host, &ad) ? snprintf(buf, n, "%s", inet_ntoa(ad)) : 0;
2470
}
2471
#endif /* MG_ENABLE_SYNC_RESOLVER */
2472

    
2473
MG_INTERNAL struct mg_connection *mg_create_connection_base(
2474
    struct mg_mgr *mgr, mg_event_handler_t callback,
2475
    struct mg_add_sock_opts opts) {
2476
  struct mg_connection *conn;
2477

    
2478
  if ((conn = (struct mg_connection *) MG_CALLOC(1, sizeof(*conn))) != NULL) {
2479
    conn->sock = INVALID_SOCKET;
2480
    conn->handler = callback;
2481
    conn->mgr = mgr;
2482
    conn->last_io_time = (time_t) mg_time();
2483
    conn->iface =
2484
        (opts.iface != NULL ? opts.iface : mgr->ifaces[MG_MAIN_IFACE]);
2485
    conn->flags = opts.flags & _MG_ALLOWED_CONNECT_FLAGS_MASK;
2486
    conn->user_data = opts.user_data;
2487
    /*
2488
     * SIZE_MAX is defined as a long long constant in
2489
     * system headers on some platforms and so it
2490
     * doesn't compile with pedantic ansi flags.
2491
     */
2492
    conn->recv_mbuf_limit = ~0;
2493
  } else {
2494
    MG_SET_PTRPTR(opts.error_string, "failed to create connection");
2495
  }
2496

    
2497
  return conn;
2498
}
2499

    
2500
MG_INTERNAL struct mg_connection *mg_create_connection(
2501
    struct mg_mgr *mgr, mg_event_handler_t callback,
2502
    struct mg_add_sock_opts opts) {
2503
  struct mg_connection *conn = mg_create_connection_base(mgr, callback, opts);
2504

    
2505
  if (conn != NULL && !conn->iface->vtable->create_conn(conn)) {
2506
    MG_FREE(conn);
2507
    conn = NULL;
2508
  }
2509
  if (conn == NULL) {
2510
    MG_SET_PTRPTR(opts.error_string, "failed to init connection");
2511
  }
2512

    
2513
  return conn;
2514
}
2515

    
2516
/*
2517
 * Address format: [PROTO://][HOST]:PORT
2518
 *
2519
 * HOST could be IPv4/IPv6 address or a host name.
2520
 * `host` is a destination buffer to hold parsed HOST part. Should be at least
2521
 * MG_MAX_HOST_LEN bytes long.
2522
 * `proto` is a returned socket type, either SOCK_STREAM or SOCK_DGRAM
2523
 *
2524
 * Return:
2525
 *   -1   on parse error
2526
 *    0   if HOST needs DNS lookup
2527
 *   >0   length of the address string
2528
 */
2529
MG_INTERNAL int mg_parse_address(const char *str, union socket_address *sa,
2530
                                 int *proto, char *host, size_t host_len) {
2531
  unsigned int a, b, c, d, port = 0;
2532
  int ch, len = 0;
2533
#if MG_ENABLE_IPV6
2534
  char buf[100];
2535
#endif
2536

    
2537
  /*
2538
   * MacOS needs that. If we do not zero it, subsequent bind() will fail.
2539
   * Also, all-zeroes in the socket address means binding to all addresses
2540
   * for both IPv4 and IPv6 (INADDR_ANY and IN6ADDR_ANY_INIT).
2541
   */
2542
  memset(sa, 0, sizeof(*sa));
2543
  sa->sin.sin_family = AF_INET;
2544

    
2545
  *proto = SOCK_STREAM;
2546

    
2547
  if (strncmp(str, "udp://", 6) == 0) {
2548
    str += 6;
2549
    *proto = SOCK_DGRAM;
2550
  } else if (strncmp(str, "tcp://", 6) == 0) {
2551
    str += 6;
2552
  }
2553

    
2554
  if (sscanf(str, "%u.%u.%u.%u:%u%n", &a, &b, &c, &d, &port, &len) == 5) {
2555
    /* Bind to a specific IPv4 address, e.g. 192.168.1.5:8080 */
2556
    sa->sin.sin_addr.s_addr =
2557
        htonl(((uint32_t) a << 24) | ((uint32_t) b << 16) | c << 8 | d);
2558
    sa->sin.sin_port = htons((uint16_t) port);
2559
#if MG_ENABLE_IPV6
2560
  } else if (sscanf(str, "[%99[^]]]:%u%n", buf, &port, &len) == 2 &&
2561
             inet_pton(AF_INET6, buf, &sa->sin6.sin6_addr)) {
2562
    /* IPv6 address, e.g. [3ffe:2a00:100:7031::1]:8080 */
2563
    sa->sin6.sin6_family = AF_INET6;
2564
    sa->sin.sin_port = htons((uint16_t) port);
2565
#endif
2566
#if MG_ENABLE_ASYNC_RESOLVER
2567
  } else if (strlen(str) < host_len &&
2568
             sscanf(str, "%[^ :]:%u%n", host, &port, &len) == 2) {
2569
    sa->sin.sin_port = htons((uint16_t) port);
2570
    if (mg_resolve_from_hosts_file(host, sa) != 0) {
2571
      /*
2572
       * if resolving from hosts file failed and the host
2573
       * we are trying to resolve is `localhost` - we should
2574
       * try to resolve it using `gethostbyname` and do not try
2575
       * to resolve it via DNS server if gethostbyname has failed too
2576
       */
2577
      if (mg_ncasecmp(host, "localhost", 9) != 0) {
2578
        return 0;
2579
      }
2580

    
2581
#if MG_ENABLE_SYNC_RESOLVER
2582
      if (!mg_resolve2(host, &sa->sin.sin_addr)) {
2583
        return -1;
2584
      }
2585
#else
2586
      return -1;
2587
#endif
2588
    }
2589
#endif
2590
  } else if (sscanf(str, ":%u%n", &port, &len) == 1 ||
2591
             sscanf(str, "%u%n", &port, &len) == 1) {
2592
    /* If only port is specified, bind to IPv4, INADDR_ANY */
2593
    sa->sin.sin_port = htons((uint16_t) port);
2594
  } else {
2595
    return -1;
2596
  }
2597

    
2598
  /* Required for MG_ENABLE_ASYNC_RESOLVER=0 */
2599
  (void) host;
2600
  (void) host_len;
2601

    
2602
  ch = str[len]; /* Character that follows the address */
2603
  return port < 0xffffUL && (ch == '\0' || ch == ',' || isspace(ch)) ? len : -1;
2604
}
2605

    
2606
struct mg_connection *mg_if_accept_new_conn(struct mg_connection *lc) {
2607
  struct mg_add_sock_opts opts;
2608
  struct mg_connection *nc;
2609
  memset(&opts, 0, sizeof(opts));
2610
  nc = mg_create_connection(lc->mgr, lc->handler, opts);
2611
  if (nc == NULL) return NULL;
2612
  nc->listener = lc;
2613
  nc->proto_handler = lc->proto_handler;
2614
  nc->user_data = lc->user_data;
2615
  nc->recv_mbuf_limit = lc->recv_mbuf_limit;
2616
  nc->iface = lc->iface;
2617
  if (lc->flags & MG_F_SSL) nc->flags |= MG_F_SSL;
2618
  mg_add_conn(nc->mgr, nc);
2619
  DBG(("%p %p %d %d", lc, nc, nc->sock, (int) nc->flags));
2620
  return nc;
2621
}
2622

    
2623
void mg_if_accept_tcp_cb(struct mg_connection *nc, union socket_address *sa,
2624
                         size_t sa_len) {
2625
  (void) sa_len;
2626
  nc->sa = *sa;
2627
  mg_call(nc, NULL, nc->user_data, MG_EV_ACCEPT, &nc->sa);
2628
}
2629

    
2630
void mg_send(struct mg_connection *nc, const void *buf, int len) {
2631
  nc->last_io_time = (time_t) mg_time();
2632
  if (nc->flags & MG_F_UDP) {
2633
    nc->iface->vtable->udp_send(nc, buf, len);
2634
  } else {
2635
    nc->iface->vtable->tcp_send(nc, buf, len);
2636
  }
2637
}
2638

    
2639
void mg_if_sent_cb(struct mg_connection *nc, int num_sent) {
2640
  DBG(("%p %d", nc, num_sent));
2641
#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP
2642
  if (nc->mgr && nc->mgr->hexdump_file != NULL) {
2643
    char *buf = nc->send_mbuf.buf;
2644
    mg_hexdump_connection(nc, nc->mgr->hexdump_file, buf, num_sent, MG_EV_SEND);
2645
  }
2646
#endif
2647
  if (num_sent < 0) {
2648
    nc->flags |= MG_F_CLOSE_IMMEDIATELY;
2649
  } else {
2650
    mbuf_remove(&nc->send_mbuf, num_sent);
2651
    mbuf_trim(&nc->send_mbuf);
2652
  }
2653
  mg_call(nc, NULL, nc->user_data, MG_EV_SEND, &num_sent);
2654
}
2655

    
2656
MG_INTERNAL void mg_recv_common(struct mg_connection *nc, void *buf, int len,
2657
                                int own) {
2658
  DBG(("%p %d %u", nc, len, (unsigned int) nc->recv_mbuf.len));
2659

    
2660
#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP
2661
  if (nc->mgr && nc->mgr->hexdump_file != NULL) {
2662
    mg_hexdump_connection(nc, nc->mgr->hexdump_file, buf, len, MG_EV_RECV);
2663
  }
2664
#endif
2665

    
2666
  if (nc->flags & MG_F_CLOSE_IMMEDIATELY) {
2667
    DBG(("%p discarded %d bytes", nc, len));
2668
    /*
2669
     * This connection will not survive next poll. Do not deliver events,
2670
     * send data to /dev/null without acking.
2671
     */
2672
    if (own) {
2673
      MG_FREE(buf);
2674
    }
2675
    return;
2676
  }
2677
  nc->last_io_time = (time_t) mg_time();
2678
  if (!own) {
2679
    mbuf_append(&nc->recv_mbuf, buf, len);
2680
  } else if (nc->recv_mbuf.len == 0) {
2681
    /* Adopt buf as recv_mbuf's backing store. */
2682
    mbuf_free(&nc->recv_mbuf);
2683
    nc->recv_mbuf.buf = (char *) buf;
2684
    nc->recv_mbuf.size = nc->recv_mbuf.len = len;
2685
  } else {
2686
    mbuf_append(&nc->recv_mbuf, buf, len);
2687
    MG_FREE(buf);
2688
  }
2689
  mg_call(nc, NULL, nc->user_data, MG_EV_RECV, &len);
2690
}
2691

    
2692
void mg_if_recv_tcp_cb(struct mg_connection *nc, void *buf, int len, int own) {
2693
  mg_recv_common(nc, buf, len, own);
2694
}
2695

    
2696
void mg_if_recv_udp_cb(struct mg_connection *nc, void *buf, int len,
2697
                       union socket_address *sa, size_t sa_len) {
2698
  assert(nc->flags & MG_F_UDP);
2699
  DBG(("%p %u", nc, (unsigned int) len));
2700
  if (nc->flags & MG_F_LISTENING) {
2701
    struct mg_connection *lc = nc;
2702
    /*
2703
     * Do we have an existing connection for this source?
2704
     * This is very inefficient for long connection lists.
2705
     */
2706
    for (nc = mg_next(lc->mgr, NULL); nc != NULL; nc = mg_next(lc->mgr, nc)) {
2707
      if (memcmp(&nc->sa.sa, &sa->sa, sa_len) == 0 && nc->listener == lc) {
2708
        break;
2709
      }
2710
    }
2711
    if (nc == NULL) {
2712
      struct mg_add_sock_opts opts;
2713
      memset(&opts, 0, sizeof(opts));
2714
      /* Create fake connection w/out sock initialization */
2715
      nc = mg_create_connection_base(lc->mgr, lc->handler, opts);
2716
      if (nc != NULL) {
2717
        nc->sock = lc->sock;
2718
        nc->listener = lc;
2719
        nc->sa = *sa;
2720
        nc->proto_handler = lc->proto_handler;
2721
        nc->user_data = lc->user_data;
2722
        nc->recv_mbuf_limit = lc->recv_mbuf_limit;
2723
        nc->flags = MG_F_UDP;
2724
        /*
2725
         * Long-lived UDP "connections" i.e. interactions that involve more
2726
         * than one request and response are rare, most are transactional:
2727
         * response is sent and the "connection" is closed. Or - should be.
2728
         * But users (including ourselves) tend to forget about that part,
2729
         * because UDP is connectionless and one does not think about
2730
         * processing a UDP request as handling a connection that needs to be
2731
         * closed. Thus, we begin with SEND_AND_CLOSE flag set, which should
2732
         * be a reasonable default for most use cases, but it is possible to
2733
         * turn it off the connection should be kept alive after processing.
2734
         */
2735
        nc->flags |= MG_F_SEND_AND_CLOSE;
2736
        mg_add_conn(lc->mgr, nc);
2737
        mg_call(nc, NULL, nc->user_data, MG_EV_ACCEPT, &nc->sa);
2738
      } else {
2739
        DBG(("OOM"));
2740
        /* No return here, we still need to drop on the floor */
2741
      }
2742
    }
2743
  }
2744
  if (nc != NULL) {
2745
    mg_recv_common(nc, buf, len, 1);
2746
  } else {
2747
    /* Drop on the floor. */
2748
    MG_FREE(buf);
2749
  }
2750
}
2751

    
2752
/*
2753
 * Schedules an async connect for a resolved address and proto.
2754
 * Called from two places: `mg_connect_opt()` and from async resolver.
2755
 * When called from the async resolver, it must trigger `MG_EV_CONNECT` event
2756
 * with a failure flag to indicate connection failure.
2757
 */
2758
MG_INTERNAL struct mg_connection *mg_do_connect(struct mg_connection *nc,
2759
                                                int proto,
2760
                                                union socket_address *sa) {
2761
  DBG(("%p %s://%s:%hu", nc, proto == SOCK_DGRAM ? "udp" : "tcp",
2762
       inet_ntoa(sa->sin.sin_addr), ntohs(sa->sin.sin_port)));
2763

    
2764
  nc->flags |= MG_F_CONNECTING;
2765
  if (proto == SOCK_DGRAM) {
2766
    nc->iface->vtable->connect_udp(nc);
2767
  } else {
2768
    nc->iface->vtable->connect_tcp(nc, sa);
2769
  }
2770
  mg_add_conn(nc->mgr, nc);
2771
  return nc;
2772
}
2773

    
2774
void mg_if_connect_cb(struct mg_connection *nc, int err) {
2775
  DBG(("%p connect, err=%d", nc, err));
2776
  nc->flags &= ~MG_F_CONNECTING;
2777
  if (err != 0) {
2778
    nc->flags |= MG_F_CLOSE_IMMEDIATELY;
2779
  }
2780
  mg_call(nc, NULL, nc->user_data, MG_EV_CONNECT, &err);
2781
}
2782

    
2783
#if MG_ENABLE_ASYNC_RESOLVER
2784
/*
2785
 * Callback for the async resolver on mg_connect_opt() call.
2786
 * Main task of this function is to trigger MG_EV_CONNECT event with
2787
 *    either failure (and dealloc the connection)
2788
 *    or success (and proceed with connect()
2789
 */
2790
static void resolve_cb(struct mg_dns_message *msg, void *data,
2791
                       enum mg_resolve_err e) {
2792
  struct mg_connection *nc = (struct mg_connection *) data;
2793
  int i;
2794
  int failure = -1;
2795

    
2796
  nc->flags &= ~MG_F_RESOLVING;
2797
  if (msg != NULL) {
2798
    /*
2799
     * Take the first DNS A answer and run...
2800
     */
2801
    for (i = 0; i < msg->num_answers; i++) {
2802
      if (msg->answers[i].rtype == MG_DNS_A_RECORD) {
2803
        /*
2804
         * Async resolver guarantees that there is at least one answer.
2805
         * TODO(lsm): handle IPv6 answers too
2806
         */
2807
        mg_dns_parse_record_data(msg, &msg->answers[i], &nc->sa.sin.sin_addr,
2808
                                 4);
2809
        mg_do_connect(nc, nc->flags & MG_F_UDP ? SOCK_DGRAM : SOCK_STREAM,
2810
                      &nc->sa);
2811
        return;
2812
      }
2813
    }
2814
  }
2815

    
2816
  if (e == MG_RESOLVE_TIMEOUT) {
2817
    double now = mg_time();
2818
    mg_call(nc, NULL, nc->user_data, MG_EV_TIMER, &now);
2819
  }
2820

    
2821
  /*
2822
   * If we get there was no MG_DNS_A_RECORD in the answer
2823
   */
2824
  mg_call(nc, NULL, nc->user_data, MG_EV_CONNECT, &failure);
2825
  mg_call(nc, NULL, nc->user_data, MG_EV_CLOSE, NULL);
2826
  mg_destroy_conn(nc, 1 /* destroy_if */);
2827
}
2828
#endif
2829

    
2830
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *address,
2831
                                 MG_CB(mg_event_handler_t callback,
2832
                                       void *user_data)) {
2833
  struct mg_connect_opts opts;
2834
  memset(&opts, 0, sizeof(opts));
2835
  return mg_connect_opt(mgr, address, MG_CB(callback, user_data), opts);
2836
}
2837

    
2838
struct mg_connection *mg_connect_opt(struct mg_mgr *mgr, const char *address,
2839
                                     MG_CB(mg_event_handler_t callback,
2840
                                           void *user_data),
2841
                                     struct mg_connect_opts opts) {
2842
  struct mg_connection *nc = NULL;
2843
  int proto, rc;
2844
  struct mg_add_sock_opts add_sock_opts;
2845
  char host[MG_MAX_HOST_LEN];
2846

    
2847
  MG_COPY_COMMON_CONNECTION_OPTIONS(&add_sock_opts, &opts);
2848

    
2849
  if ((nc = mg_create_connection(mgr, callback, add_sock_opts)) == NULL) {
2850
    return NULL;
2851
  }
2852

    
2853
  if ((rc = mg_parse_address(address, &nc->sa, &proto, host, sizeof(host))) <
2854
      0) {
2855
    /* Address is malformed */
2856
    MG_SET_PTRPTR(opts.error_string, "cannot parse address");
2857
    mg_destroy_conn(nc, 1 /* destroy_if */);
2858
    return NULL;
2859
  }
2860

    
2861
  nc->flags |= opts.flags & _MG_ALLOWED_CONNECT_FLAGS_MASK;
2862
  nc->flags |= (proto == SOCK_DGRAM) ? MG_F_UDP : 0;
2863
#if MG_ENABLE_CALLBACK_USERDATA
2864
  nc->user_data = user_data;
2865
#else
2866
  nc->user_data = opts.user_data;
2867
#endif
2868

    
2869
#if MG_ENABLE_SSL
2870
  DBG(("%p %s %s,%s,%s", nc, address, (opts.ssl_cert ? opts.ssl_cert : "-"),
2871
       (opts.ssl_key ? opts.ssl_key : "-"),
2872
       (opts.ssl_ca_cert ? opts.ssl_ca_cert : "-")));
2873

    
2874
  if (opts.ssl_cert != NULL || opts.ssl_ca_cert != NULL ||
2875
      opts.ssl_psk_identity != NULL) {
2876
    const char *err_msg = NULL;
2877
    struct mg_ssl_if_conn_params params;
2878
    if (nc->flags & MG_F_UDP) {
2879
      MG_SET_PTRPTR(opts.error_string, "SSL for UDP is not supported");
2880
      mg_destroy_conn(nc, 1 /* destroy_if */);
2881
      return NULL;
2882
    }
2883
    memset(&params, 0, sizeof(params));
2884
    params.cert = opts.ssl_cert;
2885
    params.key = opts.ssl_key;
2886
    params.ca_cert = opts.ssl_ca_cert;
2887
    params.cipher_suites = opts.ssl_cipher_suites;
2888
    params.psk_identity = opts.ssl_psk_identity;
2889
    params.psk_key = opts.ssl_psk_key;
2890
    if (opts.ssl_ca_cert != NULL) {
2891
      if (opts.ssl_server_name != NULL) {
2892
        if (strcmp(opts.ssl_server_name, "*") != 0) {
2893
          params.server_name = opts.ssl_server_name;
2894
        }
2895
      } else if (rc == 0) { /* If it's a DNS name, use host. */
2896
        params.server_name = host;
2897
      }
2898
    }
2899
    if (mg_ssl_if_conn_init(nc, &params, &err_msg) != MG_SSL_OK) {
2900
      MG_SET_PTRPTR(opts.error_string, err_msg);
2901
      mg_destroy_conn(nc, 1 /* destroy_if */);
2902
      return NULL;
2903
    }
2904
    nc->flags |= MG_F_SSL;
2905
  }
2906
#endif /* MG_ENABLE_SSL */
2907

    
2908
  if (rc == 0) {
2909
#if MG_ENABLE_ASYNC_RESOLVER
2910
    /*
2911
     * DNS resolution is required for host.
2912
     * mg_parse_address() fills port in nc->sa, which we pass to resolve_cb()
2913
     */
2914
    struct mg_connection *dns_conn = NULL;
2915
    struct mg_resolve_async_opts o;
2916
    memset(&o, 0, sizeof(o));
2917
    o.dns_conn = &dns_conn;
2918
    o.nameserver = opts.nameserver;
2919
    if (mg_resolve_async_opt(nc->mgr, host, MG_DNS_A_RECORD, resolve_cb, nc,
2920
                             o) != 0) {
2921
      MG_SET_PTRPTR(opts.error_string, "cannot schedule DNS lookup");
2922
      mg_destroy_conn(nc, 1 /* destroy_if */);
2923
      return NULL;
2924
    }
2925
    nc->priv_2 = dns_conn;
2926
    nc->flags |= MG_F_RESOLVING;
2927
    return nc;
2928
#else
2929
    MG_SET_PTRPTR(opts.error_string, "Resolver is disabled");
2930
    mg_destroy_conn(nc, 1 /* destroy_if */);
2931
    return NULL;
2932
#endif
2933
  } else {
2934
    /* Address is parsed and resolved to IP. proceed with connect() */
2935
    return mg_do_connect(nc, proto, &nc->sa);
2936
  }
2937
}
2938

    
2939
struct mg_connection *mg_bind(struct mg_mgr *srv, const char *address,
2940
                              MG_CB(mg_event_handler_t event_handler,
2941
                                    void *user_data)) {
2942
  struct mg_bind_opts opts;
2943
  memset(&opts, 0, sizeof(opts));
2944
  return mg_bind_opt(srv, address, MG_CB(event_handler, user_data), opts);
2945
}
2946

    
2947
struct mg_connection *mg_bind_opt(struct mg_mgr *mgr, const char *address,
2948
                                  MG_CB(mg_event_handler_t callback,
2949
                                        void *user_data),
2950
                                  struct mg_bind_opts opts) {
2951
  union socket_address sa;
2952
  struct mg_connection *nc = NULL;
2953
  int proto, rc;
2954
  struct mg_add_sock_opts add_sock_opts;
2955
  char host[MG_MAX_HOST_LEN];
2956

    
2957
#if MG_ENABLE_CALLBACK_USERDATA
2958
  opts.user_data = user_data;
2959
#endif
2960

    
2961
  if (callback == NULL) {
2962
    MG_SET_PTRPTR(opts.error_string, "handler is required");
2963
    return NULL;
2964
  }
2965

    
2966
  MG_COPY_COMMON_CONNECTION_OPTIONS(&add_sock_opts, &opts);
2967

    
2968
  if (mg_parse_address(address, &sa, &proto, host, sizeof(host)) <= 0) {
2969
    MG_SET_PTRPTR(opts.error_string, "cannot parse address");
2970
    return NULL;
2971
  }
2972

    
2973
  nc = mg_create_connection(mgr, callback, add_sock_opts);
2974
  if (nc == NULL) {
2975
    return NULL;
2976
  }
2977

    
2978
  nc->sa = sa;
2979
  nc->flags |= MG_F_LISTENING;
2980
  if (proto == SOCK_DGRAM) nc->flags |= MG_F_UDP;
2981

    
2982
#if MG_ENABLE_SSL
2983
  DBG(("%p %s %s,%s,%s", nc, address, (opts.ssl_cert ? opts.ssl_cert : "-"),
2984
       (opts.ssl_key ? opts.ssl_key : "-"),
2985
       (opts.ssl_ca_cert ? opts.ssl_ca_cert : "-")));
2986

    
2987
  if (opts.ssl_cert != NULL || opts.ssl_ca_cert != NULL) {
2988
    const char *err_msg = NULL;
2989
    struct mg_ssl_if_conn_params params;
2990
    if (nc->flags & MG_F_UDP) {
2991
      MG_SET_PTRPTR(opts.error_string, "SSL for UDP is not supported");
2992
      mg_destroy_conn(nc, 1 /* destroy_if */);
2993
      return NULL;
2994
    }
2995
    memset(&params, 0, sizeof(params));
2996
    params.cert = opts.ssl_cert;
2997
    params.key = opts.ssl_key;
2998
    params.ca_cert = opts.ssl_ca_cert;
2999
    params.cipher_suites = opts.ssl_cipher_suites;
3000
    if (mg_ssl_if_conn_init(nc, &params, &err_msg) != MG_SSL_OK) {
3001
      MG_SET_PTRPTR(opts.error_string, err_msg);
3002
      mg_destroy_conn(nc, 1 /* destroy_if */);
3003
      return NULL;
3004
    }
3005
    nc->flags |= MG_F_SSL;
3006
  }
3007
#endif /* MG_ENABLE_SSL */
3008

    
3009
  if (nc->flags & MG_F_UDP) {
3010
    rc = nc->iface->vtable->listen_udp(nc, &nc->sa);
3011
  } else {
3012
    rc = nc->iface->vtable->listen_tcp(nc, &nc->sa);
3013
  }
3014
  if (rc != 0) {
3015
    DBG(("Failed to open listener: %d", rc));
3016
    MG_SET_PTRPTR(opts.error_string, "failed to open listener");
3017
    mg_destroy_conn(nc, 1 /* destroy_if */);
3018
    return NULL;
3019
  }
3020
  mg_add_conn(nc->mgr, nc);
3021

    
3022
  return nc;
3023
}
3024

    
3025
struct mg_connection *mg_next(struct mg_mgr *s, struct mg_connection *conn) {
3026
  return conn == NULL ? s->active_connections : conn->next;
3027
}
3028

    
3029
#if MG_ENABLE_BROADCAST
3030
void mg_broadcast(struct mg_mgr *mgr, mg_event_handler_t cb, void *data,
3031
                  size_t len) {
3032
  struct ctl_msg ctl_msg;
3033

    
3034
  /*
3035
   * Mongoose manager has a socketpair, `struct mg_mgr::ctl`,
3036
   * where `mg_broadcast()` pushes the message.
3037
   * `mg_mgr_poll()` wakes up, reads a message from the socket pair, and calls
3038
   * specified callback for each connection. Thus the callback function executes
3039
   * in event manager thread.
3040
   */
3041
  if (mgr->ctl[0] != INVALID_SOCKET && data != NULL &&
3042
      len < sizeof(ctl_msg.message)) {
3043
    size_t dummy;
3044

    
3045
    ctl_msg.callback = cb;
3046
    memcpy(ctl_msg.message, data, len);
3047
    dummy = MG_SEND_FUNC(mgr->ctl[0], (char *) &ctl_msg,
3048
                         offsetof(struct ctl_msg, message) + len, 0);
3049
    dummy = MG_RECV_FUNC(mgr->ctl[0], (char *) &len, 1, 0);
3050
    (void) dummy; /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509 */
3051
  }
3052
}
3053
#endif /* MG_ENABLE_BROADCAST */
3054

    
3055
static int isbyte(int n) {
3056
  return n >= 0 && n <= 255;
3057
}
3058

    
3059
static int parse_net(const char *spec, uint32_t *net, uint32_t *mask) {
3060
  int n, a, b, c, d, slash = 32, len = 0;
3061

    
3062
  if ((sscanf(spec, "%d.%d.%d.%d/%d%n", &a, &b, &c, &d, &slash, &n) == 5 ||
3063
       sscanf(spec, "%d.%d.%d.%d%n", &a, &b, &c, &d, &n) == 4) &&
3064
      isbyte(a) && isbyte(b) && isbyte(c) && isbyte(d) && slash >= 0 &&
3065
      slash < 33) {
3066
    len = n;
3067
    *net =
3068
        ((uint32_t) a << 24) | ((uint32_t) b << 16) | ((uint32_t) c << 8) | d;
3069
    *mask = slash ? 0xffffffffU << (32 - slash) : 0;
3070
  }
3071

    
3072
  return len;
3073
}
3074

    
3075
int mg_check_ip_acl(const char *acl, uint32_t remote_ip) {
3076
  int allowed, flag;
3077
  uint32_t net, mask;
3078
  struct mg_str vec;
3079

    
3080
  /* If any ACL is set, deny by default */
3081
  allowed = (acl == NULL || *acl == '\0') ? '+' : '-';
3082

    
3083
  while ((acl = mg_next_comma_list_entry(acl, &vec, NULL)) != NULL) {
3084
    flag = vec.p[0];
3085
    if ((flag != '+' && flag != '-') ||
3086
        parse_net(&vec.p[1], &net, &mask) == 0) {
3087
      return -1;
3088
    }
3089

    
3090
    if (net == (remote_ip & mask)) {
3091
      allowed = flag;
3092
    }
3093
  }
3094

    
3095
  DBG(("%08x %c", (unsigned int) remote_ip, allowed));
3096
  return allowed == '+';
3097
}
3098

    
3099
/* Move data from one connection to another */
3100
void mg_forward(struct mg_connection *from, struct mg_connection *to) {
3101
  mg_send(to, from->recv_mbuf.buf, from->recv_mbuf.len);
3102
  mbuf_remove(&from->recv_mbuf, from->recv_mbuf.len);
3103
}
3104

    
3105
double mg_set_timer(struct mg_connection *c, double timestamp) {
3106
  double result = c->ev_timer_time;
3107
  c->ev_timer_time = timestamp;
3108
  /*
3109
   * If this connection is resolving, it's not in the list of active
3110
   * connections, so not processed yet. It has a DNS resolver connection
3111
   * linked to it. Set up a timer for the DNS connection.
3112
   */
3113
  DBG(("%p %p %d -> %lu", c, c->priv_2, (c->flags & MG_F_RESOLVING ? 1 : 0),
3114
       (unsigned long) timestamp));
3115
  if ((c->flags & MG_F_RESOLVING) && c->priv_2 != NULL) {
3116
    ((struct mg_connection *) c->priv_2)->ev_timer_time = timestamp;
3117
  }
3118
  return result;
3119
}
3120

    
3121
void mg_sock_set(struct mg_connection *nc, sock_t sock) {
3122
  if (sock != INVALID_SOCKET) {
3123
    nc->iface->vtable->sock_set(nc, sock);
3124
  }
3125
}
3126

    
3127
void mg_if_get_conn_addr(struct mg_connection *nc, int remote,
3128
                         union socket_address *sa) {
3129
  nc->iface->vtable->get_conn_addr(nc, remote, sa);
3130
}
3131

    
3132
struct mg_connection *mg_add_sock_opt(struct mg_mgr *s, sock_t sock,
3133
                                      MG_CB(mg_event_handler_t callback,
3134
                                            void *user_data),
3135
                                      struct mg_add_sock_opts opts) {
3136
struct mg_connection *nc =NULL;//wyf
3137
#if MG_ENABLE_CALLBACK_USERDATA
3138
  opts.user_data = user_data;
3139
#endif
3140

    
3141
  nc = mg_create_connection_base(s, callback, opts);//wyf
3142
  if (nc != NULL) {
3143
    mg_sock_set(nc, sock);
3144
    mg_add_conn(nc->mgr, nc);
3145
  }
3146
  return nc;
3147
}
3148

    
3149
struct mg_connection *mg_add_sock(struct mg_mgr *s, sock_t sock,
3150
                                  MG_CB(mg_event_handler_t callback,
3151
                                        void *user_data)) {
3152
  struct mg_add_sock_opts opts;
3153
  memset(&opts, 0, sizeof(opts));
3154
  return mg_add_sock_opt(s, sock, MG_CB(callback, user_data), opts);
3155
}
3156

    
3157
double mg_time(void) {
3158
  return cs_time();
3159
}
3160
#ifdef MG_MODULE_LINES
3161
#line 1 "mongoose/src/mg_net_if_socket.h"
3162
#endif
3163
/*
3164
 * Copyright (c) 2014-2016 Cesanta Software Limited
3165
 * All rights reserved
3166
 */
3167

    
3168
#ifndef CS_MONGOOSE_SRC_NET_IF_SOCKET_H_
3169
#define CS_MONGOOSE_SRC_NET_IF_SOCKET_H_
3170

    
3171
/* Amalgamated: #include "mg_net_if.h" */
3172

    
3173
#ifdef __cplusplus
3174
extern "C" {
3175
#endif /* __cplusplus */
3176

    
3177
#ifndef MG_ENABLE_NET_IF_SOCKET
3178
#define MG_ENABLE_NET_IF_SOCKET MG_NET_IF == MG_NET_IF_SOCKET
3179
#endif
3180

    
3181
extern const struct mg_iface_vtable mg_socket_iface_vtable;
3182

    
3183
#ifdef __cplusplus
3184
}
3185
#endif /* __cplusplus */
3186

    
3187
#endif /* CS_MONGOOSE_SRC_NET_IF_SOCKET_H_ */
3188
#ifdef MG_MODULE_LINES
3189
#line 1 "mongoose/src/mg_net_if_socks.h"
3190
#endif
3191
/*
3192
* Copyright (c) 2014-2017 Cesanta Software Limited
3193
* All rights reserved
3194
*/
3195

    
3196
#ifndef CS_MONGOOSE_SRC_NET_IF_SOCKS_H_
3197
#define CS_MONGOOSE_SRC_NET_IF_SOCKS_H_
3198

    
3199
#if MG_ENABLE_SOCKS
3200
/* Amalgamated: #include "mg_net_if.h" */
3201

    
3202
#ifdef __cplusplus
3203
extern "C" {
3204
#endif /* __cplusplus */
3205

    
3206
extern const struct mg_iface_vtable mg_socks_iface_vtable;
3207

    
3208
#ifdef __cplusplus
3209
}
3210
#endif /* __cplusplus */
3211
#endif /* MG_ENABLE_SOCKS */
3212
#endif /* CS_MONGOOSE_SRC_NET_IF_SOCKS_H_ */
3213
#ifdef MG_MODULE_LINES
3214
#line 1 "mongoose/src/mg_net_if.c"
3215
#endif
3216
/* Amalgamated: #include "mg_net_if.h" */
3217
/* Amalgamated: #include "mg_internal.h" */
3218
/* Amalgamated: #include "mg_net_if_socket.h" */
3219

    
3220
extern const struct mg_iface_vtable mg_default_iface_vtable;
3221

    
3222
const struct mg_iface_vtable *mg_ifaces[] = {
3223
    &mg_default_iface_vtable,
3224
};
3225

    
3226
int mg_num_ifaces = (int) (sizeof(mg_ifaces) / sizeof(mg_ifaces[0]));
3227

    
3228
struct mg_iface *mg_if_create_iface(const struct mg_iface_vtable *vtable,
3229
                                    struct mg_mgr *mgr) {
3230
  struct mg_iface *iface = (struct mg_iface *) MG_CALLOC(1, sizeof(*iface));
3231
  iface->mgr = mgr;
3232
  iface->data = NULL;
3233
  iface->vtable = vtable;
3234
  return iface;
3235
}
3236

    
3237
struct mg_iface *mg_find_iface(struct mg_mgr *mgr,
3238
                               const struct mg_iface_vtable *vtable,
3239
                               struct mg_iface *from) {
3240
  int i = 0;
3241
  if (from != NULL) {
3242
    for (i = 0; i < mgr->num_ifaces; i++) {
3243
      if (mgr->ifaces[i] == from) {
3244
        i++;
3245
        break;
3246
      }
3247
    }
3248
  }
3249

    
3250
  for (; i < mgr->num_ifaces; i++) {
3251
    if (mgr->ifaces[i]->vtable == vtable) {
3252
      return mgr->ifaces[i];
3253
    }
3254
  }
3255
  return NULL;
3256
}
3257
#ifdef MG_MODULE_LINES
3258
#line 1 "mongoose/src/mg_net_if_socket.c"
3259
#endif
3260
/*
3261
 * Copyright (c) 2014-2016 Cesanta Software Limited
3262
 * All rights reserved
3263
 */
3264

    
3265
#if MG_ENABLE_NET_IF_SOCKET
3266

    
3267
/* Amalgamated: #include "mg_net_if_socket.h" */
3268
/* Amalgamated: #include "mg_internal.h" */
3269
/* Amalgamated: #include "mg_util.h" */
3270

    
3271
#define MG_TCP_RECV_BUFFER_SIZE 1024
3272
#define MG_UDP_RECV_BUFFER_SIZE 1500
3273

    
3274
static sock_t mg_open_listening_socket(union socket_address *sa, int type,
3275
                                       int proto);
3276
#if MG_ENABLE_SSL
3277
static void mg_ssl_begin(struct mg_connection *nc);
3278
#endif
3279

    
3280
void mg_set_non_blocking_mode(sock_t sock) {
3281
#ifdef _WIN32
3282
  unsigned long on = 1;
3283
  ioctlsocket(sock, FIONBIO, &on);
3284
#else
3285
  int flags = fcntl(sock, F_GETFL, 0);
3286
  fcntl(sock, F_SETFL, flags | O_NONBLOCK);
3287
#endif
3288
}
3289

    
3290
static int mg_is_error(void) {
3291
  int err = mg_get_errno();
3292
  return err != EINPROGRESS && err != EWOULDBLOCK
3293
#ifndef WINCE
3294
         && err != EAGAIN && err != EINTR
3295
#endif
3296
#ifdef _WIN32
3297
         && WSAGetLastError() != WSAEINTR && WSAGetLastError() != WSAEWOULDBLOCK
3298
#endif
3299
      ;
3300
}
3301

    
3302
void mg_socket_if_connect_tcp(struct mg_connection *nc,
3303
                              const union socket_address *sa) {
3304
  int rc, proto = 0;
3305
  nc->sock = socket(AF_INET, SOCK_STREAM, proto);
3306
  if (nc->sock == INVALID_SOCKET) {
3307
    nc->err = mg_get_errno() ? mg_get_errno() : 1;
3308
    return;
3309
  }
3310
#if !defined(MG_ESP8266)
3311
  mg_set_non_blocking_mode(nc->sock);
3312
#endif
3313
  rc = connect(nc->sock, &sa->sa, sizeof(sa->sin));
3314
  nc->err = rc < 0 && mg_is_error() ? mg_get_errno() : 0;
3315
  DBG(("%p sock %d rc %d errno %d err %d", nc, nc->sock, rc, mg_get_errno(),
3316
       nc->err));
3317
}
3318

    
3319
void mg_socket_if_connect_udp(struct mg_connection *nc) {
3320
  nc->sock = socket(AF_INET, SOCK_DGRAM, 0);
3321
  if (nc->sock == INVALID_SOCKET) {
3322
    nc->err = mg_get_errno() ? mg_get_errno() : 1;
3323
    return;
3324
  }
3325
  if (nc->flags & MG_F_ENABLE_BROADCAST) {
3326
    int optval = 1;
3327
    if (setsockopt(nc->sock, SOL_SOCKET, SO_BROADCAST, (const char *) &optval,
3328
                   sizeof(optval)) < 0) {
3329
      nc->err = mg_get_errno() ? mg_get_errno() : 1;
3330
      return;
3331
    }
3332
  }
3333
  nc->err = 0;
3334
}
3335

    
3336
int mg_socket_if_listen_tcp(struct mg_connection *nc,
3337
                            union socket_address *sa) {
3338
  int proto = 0;
3339
  sock_t sock = mg_open_listening_socket(sa, SOCK_STREAM, proto);
3340
  if (sock == INVALID_SOCKET) {
3341
    return (mg_get_errno() ? mg_get_errno() : 1);
3342
  }
3343
  mg_sock_set(nc, sock);
3344
  return 0;
3345
}
3346

    
3347
int mg_socket_if_listen_udp(struct mg_connection *nc,
3348
                            union socket_address *sa) {
3349
  sock_t sock = mg_open_listening_socket(sa, SOCK_DGRAM, 0);
3350
  if (sock == INVALID_SOCKET) return (mg_get_errno() ? mg_get_errno() : 1);
3351
  mg_sock_set(nc, sock);
3352
  return 0;
3353
}
3354

    
3355
void mg_socket_if_tcp_send(struct mg_connection *nc, const void *buf,
3356
                           size_t len) {
3357
  mbuf_append(&nc->send_mbuf, buf, len);
3358
}
3359

    
3360
void mg_socket_if_udp_send(struct mg_connection *nc, const void *buf,
3361
                           size_t len) {
3362
  mbuf_append(&nc->send_mbuf, buf, len);
3363
}
3364

    
3365
void mg_socket_if_recved(struct mg_connection *nc, size_t len) {
3366
  (void) nc;
3367
  (void) len;
3368
}
3369

    
3370
int mg_socket_if_create_conn(struct mg_connection *nc) {
3371
  (void) nc;
3372
  return 1;
3373
}
3374

    
3375
void mg_socket_if_destroy_conn(struct mg_connection *nc) {
3376
  if (nc->sock == INVALID_SOCKET) return;
3377
  if (!(nc->flags & MG_F_UDP)) {
3378
    closesocket(nc->sock);
3379
  } else {
3380
    /* Only close outgoing UDP sockets or listeners. */
3381
    if (nc->listener == NULL) closesocket(nc->sock);
3382
  }
3383
  nc->sock = INVALID_SOCKET;
3384
}
3385

    
3386
static int mg_accept_conn(struct mg_connection *lc) {
3387
  struct mg_connection *nc;
3388
  union socket_address sa;
3389
  socklen_t sa_len = sizeof(sa);
3390
  /* NOTE(lsm): on Windows, sock is always > FD_SETSIZE */
3391
  sock_t sock = accept(lc->sock, &sa.sa, &sa_len);
3392
  if (sock == INVALID_SOCKET) {
3393
    if (mg_is_error()) DBG(("%p: failed to accept: %d", lc, mg_get_errno()));
3394
    return 0;
3395
  }
3396
  nc = mg_if_accept_new_conn(lc);
3397
  if (nc == NULL) {
3398
    closesocket(sock);
3399
    return 0;
3400
  }
3401
  DBG(("%p conn from %s:%d", nc, inet_ntoa(sa.sin.sin_addr),
3402
       ntohs(sa.sin.sin_port)));
3403
  mg_sock_set(nc, sock);
3404
#if MG_ENABLE_SSL
3405
  if (lc->flags & MG_F_SSL) {
3406
    if (mg_ssl_if_conn_accept(nc, lc) != MG_SSL_OK) mg_close_conn(nc);
3407
  } else
3408
#endif
3409
  {
3410
    mg_if_accept_tcp_cb(nc, &sa, sa_len);
3411
  }
3412
  return 1;
3413
}
3414

    
3415
/* 'sa' must be an initialized address to bind to */
3416
static sock_t mg_open_listening_socket(union socket_address *sa, int type,
3417
                                       int proto) {
3418
  socklen_t sa_len =
3419
      (sa->sa.sa_family == AF_INET) ? sizeof(sa->sin) : sizeof(sa->sin6);
3420
  sock_t sock = INVALID_SOCKET;
3421
#if !MG_LWIP
3422
  int on = 1;
3423
#endif
3424

    
3425
  if ((sock = socket(sa->sa.sa_family, type, proto)) != INVALID_SOCKET &&
3426
#if !MG_LWIP /* LWIP doesn't support either */
3427
#if defined(_WIN32) && defined(SO_EXCLUSIVEADDRUSE) && !defined(WINCE)
3428
      /* "Using SO_REUSEADDR and SO_EXCLUSIVEADDRUSE" http://goo.gl/RmrFTm */
3429
      !setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (void *) &on,
3430
                  sizeof(on)) &&
3431
#endif
3432

    
3433
#if !defined(_WIN32) || !defined(SO_EXCLUSIVEADDRUSE)
3434
      /*
3435
       * SO_RESUSEADDR is not enabled on Windows because the semantics of
3436
       * SO_REUSEADDR on UNIX and Windows is different. On Windows,
3437
       * SO_REUSEADDR allows to bind a socket to a port without error even if
3438
       * the port is already open by another program. This is not the behavior
3439
       * SO_REUSEADDR was designed for, and leads to hard-to-track failure
3440
       * scenarios. Therefore, SO_REUSEADDR was disabled on Windows unless
3441
       * SO_EXCLUSIVEADDRUSE is supported and set on a socket.
3442
       */
3443
      !setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &on, sizeof(on)) &&
3444
#endif
3445
#endif /* !MG_LWIP */
3446

    
3447
      !bind(sock, &sa->sa, sa_len) &&
3448
      (type == SOCK_DGRAM || listen(sock, SOMAXCONN) == 0)) {
3449
#if !MG_LWIP
3450
    mg_set_non_blocking_mode(sock);
3451
    /* In case port was set to 0, get the real port number */
3452
    (void) getsockname(sock, &sa->sa, &sa_len);
3453
#endif
3454
  } else if (sock != INVALID_SOCKET) {
3455
    closesocket(sock);
3456
    sock = INVALID_SOCKET;
3457
  }
3458

    
3459
  return sock;
3460
}
3461

    
3462
static void mg_write_to_socket(struct mg_connection *nc) {
3463
  struct mbuf *io = &nc->send_mbuf;
3464
  int n = 0;
3465

    
3466
#if MG_LWIP
3467
  /* With LWIP we don't know if the socket is ready */
3468
  if (io->len == 0) return;
3469
#endif
3470

    
3471
  assert(io->len > 0);
3472

    
3473
  if (nc->flags & MG_F_UDP) {
3474
    int n =
3475
        sendto(nc->sock, io->buf, io->len, 0, &nc->sa.sa, sizeof(nc->sa.sin));
3476
    DBG(("%p %d %d %d %s:%hu", nc, nc->sock, n, mg_get_errno(),
3477
         inet_ntoa(nc->sa.sin.sin_addr), ntohs(nc->sa.sin.sin_port)));
3478
    mg_if_sent_cb(nc, n);
3479
    return;
3480
  }
3481

    
3482
#if MG_ENABLE_SSL
3483
  if (nc->flags & MG_F_SSL) {
3484
    if (nc->flags & MG_F_SSL_HANDSHAKE_DONE) {
3485
      n = mg_ssl_if_write(nc, io->buf, io->len);
3486
      DBG(("%p %d bytes -> %d (SSL)", nc, n, nc->sock));
3487
      if (n < 0) {
3488
        if (n != MG_SSL_WANT_READ && n != MG_SSL_WANT_WRITE) {
3489
          nc->flags |= MG_F_CLOSE_IMMEDIATELY;
3490
        }
3491
        return;
3492
      } else {
3493
        /* Successful SSL operation, clear off SSL wait flags */
3494
        nc->flags &= ~(MG_F_WANT_READ | MG_F_WANT_WRITE);
3495
      }
3496
    } else {
3497
      mg_ssl_begin(nc);
3498
      return;
3499
    }
3500
  } else
3501
#endif
3502
  {
3503
    n = (int) MG_SEND_FUNC(nc->sock, io->buf, io->len, 0);
3504
    DBG(("%p %d bytes -> %d", nc, n, nc->sock));
3505
  }
3506

    
3507
  mg_if_sent_cb(nc, n);
3508
}
3509

    
3510
MG_INTERNAL size_t recv_avail_size(struct mg_connection *conn, size_t max) {
3511
  size_t avail;
3512
  if (conn->recv_mbuf_limit < conn->recv_mbuf.len) return 0;
3513
  avail = conn->recv_mbuf_limit - conn->recv_mbuf.len;
3514
  return avail > max ? max : avail;
3515
}
3516

    
3517
static void mg_handle_tcp_read(struct mg_connection *conn) {
3518
  int n = 0;
3519
  char *buf = (char *) MG_MALLOC(MG_TCP_RECV_BUFFER_SIZE);
3520

    
3521
  if (buf == NULL) {
3522
    DBG(("OOM"));
3523
    return;
3524
  }
3525

    
3526
#if MG_ENABLE_SSL
3527
  if (conn->flags & MG_F_SSL) {
3528
    if (conn->flags & MG_F_SSL_HANDSHAKE_DONE) {
3529
      /* SSL library may have more bytes ready to read than we ask to read.
3530
       * Therefore, read in a loop until we read everything. Without the loop,
3531
       * we skip to the next select() cycle which can just timeout. */
3532
      while ((n = mg_ssl_if_read(conn, buf, MG_TCP_RECV_BUFFER_SIZE)) > 0) {
3533
        DBG(("%p %d bytes <- %d (SSL)", conn, n, conn->sock));
3534
        mg_if_recv_tcp_cb(conn, buf, n, 1 /* own */);
3535
        buf = NULL;
3536
        if (conn->flags & MG_F_CLOSE_IMMEDIATELY) break;
3537
        /* buf has been freed, we need a new one. */
3538
        buf = (char *) MG_MALLOC(MG_TCP_RECV_BUFFER_SIZE);
3539
        if (buf == NULL) break;
3540
      }
3541
      MG_FREE(buf);
3542
      if (n < 0 && n != MG_SSL_WANT_READ) conn->flags |= MG_F_CLOSE_IMMEDIATELY;
3543
    } else {
3544
      MG_FREE(buf);
3545
      mg_ssl_begin(conn);
3546
      return;
3547
    }
3548
  } else
3549
#endif
3550
  {
3551
    n = (int) MG_RECV_FUNC(conn->sock, buf,
3552
                           recv_avail_size(conn, MG_TCP_RECV_BUFFER_SIZE), 0);
3553
    DBG(("%p %d bytes (PLAIN) <- %d", conn, n, conn->sock));
3554
    if (n > 0) {
3555
      mg_if_recv_tcp_cb(conn, buf, n, 1 /* own */);
3556
    } else {
3557
      MG_FREE(buf);
3558
    }
3559
    if (n == 0) {
3560
      /* Orderly shutdown of the socket, try flushing output. */
3561
      conn->flags |= MG_F_SEND_AND_CLOSE;
3562
    } else if (n < 0 && mg_is_error()) {
3563
      conn->flags |= MG_F_CLOSE_IMMEDIATELY;
3564
    }
3565
  }
3566
}
3567

    
3568
static int mg_recvfrom(struct mg_connection *nc, union socket_address *sa,
3569
                       socklen_t *sa_len, char **buf) {
3570
  int n;
3571
  *buf = (char *) MG_MALLOC(MG_UDP_RECV_BUFFER_SIZE);
3572
  if (*buf == NULL) {
3573
    DBG(("Out of memory"));
3574
    return -ENOMEM;
3575
  }
3576
  n = recvfrom(nc->sock, *buf, MG_UDP_RECV_BUFFER_SIZE, 0, &sa->sa, sa_len);
3577
  if (n <= 0) {
3578
    DBG(("%p recvfrom: %s", nc, strerror(mg_get_errno())));
3579
    MG_FREE(*buf);
3580
  }
3581
  return n;
3582
}
3583

    
3584
static void mg_handle_udp_read(struct mg_connection *nc) {
3585
  char *buf = NULL;
3586
  union socket_address sa;
3587
  socklen_t sa_len = sizeof(sa);
3588
  int n = mg_recvfrom(nc, &sa, &sa_len, &buf);
3589
  DBG(("%p %d bytes from %s:%d", nc, n, inet_ntoa(nc->sa.sin.sin_addr),
3590
       ntohs(nc->sa.sin.sin_port)));
3591
  mg_if_recv_udp_cb(nc, buf, n, &sa, sa_len);
3592
}
3593

    
3594
#if MG_ENABLE_SSL
3595
static void mg_ssl_begin(struct mg_connection *nc) {
3596
  int server_side = (nc->listener != NULL);
3597
  enum mg_ssl_if_result res = mg_ssl_if_handshake(nc);
3598
  DBG(("%p %d res %d", nc, server_side, res));
3599

    
3600
  if (res == MG_SSL_OK) {
3601
    nc->flags |= MG_F_SSL_HANDSHAKE_DONE;
3602
    nc->flags &= ~(MG_F_WANT_READ | MG_F_WANT_WRITE);
3603

    
3604
    if (server_side) {
3605
      union socket_address sa;
3606
      socklen_t sa_len = sizeof(sa);
3607
      (void) getpeername(nc->sock, &sa.sa, &sa_len);
3608
      mg_if_accept_tcp_cb(nc, &sa, sa_len);
3609
    } else {
3610
      mg_if_connect_cb(nc, 0);
3611
    }
3612
  } else if (res != MG_SSL_WANT_READ && res != MG_SSL_WANT_WRITE) {
3613
    if (!server_side) {
3614
      mg_if_connect_cb(nc, res);
3615
    }
3616
    nc->flags |= MG_F_CLOSE_IMMEDIATELY;
3617
  }
3618
}
3619
#endif /* MG_ENABLE_SSL */
3620

    
3621
#define _MG_F_FD_CAN_READ 1
3622
#define _MG_F_FD_CAN_WRITE 1 << 1
3623
#define _MG_F_FD_ERROR 1 << 2
3624

    
3625
void mg_mgr_handle_conn(struct mg_connection *nc, int fd_flags, double now) {
3626
  int worth_logging =
3627
      fd_flags != 0 || (nc->flags & (MG_F_WANT_READ | MG_F_WANT_WRITE));
3628
  if (worth_logging) {
3629
    DBG(("%p fd=%d fd_flags=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->sock,
3630
         fd_flags, nc->flags, (int) nc->recv_mbuf.len,
3631
         (int) nc->send_mbuf.len));
3632
  }
3633

    
3634
  if (nc->flags & MG_F_CONNECTING) {
3635
    if (fd_flags != 0) {
3636
      int err = 0;
3637
#if !defined(MG_ESP8266)
3638
      if (!(nc->flags & MG_F_UDP)) {
3639
        socklen_t len = sizeof(err);
3640
        int ret =
3641
            getsockopt(nc->sock, SOL_SOCKET, SO_ERROR, (char *) &err, &len);
3642
        if (ret != 0) {
3643
          err = 1;
3644
        } else if (err == EAGAIN || err == EWOULDBLOCK) {
3645
          err = 0;
3646
        }
3647
      }
3648
#else
3649
      /*
3650
       * On ESP8266 we use blocking connect.
3651
       */
3652
      err = nc->err;
3653
#endif
3654
#if MG_ENABLE_SSL
3655
      if ((nc->flags & MG_F_SSL) && err == 0) {
3656
        mg_ssl_begin(nc);
3657
      } else {
3658
        mg_if_connect_cb(nc, err);
3659
      }
3660
#else
3661
      mg_if_connect_cb(nc, err);
3662
#endif
3663
    } else if (nc->err != 0) {
3664
      mg_if_connect_cb(nc, nc->err);
3665
    }
3666
  }
3667

    
3668
  if (fd_flags & _MG_F_FD_CAN_READ) {
3669
    if (nc->flags & MG_F_UDP) {
3670
      mg_handle_udp_read(nc);
3671
    } else {
3672
      if (nc->flags & MG_F_LISTENING) {
3673
        /*
3674
         * We're not looping here, and accepting just one connection at
3675
         * a time. The reason is that eCos does not respect non-blocking
3676
         * flag on a listening socket and hangs in a loop.
3677
         */
3678
        mg_accept_conn(nc);
3679
      } else {
3680
        mg_handle_tcp_read(nc);
3681
      }
3682
    }
3683
  }
3684

    
3685
  if (!(nc->flags & MG_F_CLOSE_IMMEDIATELY)) {
3686
    if ((fd_flags & _MG_F_FD_CAN_WRITE) && nc->send_mbuf.len > 0) {
3687
      mg_write_to_socket(nc);
3688
    }
3689
    mg_if_poll(nc, (time_t) now);
3690
    mg_if_timer(nc, now);
3691
  }
3692

    
3693
  if (worth_logging) {
3694
    DBG(("%p after fd=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->sock, nc->flags,
3695
         (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len));
3696
  }
3697
}
3698

    
3699
#if MG_ENABLE_BROADCAST
3700
static void mg_mgr_handle_ctl_sock(struct mg_mgr *mgr) {
3701
  struct ctl_msg ctl_msg;
3702
  int len =
3703
      (int) MG_RECV_FUNC(mgr->ctl[1], (char *) &ctl_msg, sizeof(ctl_msg), 0);
3704
  size_t dummy = MG_SEND_FUNC(mgr->ctl[1], ctl_msg.message, 1, 0);
3705
  DBG(("read %d from ctl socket", len));
3706
  (void) dummy; /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509 */
3707
  if (len >= (int) sizeof(ctl_msg.callback) && ctl_msg.callback != NULL) {
3708
    struct mg_connection *nc;
3709
    for (nc = mg_next(mgr, NULL); nc != NULL; nc = mg_next(mgr, nc)) {
3710
      ctl_msg.callback(nc, MG_EV_POLL,
3711
                       ctl_msg.message MG_UD_ARG(nc->user_data));
3712
    }
3713
  }
3714
}
3715
#endif
3716

    
3717
/* Associate a socket to a connection. */
3718
void mg_socket_if_sock_set(struct mg_connection *nc, sock_t sock) {
3719
  mg_set_non_blocking_mode(sock);
3720
  mg_set_close_on_exec(sock);
3721
  nc->sock = sock;
3722
  DBG(("%p %d", nc, sock));
3723
}
3724

    
3725
void mg_socket_if_init(struct mg_iface *iface) {
3726
  (void) iface;
3727
  DBG(("%p using select()", iface->mgr));
3728
#if MG_ENABLE_BROADCAST
3729
  mg_socketpair(iface->mgr->ctl, SOCK_DGRAM);
3730
#endif
3731
}
3732

    
3733
void mg_socket_if_free(struct mg_iface *iface) {
3734
  (void) iface;
3735
}
3736

    
3737
void mg_socket_if_add_conn(struct mg_connection *nc) {
3738
  (void) nc;
3739
}
3740

    
3741
void mg_socket_if_remove_conn(struct mg_connection *nc) {
3742
  (void) nc;
3743
}
3744

    
3745
void mg_add_to_set(sock_t sock, fd_set *set, sock_t *max_fd) {
3746
  if (sock != INVALID_SOCKET
3747
#ifdef __unix__
3748
      && sock < (sock_t) FD_SETSIZE
3749
#endif
3750
      ) {
3751
    FD_SET(sock, set);
3752
    if (*max_fd == INVALID_SOCKET || sock > *max_fd) {
3753
      *max_fd = sock;
3754
    }
3755
  }
3756
}
3757

    
3758
time_t mg_socket_if_poll(struct mg_iface *iface, int timeout_ms) {
3759
  struct mg_mgr *mgr = iface->mgr;
3760
  double now = mg_time();
3761
  double min_timer;
3762
  struct mg_connection *nc, *tmp;
3763
  struct timeval tv;
3764
  fd_set read_set, write_set, err_set;
3765
  sock_t max_fd = INVALID_SOCKET;
3766
  int num_fds, num_ev, num_timers = 0;
3767
#ifdef __unix__
3768
  int try_dup = 1;
3769
#endif
3770

    
3771
  FD_ZERO(&read_set);
3772
  FD_ZERO(&write_set);
3773
  FD_ZERO(&err_set);
3774
#if MG_ENABLE_BROADCAST
3775
  mg_add_to_set(mgr->ctl[1], &read_set, &max_fd);
3776
#endif
3777

    
3778
  /*
3779
   * Note: it is ok to have connections with sock == INVALID_SOCKET in the list,
3780
   * e.g. timer-only "connections".
3781
   */
3782
  min_timer = 0;
3783
  for (nc = mgr->active_connections, num_fds = 0; nc != NULL; nc = tmp) {
3784
    tmp = nc->next;
3785

    
3786
    if (nc->sock != INVALID_SOCKET) {
3787
      num_fds++;
3788

    
3789
#ifdef __unix__
3790
      /* A hack to make sure all our file descriptos fit into FD_SETSIZE. */
3791
      if (nc->sock >= (sock_t) FD_SETSIZE && try_dup) {
3792
        int new_sock = dup(nc->sock);
3793
        if (new_sock >= 0) {
3794
          if (new_sock < (sock_t) FD_SETSIZE) {
3795
            closesocket(nc->sock);
3796
            DBG(("new sock %d -> %d", nc->sock, new_sock));
3797
            nc->sock = new_sock;
3798
          } else {
3799
            closesocket(new_sock);
3800
            DBG(("new sock is still larger than FD_SETSIZE, disregard"));
3801
            try_dup = 0;
3802
          }
3803
        } else {
3804
          try_dup = 0;
3805
        }
3806
      }
3807
#endif
3808

    
3809
      if (!(nc->flags & MG_F_WANT_WRITE) &&
3810
          nc->recv_mbuf.len < nc->recv_mbuf_limit &&
3811
          (!(nc->flags & MG_F_UDP) || nc->listener == NULL)) {
3812
        mg_add_to_set(nc->sock, &read_set, &max_fd);
3813
      }
3814

    
3815
      if (((nc->flags & MG_F_CONNECTING) && !(nc->flags & MG_F_WANT_READ)) ||
3816
          (nc->send_mbuf.len > 0 && !(nc->flags & MG_F_CONNECTING))) {
3817
        mg_add_to_set(nc->sock, &write_set, &max_fd);
3818
        mg_add_to_set(nc->sock, &err_set, &max_fd);
3819
      }
3820
    }
3821

    
3822
    if (nc->ev_timer_time > 0) {
3823
      if (num_timers == 0 || nc->ev_timer_time < min_timer) {
3824
        min_timer = nc->ev_timer_time;
3825
      }
3826
      num_timers++;
3827
    }
3828
  }
3829

    
3830
  /*
3831
   * If there is a timer to be fired earlier than the requested timeout,
3832
   * adjust the timeout.
3833
   */
3834
  if (num_timers > 0) {
3835
    double timer_timeout_ms = (min_timer - mg_time()) * 1000 + 1 /* rounding */;
3836
    if (timer_timeout_ms < timeout_ms) {
3837
      timeout_ms = (int) timer_timeout_ms;
3838
    }
3839
  }
3840
  if (timeout_ms < 0) timeout_ms = 0;
3841

    
3842
  tv.tv_sec = timeout_ms / 1000;
3843
  tv.tv_usec = (timeout_ms % 1000) * 1000;
3844

    
3845
  num_ev = select((int) max_fd + 1, &read_set, &write_set, &err_set, &tv);
3846
  now = mg_time();
3847
#if 0
3848
  DBG(("select @ %ld num_ev=%d of %d, timeout=%d", (long) now, num_ev, num_fds,
3849
       timeout_ms));
3850
#endif
3851

    
3852
#if MG_ENABLE_BROADCAST
3853
  if (num_ev > 0 && mgr->ctl[1] != INVALID_SOCKET &&
3854
      FD_ISSET(mgr->ctl[1], &read_set)) {
3855
    mg_mgr_handle_ctl_sock(mgr);
3856
  }
3857
#endif
3858

    
3859
  for (nc = mgr->active_connections; nc != NULL; nc = tmp) {
3860
    int fd_flags = 0;
3861
    if (nc->sock != INVALID_SOCKET) {
3862
      if (num_ev > 0) {
3863
        fd_flags = (FD_ISSET(nc->sock, &read_set) &&
3864
                            (!(nc->flags & MG_F_UDP) || nc->listener == NULL)
3865
                        ? _MG_F_FD_CAN_READ
3866
                        : 0) |
3867
                   (FD_ISSET(nc->sock, &write_set) ? _MG_F_FD_CAN_WRITE : 0) |
3868
                   (FD_ISSET(nc->sock, &err_set) ? _MG_F_FD_ERROR : 0);
3869
      }
3870
#if MG_LWIP
3871
      /* With LWIP socket emulation layer, we don't get write events for UDP */
3872
      if ((nc->flags & MG_F_UDP) && nc->listener == NULL) {
3873
        fd_flags |= _MG_F_FD_CAN_WRITE;
3874
      }
3875
#endif
3876
    }
3877
    tmp = nc->next;
3878
    mg_mgr_handle_conn(nc, fd_flags, now);
3879
  }
3880

    
3881
  for (nc = mgr->active_connections; nc != NULL; nc = tmp) {
3882
    tmp = nc->next;
3883
    if ((nc->flags & MG_F_CLOSE_IMMEDIATELY) ||
3884
        (nc->send_mbuf.len == 0 && (nc->flags & MG_F_SEND_AND_CLOSE))) {
3885
      mg_close_conn(nc);
3886
    }
3887
  }
3888

    
3889
  return (time_t) now;
3890
}
3891

    
3892
#if MG_ENABLE_BROADCAST
3893
MG_INTERNAL void mg_socketpair_close(sock_t *sock) {
3894
  while (1) {
3895
    if (closesocket(*sock) == -1 && errno == EINTR) continue;
3896
    break;
3897
  }
3898
  *sock = INVALID_SOCKET;
3899
}
3900

    
3901
MG_INTERNAL sock_t
3902
mg_socketpair_accept(sock_t sock, union socket_address *sa, socklen_t sa_len) {
3903
  sock_t rc;
3904
  while (1) {
3905
    if ((rc = accept(sock, &sa->sa, &sa_len)) == INVALID_SOCKET &&
3906
        errno == EINTR)
3907
      continue;
3908
    break;
3909
  }
3910
  return rc;
3911
}
3912

    
3913
int mg_socketpair(sock_t sp[2], int sock_type) {
3914
  union socket_address sa;
3915
  sock_t sock;
3916
  socklen_t len = sizeof(sa.sin);
3917
  int ret = 0;
3918

    
3919
  sock = sp[0] = sp[1] = INVALID_SOCKET;
3920

    
3921
  (void) memset(&sa, 0, sizeof(sa));
3922
  sa.sin.sin_family = AF_INET;
3923
  sa.sin.sin_port = htons(0);
3924
  sa.sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
3925

    
3926
  if ((sock = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) {
3927
  } else if (bind(sock, &sa.sa, len) != 0) {
3928
  } else if (sock_type == SOCK_STREAM && listen(sock, 1) != 0) {
3929
  } else if (getsockname(sock, &sa.sa, &len) != 0) {
3930
  } else if ((sp[0] = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) {
3931
  } else if (connect(sp[0], &sa.sa, len) != 0) {
3932
  } else if (sock_type == SOCK_DGRAM &&
3933
             (getsockname(sp[0], &sa.sa, &len) != 0 ||
3934
              connect(sock, &sa.sa, len) != 0)) {
3935
  } else if ((sp[1] = (sock_type == SOCK_DGRAM ? sock : mg_socketpair_accept(
3936
                                                            sock, &sa, len))) ==
3937
             INVALID_SOCKET) {
3938
  } else {
3939
    mg_set_close_on_exec(sp[0]);
3940
    mg_set_close_on_exec(sp[1]);
3941
    if (sock_type == SOCK_STREAM) mg_socketpair_close(&sock);
3942
    ret = 1;
3943
  }
3944

    
3945
  if (!ret) {
3946
    if (sp[0] != INVALID_SOCKET) mg_socketpair_close(&sp[0]);
3947
    if (sp[1] != INVALID_SOCKET) mg_socketpair_close(&sp[1]);
3948
    if (sock != INVALID_SOCKET) mg_socketpair_close(&sock);
3949
  }
3950

    
3951
  return ret;
3952
}
3953
#endif /* MG_ENABLE_BROADCAST */
3954

    
3955
static void mg_sock_get_addr(sock_t sock, int remote,
3956
                             union socket_address *sa) {
3957
  socklen_t slen = sizeof(*sa);
3958
  memset(sa, 0, slen);
3959
  if (remote) {
3960
    getpeername(sock, &sa->sa, &slen);
3961
  } else {
3962
    getsockname(sock, &sa->sa, &slen);
3963
  }
3964
}
3965

    
3966
void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags) {
3967
  union socket_address sa;
3968
  mg_sock_get_addr(sock, flags & MG_SOCK_STRINGIFY_REMOTE, &sa);
3969
  mg_sock_addr_to_str(&sa, buf, len, flags);
3970
}
3971

    
3972
void mg_socket_if_get_conn_addr(struct mg_connection *nc, int remote,
3973
                                union socket_address *sa) {
3974
  if ((nc->flags & MG_F_UDP) && remote) {
3975
    memcpy(sa, &nc->sa, sizeof(*sa));
3976
    return;
3977
  }
3978
  mg_sock_get_addr(nc->sock, remote, sa);
3979
}
3980

    
3981
/* clang-format off */
3982
#define MG_SOCKET_IFACE_VTABLE                                          \
3983
  {                                                                     \
3984
    mg_socket_if_init,                                                  \
3985
    mg_socket_if_free,                                                  \
3986
    mg_socket_if_add_conn,                                              \
3987
    mg_socket_if_remove_conn,                                           \
3988
    mg_socket_if_poll,                                                  \
3989
    mg_socket_if_listen_tcp,                                            \
3990
    mg_socket_if_listen_udp,                                            \
3991
    mg_socket_if_connect_tcp,                                           \
3992
    mg_socket_if_connect_udp,                                           \
3993
    mg_socket_if_tcp_send,                                              \
3994
    mg_socket_if_udp_send,                                              \
3995
    mg_socket_if_recved,                                                \
3996
    mg_socket_if_create_conn,                                           \
3997
    mg_socket_if_destroy_conn,                                          \
3998
    mg_socket_if_sock_set,                                              \
3999
    mg_socket_if_get_conn_addr,                                         \
4000
  }
4001
/* clang-format on */
4002

    
4003
const struct mg_iface_vtable mg_socket_iface_vtable = MG_SOCKET_IFACE_VTABLE;
4004
#if MG_NET_IF == MG_NET_IF_SOCKET
4005
const struct mg_iface_vtable mg_default_iface_vtable = MG_SOCKET_IFACE_VTABLE;
4006
#endif
4007

    
4008
#endif /* MG_ENABLE_NET_IF_SOCKET */
4009
#ifdef MG_MODULE_LINES
4010
#line 1 "mongoose/src/mg_net_if_socks.c"
4011
#endif
4012
/*
4013
 * Copyright (c) 2014-2016 Cesanta Software Limited
4014
 * All rights reserved
4015
 */
4016

    
4017
#if MG_ENABLE_SOCKS
4018

    
4019
struct socksdata {
4020
  char *proxy_addr;        /* HOST:PORT of the socks5 proxy server */
4021
  struct mg_connection *s; /* Respective connection to the server */
4022
  struct mg_connection *c; /* Connection to the client */
4023
  struct mbuf tmp;         /* Temporary buffer for sent data */
4024
};
4025

    
4026
static void socks_if_disband(struct socksdata *d) {
4027
  LOG(LL_DEBUG, ("disbanding proxy %p %p", d->c, d->s));
4028
  if (d->c) d->c->flags |= MG_F_SEND_AND_CLOSE;
4029
  if (d->s) d->s->flags |= MG_F_SEND_AND_CLOSE;
4030
  d->c = d->s = NULL;
4031
}
4032

    
4033
static void socks_if_handler(struct mg_connection *c, int ev, void *ev_data) {
4034
  struct socksdata *d = (struct socksdata *) c->user_data;
4035
  if (ev == MG_EV_CONNECT) {
4036
    int res = *(int *) ev_data;
4037
    if (res == 0) {
4038
      /* Send handshake to the proxy server */
4039
      unsigned char buf[] = {MG_SOCKS_VERSION, 1, MG_SOCKS_HANDSHAKE_NOAUTH};
4040
      mg_send(d->s, buf, sizeof(buf));
4041
      LOG(LL_DEBUG, ("Sent handshake to %s", d->proxy_addr));
4042
    } else {
4043
      LOG(LL_ERROR, ("Cannot connect to %s: %d", d->proxy_addr, res));
4044
      d->c->flags |= MG_F_CLOSE_IMMEDIATELY;
4045
    }
4046
  } else if (ev == MG_EV_CLOSE) {
4047
    socks_if_disband(d);
4048
  } else if (ev == MG_EV_RECV) {
4049
    /* Handle handshake reply */
4050
    if (!(c->flags & MG_SOCKS_HANDSHAKE_DONE)) {
4051
      /* TODO(lsm): process IPv6 too */
4052
      unsigned char buf[10] = {MG_SOCKS_VERSION, MG_SOCKS_CMD_CONNECT, 0,
4053
                               MG_SOCKS_ADDR_IPV4};
4054
      if (c->recv_mbuf.len < 2) return;
4055
      if ((unsigned char) c->recv_mbuf.buf[1] == MG_SOCKS_HANDSHAKE_FAILURE) {
4056
        LOG(LL_ERROR, ("Server kicked us out"));
4057
        socks_if_disband(d);
4058
        return;
4059
      }
4060
      mbuf_remove(&c->recv_mbuf, 2);
4061
      c->flags |= MG_SOCKS_HANDSHAKE_DONE;
4062

    
4063
      /* Send connect request */
4064
      memcpy(buf + 4, &d->c->sa.sin.sin_addr, 4);
4065
      memcpy(buf + 8, &d->c->sa.sin.sin_port, 2);
4066
      mg_send(c, buf, sizeof(buf));
4067
    }
4068
    /* Process connect request */
4069
    if ((c->flags & MG_SOCKS_HANDSHAKE_DONE) &&
4070
        !(c->flags & MG_SOCKS_CONNECT_DONE)) {
4071
      if (c->recv_mbuf.len < 10) return;
4072
      if (c->recv_mbuf.buf[1] != MG_SOCKS_SUCCESS) {
4073
        LOG(LL_ERROR, ("Socks connection error: %d", c->recv_mbuf.buf[1]));
4074
        socks_if_disband(d);
4075
        return;
4076
      }
4077
      mbuf_remove(&c->recv_mbuf, 10);
4078
      c->flags |= MG_SOCKS_CONNECT_DONE;
4079
      /* Connected. Move sent data from client, if any, to server */
4080
      if (d->s && d->c) {
4081
        mbuf_append(&d->s->send_mbuf, d->tmp.buf, d->tmp.len);
4082
        mbuf_free(&d->tmp);
4083
      }
4084
    }
4085
    /* All flags are set, we're in relay mode */
4086
    if ((c->flags & MG_SOCKS_CONNECT_DONE) && d->c && d->s) {
4087
      mbuf_append(&d->c->recv_mbuf, d->s->recv_mbuf.buf, d->s->recv_mbuf.len);
4088
      mbuf_remove(&d->s->recv_mbuf, d->s->recv_mbuf.len);
4089
    }
4090
  }
4091
}
4092

    
4093
static void mg_socks_if_connect_tcp(struct mg_connection *c,
4094
                                    const union socket_address *sa) {
4095
  struct socksdata *d = (struct socksdata *) c->iface->data;
4096
  d->c = c;
4097
  d->s = mg_connect(c->mgr, d->proxy_addr, socks_if_handler);
4098
  d->s->user_data = d;
4099
  LOG(LL_DEBUG, ("%p %s", c, d->proxy_addr));
4100
  (void) sa;
4101
}
4102

    
4103
static void mg_socks_if_connect_udp(struct mg_connection *c) {
4104
  (void) c;
4105
}
4106

    
4107
static int mg_socks_if_listen_tcp(struct mg_connection *c,
4108
                                  union socket_address *sa) {
4109
  (void) c;
4110
  (void) sa;
4111
  return 0;
4112
}
4113

    
4114
static int mg_socks_if_listen_udp(struct mg_connection *c,
4115
                                  union socket_address *sa) {
4116
  (void) c;
4117
  (void) sa;
4118
  return -1;
4119
}
4120

    
4121
static void mg_socks_if_tcp_send(struct mg_connection *c, const void *buf,
4122
                                 size_t len) {
4123
  struct socksdata *d = (struct socksdata *) c->iface->data;
4124
  LOG(LL_DEBUG, ("%p -> %p %d %d", c, buf, (int) len, (int) c->send_mbuf.len));
4125
  if (d && d->s && d->s->flags & MG_SOCKS_CONNECT_DONE) {
4126
    mbuf_append(&d->s->send_mbuf, d->tmp.buf, d->tmp.len);
4127
    mbuf_append(&d->s->send_mbuf, buf, len);
4128
    mbuf_free(&d->tmp);
4129
  } else {
4130
    mbuf_append(&d->tmp, buf, len);
4131
  }
4132
}
4133

    
4134
static void mg_socks_if_udp_send(struct mg_connection *c, const void *buf,
4135
                                 size_t len) {
4136
  (void) c;
4137
  (void) buf;
4138
  (void) len;
4139
}
4140

    
4141
static void mg_socks_if_recved(struct mg_connection *c, size_t len) {
4142
  (void) c;
4143
  (void) len;
4144
}
4145

    
4146
static int mg_socks_if_create_conn(struct mg_connection *c) {
4147
  (void) c;
4148
  return 1;
4149
}
4150

    
4151
static void mg_socks_if_destroy_conn(struct mg_connection *c) {
4152
  c->iface->vtable->free(c->iface);
4153
  MG_FREE(c->iface);
4154
  c->iface = NULL;
4155
  LOG(LL_DEBUG, ("%p", c));
4156
}
4157

    
4158
static void mg_socks_if_sock_set(struct mg_connection *c, sock_t sock) {
4159
  (void) c;
4160
  (void) sock;
4161
}
4162

    
4163
static void mg_socks_if_init(struct mg_iface *iface) {
4164
  (void) iface;
4165
}
4166

    
4167
static void mg_socks_if_free(struct mg_iface *iface) {
4168
  struct socksdata *d = (struct socksdata *) iface->data;
4169
  LOG(LL_DEBUG, ("%p", iface));
4170
  if (d != NULL) {
4171
    socks_if_disband(d);
4172
    mbuf_free(&d->tmp);
4173
    MG_FREE(d->proxy_addr);
4174
    MG_FREE(d);
4175
    iface->data = NULL;
4176
  }
4177
}
4178

    
4179
static void mg_socks_if_add_conn(struct mg_connection *c) {
4180
  c->sock = INVALID_SOCKET;
4181
}
4182

    
4183
static void mg_socks_if_remove_conn(struct mg_connection *c) {
4184
  (void) c;
4185
}
4186

    
4187
static time_t mg_socks_if_poll(struct mg_iface *iface, int timeout_ms) {
4188
  LOG(LL_DEBUG, ("%p", iface));
4189
  (void) iface;
4190
  (void) timeout_ms;
4191
  return (time_t) cs_time();
4192
}
4193

    
4194
static void mg_socks_if_get_conn_addr(struct mg_connection *c, int remote,
4195
                                      union socket_address *sa) {
4196
  LOG(LL_DEBUG, ("%p", c));
4197
  (void) c;
4198
  (void) remote;
4199
  (void) sa;
4200
}
4201

    
4202
const struct mg_iface_vtable mg_socks_iface_vtable = {
4203
    mg_socks_if_init,        mg_socks_if_free,
4204
    mg_socks_if_add_conn,    mg_socks_if_remove_conn,
4205
    mg_socks_if_poll,        mg_socks_if_listen_tcp,
4206
    mg_socks_if_listen_udp,  mg_socks_if_connect_tcp,
4207
    mg_socks_if_connect_udp, mg_socks_if_tcp_send,
4208
    mg_socks_if_udp_send,    mg_socks_if_recved,
4209
    mg_socks_if_create_conn, mg_socks_if_destroy_conn,
4210
    mg_socks_if_sock_set,    mg_socks_if_get_conn_addr,
4211
};
4212

    
4213
struct mg_iface *mg_socks_mk_iface(struct mg_mgr *mgr, const char *proxy_addr) {
4214
  struct mg_iface *iface = mg_if_create_iface(&mg_socks_iface_vtable, mgr);
4215
  iface->data = MG_CALLOC(1, sizeof(struct socksdata));
4216
  ((struct socksdata *) iface->data)->proxy_addr = strdup(proxy_addr);
4217
  return iface;
4218
}
4219

    
4220
#endif
4221
#ifdef MG_MODULE_LINES
4222
#line 1 "mongoose/src/mg_ssl_if_openssl.c"
4223
#endif
4224
/*
4225
 * Copyright (c) 2014-2016 Cesanta Software Limited
4226
 * All rights reserved
4227
 */
4228

    
4229
#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_OPENSSL
4230

    
4231
#ifdef __APPLE__
4232
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4233
#endif
4234

    
4235
#include <openssl/ssl.h>
4236

    
4237
struct mg_ssl_if_ctx {
4238
  SSL *ssl;
4239
  SSL_CTX *ssl_ctx;
4240
  struct mbuf psk;
4241
  size_t identity_len;
4242
};
4243

    
4244
void mg_ssl_if_init() {
4245
  SSL_library_init();
4246
}
4247

    
4248
enum mg_ssl_if_result mg_ssl_if_conn_accept(struct mg_connection *nc,
4249
                                            struct mg_connection *lc) {
4250
  struct mg_ssl_if_ctx *ctx =
4251
      (struct mg_ssl_if_ctx *) MG_CALLOC(1, sizeof(*ctx));
4252
  struct mg_ssl_if_ctx *lc_ctx = (struct mg_ssl_if_ctx *) lc->ssl_if_data;
4253
  nc->ssl_if_data = ctx;
4254
  if (ctx == NULL || lc_ctx == NULL) return MG_SSL_ERROR;
4255
  ctx->ssl_ctx = lc_ctx->ssl_ctx;
4256
  if ((ctx->ssl = SSL_new(ctx->ssl_ctx)) == NULL) {
4257
    return MG_SSL_ERROR;
4258
  }
4259
  return MG_SSL_OK;
4260
}
4261

    
4262
static enum mg_ssl_if_result mg_use_cert(SSL_CTX *ctx, const char *cert,
4263
                                         const char *key, const char **err_msg);
4264
static enum mg_ssl_if_result mg_use_ca_cert(SSL_CTX *ctx, const char *cert);
4265
static enum mg_ssl_if_result mg_set_cipher_list(SSL_CTX *ctx, const char *cl);
4266
static enum mg_ssl_if_result mg_ssl_if_ossl_set_psk(struct mg_ssl_if_ctx *ctx,
4267
                                                    const char *identity,
4268
                                                    const char *key_str);
4269

    
4270
enum mg_ssl_if_result mg_ssl_if_conn_init(
4271
    struct mg_connection *nc, const struct mg_ssl_if_conn_params *params,
4272
    const char **err_msg) {
4273
  struct mg_ssl_if_ctx *ctx =
4274
      (struct mg_ssl_if_ctx *) MG_CALLOC(1, sizeof(*ctx));
4275
  DBG(("%p %s,%s,%s", nc, (params->cert ? params->cert : ""),
4276
       (params->key ? params->key : ""),
4277
       (params->ca_cert ? params->ca_cert : "")));
4278
  if (ctx == NULL) {
4279
    MG_SET_PTRPTR(err_msg, "Out of memory");
4280
    return MG_SSL_ERROR;
4281
  }
4282
  nc->ssl_if_data = ctx;
4283
  if (nc->flags & MG_F_LISTENING) {
4284
    ctx->ssl_ctx = SSL_CTX_new(SSLv23_server_method());
4285
  } else {
4286
    ctx->ssl_ctx = SSL_CTX_new(SSLv23_client_method());
4287
  }
4288
  if (ctx->ssl_ctx == NULL) {
4289
    MG_SET_PTRPTR(err_msg, "Failed to create SSL context");
4290
    return MG_SSL_ERROR;
4291
  }
4292

    
4293
#ifndef KR_VERSION
4294
  /* Disable deprecated protocols. */
4295
  SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_SSLv2);
4296
  SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_SSLv3);
4297
  SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_TLSv1);
4298
#ifdef MG_SSL_OPENSSL_NO_COMPRESSION
4299
  SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_COMPRESSION);
4300
#endif
4301
#ifdef MG_SSL_OPENSSL_CIPHER_SERVER_PREFERENCE
4302
  SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
4303
#endif
4304
#else
4305
/* Krypton only supports TLSv1.2 anyway. */
4306
#endif
4307

    
4308
  if (params->cert != NULL &&
4309
      mg_use_cert(ctx->ssl_ctx, params->cert, params->key, err_msg) !=
4310
          MG_SSL_OK) {
4311
    return MG_SSL_ERROR;
4312
  }
4313

    
4314
  if (params->ca_cert != NULL &&
4315
      mg_use_ca_cert(ctx->ssl_ctx, params->ca_cert) != MG_SSL_OK) {
4316
    MG_SET_PTRPTR(err_msg, "Invalid SSL CA cert");
4317
    return MG_SSL_ERROR;
4318
  }
4319

    
4320
  if (params->server_name != NULL) {
4321
#ifdef KR_VERSION
4322
    SSL_CTX_kr_set_verify_name(ctx->ssl_ctx, params->server_name);
4323
#else
4324
/* TODO(rojer): Implement server name verification on OpenSSL. */
4325
#endif
4326
  }
4327

    
4328
  if (mg_set_cipher_list(ctx->ssl_ctx, params->cipher_suites) != MG_SSL_OK) {
4329
    MG_SET_PTRPTR(err_msg, "Invalid cipher suite list");
4330
    return MG_SSL_ERROR;
4331
  }
4332

    
4333
  mbuf_init(&ctx->psk, 0);
4334
  if (mg_ssl_if_ossl_set_psk(ctx, params->psk_identity, params->psk_key) !=
4335
      MG_SSL_OK) {
4336
    MG_SET_PTRPTR(err_msg, "Invalid PSK settings");
4337
    return MG_SSL_ERROR;
4338
  }
4339

    
4340
  if (!(nc->flags & MG_F_LISTENING) &&
4341
      (ctx->ssl = SSL_new(ctx->ssl_ctx)) == NULL) {
4342
    MG_SET_PTRPTR(err_msg, "Failed to create SSL session");
4343
    return MG_SSL_ERROR;
4344
  }
4345

    
4346
  nc->flags |= MG_F_SSL;
4347

    
4348
  return MG_SSL_OK;
4349
}
4350

    
4351
static enum mg_ssl_if_result mg_ssl_if_ssl_err(struct mg_connection *nc,
4352
                                               int res) {
4353
  struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
4354
  int err = SSL_get_error(ctx->ssl, res);
4355
  if (err == SSL_ERROR_WANT_READ) return MG_SSL_WANT_READ;
4356
  if (err == SSL_ERROR_WANT_WRITE) return MG_SSL_WANT_WRITE;
4357
  DBG(("%p %p SSL error: %d %d", nc, ctx->ssl_ctx, res, err));
4358
  nc->err = err;
4359
  return MG_SSL_ERROR;
4360
}
4361

    
4362
enum mg_ssl_if_result mg_ssl_if_handshake(struct mg_connection *nc) {
4363
  struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
4364
  int server_side = (nc->listener != NULL);
4365
  int res;
4366
  /* If descriptor is not yet set, do it now. */
4367
  if (SSL_get_fd(ctx->ssl) < 0) {
4368
    if (SSL_set_fd(ctx->ssl, nc->sock) != 1) return MG_SSL_ERROR;
4369
  }
4370
  res = server_side ? SSL_accept(ctx->ssl) : SSL_connect(ctx->ssl);
4371
  if (res != 1) return mg_ssl_if_ssl_err(nc, res);
4372
  return MG_SSL_OK;
4373
}
4374

    
4375
int mg_ssl_if_read(struct mg_connection *nc, void *buf, size_t buf_size) {
4376
  struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
4377
  int n = SSL_read(ctx->ssl, buf, buf_size);
4378
  DBG(("%p %d -> %d", nc, (int) buf_size, n));
4379
  if (n < 0) return mg_ssl_if_ssl_err(nc, n);
4380
  if (n == 0) nc->flags |= MG_F_CLOSE_IMMEDIATELY;
4381
  return n;
4382
}
4383

    
4384
int mg_ssl_if_write(struct mg_connection *nc, const void *data, size_t len) {
4385
  struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
4386
  int n = SSL_write(ctx->ssl, data, len);
4387
  DBG(("%p %d -> %d", nc, (int) len, n));
4388
  if (n <= 0) return mg_ssl_if_ssl_err(nc, n);
4389
  return n;
4390
}
4391

    
4392
void mg_ssl_if_conn_close_notify(struct mg_connection *nc) {
4393
  struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
4394
  if (ctx == NULL) return;
4395
  SSL_shutdown(ctx->ssl);
4396
}
4397

    
4398
void mg_ssl_if_conn_free(struct mg_connection *nc) {
4399
  struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
4400
  if (ctx == NULL) return;
4401
  nc->ssl_if_data = NULL;
4402
  if (ctx->ssl != NULL) SSL_free(ctx->ssl);
4403
  if (ctx->ssl_ctx != NULL && nc->listener == NULL) SSL_CTX_free(ctx->ssl_ctx);
4404
  mbuf_free(&ctx->psk);
4405
  memset(ctx, 0, sizeof(*ctx));
4406
  MG_FREE(ctx);
4407
}
4408

    
4409
/*
4410
 * Cipher suite options used for TLS negotiation.
4411
 * https://wiki.mozilla.org/Security/Server_Side_TLS#Recommended_configurations
4412
 */
4413
static const char mg_s_cipher_list[] =
4414
#if defined(MG_SSL_CRYPTO_MODERN)
4415
    "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:"
4416
    "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:"
4417
    "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:"
4418
    "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:"
4419
    "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:"
4420
    "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:"
4421
    "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:"
4422
    "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:"
4423
    "!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK"
4424
#elif defined(MG_SSL_CRYPTO_OLD)
4425
    "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:"
4426
    "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:"
4427
    "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:"
4428
    "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:"
4429
    "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:"
4430
    "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:"
4431
    "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:"
4432
    "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:"
4433
    "ECDHE-ECDSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:"
4434
    "AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:DES-CBC3-SHA:"
4435
    "HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:"
4436
    "!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA"
4437
#else /* Default - intermediate. */
4438
    "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:"
4439
    "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:"
4440
    "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:"
4441
    "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:"
4442
    "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:"
4443
    "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:"
4444
    "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:"
4445
    "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:"
4446
    "AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:"
4447
    "DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:"
4448
    "!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA"
4449
#endif
4450
    ;
4451

    
4452
/*
4453
 * Default DH params for PFS cipher negotiation. This is a 2048-bit group.
4454
 * Will be used if none are provided by the user in the certificate file.
4455
 */
4456
#if !MG_DISABLE_PFS && !defined(KR_VERSION)
4457
static const char mg_s_default_dh_params[] =
4458
    "\
4459
-----BEGIN DH PARAMETERS-----\n\
4460
MIIBCAKCAQEAlvbgD/qh9znWIlGFcV0zdltD7rq8FeShIqIhkQ0C7hYFThrBvF2E\n\
4461
Z9bmgaP+sfQwGpVlv9mtaWjvERbu6mEG7JTkgmVUJrUt/wiRzwTaCXBqZkdUO8Tq\n\
4462
+E6VOEQAilstG90ikN1Tfo+K6+X68XkRUIlgawBTKuvKVwBhuvlqTGerOtnXWnrt\n\
4463
ym//hd3cd5PBYGBix0i7oR4xdghvfR2WLVu0LgdThTBb6XP7gLd19cQ1JuBtAajZ\n\
4464
wMuPn7qlUkEFDIkAZy59/Hue/H2Q2vU/JsvVhHWCQBL4F1ofEAt50il6ZxR1QfFK\n\
4465
9VGKDC4oOgm9DlxwwBoC2FjqmvQlqVV3kwIBAg==\n\
4466
-----END DH PARAMETERS-----\n";
4467
#endif
4468

    
4469
static enum mg_ssl_if_result mg_use_ca_cert(SSL_CTX *ctx, const char *cert) {
4470
  if (cert == NULL || strcmp(cert, "*") == 0) {
4471
    return MG_SSL_OK;
4472
  }
4473
  SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
4474
  return SSL_CTX_load_verify_locations(ctx, cert, NULL) == 1 ? MG_SSL_OK
4475
                                                             : MG_SSL_ERROR;
4476
}
4477

    
4478
static enum mg_ssl_if_result mg_use_cert(SSL_CTX *ctx, const char *cert,
4479
                                         const char *key,
4480
                                         const char **err_msg) {
4481
  if (key == NULL) key = cert;
4482
  if (cert == NULL || cert[0] == '\0' || key == NULL || key[0] == '\0') {
4483
    return MG_SSL_OK;
4484
  } else if (SSL_CTX_use_certificate_file(ctx, cert, 1) == 0) {
4485
    MG_SET_PTRPTR(err_msg, "Invalid SSL cert");
4486
    return MG_SSL_ERROR;
4487
  } else if (SSL_CTX_use_PrivateKey_file(ctx, key, 1) == 0) {
4488
    MG_SET_PTRPTR(err_msg, "Invalid SSL key");
4489
    return MG_SSL_ERROR;
4490
  } else if (SSL_CTX_use_certificate_chain_file(ctx, cert) == 0) {
4491
    MG_SET_PTRPTR(err_msg, "Invalid CA bundle");
4492
    return MG_SSL_ERROR;
4493
  } else {
4494
    SSL_CTX_set_mode(ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
4495
#if !MG_DISABLE_PFS && !defined(KR_VERSION)
4496
    BIO *bio = NULL;
4497
    DH *dh = NULL;
4498

    
4499
    /* Try to read DH parameters from the cert/key file. */
4500
    bio = BIO_new_file(cert, "r");
4501
    if (bio != NULL) {
4502
      dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
4503
      BIO_free(bio);
4504
    }
4505
    /*
4506
     * If there are no DH params in the file, fall back to hard-coded ones.
4507
     * Not ideal, but better than nothing.
4508
     */
4509
    if (dh == NULL) {
4510
      bio = BIO_new_mem_buf((void *) mg_s_default_dh_params, -1);
4511
      dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
4512
      BIO_free(bio);
4513
    }
4514
    if (dh != NULL) {
4515
      SSL_CTX_set_tmp_dh(ctx, dh);
4516
      SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
4517
      DH_free(dh);
4518
    }
4519
#if OPENSSL_VERSION_NUMBER > 0x10002000L
4520
    SSL_CTX_set_ecdh_auto(ctx, 1);
4521
#endif
4522
#endif
4523
  }
4524
  return MG_SSL_OK;
4525
}
4526

    
4527
static enum mg_ssl_if_result mg_set_cipher_list(SSL_CTX *ctx, const char *cl) {
4528
  return (SSL_CTX_set_cipher_list(ctx, cl ? cl : mg_s_cipher_list) == 1
4529
              ? MG_SSL_OK
4530
              : MG_SSL_ERROR);
4531
}
4532

    
4533
#ifndef KR_VERSION
4534
static unsigned int mg_ssl_if_ossl_psk_cb(SSL *ssl, const char *hint,
4535
                                          char *identity,
4536
                                          unsigned int max_identity_len,
4537
                                          unsigned char *psk,
4538
                                          unsigned int max_psk_len) {
4539
  struct mg_ssl_if_ctx *ctx =
4540
      (struct mg_ssl_if_ctx *) SSL_CTX_get_app_data(SSL_get_SSL_CTX(ssl));
4541
  size_t key_len = ctx->psk.len - ctx->identity_len - 1;
4542
  DBG(("hint: '%s'", (hint ? hint : "")));
4543
  if (ctx->identity_len + 1 > max_identity_len) {
4544
    DBG(("identity too long"));
4545
    return 0;
4546
  }
4547
  if (key_len > max_psk_len) {
4548
    DBG(("key too long"));
4549
    return 0;
4550
  }
4551
  memcpy(identity, ctx->psk.buf, ctx->identity_len + 1);
4552
  memcpy(psk, ctx->psk.buf + ctx->identity_len + 1, key_len);
4553
  (void) ssl;
4554
  return key_len;
4555
}
4556

    
4557
static enum mg_ssl_if_result mg_ssl_if_ossl_set_psk(struct mg_ssl_if_ctx *ctx,
4558
                                                    const char *identity,
4559
                                                    const char *key_str) {
4560
  unsigned char key[32];
4561
  size_t key_len;
4562
  size_t i = 0;
4563
  if (identity == NULL && key_str == NULL) return MG_SSL_OK;
4564
  if (identity == NULL || key_str == NULL) return MG_SSL_ERROR;
4565
  key_len = strlen(key_str);
4566
  if (key_len != 32 && key_len != 64) return MG_SSL_ERROR;
4567
  memset(key, 0, sizeof(key));
4568
  key_len = 0;
4569
  for (i = 0; key_str[i] != '\0'; i++) {
4570
    unsigned char c;
4571
    char hc = tolower((int) key_str[i]);
4572
    if (hc >= '0' && hc <= '9') {
4573
      c = hc - '0';
4574
    } else if (hc >= 'a' && hc <= 'f') {
4575
      c = hc - 'a' + 0xa;
4576
    } else {
4577
      return MG_SSL_ERROR;
4578
    }
4579
    key_len = i / 2;
4580
    key[key_len] <<= 4;
4581
    key[key_len] |= c;
4582
  }
4583
  key_len++;
4584
  DBG(("identity = '%s', key = (%u)", identity, (unsigned int) key_len));
4585
  ctx->identity_len = strlen(identity);
4586
  mbuf_append(&ctx->psk, identity, ctx->identity_len + 1);
4587
  mbuf_append(&ctx->psk, key, key_len);
4588
  SSL_CTX_set_psk_client_callback(ctx->ssl_ctx, mg_ssl_if_ossl_psk_cb);
4589
  SSL_CTX_set_app_data(ctx->ssl_ctx, ctx);
4590
  return MG_SSL_OK;
4591
}
4592
#else
4593
static enum mg_ssl_if_result mg_ssl_if_ossl_set_psk(struct mg_ssl_if_ctx *ctx,
4594
                                                    const char *identity,
4595
                                                    const char *key_str) {
4596
  (void) ctx;
4597
  (void) identity;
4598
  (void) key_str;
4599
  /* Krypton does not support PSK. */
4600
  return MG_SSL_ERROR;
4601
}
4602
#endif /* defined(KR_VERSION) */
4603

    
4604
const char *mg_set_ssl(struct mg_connection *nc, const char *cert,
4605
                       const char *ca_cert) {
4606
  const char *err_msg = NULL;
4607
  struct mg_ssl_if_conn_params params;
4608
  memset(&params, 0, sizeof(params));
4609
  params.cert = cert;
4610
  params.ca_cert = ca_cert;
4611
  if (mg_ssl_if_conn_init(nc, &params, &err_msg) != MG_SSL_OK) {
4612
    return err_msg;
4613
  }
4614
  return NULL;
4615
}
4616

    
4617
#endif /* MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_OPENSSL */
4618
#ifdef MG_MODULE_LINES
4619
#line 1 "mongoose/src/mg_ssl_if_mbedtls.c"
4620
#endif
4621
/*
4622
 * Copyright (c) 2014-2016 Cesanta Software Limited
4623
 * All rights reserved
4624
 */
4625

    
4626
#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_MBEDTLS
4627

    
4628
#include <mbedtls/debug.h>
4629
#include <mbedtls/ecp.h>
4630
#include <mbedtls/platform.h>
4631
#include <mbedtls/ssl.h>
4632
#include <mbedtls/x509_crt.h>
4633

    
4634
static void mg_ssl_mbed_log(void *ctx, int level, const char *file, int line,
4635
                            const char *str) {
4636
  enum cs_log_level cs_level;
4637
  switch (level) {
4638
    case 1:
4639
      cs_level = LL_ERROR;
4640
      break;
4641
    case 2:
4642
    case 3:
4643
      cs_level = LL_DEBUG;
4644
      break;
4645
    default:
4646
      cs_level = LL_VERBOSE_DEBUG;
4647
  }
4648
  /* mbedTLS passes strings with \n at the end, strip it. */
4649
  LOG(cs_level, ("%p %.*s", ctx, (int) (strlen(str) - 1), str));
4650
  (void) file;
4651
  (void) line;
4652
}
4653

    
4654
struct mg_ssl_if_ctx {
4655
  mbedtls_ssl_config *conf;
4656
  mbedtls_ssl_context *ssl;
4657
  mbedtls_x509_crt *cert;
4658
  mbedtls_pk_context *key;
4659
  mbedtls_x509_crt *ca_cert;
4660
  struct mbuf cipher_suites;
4661
};
4662

    
4663
/* Must be provided by the platform. ctx is struct mg_connection. */
4664
extern int mg_ssl_if_mbed_random(void *ctx, unsigned char *buf, size_t len);
4665

    
4666
void mg_ssl_if_init() {
4667
}
4668

    
4669
enum mg_ssl_if_result mg_ssl_if_conn_accept(struct mg_connection *nc,
4670
                                            struct mg_connection *lc) {
4671
  struct mg_ssl_if_ctx *ctx =
4672
      (struct mg_ssl_if_ctx *) MG_CALLOC(1, sizeof(*ctx));
4673
  struct mg_ssl_if_ctx *lc_ctx = (struct mg_ssl_if_ctx *) lc->ssl_if_data;
4674
  nc->ssl_if_data = ctx;
4675
  if (ctx == NULL || lc_ctx == NULL) return MG_SSL_ERROR;
4676
  ctx->ssl = (mbedtls_ssl_context *) MG_CALLOC(1, sizeof(*ctx->ssl));
4677
  if (mbedtls_ssl_setup(ctx->ssl, lc_ctx->conf) != 0) {
4678
    return MG_SSL_ERROR;
4679
  }
4680
  return MG_SSL_OK;
4681
}
4682

    
4683
static enum mg_ssl_if_result mg_use_cert(struct mg_ssl_if_ctx *ctx,
4684
                                         const char *cert, const char *key,
4685
                                         const char **err_msg);
4686
static enum mg_ssl_if_result mg_use_ca_cert(struct mg_ssl_if_ctx *ctx,
4687
                                            const char *cert);
4688
static enum mg_ssl_if_result mg_set_cipher_list(struct mg_ssl_if_ctx *ctx,
4689
                                                const char *ciphers);
4690
static enum mg_ssl_if_result mg_ssl_if_mbed_set_psk(struct mg_ssl_if_ctx *ctx,
4691
                                                    const char *identity,
4692
                                                    const char *key);
4693

    
4694
enum mg_ssl_if_result mg_ssl_if_conn_init(
4695
    struct mg_connection *nc, const struct mg_ssl_if_conn_params *params,
4696
    const char **err_msg) {
4697
  struct mg_ssl_if_ctx *ctx =
4698
      (struct mg_ssl_if_ctx *) MG_CALLOC(1, sizeof(*ctx));
4699
  DBG(("%p %s,%s,%s", nc, (params->cert ? params->cert : ""),
4700
       (params->key ? params->key : ""),
4701
       (params->ca_cert ? params->ca_cert : "")));
4702

    
4703
  if (ctx == NULL) {
4704
    MG_SET_PTRPTR(err_msg, "Out of memory");
4705
    return MG_SSL_ERROR;
4706
  }
4707
  nc->ssl_if_data = ctx;
4708
  ctx->conf = (mbedtls_ssl_config *) MG_CALLOC(1, sizeof(*ctx->conf));
4709
  mbuf_init(&ctx->cipher_suites, 0);
4710
  mbedtls_ssl_config_init(ctx->conf);
4711
  mbedtls_ssl_conf_dbg(ctx->conf, mg_ssl_mbed_log, nc);
4712
  if (mbedtls_ssl_config_defaults(
4713
          ctx->conf, (nc->flags & MG_F_LISTENING ? MBEDTLS_SSL_IS_SERVER
4714
                                                 : MBEDTLS_SSL_IS_CLIENT),
4715
          MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT) != 0) {
4716
    MG_SET_PTRPTR(err_msg, "Failed to init SSL config");
4717
    return MG_SSL_ERROR;
4718
  }
4719

    
4720
  /* TLS 1.2 and up */
4721
  mbedtls_ssl_conf_min_version(ctx->conf, MBEDTLS_SSL_MAJOR_VERSION_3,
4722
                               MBEDTLS_SSL_MINOR_VERSION_3);
4723
  mbedtls_ssl_conf_rng(ctx->conf, mg_ssl_if_mbed_random, nc);
4724

    
4725
  if (params->cert != NULL &&
4726
      mg_use_cert(ctx, params->cert, params->key, err_msg) != MG_SSL_OK) {
4727
    return MG_SSL_ERROR;
4728
  }
4729

    
4730
  if (params->ca_cert != NULL &&
4731
      mg_use_ca_cert(ctx, params->ca_cert) != MG_SSL_OK) {
4732
    MG_SET_PTRPTR(err_msg, "Invalid SSL CA cert");
4733
    return MG_SSL_ERROR;
4734
  }
4735

    
4736
  if (mg_set_cipher_list(ctx, params->cipher_suites) != MG_SSL_OK) {
4737
    MG_SET_PTRPTR(err_msg, "Invalid cipher suite list");
4738
    return MG_SSL_ERROR;
4739
  }
4740

    
4741
  if (mg_ssl_if_mbed_set_psk(ctx, params->psk_identity, params->psk_key) !=
4742
      MG_SSL_OK) {
4743
    MG_SET_PTRPTR(err_msg, "Invalid PSK settings");
4744
    return MG_SSL_ERROR;
4745
  }
4746

    
4747
  if (!(nc->flags & MG_F_LISTENING)) {
4748
    ctx->ssl = (mbedtls_ssl_context *) MG_CALLOC(1, sizeof(*ctx->ssl));
4749
    mbedtls_ssl_init(ctx->ssl);
4750
    if (mbedtls_ssl_setup(ctx->ssl, ctx->conf) != 0) {
4751
      MG_SET_PTRPTR(err_msg, "Failed to create SSL session");
4752
      return MG_SSL_ERROR;
4753
    }
4754
    if (params->server_name != NULL &&
4755
        mbedtls_ssl_set_hostname(ctx->ssl, params->server_name) != 0) {
4756
      return MG_SSL_ERROR;
4757
    }
4758
  }
4759

    
4760
#ifdef MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN
4761
  if (mbedtls_ssl_conf_max_frag_len(ctx->conf,
4762
#if MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN == 512
4763
                                    MBEDTLS_SSL_MAX_FRAG_LEN_512
4764
#elif MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN == 1024
4765
                                    MBEDTLS_SSL_MAX_FRAG_LEN_1024
4766
#elif MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN == 2048
4767
                                    MBEDTLS_SSL_MAX_FRAG_LEN_2048
4768
#elif MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN == 4096
4769
                                    MBEDTLS_SSL_MAX_FRAG_LEN_4096
4770
#else
4771
#error Invalid MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN
4772
#endif
4773
                                    ) != 0) {
4774
    return MG_SSL_ERROR;
4775
  }
4776
#endif
4777

    
4778
  nc->flags |= MG_F_SSL;
4779

    
4780
  return MG_SSL_OK;
4781
}
4782

    
4783
#if MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL
4784
int ssl_socket_send(void *ctx, const unsigned char *buf, size_t len);
4785
int ssl_socket_recv(void *ctx, unsigned char *buf, size_t len);
4786
#else
4787
static int ssl_socket_send(void *ctx, const unsigned char *buf, size_t len) {
4788
  struct mg_connection *nc = (struct mg_connection *) ctx;
4789
  int n = (int) MG_SEND_FUNC(nc->sock, buf, len, 0);
4790
  LOG(LL_DEBUG, ("%p %d -> %d", nc, (int) len, n));
4791
  if (n >= 0) return n;
4792
  n = mg_get_errno();
4793
  return ((n == EAGAIN || n == EINPROGRESS) ? MBEDTLS_ERR_SSL_WANT_WRITE : -1);
4794
}
4795

    
4796
static int ssl_socket_recv(void *ctx, unsigned char *buf, size_t len) {
4797
  struct mg_connection *nc = (struct mg_connection *) ctx;
4798
  int n = (int) MG_RECV_FUNC(nc->sock, buf, len, 0);
4799
  LOG(LL_DEBUG, ("%p %d <- %d", nc, (int) len, n));
4800
  if (n >= 0) return n;
4801
  n = mg_get_errno();
4802
  return ((n == EAGAIN || n == EINPROGRESS) ? MBEDTLS_ERR_SSL_WANT_READ : -1);
4803
}
4804
#endif
4805

    
4806
static enum mg_ssl_if_result mg_ssl_if_mbed_err(struct mg_connection *nc,
4807
                                                int ret) {
4808
  if (ret == MBEDTLS_ERR_SSL_WANT_READ) return MG_SSL_WANT_READ;
4809
  if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) return MG_SSL_WANT_WRITE;
4810
  if (ret !=
4811
      MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) { /* CLOSE_NOTIFY = Normal shutdown */
4812
    LOG(LL_ERROR, ("%p SSL error: %d", nc, ret));
4813
  }
4814
  nc->err = ret;
4815
  nc->flags |= MG_F_CLOSE_IMMEDIATELY;
4816
  return MG_SSL_ERROR;
4817
}
4818

    
4819
static void mg_ssl_if_mbed_free_certs_and_keys(struct mg_ssl_if_ctx *ctx) {
4820
  if (ctx->cert != NULL) {
4821
    mbedtls_x509_crt_free(ctx->cert);
4822
    MG_FREE(ctx->cert);
4823
    ctx->cert = NULL;
4824
    mbedtls_pk_free(ctx->key);
4825
    MG_FREE(ctx->key);
4826
    ctx->key = NULL;
4827
  }
4828
  if (ctx->ca_cert != NULL) {
4829
    mbedtls_ssl_conf_ca_chain(ctx->conf, NULL, NULL);
4830
#ifdef MBEDTLS_X509_CA_CHAIN_ON_DISK
4831
    if (ctx->ca_cert->ca_chain_file != NULL) {
4832
      MG_FREE((void *) ctx->ca_cert->ca_chain_file);
4833
      ctx->ca_cert->ca_chain_file = NULL;
4834
    }
4835
#endif
4836
    mbedtls_x509_crt_free(ctx->ca_cert);
4837
    MG_FREE(ctx->ca_cert);
4838
    ctx->ca_cert = NULL;
4839
  }
4840
}
4841

    
4842
enum mg_ssl_if_result mg_ssl_if_handshake(struct mg_connection *nc) {
4843
  struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
4844
  int err;
4845
  /* If bio is not yet set, do it now. */
4846
  if (ctx->ssl->p_bio == NULL) {
4847
    mbedtls_ssl_set_bio(ctx->ssl, nc, ssl_socket_send, ssl_socket_recv, NULL);
4848
  }
4849
  err = mbedtls_ssl_handshake(ctx->ssl);
4850
  if (err != 0) return mg_ssl_if_mbed_err(nc, err);
4851
#ifdef MG_SSL_IF_MBEDTLS_FREE_CERTS
4852
  /*
4853
   * Free the peer certificate, we don't need it after handshake.
4854
   * Note that this effectively disables renegotiation.
4855
   */
4856
  mbedtls_x509_crt_free(ctx->ssl->session->peer_cert);
4857
  mbedtls_free(ctx->ssl->session->peer_cert);
4858
  ctx->ssl->session->peer_cert = NULL;
4859
  /* On a client connection we can also free our own and CA certs. */
4860
  if (nc->listener == NULL) {
4861
    if (ctx->conf->key_cert != NULL) {
4862
      /* Note that this assumes one key_cert entry, which matches our init. */
4863
      MG_FREE(ctx->conf->key_cert);
4864
      ctx->conf->key_cert = NULL;
4865
    }
4866
    mbedtls_ssl_conf_ca_chain(ctx->conf, NULL, NULL);
4867
    mg_ssl_if_mbed_free_certs_and_keys(ctx);
4868
  }
4869
#endif
4870
  return MG_SSL_OK;
4871
}
4872

    
4873
int mg_ssl_if_read(struct mg_connection *nc, void *buf, size_t buf_size) {
4874
  struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
4875
  int n = mbedtls_ssl_read(ctx->ssl, (unsigned char *) buf, buf_size);
4876
  DBG(("%p %d -> %d", nc, (int) buf_size, n));
4877
  if (n < 0) return mg_ssl_if_mbed_err(nc, n);
4878
  if (n == 0) nc->flags |= MG_F_CLOSE_IMMEDIATELY;
4879
  return n;
4880
}
4881

    
4882
int mg_ssl_if_write(struct mg_connection *nc, const void *data, size_t len) {
4883
  struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
4884
  int n = mbedtls_ssl_write(ctx->ssl, (const unsigned char *) data, len);
4885
  DBG(("%p %d -> %d", nc, (int) len, n));
4886
  if (n < 0) return mg_ssl_if_mbed_err(nc, n);
4887
  return n;
4888
}
4889

    
4890
void mg_ssl_if_conn_close_notify(struct mg_connection *nc) {
4891
  struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
4892
  if (ctx == NULL) return;
4893
  mbedtls_ssl_close_notify(ctx->ssl);
4894
}
4895

    
4896
void mg_ssl_if_conn_free(struct mg_connection *nc) {
4897
  struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
4898
  if (ctx == NULL) return;
4899
  nc->ssl_if_data = NULL;
4900
  if (ctx->ssl != NULL) {
4901
    mbedtls_ssl_free(ctx->ssl);
4902
    MG_FREE(ctx->ssl);
4903
  }
4904
  mg_ssl_if_mbed_free_certs_and_keys(ctx);
4905
  if (ctx->conf != NULL) {
4906
    mbedtls_ssl_config_free(ctx->conf);
4907
    MG_FREE(ctx->conf);
4908
  }
4909
  mbuf_free(&ctx->cipher_suites);
4910
  memset(ctx, 0, sizeof(*ctx));
4911
  MG_FREE(ctx);
4912
}
4913

    
4914
static enum mg_ssl_if_result mg_use_ca_cert(struct mg_ssl_if_ctx *ctx,
4915
                                            const char *ca_cert) {
4916
  if (ca_cert == NULL || strcmp(ca_cert, "*") == 0) {
4917
    mbedtls_ssl_conf_authmode(ctx->conf, MBEDTLS_SSL_VERIFY_NONE);
4918
    return MG_SSL_OK;
4919
  }
4920
  ctx->ca_cert = (mbedtls_x509_crt *) MG_CALLOC(1, sizeof(*ctx->ca_cert));
4921
  mbedtls_x509_crt_init(ctx->ca_cert);
4922
#ifdef MBEDTLS_X509_CA_CHAIN_ON_DISK
4923
  ca_cert = strdup(ca_cert);
4924
  if (mbedtls_x509_crt_set_ca_chain_file(ctx->ca_cert, ca_cert) != 0) {
4925
    return MG_SSL_ERROR;
4926
  }
4927
#else
4928
  if (mbedtls_x509_crt_parse_file(ctx->ca_cert, ca_cert) != 0) {
4929
    return MG_SSL_ERROR;
4930
  }
4931
#endif
4932
  mbedtls_ssl_conf_ca_chain(ctx->conf, ctx->ca_cert, NULL);
4933
  mbedtls_ssl_conf_authmode(ctx->conf, MBEDTLS_SSL_VERIFY_REQUIRED);
4934
  return MG_SSL_OK;
4935
}
4936

    
4937
static enum mg_ssl_if_result mg_use_cert(struct mg_ssl_if_ctx *ctx,
4938
                                         const char *cert, const char *key,
4939
                                         const char **err_msg) {
4940
  if (key == NULL) key = cert;
4941
  if (cert == NULL || cert[0] == '\0' || key == NULL || key[0] == '\0') {
4942
    return MG_SSL_OK;
4943
  }
4944
  ctx->cert = (mbedtls_x509_crt *) MG_CALLOC(1, sizeof(*ctx->cert));
4945
  mbedtls_x509_crt_init(ctx->cert);
4946
  ctx->key = (mbedtls_pk_context *) MG_CALLOC(1, sizeof(*ctx->key));
4947
  mbedtls_pk_init(ctx->key);
4948
  if (mbedtls_x509_crt_parse_file(ctx->cert, cert) != 0) {
4949
    MG_SET_PTRPTR(err_msg, "Invalid SSL cert");
4950
    return MG_SSL_ERROR;
4951
  }
4952
  if (mbedtls_pk_parse_keyfile(ctx->key, key, NULL) != 0) {
4953
    MG_SET_PTRPTR(err_msg, "Invalid SSL key");
4954
    return MG_SSL_ERROR;
4955
  }
4956
  if (mbedtls_ssl_conf_own_cert(ctx->conf, ctx->cert, ctx->key) != 0) {
4957
    MG_SET_PTRPTR(err_msg, "Invalid SSL key or cert");
4958
    return MG_SSL_ERROR;
4959
  }
4960
  return MG_SSL_OK;
4961
}
4962

    
4963
static const int mg_s_cipher_list[] = {
4964
#if CS_PLATFORM != CS_P_ESP8266
4965
    MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4966
    MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
4967
    MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
4968
    MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
4969
    MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
4970
    MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,
4971
    MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
4972
    MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
4973
    MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
4974
    MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
4975
    MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
4976
    MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
4977
    MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256,
4978
    MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256,
4979
    MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA,
4980
#else
4981
    /*
4982
     * ECDHE is way too slow on ESP8266 w/o cryptochip, this sometimes results
4983
     * in WiFi STA deauths. Use weaker but faster cipher suites. Sad but true.
4984
     * Disable DHE completely because it's just hopelessly slow.
4985
     */
4986
    MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256,
4987
    MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256,
4988
    MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4989
    MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
4990
    MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
4991
    MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
4992
    MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
4993
    MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
4994
    MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
4995
    MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
4996
    MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
4997
    MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
4998
    MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA,
4999
#endif /* CS_PLATFORM != CS_P_ESP8266 */
5000
    0,
5001
};
5002

    
5003
/*
5004
 * Ciphers can be specified as a colon-separated list of cipher suite names.
5005
 * These can be found in
5006
 * https://github.com/ARMmbed/mbedtls/blob/development/library/ssl_ciphersuites.c#L267
5007
 * E.g.: TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-256-CCM
5008
 */
5009
static enum mg_ssl_if_result mg_set_cipher_list(struct mg_ssl_if_ctx *ctx,
5010
                                                const char *ciphers) {
5011
  if (ciphers != NULL) {
5012
    int l, id;
5013
    const char *s = ciphers, *e;
5014
    char tmp[50];
5015
    while (s != NULL) {
5016
      e = strchr(s, ':');
5017
      l = (e != NULL ? (e - s) : (int) strlen(s));
5018
      strncpy(tmp, s, l);
5019
      tmp[l] = '\0';
5020
      id = mbedtls_ssl_get_ciphersuite_id(tmp);
5021
      DBG(("%s -> %04x", tmp, id));
5022
      if (id != 0) {
5023
        mbuf_append(&ctx->cipher_suites, &id, sizeof(id));
5024
      }
5025
      s = (e != NULL ? e + 1 : NULL);
5026
    }
5027
    if (ctx->cipher_suites.len == 0) return MG_SSL_ERROR;
5028
    id = 0;
5029
    mbuf_append(&ctx->cipher_suites, &id, sizeof(id));
5030
    mbuf_trim(&ctx->cipher_suites);
5031
    mbedtls_ssl_conf_ciphersuites(ctx->conf,
5032
                                  (const int *) ctx->cipher_suites.buf);
5033
  } else {
5034
    mbedtls_ssl_conf_ciphersuites(ctx->conf, mg_s_cipher_list);
5035
  }
5036
  return MG_SSL_OK;
5037
}
5038

    
5039
static enum mg_ssl_if_result mg_ssl_if_mbed_set_psk(struct mg_ssl_if_ctx *ctx,
5040
                                                    const char *identity,
5041
                                                    const char *key_str) {
5042
  unsigned char key[32];
5043
  size_t key_len;
5044
  if (identity == NULL && key_str == NULL) return MG_SSL_OK;
5045
  if (identity == NULL || key_str == NULL) return MG_SSL_ERROR;
5046
  key_len = strlen(key_str);
5047
  if (key_len != 32 && key_len != 64) return MG_SSL_ERROR;
5048
  size_t i = 0;
5049
  memset(key, 0, sizeof(key));
5050
  key_len = 0;
5051
  for (i = 0; key_str[i] != '\0'; i++) {
5052
    unsigned char c;
5053
    char hc = tolower((int) key_str[i]);
5054
    if (hc >= '0' && hc <= '9') {
5055
      c = hc - '0';
5056
    } else if (hc >= 'a' && hc <= 'f') {
5057
      c = hc - 'a' + 0xa;
5058
    } else {
5059
      return MG_SSL_ERROR;
5060
    }
5061
    key_len = i / 2;
5062
    key[key_len] <<= 4;
5063
    key[key_len] |= c;
5064
  }
5065
  key_len++;
5066
  DBG(("identity = '%s', key = (%u)", identity, (unsigned int) key_len));
5067
  /* mbedTLS makes copies of psk and identity. */
5068
  if (mbedtls_ssl_conf_psk(ctx->conf, (const unsigned char *) key, key_len,
5069
                           (const unsigned char *) identity,
5070
                           strlen(identity)) != 0) {
5071
    return MG_SSL_ERROR;
5072
  }
5073
  return MG_SSL_OK;
5074
}
5075

    
5076
const char *mg_set_ssl(struct mg_connection *nc, const char *cert,
5077
                       const char *ca_cert) {
5078
  const char *err_msg = NULL;
5079
  struct mg_ssl_if_conn_params params;
5080
  memset(&params, 0, sizeof(params));
5081
  params.cert = cert;
5082
  params.ca_cert = ca_cert;
5083
  if (mg_ssl_if_conn_init(nc, &params, &err_msg) != MG_SSL_OK) {
5084
    return err_msg;
5085
  }
5086
  return NULL;
5087
}
5088

    
5089
/* Lazy RNG. Warning: it would be a bad idea to do this in production! */
5090
#ifdef MG_SSL_MBED_DUMMY_RANDOM
5091
int mg_ssl_if_mbed_random(void *ctx, unsigned char *buf, size_t len) {
5092
  (void) ctx;
5093
  while (len--) *buf++ = rand();
5094
  return 0;
5095
}
5096
#endif
5097

    
5098
#endif /* MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_MBEDTLS */
5099
#ifdef MG_MODULE_LINES
5100
#line 1 "mongoose/src/mg_uri.c"
5101
#endif
5102
/*
5103
 * Copyright (c) 2014 Cesanta Software Limited
5104
 * All rights reserved
5105
 */
5106

    
5107
/* Amalgamated: #include "mg_internal.h" */
5108
/* Amalgamated: #include "mg_uri.h" */
5109

    
5110
/*
5111
 * scan string until encountering one of `seps`, keeping track of component
5112
 * boundaries in `res`.
5113
 *
5114
 * `p` will point to the char after the separator or it will be `end`.
5115
 */
5116
static void parse_uri_component(const char **p, const char *end,
5117
                                const char *seps, struct mg_str *res) {
5118
  const char *q;
5119
  res->p = *p;
5120
  for (; *p < end; (*p)++) {
5121
    for (q = seps; *q != '\0'; q++) {
5122
      if (**p == *q) break;
5123
    }
5124
    if (*q != '\0') break;
5125
  }
5126
  res->len = (*p) - res->p;
5127
  if (*p < end) (*p)++;
5128
}
5129

    
5130
int mg_parse_uri(const struct mg_str uri, struct mg_str *scheme,
5131
                 struct mg_str *user_info, struct mg_str *host,
5132
                 unsigned int *port, struct mg_str *path, struct mg_str *query,
5133
                 struct mg_str *fragment) {
5134
  struct mg_str rscheme = {0, 0}, ruser_info = {0, 0}, rhost = {0, 0},
5135
                rpath = {0, 0}, rquery = {0, 0}, rfragment = {0, 0};
5136
  unsigned int rport = 0;
5137
  enum {
5138
    P_START,
5139
    P_SCHEME_OR_PORT,
5140
    P_USER_INFO,
5141
    P_HOST,
5142
    P_PORT,
5143
    P_REST
5144
  } state = P_START;
5145

    
5146
  const char *p = uri.p, *end = p + uri.len;
5147
  while (p < end) {
5148
    switch (state) {
5149
      case P_START:
5150
        /*
5151
         * expecting on of:
5152
         * - `scheme://xxxx`
5153
         * - `xxxx:port`
5154
         * - `[a:b:c]:port`
5155
         * - `xxxx/path`
5156
         */
5157
        if (*p == '[') {
5158
          state = P_HOST;
5159
          break;
5160
        }
5161
        for (; p < end; p++) {
5162
          if (*p == ':') {
5163
            state = P_SCHEME_OR_PORT;
5164
            break;
5165
          } else if (*p == '/') {
5166
            state = P_REST;
5167
            break;
5168
          }
5169
        }
5170
        if (state == P_START || state == P_REST) {
5171
          rhost.p = uri.p;
5172
          rhost.len = p - uri.p;
5173
        }
5174
        break;
5175
      case P_SCHEME_OR_PORT:
5176
        if (end - p >= 3 && strncmp(p, "://", 3) == 0) {
5177
          rscheme.p = uri.p;
5178
          rscheme.len = p - uri.p;
5179
          state = P_USER_INFO;
5180
          p += 3;
5181
        } else {
5182
          rhost.p = uri.p;
5183
          rhost.len = p - uri.p;
5184
          state = P_PORT;
5185
        }
5186
        break;
5187
      case P_USER_INFO:
5188
        ruser_info.p = p;
5189
        for (; p < end; p++) {
5190
          if (*p == '@' || *p == '[' || *p == '/') {
5191
            break;
5192
          }
5193
        }
5194
        if (p == end || *p == '/' || *p == '[') {
5195
          /* backtrack and parse as host */
5196
          p = ruser_info.p;
5197
        }
5198
        ruser_info.len = p - ruser_info.p;
5199
        state = P_HOST;
5200
        break;
5201
      case P_HOST:
5202
        if (*p == '@') p++;
5203
        rhost.p = p;
5204
        if (*p == '[') {
5205
          int found = 0;
5206
          for (; !found && p < end; p++) {
5207
            found = (*p == ']');
5208
          }
5209
          if (!found) return -1;
5210
        } else {
5211
          for (; p < end; p++) {
5212
            if (*p == ':' || *p == '/') break;
5213
          }
5214
        }
5215
        rhost.len = p - rhost.p;
5216
        if (p < end) {
5217
          if (*p == ':') {
5218
            state = P_PORT;
5219
            break;
5220
          } else if (*p == '/') {
5221
            state = P_REST;
5222
            break;
5223
          }
5224
        }
5225
        break;
5226
      case P_PORT:
5227
        p++;
5228
        for (; p < end; p++) {
5229
          if (*p == '/') {
5230
            state = P_REST;
5231
            break;
5232
          }
5233
          rport *= 10;
5234
          rport += *p - '0';
5235
        }
5236
        break;
5237
      case P_REST:
5238
        /* `p` points to separator. `path` includes the separator */
5239
        parse_uri_component(&p, end, "?#", &rpath);
5240
        if (p < end && *(p - 1) == '?') {
5241
          parse_uri_component(&p, end, "#", &rquery);
5242
        }
5243
        parse_uri_component(&p, end, "", &rfragment);
5244
        break;
5245
    }
5246
  }
5247

    
5248
  if (scheme != 0) *scheme = rscheme;
5249
  if (user_info != 0) *user_info = ruser_info;
5250
  if (host != 0) *host = rhost;
5251
  if (port != 0) *port = rport;
5252
  if (path != 0) *path = rpath;
5253
  if (query != 0) *query = rquery;
5254
  if (fragment != 0) *fragment = rfragment;
5255

    
5256
  return 0;
5257
}
5258

    
5259
/* Normalize the URI path. Remove/resolve "." and "..". */
5260
int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out) {
5261
  const char *s = in->p, *se = s + in->len;
5262
  char *cp = (char *) out->p, *d;
5263

    
5264
  if (in->len == 0 || *s != '/') {
5265
    out->len = 0;
5266
    return 0;
5267
  }
5268

    
5269
  d = cp;
5270

    
5271
  while (s < se) {
5272
    const char *next = s;
5273
    struct mg_str component;
5274
    parse_uri_component(&next, se, "/", &component);
5275
    if (mg_vcmp(&component, ".") == 0) {
5276
      /* Yum. */
5277
    } else if (mg_vcmp(&component, "..") == 0) {
5278
      /* Backtrack to previous slash. */
5279
      if (d > cp + 1 && *(d - 1) == '/') d--;
5280
      while (d > cp && *(d - 1) != '/') d--;
5281
    } else {
5282
      memmove(d, s, next - s);
5283
      d += next - s;
5284
    }
5285
    s = next;
5286
  }
5287
  if (d == cp) *d++ = '/';
5288

    
5289
  out->p = cp;
5290
  out->len = d - cp;
5291
  return 1;
5292
}
5293

    
5294
int mg_assemble_uri(const struct mg_str *scheme, const struct mg_str *user_info,
5295
                    const struct mg_str *host, unsigned int port,
5296
                    const struct mg_str *path, const struct mg_str *query,
5297
                    const struct mg_str *fragment, int normalize_path,
5298
                    struct mg_str *uri) {
5299
  int result = -1;
5300
  struct mbuf out;
5301
  mbuf_init(&out, 0);
5302

    
5303
  if (scheme != NULL && scheme->len > 0) {
5304
    mbuf_append(&out, scheme->p, scheme->len);
5305
    mbuf_append(&out, "://", 3);
5306
  }
5307

    
5308
  if (user_info != NULL && user_info->len > 0) {
5309
    mbuf_append(&out, user_info->p, user_info->len);
5310
    mbuf_append(&out, "@", 1);
5311
  }
5312

    
5313
  if (host != NULL && host->len > 0) {
5314
    mbuf_append(&out, host->p, host->len);
5315
  }
5316

    
5317
  if (port != 0) {
5318
    char port_str[20];
5319
    int port_str_len = sprintf(port_str, ":%u", port);
5320
    mbuf_append(&out, port_str, port_str_len);
5321
  }
5322

    
5323
  if (path != NULL && path->len > 0) {
5324
    if (normalize_path) {
5325
      struct mg_str npath = mg_strdup(*path);
5326
      if (npath.len != path->len) goto out;
5327
      if (!mg_normalize_uri_path(path, &npath)) {
5328
        free((void *) npath.p);
5329
        goto out;
5330
      }
5331
      mbuf_append(&out, npath.p, npath.len);
5332
      free((void *) npath.p);
5333
    } else {
5334
      mbuf_append(&out, path->p, path->len);
5335
    }
5336
  } else if (normalize_path) {
5337
    mbuf_append(&out, "/", 1);
5338
  }
5339

    
5340
  if (query != NULL && query->len > 0) {
5341
    mbuf_append(&out, "?", 1);
5342
    mbuf_append(&out, query->p, query->len);
5343
  }
5344

    
5345
  if (fragment != NULL && fragment->len > 0) {
5346
    mbuf_append(&out, "#", 1);
5347
    mbuf_append(&out, fragment->p, fragment->len);
5348
  }
5349

    
5350
  result = 0;
5351

    
5352
out:
5353
  if (result == 0) {
5354
    uri->p = out.buf;
5355
    uri->len = out.len;
5356
  } else {
5357
    mbuf_free(&out);
5358
    uri->p = NULL;
5359
    uri->len = 0;
5360
  }
5361
  return result;
5362
}
5363
#ifdef MG_MODULE_LINES
5364
#line 1 "mongoose/src/mg_http.c"
5365
#endif
5366
/*
5367
 * Copyright (c) 2014 Cesanta Software Limited
5368
 * All rights reserved
5369
 */
5370

    
5371
#if MG_ENABLE_HTTP
5372

    
5373
/* Amalgamated: #include "common/cs_md5.h" */
5374
/* Amalgamated: #include "mg_internal.h" */
5375
/* Amalgamated: #include "mg_util.h" */
5376

    
5377
/* altbuf {{{ */
5378

    
5379
/*
5380
 * Alternate buffer: fills the client-provided buffer with data; and if it's
5381
 * not large enough, allocates another buffer (via mbuf), similar to asprintf.
5382
 */
5383
struct altbuf {
5384
  struct mbuf m;
5385
  char *user_buf;
5386
  size_t len;
5387
  size_t user_buf_size;
5388
};
5389

    
5390
/*
5391
 * Initializes altbuf; `buf`, `buf_size` is the client-provided buffer.
5392
 */
5393
MG_INTERNAL void altbuf_init(struct altbuf *ab, char *buf, size_t buf_size) {
5394
  mbuf_init(&ab->m, 0);
5395
  ab->user_buf = buf;
5396
  ab->user_buf_size = buf_size;
5397
  ab->len = 0;
5398
}
5399

    
5400
/*
5401
 * Appends a single char to the altbuf.
5402
 */
5403
MG_INTERNAL void altbuf_append(struct altbuf *ab, char c) {
5404
  if (ab->len < ab->user_buf_size) {
5405
    /* The data fits into the original buffer */
5406
    ab->user_buf[ab->len++] = c;
5407
  } else {
5408
    /* The data can't fit into the original buffer, so write it to mbuf.  */
5409

    
5410
    /*
5411
     * First of all, see if that's the first byte which overflows the original
5412
     * buffer: if so, copy the existing data from there to a newly allocated
5413
     * mbuf.
5414
     */
5415
    if (ab->len > 0 && ab->m.len == 0) {
5416
      mbuf_append(&ab->m, ab->user_buf, ab->len);
5417
    }
5418

    
5419
    mbuf_append(&ab->m, &c, 1);
5420
    ab->len = ab->m.len;
5421
  }
5422
}
5423

    
5424
/*
5425
 * Resets any data previously appended to altbuf.
5426
 */
5427
MG_INTERNAL void altbuf_reset(struct altbuf *ab) {
5428
  mbuf_free(&ab->m);
5429
  ab->len = 0;
5430
}
5431

    
5432
/*
5433
 * Returns whether the additional buffer was allocated (and thus the data
5434
 * is in the mbuf, not the client-provided buffer)
5435
 */
5436
MG_INTERNAL int altbuf_reallocated(struct altbuf *ab) {
5437
  return ab->len > ab->user_buf_size;
5438
}
5439

    
5440
/*
5441
 * Returns the actual buffer with data, either the client-provided or a newly
5442
 * allocated one. If `trim` is non-zero, mbuf-backed buffer is trimmed first.
5443
 */
5444
MG_INTERNAL char *altbuf_get_buf(struct altbuf *ab, int trim) {
5445
  if (altbuf_reallocated(ab)) {
5446
    if (trim) {
5447
      mbuf_trim(&ab->m);
5448
    }
5449
    return ab->m.buf;
5450
  } else {
5451
    return ab->user_buf;
5452
  }
5453
}
5454

    
5455
/* }}} */
5456

    
5457
static const char *mg_version_header = "Mongoose/" MG_VERSION;
5458

    
5459
enum mg_http_proto_data_type { DATA_NONE, DATA_FILE, DATA_PUT };
5460

    
5461
struct mg_http_proto_data_file {
5462
  FILE *fp;      /* Opened file. */
5463
  int64_t cl;    /* Content-Length. How many bytes to send. */
5464
  int64_t sent;  /* How many bytes have been already sent. */
5465
  int keepalive; /* Keep connection open after sending. */
5466
  enum mg_http_proto_data_type type;
5467
};
5468

    
5469
#if MG_ENABLE_HTTP_CGI
5470
struct mg_http_proto_data_cgi {
5471
  struct mg_connection *cgi_nc;
5472
};
5473
#endif
5474

    
5475
struct mg_http_proto_data_chuncked {
5476
  int64_t body_len; /* How many bytes of chunked body was reassembled. */
5477
};
5478

    
5479
struct mg_http_endpoint {
5480
  struct mg_http_endpoint *next;
5481
  struct mg_str uri_pattern; /* owned */
5482
  char *auth_domain;         /* owned */
5483
  char *auth_file;           /* owned */
5484

    
5485
  mg_event_handler_t handler;
5486
#if MG_ENABLE_CALLBACK_USERDATA
5487
  void *user_data;
5488
#endif
5489
};
5490

    
5491
enum mg_http_multipart_stream_state {
5492
  MPS_BEGIN,
5493
  MPS_WAITING_FOR_BOUNDARY,
5494
  MPS_WAITING_FOR_CHUNK,
5495
  MPS_GOT_CHUNK,
5496
  MPS_GOT_BOUNDARY,
5497
  MPS_FINALIZE,
5498
  MPS_FINISHED
5499
};
5500

    
5501
struct mg_http_multipart_stream {
5502
  const char *boundary;
5503
  int boundary_len;
5504
  const char *var_name;
5505
  const char *file_name;
5506
  void *user_data;
5507
  int prev_io_len;
5508
  enum mg_http_multipart_stream_state state;
5509
  int processing_part;
5510
};
5511

    
5512
struct mg_reverse_proxy_data {
5513
  struct mg_connection *linked_conn;
5514
};
5515

    
5516
struct mg_ws_proto_data {
5517
  /*
5518
   * Defragmented size of the frame so far.
5519
   *
5520
   * First byte of nc->recv_mbuf.buf is an op, the rest of the data is
5521
   * defragmented data.
5522
   */
5523
  size_t reass_len;
5524
};
5525

    
5526
struct mg_http_proto_data {
5527
#if MG_ENABLE_FILESYSTEM
5528
  struct mg_http_proto_data_file file;
5529
#endif
5530
#if MG_ENABLE_HTTP_CGI
5531
  struct mg_http_proto_data_cgi cgi;
5532
#endif
5533
#if MG_ENABLE_HTTP_STREAMING_MULTIPART
5534
  struct mg_http_multipart_stream mp_stream;
5535
#endif
5536
#if MG_ENABLE_HTTP_WEBSOCKET
5537
  struct mg_ws_proto_data ws_data;
5538
#endif
5539
  struct mg_http_proto_data_chuncked chunk;
5540
  struct mg_http_endpoint *endpoints;
5541
  mg_event_handler_t endpoint_handler;
5542
  struct mg_reverse_proxy_data reverse_proxy_data;
5543
  size_t rcvd; /* How many bytes we have received. */
5544
};
5545

    
5546
static void mg_http_conn_destructor(void *proto_data);
5547
struct mg_connection *mg_connect_http_base(
5548
    struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data),
5549
    struct mg_connect_opts opts, const char *scheme1, const char *scheme2,
5550
    const char *scheme_ssl1, const char *scheme_ssl2, const char *url,
5551
    struct mg_str *path, struct mg_str *user_info, struct mg_str *host);
5552

    
5553
static struct mg_http_proto_data *mg_http_get_proto_data(
5554
    struct mg_connection *c) {
5555
  if (c->proto_data == NULL) {
5556
    c->proto_data = MG_CALLOC(1, sizeof(struct mg_http_proto_data));
5557
    c->proto_data_destructor = mg_http_conn_destructor;
5558
  }
5559

    
5560
  return (struct mg_http_proto_data *) c->proto_data;
5561
}
5562

    
5563
#if MG_ENABLE_HTTP_STREAMING_MULTIPART
5564
static void mg_http_free_proto_data_mp_stream(
5565
    struct mg_http_multipart_stream *mp) {
5566
  MG_FREE((void *) mp->boundary);
5567
  MG_FREE((void *) mp->var_name);
5568
  MG_FREE((void *) mp->file_name);
5569
  memset(mp, 0, sizeof(*mp));
5570
}
5571
#endif
5572

    
5573
#if MG_ENABLE_FILESYSTEM
5574
static void mg_http_free_proto_data_file(struct mg_http_proto_data_file *d) {
5575
  if (d != NULL) {
5576
    if (d->fp != NULL) {
5577
      fclose(d->fp);
5578
    }
5579
    memset(d, 0, sizeof(struct mg_http_proto_data_file));
5580
  }
5581
}
5582
#endif
5583

    
5584
static void mg_http_free_proto_data_endpoints(struct mg_http_endpoint **ep) {
5585
  struct mg_http_endpoint *current = *ep;
5586

    
5587
  while (current != NULL) {
5588
    struct mg_http_endpoint *tmp = current->next;
5589
    MG_FREE((void *) current->uri_pattern.p);
5590
    MG_FREE((void *) current->auth_domain);
5591
    MG_FREE((void *) current->auth_file);
5592
    MG_FREE(current);
5593
    current = tmp;
5594
  }
5595

    
5596
  ep = NULL;
5597
}
5598

    
5599
static void mg_http_free_reverse_proxy_data(struct mg_reverse_proxy_data *rpd) {
5600
  if (rpd->linked_conn != NULL) {
5601
    /*
5602
     * Connection has linked one, we have to unlink & close it
5603
     * since _this_ connection is going to die and
5604
     * it doesn't make sense to keep another one
5605
     */
5606
    struct mg_http_proto_data *pd = mg_http_get_proto_data(rpd->linked_conn);
5607
    if (pd->reverse_proxy_data.linked_conn != NULL) {
5608
      pd->reverse_proxy_data.linked_conn->flags |= MG_F_SEND_AND_CLOSE;
5609
      pd->reverse_proxy_data.linked_conn = NULL;
5610
    }
5611
    rpd->linked_conn = NULL;
5612
  }
5613
}
5614

    
5615
static void mg_http_conn_destructor(void *proto_data) {
5616
  struct mg_http_proto_data *pd = (struct mg_http_proto_data *) proto_data;
5617
#if MG_ENABLE_FILESYSTEM
5618
  mg_http_free_proto_data_file(&pd->file);
5619
#endif
5620
#if MG_ENABLE_HTTP_CGI
5621
  mg_http_free_proto_data_cgi(&pd->cgi);
5622
#endif
5623
#if MG_ENABLE_HTTP_STREAMING_MULTIPART
5624
  mg_http_free_proto_data_mp_stream(&pd->mp_stream);
5625
#endif
5626
  mg_http_free_proto_data_endpoints(&pd->endpoints);
5627
  mg_http_free_reverse_proxy_data(&pd->reverse_proxy_data);
5628
  MG_FREE(proto_data);
5629
}
5630

    
5631
#if MG_ENABLE_FILESYSTEM
5632

    
5633
#define MIME_ENTRY(_ext, _type) \
5634
  { _ext, sizeof(_ext) - 1, _type }
5635
static const struct {
5636
  const char *extension;
5637
  size_t ext_len;
5638
  const char *mime_type;
5639
} mg_static_builtin_mime_types[] = {
5640
    MIME_ENTRY("html", "text/html"),
5641
    MIME_ENTRY("html", "text/html"),
5642
    MIME_ENTRY("htm", "text/html"),
5643
    MIME_ENTRY("shtm", "text/html"),
5644
    MIME_ENTRY("shtml", "text/html"),
5645
    MIME_ENTRY("css", "text/css"),
5646
    MIME_ENTRY("js", "application/x-javascript"),
5647
    MIME_ENTRY("ico", "image/x-icon"),
5648
    MIME_ENTRY("gif", "image/gif"),
5649
    MIME_ENTRY("jpg", "image/jpeg"),
5650
    MIME_ENTRY("jpeg", "image/jpeg"),
5651
    MIME_ENTRY("png", "image/png"),
5652
    MIME_ENTRY("svg", "image/svg+xml"),
5653
    MIME_ENTRY("txt", "text/plain"),
5654
    MIME_ENTRY("torrent", "application/x-bittorrent"),
5655
    MIME_ENTRY("wav", "audio/x-wav"),
5656
    MIME_ENTRY("mp3", "audio/x-mp3"),
5657
    MIME_ENTRY("mid", "audio/mid"),
5658
    MIME_ENTRY("m3u", "audio/x-mpegurl"),
5659
    MIME_ENTRY("ogg", "application/ogg"),
5660
    MIME_ENTRY("ram", "audio/x-pn-realaudio"),
5661
    MIME_ENTRY("xml", "text/xml"),
5662
    MIME_ENTRY("ttf", "application/x-font-ttf"),
5663
    MIME_ENTRY("json", "application/json"),
5664
    MIME_ENTRY("xslt", "application/xml"),
5665
    MIME_ENTRY("xsl", "application/xml"),
5666
    MIME_ENTRY("ra", "audio/x-pn-realaudio"),
5667
    MIME_ENTRY("doc", "application/msword"),
5668
    MIME_ENTRY("exe", "application/octet-stream"),
5669
    MIME_ENTRY("zip", "application/x-zip-compressed"),
5670
    MIME_ENTRY("xls", "application/excel"),
5671
    MIME_ENTRY("tgz", "application/x-tar-gz"),
5672
    MIME_ENTRY("tar", "application/x-tar"),
5673
    MIME_ENTRY("gz", "application/x-gunzip"),
5674
    MIME_ENTRY("arj", "application/x-arj-compressed"),
5675
    MIME_ENTRY("rar", "application/x-rar-compressed"),
5676
    MIME_ENTRY("rtf", "application/rtf"),
5677
    MIME_ENTRY("pdf", "application/pdf"),
5678
    MIME_ENTRY("swf", "application/x-shockwave-flash"),
5679
    MIME_ENTRY("mpg", "video/mpeg"),
5680
    MIME_ENTRY("webm", "video/webm"),
5681
    MIME_ENTRY("mpeg", "video/mpeg"),
5682
    MIME_ENTRY("mov", "video/quicktime"),
5683
    MIME_ENTRY("mp4", "video/mp4"),
5684
    MIME_ENTRY("m4v", "video/x-m4v"),
5685
    MIME_ENTRY("asf", "video/x-ms-asf"),
5686
    MIME_ENTRY("avi", "video/x-msvideo"),
5687
    MIME_ENTRY("bmp", "image/bmp"),
5688
    {NULL, 0, NULL}};
5689

    
5690
static struct mg_str mg_get_mime_type(const char *path, const char *dflt,
5691
                                      const struct mg_serve_http_opts *opts) {
5692
  const char *ext, *overrides;
5693
  size_t i, path_len;
5694
  struct mg_str r, k, v;
5695

    
5696
  path_len = strlen(path);
5697

    
5698
  overrides = opts->custom_mime_types;
5699
  while ((overrides = mg_next_comma_list_entry(overrides, &k, &v)) != NULL) {
5700
    ext = path + (path_len - k.len);
5701
    if (path_len > k.len && mg_vcasecmp(&k, ext) == 0) {
5702
      return v;
5703
    }
5704
  }
5705

    
5706
  for (i = 0; mg_static_builtin_mime_types[i].extension != NULL; i++) {
5707
    ext = path + (path_len - mg_static_builtin_mime_types[i].ext_len);
5708
    if (path_len > mg_static_builtin_mime_types[i].ext_len && ext[-1] == '.' &&
5709
        mg_casecmp(ext, mg_static_builtin_mime_types[i].extension) == 0) {
5710
      r.p = mg_static_builtin_mime_types[i].mime_type;
5711
      r.len = strlen(r.p);
5712
      return r;
5713
    }
5714
  }
5715

    
5716
  r.p = dflt;
5717
  r.len = strlen(r.p);
5718
  return r;
5719
}
5720
#endif
5721

    
5722
/*
5723
 * Check whether full request is buffered. Return:
5724
 *   -1  if request is malformed
5725
 *    0  if request is not yet fully buffered
5726
 *   >0  actual request length, including last \r\n\r\n
5727
 */
5728
static int mg_http_get_request_len(const char *s, int buf_len) {
5729
  const unsigned char *buf = (unsigned char *) s;
5730
  int i;
5731

    
5732
  for (i = 0; i < buf_len; i++) {
5733
    if (!isprint(buf[i]) && buf[i] != '\r' && buf[i] != '\n' && buf[i] < 128) {
5734
      return -1;
5735
    } else if (buf[i] == '\n' && i + 1 < buf_len && buf[i + 1] == '\n') {
5736
      return i + 2;
5737
    } else if (buf[i] == '\n' && i + 2 < buf_len && buf[i + 1] == '\r' &&
5738
               buf[i + 2] == '\n') {
5739
      return i + 3;
5740
    }
5741
  }
5742

    
5743
  return 0;
5744
}
5745

    
5746
static const char *mg_http_parse_headers(const char *s, const char *end,
5747
                                         int len, struct http_message *req) {
5748
  int i = 0;
5749
  while (i < (int) ARRAY_SIZE(req->header_names) - 1) {
5750
    struct mg_str *k = &req->header_names[i], *v = &req->header_values[i];
5751

    
5752
    s = mg_skip(s, end, ": ", k);
5753
    s = mg_skip(s, end, "\r\n", v);
5754

    
5755
    while (v->len > 0 && v->p[v->len - 1] == ' ') {
5756
      v->len--; /* Trim trailing spaces in header value */
5757
    }
5758

    
5759
    /*
5760
     * If header value is empty - skip it and go to next (if any).
5761
     * NOTE: Do not add it to headers_values because such addition changes API
5762
     * behaviour
5763
     */
5764
    if (k->len != 0 && v->len == 0) {
5765
      continue;
5766
    }
5767

    
5768
    if (k->len == 0 || v->len == 0) {
5769
      k->p = v->p = NULL;
5770
      k->len = v->len = 0;
5771
      break;
5772
    }
5773

    
5774
    if (!mg_ncasecmp(k->p, "Content-Length", 14)) {
5775
      req->body.len = (size_t) to64(v->p);
5776
      req->message.len = len + req->body.len;
5777
    }
5778

    
5779
    i++;
5780
  }
5781

    
5782
  return s;
5783
}
5784

    
5785
int mg_parse_http(const char *s, int n, struct http_message *hm, int is_req) {
5786
  const char *end, *qs;
5787
  int len = mg_http_get_request_len(s, n);
5788

    
5789
  if (len <= 0) return len;
5790

    
5791
  memset(hm, 0, sizeof(*hm));
5792
  hm->message.p = s;
5793
  hm->body.p = s + len;
5794
  hm->message.len = hm->body.len = (size_t) ~0;
5795
  end = s + len;
5796

    
5797
  /* Request is fully buffered. Skip leading whitespaces. */
5798
  while (s < end && isspace(*(unsigned char *) s)) s++;
5799

    
5800
  if (is_req) {
5801
    /* Parse request line: method, URI, proto */
5802
    s = mg_skip(s, end, " ", &hm->method);
5803
    s = mg_skip(s, end, " ", &hm->uri);
5804
    s = mg_skip(s, end, "\r\n", &hm->proto);
5805
    if (hm->uri.p <= hm->method.p || hm->proto.p <= hm->uri.p) return -1;
5806

    
5807
    /* If URI contains '?' character, initialize query_string */
5808
    if ((qs = (char *) memchr(hm->uri.p, '?', hm->uri.len)) != NULL) {
5809
      hm->query_string.p = qs + 1;
5810
      hm->query_string.len = &hm->uri.p[hm->uri.len] - (qs + 1);
5811
      hm->uri.len = qs - hm->uri.p;
5812
    }
5813
  } else {
5814
    s = mg_skip(s, end, " ", &hm->proto);
5815
    if (end - s < 4 || s[3] != ' ') return -1;
5816
    hm->resp_code = atoi(s);
5817
    if (hm->resp_code < 100 || hm->resp_code >= 600) return -1;
5818
    s += 4;
5819
    s = mg_skip(s, end, "\r\n", &hm->resp_status_msg);
5820
  }
5821

    
5822
  s = mg_http_parse_headers(s, end, len, hm);
5823

    
5824
  /*
5825
   * mg_parse_http() is used to parse both HTTP requests and HTTP
5826
   * responses. If HTTP response does not have Content-Length set, then
5827
   * body is read until socket is closed, i.e. body.len is infinite (~0).
5828
   *
5829
   * For HTTP requests though, according to
5830
   * http://tools.ietf.org/html/rfc7231#section-8.1.3,
5831
   * only POST and PUT methods have defined body semantics.
5832
   * Therefore, if Content-Length is not specified and methods are
5833
   * not one of PUT or POST, set body length to 0.
5834
   *
5835
   * So,
5836
   * if it is HTTP request, and Content-Length is not set,
5837
   * and method is not (PUT or POST) then reset body length to zero.
5838
   */
5839
  if (hm->body.len == (size_t) ~0 && is_req &&
5840
      mg_vcasecmp(&hm->method, "PUT") != 0 &&
5841
      mg_vcasecmp(&hm->method, "POST") != 0) {
5842
    hm->body.len = 0;
5843
    hm->message.len = len;
5844
  }
5845

    
5846
  return len;
5847
}
5848

    
5849
struct mg_str *mg_get_http_header(struct http_message *hm, const char *name) {
5850
  size_t i, len = strlen(name);
5851

    
5852
  for (i = 0; hm->header_names[i].len > 0; i++) {
5853
    struct mg_str *h = &hm->header_names[i], *v = &hm->header_values[i];
5854
    if (h->p != NULL && h->len == len && !mg_ncasecmp(h->p, name, len))
5855
      return v;
5856
  }
5857

    
5858
  return NULL;
5859
}
5860

    
5861
#if MG_ENABLE_FILESYSTEM
5862
static void mg_http_transfer_file_data(struct mg_connection *nc) {
5863
  struct mg_http_proto_data *pd = mg_http_get_proto_data(nc);
5864
  char buf[MG_MAX_HTTP_SEND_MBUF];
5865
  size_t n = 0, to_read = 0, left = (size_t)(pd->file.cl - pd->file.sent);
5866

    
5867
  if (pd->file.type == DATA_FILE) {
5868
    struct mbuf *io = &nc->send_mbuf;
5869
    if (io->len >= MG_MAX_HTTP_SEND_MBUF) {
5870
      to_read = 0;
5871
    } else {
5872
      to_read = MG_MAX_HTTP_SEND_MBUF - io->len;
5873
    }
5874
    if (to_read > left) {
5875
      to_read = left;
5876
    }
5877
    if (to_read > 0) {
5878
      n = mg_fread(buf, 1, to_read, pd->file.fp);
5879
      if (n > 0) {
5880
        mg_send(nc, buf, n);
5881
        pd->file.sent += n;
5882
        DBG(("%p sent %d (total %d)", nc, (int) n, (int) pd->file.sent));
5883
      }
5884
    } else {
5885
      /* Rate-limited */
5886
    }
5887
    if (pd->file.sent >= pd->file.cl) {
5888
      LOG(LL_DEBUG, ("%p done, %d bytes", nc, (int) pd->file.sent));
5889
      if (!pd->file.keepalive) nc->flags |= MG_F_SEND_AND_CLOSE;
5890
      mg_http_free_proto_data_file(&pd->file);
5891
    }
5892
  } else if (pd->file.type == DATA_PUT) {
5893
    struct mbuf *io = &nc->recv_mbuf;
5894
    size_t to_write = left <= 0 ? 0 : left < io->len ? (size_t) left : io->len;
5895
    size_t n = mg_fwrite(io->buf, 1, to_write, pd->file.fp);
5896
    if (n > 0) {
5897
      mbuf_remove(io, n);
5898
      pd->file.sent += n;
5899
    }
5900
    if (n == 0 || pd->file.sent >= pd->file.cl) {
5901
      if (!pd->file.keepalive) nc->flags |= MG_F_SEND_AND_CLOSE;
5902
      mg_http_free_proto_data_file(&pd->file);
5903
    }
5904
  }
5905
#if MG_ENABLE_HTTP_CGI
5906
  else if (pd->cgi.cgi_nc != NULL) {
5907
    /* This is POST data that needs to be forwarded to the CGI process */
5908
    if (pd->cgi.cgi_nc != NULL) {
5909
      mg_forward(nc, pd->cgi.cgi_nc);
5910
    } else {
5911
      nc->flags |= MG_F_SEND_AND_CLOSE;
5912
    }
5913
  }
5914
#endif
5915
}
5916
#endif /* MG_ENABLE_FILESYSTEM */
5917

    
5918
/*
5919
 * Parse chunked-encoded buffer. Return 0 if the buffer is not encoded, or
5920
 * if it's incomplete. If the chunk is fully buffered, return total number of
5921
 * bytes in a chunk, and store data in `data`, `data_len`.
5922
 */
5923
static size_t mg_http_parse_chunk(char *buf, size_t len, char **chunk_data,
5924
                                  size_t *chunk_len) {
5925
  unsigned char *s = (unsigned char *) buf;
5926
  size_t n = 0; /* scanned chunk length */
5927
  size_t i = 0; /* index in s */
5928

    
5929
  /* Scan chunk length. That should be a hexadecimal number. */
5930
  while (i < len && isxdigit(s[i])) {
5931
    n *= 16;
5932
    n += (s[i] >= '0' && s[i] <= '9') ? s[i] - '0' : tolower(s[i]) - 'a' + 10;
5933
    i++;
5934
  }
5935

    
5936
  /* Skip new line */
5937
  if (i == 0 || i + 2 > len || s[i] != '\r' || s[i + 1] != '\n') {
5938
    return 0;
5939
  }
5940
  i += 2;
5941

    
5942
  /* Record where the data is */
5943
  *chunk_data = (char *) s + i;
5944
  *chunk_len = n;
5945

    
5946
  /* Skip data */
5947
  i += n;
5948

    
5949
  /* Skip new line */
5950
  if (i == 0 || i + 2 > len || s[i] != '\r' || s[i + 1] != '\n') {
5951
    return 0;
5952
  }
5953
  return i + 2;
5954
}
5955

    
5956
MG_INTERNAL size_t mg_handle_chunked(struct mg_connection *nc,
5957
                                     struct http_message *hm, char *buf,
5958
                                     size_t blen) {
5959
  struct mg_http_proto_data *pd = mg_http_get_proto_data(nc);
5960
  char *data;
5961
  size_t i, n, data_len, body_len, zero_chunk_received = 0;
5962
  /* Find out piece of received data that is not yet reassembled */
5963
  body_len = (size_t) pd->chunk.body_len;
5964
  assert(blen >= body_len);
5965

    
5966
  /* Traverse all fully buffered chunks */
5967
  for (i = body_len;
5968
       (n = mg_http_parse_chunk(buf + i, blen - i, &data, &data_len)) > 0;
5969
       i += n) {
5970
    /* Collapse chunk data to the rest of HTTP body */
5971
    memmove(buf + body_len, data, data_len);
5972
    body_len += data_len;
5973
    hm->body.len = body_len;
5974

    
5975
    if (data_len == 0) {
5976
      zero_chunk_received = 1;
5977
      i += n;
5978
      break;
5979
    }
5980
  }
5981

    
5982
  if (i > body_len) {
5983
    /* Shift unparsed content to the parsed body */
5984
    assert(i <= blen);
5985
    memmove(buf + body_len, buf + i, blen - i);
5986
    memset(buf + body_len + blen - i, 0, i - body_len);
5987
    nc->recv_mbuf.len -= i - body_len;
5988
    pd->chunk.body_len = body_len;
5989

    
5990
    /* Send MG_EV_HTTP_CHUNK event */
5991
    nc->flags &= ~MG_F_DELETE_CHUNK;
5992
    mg_call(nc, nc->handler, nc->user_data, MG_EV_HTTP_CHUNK, hm);
5993

    
5994
    /* Delete processed data if user set MG_F_DELETE_CHUNK flag */
5995
    if (nc->flags & MG_F_DELETE_CHUNK) {
5996
      memset(buf, 0, body_len);
5997
      memmove(buf, buf + body_len, blen - i);
5998
      nc->recv_mbuf.len -= body_len;
5999
      hm->body.len = 0;
6000
      pd->chunk.body_len = 0;
6001
    }
6002

    
6003
    if (zero_chunk_received) {
6004
      /* Total message size is len(body) + len(headers) */
6005
      hm->message.len =
6006
          (size_t) pd->chunk.body_len + blen - i + (hm->body.p - hm->message.p);
6007
    }
6008
  }
6009

    
6010
  return body_len;
6011
}
6012

    
6013
struct mg_http_endpoint *mg_http_get_endpoint_handler(struct mg_connection *nc,
6014
                                                      struct mg_str *uri_path) {
6015
  struct mg_http_proto_data *pd;
6016
  struct mg_http_endpoint *ret = NULL;
6017
  int matched, matched_max = 0;
6018
  struct mg_http_endpoint *ep;
6019

    
6020
  if (nc == NULL) {
6021
    return NULL;
6022
  }
6023

    
6024
  pd = mg_http_get_proto_data(nc);
6025

    
6026
  ep = pd->endpoints;
6027
  while (ep != NULL) {
6028
    if ((matched = mg_match_prefix_n(ep->uri_pattern, *uri_path)) > 0) {
6029
      if (matched > matched_max) {
6030
        /* Looking for the longest suitable handler */
6031
        ret = ep;
6032
        matched_max = matched;
6033
      }
6034
    }
6035

    
6036
    ep = ep->next;
6037
  }
6038

    
6039
  return ret;
6040
}
6041

    
6042
#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6043
static void mg_http_multipart_continue(struct mg_connection *nc);
6044

    
6045
static void mg_http_multipart_begin(struct mg_connection *nc,
6046
                                    struct http_message *hm, int req_len);
6047

    
6048
#endif
6049

    
6050
static void mg_http_call_endpoint_handler(struct mg_connection *nc, int ev,
6051
                                          struct http_message *hm);
6052

    
6053
static void deliver_chunk(struct mg_connection *c, struct http_message *hm,
6054
                          int req_len) {
6055
  /* Incomplete message received. Send MG_EV_HTTP_CHUNK event */
6056
  hm->body.len = c->recv_mbuf.len - req_len;
6057
  c->flags &= ~MG_F_DELETE_CHUNK;
6058
  mg_call(c, c->handler, c->user_data, MG_EV_HTTP_CHUNK, hm);
6059
  /* Delete processed data if user set MG_F_DELETE_CHUNK flag */
6060
  if (c->flags & MG_F_DELETE_CHUNK) c->recv_mbuf.len = req_len;
6061
}
6062

    
6063
/*
6064
 * lx106 compiler has a bug (TODO(mkm) report and insert tracking bug here)
6065
 * If a big structure is declared in a big function, lx106 gcc will make it
6066
 * even bigger (round up to 4k, from 700 bytes of actual size).
6067
 */
6068
#ifdef __xtensa__
6069
static void mg_http_handler2(struct mg_connection *nc, int ev,
6070
                             void *ev_data MG_UD_ARG(void *user_data),
6071
                             struct http_message *hm) __attribute__((noinline));
6072

    
6073
void mg_http_handler(struct mg_connection *nc, int ev,
6074
                     void *ev_data MG_UD_ARG(void *user_data)) {
6075
  struct http_message hm;
6076
  mg_http_handler2(nc, ev, ev_data MG_UD_ARG(user_data), &hm);
6077
}
6078

    
6079
static void mg_http_handler2(struct mg_connection *nc, int ev,
6080
                             void *ev_data MG_UD_ARG(void *user_data),
6081
                             struct http_message *hm) {
6082
#else  /* !__XTENSA__ */
6083
void mg_http_handler(struct mg_connection *nc, int ev,
6084
                     void *ev_data MG_UD_ARG(void *user_data)) {
6085
  struct http_message shm, *hm = &shm;
6086
#endif /* __XTENSA__ */
6087
  struct mg_http_proto_data *pd = mg_http_get_proto_data(nc);
6088
  struct mbuf *io = &nc->recv_mbuf;
6089
  int req_len;
6090
  const int is_req = (nc->listener != NULL);
6091
#if MG_ENABLE_HTTP_WEBSOCKET
6092
  struct mg_str *vec;
6093
#endif
6094
  if (ev == MG_EV_CLOSE) {
6095
#if MG_ENABLE_HTTP_CGI
6096
    /* Close associated CGI forwarder connection */
6097
    if (pd->cgi.cgi_nc != NULL) {
6098
      pd->cgi.cgi_nc->user_data = NULL;
6099
      pd->cgi.cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY;
6100
    }
6101
#endif
6102
#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6103
    if (pd->mp_stream.boundary != NULL) {
6104
      /*
6105
       * Multipart message is in progress, but connection is closed.
6106
       * Finish part and request with an error flag.
6107
       */
6108
      struct mg_http_multipart_part mp;
6109
      memset(&mp, 0, sizeof(mp));
6110
      mp.status = -1;
6111
      mp.var_name = pd->mp_stream.var_name;
6112
      mp.file_name = pd->mp_stream.file_name;
6113
      mg_call(nc, (pd->endpoint_handler ? pd->endpoint_handler : nc->handler),
6114
              nc->user_data, MG_EV_HTTP_PART_END, &mp);
6115
      mp.var_name = NULL;
6116
      mp.file_name = NULL;
6117
      mg_call(nc, (pd->endpoint_handler ? pd->endpoint_handler : nc->handler),
6118
              nc->user_data, MG_EV_HTTP_MULTIPART_REQUEST_END, &mp);
6119
    } else
6120
#endif
6121
        if (io->len > 0 &&
6122
            (req_len = mg_parse_http(io->buf, io->len, hm, is_req)) > 0) {
6123
      /*
6124
      * For HTTP messages without Content-Length, always send HTTP message
6125
      * before MG_EV_CLOSE message.
6126
      */
6127
      int ev2 = is_req ? MG_EV_HTTP_REQUEST : MG_EV_HTTP_REPLY;
6128
      hm->message.len = io->len;
6129
      hm->body.len = io->buf + io->len - hm->body.p;
6130
      deliver_chunk(nc, hm, req_len);
6131
      mg_http_call_endpoint_handler(nc, ev2, hm);
6132
    }
6133
    pd->rcvd = 0;
6134
  }
6135

    
6136
#if MG_ENABLE_FILESYSTEM
6137
  if (pd->file.fp != NULL) {
6138
    mg_http_transfer_file_data(nc);
6139
  }
6140
#endif
6141

    
6142
  mg_call(nc, nc->handler, nc->user_data, ev, ev_data);
6143

    
6144
  if (ev == MG_EV_RECV) {
6145
    struct mg_str *s;
6146
    pd->rcvd += *(int *) ev_data;
6147

    
6148
#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6149
    if (pd->mp_stream.boundary != NULL) {
6150
      mg_http_multipart_continue(nc);
6151
      return;
6152
    }
6153
#endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */
6154

    
6155
    req_len = mg_parse_http(io->buf, io->len, hm, is_req);
6156

    
6157
    if (req_len > 0 &&
6158
        (s = mg_get_http_header(hm, "Transfer-Encoding")) != NULL &&
6159
        mg_vcasecmp(s, "chunked") == 0) {
6160
      mg_handle_chunked(nc, hm, io->buf + req_len, io->len - req_len);
6161
    }
6162

    
6163
#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6164
    if (req_len > 0 && (s = mg_get_http_header(hm, "Content-Type")) != NULL &&
6165
        s->len >= 9 && strncmp(s->p, "multipart", 9) == 0) {
6166
      mg_http_multipart_begin(nc, hm, req_len);
6167
      mg_http_multipart_continue(nc);
6168
      return;
6169
    }
6170
#endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */
6171

    
6172
    /* TODO(alashkin): refactor this ifelseifelseifelseifelse */
6173
    if ((req_len < 0 ||
6174
         (req_len == 0 && io->len >= MG_MAX_HTTP_REQUEST_SIZE))) {
6175
      DBG(("invalid request"));
6176
      nc->flags |= MG_F_CLOSE_IMMEDIATELY;
6177
    } else if (req_len == 0) {
6178
      /* Do nothing, request is not yet fully buffered */
6179
    }
6180
#if MG_ENABLE_HTTP_WEBSOCKET
6181
    else if (nc->listener == NULL &&
6182
             mg_get_http_header(hm, "Sec-WebSocket-Accept")) {
6183
      /* We're websocket client, got handshake response from server. */
6184
      /* TODO(lsm): check the validity of accept Sec-WebSocket-Accept */
6185
      mbuf_remove(io, req_len);
6186
      nc->proto_handler = mg_ws_handler;
6187
      nc->flags |= MG_F_IS_WEBSOCKET;
6188
      mg_call(nc, nc->handler, nc->user_data, MG_EV_WEBSOCKET_HANDSHAKE_DONE,
6189
              NULL);
6190
      mg_ws_handler(nc, MG_EV_RECV, ev_data MG_UD_ARG(user_data));
6191
    } else if (nc->listener != NULL &&
6192
               (vec = mg_get_http_header(hm, "Sec-WebSocket-Key")) != NULL) {
6193
      struct mg_http_endpoint *ep;
6194

    
6195
      /* This is a websocket request. Switch protocol handlers. */
6196
      mbuf_remove(io, req_len);
6197
      nc->proto_handler = mg_ws_handler;
6198
      nc->flags |= MG_F_IS_WEBSOCKET;
6199

    
6200
      /*
6201
       * If we have a handler set up with mg_register_http_endpoint(),
6202
       * deliver subsequent websocket events to this handler after the
6203
       * protocol switch.
6204
       */
6205
      ep = mg_http_get_endpoint_handler(nc->listener, &hm->uri);
6206
      if (ep != NULL) {
6207
        nc->handler = ep->handler;
6208
#if MG_ENABLE_CALLBACK_USERDATA
6209
        nc->user_data = ep->user_data;
6210
#endif
6211
      }
6212

    
6213
      /* Send handshake */
6214
      mg_call(nc, nc->handler, nc->user_data, MG_EV_WEBSOCKET_HANDSHAKE_REQUEST,
6215
              hm);
6216
      if (!(nc->flags & (MG_F_CLOSE_IMMEDIATELY | MG_F_SEND_AND_CLOSE))) {
6217
        if (nc->send_mbuf.len == 0) {
6218
          mg_ws_handshake(nc, vec, hm);
6219
        }
6220
        mg_call(nc, nc->handler, nc->user_data, MG_EV_WEBSOCKET_HANDSHAKE_DONE,
6221
                NULL);
6222
        mg_ws_handler(nc, MG_EV_RECV, ev_data MG_UD_ARG(user_data));
6223
      }
6224
    }
6225
#endif /* MG_ENABLE_HTTP_WEBSOCKET */
6226
    else if (hm->message.len > pd->rcvd) {
6227
      /* Not yet received all HTTP body, deliver MG_EV_HTTP_CHUNK */
6228
      deliver_chunk(nc, hm, req_len);
6229
      if (nc->recv_mbuf_limit > 0 && nc->recv_mbuf.len >= nc->recv_mbuf_limit) {
6230
        LOG(LL_ERROR, ("%p recv buffer (%lu bytes) exceeds the limit "
6231
                       "%lu bytes, and not drained, closing",
6232
                       nc, (unsigned long) nc->recv_mbuf.len,
6233
                       (unsigned long) nc->recv_mbuf_limit));
6234
        nc->flags |= MG_F_CLOSE_IMMEDIATELY;
6235
      }
6236
    } else {
6237
      /* We did receive all HTTP body. */
6238
      int trigger_ev = nc->listener ? MG_EV_HTTP_REQUEST : MG_EV_HTTP_REPLY;
6239
      char addr[32];
6240
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
6241
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
6242
      DBG(("%p %s %.*s %.*s", nc, addr, (int) hm->method.len, hm->method.p,
6243
           (int) hm->uri.len, hm->uri.p));
6244
      deliver_chunk(nc, hm, req_len);
6245
      /* Whole HTTP message is fully buffered, call event handler */
6246
      mg_http_call_endpoint_handler(nc, trigger_ev, hm);
6247
      mbuf_remove(io, hm->message.len);
6248
      pd->rcvd = 0;
6249
    }
6250
  }
6251
}
6252

    
6253
static size_t mg_get_line_len(const char *buf, size_t buf_len) {
6254
  size_t len = 0;
6255
  while (len < buf_len && buf[len] != '\n') len++;
6256
  return len == buf_len ? 0 : len + 1;
6257
}
6258

    
6259
#if MG_ENABLE_HTTP_STREAMING_MULTIPART
6260
static void mg_http_multipart_begin(struct mg_connection *nc,
6261
                                    struct http_message *hm, int req_len) {
6262
  struct mg_http_proto_data *pd = mg_http_get_proto_data(nc);
6263
  struct mg_str *ct;
6264
  struct mbuf *io = &nc->recv_mbuf;
6265

    
6266
  char boundary_buf[100];
6267
  char *boundary = boundary_buf;
6268
  int boundary_len;
6269

    
6270
  ct = mg_get_http_header(hm, "Content-Type");
6271
  if (ct == NULL) {
6272
    /* We need more data - or it isn't multipart mesage */
6273
    goto exit_mp;
6274
  }
6275

    
6276
  /* Content-type should start with "multipart" */
6277
  if (ct->len < 9 || strncmp(ct->p, "multipart", 9) != 0) {
6278
    goto exit_mp;
6279
  }
6280

    
6281
  boundary_len =
6282
      mg_http_parse_header2(ct, "boundary", &boundary, sizeof(boundary_buf));
6283
  if (boundary_len == 0) {
6284
    /*
6285
     * Content type is multipart, but there is no boundary,
6286
     * probably malformed request
6287
     */
6288
    nc->flags = MG_F_CLOSE_IMMEDIATELY;
6289
    DBG(("invalid request"));
6290
    goto exit_mp;
6291
  }
6292

    
6293
  /* If we reach this place - that is multipart request */
6294

    
6295
  if (pd->mp_stream.boundary != NULL) {
6296
    /*
6297
     * Another streaming request was in progress,
6298
     * looks like protocol error
6299
     */
6300
    nc->flags |= MG_F_CLOSE_IMMEDIATELY;
6301
  } else {
6302
    struct mg_http_endpoint *ep = NULL;
6303
    pd->mp_stream.state = MPS_BEGIN;
6304
    pd->mp_stream.boundary = strdup(boundary);
6305
    pd->mp_stream.boundary_len = strlen(boundary);
6306
    pd->mp_stream.var_name = pd->mp_stream.file_name = NULL;
6307
    pd->endpoint_handler = nc->handler;
6308

    
6309
    ep = mg_http_get_endpoint_handler(nc->listener, &hm->uri);
6310
    if (ep != NULL) {
6311
      pd->endpoint_handler = ep->handler;
6312
    }
6313

    
6314
    mg_http_call_endpoint_handler(nc, MG_EV_HTTP_MULTIPART_REQUEST, hm);
6315

    
6316
    mbuf_remove(io, req_len);
6317
  }
6318
exit_mp:
6319
  if (boundary != boundary_buf) MG_FREE(boundary);
6320
}
6321

    
6322
#define CONTENT_DISPOSITION "Content-Disposition: "
6323

    
6324
static void mg_http_multipart_call_handler(struct mg_connection *c, int ev,
6325
                                           const char *data, size_t data_len) {
6326
  struct mg_http_multipart_part mp;
6327
  struct mg_http_proto_data *pd = mg_http_get_proto_data(c);
6328
  memset(&mp, 0, sizeof(mp));
6329

    
6330
  mp.var_name = pd->mp_stream.var_name;
6331
  mp.file_name = pd->mp_stream.file_name;
6332
  mp.user_data = pd->mp_stream.user_data;
6333
  mp.data.p = data;
6334
  mp.data.len = data_len;
6335
  mg_call(c, pd->endpoint_handler, c->user_data, ev, &mp);
6336
  pd->mp_stream.user_data = mp.user_data;
6337
}
6338

    
6339
static int mg_http_multipart_got_chunk(struct mg_connection *c) {
6340
  struct mg_http_proto_data *pd = mg_http_get_proto_data(c);
6341
  struct mbuf *io = &c->recv_mbuf;
6342

    
6343
  mg_http_multipart_call_handler(c, MG_EV_HTTP_PART_DATA, io->buf,
6344
                                 pd->mp_stream.prev_io_len);
6345
  mbuf_remove(io, pd->mp_stream.prev_io_len);
6346
  pd->mp_stream.prev_io_len = 0;
6347
  pd->mp_stream.state = MPS_WAITING_FOR_CHUNK;
6348

    
6349
  return 0;
6350
}
6351

    
6352
static int mg_http_multipart_finalize(struct mg_connection *c) {
6353
  struct mg_http_proto_data *pd = mg_http_get_proto_data(c);
6354

    
6355
  mg_http_multipart_call_handler(c, MG_EV_HTTP_PART_END, NULL, 0);
6356
  MG_FREE((void *) pd->mp_stream.file_name);
6357
  pd->mp_stream.file_name = NULL;
6358
  MG_FREE((void *) pd->mp_stream.var_name);
6359
  pd->mp_stream.var_name = NULL;
6360
  mg_http_multipart_call_handler(c, MG_EV_HTTP_MULTIPART_REQUEST_END, NULL, 0);
6361
  mg_http_free_proto_data_mp_stream(&pd->mp_stream);
6362
  pd->mp_stream.state = MPS_FINISHED;
6363

    
6364
  return 1;
6365
}
6366

    
6367
static int mg_http_multipart_wait_for_boundary(struct mg_connection *c) {
6368
  const char *boundary;
6369
  struct mbuf *io = &c->recv_mbuf;
6370
  struct mg_http_proto_data *pd = mg_http_get_proto_data(c);
6371

    
6372
  if (pd->mp_stream.boundary == NULL) {
6373
    pd->mp_stream.state = MPS_FINALIZE;
6374
    DBG(("Invalid request: boundary not initialized"));
6375
    return 0;
6376
  }
6377

    
6378
  if ((int) io->len < pd->mp_stream.boundary_len + 2) {
6379
    return 0;
6380
  }
6381

    
6382
  boundary = c_strnstr(io->buf, pd->mp_stream.boundary, io->len);
6383
  if (boundary != NULL) {
6384
    const char *boundary_end = (boundary + pd->mp_stream.boundary_len);
6385
    if (io->len - (boundary_end - io->buf) < 4) {
6386
      return 0;
6387
    }
6388
    if (strncmp(boundary_end, "--\r\n", 4) == 0) {
6389
      pd->mp_stream.state = MPS_FINALIZE;
6390
      mbuf_remove(io, (boundary_end - io->buf) + 4);
6391
    } else {
6392
      pd->mp_stream.state = MPS_GOT_BOUNDARY;
6393
    }
6394
  } else {
6395
    return 0;
6396
  }
6397

    
6398
  return 1;
6399
}
6400

    
6401
static void mg_http_parse_header_internal(struct mg_str *hdr,
6402
                                          const char *var_name,
6403
                                          struct altbuf *ab);
6404

    
6405
static int mg_http_multipart_process_boundary(struct mg_connection *c) {
6406
  int data_size;
6407
  const char *boundary, *block_begin;
6408
  struct mbuf *io = &c->recv_mbuf;
6409
  struct mg_http_proto_data *pd = mg_http_get_proto_data(c);
6410
  struct altbuf ab_file_name, ab_var_name;
6411
  int line_len;
6412
  boundary = c_strnstr(io->buf, pd->mp_stream.boundary, io->len);
6413
  block_begin = boundary + pd->mp_stream.boundary_len + 2;
6414
  data_size = io->len - (block_begin - io->buf);
6415

    
6416
  altbuf_init(&ab_file_name, NULL, 0);
6417
  altbuf_init(&ab_var_name, NULL, 0);
6418

    
6419
  while (data_size > 0 &&
6420
         (line_len = mg_get_line_len(block_begin, data_size)) != 0) {
6421
    if (line_len > (int) sizeof(CONTENT_DISPOSITION) &&
6422
        mg_ncasecmp(block_begin, CONTENT_DISPOSITION,
6423
                    sizeof(CONTENT_DISPOSITION) - 1) == 0) {
6424
      struct mg_str header;
6425

    
6426
      header.p = block_begin + sizeof(CONTENT_DISPOSITION) - 1;
6427
      header.len = line_len - sizeof(CONTENT_DISPOSITION) - 1;
6428

    
6429
      altbuf_reset(&ab_var_name);
6430
      mg_http_parse_header_internal(&header, "name", &ab_var_name);
6431

    
6432
      altbuf_reset(&ab_file_name);
6433
      mg_http_parse_header_internal(&header, "filename", &ab_file_name);
6434

    
6435
      block_begin += line_len;
6436
      data_size -= line_len;
6437

    
6438
      continue;
6439
    }
6440

    
6441
    if (line_len == 2 && mg_ncasecmp(block_begin, "\r\n", 2) == 0) {
6442
      mbuf_remove(io, block_begin - io->buf + 2);
6443

    
6444
      if (pd->mp_stream.processing_part != 0) {
6445
        mg_http_multipart_call_handler(c, MG_EV_HTTP_PART_END, NULL, 0);
6446
      }
6447

    
6448
      /* Reserve 2 bytes for "\r\n" in file_name and var_name */
6449
      altbuf_append(&ab_file_name, '\0');
6450
      altbuf_append(&ab_file_name, '\0');
6451
      altbuf_append(&ab_var_name, '\0');
6452
      altbuf_append(&ab_var_name, '\0');
6453

    
6454
      MG_FREE((void *) pd->mp_stream.file_name);
6455
      pd->mp_stream.file_name = altbuf_get_buf(&ab_file_name, 1 /* trim */);
6456
      MG_FREE((void *) pd->mp_stream.var_name);
6457
      pd->mp_stream.var_name = altbuf_get_buf(&ab_var_name, 1 /* trim */);
6458

    
6459
      mg_http_multipart_call_handler(c, MG_EV_HTTP_PART_BEGIN, NULL, 0);
6460
      pd->mp_stream.state = MPS_WAITING_FOR_CHUNK;
6461
      pd->mp_stream.processing_part++;
6462
      return 1;
6463
    }
6464

    
6465
    block_begin += line_len;
6466
  }
6467

    
6468
  pd->mp_stream.state = MPS_WAITING_FOR_BOUNDARY;
6469

    
6470
  altbuf_reset(&ab_var_name);
6471
  altbuf_reset(&ab_file_name);
6472

    
6473
  return 0;
6474
}
6475

    
6476
static int mg_http_multipart_continue_wait_for_chunk(struct mg_connection *c) {
6477
  struct mg_http_proto_data *pd = mg_http_get_proto_data(c);
6478
  struct mbuf *io = &c->recv_mbuf;
6479

    
6480
  const char *boundary;
6481
  if ((int) io->len < pd->mp_stream.boundary_len + 6 /* \r\n, --, -- */) {
6482
    return 0;
6483
  }
6484

    
6485
  boundary = c_strnstr(io->buf, pd->mp_stream.boundary, io->len);
6486
  if (boundary == NULL && pd->mp_stream.prev_io_len == 0) {
6487
    pd->mp_stream.prev_io_len = io->len;
6488
    return 0;
6489
  } else if (boundary == NULL &&
6490
             (int) io->len >
6491
                 pd->mp_stream.prev_io_len + pd->mp_stream.boundary_len + 4) {
6492
    pd->mp_stream.state = MPS_GOT_CHUNK;
6493
    return 1;
6494
  } else if (boundary != NULL) {
6495
    int data_size = (boundary - io->buf - 4);
6496
    mg_http_multipart_call_handler(c, MG_EV_HTTP_PART_DATA, io->buf, data_size);
6497
    mbuf_remove(io, (boundary - io->buf));
6498
    pd->mp_stream.prev_io_len = 0;
6499
    pd->mp_stream.state = MPS_WAITING_FOR_BOUNDARY;
6500
    return 1;
6501
  } else {
6502
    return 0;
6503
  }
6504
}
6505

    
6506
static void mg_http_multipart_continue(struct mg_connection *c) {
6507
  struct mg_http_proto_data *pd = mg_http_get_proto_data(c);
6508
  while (1) {
6509
    switch (pd->mp_stream.state) {
6510
      case MPS_BEGIN: {
6511
        pd->mp_stream.state = MPS_WAITING_FOR_BOUNDARY;
6512
        break;
6513
      }
6514
      case MPS_WAITING_FOR_BOUNDARY: {
6515
        if (mg_http_multipart_wait_for_boundary(c) == 0) {
6516
          return;
6517
        }
6518
        break;
6519
      }
6520
      case MPS_GOT_BOUNDARY: {
6521
        if (mg_http_multipart_process_boundary(c) == 0) {
6522
          return;
6523
        }
6524
        break;
6525
      }
6526
      case MPS_WAITING_FOR_CHUNK: {
6527
        if (mg_http_multipart_continue_wait_for_chunk(c) == 0) {
6528
          return;
6529
        }
6530
        break;
6531
      }
6532
      case MPS_GOT_CHUNK: {
6533
        if (mg_http_multipart_got_chunk(c) == 0) {
6534
          return;
6535
        }
6536
        break;
6537
      }
6538
      case MPS_FINALIZE: {
6539
        if (mg_http_multipart_finalize(c) == 0) {
6540
          return;
6541
        }
6542
        break;
6543
      }
6544
      case MPS_FINISHED: {
6545
        mbuf_remove(&c->recv_mbuf, c->recv_mbuf.len);
6546
        return;
6547
      }
6548
    }
6549
  }
6550
}
6551

    
6552
struct file_upload_state {
6553
  char *lfn;
6554
  size_t num_recd;
6555
  FILE *fp;
6556
};
6557

    
6558
#endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */
6559

    
6560
void mg_set_protocol_http_websocket(struct mg_connection *nc) {
6561
  nc->proto_handler = mg_http_handler;
6562
}
6563

    
6564
const char *mg_status_message(int status_code) {
6565
  switch (status_code) {
6566
    case 206:
6567
      return "Partial Content";
6568
    case 301:
6569
      return "Moved";
6570
    case 302:
6571
      return "Found";
6572
    case 400:
6573
      return "Bad Request";
6574
    case 401:
6575
      return "Unauthorized";
6576
    case 403:
6577
      return "Forbidden";
6578
    case 404:
6579
      return "Not Found";
6580
    case 416:
6581
      return "Requested Range Not Satisfiable";
6582
    case 418:
6583
      return "I'm a teapot";
6584
    case 500:
6585
      return "Internal Server Error";
6586
    case 502:
6587
      return "Bad Gateway";
6588
    case 503:
6589
      return "Service Unavailable";
6590

    
6591
#if MG_ENABLE_EXTRA_ERRORS_DESC
6592
    case 100:
6593
      return "Continue";
6594
    case 101:
6595
      return "Switching Protocols";
6596
    case 102:
6597
      return "Processing";
6598
    case 200:
6599
      return "OK";
6600
    case 201:
6601
      return "Created";
6602
    case 202:
6603
      return "Accepted";
6604
    case 203:
6605
      return "Non-Authoritative Information";
6606
    case 204:
6607
      return "No Content";
6608
    case 205:
6609
      return "Reset Content";
6610
    case 207:
6611
      return "Multi-Status";
6612
    case 208:
6613
      return "Already Reported";
6614
    case 226:
6615
      return "IM Used";
6616
    case 300:
6617
      return "Multiple Choices";
6618
    case 303:
6619
      return "See Other";
6620
    case 304:
6621
      return "Not Modified";
6622
    case 305:
6623
      return "Use Proxy";
6624
    case 306:
6625
      return "Switch Proxy";
6626
    case 307:
6627
      return "Temporary Redirect";
6628
    case 308:
6629
      return "Permanent Redirect";
6630
    case 402:
6631
      return "Payment Required";
6632
    case 405:
6633
      return "Method Not Allowed";
6634
    case 406:
6635
      return "Not Acceptable";
6636
    case 407:
6637
      return "Proxy Authentication Required";
6638
    case 408:
6639
      return "Request Timeout";
6640
    case 409:
6641
      return "Conflict";
6642
    case 410:
6643
      return "Gone";
6644
    case 411:
6645
      return "Length Required";
6646
    case 412:
6647
      return "Precondition Failed";
6648
    case 413:
6649
      return "Payload Too Large";
6650
    case 414:
6651
      return "URI Too Long";
6652
    case 415:
6653
      return "Unsupported Media Type";
6654
    case 417:
6655
      return "Expectation Failed";
6656
    case 422:
6657
      return "Unprocessable Entity";
6658
    case 423:
6659
      return "Locked";
6660
    case 424:
6661
      return "Failed Dependency";
6662
    case 426:
6663
      return "Upgrade Required";
6664
    case 428:
6665
      return "Precondition Required";
6666
    case 429:
6667
      return "Too Many Requests";
6668
    case 431:
6669
      return "Request Header Fields Too Large";
6670
    case 451:
6671
      return "Unavailable For Legal Reasons";
6672
    case 501:
6673
      return "Not Implemented";
6674
    case 504:
6675
      return "Gateway Timeout";
6676
    case 505:
6677
      return "HTTP Version Not Supported";
6678
    case 506:
6679
      return "Variant Also Negotiates";
6680
    case 507:
6681
      return "Insufficient Storage";
6682
    case 508:
6683
      return "Loop Detected";
6684
    case 510:
6685
      return "Not Extended";
6686
    case 511:
6687
      return "Network Authentication Required";
6688
#endif /* MG_ENABLE_EXTRA_ERRORS_DESC */
6689

    
6690
    default:
6691
      return "OK";
6692
  }
6693
}
6694

    
6695
void mg_send_response_line_s(struct mg_connection *nc, int status_code,
6696
                             const struct mg_str extra_headers) {
6697
  mg_printf(nc, "HTTP/1.1 %d %s\r\nServer: %s\r\n", status_code,
6698
            mg_status_message(status_code), mg_version_header);
6699
  if (extra_headers.len > 0) {
6700
    mg_printf(nc, "%.*s\r\n", (int) extra_headers.len, extra_headers.p);
6701
  }
6702
}
6703

    
6704
void mg_send_response_line(struct mg_connection *nc, int status_code,
6705
                           const char *extra_headers) {
6706
  mg_send_response_line_s(nc, status_code, mg_mk_str(extra_headers));
6707
}
6708

    
6709
void mg_http_send_redirect(struct mg_connection *nc, int status_code,
6710
                           const struct mg_str location,
6711
                           const struct mg_str extra_headers) {
6712
  char bbody[100], *pbody = bbody;
6713
  int bl = mg_asprintf(&pbody, sizeof(bbody),
6714
                       "<p>Moved <a href='%.*s'>here</a>.\r\n",
6715
                       (int) location.len, location.p);
6716
  char bhead[150], *phead = bhead;
6717
  mg_asprintf(&phead, sizeof(bhead),
6718
              "Location: %.*s\r\n"
6719
              "Content-Type: text/html\r\n"
6720
              "Content-Length: %d\r\n"
6721
              "Cache-Control: no-cache\r\n"
6722
              "%.*s%s",
6723
              (int) location.len, location.p, bl, (int) extra_headers.len,
6724
              extra_headers.p, (extra_headers.len > 0 ? "\r\n" : ""));
6725
  mg_send_response_line(nc, status_code, phead);
6726
  if (phead != bhead) MG_FREE(phead);
6727
  mg_send(nc, pbody, bl);
6728
  if (pbody != bbody) MG_FREE(pbody);
6729
}
6730

    
6731
void mg_send_head(struct mg_connection *c, int status_code,
6732
                  int64_t content_length, const char *extra_headers) {
6733
  mg_send_response_line(c, status_code, extra_headers);
6734
  if (content_length < 0) {
6735
    mg_printf(c, "%s", "Transfer-Encoding: chunked\r\n");
6736
  } else {
6737
    mg_printf(c, "Content-Length: %" INT64_FMT "\r\n", content_length);
6738
  }
6739
  mg_send(c, "\r\n", 2);
6740
}
6741

    
6742
void mg_http_send_error(struct mg_connection *nc, int code,
6743
                        const char *reason) {
6744
  if (!reason) reason = mg_status_message(code);
6745
  LOG(LL_DEBUG, ("%p %d %s", nc, code, reason));
6746
  mg_send_head(nc, code, strlen(reason),
6747
               "Content-Type: text/plain\r\nConnection: close");
6748
  mg_send(nc, reason, strlen(reason));
6749
  nc->flags |= MG_F_SEND_AND_CLOSE;
6750
}
6751

    
6752
#if MG_ENABLE_FILESYSTEM
6753
static void mg_http_construct_etag(char *buf, size_t buf_len,
6754
                                   const cs_stat_t *st) {
6755
  snprintf(buf, buf_len, "\"%lx.%" INT64_FMT "\"", (unsigned long) st->st_mtime,
6756
           (int64_t) st->st_size);
6757
}
6758

    
6759
#ifndef WINCE
6760
static void mg_gmt_time_string(char *buf, size_t buf_len, time_t *t) {
6761
  strftime(buf, buf_len, "%a, %d %b %Y %H:%M:%S GMT", gmtime(t));
6762
}
6763
#else
6764
/* Look wince_lib.c for WindowsCE implementation */
6765
static void mg_gmt_time_string(char *buf, size_t buf_len, time_t *t);
6766
#endif
6767

    
6768
static int mg_http_parse_range_header(const struct mg_str *header, int64_t *a,
6769
                                      int64_t *b) {
6770
  /*
6771
   * There is no snscanf. Headers are not guaranteed to be NUL-terminated,
6772
   * so we have this. Ugh.
6773
   */
6774
  int result;
6775
  char *p = (char *) MG_MALLOC(header->len + 1);
6776
  if (p == NULL) return 0;
6777
  memcpy(p, header->p, header->len);
6778
  p[header->len] = '\0';
6779
  result = sscanf(p, "bytes=%" INT64_FMT "-%" INT64_FMT, a, b);
6780
  MG_FREE(p);
6781
  return result;
6782
}
6783

    
6784
void mg_http_serve_file(struct mg_connection *nc, struct http_message *hm,
6785
                        const char *path, const struct mg_str mime_type,
6786
                        const struct mg_str extra_headers) {
6787
  struct mg_http_proto_data *pd = mg_http_get_proto_data(nc);
6788
  cs_stat_t st;
6789
  LOG(LL_DEBUG, ("%p [%s] %.*s", nc, path, (int) mime_type.len, mime_type.p));
6790
  if (mg_stat(path, &st) != 0 || (pd->file.fp = mg_fopen(path, "rb")) == NULL) {
6791
    int code, err = mg_get_errno();
6792
    switch (err) {
6793
      case EACCES:
6794
        code = 403;
6795
        break;
6796
      case ENOENT:
6797
        code = 404;
6798
        break;
6799
      default:
6800
        code = 500;
6801
    };
6802
    mg_http_send_error(nc, code, "Open failed");
6803
  } else {
6804
    char etag[50], current_time[50], last_modified[50], range[70];
6805
    time_t t = (time_t) mg_time();
6806
    int64_t r1 = 0, r2 = 0, cl = st.st_size;
6807
    struct mg_str *range_hdr = mg_get_http_header(hm, "Range");
6808
    int n, status_code = 200;
6809

    
6810
    /* Handle Range header */
6811
    range[0] = '\0';
6812
    if (range_hdr != NULL &&
6813
        (n = mg_http_parse_range_header(range_hdr, &r1, &r2)) > 0 && r1 >= 0 &&
6814
        r2 >= 0) {
6815
      /* If range is specified like "400-", set second limit to content len */
6816
      if (n == 1) {
6817
        r2 = cl - 1;
6818
      }
6819
      if (r1 > r2 || r2 >= cl) {
6820
        status_code = 416;
6821
        cl = 0;
6822
        snprintf(range, sizeof(range),
6823
                 "Content-Range: bytes */%" INT64_FMT "\r\n",
6824
                 (int64_t) st.st_size);
6825
      } else {
6826
        status_code = 206;
6827
        cl = r2 - r1 + 1;
6828
        snprintf(range, sizeof(range), "Content-Range: bytes %" INT64_FMT
6829
                                       "-%" INT64_FMT "/%" INT64_FMT "\r\n",
6830
                 r1, r1 + cl - 1, (int64_t) st.st_size);
6831
#if _FILE_OFFSET_BITS == 64 || _POSIX_C_SOURCE >= 200112L || \
6832
    _XOPEN_SOURCE >= 600
6833
        fseeko(pd->file.fp, r1, SEEK_SET);
6834
#else
6835
        fseek(pd->file.fp, (long) r1, SEEK_SET);
6836
#endif
6837
      }
6838
    }
6839

    
6840
#if !MG_DISABLE_HTTP_KEEP_ALIVE
6841
    {
6842
      struct mg_str *conn_hdr = mg_get_http_header(hm, "Connection");
6843
      if (conn_hdr != NULL) {
6844
        pd->file.keepalive = (mg_vcasecmp(conn_hdr, "keep-alive") == 0);
6845
      } else {
6846
        pd->file.keepalive = (mg_vcmp(&hm->proto, "HTTP/1.1") == 0);
6847
      }
6848
    }
6849
#endif
6850

    
6851
    mg_http_construct_etag(etag, sizeof(etag), &st);
6852
    mg_gmt_time_string(current_time, sizeof(current_time), &t);
6853
    mg_gmt_time_string(last_modified, sizeof(last_modified), &st.st_mtime);
6854
    /*
6855
     * Content length casted to size_t because:
6856
     * 1) that's the maximum buffer size anyway
6857
     * 2) ESP8266 RTOS SDK newlib vprintf cannot contain a 64bit arg at non-last
6858
     *    position
6859
     * TODO(mkm): fix ESP8266 RTOS SDK
6860
     */
6861
    mg_send_response_line_s(nc, status_code, extra_headers);
6862
    mg_printf(nc,
6863
              "Date: %s\r\n"
6864
              "Last-Modified: %s\r\n"
6865
              "Accept-Ranges: bytes\r\n"
6866
              "Content-Type: %.*s\r\n"
6867
              "Connection: %s\r\n"
6868
              "Content-Length: %" SIZE_T_FMT
6869
              "\r\n"
6870
              "%sEtag: %s\r\n\r\n",
6871
              current_time, last_modified, (int) mime_type.len, mime_type.p,
6872
              (pd->file.keepalive ? "keep-alive" : "close"), (size_t) cl, range,
6873
              etag);
6874

    
6875
    pd->file.cl = cl;
6876
    pd->file.type = DATA_FILE;
6877
    mg_http_transfer_file_data(nc);
6878
  }
6879
}
6880

    
6881
static void mg_http_serve_file2(struct mg_connection *nc, const char *path,
6882
                                struct http_message *hm,
6883
                                struct mg_serve_http_opts *opts) {
6884
#if MG_ENABLE_HTTP_SSI
6885
  if (mg_match_prefix(opts->ssi_pattern, strlen(opts->ssi_pattern), path) > 0) {
6886
    mg_handle_ssi_request(nc, hm, path, opts);
6887
    return;
6888
  }
6889
#endif
6890
  mg_http_serve_file(nc, hm, path, mg_get_mime_type(path, "text/plain", opts),
6891
                     mg_mk_str(opts->extra_headers));
6892
}
6893

    
6894
#endif
6895

    
6896
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len,
6897
                  int is_form_url_encoded) {
6898
  int i, j, a, b;
6899
#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
6900

    
6901
  for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) {
6902
    if (src[i] == '%') {
6903
      if (i < src_len - 2 && isxdigit(*(const unsigned char *) (src + i + 1)) &&
6904
          isxdigit(*(const unsigned char *) (src + i + 2))) {
6905
        a = tolower(*(const unsigned char *) (src + i + 1));
6906
        b = tolower(*(const unsigned char *) (src + i + 2));
6907
        dst[j] = (char) ((HEXTOI(a) << 4) | HEXTOI(b));
6908
        i += 2;
6909
      } else {
6910
        return -1;
6911
      }
6912
    } else if (is_form_url_encoded && src[i] == '+') {
6913
      dst[j] = ' ';
6914
    } else {
6915
      dst[j] = src[i];
6916
    }
6917
  }
6918

    
6919
  dst[j] = '\0'; /* Null-terminate the destination */
6920

    
6921
  return i >= src_len ? j : -1;
6922
}
6923

    
6924
int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst,
6925
                    size_t dst_len) {
6926
  const char *p, *e, *s;
6927
  size_t name_len;
6928
  int len;
6929

    
6930
  /*
6931
   * According to the documentation function returns negative
6932
   * value in case of error. For debug purposes it returns:
6933
   * -1 - src is wrong (NUUL)
6934
   * -2 - dst is wrong (NULL)
6935
   * -3 - failed to decode url or dst is to small
6936
   * -4 - name does not exist
6937
   */
6938
  if (dst == NULL || dst_len == 0) {
6939
    len = -2;
6940
  } else if (buf->p == NULL || name == NULL || buf->len == 0) {
6941
    len = -1;
6942
    dst[0] = '\0';
6943
  } else {
6944
    name_len = strlen(name);
6945
    e = buf->p + buf->len;
6946
    len = -4;
6947
    dst[0] = '\0';
6948

    
6949
    for (p = buf->p; p + name_len < e; p++) {
6950
      if ((p == buf->p || p[-1] == '&') && p[name_len] == '=' &&
6951
          !mg_ncasecmp(name, p, name_len)) {
6952
        p += name_len + 1;
6953
        s = (const char *) memchr(p, '&', (size_t)(e - p));
6954
        if (s == NULL) {
6955
          s = e;
6956
        }
6957
        len = mg_url_decode(p, (size_t)(s - p), dst, dst_len, 1);
6958
        /* -1 means: failed to decode or dst is too small */
6959
        if (len == -1) {
6960
          len = -3;
6961
        }
6962
        break;
6963
      }
6964
    }
6965
  }
6966

    
6967
  return len;
6968
}
6969

    
6970
void mg_send_http_chunk(struct mg_connection *nc, const char *buf, size_t len) {
6971
  char chunk_size[50];
6972
  int n;
6973

    
6974
  n = snprintf(chunk_size, sizeof(chunk_size), "%lX\r\n", (unsigned long) len);
6975
  mg_send(nc, chunk_size, n);
6976
  mg_send(nc, buf, len);
6977
  mg_send(nc, "\r\n", 2);
6978
}
6979

    
6980
void mg_printf_http_chunk(struct mg_connection *nc, const char *fmt, ...) {
6981
  char mem[MG_VPRINTF_BUFFER_SIZE], *buf = mem;
6982
  int len;
6983
  va_list ap;
6984

    
6985
  va_start(ap, fmt);
6986
  len = mg_avprintf(&buf, sizeof(mem), fmt, ap);
6987
  va_end(ap);
6988

    
6989
  if (len >= 0) {
6990
    mg_send_http_chunk(nc, buf, len);
6991
  }
6992

    
6993
  /* LCOV_EXCL_START */
6994
  if (buf != mem && buf != NULL) {
6995
    MG_FREE(buf);
6996
  }
6997
  /* LCOV_EXCL_STOP */
6998
}
6999

    
7000
void mg_printf_html_escape(struct mg_connection *nc, const char *fmt, ...) {
7001
  char mem[MG_VPRINTF_BUFFER_SIZE], *buf = mem;
7002
  int i, j, len;
7003
  va_list ap;
7004

    
7005
  va_start(ap, fmt);
7006
  len = mg_avprintf(&buf, sizeof(mem), fmt, ap);
7007
  va_end(ap);
7008

    
7009
  if (len >= 0) {
7010
    for (i = j = 0; i < len; i++) {
7011
      if (buf[i] == '<' || buf[i] == '>') {
7012
        mg_send(nc, buf + j, i - j);
7013
        mg_send(nc, buf[i] == '<' ? "&lt;" : "&gt;", 4);
7014
        j = i + 1;
7015
      }
7016
    }
7017
    mg_send(nc, buf + j, i - j);
7018
  }
7019

    
7020
  /* LCOV_EXCL_START */
7021
  if (buf != mem && buf != NULL) {
7022
    MG_FREE(buf);
7023
  }
7024
  /* LCOV_EXCL_STOP */
7025
}
7026

    
7027
static void mg_http_parse_header_internal(struct mg_str *hdr,
7028
                                          const char *var_name,
7029
                                          struct altbuf *ab) {
7030
  int ch = ' ', ch1 = ',', n = strlen(var_name);
7031
  const char *p, *end = hdr ? hdr->p + hdr->len : NULL, *s = NULL;
7032

    
7033
  /* Find where variable starts */
7034
  for (s = hdr->p; s != NULL && s + n < end; s++) {
7035
    if ((s == hdr->p || s[-1] == ch || s[-1] == ch1 || s[-1] == ';') &&
7036
        s[n] == '=' && !strncmp(s, var_name, n))
7037
      break;
7038
  }
7039

    
7040
  if (s != NULL && &s[n + 1] < end) {
7041
    s += n + 1;
7042
    if (*s == '"' || *s == '\'') {
7043
      ch = ch1 = *s++;
7044
    }
7045
    p = s;
7046
    while (p < end && p[0] != ch && p[0] != ch1) {
7047
      if (ch != ' ' && p[0] == '\\' && p[1] == ch) p++;
7048
      altbuf_append(ab, *p++);
7049
    }
7050

    
7051
    if (ch != ' ' && *p != ch) {
7052
      altbuf_reset(ab);
7053
    }
7054
  }
7055

    
7056
  /* If there is some data, append a NUL. */
7057
  if (ab->len > 0) {
7058
    altbuf_append(ab, '\0');
7059
  }
7060
}
7061

    
7062
int mg_http_parse_header2(struct mg_str *hdr, const char *var_name, char **buf,
7063
                          size_t buf_size) {
7064
  struct altbuf ab;
7065
  altbuf_init(&ab, *buf, buf_size);
7066
  if (hdr == NULL) return 0;
7067
  if (*buf != NULL && buf_size > 0) *buf[0] = '\0';
7068

    
7069
  mg_http_parse_header_internal(hdr, var_name, &ab);
7070

    
7071
  /*
7072
   * Get a (trimmed) buffer, and return a len without a NUL byte which might
7073
   * have been added.
7074
   */
7075
  *buf = altbuf_get_buf(&ab, 1 /* trim */);
7076
  return ab.len > 0 ? ab.len - 1 : 0;
7077
}
7078

    
7079
int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf,
7080
                         size_t buf_size) {
7081
  char *buf2 = buf;
7082

    
7083
  int len = mg_http_parse_header2(hdr, var_name, &buf2, buf_size);
7084

    
7085
  if (buf2 != buf) {
7086
    /* Buffer was not enough and was reallocated: free it and just return 0 */
7087
    MG_FREE(buf2);
7088
    return 0;
7089
  }
7090

    
7091
  return len;
7092
}
7093

    
7094
int mg_get_http_basic_auth(struct http_message *hm, char *user, size_t user_len,
7095
                           char *pass, size_t pass_len) {
7096
  struct mg_str *hdr = mg_get_http_header(hm, "Authorization");
7097
  if (hdr == NULL) return -1;
7098
  return mg_parse_http_basic_auth(hdr, user, user_len, pass, pass_len);
7099
}
7100

    
7101
int mg_parse_http_basic_auth(struct mg_str *hdr, char *user, size_t user_len,
7102
                             char *pass, size_t pass_len) {
7103
  char *buf = NULL;
7104
  char fmt[64];
7105
  int res = 0;
7106

    
7107
  if (mg_strncmp(*hdr, mg_mk_str("Basic "), 6) != 0) return -1;
7108

    
7109
  buf = (char *) MG_MALLOC(hdr->len);
7110
  cs_base64_decode((unsigned char *) hdr->p + 6, hdr->len, buf, NULL);
7111

    
7112
  /* e.g. "%123[^:]:%321[^\n]" */
7113
  snprintf(fmt, sizeof(fmt), "%%%" SIZE_T_FMT "[^:]:%%%" SIZE_T_FMT "[^\n]",
7114
           user_len - 1, pass_len - 1);
7115
  if (sscanf(buf, fmt, user, pass) == 0) {
7116
    res = -1;
7117
  }
7118

    
7119
  MG_FREE(buf);
7120
  return res;
7121
}
7122

    
7123
#if MG_ENABLE_FILESYSTEM
7124
static int mg_is_file_hidden(const char *path,
7125
                             const struct mg_serve_http_opts *opts,
7126
                             int exclude_specials) {
7127
  const char *p1 = opts->per_directory_auth_file;
7128
  const char *p2 = opts->hidden_file_pattern;
7129

    
7130
  /* Strip directory path from the file name */
7131
  const char *pdir = strrchr(path, DIRSEP);
7132
  if (pdir != NULL) {
7133
    path = pdir + 1;
7134
  }
7135

    
7136
  return (exclude_specials && (!strcmp(path, ".") || !strcmp(path, ".."))) ||
7137
         (p1 != NULL && mg_match_prefix(p1, strlen(p1), path) == strlen(p1)) ||
7138
         (p2 != NULL && mg_match_prefix(p2, strlen(p2), path) > 0);
7139
}
7140

    
7141
#if !MG_DISABLE_HTTP_DIGEST_AUTH
7142

    
7143
#ifndef MG_EXT_MD5
7144
void mg_hash_md5_v(size_t num_msgs, const uint8_t *msgs[],
7145
                   const size_t *msg_lens, uint8_t *digest) {
7146
  size_t i;
7147
  cs_md5_ctx md5_ctx;
7148
  cs_md5_init(&md5_ctx);
7149
  for (i = 0; i < num_msgs; i++) {
7150
    cs_md5_update(&md5_ctx, msgs[i], msg_lens[i]);
7151
  }
7152
  cs_md5_final(digest, &md5_ctx);
7153
}
7154
#else
7155
extern void mg_hash_md5_v(size_t num_msgs, const uint8_t *msgs[],
7156
                          const size_t *msg_lens, uint8_t *digest);
7157
#endif
7158

    
7159
void cs_md5(char buf[33], ...) {
7160
  unsigned char hash[16];
7161
  const uint8_t *msgs[20], *p;
7162
  size_t msg_lens[20];
7163
  size_t num_msgs = 0;
7164
  va_list ap;
7165

    
7166
  va_start(ap, buf);
7167
  while ((p = va_arg(ap, const unsigned char *) ) != NULL) {
7168
    msgs[num_msgs] = p;
7169
    msg_lens[num_msgs] = va_arg(ap, size_t);
7170
    num_msgs++;
7171
  }
7172
  va_end(ap);
7173

    
7174
  mg_hash_md5_v(num_msgs, msgs, msg_lens, hash);
7175
  cs_to_hex(buf, hash, sizeof(hash));
7176
}
7177

    
7178
static void mg_mkmd5resp(const char *method, size_t method_len, const char *uri,
7179
                         size_t uri_len, const char *ha1, size_t ha1_len,
7180
                         const char *nonce, size_t nonce_len, const char *nc,
7181
                         size_t nc_len, const char *cnonce, size_t cnonce_len,
7182
                         const char *qop, size_t qop_len, char *resp) {
7183
  static const char colon[] = ":";
7184
  static const size_t one = 1;
7185
  char ha2[33];
7186
  cs_md5(ha2, method, method_len, colon, one, uri, uri_len, NULL);
7187
  cs_md5(resp, ha1, ha1_len, colon, one, nonce, nonce_len, colon, one, nc,
7188
         nc_len, colon, one, cnonce, cnonce_len, colon, one, qop, qop_len,
7189
         colon, one, ha2, sizeof(ha2) - 1, NULL);
7190
}
7191

    
7192
int mg_http_create_digest_auth_header(char *buf, size_t buf_len,
7193
                                      const char *method, const char *uri,
7194
                                      const char *auth_domain, const char *user,
7195
                                      const char *passwd, const char *nonce) {
7196
  static const char colon[] = ":", qop[] = "auth";
7197
  static const size_t one = 1;
7198
  char ha1[33], resp[33], cnonce[40];
7199

    
7200
  snprintf(cnonce, sizeof(cnonce), "%lx", (unsigned long) mg_time());
7201
  cs_md5(ha1, user, (size_t) strlen(user), colon, one, auth_domain,
7202
         (size_t) strlen(auth_domain), colon, one, passwd,
7203
         (size_t) strlen(passwd), NULL);
7204
  mg_mkmd5resp(method, strlen(method), uri, strlen(uri), ha1, sizeof(ha1) - 1,
7205
               nonce, strlen(nonce), "1", one, cnonce, strlen(cnonce), qop,
7206
               sizeof(qop) - 1, resp);
7207
  return snprintf(buf, buf_len,
7208
                  "Authorization: Digest username=\"%s\","
7209
                  "realm=\"%s\",uri=\"%s\",qop=%s,nc=1,cnonce=%s,"
7210
                  "nonce=%s,response=%s\r\n",
7211
                  user, auth_domain, uri, qop, cnonce, nonce, resp);
7212
}
7213

    
7214
/*
7215
 * Check for authentication timeout.
7216
 * Clients send time stamp encoded in nonce. Make sure it is not too old,
7217
 * to prevent replay attacks.
7218
 * Assumption: nonce is a hexadecimal number of seconds since 1970.
7219
 */
7220
static int mg_check_nonce(const char *nonce) {
7221
  unsigned long now = (unsigned long) mg_time();
7222
  unsigned long val = (unsigned long) strtoul(nonce, NULL, 16);
7223
  return (now >= val) && (now - val < 60 * 60);
7224
}
7225

    
7226
int mg_http_check_digest_auth(struct http_message *hm, const char *auth_domain,
7227
                              FILE *fp) {
7228
  int ret = 0;
7229
  struct mg_str *hdr;
7230
  char username_buf[50], cnonce_buf[64], response_buf[40], uri_buf[200],
7231
      qop_buf[20], nc_buf[20], nonce_buf[16];
7232

    
7233
  char *username = username_buf, *cnonce = cnonce_buf, *response = response_buf,
7234
       *uri = uri_buf, *qop = qop_buf, *nc = nc_buf, *nonce = nonce_buf;
7235

    
7236
  /* Parse "Authorization:" header, fail fast on parse error */
7237
  if (hm == NULL || fp == NULL ||
7238
      (hdr = mg_get_http_header(hm, "Authorization")) == NULL ||
7239
      mg_http_parse_header2(hdr, "username", &username, sizeof(username_buf)) ==
7240
          0 ||
7241
      mg_http_parse_header2(hdr, "cnonce", &cnonce, sizeof(cnonce_buf)) == 0 ||
7242
      mg_http_parse_header2(hdr, "response", &response, sizeof(response_buf)) ==
7243
          0 ||
7244
      mg_http_parse_header2(hdr, "uri", &uri, sizeof(uri_buf)) == 0 ||
7245
      mg_http_parse_header2(hdr, "qop", &qop, sizeof(qop_buf)) == 0 ||
7246
      mg_http_parse_header2(hdr, "nc", &nc, sizeof(nc_buf)) == 0 ||
7247
      mg_http_parse_header2(hdr, "nonce", &nonce, sizeof(nonce_buf)) == 0 ||
7248
      mg_check_nonce(nonce) == 0) {
7249
    ret = 0;
7250
    goto clean;
7251
  }
7252

    
7253
  /* NOTE(lsm): due to a bug in MSIE, we do not compare URIs */
7254

    
7255
  ret = mg_check_digest_auth(
7256
      hm->method,
7257
      mg_mk_str_n(
7258
          hm->uri.p,
7259
          hm->uri.len + (hm->query_string.len ? hm->query_string.len + 1 : 0)),
7260
      mg_mk_str(username), mg_mk_str(cnonce), mg_mk_str(response),
7261
      mg_mk_str(qop), mg_mk_str(nc), mg_mk_str(nonce), mg_mk_str(auth_domain),
7262
      fp);
7263

    
7264
clean:
7265
  if (username != username_buf) MG_FREE(username);
7266
  if (cnonce != cnonce_buf) MG_FREE(cnonce);
7267
  if (response != response_buf) MG_FREE(response);
7268
  if (uri != uri_buf) MG_FREE(uri);
7269
  if (qop != qop_buf) MG_FREE(qop);
7270
  if (nc != nc_buf) MG_FREE(nc);
7271
  if (nonce != nonce_buf) MG_FREE(nonce);
7272

    
7273
  return ret;
7274
}
7275

    
7276
int mg_check_digest_auth(struct mg_str method, struct mg_str uri,
7277
                         struct mg_str username, struct mg_str cnonce,
7278
                         struct mg_str response, struct mg_str qop,
7279
                         struct mg_str nc, struct mg_str nonce,
7280
                         struct mg_str auth_domain, FILE *fp) {
7281
  char buf[128], f_user[sizeof(buf)], f_ha1[sizeof(buf)], f_domain[sizeof(buf)];
7282
  char expected_response[33];
7283

    
7284
  /*
7285
   * Read passwords file line by line. If should have htdigest format,
7286
   * i.e. each line should be a colon-separated sequence:
7287
   * USER_NAME:DOMAIN_NAME:HA1_HASH_OF_USER_DOMAIN_AND_PASSWORD
7288
   */
7289
  while (fgets(buf, sizeof(buf), fp) != NULL) {
7290
    if (sscanf(buf, "%[^:]:%[^:]:%s", f_user, f_domain, f_ha1) == 3 &&
7291
        mg_vcmp(&username, f_user) == 0 &&
7292
        mg_vcmp(&auth_domain, f_domain) == 0) {
7293
      /* Username and domain matched, check the password */
7294
      mg_mkmd5resp(method.p, method.len, uri.p, uri.len, f_ha1, strlen(f_ha1),
7295
                   nonce.p, nonce.len, nc.p, nc.len, cnonce.p, cnonce.len,
7296
                   qop.p, qop.len, expected_response);
7297
      LOG(LL_DEBUG,
7298
          ("%.*s %s %.*s %s", (int) username.len, username.p, f_domain,
7299
           (int) response.len, response.p, expected_response));
7300
      return mg_ncasecmp(response.p, expected_response, response.len) == 0;
7301
    }
7302
  }
7303

    
7304
  /* None of the entries in the passwords file matched - return failure */
7305
  return 0;
7306
}
7307

    
7308
int mg_http_is_authorized(struct http_message *hm, struct mg_str path,
7309
                          const char *domain, const char *passwords_file,
7310
                          int flags) {
7311
  char buf[MG_MAX_PATH];
7312
  const char *p;
7313
  FILE *fp;
7314
  int authorized = 1;
7315

    
7316
  if (domain != NULL && passwords_file != NULL) {
7317
    if (flags & MG_AUTH_FLAG_IS_GLOBAL_PASS_FILE) {
7318
      fp = mg_fopen(passwords_file, "r");
7319
    } else if (flags & MG_AUTH_FLAG_IS_DIRECTORY) {
7320
      snprintf(buf, sizeof(buf), "%.*s%c%s", (int) path.len, path.p, DIRSEP,
7321
               passwords_file);
7322
      fp = mg_fopen(buf, "r");
7323
    } else {
7324
      p = strrchr(path.p, DIRSEP);
7325
      if (p == NULL) p = path.p;
7326
      snprintf(buf, sizeof(buf), "%.*s%c%s", (int) (p - path.p), path.p, DIRSEP,
7327
               passwords_file);
7328
      fp = mg_fopen(buf, "r");
7329
    }
7330

    
7331
    if (fp != NULL) {
7332
      authorized = mg_http_check_digest_auth(hm, domain, fp);
7333
      fclose(fp);
7334
    } else if (!(flags & MG_AUTH_FLAG_ALLOW_MISSING_FILE)) {
7335
      authorized = 0;
7336
    }
7337
  }
7338

    
7339
  LOG(LL_DEBUG, ("%.*s %s %x %d", (int) path.len, path.p,
7340
                 passwords_file ? passwords_file : "", flags, authorized));
7341
  return authorized;
7342
}
7343
#else
7344
int mg_http_is_authorized(struct http_message *hm, const struct mg_str path,
7345
                          const char *domain, const char *passwords_file,
7346
                          int flags) {
7347
  (void) hm;
7348
  (void) path;
7349
  (void) domain;
7350
  (void) passwords_file;
7351
  (void) flags;
7352
  return 1;
7353
}
7354
#endif
7355

    
7356
#if MG_ENABLE_DIRECTORY_LISTING
7357
static void mg_escape(const char *src, char *dst, size_t dst_len) {
7358
  size_t n = 0;
7359
  while (*src != '\0' && n + 5 < dst_len) {
7360
    unsigned char ch = *(unsigned char *) src++;
7361
    if (ch == '<') {
7362
      n += snprintf(dst + n, dst_len - n, "%s", "&lt;");
7363
    } else {
7364
      dst[n++] = ch;
7365
    }
7366
  }
7367
  dst[n] = '\0';
7368
}
7369

    
7370
static void mg_print_dir_entry(struct mg_connection *nc, const char *file_name,
7371
                               cs_stat_t *stp) {
7372
  char size[64], mod[64], path[MG_MAX_PATH];
7373
  int64_t fsize = stp->st_size;
7374
  int is_dir = S_ISDIR(stp->st_mode);
7375
  const char *slash = is_dir ? "/" : "";
7376
  struct mg_str href;
7377

    
7378
  if (is_dir) {
7379
    snprintf(size, sizeof(size), "%s", "[DIRECTORY]");
7380
  } else {
7381
    /*
7382
     * We use (double) cast below because MSVC 6 compiler cannot
7383
     * convert unsigned __int64 to double.
7384
     */
7385
    if (fsize < 1024) {
7386
      snprintf(size, sizeof(size), "%d", (int) fsize);
7387
    } else if (fsize < 0x100000) {
7388
      snprintf(size, sizeof(size), "%.1fk", (double) fsize / 1024.0);
7389
    } else if (fsize < 0x40000000) {
7390
      snprintf(size, sizeof(size), "%.1fM", (double) fsize / 1048576);
7391
    } else {
7392
      snprintf(size, sizeof(size), "%.1fG", (double) fsize / 1073741824);
7393
    }
7394
  }
7395
  strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime(&stp->st_mtime));
7396
  mg_escape(file_name, path, sizeof(path));
7397
  href = mg_url_encode(mg_mk_str(file_name));
7398
  mg_printf_http_chunk(nc,
7399
                       "<tr><td><a href=\"%s%s\">%s%s</a></td>"
7400
                       "<td>%s</td><td name=%" INT64_FMT ">%s</td></tr>\n",
7401
                       href.p, slash, path, slash, mod, is_dir ? -1 : fsize,
7402
                       size);
7403
  free((void *) href.p);
7404
}
7405

    
7406
static void mg_scan_directory(struct mg_connection *nc, const char *dir,
7407
                              const struct mg_serve_http_opts *opts,
7408
                              void (*func)(struct mg_connection *, const char *,
7409
                                           cs_stat_t *)) {
7410
  char path[MG_MAX_PATH];
7411
  cs_stat_t st;
7412
  struct dirent *dp;
7413
  DIR *dirp;
7414

    
7415
  LOG(LL_DEBUG, ("%p [%s]", nc, dir));
7416
  if ((dirp = (opendir(dir))) != NULL) {
7417
    while ((dp = readdir(dirp)) != NULL) {
7418
      /* Do not show current dir and hidden files */
7419
      if (mg_is_file_hidden((const char *) dp->d_name, opts, 1)) {
7420
        continue;
7421
      }
7422
      snprintf(path, sizeof(path), "%s/%s", dir, dp->d_name);
7423
      if (mg_stat(path, &st) == 0) {
7424
        func(nc, (const char *) dp->d_name, &st);
7425
      }
7426
    }
7427
    closedir(dirp);
7428
  } else {
7429
    LOG(LL_DEBUG, ("%p opendir(%s) -> %d", nc, dir, mg_get_errno()));
7430
  }
7431
}
7432

    
7433
static void mg_send_directory_listing(struct mg_connection *nc, const char *dir,
7434
                                      struct http_message *hm,
7435
                                      struct mg_serve_http_opts *opts) {
7436
  static const char *sort_js_code =
7437
      "<script>function srt(tb, sc, so, d) {"
7438
      "var tr = Array.prototype.slice.call(tb.rows, 0),"
7439
      "tr = tr.sort(function (a, b) { var c1 = a.cells[sc], c2 = b.cells[sc],"
7440
      "n1 = c1.getAttribute('name'), n2 = c2.getAttribute('name'), "
7441
      "t1 = a.cells[2].getAttribute('name'), "
7442
      "t2 = b.cells[2].getAttribute('name'); "
7443
      "return so * (t1 < 0 && t2 >= 0 ? -1 : t2 < 0 && t1 >= 0 ? 1 : "
7444
      "n1 ? parseInt(n2) - parseInt(n1) : "
7445
      "c1.textContent.trim().localeCompare(c2.textContent.trim())); });";
7446
  static const char *sort_js_code2 =
7447
      "for (var i = 0; i < tr.length; i++) tb.appendChild(tr[i]); "
7448
      "if (!d) window.location.hash = ('sc=' + sc + '&so=' + so); "
7449
      "};"
7450
      "window.onload = function() {"
7451
      "var tb = document.getElementById('tb');"
7452
      "var m = /sc=([012]).so=(1|-1)/.exec(window.location.hash) || [0, 2, 1];"
7453
      "var sc = m[1], so = m[2]; document.onclick = function(ev) { "
7454
      "var c = ev.target.rel; if (c) {if (c == sc) so *= -1; srt(tb, c, so); "
7455
      "sc = c; ev.preventDefault();}};"
7456
      "srt(tb, sc, so, true);"
7457
      "}"
7458
      "</script>";
7459

    
7460
  mg_send_response_line(nc, 200, opts->extra_headers);
7461
  mg_printf(nc, "%s: %s\r\n%s: %s\r\n\r\n", "Transfer-Encoding", "chunked",
7462
            "Content-Type", "text/html; charset=utf-8");
7463

    
7464
  mg_printf_http_chunk(
7465
      nc,
7466
      "<html><head><title>Index of %.*s</title>%s%s"
7467
      "<style>th,td {text-align: left; padding-right: 1em; "
7468
      "font-family: monospace; }</style></head>\n"
7469
      "<body><h1>Index of %.*s</h1>\n<table cellpadding=0><thead>"
7470
      "<tr><th><a href=# rel=0>Name</a></th><th>"
7471
      "<a href=# rel=1>Modified</a</th>"
7472
      "<th><a href=# rel=2>Size</a></th></tr>"
7473
      "<tr><td colspan=3><hr></td></tr>\n"
7474
      "</thead>\n"
7475
      "<tbody id=tb>",
7476
      (int) hm->uri.len, hm->uri.p, sort_js_code, sort_js_code2,
7477
      (int) hm->uri.len, hm->uri.p);
7478
  mg_scan_directory(nc, dir, opts, mg_print_dir_entry);
7479
  mg_printf_http_chunk(nc,
7480
                       "</tbody><tr><td colspan=3><hr></td></tr>\n"
7481
                       "</table>\n"
7482
                       "<address>%s</address>\n"
7483
                       "</body></html>",
7484
                       mg_version_header);
7485
  mg_send_http_chunk(nc, "", 0);
7486
  /* TODO(rojer): Remove when cesanta/dev/issues/197 is fixed. */
7487
  nc->flags |= MG_F_SEND_AND_CLOSE;
7488
}
7489
#endif /* MG_ENABLE_DIRECTORY_LISTING */
7490

    
7491
/*
7492
 * Given a directory path, find one of the files specified in the
7493
 * comma-separated list of index files `list`.
7494
 * First found index file wins. If an index file is found, then gets
7495
 * appended to the `path`, stat-ed, and result of `stat()` passed to `stp`.
7496
 * If index file is not found, then `path` and `stp` remain unchanged.
7497
 */
7498
MG_INTERNAL void mg_find_index_file(const char *path, const char *list,
7499
                                    char **index_file, cs_stat_t *stp) {
7500
  struct mg_str vec;
7501
  size_t path_len = strlen(path);
7502
  int found = 0;
7503
  *index_file = NULL;
7504

    
7505
  /* Traverse index files list. For each entry, append it to the given */
7506
  /* path and see if the file exists. If it exists, break the loop */
7507
  while ((list = mg_next_comma_list_entry(list, &vec, NULL)) != NULL) {
7508
    cs_stat_t st;
7509
    size_t len = path_len + 1 + vec.len + 1;
7510
    *index_file = (char *) MG_REALLOC(*index_file, len);
7511
    if (*index_file == NULL) break;
7512
    snprintf(*index_file, len, "%s%c%.*s", path, DIRSEP, (int) vec.len, vec.p);
7513

    
7514
    /* Does it exist? Is it a file? */
7515
    if (mg_stat(*index_file, &st) == 0 && S_ISREG(st.st_mode)) {
7516
      /* Yes it does, break the loop */
7517
      *stp = st;
7518
      found = 1;
7519
      break;
7520
    }
7521
  }
7522
  if (!found) {
7523
    MG_FREE(*index_file);
7524
    *index_file = NULL;
7525
  }
7526
  LOG(LL_DEBUG, ("[%s] [%s]", path, (*index_file ? *index_file : "")));
7527
}
7528

    
7529
#if MG_ENABLE_HTTP_URL_REWRITES
7530
static int mg_http_send_port_based_redirect(
7531
    struct mg_connection *c, struct http_message *hm,
7532
    const struct mg_serve_http_opts *opts) {
7533
  const char *rewrites = opts->url_rewrites;
7534
  struct mg_str a, b;
7535
  char local_port[20] = {'%'};
7536

    
7537
  mg_conn_addr_to_str(c, local_port + 1, sizeof(local_port) - 1,
7538
                      MG_SOCK_STRINGIFY_PORT);
7539

    
7540
  while ((rewrites = mg_next_comma_list_entry(rewrites, &a, &b)) != NULL) {
7541
    if (mg_vcmp(&a, local_port) == 0) {
7542
      mg_send_response_line(c, 301, NULL);
7543
      mg_printf(c, "Content-Length: 0\r\nLocation: %.*s%.*s\r\n\r\n",
7544
                (int) b.len, b.p, (int) (hm->proto.p - hm->uri.p - 1),
7545
                hm->uri.p);
7546
      return 1;
7547
    }
7548
  }
7549

    
7550
  return 0;
7551
}
7552

    
7553
static void mg_reverse_proxy_handler(struct mg_connection *nc, int ev,
7554
                                     void *ev_data MG_UD_ARG(void *user_data)) {
7555
  struct http_message *hm = (struct http_message *) ev_data;
7556
  struct mg_http_proto_data *pd = mg_http_get_proto_data(nc);
7557

    
7558
  if (pd == NULL || pd->reverse_proxy_data.linked_conn == NULL) {
7559
    DBG(("%p: upstream closed", nc));
7560
    return;
7561
  }
7562

    
7563
  switch (ev) {
7564
    case MG_EV_CONNECT:
7565
      if (*(int *) ev_data != 0) {
7566
        mg_http_send_error(pd->reverse_proxy_data.linked_conn, 502, NULL);
7567
      }
7568
      break;
7569
    /* TODO(mkm): handle streaming */
7570
    case MG_EV_HTTP_REPLY:
7571
      mg_send(pd->reverse_proxy_data.linked_conn, hm->message.p,
7572
              hm->message.len);
7573
      pd->reverse_proxy_data.linked_conn->flags |= MG_F_SEND_AND_CLOSE;
7574
      nc->flags |= MG_F_CLOSE_IMMEDIATELY;
7575
      break;
7576
    case MG_EV_CLOSE:
7577
      pd->reverse_proxy_data.linked_conn->flags |= MG_F_SEND_AND_CLOSE;
7578
      break;
7579
  }
7580

    
7581
#if MG_ENABLE_CALLBACK_USERDATA
7582
  (void) user_data;
7583
#endif
7584
}
7585

    
7586
void mg_http_reverse_proxy(struct mg_connection *nc,
7587
                           const struct http_message *hm, struct mg_str mount,
7588
                           struct mg_str upstream) {
7589
  struct mg_connection *be;
7590
  char burl[256], *purl = burl;
7591
  int i;
7592
  const char *error;
7593
  struct mg_connect_opts opts;
7594
  struct mg_str path = MG_NULL_STR, user_info = MG_NULL_STR, host = MG_NULL_STR;
7595
  memset(&opts, 0, sizeof(opts));
7596
  opts.error_string = &error;
7597

    
7598
  mg_asprintf(&purl, sizeof(burl), "%.*s%.*s", (int) upstream.len, upstream.p,
7599
              (int) (hm->uri.len - mount.len), hm->uri.p + mount.len);
7600

    
7601
  be = mg_connect_http_base(nc->mgr, MG_CB(mg_reverse_proxy_handler, NULL),
7602
                            opts, "http", NULL, "https", NULL, purl, &path,
7603
                            &user_info, &host);
7604
  LOG(LL_DEBUG, ("Proxying %.*s to %s (rule: %.*s)", (int) hm->uri.len,
7605
                 hm->uri.p, purl, (int) mount.len, mount.p));
7606

    
7607
  if (be == NULL) {
7608
    LOG(LL_ERROR, ("Error connecting to %s: %s", purl, error));
7609
    mg_http_send_error(nc, 502, NULL);
7610
    goto cleanup;
7611
  }
7612

    
7613
  /* link connections to each other, they must live and die together */
7614
  mg_http_get_proto_data(be)->reverse_proxy_data.linked_conn = nc;
7615
  mg_http_get_proto_data(nc)->reverse_proxy_data.linked_conn = be;
7616

    
7617
  /* send request upstream */
7618
  mg_printf(be, "%.*s %.*s HTTP/1.1\r\n", (int) hm->method.len, hm->method.p,
7619
            (int) path.len, path.p);
7620

    
7621
  mg_printf(be, "Host: %.*s\r\n", (int) host.len, host.p);
7622
  for (i = 0; i < MG_MAX_HTTP_HEADERS && hm->header_names[i].len > 0; i++) {
7623
    struct mg_str hn = hm->header_names[i];
7624
    struct mg_str hv = hm->header_values[i];
7625

    
7626
    /* we rewrite the host header */
7627
    if (mg_vcasecmp(&hn, "Host") == 0) continue;
7628
    /*
7629
     * Don't pass chunked transfer encoding to the client because hm->body is
7630
     * already dechunked when we arrive here.
7631
     */
7632
    if (mg_vcasecmp(&hn, "Transfer-encoding") == 0 &&
7633
        mg_vcasecmp(&hv, "chunked") == 0) {
7634
      mg_printf(be, "Content-Length: %" SIZE_T_FMT "\r\n", hm->body.len);
7635
      continue;
7636
    }
7637
    /* We don't support proxying Expect: 100-continue. */
7638
    if (mg_vcasecmp(&hn, "Expect") == 0 &&
7639
        mg_vcasecmp(&hv, "100-continue") == 0) {
7640
      continue;
7641
    }
7642

    
7643
    mg_printf(be, "%.*s: %.*s\r\n", (int) hn.len, hn.p, (int) hv.len, hv.p);
7644
  }
7645

    
7646
  mg_send(be, "\r\n", 2);
7647
  mg_send(be, hm->body.p, hm->body.len);
7648

    
7649
cleanup:
7650
  if (purl != burl) MG_FREE(purl);
7651
}
7652

    
7653
static int mg_http_handle_forwarding(struct mg_connection *nc,
7654
                                     struct http_message *hm,
7655
                                     const struct mg_serve_http_opts *opts) {
7656
  const char *rewrites = opts->url_rewrites;
7657
  struct mg_str a, b;
7658
  struct mg_str p1 = MG_MK_STR("http://"), p2 = MG_MK_STR("https://");
7659

    
7660
  while ((rewrites = mg_next_comma_list_entry(rewrites, &a, &b)) != NULL) {
7661
    if (mg_strncmp(a, hm->uri, a.len) == 0) {
7662
      if (mg_strncmp(b, p1, p1.len) == 0 || mg_strncmp(b, p2, p2.len) == 0) {
7663
        mg_http_reverse_proxy(nc, hm, a, b);
7664
        return 1;
7665
      }
7666
    }
7667
  }
7668

    
7669
  return 0;
7670
}
7671
#endif /* MG_ENABLE_FILESYSTEM */
7672

    
7673
MG_INTERNAL int mg_uri_to_local_path(struct http_message *hm,
7674
                                     const struct mg_serve_http_opts *opts,
7675
                                     char **local_path,
7676
                                     struct mg_str *remainder) {
7677
  int ok = 1;
7678
  const char *cp = hm->uri.p, *cp_end = hm->uri.p + hm->uri.len;
7679
  struct mg_str root = {NULL, 0};
7680
  const char *file_uri_start = cp;
7681
  *local_path = NULL;
7682
  remainder->p = NULL;
7683
  remainder->len = 0;
7684

    
7685
  { /* 1. Determine which root to use. */
7686

    
7687
#if MG_ENABLE_HTTP_URL_REWRITES
7688
    const char *rewrites = opts->url_rewrites;
7689
#else
7690
    const char *rewrites = "";
7691
#endif
7692
    struct mg_str *hh = mg_get_http_header(hm, "Host");
7693
    struct mg_str a, b;
7694
    /* Check rewrites first. */
7695
    while ((rewrites = mg_next_comma_list_entry(rewrites, &a, &b)) != NULL) {
7696
      if (a.len > 1 && a.p[0] == '@') {
7697
        /* Host rewrite. */
7698
        if (hh != NULL && hh->len == a.len - 1 &&
7699
            mg_ncasecmp(a.p + 1, hh->p, a.len - 1) == 0) {
7700
          root = b;
7701
          break;
7702
        }
7703
      } else {
7704
        /* Regular rewrite, URI=directory */
7705
        size_t match_len = mg_match_prefix_n(a, hm->uri);
7706
        if (match_len > 0) {
7707
          file_uri_start = hm->uri.p + match_len;
7708
          if (*file_uri_start == '/' || file_uri_start == cp_end) {
7709
            /* Match ended at component boundary, ok. */
7710
          } else if (*(file_uri_start - 1) == '/') {
7711
            /* Pattern ends with '/', backtrack. */
7712
            file_uri_start--;
7713
          } else {
7714
            /* No match: must fall on the component boundary. */
7715
            continue;
7716
          }
7717
          root = b;
7718
          break;
7719
        }
7720
      }
7721
    }
7722
    /* If no rewrite rules matched, use DAV or regular document root. */
7723
    if (root.p == NULL) {
7724
#if MG_ENABLE_HTTP_WEBDAV
7725
      if (opts->dav_document_root != NULL && mg_is_dav_request(&hm->method)) {
7726
        root.p = opts->dav_document_root;
7727
        root.len = strlen(opts->dav_document_root);
7728
      } else
7729
#endif
7730
      {
7731
        root.p = opts->document_root;
7732
        root.len = strlen(opts->document_root);
7733
      }
7734
    }
7735
    assert(root.p != NULL && root.len > 0);
7736
  }
7737

    
7738
  { /* 2. Find where in the canonical URI path the local path ends. */
7739
    const char *u = file_uri_start + 1;
7740
    char *lp = (char *) MG_MALLOC(root.len + hm->uri.len + 1);
7741
    char *lp_end = lp + root.len + hm->uri.len + 1;
7742
    char *p = lp, *ps;
7743
    int exists = 1;
7744
    if (lp == NULL) {
7745
      ok = 0;
7746
      goto out;
7747
    }
7748
    memcpy(p, root.p, root.len);
7749
    p += root.len;
7750
    if (*(p - 1) == DIRSEP) p--;
7751
    *p = '\0';
7752
    ps = p;
7753

    
7754
    /* Chop off URI path components one by one and build local path. */
7755
    while (u <= cp_end) {
7756
      const char *next = u;
7757
      struct mg_str component;
7758
      if (exists) {
7759
        cs_stat_t st;
7760
        exists = (mg_stat(lp, &st) == 0);
7761
        if (exists && S_ISREG(st.st_mode)) {
7762
          /* We found the terminal, the rest of the URI (if any) is path_info.
7763
           */
7764
          if (*(u - 1) == '/') u--;
7765
          break;
7766
        }
7767
      }
7768
      if (u >= cp_end) break;
7769
      parse_uri_component((const char **) &next, cp_end, "/", &component);
7770
      if (component.len > 0) {
7771
        int len;
7772
        memmove(p + 1, component.p, component.len);
7773
        len = mg_url_decode(p + 1, component.len, p + 1, lp_end - p - 1, 0);
7774
        if (len <= 0) {
7775
          ok = 0;
7776
          break;
7777
        }
7778
        component.p = p + 1;
7779
        component.len = len;
7780
        if (mg_vcmp(&component, ".") == 0) {
7781
          /* Yum. */
7782
        } else if (mg_vcmp(&component, "..") == 0) {
7783
          while (p > ps && *p != DIRSEP) p--;
7784
          *p = '\0';
7785
        } else {
7786
          size_t i;
7787
#ifdef _WIN32
7788
          /* On Windows, make sure it's valid Unicode (no funny stuff). */
7789
          wchar_t buf[MG_MAX_PATH * 2];
7790
          if (to_wchar(component.p, buf, MG_MAX_PATH) == 0) {
7791
            DBG(("[%.*s] smells funny", (int) component.len, component.p));
7792
            ok = 0;
7793
            break;
7794
          }
7795
#endif
7796
          *p++ = DIRSEP;
7797
          /* No NULs and DIRSEPs in the component (percent-encoded). */
7798
          for (i = 0; i < component.len; i++, p++) {
7799
            if (*p == '\0' || *p == DIRSEP
7800
#ifdef _WIN32
7801
                /* On Windows, "/" is also accepted, so check for that too. */
7802
                ||
7803
                *p == '/'
7804
#endif
7805
                ) {
7806
              ok = 0;
7807
              break;
7808
            }
7809
          }
7810
        }
7811
      }
7812
      u = next;
7813
    }
7814
    if (ok) {
7815
      *local_path = lp;
7816
      if (u > cp_end) u = cp_end;
7817
      remainder->p = u;
7818
      remainder->len = cp_end - u;
7819
    } else {
7820
      MG_FREE(lp);
7821
    }
7822
  }
7823

    
7824
out:
7825
  LOG(LL_DEBUG,
7826
      ("'%.*s' -> '%s' + '%.*s'", (int) hm->uri.len, hm->uri.p,
7827
       *local_path ? *local_path : "", (int) remainder->len, remainder->p));
7828
  return ok;
7829
}
7830

    
7831
static int mg_get_month_index(const char *s) {
7832
  static const char *month_names[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
7833
                                      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
7834
  size_t i;
7835

    
7836
  for (i = 0; i < ARRAY_SIZE(month_names); i++)
7837
    if (!strcmp(s, month_names[i])) return (int) i;
7838

    
7839
  return -1;
7840
}
7841

    
7842
static int mg_num_leap_years(int year) {
7843
  return year / 4 - year / 100 + year / 400;
7844
}
7845

    
7846
/* Parse UTC date-time string, and return the corresponding time_t value. */
7847
MG_INTERNAL time_t mg_parse_date_string(const char *datetime) {
7848
  static const unsigned short days_before_month[] = {
7849
      0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
7850
  char month_str[32];
7851
  int second, minute, hour, day, month, year, leap_days, days;
7852
  time_t result = (time_t) 0;
7853

    
7854
  if (((sscanf(datetime, "%d/%3s/%d %d:%d:%d", &day, month_str, &year, &hour,
7855
               &minute, &second) == 6) ||
7856
       (sscanf(datetime, "%d %3s %d %d:%d:%d", &day, month_str, &year, &hour,
7857
               &minute, &second) == 6) ||
7858
       (sscanf(datetime, "%*3s, %d %3s %d %d:%d:%d", &day, month_str, &year,
7859
               &hour, &minute, &second) == 6) ||
7860
       (sscanf(datetime, "%d-%3s-%d %d:%d:%d", &day, month_str, &year, &hour,
7861
               &minute, &second) == 6)) &&
7862
      year > 1970 && (month = mg_get_month_index(month_str)) != -1) {
7863
    leap_days = mg_num_leap_years(year) - mg_num_leap_years(1970);
7864
    year -= 1970;
7865
    days = year * 365 + days_before_month[month] + (day - 1) + leap_days;
7866
    result = days * 24 * 3600 + hour * 3600 + minute * 60 + second;
7867
  }
7868

    
7869
  return result;
7870
}
7871

    
7872
MG_INTERNAL int mg_is_not_modified(struct http_message *hm, cs_stat_t *st) {
7873
  struct mg_str *hdr;
7874
  if ((hdr = mg_get_http_header(hm, "If-None-Match")) != NULL) {
7875
    char etag[64];
7876
    mg_http_construct_etag(etag, sizeof(etag), st);
7877
    return mg_vcasecmp(hdr, etag) == 0;
7878
  } else if ((hdr = mg_get_http_header(hm, "If-Modified-Since")) != NULL) {
7879
    return st->st_mtime <= mg_parse_date_string(hdr->p);
7880
  } else {
7881
    return 0;
7882
  }
7883
}
7884

    
7885
void mg_http_send_digest_auth_request(struct mg_connection *c,
7886
                                      const char *domain) {
7887
  mg_printf(c,
7888
            "HTTP/1.1 401 Unauthorized\r\n"
7889
            "WWW-Authenticate: Digest qop=\"auth\", "
7890
            "realm=\"%s\", nonce=\"%lx\"\r\n"
7891
            "Content-Length: 0\r\n\r\n",
7892
            domain, (unsigned long) mg_time());
7893
}
7894

    
7895
static void mg_http_send_options(struct mg_connection *nc) {
7896
  mg_printf(nc, "%s",
7897
            "HTTP/1.1 200 OK\r\nAllow: GET, POST, HEAD, CONNECT, OPTIONS"
7898
#if MG_ENABLE_HTTP_WEBDAV
7899
            ", MKCOL, PUT, DELETE, PROPFIND, MOVE\r\nDAV: 1,2"
7900
#endif
7901
            "\r\n\r\n");
7902
  nc->flags |= MG_F_SEND_AND_CLOSE;
7903
}
7904

    
7905
static int mg_is_creation_request(const struct http_message *hm) {
7906
  return mg_vcmp(&hm->method, "MKCOL") == 0 || mg_vcmp(&hm->method, "PUT") == 0;
7907
}
7908

    
7909
MG_INTERNAL void mg_send_http_file(struct mg_connection *nc, char *path,
7910
                                   const struct mg_str *path_info,
7911
                                   struct http_message *hm,
7912
                                   struct mg_serve_http_opts *opts) {
7913
  int exists, is_directory, is_cgi;
7914
#if MG_ENABLE_HTTP_WEBDAV
7915
  int is_dav = mg_is_dav_request(&hm->method);
7916
#else
7917
  int is_dav = 0;
7918
#endif
7919
  char *index_file = NULL;
7920
  cs_stat_t st;
7921

    
7922
  exists = (mg_stat(path, &st) == 0);
7923
  is_directory = exists && S_ISDIR(st.st_mode);
7924

    
7925
  if (is_directory)
7926
    mg_find_index_file(path, opts->index_files, &index_file, &st);
7927

    
7928
  is_cgi =
7929
      (mg_match_prefix(opts->cgi_file_pattern, strlen(opts->cgi_file_pattern),
7930
                       index_file ? index_file : path) > 0);
7931

    
7932
  LOG(LL_DEBUG,
7933
      ("%p %.*s [%s] exists=%d is_dir=%d is_dav=%d is_cgi=%d index=%s", nc,
7934
       (int) hm->method.len, hm->method.p, path, exists, is_directory, is_dav,
7935
       is_cgi, index_file ? index_file : ""));
7936

    
7937
  if (is_directory && hm->uri.p[hm->uri.len - 1] != '/' && !is_dav) {
7938
    mg_printf(nc,
7939
              "HTTP/1.1 301 Moved\r\nLocation: %.*s/\r\n"
7940
              "Content-Length: 0\r\n\r\n",
7941
              (int) hm->uri.len, hm->uri.p);
7942
    MG_FREE(index_file);
7943
    return;
7944
  }
7945

    
7946
  /* If we have path_info, the only way to handle it is CGI. */
7947
  if (path_info->len > 0 && !is_cgi) {
7948
    mg_http_send_error(nc, 501, NULL);
7949
    MG_FREE(index_file);
7950
    return;
7951
  }
7952

    
7953
  if (is_dav && opts->dav_document_root == NULL) {
7954
    mg_http_send_error(nc, 501, NULL);
7955
  } else if (!mg_http_is_authorized(
7956
                 hm, mg_mk_str(path), opts->auth_domain, opts->global_auth_file,
7957
                 ((is_directory ? MG_AUTH_FLAG_IS_DIRECTORY : 0) |
7958
                  MG_AUTH_FLAG_IS_GLOBAL_PASS_FILE |
7959
                  MG_AUTH_FLAG_ALLOW_MISSING_FILE)) ||
7960
             !mg_http_is_authorized(
7961
                 hm, mg_mk_str(path), opts->auth_domain,
7962
                 opts->per_directory_auth_file,
7963
                 ((is_directory ? MG_AUTH_FLAG_IS_DIRECTORY : 0) |
7964
                  MG_AUTH_FLAG_ALLOW_MISSING_FILE))) {
7965
    mg_http_send_digest_auth_request(nc, opts->auth_domain);
7966
  } else if (is_cgi) {
7967
#if MG_ENABLE_HTTP_CGI
7968
    mg_handle_cgi(nc, index_file ? index_file : path, path_info, hm, opts);
7969
#else
7970
    mg_http_send_error(nc, 501, NULL);
7971
#endif /* MG_ENABLE_HTTP_CGI */
7972
  } else if ((!exists ||
7973
              mg_is_file_hidden(path, opts, 0 /* specials are ok */)) &&
7974
             !mg_is_creation_request(hm)) {
7975
    mg_http_send_error(nc, 404, NULL);
7976
#if MG_ENABLE_HTTP_WEBDAV
7977
  } else if (!mg_vcmp(&hm->method, "PROPFIND")) {
7978
    mg_handle_propfind(nc, path, &st, hm, opts);
7979
#if !MG_DISABLE_DAV_AUTH
7980
  } else if (is_dav &&
7981
             (opts->dav_auth_file == NULL ||
7982
              (strcmp(opts->dav_auth_file, "-") != 0 &&
7983
               !mg_http_is_authorized(
7984
                   hm, mg_mk_str(path), opts->auth_domain, opts->dav_auth_file,
7985
                   ((is_directory ? MG_AUTH_FLAG_IS_DIRECTORY : 0) |
7986
                    MG_AUTH_FLAG_IS_GLOBAL_PASS_FILE |
7987
                    MG_AUTH_FLAG_ALLOW_MISSING_FILE))))) {
7988
    mg_http_send_digest_auth_request(nc, opts->auth_domain);
7989
#endif
7990
  } else if (!mg_vcmp(&hm->method, "MKCOL")) {
7991
    mg_handle_mkcol(nc, path, hm);
7992
  } else if (!mg_vcmp(&hm->method, "DELETE")) {
7993
    mg_handle_delete(nc, opts, path);
7994
  } else if (!mg_vcmp(&hm->method, "PUT")) {
7995
    mg_handle_put(nc, path, hm);
7996
  } else if (!mg_vcmp(&hm->method, "MOVE")) {
7997
    mg_handle_move(nc, opts, path, hm);
7998
#if MG_ENABLE_FAKE_DAVLOCK
7999
  } else if (!mg_vcmp(&hm->method, "LOCK")) {
8000
    mg_handle_lock(nc, path);
8001
#endif
8002
#endif /* MG_ENABLE_HTTP_WEBDAV */
8003
  } else if (!mg_vcmp(&hm->method, "OPTIONS")) {
8004
    mg_http_send_options(nc);
8005
  } else if (is_directory && index_file == NULL) {
8006
#if MG_ENABLE_DIRECTORY_LISTING
8007
    if (strcmp(opts->enable_directory_listing, "yes") == 0) {
8008
      mg_send_directory_listing(nc, path, hm, opts);
8009
    } else {
8010
      mg_http_send_error(nc, 403, NULL);
8011
    }
8012
#else
8013
    mg_http_send_error(nc, 501, NULL);
8014
#endif
8015
  } else if (mg_is_not_modified(hm, &st)) {
8016
    mg_http_send_error(nc, 304, "Not Modified");
8017
  } else {
8018
    mg_http_serve_file2(nc, index_file ? index_file : path, hm, opts);
8019
  }
8020
  MG_FREE(index_file);
8021
}
8022

    
8023
void mg_serve_http(struct mg_connection *nc, struct http_message *hm,
8024
                   struct mg_serve_http_opts opts) {
8025
  char *path = NULL;
8026
  struct mg_str *hdr, path_info;
8027
  uint32_t remote_ip = ntohl(*(uint32_t *) &nc->sa.sin.sin_addr);
8028

    
8029
  if (mg_check_ip_acl(opts.ip_acl, remote_ip) != 1) {
8030
    /* Not allowed to connect */
8031
    mg_http_send_error(nc, 403, NULL);
8032
    nc->flags |= MG_F_SEND_AND_CLOSE;
8033
    return;
8034
  }
8035

    
8036
#if MG_ENABLE_HTTP_URL_REWRITES
8037
  if (mg_http_handle_forwarding(nc, hm, &opts)) {
8038
    return;
8039
  }
8040

    
8041
  if (mg_http_send_port_based_redirect(nc, hm, &opts)) {
8042
    return;
8043
  }
8044
#endif
8045

    
8046
  if (opts.document_root == NULL) {
8047
    opts.document_root = ".";
8048
  }
8049
  if (opts.per_directory_auth_file == NULL) {
8050
    opts.per_directory_auth_file = ".htpasswd";
8051
  }
8052
  if (opts.enable_directory_listing == NULL) {
8053
    opts.enable_directory_listing = "yes";
8054
  }
8055
  if (opts.cgi_file_pattern == NULL) {
8056
    opts.cgi_file_pattern = "**.cgi$|**.php$";
8057
  }
8058
  if (opts.ssi_pattern == NULL) {
8059
    opts.ssi_pattern = "**.shtml$|**.shtm$";
8060
  }
8061
  if (opts.index_files == NULL) {
8062
    opts.index_files = "index.html,index.htm,index.shtml,index.cgi,index.php";
8063
  }
8064
  /* Normalize path - resolve "." and ".." (in-place). */
8065
  if (!mg_normalize_uri_path(&hm->uri, &hm->uri)) {
8066
    mg_http_send_error(nc, 400, NULL);
8067
    return;
8068
  }
8069
  if (mg_uri_to_local_path(hm, &opts, &path, &path_info) == 0) {
8070
    mg_http_send_error(nc, 404, NULL);
8071
    return;
8072
  }
8073
  mg_send_http_file(nc, path, &path_info, hm, &opts);
8074

    
8075
  MG_FREE(path);
8076
  path = NULL;
8077

    
8078
  /* Close connection for non-keep-alive requests */
8079
  if (mg_vcmp(&hm->proto, "HTTP/1.1") != 0 ||
8080
      ((hdr = mg_get_http_header(hm, "Connection")) != NULL &&
8081
       mg_vcmp(hdr, "keep-alive") != 0)) {
8082
#if 0
8083
    nc->flags |= MG_F_SEND_AND_CLOSE;
8084
#endif
8085
  }
8086
}
8087

    
8088
#if MG_ENABLE_HTTP_STREAMING_MULTIPART
8089
void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data,
8090
                            mg_fu_fname_fn local_name_fn
8091
                                MG_UD_ARG(void *user_data)) {
8092
  switch (ev) {
8093
    case MG_EV_HTTP_PART_BEGIN: {
8094
      struct mg_http_multipart_part *mp =
8095
          (struct mg_http_multipart_part *) ev_data;
8096
      struct file_upload_state *fus =
8097
          (struct file_upload_state *) MG_CALLOC(1, sizeof(*fus));
8098
      struct mg_str lfn = local_name_fn(nc, mg_mk_str(mp->file_name));
8099
      mp->user_data = NULL;
8100
      if (lfn.p == NULL || lfn.len == 0) {
8101
        LOG(LL_ERROR, ("%p Not allowed to upload %s", nc, mp->file_name));
8102
        mg_printf(nc,
8103
                  "HTTP/1.1 403 Not Allowed\r\n"
8104
                  "Content-Type: text/plain\r\n"
8105
                  "Connection: close\r\n\r\n"
8106
                  "Not allowed to upload %s\r\n",
8107
                  mp->file_name);
8108
        nc->flags |= MG_F_SEND_AND_CLOSE;
8109
        return;
8110
      }
8111
      fus->lfn = (char *) MG_MALLOC(lfn.len + 1);
8112
      memcpy(fus->lfn, lfn.p, lfn.len);
8113
      fus->lfn[lfn.len] = '\0';
8114
      if (lfn.p != mp->file_name) MG_FREE((char *) lfn.p);
8115
      LOG(LL_DEBUG,
8116
          ("%p Receiving file %s -> %s", nc, mp->file_name, fus->lfn));
8117
      fus->fp = mg_fopen(fus->lfn, "w");
8118
      if (fus->fp == NULL) {
8119
        mg_printf(nc,
8120
                  "HTTP/1.1 500 Internal Server Error\r\n"
8121
                  "Content-Type: text/plain\r\n"
8122
                  "Connection: close\r\n\r\n");
8123
        LOG(LL_ERROR, ("Failed to open %s: %d\n", fus->lfn, mg_get_errno()));
8124
        mg_printf(nc, "Failed to open %s: %d\n", fus->lfn, mg_get_errno());
8125
        /* Do not close the connection just yet, discard remainder of the data.
8126
         * This is because at the time of writing some browsers (Chrome) fail to
8127
         * render response before all the data is sent. */
8128
      }
8129
      mp->user_data = (void *) fus;
8130
      break;
8131
    }
8132
    case MG_EV_HTTP_PART_DATA: {
8133
      struct mg_http_multipart_part *mp =
8134
          (struct mg_http_multipart_part *) ev_data;
8135
      struct file_upload_state *fus =
8136
          (struct file_upload_state *) mp->user_data;
8137
      if (fus == NULL || fus->fp == NULL) break;
8138
      if (mg_fwrite(mp->data.p, 1, mp->data.len, fus->fp) != mp->data.len) {
8139
        LOG(LL_ERROR, ("Failed to write to %s: %d, wrote %d", fus->lfn,
8140
                       mg_get_errno(), (int) fus->num_recd));
8141
        if (mg_get_errno() == ENOSPC
8142
#ifdef SPIFFS_ERR_FULL
8143
            || mg_get_errno() == SPIFFS_ERR_FULL
8144
#endif
8145
            ) {
8146
          mg_printf(nc,
8147
                    "HTTP/1.1 413 Payload Too Large\r\n"
8148
                    "Content-Type: text/plain\r\n"
8149
                    "Connection: close\r\n\r\n");
8150
          mg_printf(nc, "Failed to write to %s: no space left; wrote %d\r\n",
8151
                    fus->lfn, (int) fus->num_recd);
8152
        } else {
8153
          mg_printf(nc,
8154
                    "HTTP/1.1 500 Internal Server Error\r\n"
8155
                    "Content-Type: text/plain\r\n"
8156
                    "Connection: close\r\n\r\n");
8157
          mg_printf(nc, "Failed to write to %s: %d, wrote %d", mp->file_name,
8158
                    mg_get_errno(), (int) fus->num_recd);
8159
        }
8160
        fclose(fus->fp);
8161
        remove(fus->lfn);
8162
        fus->fp = NULL;
8163
        /* Do not close the connection just yet, discard remainder of the data.
8164
         * This is because at the time of writing some browsers (Chrome) fail to
8165
         * render response before all the data is sent. */
8166
        return;
8167
      }
8168
      fus->num_recd += mp->data.len;
8169
      LOG(LL_DEBUG, ("%p rec'd %d bytes, %d total", nc, (int) mp->data.len,
8170
                     (int) fus->num_recd));
8171
      break;
8172
    }
8173
    case MG_EV_HTTP_PART_END: {
8174
      struct mg_http_multipart_part *mp =
8175
          (struct mg_http_multipart_part *) ev_data;
8176
      struct file_upload_state *fus =
8177
          (struct file_upload_state *) mp->user_data;
8178
      if (fus == NULL) break;
8179
      if (mp->status >= 0 && fus->fp != NULL) {
8180
        LOG(LL_DEBUG, ("%p Uploaded %s (%s), %d bytes", nc, mp->file_name,
8181
                       fus->lfn, (int) fus->num_recd));
8182
        mg_printf(nc,
8183
                  "HTTP/1.1 200 OK\r\n"
8184
                  "Content-Type: text/plain\r\n"
8185
                  "Connection: close\r\n\r\n"
8186
                  "Ok, %s - %d bytes.\r\n",
8187
                  mp->file_name, (int) fus->num_recd);
8188
      } else {
8189
        LOG(LL_ERROR, ("Failed to store %s (%s)", mp->file_name, fus->lfn));
8190
        /*
8191
         * mp->status < 0 means connection was terminated, so no reason to send
8192
         * HTTP reply
8193
         */
8194
      }
8195
      if (fus->fp != NULL) fclose(fus->fp);
8196
      MG_FREE(fus->lfn);
8197
      MG_FREE(fus);
8198
      mp->user_data = NULL;
8199
      nc->flags |= MG_F_SEND_AND_CLOSE;
8200
      break;
8201
    }
8202
  }
8203

    
8204
#if MG_ENABLE_CALLBACK_USERDATA
8205
  (void) user_data;
8206
#endif
8207
}
8208

    
8209
#endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */
8210
#endif /* MG_ENABLE_FILESYSTEM */
8211

    
8212
struct mg_connection *mg_connect_http_base(
8213
    struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data),
8214
    struct mg_connect_opts opts, const char *scheme1, const char *scheme2,
8215
    const char *scheme_ssl1, const char *scheme_ssl2, const char *url,
8216
    struct mg_str *path, struct mg_str *user_info, struct mg_str *host) {
8217
  struct mg_connection *nc = NULL;
8218
  unsigned int port_i = 0;
8219
  int use_ssl = 0;
8220
  struct mg_str scheme, query, fragment;
8221
  char conn_addr_buf[2];
8222
  char *conn_addr = conn_addr_buf;
8223

    
8224
  if (mg_parse_uri(mg_mk_str(url), &scheme, user_info, host, &port_i, path,
8225
                   &query, &fragment) != 0) {
8226
    MG_SET_PTRPTR(opts.error_string, "cannot parse url");
8227
    goto out;
8228
  }
8229

    
8230
  /* If query is present, do not strip it. Pass to the caller. */
8231
  if (query.len > 0) path->len += query.len + 1;
8232

    
8233
  if (scheme.len == 0 || mg_vcmp(&scheme, scheme1) == 0 ||
8234
      (scheme2 != NULL && mg_vcmp(&scheme, scheme2) == 0)) {
8235
    use_ssl = 0;
8236
    if (port_i == 0) port_i = 80;
8237
  } else if (mg_vcmp(&scheme, scheme_ssl1) == 0 ||
8238
             (scheme2 != NULL && mg_vcmp(&scheme, scheme_ssl2) == 0)) {
8239
    use_ssl = 1;
8240
    if (port_i == 0) port_i = 443;
8241
  } else {
8242
    goto out;
8243
  }
8244

    
8245
  mg_asprintf(&conn_addr, sizeof(conn_addr_buf), "tcp://%.*s:%u",
8246
              (int) host->len, host->p, port_i);
8247
  if (conn_addr == NULL) goto out;
8248

    
8249
  LOG(LL_DEBUG, ("%s use_ssl? %d %s", url, use_ssl, conn_addr));
8250
  if (use_ssl) {
8251
#if MG_ENABLE_SSL
8252
    /*
8253
     * Schema requires SSL, but no SSL parameters were provided in opts.
8254
     * In order to maintain backward compatibility, use a faux-SSL with no
8255
     * verification.
8256
     */
8257
    if (opts.ssl_ca_cert == NULL) {
8258
      opts.ssl_ca_cert = "*";
8259
    }
8260
#else
8261
    MG_SET_PTRPTR(opts.error_string, "ssl is disabled");
8262
    goto out;
8263
#endif
8264
  }
8265

    
8266
  if ((nc = mg_connect_opt(mgr, conn_addr, MG_CB(ev_handler, user_data),
8267
                           opts)) != NULL) {
8268
    mg_set_protocol_http_websocket(nc);
8269
  }
8270

    
8271
out:
8272
  if (conn_addr != NULL && conn_addr != conn_addr_buf) MG_FREE(conn_addr);
8273
  return nc;
8274
}
8275

    
8276
struct mg_connection *mg_connect_http_opt(
8277
    struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data),
8278
    struct mg_connect_opts opts, const char *url, const char *extra_headers,
8279
    const char *post_data) {
8280
  struct mg_str user = MG_NULL_STR, null_str = MG_NULL_STR;
8281
  struct mg_str host = MG_NULL_STR, path = MG_NULL_STR;
8282
  struct mbuf auth;
8283
  struct mg_connection *nc =
8284
      mg_connect_http_base(mgr, MG_CB(ev_handler, user_data), opts, "http",
8285
                           NULL, "https", NULL, url, &path, &user, &host);
8286

    
8287
  if (nc == NULL) {
8288
    return NULL;
8289
  }
8290

    
8291
  mbuf_init(&auth, 0);
8292
  if (user.len > 0) {
8293
    mg_basic_auth_header(user, null_str, &auth);
8294
  }
8295

    
8296
  if (post_data == NULL) post_data = "";
8297
  if (extra_headers == NULL) extra_headers = "";
8298
  if (path.len == 0) path = mg_mk_str("/");
8299
  if (host.len == 0) host = mg_mk_str("");
8300

    
8301
//  mg_printf(nc, "%s %.*s HTTP/1.1\r\nHost: %.*s\r\nContent-Length: %" SIZE_T_FMT
8302
//                "\r\n%.*s%s\r\n%s",
8303
  mg_printf(nc, "%s %.*s HTTP/1.1\r\nHost: %.*s\r\nContent-Length: %d\r\n%.*s%s\r\n%s",
8304
            (post_data[0] == '\0' ? "GET" : "POST"), (int) path.len, path.p,
8305
            (int) (path.p - host.p), host.p, strlen(post_data), (int) auth.len,
8306
            (auth.buf == NULL ? "" : auth.buf), extra_headers, post_data);
8307

    
8308
  mbuf_free(&auth);
8309
  return nc;
8310
}
8311
///*WYF add*************************************/
8312
struct mg_connection *mg_connect_http_optDEL(
8313
    struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data),
8314
    struct mg_connect_opts opts, const char *url, const char *extra_headers,
8315
    const char *post_data) {
8316
  struct mg_str user = MG_NULL_STR, null_str = MG_NULL_STR;
8317
  struct mg_str host = MG_NULL_STR, path = MG_NULL_STR;
8318
  struct mbuf auth;
8319
  struct mg_connection *nc =
8320
      mg_connect_http_base(mgr, MG_CB(ev_handler, user_data), opts, "http",
8321
                           NULL, "https", NULL, url, &path, &user, &host);
8322

    
8323
  if (nc == NULL) {
8324
    return NULL;
8325
  }
8326

    
8327
  mbuf_init(&auth, 0);
8328
  if (user.len > 0) {
8329
    mg_basic_auth_header(user, null_str, &auth);
8330
  }
8331

    
8332
  if (post_data == NULL) post_data = "";
8333
  if (extra_headers == NULL) extra_headers = "";
8334
  if (path.len == 0) path = mg_mk_str("/");
8335
  if (host.len == 0) host = mg_mk_str("");
8336

    
8337
//  mg_printf(nc, "%s %.*s HTTP/1.1\r\nHost: %.*s\r\nContent-Length: %" SIZE_T_FMT
8338
//                "\r\n%.*s%s\r\n%s",
8339
//  mg_printf(nc, "%s %.*s HTTP/1.1\r\nHost: %.*s\r\nContent-Length: %" SIZE_T_FMT
8340
//                "\r\n%.*s%s\r\n%s",
8341
  mg_printf(nc, "%s %.*s HTTP/1.1\r\nHost: %.*s\r\nContent-Length: %d\r\n%.*s%s\r\n%s",
8342
            ("DELETE"), (int) path.len, path.p,
8343
            (int) (path.p - host.p), host.p, strlen(post_data), (int) auth.len,
8344
            (auth.buf == NULL ? "" : auth.buf), extra_headers, post_data);
8345

    
8346
  mbuf_free(&auth);
8347
  return nc;
8348
}
8349
struct mg_connection *mg_connect_httpDEL(
8350
    struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data),
8351
    const char *url, const char *extra_headers, const char *post_data) {
8352
  struct mg_connect_opts opts;
8353
  memset(&opts, 0, sizeof(opts));
8354
  return mg_connect_http_optDEL(mgr, MG_CB(ev_handler, user_data), opts, url,
8355
                             extra_headers, post_data);
8356
}
8357
//**WYF add*************************************/
8358

    
8359
struct mg_connection *mg_connect_http(
8360
    struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data),
8361
    const char *url, const char *extra_headers, const char *post_data) {
8362
  struct mg_connect_opts opts;
8363
  memset(&opts, 0, sizeof(opts));
8364
  return mg_connect_http_opt(mgr, MG_CB(ev_handler, user_data), opts, url,
8365
                             extra_headers, post_data);
8366
}
8367

    
8368
size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name,
8369
                          size_t var_name_len, char *file_name,
8370
                          size_t file_name_len, const char **data,
8371
                          size_t *data_len) {
8372
  static const char cd[] = "Content-Disposition: ";
8373
  size_t hl, bl, n, ll, pos, cdl = sizeof(cd) - 1;
8374
  int shl;
8375

    
8376
  if (buf == NULL || buf_len <= 0) return 0;
8377
  if ((shl = mg_http_get_request_len(buf, buf_len)) <= 0) return 0;
8378
  hl = shl;
8379
  if (buf[0] != '-' || buf[1] != '-' || buf[2] == '\n') return 0;
8380

    
8381
  /* Get boundary length */
8382
  bl = mg_get_line_len(buf, buf_len);
8383

    
8384
  /* Loop through headers, fetch variable name and file name */
8385
  var_name[0] = file_name[0] = '\0';
8386
  for (n = bl; (ll = mg_get_line_len(buf + n, hl - n)) > 0; n += ll) {
8387
    if (mg_ncasecmp(cd, buf + n, cdl) == 0) {
8388
      struct mg_str header;
8389
      header.p = buf + n + cdl;
8390
      header.len = ll - (cdl + 2);
8391
      {
8392
        char *var_name2 = var_name;
8393
        mg_http_parse_header2(&header, "name", &var_name2, var_name_len);
8394
        /* TODO: handle reallocated buffer correctly */
8395
        if (var_name2 != var_name) {
8396
          MG_FREE(var_name2);
8397
          var_name[0] = '\0';
8398
        }
8399
      }
8400
      {
8401
        char *file_name2 = file_name;
8402
        mg_http_parse_header2(&header, "filename", &file_name2, file_name_len);
8403
        /* TODO: handle reallocated buffer correctly */
8404
        if (file_name2 != file_name) {
8405
          MG_FREE(file_name2);
8406
          file_name[0] = '\0';
8407
        }
8408
      }
8409
    }
8410
  }
8411

    
8412
  /* Scan through the body, search for terminating boundary */
8413
  for (pos = hl; pos + (bl - 2) < buf_len; pos++) {
8414
    if (buf[pos] == '-' && !strncmp(buf, &buf[pos], bl - 2)) {
8415
      if (data_len != NULL) *data_len = (pos - 2) - hl;
8416
      if (data != NULL) *data = buf + hl;
8417
      return pos;
8418
    }
8419
  }
8420

    
8421
  return 0;
8422
}
8423

    
8424
void mg_register_http_endpoint_opt(struct mg_connection *nc,
8425
                                   const char *uri_path,
8426
                                   mg_event_handler_t handler,
8427
                                   struct mg_http_endpoint_opts opts) {
8428
  struct mg_http_proto_data *pd = NULL;
8429
  struct mg_http_endpoint *new_ep = NULL;
8430

    
8431
  if (nc == NULL) return;
8432
  new_ep = (struct mg_http_endpoint *) MG_CALLOC(1, sizeof(*new_ep));
8433
  if (new_ep == NULL) return;
8434

    
8435
  pd = mg_http_get_proto_data(nc);
8436
  new_ep->uri_pattern = mg_strdup(mg_mk_str(uri_path));
8437
  if (opts.auth_domain != NULL && opts.auth_file != NULL) {
8438
    new_ep->auth_domain = strdup(opts.auth_domain);
8439
    new_ep->auth_file = strdup(opts.auth_file);
8440
  }
8441
  new_ep->handler = handler;
8442
#if MG_ENABLE_CALLBACK_USERDATA
8443
  new_ep->user_data = opts.user_data;
8444
#endif
8445
  new_ep->next = pd->endpoints;
8446
  pd->endpoints = new_ep;
8447
}
8448

    
8449
static void mg_http_call_endpoint_handler(struct mg_connection *nc, int ev,
8450
                                          struct http_message *hm) {
8451
  struct mg_http_proto_data *pd = mg_http_get_proto_data(nc);
8452
  void *user_data = nc->user_data;
8453

    
8454
  if (ev == MG_EV_HTTP_REQUEST
8455
#if MG_ENABLE_HTTP_STREAMING_MULTIPART
8456
      || ev == MG_EV_HTTP_MULTIPART_REQUEST
8457
#endif
8458
      ) {
8459
    struct mg_http_endpoint *ep =
8460
        mg_http_get_endpoint_handler(nc->listener, &hm->uri);
8461
    if (ep != NULL) {
8462
#if MG_ENABLE_FILESYSTEM && !MG_DISABLE_HTTP_DIGEST_AUTH
8463
      if (!mg_http_is_authorized(hm, hm->uri, ep->auth_domain, ep->auth_file,
8464
                                 MG_AUTH_FLAG_IS_GLOBAL_PASS_FILE)) {
8465
        mg_http_send_digest_auth_request(nc, ep->auth_domain);
8466
        return;
8467
      }
8468
#endif
8469
      pd->endpoint_handler = ep->handler;
8470
#if MG_ENABLE_CALLBACK_USERDATA
8471
      user_data = ep->user_data;
8472
#endif
8473
    }
8474
  }
8475
  mg_call(nc, pd->endpoint_handler ? pd->endpoint_handler : nc->handler,
8476
          user_data, ev, hm);
8477
}
8478

    
8479
void mg_register_http_endpoint(struct mg_connection *nc, const char *uri_path,
8480
                               MG_CB(mg_event_handler_t handler,
8481
                                     void *user_data)) {
8482
  struct mg_http_endpoint_opts opts;
8483
  memset(&opts, 0, sizeof(opts));
8484
#if MG_ENABLE_CALLBACK_USERDATA
8485
  opts.user_data = user_data;
8486
#endif
8487
  mg_register_http_endpoint_opt(nc, uri_path, handler, opts);
8488
}
8489

    
8490
#endif /* MG_ENABLE_HTTP */
8491
#ifdef MG_MODULE_LINES
8492
#line 1 "mongoose/src/mg_http_cgi.c"
8493
#endif
8494
/*
8495
 * Copyright (c) 2014-2016 Cesanta Software Limited
8496
 * All rights reserved
8497
 */
8498

    
8499
#ifndef _WIN32
8500
#include <signal.h>
8501
#endif
8502

    
8503
#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_CGI
8504

    
8505
#ifndef MG_MAX_CGI_ENVIR_VARS
8506
#define MG_MAX_CGI_ENVIR_VARS 64
8507
#endif
8508

    
8509
#ifndef MG_ENV_EXPORT_TO_CGI
8510
#define MG_ENV_EXPORT_TO_CGI "MONGOOSE_CGI"
8511
#endif
8512

    
8513
#define MG_F_HTTP_CGI_PARSE_HEADERS MG_F_USER_1
8514

    
8515
/*
8516
 * This structure helps to create an environment for the spawned CGI program.
8517
 * Environment is an array of "VARIABLE=VALUE\0" ASCIIZ strings,
8518
 * last element must be NULL.
8519
 * However, on Windows there is a requirement that all these VARIABLE=VALUE\0
8520
 * strings must reside in a contiguous buffer. The end of the buffer is
8521
 * marked by two '\0' characters.
8522
 * We satisfy both worlds: we create an envp array (which is vars), all
8523
 * entries are actually pointers inside buf.
8524
 */
8525
struct mg_cgi_env_block {
8526
  struct mg_connection *nc;
8527
  char buf[MG_CGI_ENVIRONMENT_SIZE];       /* Environment buffer */
8528
  const char *vars[MG_MAX_CGI_ENVIR_VARS]; /* char *envp[] */
8529
  int len;                                 /* Space taken */
8530
  int nvars;                               /* Number of variables in envp[] */
8531
};
8532

    
8533
#ifdef _WIN32
8534
struct mg_threadparam {
8535
  sock_t s;
8536
  HANDLE hPipe;
8537
};
8538

    
8539
static int mg_wait_until_ready(sock_t sock, int for_read) {
8540
  fd_set set;
8541
  FD_ZERO(&set);
8542
  FD_SET(sock, &set);
8543
  return select(sock + 1, for_read ? &set : 0, for_read ? 0 : &set, 0, 0) == 1;
8544
}
8545

    
8546
static void *mg_push_to_stdin(void *arg) {
8547
  struct mg_threadparam *tp = (struct mg_threadparam *) arg;
8548
  int n, sent, stop = 0;
8549
  DWORD k;
8550
  char buf[BUFSIZ];
8551

    
8552
  while (!stop && mg_wait_until_ready(tp->s, 1) &&
8553
         (n = recv(tp->s, buf, sizeof(buf), 0)) > 0) {
8554
    if (n == -1 && GetLastError() == WSAEWOULDBLOCK) continue;
8555
    for (sent = 0; !stop && sent < n; sent += k) {
8556
      if (!WriteFile(tp->hPipe, buf + sent, n - sent, &k, 0)) stop = 1;
8557
    }
8558
  }
8559
  DBG(("%s", "FORWARED EVERYTHING TO CGI"));
8560
  CloseHandle(tp->hPipe);
8561
  MG_FREE(tp);
8562
  return NULL;
8563
}
8564

    
8565
static void *mg_pull_from_stdout(void *arg) {
8566
  struct mg_threadparam *tp = (struct mg_threadparam *) arg;
8567
  int k = 0, stop = 0;
8568
  DWORD n, sent;
8569
  char buf[BUFSIZ];
8570

    
8571
  while (!stop && ReadFile(tp->hPipe, buf, sizeof(buf), &n, NULL)) {
8572
    for (sent = 0; !stop && sent < n; sent += k) {
8573
      if (mg_wait_until_ready(tp->s, 0) &&
8574
          (k = send(tp->s, buf + sent, n - sent, 0)) <= 0)
8575
        stop = 1;
8576
    }
8577
  }
8578
  DBG(("%s", "EOF FROM CGI"));
8579
  CloseHandle(tp->hPipe);
8580
  shutdown(tp->s, 2);  // Without this, IO thread may get truncated data
8581
  closesocket(tp->s);
8582
  MG_FREE(tp);
8583
  return NULL;
8584
}
8585

    
8586
static void mg_spawn_stdio_thread(sock_t sock, HANDLE hPipe,
8587
                                  void *(*func)(void *)) {
8588
  struct mg_threadparam *tp = (struct mg_threadparam *) MG_MALLOC(sizeof(*tp));
8589
  if (tp != NULL) {
8590
    tp->s = sock;
8591
    tp->hPipe = hPipe;
8592
    mg_start_thread(func, tp);
8593
  }
8594
}
8595

    
8596
static void mg_abs_path(const char *utf8_path, char *abs_path, size_t len) {
8597
  wchar_t buf[MG_MAX_PATH], buf2[MG_MAX_PATH];
8598
  to_wchar(utf8_path, buf, ARRAY_SIZE(buf));
8599
  GetFullPathNameW(buf, ARRAY_SIZE(buf2), buf2, NULL);
8600
  WideCharToMultiByte(CP_UTF8, 0, buf2, wcslen(buf2) + 1, abs_path, len, 0, 0);
8601
}
8602

    
8603
static int mg_start_process(const char *interp, const char *cmd,
8604
                            const char *env, const char *envp[],
8605
                            const char *dir, sock_t sock) {
8606
  STARTUPINFOW si;
8607
  PROCESS_INFORMATION pi;
8608
  HANDLE a[2], b[2], me = GetCurrentProcess();
8609
  wchar_t wcmd[MG_MAX_PATH], full_dir[MG_MAX_PATH];
8610
  char buf[MG_MAX_PATH], buf2[MG_MAX_PATH], buf5[MG_MAX_PATH],
8611
      buf4[MG_MAX_PATH], cmdline[MG_MAX_PATH];
8612
  DWORD flags = DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS;
8613
  FILE *fp;
8614

    
8615
  memset(&si, 0, sizeof(si));
8616
  memset(&pi, 0, sizeof(pi));
8617

    
8618
  si.cb = sizeof(si);
8619
  si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
8620
  si.wShowWindow = SW_HIDE;
8621
  si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
8622

    
8623
  CreatePipe(&a[0], &a[1], NULL, 0);
8624
  CreatePipe(&b[0], &b[1], NULL, 0);
8625
  DuplicateHandle(me, a[0], me, &si.hStdInput, 0, TRUE, flags);
8626
  DuplicateHandle(me, b[1], me, &si.hStdOutput, 0, TRUE, flags);
8627

    
8628
  if (interp == NULL && (fp = mg_fopen(cmd, "r")) != NULL) {
8629
    buf[0] = buf[1] = '\0';
8630
    fgets(buf, sizeof(buf), fp);
8631
    buf[sizeof(buf) - 1] = '\0';
8632
    if (buf[0] == '#' && buf[1] == '!') {
8633
      interp = buf + 2;
8634
      /* Trim leading spaces: https://github.com/cesanta/mongoose/issues/489 */
8635
      while (*interp != '\0' && isspace(*(unsigned char *) interp)) {
8636
        interp++;
8637
      }
8638
    }
8639
    fclose(fp);
8640
  }
8641

    
8642
  snprintf(buf, sizeof(buf), "%s/%s", dir, cmd);
8643
  mg_abs_path(buf, buf2, ARRAY_SIZE(buf2));
8644

    
8645
  mg_abs_path(dir, buf5, ARRAY_SIZE(buf5));
8646
  to_wchar(dir, full_dir, ARRAY_SIZE(full_dir));
8647

    
8648
  if (interp != NULL) {
8649
    mg_abs_path(interp, buf4, ARRAY_SIZE(buf4));
8650
    snprintf(cmdline, sizeof(cmdline), "%s \"%s\"", buf4, buf2);
8651
  } else {
8652
    snprintf(cmdline, sizeof(cmdline), "\"%s\"", buf2);
8653
  }
8654
  to_wchar(cmdline, wcmd, ARRAY_SIZE(wcmd));
8655

    
8656
  if (CreateProcessW(NULL, wcmd, NULL, NULL, TRUE, CREATE_NEW_PROCESS_GROUP,
8657
                     (void *) env, full_dir, &si, &pi) != 0) {
8658
    mg_spawn_stdio_thread(sock, a[1], mg_push_to_stdin);
8659
    mg_spawn_stdio_thread(sock, b[0], mg_pull_from_stdout);
8660

    
8661
    CloseHandle(si.hStdOutput);
8662
    CloseHandle(si.hStdInput);
8663

    
8664
    CloseHandle(pi.hThread);
8665
    CloseHandle(pi.hProcess);
8666
  } else {
8667
    CloseHandle(a[1]);
8668
    CloseHandle(b[0]);
8669
    closesocket(sock);
8670
  }
8671
  DBG(("CGI command: [%ls] -> %p", wcmd, pi.hProcess));
8672

    
8673
  /* Not closing a[0] and b[1] because we've used DUPLICATE_CLOSE_SOURCE */
8674
  (void) envp;
8675
  return (pi.hProcess != NULL);
8676
}
8677
#else
8678
static int mg_start_process(const char *interp, const char *cmd,
8679
                            const char *env, const char *envp[],
8680
                            const char *dir, sock_t sock) {
8681
  char buf[500];
8682
  pid_t pid = fork();
8683
  (void) env;
8684

    
8685
  if (pid == 0) {
8686
    /*
8687
     * In Linux `chdir` declared with `warn_unused_result` attribute
8688
     * To shutup compiler we have yo use result in some way
8689
     */
8690
    int tmp = chdir(dir);
8691
    (void) tmp;
8692
    (void) dup2(sock, 0);
8693
    (void) dup2(sock, 1);
8694
    closesocket(sock);
8695

    
8696
    /*
8697
     * After exec, all signal handlers are restored to their default values,
8698
     * with one exception of SIGCHLD. According to POSIX.1-2001 and Linux's
8699
     * implementation, SIGCHLD's handler will leave unchanged after exec
8700
     * if it was set to be ignored. Restore it to default action.
8701
     */
8702
    signal(SIGCHLD, SIG_DFL);
8703

    
8704
    if (interp == NULL) {
8705
      execle(cmd, cmd, (char *) 0, envp); /* (char *) 0 to squash warning */
8706
    } else {
8707
      execle(interp, interp, cmd, (char *) 0, envp);
8708
    }
8709
    snprintf(buf, sizeof(buf),
8710
             "Status: 500\r\n\r\n"
8711
             "500 Server Error: %s%s%s: %s",
8712
             interp == NULL ? "" : interp, interp == NULL ? "" : " ", cmd,
8713
             strerror(errno));
8714
    send(1, buf, strlen(buf), 0);
8715
    _exit(EXIT_FAILURE); /* exec call failed */
8716
  }
8717

    
8718
  return (pid != 0);
8719
}
8720
#endif /* _WIN32 */
8721

    
8722
/*
8723
 * Append VARIABLE=VALUE\0 string to the buffer, and add a respective
8724
 * pointer into the vars array.
8725
 */
8726
static char *mg_addenv(struct mg_cgi_env_block *block, const char *fmt, ...) {
8727
  int n, space;
8728
  char *added = block->buf + block->len;
8729
  va_list ap;
8730

    
8731
  /* Calculate how much space is left in the buffer */
8732
  space = sizeof(block->buf) - (block->len + 2);
8733
  if (space > 0) {
8734
    /* Copy VARIABLE=VALUE\0 string into the free space */
8735
    va_start(ap, fmt);
8736
    n = vsnprintf(added, (size_t) space, fmt, ap);
8737
    va_end(ap);
8738

    
8739
    /* Make sure we do not overflow buffer and the envp array */
8740
    if (n > 0 && n + 1 < space &&
8741
        block->nvars < (int) ARRAY_SIZE(block->vars) - 2) {
8742
      /* Append a pointer to the added string into the envp array */
8743
      block->vars[block->nvars++] = added;
8744
      /* Bump up used length counter. Include \0 terminator */
8745
      block->len += n + 1;
8746
    }
8747
  }
8748

    
8749
  return added;
8750
}
8751

    
8752
static void mg_addenv2(struct mg_cgi_env_block *blk, const char *name) {
8753
  const char *s;
8754
  if ((s = getenv(name)) != NULL) mg_addenv(blk, "%s=%s", name, s);
8755
}
8756

    
8757
static void mg_prepare_cgi_environment(struct mg_connection *nc,
8758
                                       const char *prog,
8759
                                       const struct mg_str *path_info,
8760
                                       const struct http_message *hm,
8761
                                       const struct mg_serve_http_opts *opts,
8762
                                       struct mg_cgi_env_block *blk) {
8763
  const char *s;
8764
  struct mg_str *h;
8765
  char *p;
8766
  size_t i;
8767
  char buf[100];
8768
  size_t path_info_len = path_info != NULL ? path_info->len : 0;
8769

    
8770
  blk->len = blk->nvars = 0;
8771
  blk->nc = nc;
8772

    
8773
  if ((s = getenv("SERVER_NAME")) != NULL) {
8774
    mg_addenv(blk, "SERVER_NAME=%s", s);
8775
  } else {
8776
    mg_sock_to_str(nc->sock, buf, sizeof(buf), 3);
8777
    mg_addenv(blk, "SERVER_NAME=%s", buf);
8778
  }
8779
  mg_addenv(blk, "SERVER_ROOT=%s", opts->document_root);
8780
  mg_addenv(blk, "DOCUMENT_ROOT=%s", opts->document_root);
8781
  mg_addenv(blk, "SERVER_SOFTWARE=%s/%s", "Mongoose", MG_VERSION);
8782

    
8783
  /* Prepare the environment block */
8784
  mg_addenv(blk, "%s", "GATEWAY_INTERFACE=CGI/1.1");
8785
  mg_addenv(blk, "%s", "SERVER_PROTOCOL=HTTP/1.1");
8786
  mg_addenv(blk, "%s", "REDIRECT_STATUS=200"); /* For PHP */
8787

    
8788
  mg_addenv(blk, "REQUEST_METHOD=%.*s", (int) hm->method.len, hm->method.p);
8789

    
8790
  mg_addenv(blk, "REQUEST_URI=%.*s%s%.*s", (int) hm->uri.len, hm->uri.p,
8791
            hm->query_string.len == 0 ? "" : "?", (int) hm->query_string.len,
8792
            hm->query_string.p);
8793

    
8794
  mg_conn_addr_to_str(nc, buf, sizeof(buf),
8795
                      MG_SOCK_STRINGIFY_REMOTE | MG_SOCK_STRINGIFY_IP);
8796
  mg_addenv(blk, "REMOTE_ADDR=%s", buf);
8797
  mg_conn_addr_to_str(nc, buf, sizeof(buf), MG_SOCK_STRINGIFY_PORT);
8798
  mg_addenv(blk, "SERVER_PORT=%s", buf);
8799

    
8800
  s = hm->uri.p + hm->uri.len - path_info_len - 1;
8801
  if (*s == '/') {
8802
    const char *base_name = strrchr(prog, DIRSEP);
8803
    mg_addenv(blk, "SCRIPT_NAME=%.*s/%s", (int) (s - hm->uri.p), hm->uri.p,
8804
              (base_name != NULL ? base_name + 1 : prog));
8805
  } else {
8806
    mg_addenv(blk, "SCRIPT_NAME=%.*s", (int) (s - hm->uri.p + 1), hm->uri.p);
8807
  }
8808
  mg_addenv(blk, "SCRIPT_FILENAME=%s", prog);
8809

    
8810
  if (path_info != NULL && path_info->len > 0) {
8811
    mg_addenv(blk, "PATH_INFO=%.*s", (int) path_info->len, path_info->p);
8812
    /* Not really translated... */
8813
    mg_addenv(blk, "PATH_TRANSLATED=%.*s", (int) path_info->len, path_info->p);
8814
  }
8815

    
8816
#if MG_ENABLE_SSL
8817
  mg_addenv(blk, "HTTPS=%s", (nc->flags & MG_F_SSL ? "on" : "off"));
8818
#else
8819
  mg_addenv(blk, "HTTPS=off");
8820
#endif
8821

    
8822
  if ((h = mg_get_http_header((struct http_message *) hm, "Content-Type")) !=
8823
      NULL) {
8824
    mg_addenv(blk, "CONTENT_TYPE=%.*s", (int) h->len, h->p);
8825
  }
8826

    
8827
  if (hm->query_string.len > 0) {
8828
    mg_addenv(blk, "QUERY_STRING=%.*s", (int) hm->query_string.len,
8829
              hm->query_string.p);
8830
  }
8831

    
8832
  if ((h = mg_get_http_header((struct http_message *) hm, "Content-Length")) !=
8833
      NULL) {
8834
    mg_addenv(blk, "CONTENT_LENGTH=%.*s", (int) h->len, h->p);
8835
  }
8836

    
8837
  mg_addenv2(blk, "PATH");
8838
  mg_addenv2(blk, "TMP");
8839
  mg_addenv2(blk, "TEMP");
8840
  mg_addenv2(blk, "TMPDIR");
8841
  mg_addenv2(blk, "PERLLIB");
8842
  mg_addenv2(blk, MG_ENV_EXPORT_TO_CGI);
8843

    
8844
#ifdef _WIN32
8845
  mg_addenv2(blk, "COMSPEC");
8846
  mg_addenv2(blk, "SYSTEMROOT");
8847
  mg_addenv2(blk, "SystemDrive");
8848
  mg_addenv2(blk, "ProgramFiles");
8849
  mg_addenv2(blk, "ProgramFiles(x86)");
8850
  mg_addenv2(blk, "CommonProgramFiles(x86)");
8851
#else
8852
  mg_addenv2(blk, "LD_LIBRARY_PATH");
8853
#endif /* _WIN32 */
8854

    
8855
  /* Add all headers as HTTP_* variables */
8856
  for (i = 0; hm->header_names[i].len > 0; i++) {
8857
    p = mg_addenv(blk, "HTTP_%.*s=%.*s", (int) hm->header_names[i].len,
8858
                  hm->header_names[i].p, (int) hm->header_values[i].len,
8859
                  hm->header_values[i].p);
8860

    
8861
    /* Convert variable name into uppercase, and change - to _ */
8862
    for (; *p != '=' && *p != '\0'; p++) {
8863
      if (*p == '-') *p = '_';
8864
      *p = (char) toupper(*(unsigned char *) p);
8865
    }
8866
  }
8867

    
8868
  blk->vars[blk->nvars++] = NULL;
8869
  blk->buf[blk->len++] = '\0';
8870
}
8871

    
8872
static void mg_cgi_ev_handler(struct mg_connection *cgi_nc, int ev,
8873
                              void *ev_data MG_UD_ARG(void *user_data)) {
8874
#if !MG_ENABLE_CALLBACK_USERDATA
8875
  void *user_data = cgi_nc->user_data;
8876
#endif
8877
  struct mg_connection *nc = (struct mg_connection *) user_data;
8878
  (void) ev_data;
8879

    
8880
  if (nc == NULL) {
8881
    /* The corresponding network connection was closed. */
8882
    cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY;
8883
    return;
8884
  }
8885

    
8886
  switch (ev) {
8887
    case MG_EV_RECV:
8888
      /*
8889
       * CGI script does not output reply line, like "HTTP/1.1 CODE XXXXX\n"
8890
       * It outputs headers, then body. Headers might include "Status"
8891
       * header, which changes CODE, and it might include "Location" header
8892
       * which changes CODE to 302.
8893
       *
8894
       * Therefore we do not send the output from the CGI script to the user
8895
       * until all CGI headers are received.
8896
       *
8897
       * Here we parse the output from the CGI script, and if all headers has
8898
       * been received, send appropriate reply line, and forward all
8899
       * received headers to the client.
8900
       */
8901
      if (nc->flags & MG_F_HTTP_CGI_PARSE_HEADERS) {
8902
        struct mbuf *io = &cgi_nc->recv_mbuf;
8903
        int len = mg_http_get_request_len(io->buf, io->len);
8904

    
8905
        if (len == 0) break;
8906
        if (len < 0 || io->len > MG_MAX_HTTP_REQUEST_SIZE) {
8907
          cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY;
8908
          mg_http_send_error(nc, 500, "Bad headers");
8909
        } else {
8910
          struct http_message hm;
8911
          struct mg_str *h;
8912
          mg_http_parse_headers(io->buf, io->buf + io->len, io->len, &hm);
8913
          if (mg_get_http_header(&hm, "Location") != NULL) {
8914
            mg_printf(nc, "%s", "HTTP/1.1 302 Moved\r\n");
8915
          } else if ((h = mg_get_http_header(&hm, "Status")) != NULL) {
8916
            mg_printf(nc, "HTTP/1.1 %.*s\r\n", (int) h->len, h->p);
8917
          } else {
8918
            mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\n");
8919
          }
8920
        }
8921
        nc->flags &= ~MG_F_HTTP_CGI_PARSE_HEADERS;
8922
      }
8923
      if (!(nc->flags & MG_F_HTTP_CGI_PARSE_HEADERS)) {
8924
        mg_forward(cgi_nc, nc);
8925
      }
8926
      break;
8927
    case MG_EV_CLOSE:
8928
      DBG(("%p CLOSE", cgi_nc));
8929
      mg_http_free_proto_data_cgi(&mg_http_get_proto_data(nc)->cgi);
8930
      nc->flags |= MG_F_SEND_AND_CLOSE;
8931
      break;
8932
  }
8933
}
8934

    
8935
MG_INTERNAL void mg_handle_cgi(struct mg_connection *nc, const char *prog,
8936
                               const struct mg_str *path_info,
8937
                               const struct http_message *hm,
8938
                               const struct mg_serve_http_opts *opts) {
8939
  struct mg_cgi_env_block blk;
8940
  char dir[MG_MAX_PATH];
8941
  const char *p;
8942
  sock_t fds[2];
8943

    
8944
  DBG(("%p [%s]", nc, prog));
8945
  mg_prepare_cgi_environment(nc, prog, path_info, hm, opts, &blk);
8946
  /*
8947
   * CGI must be executed in its own directory. 'dir' must point to the
8948
   * directory containing executable program, 'p' must point to the
8949
   * executable program name relative to 'dir'.
8950
   */
8951
  if ((p = strrchr(prog, DIRSEP)) == NULL) {
8952
    snprintf(dir, sizeof(dir), "%s", ".");
8953
  } else {
8954
    snprintf(dir, sizeof(dir), "%.*s", (int) (p - prog), prog);
8955
    prog = p + 1;
8956
  }
8957

    
8958
  if (!mg_socketpair(fds, SOCK_STREAM)) {
8959
    nc->flags |= MG_F_CLOSE_IMMEDIATELY;
8960
    return;
8961
  }
8962

    
8963
#ifndef _WIN32
8964
  struct sigaction sa;
8965

    
8966
  sigemptyset(&sa.sa_mask);
8967
  sa.sa_handler = SIG_IGN;
8968
  sa.sa_flags = 0;
8969
  sigaction(SIGCHLD, &sa, NULL);
8970
#endif
8971

    
8972
  if (mg_start_process(opts->cgi_interpreter, prog, blk.buf, blk.vars, dir,
8973
                       fds[1]) != 0) {
8974
    size_t n = nc->recv_mbuf.len - (hm->message.len - hm->body.len);
8975
    struct mg_connection *cgi_nc =
8976
        mg_add_sock(nc->mgr, fds[0], mg_cgi_ev_handler MG_UD_ARG(nc));
8977
    struct mg_http_proto_data *cgi_pd = mg_http_get_proto_data(nc);
8978
    cgi_pd->cgi.cgi_nc = cgi_nc;
8979
#if !MG_ENABLE_CALLBACK_USERDATA
8980
    cgi_pd->cgi.cgi_nc->user_data = nc;
8981
#endif
8982
    nc->flags |= MG_F_HTTP_CGI_PARSE_HEADERS;
8983
    /* Push POST data to the CGI */
8984
    if (n > 0 && n < nc->recv_mbuf.len) {
8985
      mg_send(cgi_pd->cgi.cgi_nc, hm->body.p, n);
8986
    }
8987
    mbuf_remove(&nc->recv_mbuf, nc->recv_mbuf.len);
8988
  } else {
8989
    closesocket(fds[0]);
8990
    mg_http_send_error(nc, 500, "CGI failure");
8991
  }
8992

    
8993
#ifndef _WIN32
8994
  closesocket(fds[1]); /* On Windows, CGI stdio thread closes that socket */
8995
#endif
8996
}
8997

    
8998
MG_INTERNAL void mg_http_free_proto_data_cgi(struct mg_http_proto_data_cgi *d) {
8999
  if (d == NULL) return;
9000
  if (d->cgi_nc != NULL) {
9001
    d->cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY;
9002
    d->cgi_nc->user_data = NULL;
9003
  }
9004
  memset(d, 0, sizeof(*d));
9005
}
9006

    
9007
#endif /* MG_ENABLE_HTTP && MG_ENABLE_HTTP_CGI */
9008
#ifdef MG_MODULE_LINES
9009
#line 1 "mongoose/src/mg_http_ssi.c"
9010
#endif
9011
/*
9012
 * Copyright (c) 2014-2016 Cesanta Software Limited
9013
 * All rights reserved
9014
 */
9015

    
9016
#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_SSI && MG_ENABLE_FILESYSTEM
9017

    
9018
static void mg_send_ssi_file(struct mg_connection *nc, struct http_message *hm,
9019
                             const char *path, FILE *fp, int include_level,
9020
                             const struct mg_serve_http_opts *opts);
9021

    
9022
static void mg_send_file_data(struct mg_connection *nc, FILE *fp) {
9023
  char buf[BUFSIZ];
9024
  size_t n;
9025
  while ((n = mg_fread(buf, 1, sizeof(buf), fp)) > 0) {
9026
    mg_send(nc, buf, n);
9027
  }
9028
}
9029

    
9030
static void mg_do_ssi_include(struct mg_connection *nc, struct http_message *hm,
9031
                              const char *ssi, char *tag, int include_level,
9032
                              const struct mg_serve_http_opts *opts) {
9033
  char file_name[MG_MAX_PATH], path[MG_MAX_PATH], *p;
9034
  FILE *fp;
9035

    
9036
  /*
9037
   * sscanf() is safe here, since send_ssi_file() also uses buffer
9038
   * of size MG_BUF_LEN to get the tag. So strlen(tag) is always < MG_BUF_LEN.
9039
   */
9040
  if (sscanf(tag, " virtual=\"%[^\"]\"", file_name) == 1) {
9041
    /* File name is relative to the webserver root */
9042
    snprintf(path, sizeof(path), "%s/%s", opts->document_root, file_name);
9043
  } else if (sscanf(tag, " abspath=\"%[^\"]\"", file_name) == 1) {
9044
    /*
9045
     * File name is relative to the webserver working directory
9046
     * or it is absolute system path
9047
     */
9048
    snprintf(path, sizeof(path), "%s", file_name);
9049
  } else if (sscanf(tag, " file=\"%[^\"]\"", file_name) == 1 ||
9050
             sscanf(tag, " \"%[^\"]\"", file_name) == 1) {
9051
    /* File name is relative to the currect document */
9052
    snprintf(path, sizeof(path), "%s", ssi);
9053
    if ((p = strrchr(path, DIRSEP)) != NULL) {
9054
      p[1] = '\0';
9055
    }
9056
    snprintf(path + strlen(path), sizeof(path) - strlen(path), "%s", file_name);
9057
  } else {
9058
    mg_printf(nc, "Bad SSI #include: [%s]", tag);
9059
    return;
9060
  }
9061

    
9062
  if ((fp = mg_fopen(path, "rb")) == NULL) {
9063
    mg_printf(nc, "SSI include error: mg_fopen(%s): %s", path,
9064
              strerror(mg_get_errno()));
9065
  } else {
9066
    mg_set_close_on_exec((sock_t) fileno(fp));
9067
    if (mg_match_prefix(opts->ssi_pattern, strlen(opts->ssi_pattern), path) >
9068
        0) {
9069
      mg_send_ssi_file(nc, hm, path, fp, include_level + 1, opts);
9070
    } else {
9071
      mg_send_file_data(nc, fp);
9072
    }
9073
    fclose(fp);
9074
  }
9075
}
9076

    
9077
#if MG_ENABLE_HTTP_SSI_EXEC
9078
static void do_ssi_exec(struct mg_connection *nc, char *tag) {
9079
  char cmd[BUFSIZ];
9080
  FILE *fp;
9081

    
9082
  if (sscanf(tag, " \"%[^\"]\"", cmd) != 1) {
9083
    mg_printf(nc, "Bad SSI #exec: [%s]", tag);
9084
  } else if ((fp = popen(cmd, "r")) == NULL) {
9085
    mg_printf(nc, "Cannot SSI #exec: [%s]: %s", cmd, strerror(mg_get_errno()));
9086
  } else {
9087
    mg_send_file_data(nc, fp);
9088
    pclose(fp);
9089
  }
9090
}
9091
#endif /* MG_ENABLE_HTTP_SSI_EXEC */
9092

    
9093
/*
9094
 * SSI directive has the following format:
9095
 * <!--#directive parameter=value parameter=value -->
9096
 */
9097
static void mg_send_ssi_file(struct mg_connection *nc, struct http_message *hm,
9098
                             const char *path, FILE *fp, int include_level,
9099
                             const struct mg_serve_http_opts *opts) {
9100
  static const struct mg_str btag = MG_MK_STR("<!--#");
9101
  static const struct mg_str d_include = MG_MK_STR("include");
9102
  static const struct mg_str d_call = MG_MK_STR("call");
9103
#if MG_ENABLE_HTTP_SSI_EXEC
9104
  static const struct mg_str d_exec = MG_MK_STR("exec");
9105
#endif
9106
  char buf[BUFSIZ], *p = buf + btag.len; /* p points to SSI directive */
9107
  int ch, len, in_ssi_tag;
9108

    
9109
  if (include_level > 10) {
9110
    mg_printf(nc, "SSI #include level is too deep (%s)", path);
9111
    return;
9112
  }
9113

    
9114
  in_ssi_tag = len = 0;
9115
  while ((ch = fgetc(fp)) != EOF) {
9116
    if (in_ssi_tag && ch == '>' && buf[len - 1] == '-' && buf[len - 2] == '-') {
9117
      size_t i = len - 2;
9118
      in_ssi_tag = 0;
9119

    
9120
      /* Trim closing --> */
9121
      buf[i--] = '\0';
9122
      while (i > 0 && buf[i] == ' ') {
9123
        buf[i--] = '\0';
9124
      }
9125

    
9126
      /* Handle known SSI directives */
9127
      if (strncmp(p, d_include.p, d_include.len) == 0) {
9128
        mg_do_ssi_include(nc, hm, path, p + d_include.len + 1, include_level,
9129
                          opts);
9130
      } else if (strncmp(p, d_call.p, d_call.len) == 0) {
9131
        struct mg_ssi_call_ctx cctx;
9132
        memset(&cctx, 0, sizeof(cctx));
9133
        cctx.req = hm;
9134
        cctx.file = mg_mk_str(path);
9135
        cctx.arg = mg_mk_str(p + d_call.len + 1);
9136
        mg_call(nc, NULL, nc->user_data, MG_EV_SSI_CALL,
9137
                (void *) cctx.arg.p); /* NUL added above */
9138
        mg_call(nc, NULL, nc->user_data, MG_EV_SSI_CALL_CTX, &cctx);
9139
#if MG_ENABLE_HTTP_SSI_EXEC
9140
      } else if (strncmp(p, d_exec.p, d_exec.len) == 0) {
9141
        do_ssi_exec(nc, p + d_exec.len + 1);
9142
#endif
9143
      } else {
9144
        /* Silently ignore unknown SSI directive. */
9145
      }
9146
      len = 0;
9147
    } else if (ch == '<') {
9148
      in_ssi_tag = 1;
9149
      if (len > 0) {
9150
        mg_send(nc, buf, (size_t) len);
9151
      }
9152
      len = 0;
9153
      buf[len++] = ch & 0xff;
9154
    } else if (in_ssi_tag) {
9155
      if (len == (int) btag.len && strncmp(buf, btag.p, btag.len) != 0) {
9156
        /* Not an SSI tag */
9157
        in_ssi_tag = 0;
9158
      } else if (len == (int) sizeof(buf) - 2) {
9159
        mg_printf(nc, "%s: SSI tag is too large", path);
9160
        len = 0;
9161
      }
9162
      buf[len++] = ch & 0xff;
9163
    } else {
9164
      buf[len++] = ch & 0xff;
9165
      if (len == (int) sizeof(buf)) {
9166
        mg_send(nc, buf, (size_t) len);
9167
        len = 0;
9168
      }
9169
    }
9170
  }
9171

    
9172
  /* Send the rest of buffered data */
9173
  if (len > 0) {
9174
    mg_send(nc, buf, (size_t) len);
9175
  }
9176
}
9177

    
9178
MG_INTERNAL void mg_handle_ssi_request(struct mg_connection *nc,
9179
                                       struct http_message *hm,
9180
                                       const char *path,
9181
                                       const struct mg_serve_http_opts *opts) {
9182
  FILE *fp;
9183
  struct mg_str mime_type;
9184
  DBG(("%p %s", nc, path));
9185

    
9186
  if ((fp = mg_fopen(path, "rb")) == NULL) {
9187
    mg_http_send_error(nc, 404, NULL);
9188
  } else {
9189
    mg_set_close_on_exec((sock_t) fileno(fp));
9190

    
9191
    mime_type = mg_get_mime_type(path, "text/plain", opts);
9192
    mg_send_response_line(nc, 200, opts->extra_headers);
9193
    mg_printf(nc,
9194
              "Content-Type: %.*s\r\n"
9195
              "Connection: close\r\n\r\n",
9196
              (int) mime_type.len, mime_type.p);
9197
    mg_send_ssi_file(nc, hm, path, fp, 0, opts);
9198
    fclose(fp);
9199
    nc->flags |= MG_F_SEND_AND_CLOSE;
9200
  }
9201
}
9202

    
9203
#endif /* MG_ENABLE_HTTP_SSI && MG_ENABLE_HTTP && MG_ENABLE_FILESYSTEM */
9204
#ifdef MG_MODULE_LINES
9205
#line 1 "mongoose/src/mg_http_webdav.c"
9206
#endif
9207
/*
9208
 * Copyright (c) 2014-2016 Cesanta Software Limited
9209
 * All rights reserved
9210
 */
9211

    
9212
#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_WEBDAV
9213

    
9214
MG_INTERNAL int mg_is_dav_request(const struct mg_str *s) {
9215
  static const char *methods[] = {
9216
    "PUT",
9217
    "DELETE",
9218
    "MKCOL",
9219
    "PROPFIND",
9220
    "MOVE"
9221
#if MG_ENABLE_FAKE_DAVLOCK
9222
    ,
9223
    "LOCK",
9224
    "UNLOCK"
9225
#endif
9226
  };
9227
  size_t i;
9228

    
9229
  for (i = 0; i < ARRAY_SIZE(methods); i++) {
9230
    if (mg_vcmp(s, methods[i]) == 0) {
9231
      return 1;
9232
    }
9233
  }
9234

    
9235
  return 0;
9236
}
9237

    
9238
static int mg_mkdir(const char *path, uint32_t mode) {
9239
#ifndef _WIN32
9240
  return mkdir(path, mode);
9241
#else
9242
  (void) mode;
9243
  return _mkdir(path);
9244
#endif
9245
}
9246

    
9247
static void mg_print_props(struct mg_connection *nc, const char *name,
9248
                           cs_stat_t *stp) {
9249
  char mtime[64];
9250
  time_t t = stp->st_mtime; /* store in local variable for NDK compile */
9251
  struct mg_str name_esc = mg_url_encode(mg_mk_str(name));
9252
  mg_gmt_time_string(mtime, sizeof(mtime), &t);
9253
  mg_printf(nc,
9254
            "<d:response>"
9255
            "<d:href>%s</d:href>"
9256
            "<d:propstat>"
9257
            "<d:prop>"
9258
            "<d:resourcetype>%s</d:resourcetype>"
9259
            "<d:getcontentlength>%" INT64_FMT
9260
            "</d:getcontentlength>"
9261
            "<d:getlastmodified>%s</d:getlastmodified>"
9262
            "</d:prop>"
9263
            "<d:status>HTTP/1.1 200 OK</d:status>"
9264
            "</d:propstat>"
9265
            "</d:response>\n",
9266
            name_esc.p, S_ISDIR(stp->st_mode) ? "<d:collection/>" : "",
9267
            (int64_t) stp->st_size, mtime);
9268
  free((void *) name_esc.p);
9269
}
9270

    
9271
MG_INTERNAL void mg_handle_propfind(struct mg_connection *nc, const char *path,
9272
                                    cs_stat_t *stp, struct http_message *hm,
9273
                                    struct mg_serve_http_opts *opts) {
9274
  static const char header[] =
9275
      "HTTP/1.1 207 Multi-Status\r\n"
9276
      "Connection: close\r\n"
9277
      "Content-Type: text/xml; charset=utf-8\r\n\r\n"
9278
      "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
9279
      "<d:multistatus xmlns:d='DAV:'>\n";
9280
  static const char footer[] = "</d:multistatus>\n";
9281
  const struct mg_str *depth = mg_get_http_header(hm, "Depth");
9282

    
9283
  /* Print properties for the requested resource itself */
9284
  if (S_ISDIR(stp->st_mode) &&
9285
      strcmp(opts->enable_directory_listing, "yes") != 0) {
9286
    mg_printf(nc, "%s", "HTTP/1.1 403 Directory Listing Denied\r\n\r\n");
9287
  } else {
9288
    char uri[MG_MAX_PATH];
9289
    mg_send(nc, header, sizeof(header) - 1);
9290
    snprintf(uri, sizeof(uri), "%.*s", (int) hm->uri.len, hm->uri.p);
9291
    mg_print_props(nc, uri, stp);
9292
    if (S_ISDIR(stp->st_mode) && (depth == NULL || mg_vcmp(depth, "0") != 0)) {
9293
      mg_scan_directory(nc, path, opts, mg_print_props);
9294
    }
9295
    mg_send(nc, footer, sizeof(footer) - 1);
9296
    nc->flags |= MG_F_SEND_AND_CLOSE;
9297
  }
9298
}
9299

    
9300
#if MG_ENABLE_FAKE_DAVLOCK
9301
/*
9302
 * Windows explorer (probably there are another WebDav clients like it)
9303
 * requires LOCK support in webdav. W/out this, it still works, but fails
9304
 * to save file: shows error message and offers "Save As".
9305
 * "Save as" works, but this message is very annoying.
9306
 * This is fake lock, which doesn't lock something, just returns LOCK token,
9307
 * UNLOCK always answers "OK".
9308
 * With this fake LOCK Windows Explorer looks happy and saves file.
9309
 * NOTE: that is not DAV LOCK imlementation, it is just a way to shut up
9310
 * Windows native DAV client. This is why FAKE LOCK is not enabed by default
9311
 */
9312
MG_INTERNAL void mg_handle_lock(struct mg_connection *nc, const char *path) {
9313
  static const char *reply =
9314
      "HTTP/1.1 207 Multi-Status\r\n"
9315
      "Connection: close\r\n"
9316
      "Content-Type: text/xml; charset=utf-8\r\n\r\n"
9317
      "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
9318
      "<d:multistatus xmlns:d='DAV:'>\n"
9319
      "<D:lockdiscovery>\n"
9320
      "<D:activelock>\n"
9321
      "<D:locktoken>\n"
9322
      "<D:href>\n"
9323
      "opaquelocktoken:%s%u"
9324
      "</D:href>"
9325
      "</D:locktoken>"
9326
      "</D:activelock>\n"
9327
      "</D:lockdiscovery>"
9328
      "</d:multistatus>\n";
9329
  mg_printf(nc, reply, path, (unsigned int) mg_time());
9330
  nc->flags |= MG_F_SEND_AND_CLOSE;
9331
}
9332
#endif
9333

    
9334
MG_INTERNAL void mg_handle_mkcol(struct mg_connection *nc, const char *path,
9335
                                 struct http_message *hm) {
9336
  int status_code = 500;
9337
  if (hm->body.len != (size_t) ~0 && hm->body.len > 0) {
9338
    status_code = 415;
9339
  } else if (!mg_mkdir(path, 0755)) {
9340
    status_code = 201;
9341
  } else if (errno == EEXIST) {
9342
    status_code = 405;
9343
  } else if (errno == EACCES) {
9344
    status_code = 403;
9345
  } else if (errno == ENOENT) {
9346
    status_code = 409;
9347
  } else {
9348
    status_code = 500;
9349
  }
9350
  mg_http_send_error(nc, status_code, NULL);
9351
}
9352

    
9353
static int mg_remove_directory(const struct mg_serve_http_opts *opts,
9354
                               const char *dir) {
9355
  char path[MG_MAX_PATH];
9356
  struct dirent *dp;
9357
  cs_stat_t st;
9358
  DIR *dirp;
9359

    
9360
  if ((dirp = opendir(dir)) == NULL) return 0;
9361

    
9362
  while ((dp = readdir(dirp)) != NULL) {
9363
    if (mg_is_file_hidden((const char *) dp->d_name, opts, 1)) {
9364
      continue;
9365
    }
9366
    snprintf(path, sizeof(path), "%s%c%s", dir, '/', dp->d_name);
9367
    mg_stat(path, &st);
9368
    if (S_ISDIR(st.st_mode)) {
9369
      mg_remove_directory(opts, path);
9370
    } else {
9371
      remove(path);
9372
    }
9373
  }
9374
  closedir(dirp);
9375
  rmdir(dir);
9376

    
9377
  return 1;
9378
}
9379

    
9380
MG_INTERNAL void mg_handle_move(struct mg_connection *c,
9381
                                const struct mg_serve_http_opts *opts,
9382
                                const char *path, struct http_message *hm) {
9383
  const struct mg_str *dest = mg_get_http_header(hm, "Destination");
9384
  if (dest == NULL) {
9385
    mg_http_send_error(c, 411, NULL);
9386
  } else {
9387
    const char *p = (char *) memchr(dest->p, '/', dest->len);
9388
    if (p != NULL && p[1] == '/' &&
9389
        (p = (char *) memchr(p + 2, '/', dest->p + dest->len - p)) != NULL) {
9390
      char buf[MG_MAX_PATH];
9391
      snprintf(buf, sizeof(buf), "%s%.*s", opts->dav_document_root,
9392
               (int) (dest->p + dest->len - p), p);
9393
      if (rename(path, buf) == 0) {
9394
        mg_http_send_error(c, 200, NULL);
9395
      } else {
9396
        mg_http_send_error(c, 418, NULL);
9397
      }
9398
    } else {
9399
      mg_http_send_error(c, 500, NULL);
9400
    }
9401
  }
9402
}
9403

    
9404
MG_INTERNAL void mg_handle_delete(struct mg_connection *nc,
9405
                                  const struct mg_serve_http_opts *opts,
9406
                                  const char *path) {
9407
  cs_stat_t st;
9408
  if (mg_stat(path, &st) != 0) {
9409
    mg_http_send_error(nc, 404, NULL);
9410
  } else if (S_ISDIR(st.st_mode)) {
9411
    mg_remove_directory(opts, path);
9412
    mg_http_send_error(nc, 204, NULL);
9413
  } else if (remove(path) == 0) {
9414
    mg_http_send_error(nc, 204, NULL);
9415
  } else {
9416
    mg_http_send_error(nc, 423, NULL);
9417
  }
9418
}
9419

    
9420
/* Return -1 on error, 1 on success. */
9421
static int mg_create_itermediate_directories(const char *path) {
9422
  const char *s;
9423

    
9424
  /* Create intermediate directories if they do not exist */
9425
  for (s = path + 1; *s != '\0'; s++) {
9426
    if (*s == '/') {
9427
      char buf[MG_MAX_PATH];
9428
      cs_stat_t st;
9429
      snprintf(buf, sizeof(buf), "%.*s", (int) (s - path), path);
9430
      buf[sizeof(buf) - 1] = '\0';
9431
      if (mg_stat(buf, &st) != 0 && mg_mkdir(buf, 0755) != 0) {
9432
        return -1;
9433
      }
9434
    }
9435
  }
9436

    
9437
  return 1;
9438
}
9439

    
9440
MG_INTERNAL void mg_handle_put(struct mg_connection *nc, const char *path,
9441
                               struct http_message *hm) {
9442
  struct mg_http_proto_data *pd = mg_http_get_proto_data(nc);
9443
  cs_stat_t st;
9444
  const struct mg_str *cl_hdr = mg_get_http_header(hm, "Content-Length");
9445
  int rc, status_code = mg_stat(path, &st) == 0 ? 200 : 201;
9446

    
9447
  mg_http_free_proto_data_file(&pd->file);
9448
  if ((rc = mg_create_itermediate_directories(path)) == 0) {
9449
    mg_printf(nc, "HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n", status_code);
9450
  } else if (rc == -1) {
9451
    mg_http_send_error(nc, 500, NULL);
9452
  } else if (cl_hdr == NULL) {
9453
    mg_http_send_error(nc, 411, NULL);
9454
  } else if ((pd->file.fp = mg_fopen(path, "w+b")) == NULL) {
9455
    mg_http_send_error(nc, 500, NULL);
9456
  } else {
9457
    const struct mg_str *range_hdr = mg_get_http_header(hm, "Content-Range");
9458
    int64_t r1 = 0, r2 = 0;
9459
    pd->file.type = DATA_PUT;
9460
    mg_set_close_on_exec((sock_t) fileno(pd->file.fp));
9461
    pd->file.cl = to64(cl_hdr->p);
9462
    if (range_hdr != NULL &&
9463
        mg_http_parse_range_header(range_hdr, &r1, &r2) > 0) {
9464
      status_code = 206;
9465
      fseeko(pd->file.fp, r1, SEEK_SET);
9466
      pd->file.cl = r2 > r1 ? r2 - r1 + 1 : pd->file.cl - r1;
9467
    }
9468
    mg_printf(nc, "HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n", status_code);
9469
    /* Remove HTTP request from the mbuf, leave only payload */
9470
    mbuf_remove(&nc->recv_mbuf, hm->message.len - hm->body.len);
9471
    mg_http_transfer_file_data(nc);
9472
  }
9473
}
9474

    
9475
#endif /* MG_ENABLE_HTTP && MG_ENABLE_HTTP_WEBDAV */
9476
#ifdef MG_MODULE_LINES
9477
#line 1 "mongoose/src/mg_http_websocket.c"
9478
#endif
9479
/*
9480
 * Copyright (c) 2014 Cesanta Software Limited
9481
 * All rights reserved
9482
 */
9483

    
9484
#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_WEBSOCKET
9485

    
9486
/* Amalgamated: #include "common/cs_sha1.h" */
9487

    
9488
#ifndef MG_WEBSOCKET_PING_INTERVAL_SECONDS
9489
#define MG_WEBSOCKET_PING_INTERVAL_SECONDS 5
9490
#endif
9491

    
9492
#define FLAGS_MASK_FIN (1 << 7)
9493
#define FLAGS_MASK_OP 0x0f
9494

    
9495
static int mg_is_ws_fragment(unsigned char flags) {
9496
  return (flags & FLAGS_MASK_FIN) == 0 ||
9497
         (flags & FLAGS_MASK_OP) == WEBSOCKET_OP_CONTINUE;
9498
}
9499

    
9500
static int mg_is_ws_first_fragment(unsigned char flags) {
9501
  return (flags & FLAGS_MASK_FIN) == 0 &&
9502
         (flags & FLAGS_MASK_OP) != WEBSOCKET_OP_CONTINUE;
9503
}
9504

    
9505
static int mg_is_ws_control_frame(unsigned char flags) {
9506
  unsigned char op = (flags & FLAGS_MASK_OP);
9507
  return op == WEBSOCKET_OP_CLOSE || op == WEBSOCKET_OP_PING ||
9508
         op == WEBSOCKET_OP_PONG;
9509
}
9510

    
9511
static void mg_handle_incoming_websocket_frame(struct mg_connection *nc,
9512
                                               struct websocket_message *wsm) {
9513
  if (wsm->flags & 0x8) {
9514
    mg_call(nc, nc->handler, nc->user_data, MG_EV_WEBSOCKET_CONTROL_FRAME, wsm);
9515
  } else {
9516
    mg_call(nc, nc->handler, nc->user_data, MG_EV_WEBSOCKET_FRAME, wsm);
9517
  }
9518
}
9519

    
9520
static struct mg_ws_proto_data *mg_ws_get_proto_data(struct mg_connection *nc) {
9521
  struct mg_http_proto_data *htd = mg_http_get_proto_data(nc);
9522
  return (htd != NULL ? &htd->ws_data : NULL);
9523
}
9524

    
9525
/*
9526
 * Sends a Close websocket frame with the given data, and closes the underlying
9527
 * connection. If `len` is ~0, strlen(data) is used.
9528
 */
9529
static void mg_ws_close(struct mg_connection *nc, const void *data,
9530
                        size_t len) {
9531
  if ((int) len == ~0) {
9532
    len = strlen((const char *) data);
9533
  }
9534
  mg_send_websocket_frame(nc, WEBSOCKET_OP_CLOSE, data, len);
9535
  nc->flags |= MG_F_SEND_AND_CLOSE;
9536
}
9537

    
9538
static int mg_deliver_websocket_data(struct mg_connection *nc) {
9539
  /* Using unsigned char *, cause of integer arithmetic below */
9540
  uint64_t i, data_len = 0, frame_len = 0, new_data_len = nc->recv_mbuf.len,
9541
              len, mask_len = 0, header_len = 0;
9542
  struct mg_ws_proto_data *wsd = mg_ws_get_proto_data(nc);
9543
  unsigned char *new_data = (unsigned char *) nc->recv_mbuf.buf,
9544
                *e = (unsigned char *) nc->recv_mbuf.buf + nc->recv_mbuf.len;
9545
  uint8_t flags;
9546
  int ok, reass;
9547

    
9548
  if (wsd->reass_len > 0) {
9549
    /*
9550
     * We already have some previously received data which we need to
9551
     * reassemble and deliver to the client code when we get the final
9552
     * fragment.
9553
     *
9554
     * NOTE: it doesn't mean that the current message must be a continuation:
9555
     * it might be a control frame (Close, Ping or Pong), which should be
9556
     * handled without breaking the fragmented message.
9557
     */
9558

    
9559
    size_t existing_len = wsd->reass_len;
9560
    assert(new_data_len >= existing_len);
9561

    
9562
    new_data += existing_len;
9563
    new_data_len -= existing_len;
9564
  }
9565

    
9566
  flags = new_data[0];
9567

    
9568
  reass = new_data_len > 0 && mg_is_ws_fragment(flags) &&
9569
          !(nc->flags & MG_F_WEBSOCKET_NO_DEFRAG);
9570

    
9571
  if (reass && mg_is_ws_control_frame(flags)) {
9572
    /*
9573
     * Control frames can't be fragmented, so if we encounter fragmented
9574
     * control frame, close connection immediately.
9575
     */
9576
    mg_ws_close(nc, "fragmented control frames are illegal", ~0);
9577
    return 0;
9578
  } else if (new_data_len > 0 && !reass && !mg_is_ws_control_frame(flags) &&
9579
             wsd->reass_len > 0) {
9580
    /*
9581
     * When in the middle of a fragmented message, only the continuations
9582
     * and control frames are allowed.
9583
     */
9584
    mg_ws_close(nc, "non-continuation in the middle of a fragmented message",
9585
                ~0);
9586
    return 0;
9587
  }
9588

    
9589
  if (new_data_len >= 2) {
9590
    len = new_data[1] & 0x7f;
9591
    mask_len = new_data[1] & FLAGS_MASK_FIN ? 4 : 0;
9592
    if (len < 126 && new_data_len >= mask_len) {
9593
      data_len = len;
9594
      header_len = 2 + mask_len;
9595
    } else if (len == 126 && new_data_len >= 4 + mask_len) {
9596
      header_len = 4 + mask_len;
9597
      data_len = ntohs(*(uint16_t *) &new_data[2]);
9598
    } else if (new_data_len >= 10 + mask_len) {
9599
      header_len = 10 + mask_len;
9600
      data_len = (((uint64_t) ntohl(*(uint32_t *) &new_data[2])) << 32) +
9601
                 ntohl(*(uint32_t *) &new_data[6]);
9602
    }
9603
  }
9604

    
9605
  frame_len = header_len + data_len;
9606
  ok = (frame_len > 0 && frame_len <= new_data_len);
9607

    
9608
  /* Check for overflow */
9609
  if (frame_len < header_len || frame_len < data_len) {
9610
    ok = 0;
9611
    mg_ws_close(nc, "overflowed message", ~0);
9612
  }
9613

    
9614
  if (ok) {
9615
    size_t cleanup_len = 0;
9616
    struct websocket_message wsm;
9617

    
9618
    wsm.size = (size_t) data_len;
9619
    wsm.data = new_data + header_len;
9620
    wsm.flags = flags;
9621

    
9622
    /* Apply mask if necessary */
9623
    if (mask_len > 0) {
9624
      for (i = 0; i < data_len; i++) {
9625
        new_data[i + header_len] ^= (new_data + header_len - mask_len)[i % 4];
9626
      }
9627
    }
9628

    
9629
    if (reass) {
9630
      /* This is a message fragment */
9631

    
9632
      if (mg_is_ws_first_fragment(flags)) {
9633
        /*
9634
         * On the first fragmented frame, skip the first byte (op) and also
9635
         * reset size to 1 (op), it'll be incremented with the data len below.
9636
         */
9637
        new_data += 1;
9638
        wsd->reass_len = 1 /* op */;
9639
      }
9640

    
9641
      /* Append this frame to the reassembled buffer */
9642
      memmove(new_data, wsm.data, e - wsm.data);
9643
      wsd->reass_len += wsm.size;
9644
      nc->recv_mbuf.len -= wsm.data - new_data;
9645

    
9646
      if (flags & FLAGS_MASK_FIN) {
9647
        /* On last fragmented frame - call user handler and remove data */
9648
        wsm.flags = FLAGS_MASK_FIN | nc->recv_mbuf.buf[0];
9649
        wsm.data = (unsigned char *) nc->recv_mbuf.buf + 1 /* op */;
9650
        wsm.size = wsd->reass_len - 1 /* op */;
9651
        cleanup_len = wsd->reass_len;
9652
        wsd->reass_len = 0;
9653

    
9654
        /* Pass reassembled message to the client code. */
9655
        mg_handle_incoming_websocket_frame(nc, &wsm);
9656
        mbuf_remove(&nc->recv_mbuf, cleanup_len); /* Cleanup frame */
9657
      }
9658
    } else {
9659
      /*
9660
       * This is a complete message, not a fragment. It might happen in between
9661
       * of a fragmented message (in this case, WebSocket protocol requires
9662
       * current message to be a control frame).
9663
       */
9664
      cleanup_len = (size_t) frame_len;
9665

    
9666
      /* First of all, check if we need to react on a control frame. */
9667
      switch (flags & FLAGS_MASK_OP) {
9668
        case WEBSOCKET_OP_PING:
9669
          mg_send_websocket_frame(nc, WEBSOCKET_OP_PONG, wsm.data, wsm.size);
9670
          break;
9671

    
9672
        case WEBSOCKET_OP_CLOSE:
9673
          mg_ws_close(nc, wsm.data, wsm.size);
9674
          break;
9675
      }
9676

    
9677
      /* Pass received message to the client code. */
9678
      mg_handle_incoming_websocket_frame(nc, &wsm);
9679

    
9680
      /* Cleanup frame */
9681
      memmove(nc->recv_mbuf.buf + wsd->reass_len,
9682
              nc->recv_mbuf.buf + wsd->reass_len + cleanup_len,
9683
              nc->recv_mbuf.len - wsd->reass_len - cleanup_len);
9684
      nc->recv_mbuf.len -= cleanup_len;
9685
    }
9686
  }
9687

    
9688
  return ok;
9689
}
9690

    
9691
struct ws_mask_ctx {
9692
  size_t pos; /* zero means unmasked */
9693
  uint32_t mask;
9694
};
9695

    
9696
static uint32_t mg_ws_random_mask(void) {
9697
  uint32_t mask;
9698
/*
9699
 * The spec requires WS client to generate hard to
9700
 * guess mask keys. From RFC6455, Section 5.3:
9701
 *
9702
 * The unpredictability of the masking key is essential to prevent
9703
 * authors of malicious applications from selecting the bytes that appear on
9704
 * the wire.
9705
 *
9706
 * Hence this feature is essential when the actual end user of this API
9707
 * is untrusted code that wouldn't have access to a lower level net API
9708
 * anyway (e.g. web browsers). Hence this feature is low prio for most
9709
 * mongoose use cases and thus can be disabled, e.g. when porting to a platform
9710
 * that lacks rand().
9711
 */
9712
#if MG_DISABLE_WS_RANDOM_MASK
9713
  mask = 0xefbeadde; /* generated with a random number generator, I swear */
9714
#else
9715
  if (sizeof(long) >= 4) {
9716
    mask = (uint32_t) rand();
9717
  } else if (sizeof(long) == 2) {
9718
    mask = (uint32_t) rand() << 16 | (uint32_t) rand();
9719
  }
9720
#endif
9721
  return mask;
9722
}
9723

    
9724
static void mg_send_ws_header(struct mg_connection *nc, int op, size_t len,
9725
                              struct ws_mask_ctx *ctx) {
9726
  int header_len;
9727
  unsigned char header[10];
9728

    
9729
  header[0] =
9730
      (op & WEBSOCKET_DONT_FIN ? 0x0 : FLAGS_MASK_FIN) | (op & FLAGS_MASK_OP);
9731
  if (len < 126) {
9732
    header[1] = (unsigned char) len;
9733
    header_len = 2;
9734
  } else if (len < 65535) {
9735
    uint16_t tmp = htons((uint16_t) len);
9736
    header[1] = 126;
9737
    memcpy(&header[2], &tmp, sizeof(tmp));
9738
    header_len = 4;
9739
  } else {
9740
    uint32_t tmp;
9741
    header[1] = 127;
9742
    tmp = htonl((uint32_t)((uint64_t) len >> 32));
9743
    memcpy(&header[2], &tmp, sizeof(tmp));
9744
    tmp = htonl((uint32_t)(len & 0xffffffff));
9745
    memcpy(&header[6], &tmp, sizeof(tmp));
9746
    header_len = 10;
9747
  }
9748

    
9749
  /* client connections enable masking */
9750
  if (nc->listener == NULL) {
9751
    header[1] |= 1 << 7; /* set masking flag */
9752
    mg_send(nc, header, header_len);
9753
    ctx->mask = mg_ws_random_mask();
9754
    mg_send(nc, &ctx->mask, sizeof(ctx->mask));
9755
    ctx->pos = nc->send_mbuf.len;
9756
  } else {
9757
    mg_send(nc, header, header_len);
9758
    ctx->pos = 0;
9759
  }
9760
}
9761

    
9762
static void mg_ws_mask_frame(struct mbuf *mbuf, struct ws_mask_ctx *ctx) {
9763
  size_t i;
9764
  if (ctx->pos == 0) return;
9765
  for (i = 0; i < (mbuf->len - ctx->pos); i++) {
9766
    mbuf->buf[ctx->pos + i] ^= ((char *) &ctx->mask)[i % 4];
9767
  }
9768
}
9769

    
9770
void mg_send_websocket_frame(struct mg_connection *nc, int op, const void *data,
9771
                             size_t len) {
9772
  struct ws_mask_ctx ctx;
9773
  DBG(("%p %d %d", nc, op, (int) len));
9774
  mg_send_ws_header(nc, op, len, &ctx);
9775
  mg_send(nc, data, len);
9776

    
9777
  mg_ws_mask_frame(&nc->send_mbuf, &ctx);
9778

    
9779
  if (op == WEBSOCKET_OP_CLOSE) {
9780
    nc->flags |= MG_F_SEND_AND_CLOSE;
9781
  }
9782
}
9783

    
9784
void mg_send_websocket_framev(struct mg_connection *nc, int op,
9785
                              const struct mg_str *strv, int strvcnt) {
9786
  struct ws_mask_ctx ctx;
9787
  int i;
9788
  int len = 0;
9789
  for (i = 0; i < strvcnt; i++) {
9790
    len += strv[i].len;
9791
  }
9792

    
9793
  mg_send_ws_header(nc, op, len, &ctx);
9794

    
9795
  for (i = 0; i < strvcnt; i++) {
9796
    mg_send(nc, strv[i].p, strv[i].len);
9797
  }
9798

    
9799
  mg_ws_mask_frame(&nc->send_mbuf, &ctx);
9800

    
9801
  if (op == WEBSOCKET_OP_CLOSE) {
9802
    nc->flags |= MG_F_SEND_AND_CLOSE;
9803
  }
9804
}
9805

    
9806
void mg_printf_websocket_frame(struct mg_connection *nc, int op,
9807
                               const char *fmt, ...) {
9808
  char mem[MG_VPRINTF_BUFFER_SIZE], *buf = mem;
9809
  va_list ap;
9810
  int len;
9811

    
9812
  va_start(ap, fmt);
9813
  if ((len = mg_avprintf(&buf, sizeof(mem), fmt, ap)) > 0) {
9814
    mg_send_websocket_frame(nc, op, buf, len);
9815
  }
9816
  va_end(ap);
9817

    
9818
  if (buf != mem && buf != NULL) {
9819
    MG_FREE(buf);
9820
  }
9821
}
9822

    
9823
MG_INTERNAL void mg_ws_handler(struct mg_connection *nc, int ev,
9824
                               void *ev_data MG_UD_ARG(void *user_data)) {
9825
  mg_call(nc, nc->handler, nc->user_data, ev, ev_data);
9826

    
9827
  switch (ev) {
9828
    case MG_EV_RECV:
9829
      do {
9830
      } while (mg_deliver_websocket_data(nc));
9831
      break;
9832
    case MG_EV_POLL:
9833
      /* Ping idle websocket connections */
9834
      {
9835
        time_t now = *(time_t *) ev_data;
9836
        if (nc->flags & MG_F_IS_WEBSOCKET &&
9837
            now > nc->last_io_time + MG_WEBSOCKET_PING_INTERVAL_SECONDS) {
9838
          mg_send_websocket_frame(nc, WEBSOCKET_OP_PING, "", 0);
9839
        }
9840
      }
9841
      break;
9842
    default:
9843
      break;
9844
  }
9845
#if MG_ENABLE_CALLBACK_USERDATA
9846
  (void) user_data;
9847
#endif
9848
}
9849

    
9850
#ifndef MG_EXT_SHA1
9851
void mg_hash_sha1_v(size_t num_msgs, const uint8_t *msgs[],
9852
                    const size_t *msg_lens, uint8_t *digest) {
9853
  size_t i;
9854
  cs_sha1_ctx sha_ctx;
9855
  cs_sha1_init(&sha_ctx);
9856
  for (i = 0; i < num_msgs; i++) {
9857
    cs_sha1_update(&sha_ctx, msgs[i], msg_lens[i]);
9858
  }
9859
  cs_sha1_final(digest, &sha_ctx);
9860
}
9861
#else
9862
extern void mg_hash_sha1_v(size_t num_msgs, const uint8_t *msgs[],
9863
                           const size_t *msg_lens, uint8_t *digest);
9864
#endif
9865

    
9866
MG_INTERNAL void mg_ws_handshake(struct mg_connection *nc,
9867
                                 const struct mg_str *key,
9868
                                 struct http_message *hm) {
9869
  static const char *magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
9870
  const uint8_t *msgs[2] = {(const uint8_t *) key->p, (const uint8_t *) magic};
9871
  const size_t msg_lens[2] = {key->len, 36};
9872
  unsigned char sha[20];
9873
  char b64_sha[30];
9874
  struct mg_str *s;
9875

    
9876
  mg_hash_sha1_v(2, msgs, msg_lens, sha);
9877
  mg_base64_encode(sha, sizeof(sha), b64_sha);
9878
  mg_printf(nc, "%s",
9879
            "HTTP/1.1 101 Switching Protocols\r\n"
9880
            "Upgrade: websocket\r\n"
9881
            "Connection: Upgrade\r\n");
9882

    
9883
  s = mg_get_http_header(hm, "Sec-WebSocket-Protocol");
9884
  if (s != NULL) {
9885
    mg_printf(nc, "Sec-WebSocket-Protocol: %.*s\r\n", (int) s->len, s->p);
9886
  }
9887
  mg_printf(nc, "Sec-WebSocket-Accept: %s%s", b64_sha, "\r\n\r\n");
9888

    
9889
  DBG(("%p %.*s %s", nc, (int) key->len, key->p, b64_sha));
9890
}
9891

    
9892
void mg_send_websocket_handshake2(struct mg_connection *nc, const char *path,
9893
                                  const char *host, const char *protocol,
9894
                                  const char *extra_headers) {
9895
  mg_send_websocket_handshake3(nc, path, host, protocol, extra_headers, NULL,
9896
                               NULL);
9897
}
9898

    
9899
void mg_send_websocket_handshake3(struct mg_connection *nc, const char *path,
9900
                                  const char *host, const char *protocol,
9901
                                  const char *extra_headers, const char *user,
9902
                                  const char *pass) {
9903
  mg_send_websocket_handshake3v(nc, mg_mk_str(path), mg_mk_str(host),
9904
                                mg_mk_str(protocol), mg_mk_str(extra_headers),
9905
                                mg_mk_str(user), mg_mk_str(pass));
9906
}
9907

    
9908
void mg_send_websocket_handshake3v(struct mg_connection *nc,
9909
                                   const struct mg_str path,
9910
                                   const struct mg_str host,
9911
                                   const struct mg_str protocol,
9912
                                   const struct mg_str extra_headers,
9913
                                   const struct mg_str user,
9914
                                   const struct mg_str pass) {
9915
  struct mbuf auth;
9916
  char key[25];
9917
  uint32_t nonce[4];
9918
  nonce[0] = mg_ws_random_mask();
9919
  nonce[1] = mg_ws_random_mask();
9920
  nonce[2] = mg_ws_random_mask();
9921
  nonce[3] = mg_ws_random_mask();
9922
  mg_base64_encode((unsigned char *) &nonce, sizeof(nonce), key);
9923

    
9924
  mbuf_init(&auth, 0);
9925
  if (user.len > 0) {
9926
    mg_basic_auth_header(user, pass, &auth);
9927
  }
9928

    
9929
  /*
9930
   * NOTE: the  (auth.buf == NULL ? "" : auth.buf) is because cc3200 libc is
9931
   * broken: it doesn't like zero length to be passed to %.*s
9932
   * i.e. sprintf("f%.*so", (int)0, NULL), yields `f\0o`.
9933
   * because it handles NULL specially (and incorrectly).
9934
   */
9935
  mg_printf(nc,
9936
            "GET %.*s HTTP/1.1\r\n"
9937
            "Upgrade: websocket\r\n"
9938
            "Connection: Upgrade\r\n"
9939
            "%.*s"
9940
            "Sec-WebSocket-Version: 13\r\n"
9941
            "Sec-WebSocket-Key: %s\r\n",
9942
            (int) path.len, path.p, (int) auth.len,
9943
            (auth.buf == NULL ? "" : auth.buf), key);
9944

    
9945
  /* TODO(mkm): take default hostname from http proto data if host == NULL */
9946
  if (host.len > 0) {
9947
    int host_len = (int) (path.p - host.p); /* Account for possible :PORT */
9948
    mg_printf(nc, "Host: %.*s\r\n", host_len, host.p);
9949
  }
9950
  if (protocol.len > 0) {
9951
    mg_printf(nc, "Sec-WebSocket-Protocol: %.*s\r\n", (int) protocol.len,
9952
              protocol.p);
9953
  }
9954
  if (extra_headers.len > 0) {
9955
    mg_printf(nc, "%.*s", (int) extra_headers.len, extra_headers.p);
9956
  }
9957
  mg_printf(nc, "\r\n");
9958

    
9959
  mbuf_free(&auth);
9960
}
9961

    
9962
void mg_send_websocket_handshake(struct mg_connection *nc, const char *path,
9963
                                 const char *extra_headers) {
9964
  struct mg_str null_str = MG_NULL_STR;
9965
  mg_send_websocket_handshake3v(
9966
      nc, mg_mk_str(path), null_str /* host */, null_str /* protocol */,
9967
      mg_mk_str(extra_headers), null_str /* user */, null_str /* pass */);
9968
}
9969

    
9970
struct mg_connection *mg_connect_ws_opt(
9971
    struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data),
9972
    struct mg_connect_opts opts, const char *url, const char *protocol,
9973
    const char *extra_headers) {
9974
  struct mg_str null_str = MG_NULL_STR;
9975
  struct mg_str host = MG_NULL_STR, path = MG_NULL_STR, user_info = MG_NULL_STR;
9976
  struct mg_connection *nc =
9977
      mg_connect_http_base(mgr, MG_CB(ev_handler, user_data), opts, "http",
9978
                           "ws", "https", "wss", url, &path, &user_info, &host);
9979
  if (nc != NULL) {
9980
    mg_send_websocket_handshake3v(nc, path, host, mg_mk_str(protocol),
9981
                                  mg_mk_str(extra_headers), user_info,
9982
                                  null_str);
9983
  }
9984
  return nc;
9985
}
9986

    
9987
struct mg_connection *mg_connect_ws(
9988
    struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data),
9989
    const char *url, const char *protocol, const char *extra_headers) {
9990
  struct mg_connect_opts opts;
9991
  memset(&opts, 0, sizeof(opts));
9992
  return mg_connect_ws_opt(mgr, MG_CB(ev_handler, user_data), opts, url,
9993
                           protocol, extra_headers);
9994
}
9995
#endif /* MG_ENABLE_HTTP && MG_ENABLE_HTTP_WEBSOCKET */
9996
#ifdef MG_MODULE_LINES
9997
#line 1 "mongoose/src/mg_util.c"
9998
#endif
9999
/*
10000
 * Copyright (c) 2014 Cesanta Software Limited
10001
 * All rights reserved
10002
 */
10003

    
10004
/* Amalgamated: #include "common/cs_base64.h" */
10005
/* Amalgamated: #include "mg_internal.h" */
10006
/* Amalgamated: #include "mg_util.h" */
10007

    
10008
/* For platforms with limited libc */
10009
#ifndef MAX
10010
#define MAX(a, b) ((a) > (b) ? (a) : (b))
10011
#endif
10012

    
10013
const char *mg_skip(const char *s, const char *end, const char *delims,
10014
                    struct mg_str *v) {
10015
  v->p = s;
10016
  while (s < end && strchr(delims, *(unsigned char *) s) == NULL) s++;
10017
  v->len = s - v->p;
10018
  while (s < end && strchr(delims, *(unsigned char *) s) != NULL) s++;
10019
  return s;
10020
}
10021

    
10022
#if MG_ENABLE_FILESYSTEM && !defined(MG_USER_FILE_FUNCTIONS)
10023
int mg_stat(const char *path, cs_stat_t *st) {
10024
#ifdef _WIN32
10025
  wchar_t wpath[MG_MAX_PATH];
10026
  to_wchar(path, wpath, ARRAY_SIZE(wpath));
10027
  DBG(("[%ls] -> %d", wpath, _wstati64(wpath, st)));
10028
  return _wstati64(wpath, st);
10029
#else
10030
  return stat(path, st);
10031
#endif
10032
}
10033

    
10034
FILE *mg_fopen(const char *path, const char *mode) {
10035
#ifdef _WIN32
10036
  wchar_t wpath[MG_MAX_PATH], wmode[10];
10037
  to_wchar(path, wpath, ARRAY_SIZE(wpath));
10038
  to_wchar(mode, wmode, ARRAY_SIZE(wmode));
10039
  return _wfopen(wpath, wmode);
10040
#else
10041
  return fopen(path, mode);
10042
#endif
10043
}
10044

    
10045
int mg_open(const char *path, int flag, int mode) { /* LCOV_EXCL_LINE */
10046
#if defined(_WIN32) && !defined(WINCE)
10047
  wchar_t wpath[MG_MAX_PATH];
10048
  to_wchar(path, wpath, ARRAY_SIZE(wpath));
10049
  return _wopen(wpath, flag, mode);
10050
#else
10051
  return open(path, flag, mode); /* LCOV_EXCL_LINE */
10052
#endif
10053
}
10054

    
10055
size_t mg_fread(void *ptr, size_t size, size_t count, FILE *f) {
10056
  return fread(ptr, size, count, f);
10057
}
10058

    
10059
size_t mg_fwrite(const void *ptr, size_t size, size_t count, FILE *f) {
10060
  return fwrite(ptr, size, count, f);
10061
}
10062
#endif
10063

    
10064
void mg_base64_encode(const unsigned char *src, int src_len, char *dst) {
10065
  cs_base64_encode(src, src_len, dst);
10066
}
10067

    
10068
int mg_base64_decode(const unsigned char *s, int len, char *dst) {
10069
  return cs_base64_decode(s, len, dst, NULL);
10070
}
10071

    
10072
#if MG_ENABLE_THREADS
10073
void *mg_start_thread(void *(*f)(void *), void *p) {
10074
#ifdef WINCE
10075
  return (void *) CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) f, p, 0, NULL);
10076
#elif defined(_WIN32)
10077
  return (void *) _beginthread((void(__cdecl *) (void *) ) f, 0, p);
10078
#else
10079
  pthread_t thread_id = (pthread_t) 0;
10080
  pthread_attr_t attr;
10081

    
10082
  (void) pthread_attr_init(&attr);
10083
  (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
10084

    
10085
#if defined(MG_STACK_SIZE) && MG_STACK_SIZE > 1
10086
  (void) pthread_attr_setstacksize(&attr, MG_STACK_SIZE);
10087
#endif
10088

    
10089
  pthread_create(&thread_id, &attr, f, p);
10090
  pthread_attr_destroy(&attr);
10091

    
10092
  return (void *) thread_id;
10093
#endif
10094
}
10095
#endif /* MG_ENABLE_THREADS */
10096

    
10097
/* Set close-on-exec bit for a given socket. */
10098
void mg_set_close_on_exec(sock_t sock) {
10099
#if defined(_WIN32) && !defined(WINCE)
10100
  (void) SetHandleInformation((HANDLE) sock, HANDLE_FLAG_INHERIT, 0);
10101
#elif defined(__unix__)
10102
  fcntl(sock, F_SETFD, FD_CLOEXEC);
10103
#else
10104
  (void) sock;
10105
#endif
10106
}
10107

    
10108
int mg_sock_addr_to_str(const union socket_address *sa, char *buf, size_t len,
10109
                        int flags) {
10110
  int is_v6;
10111
  if (buf == NULL || len <= 0) return 0;
10112
  memset(buf, 0, len);
10113
#if MG_ENABLE_IPV6
10114
  is_v6 = sa->sa.sa_family == AF_INET6;
10115
#else
10116
  is_v6 = 0;
10117
#endif
10118
  if (flags & MG_SOCK_STRINGIFY_IP) {
10119
#if MG_ENABLE_IPV6
10120
    const void *addr = NULL;
10121
    char *start = buf;
10122
    socklen_t capacity = len;
10123
    if (!is_v6) {
10124
      addr = &sa->sin.sin_addr;
10125
    } else {
10126
      addr = (void *) &sa->sin6.sin6_addr;
10127
      if (flags & MG_SOCK_STRINGIFY_PORT) {
10128
        *buf = '[';
10129
        start++;
10130
        capacity--;
10131
      }
10132
    }
10133
    if (inet_ntop(sa->sa.sa_family, addr, start, capacity) == NULL) {
10134
      goto cleanup;
10135
    }
10136
#elif defined(_WIN32) || MG_LWIP || (MG_NET_IF == MG_NET_IF_PIC32)
10137
    /* Only Windoze Vista (and newer) have inet_ntop() */
10138
    char *addr_str = inet_ntoa(sa->sin.sin_addr);
10139
    if (addr_str != NULL) {
10140
      strncpy(buf, inet_ntoa(sa->sin.sin_addr), len - 1);
10141
    } else {
10142
      goto cleanup;
10143
    }
10144
#else
10145
    if (inet_ntop(AF_INET, (void *) &sa->sin.sin_addr, buf, len - 1) == NULL) {
10146
      goto cleanup;
10147
    }
10148
#endif
10149
  }
10150
  if (flags & MG_SOCK_STRINGIFY_PORT) {
10151
    int port = ntohs(sa->sin.sin_port);
10152
    if (flags & MG_SOCK_STRINGIFY_IP) {
10153
      int buf_len = strlen(buf);
10154
      snprintf(buf + buf_len, len - (buf_len + 1), "%s:%d", (is_v6 ? "]" : ""),
10155
               port);
10156
    } else {
10157
      snprintf(buf, len, "%d", port);
10158
    }
10159
  }
10160

    
10161
  return strlen(buf);
10162

    
10163
cleanup:
10164
  *buf = '\0';
10165
  return 0;
10166
}
10167

    
10168
int mg_conn_addr_to_str(struct mg_connection *nc, char *buf, size_t len,
10169
                        int flags) {
10170
  union socket_address sa;
10171
  memset(&sa, 0, sizeof(sa));
10172
  mg_if_get_conn_addr(nc, flags & MG_SOCK_STRINGIFY_REMOTE, &sa);
10173
  return mg_sock_addr_to_str(&sa, buf, len, flags);
10174
}
10175

    
10176
#if MG_ENABLE_HEXDUMP
10177
static int mg_hexdump_n(const void *buf, int len, char *dst, int dst_len,
10178
                        int offset) {
10179
  const unsigned char *p = (const unsigned char *) buf;
10180
  char ascii[17] = "";
10181
  int i, idx, n = 0;
10182

    
10183
  for (i = 0; i < len; i++) {
10184
    idx = i % 16;
10185
    if (idx == 0) {
10186
      if (i > 0) n += snprintf(dst + n, MAX(dst_len - n, 0), "  %s\n", ascii);
10187
      n += snprintf(dst + n, MAX(dst_len - n, 0), "%04x ", i + offset);
10188
    }
10189
    if (dst_len - n < 0) {
10190
      return n;
10191
    }
10192
    n += snprintf(dst + n, MAX(dst_len - n, 0), " %02x", p[i]);
10193
    ascii[idx] = p[i] < 0x20 || p[i] > 0x7e ? '.' : p[i];
10194
    ascii[idx + 1] = '\0';
10195
  }
10196

    
10197
  while (i++ % 16) n += snprintf(dst + n, MAX(dst_len - n, 0), "%s", "   ");
10198
  n += snprintf(dst + n, MAX(dst_len - n, 0), "  %s\n", ascii);
10199

    
10200
  return n;
10201
}
10202

    
10203
int mg_hexdump(const void *buf, int len, char *dst, int dst_len) {
10204
  return mg_hexdump_n(buf, len, dst, dst_len, 0);
10205
}
10206

    
10207
void mg_hexdumpf(FILE *fp, const void *buf, int len) {
10208
  char tmp[80];
10209
  int offset = 0, n;
10210
  while (len > 0) {
10211
    n = (len < 16 ? len : 16);
10212
    mg_hexdump_n(((const char *) buf) + offset, n, tmp, sizeof(tmp), offset);
10213
    fputs(tmp, fp);
10214
    offset += n;
10215
    len -= n;
10216
  }
10217
}
10218

    
10219
void mg_hexdump_connection(struct mg_connection *nc, const char *path,
10220
                           const void *buf, int num_bytes, int ev) {
10221
  FILE *fp = NULL;
10222
  char src[60], dst[60];
10223
  const char *tag = NULL;
10224
  switch (ev) {
10225
    case MG_EV_RECV:
10226
      tag = "<-";
10227
      break;
10228
    case MG_EV_SEND:
10229
      tag = "->";
10230
      break;
10231
    case MG_EV_ACCEPT:
10232
      tag = "<A";
10233
      break;
10234
    case MG_EV_CONNECT:
10235
      tag = "C>";
10236
      break;
10237
    case MG_EV_CLOSE:
10238
      tag = "XX";
10239
      break;
10240
  }
10241
  if (tag == NULL) return; /* Don't log MG_EV_TIMER, etc */
10242

    
10243
  if (strcmp(path, "-") == 0) {
10244
    fp = stdout;
10245
  } else if (strcmp(path, "--") == 0) {
10246
    fp = stderr;
10247
#if MG_ENABLE_FILESYSTEM
10248
  } else {
10249
    fp = mg_fopen(path, "a");
10250
#endif
10251
  }
10252
  if (fp == NULL) return;
10253

    
10254
  mg_conn_addr_to_str(nc, src, sizeof(src),
10255
                      MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
10256
  mg_conn_addr_to_str(nc, dst, sizeof(dst), MG_SOCK_STRINGIFY_IP |
10257
                                                MG_SOCK_STRINGIFY_PORT |
10258
                                                MG_SOCK_STRINGIFY_REMOTE);
10259
  fprintf(fp, "%lu %p %s %s %s %d\n", (unsigned long) mg_time(), (void *) nc,
10260
          src, tag, dst, (int) num_bytes);
10261
  if (num_bytes > 0) {
10262
    mg_hexdumpf(fp, buf, num_bytes);
10263
  }
10264
  if (fp != stdout && fp != stderr) fclose(fp);
10265
}
10266
#endif
10267

    
10268
int mg_is_big_endian(void) {
10269
  static const int n = 1;
10270
  /* TODO(mkm) use compiletime check with 4-byte char literal */
10271
  return ((char *) &n)[0] == 0;
10272
}
10273

    
10274
DO_NOT_WARN_UNUSED MG_INTERNAL int mg_get_errno(void) {
10275
#ifndef WINCE
10276
  return errno;
10277
#else
10278
  /* TODO(alashkin): translate error codes? */
10279
  return GetLastError();
10280
#endif
10281
}
10282

    
10283
void mg_mbuf_append_base64_putc(char ch, void *user_data) {
10284
  struct mbuf *mbuf = (struct mbuf *) user_data;
10285
  mbuf_append(mbuf, &ch, sizeof(ch));
10286
}
10287

    
10288
void mg_mbuf_append_base64(struct mbuf *mbuf, const void *data, size_t len) {
10289
  struct cs_base64_ctx ctx;
10290
  cs_base64_init(&ctx, mg_mbuf_append_base64_putc, mbuf);
10291
  cs_base64_update(&ctx, (const char *) data, len);
10292
  cs_base64_finish(&ctx);
10293
}
10294

    
10295
void mg_basic_auth_header(const struct mg_str user, const struct mg_str pass,
10296
                          struct mbuf *buf) {
10297
  const char *header_prefix = "Authorization: Basic ";
10298
  const char *header_suffix = "\r\n";
10299

    
10300
  struct cs_base64_ctx ctx;
10301
  cs_base64_init(&ctx, mg_mbuf_append_base64_putc, buf);
10302

    
10303
  mbuf_append(buf, header_prefix, strlen(header_prefix));
10304

    
10305
  cs_base64_update(&ctx, user.p, user.len);
10306
  if (pass.len > 0) {
10307
    cs_base64_update(&ctx, ":", 1);
10308
    cs_base64_update(&ctx, pass.p, pass.len);
10309
  }
10310
  cs_base64_finish(&ctx);
10311
  mbuf_append(buf, header_suffix, strlen(header_suffix));
10312
}
10313

    
10314
struct mg_str mg_url_encode(const struct mg_str src) {
10315
  static const char *dont_escape = "._-$,;~()/";
10316
  static const char *hex = "0123456789abcdef";
10317
  size_t i = 0;
10318
  struct mbuf mb;
10319
  mbuf_init(&mb, src.len);
10320

    
10321
  for (i = 0; i < src.len; i++) {
10322
    const unsigned char c = *((const unsigned char *) src.p + i);
10323
    if (isalnum(c) || strchr(dont_escape, c) != NULL) {
10324
      mbuf_append(&mb, &c, 1);
10325
    } else {
10326
      mbuf_append(&mb, "%", 1);
10327
      mbuf_append(&mb, &hex[c >> 4], 1);
10328
      mbuf_append(&mb, &hex[c & 15], 1);
10329
    }
10330
  }
10331
  mbuf_append(&mb, "", 1);
10332
  mbuf_trim(&mb);
10333
  return mg_mk_str_n(mb.buf, mb.len - 1);
10334
}
10335
#ifdef MG_MODULE_LINES
10336
#line 1 "mongoose/src/mg_mqtt.c"
10337
#endif
10338
/*
10339
 * Copyright (c) 2014 Cesanta Software Limited
10340
 * All rights reserved
10341
 */
10342

    
10343
#if MG_ENABLE_MQTT
10344

    
10345
#include <string.h>
10346

    
10347
/* Amalgamated: #include "mg_internal.h" */
10348
/* Amalgamated: #include "mg_mqtt.h" */
10349

    
10350
static uint16_t getu16(const char *p) {
10351
  const uint8_t *up = (const uint8_t *) p;
10352
  return (up[0] << 8) + up[1];
10353
}
10354

    
10355
static const char *scanto(const char *p, struct mg_str *s) {
10356
  s->len = getu16(p);
10357
  s->p = p + 2;
10358
  return s->p + s->len;
10359
}
10360

    
10361
MG_INTERNAL int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm) {
10362
  uint8_t header;
10363
  size_t len = 0, len_len = 0;
10364
  const char *p, *end;
10365
  unsigned char lc = 0;
10366
  int cmd;
10367

    
10368
  if (io->len < 2) return MG_MQTT_ERROR_INCOMPLETE_MSG;
10369
  header = io->buf[0];
10370
  cmd = header >> 4;
10371

    
10372
  /* decode mqtt variable length */
10373
  len = len_len = 0;
10374
  p = io->buf + 1;
10375
  while ((size_t)(p - io->buf) < io->len) {
10376
    lc = *((const unsigned char *) p++);
10377
    len += (lc & 0x7f) << 7 * len_len;
10378
    len_len++;
10379
    if (!(lc & 0x80)) break;
10380
    if (len_len > 4) return MG_MQTT_ERROR_MALFORMED_MSG;
10381
  }
10382

    
10383
  end = p + len;
10384
  if (lc & 0x80 || len > (io->len - (p - io->buf))) {
10385
    return MG_MQTT_ERROR_INCOMPLETE_MSG;
10386
  }
10387

    
10388
  mm->cmd = cmd;
10389
  mm->qos = MG_MQTT_GET_QOS(header);
10390

    
10391
  switch (cmd) {
10392
    case MG_MQTT_CMD_CONNECT: {
10393
      p = scanto(p, &mm->protocol_name);
10394
      if (p > end - 4) return MG_MQTT_ERROR_MALFORMED_MSG;
10395
      mm->protocol_version = *(uint8_t *) p++;
10396
      mm->connect_flags = *(uint8_t *) p++;
10397
      mm->keep_alive_timer = getu16(p);
10398
      p += 2;
10399
      if (p >= end) return MG_MQTT_ERROR_MALFORMED_MSG;
10400
      p = scanto(p, &mm->client_id);
10401
      if (p > end) return MG_MQTT_ERROR_MALFORMED_MSG;
10402
      if (mm->connect_flags & MG_MQTT_HAS_WILL) {
10403
        if (p >= end) return MG_MQTT_ERROR_MALFORMED_MSG;
10404
        p = scanto(p, &mm->will_topic);
10405
      }
10406
      if (mm->connect_flags & MG_MQTT_HAS_WILL) {
10407
        if (p >= end) return MG_MQTT_ERROR_MALFORMED_MSG;
10408
        p = scanto(p, &mm->will_message);
10409
      }
10410
      if (mm->connect_flags & MG_MQTT_HAS_USER_NAME) {
10411
        if (p >= end) return MG_MQTT_ERROR_MALFORMED_MSG;
10412
        p = scanto(p, &mm->user_name);
10413
      }
10414
      if (mm->connect_flags & MG_MQTT_HAS_PASSWORD) {
10415
        if (p >= end) return MG_MQTT_ERROR_MALFORMED_MSG;
10416
        p = scanto(p, &mm->password);
10417
      }
10418
      if (p != end) return MG_MQTT_ERROR_MALFORMED_MSG;
10419

    
10420
      LOG(LL_DEBUG,
10421
          ("%d %2x %d proto [%.*s] client_id [%.*s] will_topic [%.*s] "
10422
           "will_msg [%.*s] user_name [%.*s] password [%.*s]",
10423
           (int) len, (int) mm->connect_flags, (int) mm->keep_alive_timer,
10424
           (int) mm->protocol_name.len, mm->protocol_name.p,
10425
           (int) mm->client_id.len, mm->client_id.p, (int) mm->will_topic.len,
10426
           mm->will_topic.p, (int) mm->will_message.len, mm->will_message.p,
10427
           (int) mm->user_name.len, mm->user_name.p, (int) mm->password.len,
10428
           mm->password.p));
10429
      break;
10430
    }
10431
    case MG_MQTT_CMD_CONNACK:
10432
      if (end - p < 2) return MG_MQTT_ERROR_MALFORMED_MSG;
10433
      mm->connack_ret_code = p[1];
10434
      break;
10435
    case MG_MQTT_CMD_PUBACK:
10436
    case MG_MQTT_CMD_PUBREC:
10437
    case MG_MQTT_CMD_PUBREL:
10438
    case MG_MQTT_CMD_PUBCOMP:
10439
    case MG_MQTT_CMD_SUBACK:
10440
      mm->message_id = getu16(p);
10441
      break;
10442
    case MG_MQTT_CMD_PUBLISH: {
10443
      p = scanto(p, &mm->topic);
10444
      if (p > end) return MG_MQTT_ERROR_MALFORMED_MSG;
10445
      if (mm->qos > 0) {
10446
        if (end - p < 2) return MG_MQTT_ERROR_MALFORMED_MSG;
10447
        mm->message_id = getu16(p);
10448
        p += 2;
10449
      }
10450
      mm->payload.p = p;
10451
      mm->payload.len = end - p;
10452
      break;
10453
    }
10454
    case MG_MQTT_CMD_SUBSCRIBE:
10455
      if (end - p < 2) return MG_MQTT_ERROR_MALFORMED_MSG;
10456
      mm->message_id = getu16(p);
10457
      p += 2;
10458
      /*
10459
       * topic expressions are left in the payload and can be parsed with
10460
       * `mg_mqtt_next_subscribe_topic`
10461
       */
10462
      mm->payload.p = p;
10463
      mm->payload.len = end - p;
10464
      break;
10465
    default:
10466
      /* Unhandled command */
10467
      break;
10468
  }
10469

    
10470
  mm->len = end - io->buf;
10471
  return mm->len;
10472
}
10473

    
10474
static void mqtt_handler(struct mg_connection *nc, int ev,
10475
                         void *ev_data MG_UD_ARG(void *user_data)) {
10476
  struct mbuf *io = &nc->recv_mbuf;
10477
  struct mg_mqtt_message mm;
10478
  memset(&mm, 0, sizeof(mm));
10479

    
10480
  nc->handler(nc, ev, ev_data MG_UD_ARG(user_data));
10481

    
10482
  switch (ev) {
10483
    case MG_EV_ACCEPT:
10484
      if (nc->proto_data == NULL) mg_set_protocol_mqtt(nc);
10485
      break;
10486
    case MG_EV_RECV: {
10487
      /* There can be multiple messages in the buffer, process them all. */
10488
      while (1) {
10489
        int len = parse_mqtt(io, &mm);
10490
        if (len < 0) {
10491
          if (len == MG_MQTT_ERROR_MALFORMED_MSG) {
10492
            /* Protocol error. */
10493
            nc->flags |= MG_F_CLOSE_IMMEDIATELY;
10494
          } else if (len == MG_MQTT_ERROR_INCOMPLETE_MSG) {
10495
            /* Not fully buffered, let's check if we have a chance to get more
10496
             * data later */
10497
            if (nc->recv_mbuf_limit > 0 &&
10498
                nc->recv_mbuf.len >= nc->recv_mbuf_limit) {
10499
              LOG(LL_ERROR, ("%p recv buffer (%lu bytes) exceeds the limit "
10500
                             "%lu bytes, and not drained, closing",
10501
                             nc, (unsigned long) nc->recv_mbuf.len,
10502
                             (unsigned long) nc->recv_mbuf_limit));
10503
              nc->flags |= MG_F_CLOSE_IMMEDIATELY;
10504
            }
10505
          } else {
10506
            /* Should never be here */
10507
            LOG(LL_ERROR, ("%p invalid len: %d, closing", nc, len));
10508
            nc->flags |= MG_F_CLOSE_IMMEDIATELY;
10509
          }
10510
          break;
10511
        }
10512

    
10513
        nc->handler(nc, MG_MQTT_EVENT_BASE + mm.cmd, &mm MG_UD_ARG(user_data));
10514
        mbuf_remove(io, len);
10515
      }
10516
      break;
10517
    }
10518
    case MG_EV_POLL: {
10519
      struct mg_mqtt_proto_data *pd =
10520
          (struct mg_mqtt_proto_data *) nc->proto_data;
10521
      double now = mg_time();
10522
      if (pd->keep_alive > 0 && pd->last_control_time > 0 &&
10523
          (now - pd->last_control_time) > pd->keep_alive) {
10524
        LOG(LL_DEBUG, ("Send PINGREQ"));
10525
        mg_mqtt_ping(nc);
10526
      }
10527
      break;
10528
    }
10529
  }
10530
}
10531

    
10532
static void mg_mqtt_proto_data_destructor(void *proto_data) {
10533
  MG_FREE(proto_data);
10534
}
10535

    
10536
int mg_mqtt_match_topic_expression(struct mg_str exp, struct mg_str topic) {
10537
  /* TODO(mkm): implement real matching */
10538
  if (memchr(exp.p, '#', exp.len)) {
10539
    /* exp `foo/#` will become `foo/` */
10540
    exp.len -= 1;
10541
    /*
10542
     * topic should be longer than the expression: e.g. topic `foo/bar` does
10543
     * match `foo/#`, but neither `foo` nor `foo/` do.
10544
     */
10545
    if (topic.len <= exp.len) {
10546
      return 0;
10547
    }
10548

    
10549
    /* Truncate topic so that it'll pass the next length check */
10550
    topic.len = exp.len;
10551
  }
10552
  if (topic.len != exp.len) {
10553
    return 0;
10554
  }
10555
  return strncmp(topic.p, exp.p, exp.len) == 0;
10556
}
10557

    
10558
int mg_mqtt_vmatch_topic_expression(const char *exp, struct mg_str topic) {
10559
  return mg_mqtt_match_topic_expression(mg_mk_str(exp), topic);
10560
}
10561

    
10562
void mg_set_protocol_mqtt(struct mg_connection *nc) {
10563
  nc->proto_handler = mqtt_handler;
10564
  nc->proto_data = MG_CALLOC(1, sizeof(struct mg_mqtt_proto_data));
10565
  nc->proto_data_destructor = mg_mqtt_proto_data_destructor;
10566
}
10567

    
10568
static void mg_mqtt_prepend_header(struct mg_connection *nc, uint8_t cmd,
10569
                                   uint8_t flags, size_t len) {
10570
  struct mg_mqtt_proto_data *pd = (struct mg_mqtt_proto_data *) nc->proto_data;
10571
  size_t off = nc->send_mbuf.len - len;
10572
  uint8_t header = cmd << 4 | (uint8_t) flags;
10573

    
10574
  uint8_t buf[1 + sizeof(size_t)];
10575
  uint8_t *vlen = &buf[1];
10576

    
10577
  assert(nc->send_mbuf.len >= len);
10578

    
10579
  buf[0] = header;
10580

    
10581
  /* mqtt variable length encoding */
10582
  do {
10583
    *vlen = len % 0x80;
10584
    len /= 0x80;
10585
    if (len > 0) *vlen |= 0x80;
10586
    vlen++;
10587
  } while (len > 0);
10588

    
10589
  mbuf_insert(&nc->send_mbuf, off, buf, vlen - buf);
10590
  pd->last_control_time = mg_time();
10591
}
10592

    
10593
void mg_send_mqtt_handshake(struct mg_connection *nc, const char *client_id) {
10594
  static struct mg_send_mqtt_handshake_opts opts;
10595
  mg_send_mqtt_handshake_opt(nc, client_id, opts);
10596
}
10597

    
10598
void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id,
10599
                                struct mg_send_mqtt_handshake_opts opts) {
10600
  uint16_t hlen, nlen, rem_len = 0;
10601
  struct mg_mqtt_proto_data *pd = (struct mg_mqtt_proto_data *) nc->proto_data;
10602

    
10603
  mg_send(nc, "\00\04MQTT\04", 7);
10604
  rem_len += 7;
10605

    
10606
  if (opts.user_name != NULL) {
10607
    opts.flags |= MG_MQTT_HAS_USER_NAME;
10608
  }
10609
  if (opts.password != NULL) {
10610
    opts.flags |= MG_MQTT_HAS_PASSWORD;
10611
  }
10612
  if (opts.will_topic != NULL && opts.will_message != NULL) {
10613
    opts.flags |= MG_MQTT_HAS_WILL;
10614
  }
10615
  if (opts.keep_alive == 0) {
10616
    opts.keep_alive = 60;
10617
  }
10618

    
10619
  mg_send(nc, &opts.flags, 1);
10620
  rem_len += 1;
10621

    
10622
  nlen = htons(opts.keep_alive);
10623
  mg_send(nc, &nlen, 2);
10624
  rem_len += 2;
10625

    
10626
  hlen = strlen(client_id);
10627
  nlen = htons((uint16_t) hlen);
10628
  mg_send(nc, &nlen, 2);
10629
  mg_send(nc, client_id, hlen);
10630
  rem_len += 2 + hlen;
10631

    
10632
  if (opts.flags & MG_MQTT_HAS_WILL) {
10633
    hlen = strlen(opts.will_topic);
10634
    nlen = htons((uint16_t) hlen);
10635
    mg_send(nc, &nlen, 2);
10636
    mg_send(nc, opts.will_topic, hlen);
10637
    rem_len += 2 + hlen;
10638

    
10639
    hlen = strlen(opts.will_message);
10640
    nlen = htons((uint16_t) hlen);
10641
    mg_send(nc, &nlen, 2);
10642
    mg_send(nc, opts.will_message, hlen);
10643
    rem_len += 2 + hlen;
10644
  }
10645

    
10646
  if (opts.flags & MG_MQTT_HAS_USER_NAME) {
10647
    hlen = strlen(opts.user_name);
10648
    nlen = htons((uint16_t) hlen);
10649
    mg_send(nc, &nlen, 2);
10650
    mg_send(nc, opts.user_name, hlen);
10651
    rem_len += 2 + hlen;
10652
  }
10653
  if (opts.flags & MG_MQTT_HAS_PASSWORD) {
10654
    hlen = strlen(opts.password);
10655
    nlen = htons((uint16_t) hlen);
10656
    mg_send(nc, &nlen, 2);
10657
    mg_send(nc, opts.password, hlen);
10658
    rem_len += 2 + hlen;
10659
  }
10660

    
10661
  mg_mqtt_prepend_header(nc, MG_MQTT_CMD_CONNECT, 0, rem_len);
10662

    
10663
  if (pd != NULL) {
10664
    pd->keep_alive = opts.keep_alive;
10665
  }
10666
}
10667

    
10668
void mg_mqtt_publish(struct mg_connection *nc, const char *topic,
10669
                     uint16_t message_id, int flags, const void *data,
10670
                     size_t len) {
10671
  size_t old_len = nc->send_mbuf.len;
10672

    
10673
  uint16_t topic_len = htons((uint16_t) strlen(topic));
10674
  uint16_t message_id_net = htons(message_id);
10675

    
10676
  mg_send(nc, &topic_len, 2);
10677
  mg_send(nc, topic, strlen(topic));
10678
  if (MG_MQTT_GET_QOS(flags) > 0) {
10679
    mg_send(nc, &message_id_net, 2);
10680
  }
10681
  mg_send(nc, data, len);
10682

    
10683
  mg_mqtt_prepend_header(nc, MG_MQTT_CMD_PUBLISH, flags,
10684
                         nc->send_mbuf.len - old_len);
10685
}
10686

    
10687
void mg_mqtt_subscribe(struct mg_connection *nc,
10688
                       const struct mg_mqtt_topic_expression *topics,
10689
                       size_t topics_len, uint16_t message_id) {
10690
  size_t old_len = nc->send_mbuf.len;
10691

    
10692
  uint16_t message_id_n = htons(message_id);
10693
  size_t i;
10694

    
10695
  mg_send(nc, (char *) &message_id_n, 2);
10696
  for (i = 0; i < topics_len; i++) {
10697
    uint16_t topic_len_n = htons((uint16_t) strlen(topics[i].topic));
10698
    mg_send(nc, &topic_len_n, 2);
10699
    mg_send(nc, topics[i].topic, strlen(topics[i].topic));
10700
    mg_send(nc, &topics[i].qos, 1);
10701
  }
10702

    
10703
  mg_mqtt_prepend_header(nc, MG_MQTT_CMD_SUBSCRIBE, MG_MQTT_QOS(1),
10704
                         nc->send_mbuf.len - old_len);
10705
}
10706

    
10707
int mg_mqtt_next_subscribe_topic(struct mg_mqtt_message *msg,
10708
                                 struct mg_str *topic, uint8_t *qos, int pos) {
10709
  unsigned char *buf = (unsigned char *) msg->payload.p + pos;
10710
  int new_pos;
10711

    
10712
  if ((size_t) pos >= msg->payload.len) return -1;
10713

    
10714
  topic->len = buf[0] << 8 | buf[1];
10715
  topic->p = (char *) buf + 2;
10716
  new_pos = pos + 2 + topic->len + 1;
10717
  if ((size_t) new_pos > msg->payload.len) return -1;
10718
  *qos = buf[2 + topic->len];
10719
  return new_pos;
10720
}
10721

    
10722
void mg_mqtt_unsubscribe(struct mg_connection *nc, char **topics,
10723
                         size_t topics_len, uint16_t message_id) {
10724
  size_t old_len = nc->send_mbuf.len;
10725

    
10726
  uint16_t message_id_n = htons(message_id);
10727
  size_t i;
10728

    
10729
  mg_send(nc, (char *) &message_id_n, 2);
10730
  for (i = 0; i < topics_len; i++) {
10731
    uint16_t topic_len_n = htons((uint16_t) strlen(topics[i]));
10732
    mg_send(nc, &topic_len_n, 2);
10733
    mg_send(nc, topics[i], strlen(topics[i]));
10734
  }
10735

    
10736
  mg_mqtt_prepend_header(nc, MG_MQTT_CMD_UNSUBSCRIBE, MG_MQTT_QOS(1),
10737
                         nc->send_mbuf.len - old_len);
10738
}
10739

    
10740
void mg_mqtt_connack(struct mg_connection *nc, uint8_t return_code) {
10741
  uint8_t unused = 0;
10742
  mg_send(nc, &unused, 1);
10743
  mg_send(nc, &return_code, 1);
10744
  mg_mqtt_prepend_header(nc, MG_MQTT_CMD_CONNACK, 0, 2);
10745
}
10746

    
10747
/*
10748
 * Sends a command which contains only a `message_id` and a QoS level of 1.
10749
 *
10750
 * Helper function.
10751
 */
10752
static void mg_send_mqtt_short_command(struct mg_connection *nc, uint8_t cmd,
10753
                                       uint16_t message_id) {
10754
  uint16_t message_id_net = htons(message_id);
10755
  uint8_t flags = (cmd == MG_MQTT_CMD_PUBREL ? 2 : 0);
10756
  mg_send(nc, &message_id_net, 2);
10757
  mg_mqtt_prepend_header(nc, cmd, flags, 2 /* len */);
10758
}
10759

    
10760
void mg_mqtt_puback(struct mg_connection *nc, uint16_t message_id) {
10761
  mg_send_mqtt_short_command(nc, MG_MQTT_CMD_PUBACK, message_id);
10762
}
10763

    
10764
void mg_mqtt_pubrec(struct mg_connection *nc, uint16_t message_id) {
10765
  mg_send_mqtt_short_command(nc, MG_MQTT_CMD_PUBREC, message_id);
10766
}
10767

    
10768
void mg_mqtt_pubrel(struct mg_connection *nc, uint16_t message_id) {
10769
  mg_send_mqtt_short_command(nc, MG_MQTT_CMD_PUBREL, message_id);
10770
}
10771

    
10772
void mg_mqtt_pubcomp(struct mg_connection *nc, uint16_t message_id) {
10773
  mg_send_mqtt_short_command(nc, MG_MQTT_CMD_PUBCOMP, message_id);
10774
}
10775

    
10776
void mg_mqtt_suback(struct mg_connection *nc, uint8_t *qoss, size_t qoss_len,
10777
                    uint16_t message_id) {
10778
  size_t i;
10779
  uint16_t message_id_net = htons(message_id);
10780
  mg_send(nc, &message_id_net, 2);
10781
  for (i = 0; i < qoss_len; i++) {
10782
    mg_send(nc, &qoss[i], 1);
10783
  }
10784
  mg_mqtt_prepend_header(nc, MG_MQTT_CMD_SUBACK, MG_MQTT_QOS(1), 2 + qoss_len);
10785
}
10786

    
10787
void mg_mqtt_unsuback(struct mg_connection *nc, uint16_t message_id) {
10788
  mg_send_mqtt_short_command(nc, MG_MQTT_CMD_UNSUBACK, message_id);
10789
}
10790

    
10791
void mg_mqtt_ping(struct mg_connection *nc) {
10792
  mg_mqtt_prepend_header(nc, MG_MQTT_CMD_PINGREQ, 0, 0);
10793
}
10794

    
10795
void mg_mqtt_pong(struct mg_connection *nc) {
10796
  mg_mqtt_prepend_header(nc, MG_MQTT_CMD_PINGRESP, 0, 0);
10797
}
10798

    
10799
void mg_mqtt_disconnect(struct mg_connection *nc) {
10800
  mg_mqtt_prepend_header(nc, MG_MQTT_CMD_DISCONNECT, 0, 0);
10801
}
10802

    
10803
#endif /* MG_ENABLE_MQTT */
10804
#ifdef MG_MODULE_LINES
10805
#line 1 "mongoose/src/mg_mqtt_server.c"
10806
#endif
10807
/*
10808
 * Copyright (c) 2014 Cesanta Software Limited
10809
 * All rights reserved
10810
 */
10811

    
10812
/* Amalgamated: #include "mg_internal.h" */
10813
/* Amalgamated: #include "mg_mqtt_server.h" */
10814

    
10815
#if MG_ENABLE_MQTT_BROKER
10816

    
10817
static void mg_mqtt_session_init(struct mg_mqtt_broker *brk,
10818
                                 struct mg_mqtt_session *s,
10819
                                 struct mg_connection *nc) {
10820
  s->brk = brk;
10821
  s->subscriptions = NULL;
10822
  s->num_subscriptions = 0;
10823
  s->nc = nc;
10824
}
10825

    
10826
static void mg_mqtt_add_session(struct mg_mqtt_session *s) {
10827
  LIST_INSERT_HEAD(&s->brk->sessions, s, link);
10828
}
10829

    
10830
static void mg_mqtt_remove_session(struct mg_mqtt_session *s) {
10831
  LIST_REMOVE(s, link);
10832
}
10833

    
10834
static void mg_mqtt_destroy_session(struct mg_mqtt_session *s) {
10835
  size_t i;
10836
  for (i = 0; i < s->num_subscriptions; i++) {
10837
    MG_FREE((void *) s->subscriptions[i].topic);
10838
  }
10839
  MG_FREE(s->subscriptions);
10840
  MG_FREE(s);
10841
}
10842

    
10843
static void mg_mqtt_close_session(struct mg_mqtt_session *s) {
10844
  mg_mqtt_remove_session(s);
10845
  mg_mqtt_destroy_session(s);
10846
}
10847

    
10848
void mg_mqtt_broker_init(struct mg_mqtt_broker *brk, void *user_data) {
10849
  LIST_INIT(&brk->sessions);
10850
  brk->user_data = user_data;
10851
}
10852

    
10853
static void mg_mqtt_broker_handle_connect(struct mg_mqtt_broker *brk,
10854
                                          struct mg_connection *nc) {
10855
  struct mg_mqtt_session *s =
10856
      (struct mg_mqtt_session *) MG_CALLOC(1, sizeof *s);
10857
  if (s == NULL) {
10858
    /* LCOV_EXCL_START */
10859
    mg_mqtt_connack(nc, MG_EV_MQTT_CONNACK_SERVER_UNAVAILABLE);
10860
    return;
10861
    /* LCOV_EXCL_STOP */
10862
  }
10863

    
10864
  /* TODO(mkm): check header (magic and version) */
10865

    
10866
  mg_mqtt_session_init(brk, s, nc);
10867
  nc->priv_2 = s;
10868
  mg_mqtt_add_session(s);
10869

    
10870
  mg_mqtt_connack(nc, MG_EV_MQTT_CONNACK_ACCEPTED);
10871
}
10872

    
10873
static void mg_mqtt_broker_handle_subscribe(struct mg_connection *nc,
10874
                                            struct mg_mqtt_message *msg) {
10875
  struct mg_mqtt_session *ss = (struct mg_mqtt_session *) nc->priv_2;
10876
  uint8_t qoss[MG_MQTT_MAX_SESSION_SUBSCRIPTIONS];
10877
  size_t num_subs = 0;
10878
  struct mg_str topic;
10879
  uint8_t qos;
10880
  int pos;
10881
  struct mg_mqtt_topic_expression *te;
10882

    
10883
  for (pos = 0;
10884
       (pos = mg_mqtt_next_subscribe_topic(msg, &topic, &qos, pos)) != -1;) {
10885
    if (num_subs >= sizeof(MG_MQTT_MAX_SESSION_SUBSCRIPTIONS) ||
10886
        (ss->num_subscriptions + num_subs >=
10887
         MG_MQTT_MAX_SESSION_SUBSCRIPTIONS)) {
10888
      nc->flags |= MG_F_CLOSE_IMMEDIATELY;
10889
      return;
10890
    }
10891
    qoss[num_subs++] = qos;
10892
  }
10893

    
10894
  if (num_subs > 0) {
10895
    te = (struct mg_mqtt_topic_expression *) MG_REALLOC(
10896
        ss->subscriptions,
10897
        sizeof(*ss->subscriptions) * (ss->num_subscriptions + num_subs));
10898
    if (te == NULL) {
10899
      nc->flags |= MG_F_CLOSE_IMMEDIATELY;
10900
      return;
10901
    }
10902
    ss->subscriptions = te;
10903
    for (pos = 0;
10904
         pos < (int) msg->payload.len &&
10905
             (pos = mg_mqtt_next_subscribe_topic(msg, &topic, &qos, pos)) != -1;
10906
         ss->num_subscriptions++) {
10907
      te = &ss->subscriptions[ss->num_subscriptions];
10908
      te->topic = (char *) MG_MALLOC(topic.len + 1);
10909
      te->qos = qos;
10910
      memcpy((char *) te->topic, topic.p, topic.len);
10911
      ((char *) te->topic)[topic.len] = '\0';
10912
    }
10913
  }
10914

    
10915
  if (pos == (int) msg->payload.len) {
10916
    mg_mqtt_suback(nc, qoss, num_subs, msg->message_id);
10917
  } else {
10918
    /* We did not fully parse the payload, something must be wrong. */
10919
    nc->flags |= MG_F_CLOSE_IMMEDIATELY;
10920
  }
10921
}
10922

    
10923
static void mg_mqtt_broker_handle_publish(struct mg_mqtt_broker *brk,
10924
                                          struct mg_mqtt_message *msg) {
10925
  struct mg_mqtt_session *s;
10926
  size_t i;
10927

    
10928
  for (s = mg_mqtt_next(brk, NULL); s != NULL; s = mg_mqtt_next(brk, s)) {
10929
    for (i = 0; i < s->num_subscriptions; i++) {
10930
      if (mg_mqtt_vmatch_topic_expression(s->subscriptions[i].topic,
10931
                                          msg->topic)) {
10932
        char buf[100], *p = buf;
10933
        mg_asprintf(&p, sizeof(buf), "%.*s", (int) msg->topic.len,
10934
                    msg->topic.p);
10935
        if (p == NULL) {
10936
          return;
10937
        }
10938
        mg_mqtt_publish(s->nc, p, 0, 0, msg->payload.p, msg->payload.len);
10939
        if (p != buf) {
10940
          MG_FREE(p);
10941
        }
10942
        break;
10943
      }
10944
    }
10945
  }
10946
}
10947

    
10948
void mg_mqtt_broker(struct mg_connection *nc, int ev, void *data) {
10949
  struct mg_mqtt_message *msg = (struct mg_mqtt_message *) data;
10950
  struct mg_mqtt_broker *brk;
10951

    
10952
  if (nc->listener) {
10953
    brk = (struct mg_mqtt_broker *) nc->listener->priv_2;
10954
  } else {
10955
    brk = (struct mg_mqtt_broker *) nc->priv_2;
10956
  }
10957

    
10958
  switch (ev) {
10959
    case MG_EV_ACCEPT:
10960
      if (nc->proto_data == NULL) mg_set_protocol_mqtt(nc);
10961
      nc->priv_2 = NULL; /* Clear up the inherited pointer to broker */
10962
      break;
10963
    case MG_EV_MQTT_CONNECT:
10964
      if (nc->priv_2 == NULL) {
10965
        mg_mqtt_broker_handle_connect(brk, nc);
10966
      } else {
10967
        /* Repeated CONNECT */
10968
        nc->flags |= MG_F_CLOSE_IMMEDIATELY;
10969
      }
10970
      break;
10971
    case MG_EV_MQTT_SUBSCRIBE:
10972
      if (nc->priv_2 != NULL) {
10973
        mg_mqtt_broker_handle_subscribe(nc, msg);
10974
      } else {
10975
        /* Subscribe before CONNECT */
10976
        nc->flags |= MG_F_CLOSE_IMMEDIATELY;
10977
      }
10978
      break;
10979
    case MG_EV_MQTT_PUBLISH:
10980
      if (nc->priv_2 != NULL) {
10981
        mg_mqtt_broker_handle_publish(brk, msg);
10982
      } else {
10983
        /* Publish before CONNECT */
10984
        nc->flags |= MG_F_CLOSE_IMMEDIATELY;
10985
      }
10986
      break;
10987
    case MG_EV_CLOSE:
10988
      if (nc->listener && nc->priv_2 != NULL) {
10989
        mg_mqtt_close_session((struct mg_mqtt_session *) nc->priv_2);
10990
      }
10991
      break;
10992
  }
10993
}
10994

    
10995
struct mg_mqtt_session *mg_mqtt_next(struct mg_mqtt_broker *brk,
10996
                                     struct mg_mqtt_session *s) {
10997
  return s == NULL ? LIST_FIRST(&brk->sessions) : LIST_NEXT(s, link);
10998
}
10999

    
11000
#endif /* MG_ENABLE_MQTT_BROKER */
11001
#ifdef MG_MODULE_LINES
11002
#line 1 "mongoose/src/mg_dns.c"
11003
#endif
11004
/*
11005
 * Copyright (c) 2014 Cesanta Software Limited
11006
 * All rights reserved
11007
 */
11008

    
11009
#if MG_ENABLE_DNS
11010

    
11011
/* Amalgamated: #include "mg_internal.h" */
11012
/* Amalgamated: #include "mg_dns.h" */
11013

    
11014
static int mg_dns_tid = 0xa0;
11015

    
11016
struct mg_dns_header {
11017
  uint16_t transaction_id;
11018
  uint16_t flags;
11019
  uint16_t num_questions;
11020
  uint16_t num_answers;
11021
  uint16_t num_authority_prs;
11022
  uint16_t num_other_prs;
11023
};
11024

    
11025
struct mg_dns_resource_record *mg_dns_next_record(
11026
    struct mg_dns_message *msg, int query,
11027
    struct mg_dns_resource_record *prev) {
11028
  struct mg_dns_resource_record *rr;
11029

    
11030
  for (rr = (prev == NULL ? msg->answers : prev + 1);
11031
       rr - msg->answers < msg->num_answers; rr++) {
11032
    if (rr->rtype == query) {
11033
      return rr;
11034
    }
11035
  }
11036
  return NULL;
11037
}
11038

    
11039
int mg_dns_parse_record_data(struct mg_dns_message *msg,
11040
                             struct mg_dns_resource_record *rr, void *data,
11041
                             size_t data_len) {
11042
  switch (rr->rtype) {
11043
    case MG_DNS_A_RECORD:
11044
      if (data_len < sizeof(struct in_addr)) {
11045
        return -1;
11046
      }
11047
      if (rr->rdata.p + data_len > msg->pkt.p + msg->pkt.len) {
11048
        return -1;
11049
      }
11050
      memcpy(data, rr->rdata.p, data_len);
11051
      return 0;
11052
#if MG_ENABLE_IPV6
11053
    case MG_DNS_AAAA_RECORD:
11054
      if (data_len < sizeof(struct in6_addr)) {
11055
        return -1; /* LCOV_EXCL_LINE */
11056
      }
11057
      memcpy(data, rr->rdata.p, data_len);
11058
      return 0;
11059
#endif
11060
    case MG_DNS_CNAME_RECORD:
11061
      mg_dns_uncompress_name(msg, &rr->rdata, (char *) data, data_len);
11062
      return 0;
11063
  }
11064

    
11065
  return -1;
11066
}
11067

    
11068
int mg_dns_insert_header(struct mbuf *io, size_t pos,
11069
                         struct mg_dns_message *msg) {
11070
  struct mg_dns_header header;
11071

    
11072
  memset(&header, 0, sizeof(header));
11073
  header.transaction_id = msg->transaction_id;
11074
  header.flags = htons(msg->flags);
11075
  header.num_questions = htons(msg->num_questions);
11076
  header.num_answers = htons(msg->num_answers);
11077

    
11078
  return mbuf_insert(io, pos, &header, sizeof(header));
11079
}
11080

    
11081
int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg) {
11082
  unsigned char *begin, *end;
11083
  struct mg_dns_resource_record *last_q;
11084
  if (msg->num_questions <= 0) return 0;
11085
  begin = (unsigned char *) msg->pkt.p + sizeof(struct mg_dns_header);
11086
  last_q = &msg->questions[msg->num_questions - 1];
11087
  end = (unsigned char *) last_q->name.p + last_q->name.len + 4;
11088
  return mbuf_append(io, begin, end - begin);
11089
}
11090

    
11091
int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len) {
11092
  const char *s;
11093
  unsigned char n;
11094
  size_t pos = io->len;
11095

    
11096
  do {
11097
    if ((s = strchr(name, '.')) == NULL) {
11098
      s = name + len;
11099
    }
11100

    
11101
    if (s - name > 127) {
11102
      return -1; /* TODO(mkm) cover */
11103
    }
11104
    n = s - name;           /* chunk length */
11105
    mbuf_append(io, &n, 1); /* send length */
11106
    mbuf_append(io, name, n);
11107

    
11108
    if (*s == '.') {
11109
      n++;
11110
    }
11111

    
11112
    name += n;
11113
    len -= n;
11114
  } while (*s != '\0');
11115
  mbuf_append(io, "\0", 1); /* Mark end of host name */
11116

    
11117
  return io->len - pos;
11118
}
11119

    
11120
int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr,
11121
                         const char *name, size_t nlen, const void *rdata,
11122
                         size_t rlen) {
11123
  size_t pos = io->len;
11124
  uint16_t u16;
11125
  uint32_t u32;
11126

    
11127
  if (rr->kind == MG_DNS_INVALID_RECORD) {
11128
    return -1; /* LCOV_EXCL_LINE */
11129
  }
11130

    
11131
  if (mg_dns_encode_name(io, name, nlen) == -1) {
11132
    return -1;
11133
  }
11134

    
11135
  u16 = htons(rr->rtype);
11136
  mbuf_append(io, &u16, 2);
11137
  u16 = htons(rr->rclass);
11138
  mbuf_append(io, &u16, 2);
11139

    
11140
  if (rr->kind == MG_DNS_ANSWER) {
11141
    u32 = htonl(rr->ttl);
11142
    mbuf_append(io, &u32, 4);
11143

    
11144
    if (rr->rtype == MG_DNS_CNAME_RECORD) {
11145
      int clen;
11146
      /* fill size after encoding */
11147
      size_t off = io->len;
11148
      mbuf_append(io, &u16, 2);
11149
      if ((clen = mg_dns_encode_name(io, (const char *) rdata, rlen)) == -1) {
11150
        return -1;
11151
      }
11152
      u16 = clen;
11153
      io->buf[off] = u16 >> 8;
11154
      io->buf[off + 1] = u16 & 0xff;
11155
    } else {
11156
      u16 = htons((uint16_t) rlen);
11157
      mbuf_append(io, &u16, 2);
11158
      mbuf_append(io, rdata, rlen);
11159
    }
11160
  }
11161

    
11162
  return io->len - pos;
11163
}
11164

    
11165
void mg_send_dns_query(struct mg_connection *nc, const char *name,
11166
                       int query_type) {
11167
  struct mg_dns_message *msg =
11168
      (struct mg_dns_message *) MG_CALLOC(1, sizeof(*msg));
11169
  struct mbuf pkt;
11170
  struct mg_dns_resource_record *rr = &msg->questions[0];
11171

    
11172
  DBG(("%s %d", name, query_type));
11173

    
11174
  mbuf_init(&pkt, 64 /* Start small, it'll grow as needed. */);
11175

    
11176
  msg->transaction_id = ++mg_dns_tid;
11177
  msg->flags = 0x100;
11178
  msg->num_questions = 1;
11179

    
11180
  mg_dns_insert_header(&pkt, 0, msg);
11181

    
11182
  rr->rtype = query_type;
11183
  rr->rclass = 1; /* Class: inet */
11184
  rr->kind = MG_DNS_QUESTION;
11185

    
11186
  if (mg_dns_encode_record(&pkt, rr, name, strlen(name), NULL, 0) == -1) {
11187
    /* TODO(mkm): return an error code */
11188
    goto cleanup; /* LCOV_EXCL_LINE */
11189
  }
11190

    
11191
  /* TCP DNS requires messages to be prefixed with len */
11192
  if (!(nc->flags & MG_F_UDP)) {
11193
    uint16_t len = htons((uint16_t) pkt.len);
11194
    mbuf_insert(&pkt, 0, &len, 2);
11195
  }
11196

    
11197
  mg_send(nc, pkt.buf, pkt.len);
11198
  mbuf_free(&pkt);
11199

    
11200
cleanup:
11201
  MG_FREE(msg);
11202
}
11203

    
11204
static unsigned char *mg_parse_dns_resource_record(
11205
    unsigned char *data, unsigned char *end, struct mg_dns_resource_record *rr,
11206
    int reply) {
11207
  unsigned char *name = data;
11208
  int chunk_len, data_len;
11209

    
11210
  while (data < end && (chunk_len = *data)) {
11211
    if (((unsigned char *) data)[0] & 0xc0) {
11212
      data += 1;
11213
      break;
11214
    }
11215
    data += chunk_len + 1;
11216
  }
11217

    
11218
  if (data > end - 5) {
11219
    return NULL;
11220
  }
11221

    
11222
  rr->name.p = (char *) name;
11223
  rr->name.len = data - name + 1;
11224
  data++;
11225

    
11226
  rr->rtype = data[0] << 8 | data[1];
11227
  data += 2;
11228

    
11229
  rr->rclass = data[0] << 8 | data[1];
11230
  data += 2;
11231

    
11232
  rr->kind = reply ? MG_DNS_ANSWER : MG_DNS_QUESTION;
11233
  if (reply) {
11234
    if (data >= end - 6) {
11235
      return NULL;
11236
    }
11237

    
11238
    rr->ttl = (uint32_t) data[0] << 24 | (uint32_t) data[1] << 16 |
11239
              data[2] << 8 | data[3];
11240
    data += 4;
11241

    
11242
    data_len = *data << 8 | *(data + 1);
11243
    data += 2;
11244

    
11245
    rr->rdata.p = (char *) data;
11246
    rr->rdata.len = data_len;
11247
    data += data_len;
11248
  }
11249
  return data;
11250
}
11251

    
11252
int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg) {
11253
  struct mg_dns_header *header = (struct mg_dns_header *) buf;
11254
  unsigned char *data = (unsigned char *) buf + sizeof(*header);
11255
  unsigned char *end = (unsigned char *) buf + len;
11256
  int i;
11257

    
11258
  memset(msg, 0, sizeof(*msg));
11259
  msg->pkt.p = buf;
11260
  msg->pkt.len = len;
11261

    
11262
  if (len < (int) sizeof(*header)) return -1;
11263

    
11264
  msg->transaction_id = header->transaction_id;
11265
  msg->flags = ntohs(header->flags);
11266
  msg->num_questions = ntohs(header->num_questions);
11267
  if (msg->num_questions > (int) ARRAY_SIZE(msg->questions)) {
11268
    msg->num_questions = (int) ARRAY_SIZE(msg->questions);
11269
  }
11270
  msg->num_answers = ntohs(header->num_answers);
11271
  if (msg->num_answers > (int) ARRAY_SIZE(msg->answers)) {
11272
    msg->num_answers = (int) ARRAY_SIZE(msg->answers);
11273
  }
11274

    
11275
  for (i = 0; i < msg->num_questions; i++) {
11276
    data = mg_parse_dns_resource_record(data, end, &msg->questions[i], 0);
11277
    if (data == NULL) return -1;
11278
  }
11279

    
11280
  for (i = 0; i < msg->num_answers; i++) {
11281
    data = mg_parse_dns_resource_record(data, end, &msg->answers[i], 1);
11282
    if (data == NULL) return -1;
11283
  }
11284

    
11285
  return 0;
11286
}
11287

    
11288
size_t mg_dns_uncompress_name(struct mg_dns_message *msg, struct mg_str *name,
11289
                              char *dst, int dst_len) {
11290
  int chunk_len, num_ptrs = 0;
11291
  char *old_dst = dst;
11292
  const unsigned char *data = (unsigned char *) name->p;
11293
  const unsigned char *end = (unsigned char *) msg->pkt.p + msg->pkt.len;
11294

    
11295
  if (data >= end) {
11296
    return 0;
11297
  }
11298

    
11299
  while ((chunk_len = *data++)) {
11300
    int leeway = dst_len - (dst - old_dst);
11301
    if (data >= end) {
11302
      return 0;
11303
    }
11304

    
11305
    if ((chunk_len & 0xc0) == 0xc0) {
11306
      uint16_t off = (data[-1] & (~0xc0)) << 8 | data[0];
11307
      if (off >= msg->pkt.len) {
11308
        return 0;
11309
      }
11310
      /* Basic circular loop avoidance: allow up to 16 pointer hops. */
11311
      if (++num_ptrs > 15) {
11312
        return 0;
11313
      }
11314
      data = (unsigned char *) msg->pkt.p + off;
11315
      continue;
11316
    }
11317
    if (chunk_len > 63) {
11318
      return 0;
11319
    }
11320
    if (chunk_len > leeway) {
11321
      chunk_len = leeway;
11322
    }
11323

    
11324
    if (data + chunk_len >= end) {
11325
      return 0;
11326
    }
11327

    
11328
    memcpy(dst, data, chunk_len);
11329
    data += chunk_len;
11330
    dst += chunk_len;
11331
    leeway -= chunk_len;
11332
    if (leeway == 0) {
11333
      return dst - old_dst;
11334
    }
11335
    *dst++ = '.';
11336
  }
11337

    
11338
  if (dst != old_dst) {
11339
    *--dst = 0;
11340
  }
11341
  return dst - old_dst;
11342
}
11343

    
11344
static void dns_handler(struct mg_connection *nc, int ev,
11345
                        void *ev_data MG_UD_ARG(void *user_data)) {
11346
  struct mbuf *io = &nc->recv_mbuf;
11347
  struct mg_dns_message msg;
11348

    
11349
  /* Pass low-level events to the user handler */
11350
  nc->handler(nc, ev, ev_data MG_UD_ARG(user_data));
11351

    
11352
  switch (ev) {
11353
    case MG_EV_RECV:
11354
      if (!(nc->flags & MG_F_UDP)) {
11355
        mbuf_remove(&nc->recv_mbuf, 2);
11356
      }
11357
      if (mg_parse_dns(nc->recv_mbuf.buf, nc->recv_mbuf.len, &msg) == -1) {
11358
        /* reply + recursion allowed + format error */
11359
        memset(&msg, 0, sizeof(msg));
11360
        msg.flags = 0x8081;
11361
        mg_dns_insert_header(io, 0, &msg);
11362
        if (!(nc->flags & MG_F_UDP)) {
11363
          uint16_t len = htons((uint16_t) io->len);
11364
          mbuf_insert(io, 0, &len, 2);
11365
        }
11366
        mg_send(nc, io->buf, io->len);
11367
      } else {
11368
        /* Call user handler with parsed message */
11369
        nc->handler(nc, MG_DNS_MESSAGE, &msg MG_UD_ARG(user_data));
11370
      }
11371
      mbuf_remove(io, io->len);
11372
      break;
11373
  }
11374
}
11375

    
11376
void mg_set_protocol_dns(struct mg_connection *nc) {
11377
  nc->proto_handler = dns_handler;
11378
}
11379

    
11380
#endif /* MG_ENABLE_DNS */
11381
#ifdef MG_MODULE_LINES
11382
#line 1 "mongoose/src/mg_dns_server.c"
11383
#endif
11384
/*
11385
 * Copyright (c) 2014 Cesanta Software Limited
11386
 * All rights reserved
11387
 */
11388

    
11389
#if MG_ENABLE_DNS_SERVER
11390

    
11391
/* Amalgamated: #include "mg_internal.h" */
11392
/* Amalgamated: #include "dns-server.h" */
11393

    
11394
struct mg_dns_reply mg_dns_create_reply(struct mbuf *io,
11395
                                        struct mg_dns_message *msg) {
11396
  struct mg_dns_reply rep;
11397
  rep.msg = msg;
11398
  rep.io = io;
11399
  rep.start = io->len;
11400

    
11401
  /* reply + recursion allowed */
11402
  msg->flags |= 0x8080;
11403
  mg_dns_copy_questions(io, msg);
11404

    
11405
  msg->num_answers = 0;
11406
  return rep;
11407
}
11408

    
11409
void mg_dns_send_reply(struct mg_connection *nc, struct mg_dns_reply *r) {
11410
  size_t sent = r->io->len - r->start;
11411
  mg_dns_insert_header(r->io, r->start, r->msg);
11412
  if (!(nc->flags & MG_F_UDP)) {
11413
    uint16_t len = htons((uint16_t) sent);
11414
    mbuf_insert(r->io, r->start, &len, 2);
11415
  }
11416

    
11417
  if (&nc->send_mbuf != r->io) {
11418
    mg_send(nc, r->io->buf + r->start, r->io->len - r->start);
11419
    r->io->len = r->start;
11420
  }
11421
}
11422

    
11423
int mg_dns_reply_record(struct mg_dns_reply *reply,
11424
                        struct mg_dns_resource_record *question,
11425
                        const char *name, int rtype, int ttl, const void *rdata,
11426
                        size_t rdata_len) {
11427
  struct mg_dns_message *msg = (struct mg_dns_message *) reply->msg;
11428
  char rname[512];
11429
  struct mg_dns_resource_record *ans = &msg->answers[msg->num_answers];
11430
  if (msg->num_answers >= MG_MAX_DNS_ANSWERS) {
11431
    return -1; /* LCOV_EXCL_LINE */
11432
  }
11433

    
11434
  if (name == NULL) {
11435
    name = rname;
11436
    rname[511] = 0;
11437
    mg_dns_uncompress_name(msg, &question->name, rname, sizeof(rname) - 1);
11438
  }
11439

    
11440
  *ans = *question;
11441
  ans->kind = MG_DNS_ANSWER;
11442
  ans->rtype = rtype;
11443
  ans->ttl = ttl;
11444

    
11445
  if (mg_dns_encode_record(reply->io, ans, name, strlen(name), rdata,
11446
                           rdata_len) == -1) {
11447
    return -1; /* LCOV_EXCL_LINE */
11448
  };
11449

    
11450
  msg->num_answers++;
11451
  return 0;
11452
}
11453

    
11454
#endif /* MG_ENABLE_DNS_SERVER */
11455
#ifdef MG_MODULE_LINES
11456
#line 1 "mongoose/src/mg_resolv.c"
11457
#endif
11458
/*
11459
 * Copyright (c) 2014 Cesanta Software Limited
11460
 * All rights reserved
11461
 */
11462

    
11463
#if MG_ENABLE_ASYNC_RESOLVER
11464

    
11465
/* Amalgamated: #include "mg_internal.h" */
11466
/* Amalgamated: #include "mg_resolv.h" */
11467

    
11468
#ifndef MG_DEFAULT_NAMESERVER
11469
#define MG_DEFAULT_NAMESERVER "8.8.8.8"
11470
#endif
11471

    
11472
struct mg_resolve_async_request {
11473
  char name[1024];
11474
  int query;
11475
  mg_resolve_callback_t callback;
11476
  void *data;
11477
  time_t timeout;
11478
  int max_retries;
11479
  enum mg_resolve_err err;
11480

    
11481
  /* state */
11482
  time_t last_time;
11483
  int retries;
11484
};
11485

    
11486
/*
11487
 * Find what nameserver to use.
11488
 *
11489
 * Return 0 if OK, -1 if error
11490
 */
11491
static int mg_get_ip_address_of_nameserver(char *name, size_t name_len) {
11492
  int ret = -1;
11493

    
11494
#ifdef _WIN32
11495
  int i;
11496
  LONG err;
11497
  HKEY hKey, hSub;
11498
  wchar_t subkey[512], value[128],
11499
      *key = L"SYSTEM\\ControlSet001\\Services\\Tcpip\\Parameters\\Interfaces";
11500

    
11501
  if ((err = RegOpenKeyExW(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hKey)) !=
11502
      ERROR_SUCCESS) {
11503
    fprintf(stderr, "cannot open reg key %S: %ld\n", key, err);
11504
    ret = -1;
11505
  } else {
11506
    for (ret = -1, i = 0; 1; i++) {
11507
      DWORD subkey_size = sizeof(subkey), type, len = sizeof(value);
11508
      if (RegEnumKeyExW(hKey, i, subkey, &subkey_size, NULL, NULL, NULL,
11509
                        NULL) != ERROR_SUCCESS) {
11510
        break;
11511
      }
11512
      if (RegOpenKeyExW(hKey, subkey, 0, KEY_READ, &hSub) == ERROR_SUCCESS &&
11513
          ((RegQueryValueExW(hSub, L"NameServer", 0, &type, (void *) value,
11514
                             &len) == ERROR_SUCCESS &&
11515
            value[0] != '\0') ||
11516
           (RegQueryValueExW(hSub, L"DhcpNameServer", 0, &type, (void *) value,
11517
                             &len) == ERROR_SUCCESS &&
11518
            value[0] != '\0'))) {
11519
        /*
11520
         * See https://github.com/cesanta/mongoose/issues/176
11521
         * The value taken from the registry can be empty, a single
11522
         * IP address, or multiple IP addresses separated by comma.
11523
         * If it's empty, check the next interface.
11524
         * If it's multiple IP addresses, take the first one.
11525
         */
11526
        wchar_t *comma = wcschr(value, ',');
11527
        if (comma != NULL) {
11528
          *comma = '\0';
11529
        }
11530
        /* %S will convert wchar_t -> char */
11531
        snprintf(name, name_len, "%S", value);
11532
        ret = 0;
11533
        RegCloseKey(hSub);
11534
        break;
11535
      }
11536
    }
11537
    RegCloseKey(hKey);
11538
  }
11539
#elif MG_ENABLE_FILESYSTEM && defined(MG_RESOLV_CONF_FILE_NAME)
11540
  FILE *fp;
11541
  char line[512];
11542

    
11543
  if ((fp = mg_fopen(MG_RESOLV_CONF_FILE_NAME, "r")) == NULL) {
11544
    ret = -1;
11545
  } else {
11546
    /* Try to figure out what nameserver to use */
11547
    for (ret = -1; fgets(line, sizeof(line), fp) != NULL;) {
11548
      unsigned int a, b, c, d;
11549
      if (sscanf(line, "nameserver %u.%u.%u.%u", &a, &b, &c, &d) == 4) {
11550
        snprintf(name, name_len, "%u.%u.%u.%u", a, b, c, d);
11551
        ret = 0;
11552
        break;
11553
      }
11554
    }
11555
    (void) fclose(fp);
11556
  }
11557
#else
11558
  snprintf(name, name_len, "%s", MG_DEFAULT_NAMESERVER);
11559
#endif /* _WIN32 */
11560

    
11561
  return ret;
11562
}
11563

    
11564
int mg_resolve_from_hosts_file(const char *name, union socket_address *usa) {
11565
#if MG_ENABLE_FILESYSTEM && defined(MG_HOSTS_FILE_NAME)
11566
  /* TODO(mkm) cache /etc/hosts */
11567
  FILE *fp;
11568
  char line[1024];
11569
  char *p;
11570
  char alias[256];
11571
  unsigned int a, b, c, d;
11572
  int len = 0;
11573

    
11574
  if ((fp = mg_fopen(MG_HOSTS_FILE_NAME, "r")) == NULL) {
11575
    return -1;
11576
  }
11577

    
11578
  for (; fgets(line, sizeof(line), fp) != NULL;) {
11579
    if (line[0] == '#') continue;
11580

    
11581
    if (sscanf(line, "%u.%u.%u.%u%n", &a, &b, &c, &d, &len) == 0) {
11582
      /* TODO(mkm): handle ipv6 */
11583
      continue;
11584
    }
11585
    for (p = line + len; sscanf(p, "%s%n", alias, &len) == 1; p += len) {
11586
      if (strcmp(alias, name) == 0) {
11587
        usa->sin.sin_addr.s_addr = htonl(a << 24 | b << 16 | c << 8 | d);
11588
        fclose(fp);
11589
        return 0;
11590
      }
11591
    }
11592
  }
11593

    
11594
  fclose(fp);
11595
#else
11596
  (void) name;
11597
  (void) usa;
11598
#endif
11599

    
11600
  return -1;
11601
}
11602

    
11603
static void mg_resolve_async_eh(struct mg_connection *nc, int ev,
11604
                                void *data MG_UD_ARG(void *user_data)) {
11605
  time_t now = (time_t) mg_time();
11606
  struct mg_resolve_async_request *req;
11607
  struct mg_dns_message *msg;
11608
  int first = 0;
11609
#if !MG_ENABLE_CALLBACK_USERDATA
11610
  void *user_data = nc->user_data;
11611
#endif
11612

    
11613
  if (ev != MG_EV_POLL) DBG(("ev=%d user_data=%p", ev, user_data));
11614

    
11615
  req = (struct mg_resolve_async_request *) user_data;
11616

    
11617
  if (req == NULL) {
11618
    return;
11619
  }
11620

    
11621
  switch (ev) {
11622
    case MG_EV_CONNECT:
11623
      /* don't depend on timer not being at epoch for sending out first req */
11624
      first = 1;
11625
    /* fallthrough */
11626
    case MG_EV_POLL:
11627
      if (req->retries > req->max_retries) {
11628
        req->err = MG_RESOLVE_EXCEEDED_RETRY_COUNT;
11629
        nc->flags |= MG_F_CLOSE_IMMEDIATELY;
11630
        break;
11631
      }
11632
      if (first || now - req->last_time >= req->timeout) {
11633
        mg_send_dns_query(nc, req->name, req->query);
11634
        req->last_time = now;
11635
        req->retries++;
11636
      }
11637
      break;
11638
    case MG_EV_RECV:
11639
      msg = (struct mg_dns_message *) MG_MALLOC(sizeof(*msg));
11640
      if (mg_parse_dns(nc->recv_mbuf.buf, *(int *) data, msg) == 0 &&
11641
          msg->num_answers > 0) {
11642
        req->callback(msg, req->data, MG_RESOLVE_OK);
11643
        nc->user_data = NULL;
11644
        MG_FREE(req);
11645
      } else {
11646
        req->err = MG_RESOLVE_NO_ANSWERS;
11647
      }
11648
      MG_FREE(msg);
11649
      nc->flags |= MG_F_CLOSE_IMMEDIATELY;
11650
      break;
11651
    case MG_EV_SEND:
11652
      /*
11653
       * If a send error occurs, prevent closing of the connection by the core.
11654
       * We will retry after timeout.
11655
       */
11656
      nc->flags &= ~MG_F_CLOSE_IMMEDIATELY;
11657
      mbuf_remove(&nc->send_mbuf, nc->send_mbuf.len);
11658
      break;
11659
    case MG_EV_TIMER:
11660
      req->err = MG_RESOLVE_TIMEOUT;
11661
      nc->flags |= MG_F_CLOSE_IMMEDIATELY;
11662
      break;
11663
    case MG_EV_CLOSE:
11664
      /* If we got here with request still not done, fire an error callback. */
11665
      if (req != NULL) {
11666
        char addr[32];
11667
        mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP);
11668
#ifdef MG_LOG_DNS_FAILURES
11669
        LOG(LL_ERROR, ("Failed to resolve '%s', server %s", req->name, addr));
11670
#endif
11671
        req->callback(NULL, req->data, req->err);
11672
        nc->user_data = NULL;
11673
        MG_FREE(req);
11674
      }
11675
      break;
11676
  }
11677
}
11678

    
11679
int mg_resolve_async(struct mg_mgr *mgr, const char *name, int query,
11680
                     mg_resolve_callback_t cb, void *data) {
11681
  struct mg_resolve_async_opts opts;
11682
  memset(&opts, 0, sizeof(opts));
11683
  return mg_resolve_async_opt(mgr, name, query, cb, data, opts);
11684
}
11685

    
11686
int mg_resolve_async_opt(struct mg_mgr *mgr, const char *name, int query,
11687
                         mg_resolve_callback_t cb, void *data,
11688
                         struct mg_resolve_async_opts opts) {
11689
  struct mg_resolve_async_request *req;
11690
  struct mg_connection *dns_nc;
11691
  const char *nameserver = opts.nameserver;
11692
  char dns_server_buff[17], nameserver_url[26];
11693

    
11694
  if (nameserver == NULL) {
11695
    nameserver = mgr->nameserver;
11696
  }
11697

    
11698
  DBG(("%s %d %p", name, query, opts.dns_conn));
11699

    
11700
  /* resolve with DNS */
11701
  req = (struct mg_resolve_async_request *) MG_CALLOC(1, sizeof(*req));
11702
  if (req == NULL) {
11703
    return -1;
11704
  }
11705

    
11706
  strncpy(req->name, name, sizeof(req->name));
11707
  req->name[sizeof(req->name) - 1] = '\0';
11708

    
11709
  req->query = query;
11710
  req->callback = cb;
11711
  req->data = data;
11712
  /* TODO(mkm): parse defaults out of resolve.conf */
11713
  req->max_retries = opts.max_retries ? opts.max_retries : 2;
11714
  req->timeout = opts.timeout ? opts.timeout : 5;
11715

    
11716
  /* Lazily initialize dns server */
11717
  if (nameserver == NULL) {
11718
    if (mg_get_ip_address_of_nameserver(dns_server_buff,
11719
                                        sizeof(dns_server_buff)) != -1) {
11720
      nameserver = dns_server_buff;
11721
    } else {
11722
      nameserver = MG_DEFAULT_NAMESERVER;
11723
    }
11724
  }
11725

    
11726
  snprintf(nameserver_url, sizeof(nameserver_url), "udp://%s:53", nameserver);
11727

    
11728
  dns_nc = mg_connect(mgr, nameserver_url, MG_CB(mg_resolve_async_eh, NULL));
11729
  if (dns_nc == NULL) {
11730
    MG_FREE(req);
11731
    return -1;
11732
  }
11733
  dns_nc->user_data = req;
11734
  if (opts.dns_conn != NULL) {
11735
    *opts.dns_conn = dns_nc;
11736
  }
11737

    
11738
  return 0;
11739
}
11740

    
11741
void mg_set_nameserver(struct mg_mgr *mgr, const char *nameserver) {
11742
  MG_FREE((char *) mgr->nameserver);
11743
  mgr->nameserver = NULL;
11744
  if (nameserver != NULL) {
11745
    mgr->nameserver = strdup(nameserver);
11746
  }
11747
}
11748

    
11749
#endif /* MG_ENABLE_ASYNC_RESOLVER */
11750
#ifdef MG_MODULE_LINES
11751
#line 1 "mongoose/src/mg_coap.c"
11752
#endif
11753
/*
11754
 * Copyright (c) 2015 Cesanta Software Limited
11755
 * All rights reserved
11756
 * This software is dual-licensed: you can redistribute it and/or modify
11757
 * it under the terms of the GNU General Public License version 2 as
11758
 * published by the Free Software Foundation. For the terms of this
11759
 * license, see <http://www.gnu.org/licenses/>.
11760
 *
11761
 * You are free to use this software under the terms of the GNU General
11762
 * Public License, but WITHOUT ANY WARRANTY; without even the implied
11763
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11764
 * See the GNU General Public License for more details.
11765
 *
11766
 * Alternatively, you can license this software under a commercial
11767
 * license, as set out in <https://www.cesanta.com/license>.
11768
 */
11769

    
11770
/* Amalgamated: #include "mg_internal.h" */
11771
/* Amalgamated: #include "mg_coap.h" */
11772

    
11773
#if MG_ENABLE_COAP
11774

    
11775
void mg_coap_free_options(struct mg_coap_message *cm) {
11776
  while (cm->options != NULL) {
11777
    struct mg_coap_option *next = cm->options->next;
11778
    MG_FREE(cm->options);
11779
    cm->options = next;
11780
  }
11781
}
11782

    
11783
struct mg_coap_option *mg_coap_add_option(struct mg_coap_message *cm,
11784
                                          uint32_t number, char *value,
11785
                                          size_t len) {
11786
  struct mg_coap_option *new_option =
11787
      (struct mg_coap_option *) MG_CALLOC(1, sizeof(*new_option));
11788

    
11789
  new_option->number = number;
11790
  new_option->value.p = value;
11791
  new_option->value.len = len;
11792

    
11793
  if (cm->options == NULL) {
11794
    cm->options = cm->optiomg_tail = new_option;
11795
  } else {
11796
    /*
11797
     * A very simple attention to help clients to compose options:
11798
     * CoAP wants to see options ASC ordered.
11799
     * Could be change by using sort in coap_compose
11800
     */
11801
    if (cm->optiomg_tail->number <= new_option->number) {
11802
      /* if option is already ordered just add it */
11803
      cm->optiomg_tail = cm->optiomg_tail->next = new_option;
11804
    } else {
11805
      /* looking for appropriate position */
11806
      struct mg_coap_option *current_opt = cm->options;
11807
      struct mg_coap_option *prev_opt = 0;
11808

    
11809
      while (current_opt != NULL) {
11810
        if (current_opt->number > new_option->number) {
11811
          break;
11812
        }
11813
        prev_opt = current_opt;
11814
        current_opt = current_opt->next;
11815
      }
11816

    
11817
      if (prev_opt != NULL) {
11818
        prev_opt->next = new_option;
11819
        new_option->next = current_opt;
11820
      } else {
11821
        /* insert new_option to the beginning */
11822
        new_option->next = cm->options;
11823
        cm->options = new_option;
11824
      }
11825
    }
11826
  }
11827

    
11828
  return new_option;
11829
}
11830

    
11831
/*
11832
 * Fills CoAP header in mg_coap_message.
11833
 *
11834
 * Helper function.
11835
 */
11836
static char *coap_parse_header(char *ptr, struct mbuf *io,
11837
                               struct mg_coap_message *cm) {
11838
  if (io->len < sizeof(uint32_t)) {
11839
    cm->flags |= MG_COAP_NOT_ENOUGH_DATA;
11840
    return NULL;
11841
  }
11842

    
11843
  /*
11844
   * Version (Ver):  2-bit unsigned integer.  Indicates the CoAP version
11845
   * number.  Implementations of this specification MUST set this field
11846
   * to 1 (01 binary).  Other values are reserved for future versions.
11847
   * Messages with unknown version numbers MUST be silently ignored.
11848
   */
11849
  if (((uint8_t) *ptr >> 6) != 1) {
11850
    cm->flags |= MG_COAP_IGNORE;
11851
    return NULL;
11852
  }
11853

    
11854
  /*
11855
   * Type (T):  2-bit unsigned integer.  Indicates if this message is of
11856
   * type Confirmable (0), Non-confirmable (1), Acknowledgement (2), or
11857
   * Reset (3).
11858
   */
11859
  cm->msg_type = ((uint8_t) *ptr & 0x30) >> 4;
11860
  cm->flags |= MG_COAP_MSG_TYPE_FIELD;
11861

    
11862
  /*
11863
   * Token Length (TKL):  4-bit unsigned integer.  Indicates the length of
11864
   * the variable-length Token field (0-8 bytes).  Lengths 9-15 are
11865
   * reserved, MUST NOT be sent, and MUST be processed as a message
11866
   * format error.
11867
   */
11868
  cm->token.len = *ptr & 0x0F;
11869
  if (cm->token.len > 8) {
11870
    cm->flags |= MG_COAP_FORMAT_ERROR;
11871
    return NULL;
11872
  }
11873

    
11874
  ptr++;
11875

    
11876
  /*
11877
   * Code:  8-bit unsigned integer, split into a 3-bit class (most
11878
   * significant bits) and a 5-bit detail (least significant bits)
11879
   */
11880
  cm->code_class = (uint8_t) *ptr >> 5;
11881
  cm->code_detail = *ptr & 0x1F;
11882
  cm->flags |= (MG_COAP_CODE_CLASS_FIELD | MG_COAP_CODE_DETAIL_FIELD);
11883

    
11884
  ptr++;
11885

    
11886
  /* Message ID:  16-bit unsigned integer in network byte order. */
11887
  cm->msg_id = (uint8_t) *ptr << 8 | (uint8_t) * (ptr + 1);
11888
  cm->flags |= MG_COAP_MSG_ID_FIELD;
11889

    
11890
  ptr += 2;
11891

    
11892
  return ptr;
11893
}
11894

    
11895
/*
11896
 * Fills token information in mg_coap_message.
11897
 *
11898
 * Helper function.
11899
 */
11900
static char *coap_get_token(char *ptr, struct mbuf *io,
11901
                            struct mg_coap_message *cm) {
11902
  if (cm->token.len != 0) {
11903
    if (ptr + cm->token.len > io->buf + io->len) {
11904
      cm->flags |= MG_COAP_NOT_ENOUGH_DATA;
11905
      return NULL;
11906
    } else {
11907
      cm->token.p = ptr;
11908
      ptr += cm->token.len;
11909
      cm->flags |= MG_COAP_TOKEN_FIELD;
11910
    }
11911
  }
11912

    
11913
  return ptr;
11914
}
11915

    
11916
/*
11917
 * Returns Option Delta or Length.
11918
 *
11919
 * Helper function.
11920
 */
11921
static int coap_get_ext_opt(char *ptr, struct mbuf *io, uint16_t *opt_info) {
11922
  int ret = 0;
11923

    
11924
  if (*opt_info == 13) {
11925
    /*
11926
     * 13:  An 8-bit unsigned integer follows the initial byte and
11927
     * indicates the Option Delta/Length minus 13.
11928
     */
11929
    if (ptr < io->buf + io->len) {
11930
      *opt_info = (uint8_t) *ptr + 13;
11931
      ret = sizeof(uint8_t);
11932
    } else {
11933
      ret = -1; /* LCOV_EXCL_LINE */
11934
    }
11935
  } else if (*opt_info == 14) {
11936
    /*
11937
     * 14:  A 16-bit unsigned integer in network byte order follows the
11938
     * initial byte and indicates the Option Delta/Length minus 269.
11939
     */
11940
    if (ptr + sizeof(uint8_t) < io->buf + io->len) {
11941
      *opt_info = ((uint8_t) *ptr << 8 | (uint8_t) * (ptr + 1)) + 269;
11942
      ret = sizeof(uint16_t);
11943
    } else {
11944
      ret = -1; /* LCOV_EXCL_LINE */
11945
    }
11946
  }
11947

    
11948
  return ret;
11949
}
11950

    
11951
/*
11952
 * Fills options in mg_coap_message.
11953
 *
11954
 * Helper function.
11955
 *
11956
 * General options format:
11957
 * +---------------+---------------+
11958
 * | Option Delta  | Option Length |  1 byte
11959
 * +---------------+---------------+
11960
 * \    Option Delta (extended)    \  0-2 bytes
11961
 * +-------------------------------+
11962
 * / Option Length  (extended)     \  0-2 bytes
11963
 * +-------------------------------+
11964
 * \         Option Value          \  0 or more bytes
11965
 * +-------------------------------+
11966
 */
11967
static char *coap_get_options(char *ptr, struct mbuf *io,
11968
                              struct mg_coap_message *cm) {
11969
  uint16_t prev_opt = 0;
11970

    
11971
  if (ptr == io->buf + io->len) {
11972
    /* end of packet, ok */
11973
    return NULL;
11974
  }
11975

    
11976
  /* 0xFF is payload marker */
11977
  while (ptr < io->buf + io->len && (uint8_t) *ptr != 0xFF) {
11978
    uint16_t option_delta, option_lenght;
11979
    int optinfo_len;
11980

    
11981
    /* Option Delta:  4-bit unsigned integer */
11982
    option_delta = ((uint8_t) *ptr & 0xF0) >> 4;
11983
    /* Option Length:  4-bit unsigned integer */
11984
    option_lenght = *ptr & 0x0F;
11985

    
11986
    if (option_delta == 15 || option_lenght == 15) {
11987
      /*
11988
       * 15:  Reserved for future use.  If the field is set to this value,
11989
       * it MUST be processed as a message format error
11990
       */
11991
      cm->flags |= MG_COAP_FORMAT_ERROR;
11992
      break;
11993
    }
11994

    
11995
    ptr++;
11996

    
11997
    /* check for extended option delta */
11998
    optinfo_len = coap_get_ext_opt(ptr, io, &option_delta);
11999
    if (optinfo_len == -1) {
12000
      cm->flags |= MG_COAP_NOT_ENOUGH_DATA; /* LCOV_EXCL_LINE */
12001
      break;                                /* LCOV_EXCL_LINE */
12002
    }
12003

    
12004
    ptr += optinfo_len;
12005

    
12006
    /* check or extended option lenght */
12007
    optinfo_len = coap_get_ext_opt(ptr, io, &option_lenght);
12008
    if (optinfo_len == -1) {
12009
      cm->flags |= MG_COAP_NOT_ENOUGH_DATA; /* LCOV_EXCL_LINE */
12010
      break;                                /* LCOV_EXCL_LINE */
12011
    }
12012

    
12013
    ptr += optinfo_len;
12014

    
12015
    /*
12016
     * Instead of specifying the Option Number directly, the instances MUST
12017
     * appear in order of their Option Numbers and a delta encoding is used
12018
     * between them.
12019
     */
12020
    option_delta += prev_opt;
12021

    
12022
    mg_coap_add_option(cm, option_delta, ptr, option_lenght);
12023

    
12024
    prev_opt = option_delta;
12025

    
12026
    if (ptr + option_lenght > io->buf + io->len) {
12027
      cm->flags |= MG_COAP_NOT_ENOUGH_DATA; /* LCOV_EXCL_LINE */
12028
      break;                                /* LCOV_EXCL_LINE */
12029
    }
12030

    
12031
    ptr += option_lenght;
12032
  }
12033

    
12034
  if ((cm->flags & MG_COAP_ERROR) != 0) {
12035
    mg_coap_free_options(cm);
12036
    return NULL;
12037
  }
12038

    
12039
  cm->flags |= MG_COAP_OPTIOMG_FIELD;
12040

    
12041
  if (ptr == io->buf + io->len) {
12042
    /* end of packet, ok */
12043
    return NULL;
12044
  }
12045

    
12046
  ptr++;
12047

    
12048
  return ptr;
12049
}
12050

    
12051
uint32_t mg_coap_parse(struct mbuf *io, struct mg_coap_message *cm) {
12052
  char *ptr;
12053

    
12054
  memset(cm, 0, sizeof(*cm));
12055

    
12056
  if ((ptr = coap_parse_header(io->buf, io, cm)) == NULL) {
12057
    return cm->flags;
12058
  }
12059

    
12060
  if ((ptr = coap_get_token(ptr, io, cm)) == NULL) {
12061
    return cm->flags;
12062
  }
12063

    
12064
  if ((ptr = coap_get_options(ptr, io, cm)) == NULL) {
12065
    return cm->flags;
12066
  }
12067

    
12068
  /* the rest is payload */
12069
  cm->payload.len = io->len - (ptr - io->buf);
12070
  if (cm->payload.len != 0) {
12071
    cm->payload.p = ptr;
12072
    cm->flags |= MG_COAP_PAYLOAD_FIELD;
12073
  }
12074

    
12075
  return cm->flags;
12076
}
12077

    
12078
/*
12079
 * Calculates extended size of given Opt Number/Length in coap message.
12080
 *
12081
 * Helper function.
12082
 */
12083
static size_t coap_get_ext_opt_size(uint32_t value) {
12084
  int ret = 0;
12085

    
12086
  if (value >= 13 && value <= 0xFF + 13) {
12087
    ret = sizeof(uint8_t);
12088
  } else if (value > 0xFF + 13 && value <= 0xFFFF + 269) {
12089
    ret = sizeof(uint16_t);
12090
  }
12091

    
12092
  return ret;
12093
}
12094

    
12095
/*
12096
 * Splits given Opt Number/Length into base and ext values.
12097
 *
12098
 * Helper function.
12099
 */
12100
static int coap_split_opt(uint32_t value, uint8_t *base, uint16_t *ext) {
12101
  int ret = 0;
12102

    
12103
  if (value < 13) {
12104
    *base = value;
12105
  } else if (value >= 13 && value <= 0xFF + 13) {
12106
    *base = 13;
12107
    *ext = value - 13;
12108
    ret = sizeof(uint8_t);
12109
  } else if (value > 0xFF + 13 && value <= 0xFFFF + 269) {
12110
    *base = 14;
12111
    *ext = value - 269;
12112
    ret = sizeof(uint16_t);
12113
  }
12114

    
12115
  return ret;
12116
}
12117

    
12118
/*
12119
 * Puts uint16_t (in network order) into given char stream.
12120
 *
12121
 * Helper function.
12122
 */
12123
static char *coap_add_uint16(char *ptr, uint16_t val) {
12124
  *ptr = val >> 8;
12125
  ptr++;
12126
  *ptr = val & 0x00FF;
12127
  ptr++;
12128
  return ptr;
12129
}
12130

    
12131
/*
12132
 * Puts extended value of Opt Number/Length into given char stream.
12133
 *
12134
 * Helper function.
12135
 */
12136
static char *coap_add_opt_info(char *ptr, uint16_t val, size_t len) {
12137
  if (len == sizeof(uint8_t)) {
12138
    *ptr = (char) val;
12139
    ptr++;
12140
  } else if (len == sizeof(uint16_t)) {
12141
    ptr = coap_add_uint16(ptr, val);
12142
  }
12143

    
12144
  return ptr;
12145
}
12146

    
12147
/*
12148
 * Verifies given mg_coap_message and calculates message size for it.
12149
 *
12150
 * Helper function.
12151
 */
12152
static uint32_t coap_calculate_packet_size(struct mg_coap_message *cm,
12153
                                           size_t *len) {
12154
  struct mg_coap_option *opt;
12155
  uint32_t prev_opt_number;
12156

    
12157
  *len = 4; /* header */
12158
  if (cm->msg_type > MG_COAP_MSG_MAX) {
12159
    return MG_COAP_ERROR | MG_COAP_MSG_TYPE_FIELD;
12160
  }
12161
  if (cm->token.len > 8) {
12162
    return MG_COAP_ERROR | MG_COAP_TOKEN_FIELD;
12163
  }
12164
  if (cm->code_class > 7) {
12165
    return MG_COAP_ERROR | MG_COAP_CODE_CLASS_FIELD;
12166
  }
12167
  if (cm->code_detail > 31) {
12168
    return MG_COAP_ERROR | MG_COAP_CODE_DETAIL_FIELD;
12169
  }
12170

    
12171
  *len += cm->token.len;
12172
  if (cm->payload.len != 0) {
12173
    *len += cm->payload.len + 1; /* ... + 1; add payload marker */
12174
  }
12175

    
12176
  opt = cm->options;
12177
  prev_opt_number = 0;
12178
  while (opt != NULL) {
12179
    *len += 1; /* basic delta/length */
12180
    *len += coap_get_ext_opt_size(opt->number - prev_opt_number);
12181
    *len += coap_get_ext_opt_size((uint32_t) opt->value.len);
12182
    /*
12183
     * Current implementation performs check if
12184
     * option_number > previous option_number and produces an error
12185
     * TODO(alashkin): write design doc with limitations
12186
     * May be resorting is more suitable solution.
12187
     */
12188
    if ((opt->next != NULL && opt->number > opt->next->number) ||
12189
        opt->value.len > 0xFFFF + 269 ||
12190
        opt->number - prev_opt_number > 0xFFFF + 269) {
12191
      return MG_COAP_ERROR | MG_COAP_OPTIOMG_FIELD;
12192
    }
12193
    *len += opt->value.len;
12194
    prev_opt_number = opt->number;
12195
    opt = opt->next;
12196
  }
12197

    
12198
  return 0;
12199
}
12200

    
12201
uint32_t mg_coap_compose(struct mg_coap_message *cm, struct mbuf *io) {
12202
  struct mg_coap_option *opt;
12203
  uint32_t res, prev_opt_number;
12204
  size_t prev_io_len, packet_size;
12205
  char *ptr;
12206

    
12207
  res = coap_calculate_packet_size(cm, &packet_size);
12208
  if (res != 0) {
12209
    return res;
12210
  }
12211

    
12212
  /* saving previous lenght to handle non-empty mbuf */
12213
  prev_io_len = io->len;
12214
  if (mbuf_append(io, NULL, packet_size) == 0) return MG_COAP_ERROR;
12215
  ptr = io->buf + prev_io_len;
12216

    
12217
  /*
12218
   * since cm is verified, it is possible to use bits shift operator
12219
   * without additional zeroing of unused bits
12220
   */
12221

    
12222
  /* ver: 2 bits, msg_type: 2 bits, toklen: 4 bits */
12223
  *ptr = (1 << 6) | (cm->msg_type << 4) | (uint8_t)(cm->token.len);
12224
  ptr++;
12225

    
12226
  /* code class: 3 bits, code detail: 5 bits */
12227
  *ptr = (cm->code_class << 5) | (cm->code_detail);
12228
  ptr++;
12229

    
12230
  ptr = coap_add_uint16(ptr, cm->msg_id);
12231

    
12232
  if (cm->token.len != 0) {
12233
    memcpy(ptr, cm->token.p, cm->token.len);
12234
    ptr += cm->token.len;
12235
  }
12236

    
12237
  opt = cm->options;
12238
  prev_opt_number = 0;
12239
  while (opt != NULL) {
12240
    uint8_t delta_base = 0, length_base = 0;
12241
    uint16_t delta_ext = 0, length_ext = 0;
12242

    
12243
    size_t opt_delta_len =
12244
        coap_split_opt(opt->number - prev_opt_number, &delta_base, &delta_ext);
12245
    size_t opt_lenght_len =
12246
        coap_split_opt((uint32_t) opt->value.len, &length_base, &length_ext);
12247

    
12248
    *ptr = (delta_base << 4) | length_base;
12249
    ptr++;
12250

    
12251
    ptr = coap_add_opt_info(ptr, delta_ext, opt_delta_len);
12252
    ptr = coap_add_opt_info(ptr, length_ext, opt_lenght_len);
12253

    
12254
    if (opt->value.len != 0) {
12255
      memcpy(ptr, opt->value.p, opt->value.len);
12256
      ptr += opt->value.len;
12257
    }
12258

    
12259
    prev_opt_number = opt->number;
12260
    opt = opt->next;
12261
  }
12262

    
12263
  if (cm->payload.len != 0) {
12264
    *ptr = (char) -1;
12265
    ptr++;
12266
    memcpy(ptr, cm->payload.p, cm->payload.len);
12267
  }
12268

    
12269
  return 0;
12270
}
12271

    
12272
uint32_t mg_coap_send_message(struct mg_connection *nc,
12273
                              struct mg_coap_message *cm) {
12274
  struct mbuf packet_out;
12275
  uint32_t compose_res;
12276

    
12277
  mbuf_init(&packet_out, 0);
12278
  compose_res = mg_coap_compose(cm, &packet_out);
12279
  if (compose_res != 0) {
12280
    return compose_res; /* LCOV_EXCL_LINE */
12281
  }
12282

    
12283
  mg_send(nc, packet_out.buf, (int) packet_out.len);
12284
  mbuf_free(&packet_out);
12285

    
12286
  return 0;
12287
}
12288

    
12289
uint32_t mg_coap_send_ack(struct mg_connection *nc, uint16_t msg_id) {
12290
  struct mg_coap_message cm;
12291
  memset(&cm, 0, sizeof(cm));
12292
  cm.msg_type = MG_COAP_MSG_ACK;
12293
  cm.msg_id = msg_id;
12294

    
12295
  return mg_coap_send_message(nc, &cm);
12296
}
12297

    
12298
static void coap_handler(struct mg_connection *nc, int ev,
12299
                         void *ev_data MG_UD_ARG(void *user_data)) {
12300
  struct mbuf *io = &nc->recv_mbuf;
12301
  struct mg_coap_message cm;
12302
  uint32_t parse_res;
12303

    
12304
  memset(&cm, 0, sizeof(cm));
12305

    
12306
  nc->handler(nc, ev, ev_data MG_UD_ARG(user_data));
12307

    
12308
  switch (ev) {
12309
    case MG_EV_RECV:
12310
      parse_res = mg_coap_parse(io, &cm);
12311
      if ((parse_res & MG_COAP_IGNORE) == 0) {
12312
        if ((cm.flags & MG_COAP_NOT_ENOUGH_DATA) != 0) {
12313
          /*
12314
           * Since we support UDP only
12315
           * MG_COAP_NOT_ENOUGH_DATA == MG_COAP_FORMAT_ERROR
12316
           */
12317
          cm.flags |= MG_COAP_FORMAT_ERROR; /* LCOV_EXCL_LINE */
12318
        }                                   /* LCOV_EXCL_LINE */
12319
        nc->handler(nc, MG_COAP_EVENT_BASE + cm.msg_type,
12320
                    &cm MG_UD_ARG(user_data));
12321
      }
12322

    
12323
      mg_coap_free_options(&cm);
12324
      mbuf_remove(io, io->len);
12325
      break;
12326
  }
12327
}
12328
/*
12329
 * Attach built-in CoAP event handler to the given connection.
12330
 *
12331
 * The user-defined event handler will receive following extra events:
12332
 *
12333
 * - MG_EV_COAP_CON
12334
 * - MG_EV_COAP_NOC
12335
 * - MG_EV_COAP_ACK
12336
 * - MG_EV_COAP_RST
12337
 */
12338
int mg_set_protocol_coap(struct mg_connection *nc) {
12339
  /* supports UDP only */
12340
  if ((nc->flags & MG_F_UDP) == 0) {
12341
    return -1;
12342
  }
12343

    
12344
  nc->proto_handler = coap_handler;
12345

    
12346
  return 0;
12347
}
12348

    
12349
#endif /* MG_ENABLE_COAP */
12350
#ifdef MG_MODULE_LINES
12351
#line 1 "mongoose/src/mg_sntp.c"
12352
#endif
12353
/*
12354
 * Copyright (c) 2016 Cesanta Software Limited
12355
 * All rights reserved
12356
 */
12357

    
12358
/* Amalgamated: #include "mg_internal.h" */
12359
/* Amalgamated: #include "mg_sntp.h" */
12360
/* Amalgamated: #include "mg_util.h" */
12361

    
12362
#if MG_ENABLE_SNTP
12363

    
12364
#define SNTP_TIME_OFFSET 2208988800
12365

    
12366
#ifndef SNTP_TIMEOUT
12367
#define SNTP_TIMEOUT 10
12368
#endif
12369

    
12370
#ifndef SNTP_ATTEMPTS
12371
#define SNTP_ATTEMPTS 3
12372
#endif
12373

    
12374
static uint64_t mg_get_sec(uint64_t val) {
12375
  return (val & 0xFFFFFFFF00000000) >> 32;
12376
}
12377

    
12378
static uint64_t mg_get_usec(uint64_t val) {
12379
  uint64_t tmp = (val & 0x00000000FFFFFFFF);
12380
  tmp *= 1000000;
12381
  tmp >>= 32;
12382
  return tmp;
12383
}
12384

    
12385
static void mg_ntp_to_tv(uint64_t val, struct timeval *tv) {
12386
  uint64_t tmp;
12387
  tmp = mg_get_sec(val);
12388
  tmp -= SNTP_TIME_OFFSET;
12389
  tv->tv_sec = tmp;
12390
  tv->tv_usec = mg_get_usec(val);
12391
}
12392

    
12393
static void mg_get_ntp_ts(const char *ntp, uint64_t *val) {
12394
  uint32_t tmp;
12395
  memcpy(&tmp, ntp, sizeof(tmp));
12396
  tmp = ntohl(tmp);
12397
  *val = (uint64_t) tmp << 32;
12398
  memcpy(&tmp, ntp + 4, sizeof(tmp));
12399
  tmp = ntohl(tmp);
12400
  *val |= tmp;
12401
}
12402

    
12403
void mg_sntp_send_request(struct mg_connection *c) {
12404
  uint8_t buf[48] = {0};
12405
  /*
12406
   * header - 8 bit:
12407
   * LI (2 bit) - 3 (not in sync), VN (3 bit) - 4 (version),
12408
   * mode (3 bit) - 3 (client)
12409
   */
12410
  buf[0] = (3 << 6) | (4 << 3) | 3;
12411

    
12412
/*
12413
 * Next fields should be empty in client request
12414
 * stratum, 8 bit
12415
 * poll interval, 8 bit
12416
 * rrecision, 8 bit
12417
 * root delay, 32 bit
12418
 * root dispersion, 32 bit
12419
 * ref id, 32 bit
12420
 * ref timestamp, 64 bit
12421
 * originate Timestamp, 64 bit
12422
 * receive Timestamp, 64 bit
12423
*/
12424

    
12425
/*
12426
 * convert time to sntp format (sntp starts from 00:00:00 01.01.1900)
12427
 * according to rfc868 it is 2208988800L sec
12428
 * this information is used to correct roundtrip delay
12429
 * but if local clock is absolutely broken (and doesn't work even
12430
 * as simple timer), it is better to disable it
12431
*/
12432
#ifndef MG_SNTP_NO_DELAY_CORRECTION
12433
  uint32_t sec;
12434
  sec = htonl((uint32_t)(mg_time() + SNTP_TIME_OFFSET));
12435
  memcpy(&buf[40], &sec, sizeof(sec));
12436
#endif
12437

    
12438
  mg_send(c, buf, sizeof(buf));
12439
}
12440

    
12441
#ifndef MG_SNTP_NO_DELAY_CORRECTION
12442
static uint64_t mg_calculate_delay(uint64_t t1, uint64_t t2, uint64_t t3) {
12443
  /* roundloop delay = (T4 - T1) - (T3 - T2) */
12444
  uint64_t d1 = ((mg_time() + SNTP_TIME_OFFSET) * 1000000) -
12445
                (mg_get_sec(t1) * 1000000 + mg_get_usec(t1));
12446
  uint64_t d2 = (mg_get_sec(t3) * 1000000 + mg_get_usec(t3)) -
12447
                (mg_get_sec(t2) * 1000000 + mg_get_usec(t2));
12448

    
12449
  return (d1 > d2) ? d1 - d2 : 0;
12450
}
12451
#endif
12452

    
12453
MG_INTERNAL int mg_sntp_parse_reply(const char *buf, int len,
12454
                                    struct mg_sntp_message *msg) {
12455
  uint8_t hdr;
12456
  uint64_t trsm_ts_T3, delay = 0;
12457
  int mode;
12458
  struct timeval tv;
12459

    
12460
  if (len < 48) {
12461
    return -1;
12462
  }
12463

    
12464
  hdr = buf[0];
12465

    
12466
  if ((hdr & 0x38) >> 3 != 4) {
12467
    /* Wrong version */
12468
    return -1;
12469
  }
12470

    
12471
  mode = hdr & 0x7;
12472
  if (mode != 4 && mode != 5) {
12473
    /* Not a server reply */
12474
    return -1;
12475
  }
12476

    
12477
  memset(msg, 0, sizeof(*msg));
12478

    
12479
  msg->kiss_of_death = (buf[1] == 0); /* Server asks to not send requests */
12480

    
12481
  mg_get_ntp_ts(&buf[40], &trsm_ts_T3);
12482

    
12483
#ifndef MG_SNTP_NO_DELAY_CORRECTION
12484
  {
12485
    uint64_t orig_ts_T1, recv_ts_T2;
12486
    mg_get_ntp_ts(&buf[24], &orig_ts_T1);
12487
    mg_get_ntp_ts(&buf[32], &recv_ts_T2);
12488
    delay = mg_calculate_delay(orig_ts_T1, recv_ts_T2, trsm_ts_T3);
12489
  }
12490
#endif
12491

    
12492
  mg_ntp_to_tv(trsm_ts_T3, &tv);
12493

    
12494
  msg->time = (double) tv.tv_sec + (((double) tv.tv_usec + delay) / 1000000.0);
12495

    
12496
  return 0;
12497
}
12498

    
12499
static void mg_sntp_handler(struct mg_connection *c, int ev,
12500
                            void *ev_data MG_UD_ARG(void *user_data)) {
12501
  struct mbuf *io = &c->recv_mbuf;
12502
  struct mg_sntp_message msg;
12503

    
12504
  c->handler(c, ev, ev_data MG_UD_ARG(user_data));
12505

    
12506
  switch (ev) {
12507
    case MG_EV_RECV: {
12508
      if (mg_sntp_parse_reply(io->buf, io->len, &msg) < 0) {
12509
        DBG(("Invalid SNTP packet received (%d)", (int) io->len));
12510
        c->handler(c, MG_SNTP_MALFORMED_REPLY, NULL MG_UD_ARG(user_data));
12511
      } else {
12512
        c->handler(c, MG_SNTP_REPLY, (void *) &msg MG_UD_ARG(user_data));
12513
      }
12514

    
12515
      mbuf_remove(io, io->len);
12516
      break;
12517
    }
12518
  }
12519
}
12520

    
12521
int mg_set_protocol_sntp(struct mg_connection *c) {
12522
  if ((c->flags & MG_F_UDP) == 0) {
12523
    return -1;
12524
  }
12525

    
12526
  c->proto_handler = mg_sntp_handler;
12527

    
12528
  return 0;
12529
}
12530

    
12531
struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr,
12532
                                      MG_CB(mg_event_handler_t event_handler,
12533
                                            void *user_data),
12534
                                      const char *sntp_server_name) {
12535
  struct mg_connection *c = NULL;
12536
  char url[100], *p_url = url;
12537
  const char *proto = "", *port = "", *tmp;
12538

    
12539
  /* If port is not specified, use default (123) */
12540
  tmp = strchr(sntp_server_name, ':');
12541
  if (tmp != NULL && *(tmp + 1) == '/') {
12542
    tmp = strchr(tmp + 1, ':');
12543
  }
12544

    
12545
  if (tmp == NULL) {
12546
    port = ":123";
12547
  }
12548

    
12549
  /* Add udp:// if needed */
12550
  if (strncmp(sntp_server_name, "udp://", 6) != 0) {
12551
    proto = "udp://";
12552
  }
12553

    
12554
  mg_asprintf(&p_url, sizeof(url), "%s%s%s", proto, sntp_server_name, port);
12555

    
12556
  c = mg_connect(mgr, p_url, event_handler MG_UD_ARG(user_data));
12557

    
12558
  if (c == NULL) {
12559
    goto cleanup;
12560
  }
12561

    
12562
  mg_set_protocol_sntp(c);
12563

    
12564
cleanup:
12565
  if (p_url != url) {
12566
    MG_FREE(p_url);
12567
  }
12568

    
12569
  return c;
12570
}
12571

    
12572
struct sntp_data {
12573
  mg_event_handler_t hander;
12574
  int count;
12575
};
12576

    
12577
static void mg_sntp_util_ev_handler(struct mg_connection *c, int ev,
12578
                                    void *ev_data MG_UD_ARG(void *user_data)) {
12579
#if !MG_ENABLE_CALLBACK_USERDATA
12580
  void *user_data = c->user_data;
12581
#endif
12582
  struct sntp_data *sd = (struct sntp_data *) user_data;
12583

    
12584
  switch (ev) {
12585
    case MG_EV_CONNECT:
12586
      if (*(int *) ev_data != 0) {
12587
        mg_call(c, sd->hander, c->user_data, MG_SNTP_FAILED, NULL);
12588
        break;
12589
      }
12590
    /* fallthrough */
12591
    case MG_EV_TIMER:
12592
      if (sd->count <= SNTP_ATTEMPTS) {
12593
        mg_sntp_send_request(c);
12594
        mg_set_timer(c, mg_time() + 10);
12595
        sd->count++;
12596
      } else {
12597
        mg_call(c, sd->hander, c->user_data, MG_SNTP_FAILED, NULL);
12598
        c->flags |= MG_F_CLOSE_IMMEDIATELY;
12599
      }
12600
      break;
12601
    case MG_SNTP_MALFORMED_REPLY:
12602
      mg_call(c, sd->hander, c->user_data, MG_SNTP_FAILED, NULL);
12603
      c->flags |= MG_F_CLOSE_IMMEDIATELY;
12604
      break;
12605
    case MG_SNTP_REPLY:
12606
      mg_call(c, sd->hander, c->user_data, MG_SNTP_REPLY, ev_data);
12607
      c->flags |= MG_F_CLOSE_IMMEDIATELY;
12608
      break;
12609
    case MG_EV_CLOSE:
12610
      MG_FREE(user_data);
12611
      c->user_data = NULL;
12612
      break;
12613
  }
12614
}
12615

    
12616
struct mg_connection *mg_sntp_get_time(struct mg_mgr *mgr,
12617
                                       mg_event_handler_t event_handler,
12618
                                       const char *sntp_server_name) {
12619
  struct mg_connection *c;
12620
  struct sntp_data *sd = (struct sntp_data *) MG_CALLOC(1, sizeof(*sd));
12621
  if (sd == NULL) {
12622
    return NULL;
12623
  }
12624

    
12625
  c = mg_sntp_connect(mgr, MG_CB(mg_sntp_util_ev_handler, sd),
12626
                      sntp_server_name);
12627
  if (c == NULL) {
12628
    MG_FREE(sd);
12629
    return NULL;
12630
  }
12631

    
12632
  sd->hander = event_handler;
12633
#if !MG_ENABLE_CALLBACK_USERDATA
12634
  c->user_data = sd;
12635
#endif
12636

    
12637
  return c;
12638
}
12639

    
12640
#endif /* MG_ENABLE_SNTP */
12641
#ifdef MG_MODULE_LINES
12642
#line 1 "mongoose/src/mg_socks.c"
12643
#endif
12644
/*
12645
 * Copyright (c) 2017 Cesanta Software Limited
12646
 * All rights reserved
12647
 */
12648

    
12649
#if MG_ENABLE_SOCKS
12650

    
12651
/* Amalgamated: #include "mg_socks.h" */
12652
/* Amalgamated: #include "mg_internal.h" */
12653

    
12654
/*
12655
 *  https://www.ietf.org/rfc/rfc1928.txt paragraph 3, handle client handshake
12656
 *
12657
 *  +----+----------+----------+
12658
 *  |VER | NMETHODS | METHODS  |
12659
 *  +----+----------+----------+
12660
 *  | 1  |    1     | 1 to 255 |
12661
 *  +----+----------+----------+
12662
 */
12663
static void mg_socks5_handshake(struct mg_connection *c) {
12664
  struct mbuf *r = &c->recv_mbuf;
12665
  if (r->buf[0] != MG_SOCKS_VERSION) {
12666
    c->flags |= MG_F_CLOSE_IMMEDIATELY;
12667
  } else if (r->len > 2 && (size_t) r->buf[1] + 2 <= r->len) {
12668
    /* https://www.ietf.org/rfc/rfc1928.txt paragraph 3 */
12669
    unsigned char reply[2] = {MG_SOCKS_VERSION, MG_SOCKS_HANDSHAKE_FAILURE};
12670
    int i;
12671
    for (i = 2; i < r->buf[1] + 2; i++) {
12672
      /* TODO(lsm): support other auth methods */
12673
      if (r->buf[i] == MG_SOCKS_HANDSHAKE_NOAUTH) reply[1] = r->buf[i];
12674
    }
12675
    mbuf_remove(r, 2 + r->buf[1]);
12676
    mg_send(c, reply, sizeof(reply));
12677
    c->flags |= MG_SOCKS_HANDSHAKE_DONE; /* Mark handshake done */
12678
  }
12679
}
12680

    
12681
static void disband(struct mg_connection *c) {
12682
  struct mg_connection *c2 = (struct mg_connection *) c->user_data;
12683
  if (c2 != NULL) {
12684
    c2->flags |= MG_F_SEND_AND_CLOSE;
12685
    c2->user_data = NULL;
12686
  }
12687
  c->flags |= MG_F_SEND_AND_CLOSE;
12688
  c->user_data = NULL;
12689
}
12690

    
12691
static void relay_data(struct mg_connection *c) {
12692
  struct mg_connection *c2 = (struct mg_connection *) c->user_data;
12693
  if (c2 != NULL) {
12694
    mg_send(c2, c->recv_mbuf.buf, c->recv_mbuf.len);
12695
    mbuf_remove(&c->recv_mbuf, c->recv_mbuf.len);
12696
  } else {
12697
    c->flags |= MG_F_SEND_AND_CLOSE;
12698
  }
12699
}
12700

    
12701
static void serv_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
12702
  if (ev == MG_EV_CLOSE) {
12703
    disband(c);
12704
  } else if (ev == MG_EV_RECV) {
12705
    relay_data(c);
12706
  } else if (ev == MG_EV_CONNECT) {
12707
    int res = *(int *) ev_data;
12708
    if (res != 0) LOG(LL_ERROR, ("connect error: %d", res));
12709
  }
12710
}
12711

    
12712
static void mg_socks5_connect(struct mg_connection *c, const char *addr) {
12713
  struct mg_connection *serv = mg_connect(c->mgr, addr, serv_ev_handler);
12714
  serv->user_data = c;
12715
  c->user_data = serv;
12716
}
12717

    
12718
/*
12719
 *  Request, https://www.ietf.org/rfc/rfc1928.txt paragraph 4
12720
 *
12721
 *  +----+-----+-------+------+----------+----------+
12722
 *  |VER | CMD |  RSV  | ATYP | DST.ADDR | DST.PORT |
12723
 *  +----+-----+-------+------+----------+----------+
12724
 *  | 1  |  1  | X'00' |  1   | Variable |    2     |
12725
 *  +----+-----+-------+------+----------+----------+
12726
 */
12727
static void mg_socks5_handle_request(struct mg_connection *c) {
12728
  struct mbuf *r = &c->recv_mbuf;
12729
  unsigned char *p = (unsigned char *) r->buf;
12730
  unsigned char addr_len = 4, reply = MG_SOCKS_SUCCESS;
12731
  int ver, cmd, atyp;
12732
  char addr[300];
12733

    
12734
  if (r->len < 8) return; /* return if not fully buffered. min DST.ADDR is 2 */
12735
  ver = p[0];
12736
  cmd = p[1];
12737
  atyp = p[3];
12738

    
12739
  /* TODO(lsm): support other commands */
12740
  if (ver != MG_SOCKS_VERSION || cmd != MG_SOCKS_CMD_CONNECT) {
12741
    reply = MG_SOCKS_CMD_NOT_SUPPORTED;
12742
  } else if (atyp == MG_SOCKS_ADDR_IPV4) {
12743
    addr_len = 4;
12744
    if (r->len < (size_t) addr_len + 6) return; /* return if not buffered */
12745
    snprintf(addr, sizeof(addr), "%d.%d.%d.%d:%d", p[4], p[5], p[6], p[7],
12746
             p[8] << 8 | p[9]);
12747
    mg_socks5_connect(c, addr);
12748
  } else if (atyp == MG_SOCKS_ADDR_IPV6) {
12749
    addr_len = 16;
12750
    if (r->len < (size_t) addr_len + 6) return; /* return if not buffered */
12751
    snprintf(addr, sizeof(addr), "[%x:%x:%x:%x:%x:%x:%x:%x]:%d",
12752
             p[4] << 8 | p[5], p[6] << 8 | p[7], p[8] << 8 | p[9],
12753
             p[10] << 8 | p[11], p[12] << 8 | p[13], p[14] << 8 | p[15],
12754
             p[16] << 8 | p[17], p[18] << 8 | p[19], p[20] << 8 | p[21]);
12755
    mg_socks5_connect(c, addr);
12756
  } else if (atyp == MG_SOCKS_ADDR_DOMAIN) {
12757
    addr_len = p[4] + 1;
12758
    if (r->len < (size_t) addr_len + 6) return; /* return if not buffered */
12759
    snprintf(addr, sizeof(addr), "%.*s:%d", p[4], p + 5,
12760
             p[4 + addr_len] << 8 | p[4 + addr_len + 1]);
12761
    mg_socks5_connect(c, addr);
12762
  } else {
12763
    reply = MG_SOCKS_ADDR_NOT_SUPPORTED;
12764
  }
12765

    
12766
  /*
12767
   *  Reply, https://www.ietf.org/rfc/rfc1928.txt paragraph 5
12768
   *
12769
   *  +----+-----+-------+------+----------+----------+
12770
   *  |VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |
12771
   *  +----+-----+-------+------+----------+----------+
12772
   *  | 1  |  1  | X'00' |  1   | Variable |    2     |
12773
   *  +----+-----+-------+------+----------+----------+
12774
   */
12775
  {
12776
    unsigned char buf[] = {MG_SOCKS_VERSION, reply, 0};
12777
    mg_send(c, buf, sizeof(buf));
12778
  }
12779
  mg_send(c, r->buf + 3, addr_len + 1 + 2);
12780

    
12781
  mbuf_remove(r, 6 + addr_len);      /* Remove request from the input stream */
12782
  c->flags |= MG_SOCKS_CONNECT_DONE; /* Mark ourselves as connected */
12783
}
12784

    
12785
static void socks_handler(struct mg_connection *c, int ev, void *ev_data) {
12786
  if (ev == MG_EV_RECV) {
12787
    if (!(c->flags & MG_SOCKS_HANDSHAKE_DONE)) mg_socks5_handshake(c);
12788
    if (c->flags & MG_SOCKS_HANDSHAKE_DONE &&
12789
        !(c->flags & MG_SOCKS_CONNECT_DONE)) {
12790
      mg_socks5_handle_request(c);
12791
    }
12792
    if (c->flags & MG_SOCKS_CONNECT_DONE) relay_data(c);
12793
  } else if (ev == MG_EV_CLOSE) {
12794
    disband(c);
12795
  }
12796
  (void) ev_data;
12797
}
12798

    
12799
void mg_set_protocol_socks(struct mg_connection *c) {
12800
  c->proto_handler = socks_handler;
12801
}
12802
#endif
12803
#ifdef MG_MODULE_LINES
12804
#line 1 "common/platforms/cc3200/cc3200_libc.c"
12805
#endif
12806
/*
12807
 * Copyright (c) 2014-2016 Cesanta Software Limited
12808
 * All rights reserved
12809
 */
12810

    
12811
#if CS_PLATFORM == CS_P_CC3200
12812

    
12813
/* Amalgamated: #include "common/mg_mem.h" */
12814
#include <stdio.h>
12815
#include <string.h>
12816

    
12817
#ifndef __TI_COMPILER_VERSION__
12818
#include <reent.h>
12819
#include <sys/stat.h>
12820
#include <sys/time.h>
12821
#include <unistd.h>
12822
#endif
12823

    
12824
#include <inc/hw_types.h>
12825
#include <inc/hw_memmap.h>
12826
#include <driverlib/prcm.h>
12827
#include <driverlib/rom.h>
12828
#include <driverlib/rom_map.h>
12829
#include <driverlib/uart.h>
12830
#include <driverlib/utils.h>
12831

    
12832
#define CONSOLE_UART UARTA0_BASE
12833

    
12834
#ifdef __TI_COMPILER_VERSION__
12835
int asprintf(char **strp, const char *fmt, ...) {
12836
  va_list ap;
12837
  int len;
12838

    
12839
  *strp = MG_MALLOC(BUFSIZ);
12840
  if (*strp == NULL) return -1;
12841

    
12842
  va_start(ap, fmt);
12843
  len = vsnprintf(*strp, BUFSIZ, fmt, ap);
12844
  va_end(ap);
12845

    
12846
  if (len > 0) {
12847
    *strp = MG_REALLOC(*strp, len + 1);
12848
    if (*strp == NULL) return -1;
12849
  }
12850

    
12851
  if (len >= BUFSIZ) {
12852
    va_start(ap, fmt);
12853
    len = vsnprintf(*strp, len + 1, fmt, ap);
12854
    va_end(ap);
12855
  }
12856

    
12857
  return len;
12858
}
12859

    
12860
#if MG_TI_NO_HOST_INTERFACE
12861
time_t HOSTtime() {
12862
  struct timeval tp;
12863
  gettimeofday(&tp, NULL);
12864
  return tp.tv_sec;
12865
}
12866
#endif
12867

    
12868
#endif /* __TI_COMPILER_VERSION__ */
12869

    
12870
void fprint_str(FILE *fp, const char *str) {
12871
  while (*str != '\0') {
12872
    if (*str == '\n') MAP_UARTCharPut(CONSOLE_UART, '\r');
12873
    MAP_UARTCharPut(CONSOLE_UART, *str++);
12874
  }
12875
}
12876

    
12877
void _exit(int status) {
12878
  fprint_str(stderr, "_exit\n");
12879
  /* cause an unaligned access exception, that will drop you into gdb */
12880
  *(int *) 1 = status;
12881
  while (1)
12882
    ; /* avoid gcc warning because stdlib abort() has noreturn attribute */
12883
}
12884

    
12885
void _not_implemented(const char *what) {
12886
  fprint_str(stderr, what);
12887
  fprint_str(stderr, " is not implemented\n");
12888
  _exit(42);
12889
}
12890

    
12891
int _kill(int pid, int sig) {
12892
  (void) pid;
12893
  (void) sig;
12894
  _not_implemented("_kill");
12895
  return -1;
12896
}
12897

    
12898
int _getpid() {
12899
  fprint_str(stderr, "_getpid is not implemented\n");
12900
  return 42;
12901
}
12902

    
12903
int _isatty(int fd) {
12904
  /* 0, 1 and 2 are TTYs. */
12905
  return fd < 2;
12906
}
12907

    
12908
#endif /* CS_PLATFORM == CS_P_CC3200 */
12909
#ifdef MG_MODULE_LINES
12910
#line 1 "common/platforms/msp432/msp432_libc.c"
12911
#endif
12912
/*
12913
 * Copyright (c) 2014-2016 Cesanta Software Limited
12914
 * All rights reserved
12915
 */
12916

    
12917
#if CS_PLATFORM == CS_P_MSP432
12918

    
12919
#include <ti/sysbios/BIOS.h>
12920
#include <ti/sysbios/knl/Clock.h>
12921

    
12922
int gettimeofday(struct timeval *tp, void *tzp) {
12923
  uint32_t ticks = Clock_getTicks();
12924
  tp->tv_sec = ticks / 1000;
12925
  tp->tv_usec = (ticks % 1000) * 1000;
12926
  return 0;
12927
}
12928

    
12929
#endif /* CS_PLATFORM == CS_P_MSP432 */
12930
#ifdef MG_MODULE_LINES
12931
#line 1 "common/platforms/nrf5/nrf5_libc.c"
12932
#endif
12933
/*
12934
 * Copyright (c) 2014-2016 Cesanta Software Limited
12935
 * All rights reserved
12936
 */
12937

    
12938
#if (CS_PLATFORM == CS_P_NRF51 || CS_PLATFORM == CS_P_NRF52) && \
12939
    defined(__ARMCC_VERSION)
12940
int gettimeofday(struct timeval *tp, void *tzp) {
12941
  /* TODO */
12942
  tp->tv_sec = 0;
12943
  tp->tv_usec = 0;
12944
  return 0;
12945
}
12946
#endif
12947
#ifdef MG_MODULE_LINES
12948
#line 1 "common/platforms/simplelink/sl_fs_slfs.h"
12949
#endif
12950
/*
12951
 * Copyright (c) 2014-2016 Cesanta Software Limited
12952
 * All rights reserved
12953
 */
12954

    
12955
#ifndef CS_COMMON_PLATFORMS_SIMPLELINK_SL_FS_SLFS_H_
12956
#define CS_COMMON_PLATFORMS_SIMPLELINK_SL_FS_SLFS_H_
12957

    
12958
#if defined(MG_FS_SLFS)
12959

    
12960
#include <stdio.h>
12961
#ifndef __TI_COMPILER_VERSION__
12962
#include <unistd.h>
12963
#include <sys/stat.h>
12964
#endif
12965

    
12966
#define MAX_OPEN_SLFS_FILES 8
12967

    
12968
/* Indirect libc interface - same functions, different names. */
12969
int fs_slfs_open(const char *pathname, int flags, mode_t mode);
12970
int fs_slfs_close(int fd);
12971
ssize_t fs_slfs_read(int fd, void *buf, size_t count);
12972
ssize_t fs_slfs_write(int fd, const void *buf, size_t count);
12973
int fs_slfs_stat(const char *pathname, struct stat *s);
12974
int fs_slfs_fstat(int fd, struct stat *s);
12975
off_t fs_slfs_lseek(int fd, off_t offset, int whence);
12976
int fs_slfs_unlink(const char *filename);
12977
int fs_slfs_rename(const char *from, const char *to);
12978

    
12979
void fs_slfs_set_new_file_size(const char *name, size_t size);
12980

    
12981
#endif /* defined(MG_FS_SLFS) */
12982

    
12983
#endif /* CS_COMMON_PLATFORMS_SIMPLELINK_SL_FS_SLFS_H_ */
12984
#ifdef MG_MODULE_LINES
12985
#line 1 "common/platforms/simplelink/sl_fs_slfs.c"
12986
#endif
12987
/*
12988
 * Copyright (c) 2014-2016 Cesanta Software Limited
12989
 * All rights reserved
12990
 */
12991

    
12992
/* Standard libc interface to TI SimpleLink FS. */
12993

    
12994
#if defined(MG_FS_SLFS) || defined(CC3200_FS_SLFS)
12995

    
12996
/* Amalgamated: #include "common/platforms/simplelink/sl_fs_slfs.h" */
12997

    
12998
#include <errno.h>
12999

    
13000
#if CS_PLATFORM == CS_P_CC3200
13001
#include <inc/hw_types.h>
13002
#endif
13003

    
13004
/* Amalgamated: #include "common/cs_dbg.h" */
13005
/* Amalgamated: #include "common/mg_mem.h" */
13006

    
13007
#if SL_MAJOR_VERSION_NUM < 2
13008
int slfs_open(const unsigned char *fname, uint32_t flags) {
13009
  _i32 fh;
13010
  _i32 r = sl_FsOpen(fname, flags, NULL /* token */, &fh);
13011
  return (r < 0 ? r : fh);
13012
}
13013
#else /* SL_MAJOR_VERSION_NUM >= 2 */
13014
int slfs_open(const unsigned char *fname, uint32_t flags) {
13015
  return sl_FsOpen(fname, flags, NULL /* token */);
13016
}
13017
#endif
13018

    
13019
/* From sl_fs.c */
13020
int set_errno(int e);
13021
const char *drop_dir(const char *fname, bool *is_slfs);
13022

    
13023
/*
13024
 * With SLFS, you have to pre-declare max file size. Yes. Really.
13025
 * 64K should be enough for everyone. Right?
13026
 */
13027
#ifndef FS_SLFS_MAX_FILE_SIZE
13028
#define FS_SLFS_MAX_FILE_SIZE (64 * 1024)
13029
#endif
13030

    
13031
struct sl_file_size_hint {
13032
  char *name;
13033
  size_t size;
13034
};
13035

    
13036
struct sl_fd_info {
13037
  _i32 fh;
13038
  _off_t pos;
13039
  size_t size;
13040
};
13041

    
13042
static struct sl_fd_info s_sl_fds[MAX_OPEN_SLFS_FILES];
13043
static struct sl_file_size_hint s_sl_file_size_hints[MAX_OPEN_SLFS_FILES];
13044

    
13045
static int sl_fs_to_errno(_i32 r) {
13046
  DBG(("SL error: %d", (int) r));
13047
  switch (r) {
13048
    case SL_FS_OK:
13049
      return 0;
13050
    case SL_ERROR_FS_FILE_NAME_EXIST:
13051
      return EEXIST;
13052
    case SL_ERROR_FS_WRONG_FILE_NAME:
13053
      return EINVAL;
13054
    case SL_ERROR_FS_NO_AVAILABLE_NV_INDEX:
13055
    case SL_ERROR_FS_NOT_ENOUGH_STORAGE_SPACE:
13056
      return ENOSPC;
13057
    case SL_ERROR_FS_FAILED_TO_ALLOCATE_MEM:
13058
      return ENOMEM;
13059
    case SL_ERROR_FS_FILE_NOT_EXISTS:
13060
      return ENOENT;
13061
    case SL_ERROR_FS_NOT_SUPPORTED:
13062
      return ENOTSUP;
13063
  }
13064
  return ENXIO;
13065
}
13066

    
13067
int fs_slfs_open(const char *pathname, int flags, mode_t mode) {
13068
  int fd;
13069
  for (fd = 0; fd < MAX_OPEN_SLFS_FILES; fd++) {
13070
    if (s_sl_fds[fd].fh <= 0) break;
13071
  }
13072
  if (fd >= MAX_OPEN_SLFS_FILES) return set_errno(ENOMEM);
13073
  struct sl_fd_info *fi = &s_sl_fds[fd];
13074

    
13075
  /*
13076
   * Apply path manipulations again, in case we got here directly
13077
   * (via TI libc's "add_device").
13078
   */
13079
  pathname = drop_dir(pathname, NULL);
13080

    
13081
  _u32 am = 0;
13082
  fi->size = (size_t) -1;
13083
  int rw = (flags & 3);
13084
  size_t new_size = FS_SLFS_MAX_FILE_SIZE;
13085
  if (rw == O_RDONLY) {
13086
    SlFsFileInfo_t sl_fi;
13087
    _i32 r = sl_FsGetInfo((const _u8 *) pathname, 0, &sl_fi);
13088
    if (r == SL_FS_OK) {
13089
      fi->size = SL_FI_FILE_SIZE(sl_fi);
13090
    }
13091
    am = SL_FS_READ;
13092
  } else {
13093
    if (!(flags & O_TRUNC) || (flags & O_APPEND)) {
13094
      // FailFS files cannot be opened for append and will be truncated
13095
      // when opened for write.
13096
      return set_errno(ENOTSUP);
13097
    }
13098
    if (flags & O_CREAT) {
13099
      size_t i;
13100
      for (i = 0; i < MAX_OPEN_SLFS_FILES; i++) {
13101
        if (s_sl_file_size_hints[i].name != NULL &&
13102
            strcmp(s_sl_file_size_hints[i].name, pathname) == 0) {
13103
          new_size = s_sl_file_size_hints[i].size;
13104
          MG_FREE(s_sl_file_size_hints[i].name);
13105
          s_sl_file_size_hints[i].name = NULL;
13106
          break;
13107
        }
13108
      }
13109
      am = FS_MODE_OPEN_CREATE(new_size, 0);
13110
    } else {
13111
      am = SL_FS_WRITE;
13112
    }
13113
  }
13114
  fi->fh = slfs_open((_u8 *) pathname, am);
13115
  LOG(LL_DEBUG, ("sl_FsOpen(%s, 0x%x) sz %u = %d", pathname, (int) am,
13116
                 (unsigned int) new_size, (int) fi->fh));
13117
  int r;
13118
  if (fi->fh >= 0) {
13119
    fi->pos = 0;
13120
    r = fd;
13121
  } else {
13122
    r = set_errno(sl_fs_to_errno(fi->fh));
13123
  }
13124
  return r;
13125
}
13126

    
13127
int fs_slfs_close(int fd) {
13128
  struct sl_fd_info *fi = &s_sl_fds[fd];
13129
  if (fi->fh <= 0) return set_errno(EBADF);
13130
  _i32 r = sl_FsClose(fi->fh, NULL, NULL, 0);
13131
  LOG(LL_DEBUG, ("sl_FsClose(%d) = %d", (int) fi->fh, (int) r));
13132
  s_sl_fds[fd].fh = -1;
13133
  return set_errno(sl_fs_to_errno(r));
13134
}
13135

    
13136
ssize_t fs_slfs_read(int fd, void *buf, size_t count) {
13137
  struct sl_fd_info *fi = &s_sl_fds[fd];
13138
  if (fi->fh <= 0) return set_errno(EBADF);
13139
  /* Simulate EOF. sl_FsRead @ file_size return SL_FS_ERR_OFFSET_OUT_OF_RANGE.
13140
   */
13141
  if (fi->pos == fi->size) return 0;
13142
  _i32 r = sl_FsRead(fi->fh, fi->pos, buf, count);
13143
  DBG(("sl_FsRead(%d, %d, %d) = %d", (int) fi->fh, (int) fi->pos, (int) count,
13144
       (int) r));
13145
  if (r >= 0) {
13146
    fi->pos += r;
13147
    return r;
13148
  }
13149
  return set_errno(sl_fs_to_errno(r));
13150
}
13151

    
13152
ssize_t fs_slfs_write(int fd, const void *buf, size_t count) {
13153
  struct sl_fd_info *fi = &s_sl_fds[fd];
13154
  if (fi->fh <= 0) return set_errno(EBADF);
13155
  _i32 r = sl_FsWrite(fi->fh, fi->pos, (_u8 *) buf, count);
13156
  DBG(("sl_FsWrite(%d, %d, %d) = %d", (int) fi->fh, (int) fi->pos, (int) count,
13157
       (int) r));
13158
  if (r >= 0) {
13159
    fi->pos += r;
13160
    return r;
13161
  }
13162
  return set_errno(sl_fs_to_errno(r));
13163
}
13164

    
13165
int fs_slfs_stat(const char *pathname, struct stat *s) {
13166
  SlFsFileInfo_t sl_fi;
13167
  /*
13168
   * Apply path manipulations again, in case we got here directly
13169
   * (via TI libc's "add_device").
13170
   */
13171
  pathname = drop_dir(pathname, NULL);
13172
  _i32 r = sl_FsGetInfo((const _u8 *) pathname, 0, &sl_fi);
13173
  if (r == SL_FS_OK) {
13174
    s->st_mode = S_IFREG | 0666;
13175
    s->st_nlink = 1;
13176
    s->st_size = SL_FI_FILE_SIZE(sl_fi);
13177
    return 0;
13178
  }
13179
  return set_errno(sl_fs_to_errno(r));
13180
}
13181

    
13182
int fs_slfs_fstat(int fd, struct stat *s) {
13183
  struct sl_fd_info *fi = &s_sl_fds[fd];
13184
  if (fi->fh <= 0) return set_errno(EBADF);
13185
  s->st_mode = 0666;
13186
  s->st_mode = S_IFREG | 0666;
13187
  s->st_nlink = 1;
13188
  s->st_size = fi->size;
13189
  return 0;
13190
}
13191

    
13192
off_t fs_slfs_lseek(int fd, off_t offset, int whence) {
13193
  if (s_sl_fds[fd].fh <= 0) return set_errno(EBADF);
13194
  switch (whence) {
13195
    case SEEK_SET:
13196
      s_sl_fds[fd].pos = offset;
13197
      break;
13198
    case SEEK_CUR:
13199
      s_sl_fds[fd].pos += offset;
13200
      break;
13201
    case SEEK_END:
13202
      return set_errno(ENOTSUP);
13203
  }
13204
  return 0;
13205
}
13206

    
13207
int fs_slfs_unlink(const char *pathname) {
13208
  /*
13209
   * Apply path manipulations again, in case we got here directly
13210
   * (via TI libc's "add_device").
13211
   */
13212
  pathname = drop_dir(pathname, NULL);
13213
  return set_errno(sl_fs_to_errno(sl_FsDel((const _u8 *) pathname, 0)));
13214
}
13215

    
13216
int fs_slfs_rename(const char *from, const char *to) {
13217
  return set_errno(ENOTSUP);
13218
}
13219

    
13220
void fs_slfs_set_new_file_size(const char *name, size_t size) {
13221
  int i;
13222
  for (i = 0; i < MAX_OPEN_SLFS_FILES; i++) {
13223
    if (s_sl_file_size_hints[i].name == NULL) {
13224
      DBG(("File size hint: %s %d", name, (int) size));
13225
      s_sl_file_size_hints[i].name = strdup(name);
13226
      s_sl_file_size_hints[i].size = size;
13227
      break;
13228
    }
13229
  }
13230
}
13231

    
13232
#endif /* defined(MG_FS_SLFS) || defined(CC3200_FS_SLFS) */
13233
#ifdef MG_MODULE_LINES
13234
#line 1 "common/platforms/simplelink/sl_fs.c"
13235
#endif
13236
/*
13237
 * Copyright (c) 2014-2016 Cesanta Software Limited
13238
 * All rights reserved
13239
 */
13240

    
13241
#if MG_NET_IF == MG_NET_IF_SIMPLELINK && \
13242
    (defined(MG_FS_SLFS) || defined(MG_FS_SPIFFS))
13243

    
13244
int set_errno(int e) {
13245
  errno = e;
13246
  return (e == 0 ? 0 : -1);
13247
}
13248

    
13249
const char *drop_dir(const char *fname, bool *is_slfs) {
13250
  if (is_slfs != NULL) {
13251
    *is_slfs = (strncmp(fname, "SL:", 3) == 0);
13252
    if (*is_slfs) fname += 3;
13253
  }
13254
  /* Drop "./", if any */
13255
  if (fname[0] == '.' && fname[1] == '/') {
13256
    fname += 2;
13257
  }
13258
  /*
13259
   * Drop / if it is the only one in the path.
13260
   * This allows use of /pretend/directories but serves /file.txt as normal.
13261
   */
13262
  if (fname[0] == '/' && strchr(fname + 1, '/') == NULL) {
13263
    fname++;
13264
  }
13265
  return fname;
13266
}
13267

    
13268
#if !defined(MG_FS_NO_VFS)
13269

    
13270
#include <errno.h>
13271
#include <stdbool.h>
13272
#include <stdio.h>
13273
#include <stdlib.h>
13274
#include <string.h>
13275
#ifdef __TI_COMPILER_VERSION__
13276
#include <file.h>
13277
#endif
13278

    
13279
/* Amalgamated: #include "common/cs_dbg.h" */
13280
/* Amalgamated: #include "common/platform.h" */
13281

    
13282
#ifdef CC3200_FS_SPIFFS
13283
/* Amalgamated: #include "cc3200_fs_spiffs.h" */
13284
#endif
13285

    
13286
#ifdef MG_FS_SLFS
13287
/* Amalgamated: #include "sl_fs_slfs.h" */
13288
#endif
13289

    
13290
#define NUM_SYS_FDS 3
13291
#define SPIFFS_FD_BASE 10
13292
#define SLFS_FD_BASE 100
13293

    
13294
#if !defined(MG_UART_CHAR_PUT) && !defined(MG_UART_WRITE)
13295
#if CS_PLATFORM == CS_P_CC3200
13296
#include <inc/hw_types.h>
13297
#include <inc/hw_memmap.h>
13298
#include <driverlib/rom.h>
13299
#include <driverlib/rom_map.h>
13300
#include <driverlib/uart.h>
13301
#define MG_UART_CHAR_PUT(fd, c) MAP_UARTCharPut(UARTA0_BASE, c);
13302
#else
13303
#define MG_UART_WRITE(fd, buf, len)
13304
#endif /* CS_PLATFORM == CS_P_CC3200 */
13305
#endif /* !MG_UART_CHAR_PUT */
13306

    
13307
enum fd_type {
13308
  FD_INVALID,
13309
  FD_SYS,
13310
#ifdef CC3200_FS_SPIFFS
13311
  FD_SPIFFS,
13312
#endif
13313
#ifdef MG_FS_SLFS
13314
  FD_SLFS
13315
#endif
13316
};
13317
static int fd_type(int fd) {
13318
  if (fd >= 0 && fd < NUM_SYS_FDS) return FD_SYS;
13319
#ifdef CC3200_FS_SPIFFS
13320
  if (fd >= SPIFFS_FD_BASE && fd < SPIFFS_FD_BASE + MAX_OPEN_SPIFFS_FILES) {
13321
    return FD_SPIFFS;
13322
  }
13323
#endif
13324
#ifdef MG_FS_SLFS
13325
  if (fd >= SLFS_FD_BASE && fd < SLFS_FD_BASE + MAX_OPEN_SLFS_FILES) {
13326
    return FD_SLFS;
13327
  }
13328
#endif
13329
  return FD_INVALID;
13330
}
13331

    
13332
#if MG_TI_NO_HOST_INTERFACE
13333
int open(const char *pathname, unsigned flags, int mode) {
13334
#else
13335
int _open(const char *pathname, int flags, mode_t mode) {
13336
#endif
13337
  int fd = -1;
13338
  bool is_sl;
13339
  const char *fname = drop_dir(pathname, &is_sl);
13340
  if (is_sl) {
13341
#ifdef MG_FS_SLFS
13342
    fd = fs_slfs_open(fname, flags, mode);
13343
    if (fd >= 0) fd += SLFS_FD_BASE;
13344
#endif
13345
  } else {
13346
#ifdef CC3200_FS_SPIFFS
13347
    fd = fs_spiffs_open(fname, flags, mode);
13348
    if (fd >= 0) fd += SPIFFS_FD_BASE;
13349
#endif
13350
  }
13351
  LOG(LL_DEBUG,
13352
      ("open(%s, 0x%x) = %d, fname = %s", pathname, flags, fd, fname));
13353
  return fd;
13354
}
13355

    
13356
int _stat(const char *pathname, struct stat *st) {
13357
  int res = -1;
13358
  bool is_sl;
13359
  const char *fname = drop_dir(pathname, &is_sl);
13360
  memset(st, 0, sizeof(*st));
13361
  /* Simulate statting the root directory. */
13362
  if (fname[0] == '\0' || strcmp(fname, ".") == 0) {
13363
    st->st_ino = 0;
13364
    st->st_mode = S_IFDIR | 0777;
13365
    st->st_nlink = 1;
13366
    st->st_size = 0;
13367
    return 0;
13368
  }
13369
  if (is_sl) {
13370
#ifdef MG_FS_SLFS
13371
    res = fs_slfs_stat(fname, st);
13372
#endif
13373
  } else {
13374
#ifdef CC3200_FS_SPIFFS
13375
    res = fs_spiffs_stat(fname, st);
13376
#endif
13377
  }
13378
  LOG(LL_DEBUG, ("stat(%s) = %d; fname = %s", pathname, res, fname));
13379
  return res;
13380
}
13381

    
13382
#if MG_TI_NO_HOST_INTERFACE
13383
int close(int fd) {
13384
#else
13385
int _close(int fd) {
13386
#endif
13387
  int r = -1;
13388
  switch (fd_type(fd)) {
13389
    case FD_INVALID:
13390
      r = set_errno(EBADF);
13391
      break;
13392
    case FD_SYS:
13393
      r = set_errno(EACCES);
13394
      break;
13395
#ifdef CC3200_FS_SPIFFS
13396
    case FD_SPIFFS:
13397
      r = fs_spiffs_close(fd - SPIFFS_FD_BASE);
13398
      break;
13399
#endif
13400
#ifdef MG_FS_SLFS
13401
    case FD_SLFS:
13402
      r = fs_slfs_close(fd - SLFS_FD_BASE);
13403
      break;
13404
#endif
13405
  }
13406
  DBG(("close(%d) = %d", fd, r));
13407
  return r;
13408
}
13409

    
13410
#if MG_TI_NO_HOST_INTERFACE
13411
off_t lseek(int fd, off_t offset, int whence) {
13412
#else
13413
off_t _lseek(int fd, off_t offset, int whence) {
13414
#endif
13415
  int r = -1;
13416
  switch (fd_type(fd)) {
13417
    case FD_INVALID:
13418
      r = set_errno(EBADF);
13419
      break;
13420
    case FD_SYS:
13421
      r = set_errno(ESPIPE);
13422
      break;
13423
#ifdef CC3200_FS_SPIFFS
13424
    case FD_SPIFFS:
13425
      r = fs_spiffs_lseek(fd - SPIFFS_FD_BASE, offset, whence);
13426
      break;
13427
#endif
13428
#ifdef MG_FS_SLFS
13429
    case FD_SLFS:
13430
      r = fs_slfs_lseek(fd - SLFS_FD_BASE, offset, whence);
13431
      break;
13432
#endif
13433
  }
13434
  DBG(("lseek(%d, %d, %d) = %d", fd, (int) offset, whence, r));
13435
  return r;
13436
}
13437

    
13438
int _fstat(int fd, struct stat *s) {
13439
  int r = -1;
13440
  memset(s, 0, sizeof(*s));
13441
  switch (fd_type(fd)) {
13442
    case FD_INVALID:
13443
      r = set_errno(EBADF);
13444
      break;
13445
    case FD_SYS: {
13446
      /* Create barely passable stats for STD{IN,OUT,ERR}. */
13447
      memset(s, 0, sizeof(*s));
13448
      s->st_ino = fd;
13449
      s->st_mode = S_IFCHR | 0666;
13450
      r = 0;
13451
      break;
13452
    }
13453
#ifdef CC3200_FS_SPIFFS
13454
    case FD_SPIFFS:
13455
      r = fs_spiffs_fstat(fd - SPIFFS_FD_BASE, s);
13456
      break;
13457
#endif
13458
#ifdef MG_FS_SLFS
13459
    case FD_SLFS:
13460
      r = fs_slfs_fstat(fd - SLFS_FD_BASE, s);
13461
      break;
13462
#endif
13463
  }
13464
  DBG(("fstat(%d) = %d", fd, r));
13465
  return r;
13466
}
13467

    
13468
#if MG_TI_NO_HOST_INTERFACE
13469
int read(int fd, char *buf, unsigned count) {
13470
#else
13471
ssize_t _read(int fd, void *buf, size_t count) {
13472
#endif
13473
  int r = -1;
13474
  switch (fd_type(fd)) {
13475
    case FD_INVALID:
13476
      r = set_errno(EBADF);
13477
      break;
13478
    case FD_SYS: {
13479
      if (fd != 0) {
13480
        r = set_errno(EACCES);
13481
        break;
13482
      }
13483
      /* Should we allow reading from stdin = uart? */
13484
      r = set_errno(ENOTSUP);
13485
      break;
13486
    }
13487
#ifdef CC3200_FS_SPIFFS
13488
    case FD_SPIFFS:
13489
      r = fs_spiffs_read(fd - SPIFFS_FD_BASE, buf, count);
13490
      break;
13491
#endif
13492
#ifdef MG_FS_SLFS
13493
    case FD_SLFS:
13494
      r = fs_slfs_read(fd - SLFS_FD_BASE, buf, count);
13495
      break;
13496
#endif
13497
  }
13498
  DBG(("read(%d, %u) = %d", fd, count, r));
13499
  return r;
13500
}
13501

    
13502
#if MG_TI_NO_HOST_INTERFACE
13503
int write(int fd, const char *buf, unsigned count) {
13504
#else
13505
ssize_t _write(int fd, const void *buf, size_t count) {
13506
#endif
13507
  int r = -1;
13508
  switch (fd_type(fd)) {
13509
    case FD_INVALID:
13510
      r = set_errno(EBADF);
13511
      break;
13512
    case FD_SYS: {
13513
      if (fd == 0) {
13514
        r = set_errno(EACCES);
13515
        break;
13516
      }
13517
#ifdef MG_UART_WRITE
13518
      MG_UART_WRITE(fd, buf, count);
13519
#elif defined(MG_UART_CHAR_PUT)
13520
      {
13521
        size_t i;
13522
        for (i = 0; i < count; i++) {
13523
          const char c = ((const char *) buf)[i];
13524
          if (c == '\n') MG_UART_CHAR_PUT(fd, '\r');
13525
          MG_UART_CHAR_PUT(fd, c);
13526
        }
13527
      }
13528
#endif
13529
      r = count;
13530
      break;
13531
    }
13532
#ifdef CC3200_FS_SPIFFS
13533
    case FD_SPIFFS:
13534
      r = fs_spiffs_write(fd - SPIFFS_FD_BASE, buf, count);
13535
      break;
13536
#endif
13537
#ifdef MG_FS_SLFS
13538
    case FD_SLFS:
13539
      r = fs_slfs_write(fd - SLFS_FD_BASE, buf, count);
13540
      break;
13541
#endif
13542
  }
13543
  return r;
13544
}
13545

    
13546
/*
13547
 * On Newlib we override rename directly too, because the default
13548
 * implementation using _link and _unlink doesn't work for us.
13549
 */
13550
#if MG_TI_NO_HOST_INTERFACE || defined(_NEWLIB_VERSION)
13551
int rename(const char *frompath, const char *topath) {
13552
  int r = -1;
13553
  bool is_sl_from, is_sl_to;
13554
  const char *from = drop_dir(frompath, &is_sl_from);
13555
  const char *to = drop_dir(topath, &is_sl_to);
13556
  if (is_sl_from || is_sl_to) {
13557
    set_errno(ENOTSUP);
13558
  } else {
13559
#ifdef CC3200_FS_SPIFFS
13560
    r = fs_spiffs_rename(from, to);
13561
#endif
13562
  }
13563
  DBG(("rename(%s, %s) = %d", from, to, r));
13564
  return r;
13565
}
13566
#endif /* MG_TI_NO_HOST_INTERFACE || defined(_NEWLIB_VERSION) */
13567

    
13568
#if MG_TI_NO_HOST_INTERFACE
13569
int unlink(const char *pathname) {
13570
#else
13571
int _unlink(const char *pathname) {
13572
#endif
13573
  int r = -1;
13574
  bool is_sl;
13575
  const char *fname = drop_dir(pathname, &is_sl);
13576
  if (is_sl) {
13577
#ifdef MG_FS_SLFS
13578
    r = fs_slfs_unlink(fname);
13579
#endif
13580
  } else {
13581
#ifdef CC3200_FS_SPIFFS
13582
    r = fs_spiffs_unlink(fname);
13583
#endif
13584
  }
13585
  DBG(("unlink(%s) = %d, fname = %s", pathname, r, fname));
13586
  return r;
13587
}
13588

    
13589
#ifdef CC3200_FS_SPIFFS /* FailFS does not support listing files. */
13590
DIR *opendir(const char *dir_name) {
13591
  DIR *r = NULL;
13592
  bool is_sl;
13593
  drop_dir(dir_name, &is_sl);
13594
  if (is_sl) {
13595
    r = NULL;
13596
    set_errno(ENOTSUP);
13597
  } else {
13598
    r = fs_spiffs_opendir(dir_name);
13599
  }
13600
  DBG(("opendir(%s) = %p", dir_name, r));
13601
  return r;
13602
}
13603

    
13604
struct dirent *readdir(DIR *dir) {
13605
  struct dirent *res = fs_spiffs_readdir(dir);
13606
  DBG(("readdir(%p) = %p", dir, res));
13607
  return res;
13608
}
13609

    
13610
int closedir(DIR *dir) {
13611
  int res = fs_spiffs_closedir(dir);
13612
  DBG(("closedir(%p) = %d", dir, res));
13613
  return res;
13614
}
13615

    
13616
int rmdir(const char *path) {
13617
  return fs_spiffs_rmdir(path);
13618
}
13619

    
13620
int mkdir(const char *path, mode_t mode) {
13621
  (void) path;
13622
  (void) mode;
13623
  /* for spiffs supports only root dir, which comes from mongoose as '.' */
13624
  return (strlen(path) == 1 && *path == '.') ? 0 : ENOTDIR;
13625
}
13626
#endif
13627

    
13628
int sl_fs_init(void) {
13629
  int ret = 1;
13630
#ifdef __TI_COMPILER_VERSION__
13631
#ifdef MG_FS_SLFS
13632
#pragma diag_push
13633
#pragma diag_suppress 169 /* Nothing we can do about the prototype mismatch. \
13634
                             */
13635
  ret = (add_device("SL", _MSA, fs_slfs_open, fs_slfs_close, fs_slfs_read,
13636
                    fs_slfs_write, fs_slfs_lseek, fs_slfs_unlink,
13637
                    fs_slfs_rename) == 0);
13638
#pragma diag_pop
13639
#endif
13640
#endif
13641
  return ret;
13642
}
13643

    
13644
#endif /* !defined(MG_FS_NO_VFS) */
13645
#endif /* MG_NET_IF == MG_NET_IF_SIMPLELINK && (defined(MG_FS_SLFS) || \
13646
          defined(MG_FS_SPIFFS)) */
13647
#ifdef MG_MODULE_LINES
13648
#line 1 "common/platforms/simplelink/sl_socket.c"
13649
#endif
13650
/*
13651
 * Copyright (c) 2014-2016 Cesanta Software Limited
13652
 * All rights reserved
13653
 */
13654

    
13655
#if MG_NET_IF == MG_NET_IF_SIMPLELINK
13656

    
13657
#include <errno.h>
13658
#include <stdio.h>
13659

    
13660
/* Amalgamated: #include "common/platform.h" */
13661

    
13662
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size) {
13663
  int res;
13664
  struct in_addr *in = (struct in_addr *) src;
13665
  if (af != AF_INET) {
13666
    errno = ENOTSUP;
13667
    return NULL;
13668
  }
13669
  res = snprintf(dst, size, "%lu.%lu.%lu.%lu", SL_IPV4_BYTE(in->s_addr, 0),
13670
                 SL_IPV4_BYTE(in->s_addr, 1), SL_IPV4_BYTE(in->s_addr, 2),
13671
                 SL_IPV4_BYTE(in->s_addr, 3));
13672
  return res > 0 ? dst : NULL;
13673
}
13674

    
13675
char *inet_ntoa(struct in_addr n) {
13676
  static char a[16];
13677
  return (char *) inet_ntop(AF_INET, &n, a, sizeof(a));
13678
}
13679

    
13680
int inet_pton(int af, const char *src, void *dst) {
13681
  uint32_t a0, a1, a2, a3;
13682
  uint8_t *db = (uint8_t *) dst;
13683
  if (af != AF_INET) {
13684
    errno = ENOTSUP;
13685
    return 0;
13686
  }
13687
  if (sscanf(src, "%lu.%lu.%lu.%lu", &a0, &a1, &a2, &a3) != 4) {
13688
    return 0;
13689
  }
13690
  *db = a3;
13691
  *(db + 1) = a2;
13692
  *(db + 2) = a1;
13693
  *(db + 3) = a0;
13694
  return 1;
13695
}
13696

    
13697
#endif /* MG_NET_IF == MG_NET_IF_SIMPLELINK */
13698
#ifdef MG_MODULE_LINES
13699
#line 1 "common/platforms/simplelink/sl_mg_task.c"
13700
#endif
13701
#if MG_NET_IF == MG_NET_IF_SIMPLELINK && !defined(MG_SIMPLELINK_NO_OSI)
13702

    
13703
/* Amalgamated: #include "mg_task.h" */
13704

    
13705
#include <oslib/osi.h>
13706

    
13707
enum mg_q_msg_type {
13708
  MG_Q_MSG_CB,
13709
};
13710
struct mg_q_msg {
13711
  enum mg_q_msg_type type;
13712
  void (*cb)(struct mg_mgr *mgr, void *arg);
13713
  void *arg;
13714
};
13715
static OsiMsgQ_t s_mg_q;
13716
static void mg_task(void *arg);
13717

    
13718
bool mg_start_task(int priority, int stack_size, mg_init_cb mg_init) {
13719
  if (osi_MsgQCreate(&s_mg_q, "MG", sizeof(struct mg_q_msg), 16) != OSI_OK) {
13720
    return false;
13721
  }
13722
  if (osi_TaskCreate(mg_task, (const signed char *) "MG", stack_size,
13723
                     (void *) mg_init, priority, NULL) != OSI_OK) {
13724
    return false;
13725
  }
13726
  return true;
13727
}
13728

    
13729
static void mg_task(void *arg) {
13730
  struct mg_mgr mgr;
13731
  mg_init_cb mg_init = (mg_init_cb) arg;
13732
  mg_mgr_init(&mgr, NULL);
13733
  mg_init(&mgr);
13734
  while (1) {
13735
    struct mg_q_msg msg;
13736
    mg_mgr_poll(&mgr, 1);
13737
    if (osi_MsgQRead(&s_mg_q, &msg, 1) != OSI_OK) continue;
13738
    switch (msg.type) {
13739
      case MG_Q_MSG_CB: {
13740
        msg.cb(&mgr, msg.arg);
13741
      }
13742
    }
13743
  }
13744
}
13745

    
13746
void mg_run_in_task(void (*cb)(struct mg_mgr *mgr, void *arg), void *cb_arg) {
13747
  struct mg_q_msg msg = {MG_Q_MSG_CB, cb, cb_arg};
13748
  osi_MsgQWrite(&s_mg_q, &msg, OSI_NO_WAIT);
13749
}
13750

    
13751
#endif /* MG_NET_IF == MG_NET_IF_SIMPLELINK && !defined(MG_SIMPLELINK_NO_OSI) \
13752
          */
13753
#ifdef MG_MODULE_LINES
13754
#line 1 "common/platforms/simplelink/sl_net_if.h"
13755
#endif
13756
/*
13757
 * Copyright (c) 2014-2016 Cesanta Software Limited
13758
 * All rights reserved
13759
 */
13760

    
13761
#ifndef CS_COMMON_PLATFORMS_SIMPLELINK_SL_NET_IF_H_
13762
#define CS_COMMON_PLATFORMS_SIMPLELINK_SL_NET_IF_H_
13763

    
13764
/* Amalgamated: #include "mongoose/src/net_if.h" */
13765

    
13766
#ifdef __cplusplus
13767
extern "C" {
13768
#endif /* __cplusplus */
13769

    
13770
#ifndef MG_ENABLE_NET_IF_SIMPLELINK
13771
#define MG_ENABLE_NET_IF_SIMPLELINK MG_NET_IF == MG_NET_IF_SIMPLELINK
13772
#endif
13773

    
13774
extern const struct mg_iface_vtable mg_simplelink_iface_vtable;
13775

    
13776
#ifdef __cplusplus
13777
}
13778
#endif /* __cplusplus */
13779

    
13780
#endif /* CS_COMMON_PLATFORMS_SIMPLELINK_SL_NET_IF_H_ */
13781
#ifdef MG_MODULE_LINES
13782
#line 1 "common/platforms/simplelink/sl_net_if.c"
13783
#endif
13784
/*
13785
 * Copyright (c) 2014-2016 Cesanta Software Limited
13786
 * All rights reserved
13787
 */
13788

    
13789
/* Amalgamated: #include "common/platforms/simplelink/sl_net_if.h" */
13790

    
13791
#if MG_ENABLE_NET_IF_SIMPLELINK
13792

    
13793
/* Amalgamated: #include "mongoose/src/internal.h" */
13794
/* Amalgamated: #include "mongoose/src/util.h" */
13795

    
13796
#define MG_TCP_RECV_BUFFER_SIZE 1024
13797
#define MG_UDP_RECV_BUFFER_SIZE 1500
13798

    
13799
static sock_t mg_open_listening_socket(struct mg_connection *nc,
13800
                                       union socket_address *sa, int type,
13801
                                       int proto);
13802

    
13803
void mg_set_non_blocking_mode(sock_t sock) {
13804
  SlSockNonblocking_t opt;
13805
#if SL_MAJOR_VERSION_NUM < 2
13806
  opt.NonblockingEnabled = 1;
13807
#else
13808
  opt.NonBlockingEnabled = 1;
13809
#endif
13810
  sl_SetSockOpt(sock, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &opt, sizeof(opt));
13811
}
13812

    
13813
static int mg_is_error(int n) {
13814
  return (n < 0 && n != SL_ERROR_BSD_EALREADY && n != SL_ERROR_BSD_EAGAIN);
13815
}
13816

    
13817
void mg_sl_if_connect_tcp(struct mg_connection *nc,
13818
                          const union socket_address *sa) {
13819
  int proto = 0;
13820
  if (nc->flags & MG_F_SSL) proto = SL_SEC_SOCKET;
13821
  sock_t sock = sl_Socket(AF_INET, SOCK_STREAM, proto);
13822
  if (sock < 0) {
13823
    nc->err = sock;
13824
    goto out;
13825
  }
13826
  mg_sock_set(nc, sock);
13827
#if MG_ENABLE_SSL
13828
  nc->err = sl_set_ssl_opts(sock, nc);
13829
  if (nc->err != 0) goto out;
13830
#endif
13831
  nc->err = sl_Connect(sock, &sa->sa, sizeof(sa->sin));
13832
out:
13833
  DBG(("%p to %s:%d sock %d %d err %d", nc, inet_ntoa(sa->sin.sin_addr),
13834
       ntohs(sa->sin.sin_port), nc->sock, proto, nc->err));
13835
}
13836

    
13837
void mg_sl_if_connect_udp(struct mg_connection *nc) {
13838
  sock_t sock = sl_Socket(AF_INET, SOCK_DGRAM, 0);
13839
  if (sock < 0) {
13840
    nc->err = sock;
13841
    return;
13842
  }
13843
  mg_sock_set(nc, sock);
13844
  nc->err = 0;
13845
}
13846

    
13847
int mg_sl_if_listen_tcp(struct mg_connection *nc, union socket_address *sa) {
13848
  int proto = 0;
13849
  if (nc->flags & MG_F_SSL) proto = SL_SEC_SOCKET;
13850
  sock_t sock = mg_open_listening_socket(nc, sa, SOCK_STREAM, proto);
13851
  if (sock < 0) return sock;
13852
  mg_sock_set(nc, sock);
13853
  return 0;
13854
}
13855

    
13856
int mg_sl_if_listen_udp(struct mg_connection *nc, union socket_address *sa) {
13857
  sock_t sock = mg_open_listening_socket(nc, sa, SOCK_DGRAM, 0);
13858
  if (sock == INVALID_SOCKET) return (errno ? errno : 1);
13859
  mg_sock_set(nc, sock);
13860
  return 0;
13861
}
13862

    
13863
void mg_sl_if_tcp_send(struct mg_connection *nc, const void *buf, size_t len) {
13864
  mbuf_append(&nc->send_mbuf, buf, len);
13865
}
13866

    
13867
void mg_sl_if_udp_send(struct mg_connection *nc, const void *buf, size_t len) {
13868
  mbuf_append(&nc->send_mbuf, buf, len);
13869
}
13870

    
13871
void mg_sl_if_recved(struct mg_connection *nc, size_t len) {
13872
  (void) nc;
13873
  (void) len;
13874
}
13875

    
13876
int mg_sl_if_create_conn(struct mg_connection *nc) {
13877
  (void) nc;
13878
  return 1;
13879
}
13880

    
13881
void mg_sl_if_destroy_conn(struct mg_connection *nc) {
13882
  if (nc->sock == INVALID_SOCKET) return;
13883
  /* For UDP, only close outgoing sockets or listeners. */
13884
  if (!(nc->flags & MG_F_UDP) || nc->listener == NULL) {
13885
    sl_Close(nc->sock);
13886
  }
13887
  nc->sock = INVALID_SOCKET;
13888
}
13889

    
13890
static int mg_accept_conn(struct mg_connection *lc) {
13891
  struct mg_connection *nc;
13892
  union socket_address sa;
13893
  socklen_t sa_len = sizeof(sa);
13894
  sock_t sock = sl_Accept(lc->sock, &sa.sa, &sa_len);
13895
  if (sock < 0) {
13896
    DBG(("%p: failed to accept: %d", lc, sock));
13897
    return 0;
13898
  }
13899
  nc = mg_if_accept_new_conn(lc);
13900
  if (nc == NULL) {
13901
    sl_Close(sock);
13902
    return 0;
13903
  }
13904
  DBG(("%p conn from %s:%d", nc, inet_ntoa(sa.sin.sin_addr),
13905
       ntohs(sa.sin.sin_port)));
13906
  mg_sock_set(nc, sock);
13907
  if (nc->flags & MG_F_SSL) nc->flags |= MG_F_SSL_HANDSHAKE_DONE;
13908
  mg_if_accept_tcp_cb(nc, &sa, sa_len);
13909
  return 1;
13910
}
13911

    
13912
/* 'sa' must be an initialized address to bind to */
13913
static sock_t mg_open_listening_socket(struct mg_connection *nc,
13914
                                       union socket_address *sa, int type,
13915
                                       int proto) {
13916
  int r;
13917
  socklen_t sa_len =
13918
      (sa->sa.sa_family == AF_INET) ? sizeof(sa->sin) : sizeof(sa->sin6);
13919
  sock_t sock = sl_Socket(sa->sa.sa_family, type, proto);
13920
  if (sock < 0) return sock;
13921
  if ((r = sl_Bind(sock, &sa->sa, sa_len)) < 0) goto clean;
13922
  if (type != SOCK_DGRAM) {
13923
#if MG_ENABLE_SSL
13924
    if ((r = sl_set_ssl_opts(sock, nc)) < 0) goto clean;
13925
#endif
13926
    if ((r = sl_Listen(sock, SOMAXCONN)) < 0) goto clean;
13927
  }
13928
  mg_set_non_blocking_mode(sock);
13929
clean:
13930
  if (r < 0) {
13931
    sl_Close(sock);
13932
    sock = r;
13933
  }
13934
  return sock;
13935
}
13936

    
13937
static void mg_write_to_socket(struct mg_connection *nc) {
13938
  struct mbuf *io = &nc->send_mbuf;
13939
  int n = 0;
13940

    
13941
  if (nc->flags & MG_F_UDP) {
13942
    n = sl_SendTo(nc->sock, io->buf, io->len, 0, &nc->sa.sa,
13943
                  sizeof(nc->sa.sin));
13944
    DBG(("%p %d %d %d %s:%hu", nc, nc->sock, n, errno,
13945
         inet_ntoa(nc->sa.sin.sin_addr), ntohs(nc->sa.sin.sin_port)));
13946
  } else {
13947
    n = (int) sl_Send(nc->sock, io->buf, io->len, 0);
13948
    DBG(("%p %d bytes -> %d", nc, n, nc->sock));
13949
  }
13950

    
13951
  if (n > 0) {
13952
    mg_if_sent_cb(nc, n);
13953
  } else if (n < 0 && mg_is_error(n)) {
13954
    /* Something went wrong, drop the connection. */
13955
    nc->flags |= MG_F_CLOSE_IMMEDIATELY;
13956
  }
13957
}
13958

    
13959
MG_INTERNAL size_t recv_avail_size(struct mg_connection *conn, size_t max) {
13960
  size_t avail;
13961
  if (conn->recv_mbuf_limit < conn->recv_mbuf.len) return 0;
13962
  avail = conn->recv_mbuf_limit - conn->recv_mbuf.len;
13963
  return avail > max ? max : avail;
13964
}
13965

    
13966
static void mg_handle_tcp_read(struct mg_connection *conn) {
13967
  int n = 0;
13968
  char *buf = (char *) MG_MALLOC(MG_TCP_RECV_BUFFER_SIZE);
13969

    
13970
  if (buf == NULL) {
13971
    DBG(("OOM"));
13972
    return;
13973
  }
13974

    
13975
  n = (int) sl_Recv(conn->sock, buf,
13976
                    recv_avail_size(conn, MG_TCP_RECV_BUFFER_SIZE), 0);
13977
  DBG(("%p %d bytes <- %d", conn, n, conn->sock));
13978
  if (n > 0) {
13979
    mg_if_recv_tcp_cb(conn, buf, n, 1 /* own */);
13980
  } else {
13981
    MG_FREE(buf);
13982
  }
13983
  if (n == 0) {
13984
    /* Orderly shutdown of the socket, try flushing output. */
13985
    conn->flags |= MG_F_SEND_AND_CLOSE;
13986
  } else if (mg_is_error(n)) {
13987
    conn->flags |= MG_F_CLOSE_IMMEDIATELY;
13988
  }
13989
}
13990

    
13991
static void mg_handle_udp_read(struct mg_connection *nc) {
13992
  char *buf = (char *) MG_MALLOC(MG_UDP_RECV_BUFFER_SIZE);
13993
  if (buf == NULL) return;
13994
  union socket_address sa;
13995
  socklen_t sa_len = sizeof(sa);
13996
  int n = sl_RecvFrom(nc->sock, buf, MG_UDP_RECV_BUFFER_SIZE, 0,
13997
                      (SlSockAddr_t *) &sa, &sa_len);
13998
  DBG(("%p %d bytes from %s:%d", nc, n, inet_ntoa(nc->sa.sin.sin_addr),
13999
       ntohs(nc->sa.sin.sin_port)));
14000
  if (n > 0) {
14001
    mg_if_recv_udp_cb(nc, buf, n, &sa, sa_len);
14002
  } else {
14003
    MG_FREE(buf);
14004
  }
14005
}
14006

    
14007
#define _MG_F_FD_CAN_READ 1
14008
#define _MG_F_FD_CAN_WRITE 1 << 1
14009
#define _MG_F_FD_ERROR 1 << 2
14010

    
14011
void mg_mgr_handle_conn(struct mg_connection *nc, int fd_flags, double now) {
14012
  DBG(("%p fd=%d fd_flags=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->sock,
14013
       fd_flags, nc->flags, (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len));
14014

    
14015
  if (nc->flags & MG_F_CONNECTING) {
14016
    if (nc->flags & MG_F_UDP || nc->err != SL_ERROR_BSD_EALREADY) {
14017
      mg_if_connect_cb(nc, nc->err);
14018
    } else {
14019
      /* In SimpleLink, to get status of non-blocking connect() we need to wait
14020
       * until socket is writable and repeat the call to sl_Connect again,
14021
       * which will now return the real status. */
14022
      if (fd_flags & _MG_F_FD_CAN_WRITE) {
14023
        nc->err = sl_Connect(nc->sock, &nc->sa.sa, sizeof(nc->sa.sin));
14024
        DBG(("%p conn res=%d", nc, nc->err));
14025
        if (nc->err == SL_ERROR_BSD_ESECSNOVERIFY ||
14026
            /* TODO(rojer): Provide API to set the date for verification. */
14027
            nc->err == SL_ERROR_BSD_ESECDATEERROR
14028
#if SL_MAJOR_VERSION_NUM >= 2
14029
            /* Per SWRU455, this error does not mean verification failed,
14030
             * it only means that the cert used is not present in the trusted
14031
             * root CA catalog. Which is perfectly fine. */
14032
            ||
14033
            nc->err == SL_ERROR_BSD_ESECUNKNOWNROOTCA
14034
#endif
14035
            ) {
14036
          nc->err = 0;
14037
        }
14038
        if (nc->flags & MG_F_SSL && nc->err == 0) {
14039
          nc->flags |= MG_F_SSL_HANDSHAKE_DONE;
14040
        }
14041
        mg_if_connect_cb(nc, nc->err);
14042
      }
14043
    }
14044
    /* Ignore read/write in further processing, we've handled it. */
14045
    fd_flags &= ~(_MG_F_FD_CAN_READ | _MG_F_FD_CAN_WRITE);
14046
  }
14047

    
14048
  if (fd_flags & _MG_F_FD_CAN_READ) {
14049
    if (nc->flags & MG_F_UDP) {
14050
      mg_handle_udp_read(nc);
14051
    } else {
14052
      if (nc->flags & MG_F_LISTENING) {
14053
        mg_accept_conn(nc);
14054
      } else {
14055
        mg_handle_tcp_read(nc);
14056
      }
14057
    }
14058
  }
14059

    
14060
  if (!(nc->flags & MG_F_CLOSE_IMMEDIATELY)) {
14061
    if ((fd_flags & _MG_F_FD_CAN_WRITE) && nc->send_mbuf.len > 0) {
14062
      mg_write_to_socket(nc);
14063
    }
14064

    
14065
    if (!(fd_flags & (_MG_F_FD_CAN_READ | _MG_F_FD_CAN_WRITE))) {
14066
      mg_if_poll(nc, now);
14067
    }
14068
    mg_if_timer(nc, now);
14069
  }
14070

    
14071
  DBG(("%p after fd=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->sock, nc->flags,
14072
       (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len));
14073
}
14074

    
14075
/* Associate a socket to a connection. */
14076
void mg_sl_if_sock_set(struct mg_connection *nc, sock_t sock) {
14077
  mg_set_non_blocking_mode(sock);
14078
  nc->sock = sock;
14079
  DBG(("%p %d", nc, sock));
14080
}
14081

    
14082
void mg_sl_if_init(struct mg_iface *iface) {
14083
  (void) iface;
14084
  DBG(("%p using sl_Select()", iface->mgr));
14085
}
14086

    
14087
void mg_sl_if_free(struct mg_iface *iface) {
14088
  (void) iface;
14089
}
14090

    
14091
void mg_sl_if_add_conn(struct mg_connection *nc) {
14092
  (void) nc;
14093
}
14094

    
14095
void mg_sl_if_remove_conn(struct mg_connection *nc) {
14096
  (void) nc;
14097
}
14098

    
14099
time_t mg_sl_if_poll(struct mg_iface *iface, int timeout_ms) {
14100
  struct mg_mgr *mgr = iface->mgr;
14101
  double now = mg_time();
14102
  double min_timer;
14103
  struct mg_connection *nc, *tmp;
14104
  struct SlTimeval_t tv;
14105
  SlFdSet_t read_set, write_set, err_set;
14106
  sock_t max_fd = INVALID_SOCKET;
14107
  int num_fds, num_ev = 0, num_timers = 0;
14108

    
14109
  SL_SOCKET_FD_ZERO(&read_set);
14110
  SL_SOCKET_FD_ZERO(&write_set);
14111
  SL_SOCKET_FD_ZERO(&err_set);
14112

    
14113
  /*
14114
   * Note: it is ok to have connections with sock == INVALID_SOCKET in the list,
14115
   * e.g. timer-only "connections".
14116
   */
14117
  min_timer = 0;
14118
  for (nc = mgr->active_connections, num_fds = 0; nc != NULL; nc = tmp) {
14119
    tmp = nc->next;
14120

    
14121
    if (nc->sock != INVALID_SOCKET) {
14122
      num_fds++;
14123

    
14124
      if (!(nc->flags & MG_F_WANT_WRITE) &&
14125
          nc->recv_mbuf.len < nc->recv_mbuf_limit &&
14126
          (!(nc->flags & MG_F_UDP) || nc->listener == NULL)) {
14127
        SL_SOCKET_FD_SET(nc->sock, &read_set);
14128
        if (max_fd == INVALID_SOCKET || nc->sock > max_fd) max_fd = nc->sock;
14129
      }
14130

    
14131
      if (((nc->flags & MG_F_CONNECTING) && !(nc->flags & MG_F_WANT_READ)) ||
14132
          (nc->send_mbuf.len > 0 && !(nc->flags & MG_F_CONNECTING))) {
14133
        SL_SOCKET_FD_SET(nc->sock, &write_set);
14134
        SL_SOCKET_FD_SET(nc->sock, &err_set);
14135
        if (max_fd == INVALID_SOCKET || nc->sock > max_fd) max_fd = nc->sock;
14136
      }
14137
    }
14138

    
14139
    if (nc->ev_timer_time > 0) {
14140
      if (num_timers == 0 || nc->ev_timer_time < min_timer) {
14141
        min_timer = nc->ev_timer_time;
14142
      }
14143
      num_timers++;
14144
    }
14145
  }
14146

    
14147
  /*
14148
   * If there is a timer to be fired earlier than the requested timeout,
14149
   * adjust the timeout.
14150
   */
14151
  if (num_timers > 0) {
14152
    double timer_timeout_ms = (min_timer - mg_time()) * 1000 + 1 /* rounding */;
14153
    if (timer_timeout_ms < timeout_ms) {
14154
      timeout_ms = timer_timeout_ms;
14155
    }
14156
  }
14157
  if (timeout_ms < 0) timeout_ms = 0;
14158

    
14159
  tv.tv_sec = timeout_ms / 1000;
14160
  tv.tv_usec = (timeout_ms % 1000) * 1000;
14161

    
14162
  if (num_fds > 0) {
14163
    num_ev = sl_Select((int) max_fd + 1, &read_set, &write_set, &err_set, &tv);
14164
  }
14165

    
14166
  now = mg_time();
14167
  DBG(("sl_Select @ %ld num_ev=%d of %d, timeout=%d", (long) now, num_ev,
14168
       num_fds, timeout_ms));
14169

    
14170
  for (nc = mgr->active_connections; nc != NULL; nc = tmp) {
14171
    int fd_flags = 0;
14172
    if (nc->sock != INVALID_SOCKET) {
14173
      if (num_ev > 0) {
14174
        fd_flags =
14175
            (SL_SOCKET_FD_ISSET(nc->sock, &read_set) &&
14176
                     (!(nc->flags & MG_F_UDP) || nc->listener == NULL)
14177
                 ? _MG_F_FD_CAN_READ
14178
                 : 0) |
14179
            (SL_SOCKET_FD_ISSET(nc->sock, &write_set) ? _MG_F_FD_CAN_WRITE
14180
                                                      : 0) |
14181
            (SL_SOCKET_FD_ISSET(nc->sock, &err_set) ? _MG_F_FD_ERROR : 0);
14182
      }
14183
      /* SimpleLink does not report UDP sockets as writable. */
14184
      if (nc->flags & MG_F_UDP && nc->send_mbuf.len > 0) {
14185
        fd_flags |= _MG_F_FD_CAN_WRITE;
14186
      }
14187
    }
14188
    tmp = nc->next;
14189
    mg_mgr_handle_conn(nc, fd_flags, now);
14190
  }
14191

    
14192
  for (nc = mgr->active_connections; nc != NULL; nc = tmp) {
14193
    tmp = nc->next;
14194
    if ((nc->flags & MG_F_CLOSE_IMMEDIATELY) ||
14195
        (nc->send_mbuf.len == 0 && (nc->flags & MG_F_SEND_AND_CLOSE))) {
14196
      mg_close_conn(nc);
14197
    }
14198
  }
14199

    
14200
  return now;
14201
}
14202

    
14203
void mg_sl_if_get_conn_addr(struct mg_connection *nc, int remote,
14204
                            union socket_address *sa) {
14205
  /* SimpleLink does not provide a way to get socket's peer address after
14206
   * accept or connect. Address should have been preserved in the connection,
14207
   * so we do our best here by using it. */
14208
  if (remote) memcpy(sa, &nc->sa, sizeof(*sa));
14209
}
14210

    
14211
void sl_restart_cb(struct mg_mgr *mgr) {
14212
  /*
14213
   * SimpleLink has been restarted, meaning all sockets have been invalidated.
14214
   * We try our best - we'll restart the listeners, but for outgoing
14215
   * connections we have no option but to terminate.
14216
   */
14217
  struct mg_connection *nc;
14218
  for (nc = mg_next(mgr, NULL); nc != NULL; nc = mg_next(mgr, nc)) {
14219
    if (nc->sock == INVALID_SOCKET) continue; /* Could be a timer */
14220
    if (nc->flags & MG_F_LISTENING) {
14221
      DBG(("restarting %p %s:%d", nc, inet_ntoa(nc->sa.sin.sin_addr),
14222
           ntohs(nc->sa.sin.sin_port)));
14223
      int res = (nc->flags & MG_F_UDP ? mg_sl_if_listen_udp(nc, &nc->sa)
14224
                                      : mg_sl_if_listen_tcp(nc, &nc->sa));
14225
      if (res == 0) continue;
14226
      /* Well, we tried and failed. Fall through to closing. */
14227
    }
14228
    nc->sock = INVALID_SOCKET;
14229
    DBG(("terminating %p %s:%d", nc, inet_ntoa(nc->sa.sin.sin_addr),
14230
         ntohs(nc->sa.sin.sin_port)));
14231
    /* TODO(rojer): Outgoing UDP? */
14232
    nc->flags |= MG_F_CLOSE_IMMEDIATELY;
14233
  }
14234
}
14235

    
14236
/* clang-format off */
14237
#define MG_SL_IFACE_VTABLE                                              \
14238
  {                                                                     \
14239
    mg_sl_if_init,                                                      \
14240
    mg_sl_if_free,                                                      \
14241
    mg_sl_if_add_conn,                                                  \
14242
    mg_sl_if_remove_conn,                                               \
14243
    mg_sl_if_poll,                                                      \
14244
    mg_sl_if_listen_tcp,                                                \
14245
    mg_sl_if_listen_udp,                                                \
14246
    mg_sl_if_connect_tcp,                                               \
14247
    mg_sl_if_connect_udp,                                               \
14248
    mg_sl_if_tcp_send,                                                  \
14249
    mg_sl_if_udp_send,                                                  \
14250
    mg_sl_if_recved,                                                    \
14251
    mg_sl_if_create_conn,                                               \
14252
    mg_sl_if_destroy_conn,                                              \
14253
    mg_sl_if_sock_set,                                                  \
14254
    mg_sl_if_get_conn_addr,                                             \
14255
  }
14256
/* clang-format on */
14257

    
14258
const struct mg_iface_vtable mg_simplelink_iface_vtable = MG_SL_IFACE_VTABLE;
14259
#if MG_NET_IF == MG_NET_IF_SIMPLELINK
14260
const struct mg_iface_vtable mg_default_iface_vtable = MG_SL_IFACE_VTABLE;
14261
#endif
14262

    
14263
#endif /* MG_ENABLE_NET_IF_SIMPLELINK */
14264
#ifdef MG_MODULE_LINES
14265
#line 1 "common/platforms/simplelink/sl_ssl_if.c"
14266
#endif
14267
/*
14268
 * Copyright (c) 2014-2016 Cesanta Software Limited
14269
 * All rights reserved
14270
 */
14271

    
14272
#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_SIMPLELINK
14273

    
14274
/* Amalgamated: #include "common/mg_mem.h" */
14275

    
14276
#ifndef MG_SSL_IF_SIMPLELINK_SLFS_PREFIX
14277
#define MG_SSL_IF_SIMPLELINK_SLFS_PREFIX "SL:"
14278
#endif
14279

    
14280
#define MG_SSL_IF_SIMPLELINK_SLFS_PREFIX_LEN \
14281
  (sizeof(MG_SSL_IF_SIMPLELINK_SLFS_PREFIX) - 1)
14282

    
14283
struct mg_ssl_if_ctx {
14284
  char *ssl_cert;
14285
  char *ssl_key;
14286
  char *ssl_ca_cert;
14287
  char *ssl_server_name;
14288
};
14289

    
14290
void mg_ssl_if_init() {
14291
}
14292

    
14293
enum mg_ssl_if_result mg_ssl_if_conn_init(
14294
    struct mg_connection *nc, const struct mg_ssl_if_conn_params *params,
14295
    const char **err_msg) {
14296
  struct mg_ssl_if_ctx *ctx =
14297
      (struct mg_ssl_if_ctx *) MG_CALLOC(1, sizeof(*ctx));
14298
  if (ctx == NULL) {
14299
    MG_SET_PTRPTR(err_msg, "Out of memory");
14300
    return MG_SSL_ERROR;
14301
  }
14302
  nc->ssl_if_data = ctx;
14303

    
14304
  if (params->cert != NULL || params->key != NULL) {
14305
    if (params->cert != NULL && params->key != NULL) {
14306
      ctx->ssl_cert = strdup(params->cert);
14307
      ctx->ssl_key = strdup(params->key);
14308
    } else {
14309
      MG_SET_PTRPTR(err_msg, "Both cert and key are required.");
14310
      return MG_SSL_ERROR;
14311
    }
14312
  }
14313
  if (params->ca_cert != NULL && strcmp(params->ca_cert, "*") != 0) {
14314
    ctx->ssl_ca_cert = strdup(params->ca_cert);
14315
  }
14316
  /* TODO(rojer): cipher_suites. */
14317
  if (params->server_name != NULL) {
14318
    ctx->ssl_server_name = strdup(params->server_name);
14319
  }
14320
  return MG_SSL_OK;
14321
}
14322

    
14323
void mg_ssl_if_conn_close_notify(struct mg_connection *nc) {
14324
  /* Nothing to do */
14325
  (void) nc;
14326
}
14327

    
14328
void mg_ssl_if_conn_free(struct mg_connection *nc) {
14329
  struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
14330
  if (ctx == NULL) return;
14331
  nc->ssl_if_data = NULL;
14332
  MG_FREE(ctx->ssl_cert);
14333
  MG_FREE(ctx->ssl_key);
14334
  MG_FREE(ctx->ssl_ca_cert);
14335
  MG_FREE(ctx->ssl_server_name);
14336
  memset(ctx, 0, sizeof(*ctx));
14337
  MG_FREE(ctx);
14338
}
14339

    
14340
bool pem_to_der(const char *pem_file, const char *der_file) {
14341
  bool ret = false;
14342
  FILE *pf = NULL, *df = NULL;
14343
  bool writing = false;
14344
  pf = fopen(pem_file, "r");
14345
  if (pf == NULL) goto clean;
14346
  remove(der_file);
14347
  fs_slfs_set_new_file_size(der_file + MG_SSL_IF_SIMPLELINK_SLFS_PREFIX_LEN,
14348
                            2048);
14349
  df = fopen(der_file, "w");
14350
  if (df == NULL) goto clean;
14351
  while (1) {
14352
    char pem_buf[70];
14353
    char der_buf[48];
14354
    if (!fgets(pem_buf, sizeof(pem_buf), pf)) break;
14355
    if (writing) {
14356
      if (strstr(pem_buf, "-----END ") != NULL) {
14357
        ret = true;
14358
        break;
14359
      }
14360
      int l = 0;
14361
      while (!isspace((unsigned int) pem_buf[l])) l++;
14362
      int der_len = 0;
14363
      cs_base64_decode((const unsigned char *) pem_buf, sizeof(pem_buf),
14364
                       der_buf, &der_len);
14365
      if (der_len <= 0) break;
14366
      if (fwrite(der_buf, 1, der_len, df) != der_len) break;
14367
    } else if (strstr(pem_buf, "-----BEGIN ") != NULL) {
14368
      writing = true;
14369
    }
14370
  }
14371

    
14372
clean:
14373
  if (pf != NULL) fclose(pf);
14374
  if (df != NULL) {
14375
    fclose(df);
14376
    if (!ret) remove(der_file);
14377
  }
14378
  return ret;
14379
}
14380

    
14381
#if MG_ENABLE_FILESYSTEM && defined(MG_FS_SLFS)
14382
/* If the file's extension is .pem, convert it to DER format and put on SLFS. */
14383
static char *sl_pem2der(const char *pem_file) {
14384
  const char *pem_ext = strstr(pem_file, ".pem");
14385
  if (pem_ext == NULL || *(pem_ext + 4) != '\0') {
14386
    return strdup(pem_file);
14387
  }
14388
  char *der_file = NULL;
14389
  /* DER file must be located on SLFS, add prefix. */
14390
  int l = mg_asprintf(&der_file, 0, MG_SSL_IF_SIMPLELINK_SLFS_PREFIX "%.*s.der",
14391
                      (int) (pem_ext - pem_file), pem_file);
14392
  if (der_file == NULL) return NULL;
14393
  bool result = false;
14394
  cs_stat_t st;
14395
  if (mg_stat(der_file, &st) != 0) {
14396
    result = pem_to_der(pem_file, der_file);
14397
    LOG(LL_DEBUG, ("%s -> %s = %d", pem_file, der_file, result));
14398
  } else {
14399
    /* File exists, assume it's already been converted. */
14400
    result = true;
14401
  }
14402
  if (result) {
14403
    /* Strip the SL: prefix we added since NWP does not expect it. */
14404
    memmove(der_file, der_file + MG_SSL_IF_SIMPLELINK_SLFS_PREFIX_LEN,
14405
            l - 2 /* including \0 */);
14406
  } else {
14407
    MG_FREE(der_file);
14408
    der_file = NULL;
14409
  }
14410
  return der_file;
14411
}
14412
#else
14413
static char *sl_pem2der(const char *pem_file) {
14414
  return strdup(pem_file);
14415
}
14416
#endif
14417

    
14418
int sl_set_ssl_opts(int sock, struct mg_connection *nc) {
14419
  int err;
14420
  const struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data;
14421
  DBG(("%p ssl ctx: %p", nc, ctx));
14422

    
14423
  if (ctx != NULL) {
14424
    DBG(("%p %s,%s,%s,%s", nc, (ctx->ssl_cert ? ctx->ssl_cert : "-"),
14425
         (ctx->ssl_key ? ctx->ssl_cert : "-"),
14426
         (ctx->ssl_ca_cert ? ctx->ssl_ca_cert : "-"),
14427
         (ctx->ssl_server_name ? ctx->ssl_server_name : "-")));
14428
    if (ctx->ssl_cert != NULL && ctx->ssl_key != NULL) {
14429
      char *ssl_cert = sl_pem2der(ctx->ssl_cert);
14430
      char *ssl_key = sl_pem2der(ctx->ssl_key);
14431
      if (ssl_cert != NULL && ssl_key != NULL) {
14432
        err = sl_SetSockOpt(sock, SL_SOL_SOCKET,
14433
                            SL_SO_SECURE_FILES_CERTIFICATE_FILE_NAME, ssl_cert,
14434
                            strlen(ssl_cert));
14435
        LOG(LL_INFO, ("CERTIFICATE_FILE_NAME %s -> %d", ssl_cert, err));
14436
        err = sl_SetSockOpt(sock, SL_SOL_SOCKET,
14437
                            SL_SO_SECURE_FILES_PRIVATE_KEY_FILE_NAME, ssl_key,
14438
                            strlen(ssl_key));
14439
        LOG(LL_INFO, ("PRIVATE_KEY_FILE_NAME %s -> %d", ssl_key, err));
14440
      } else {
14441
        err = -1;
14442
      }
14443
      MG_FREE(ssl_cert);
14444
      MG_FREE(ssl_key);
14445
      if (err != 0) return err;
14446
    }
14447
    if (ctx->ssl_ca_cert != NULL) {
14448
      if (ctx->ssl_ca_cert[0] != '\0') {
14449
        char *ssl_ca_cert = sl_pem2der(ctx->ssl_ca_cert);
14450
        if (ssl_ca_cert != NULL) {
14451
          err = sl_SetSockOpt(sock, SL_SOL_SOCKET,
14452
                              SL_SO_SECURE_FILES_CA_FILE_NAME, ssl_ca_cert,
14453
                              strlen(ssl_ca_cert));
14454
          LOG(LL_INFO, ("CA_FILE_NAME %s -> %d", ssl_ca_cert, err));
14455
        } else {
14456
          err = -1;
14457
        }
14458
        MG_FREE(ssl_ca_cert);
14459
        if (err != 0) return err;
14460
      }
14461
    }
14462
    if (ctx->ssl_server_name != NULL) {
14463
      err = sl_SetSockOpt(sock, SL_SOL_SOCKET,
14464
                          SL_SO_SECURE_DOMAIN_NAME_VERIFICATION,
14465
                          ctx->ssl_server_name, strlen(ctx->ssl_server_name));
14466
      DBG(("DOMAIN_NAME_VERIFICATION %s -> %d", ctx->ssl_server_name, err));
14467
      /* Domain name verificationw as added in a NWP service pack, older
14468
       * versions return SL_ERROR_BSD_ENOPROTOOPT. There isn't much we can do
14469
       * about it,
14470
       * so we ignore the error. */
14471
      if (err != 0 && err != SL_ERROR_BSD_ENOPROTOOPT) return err;
14472
    }
14473
  }
14474
  return 0;
14475
}
14476

    
14477
#endif /* MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_SIMPLELINK */
14478
#ifdef MG_MODULE_LINES
14479
#line 1 "common/platforms/lwip/mg_lwip_net_if.h"
14480
#endif
14481
/*
14482
 * Copyright (c) 2014-2016 Cesanta Software Limited
14483
 * All rights reserved
14484
 */
14485

    
14486
#ifndef CS_COMMON_PLATFORMS_LWIP_MG_NET_IF_LWIP_H_
14487
#define CS_COMMON_PLATFORMS_LWIP_MG_NET_IF_LWIP_H_
14488

    
14489
#ifndef MG_ENABLE_NET_IF_LWIP_LOW_LEVEL
14490
#define MG_ENABLE_NET_IF_LWIP_LOW_LEVEL MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL
14491
#endif
14492

    
14493
#if MG_ENABLE_NET_IF_LWIP_LOW_LEVEL
14494

    
14495
#include <stdint.h>
14496

    
14497
extern const struct mg_iface_vtable mg_lwip_iface_vtable;
14498

    
14499
struct mg_lwip_conn_state {
14500
  struct mg_connection *nc;
14501
  struct mg_connection *lc;
14502
  union {
14503
    struct tcp_pcb *tcp;
14504
    struct udp_pcb *udp;
14505
  } pcb;
14506
  err_t err;
14507
  size_t num_sent; /* Number of acknowledged bytes to be reported to the core */
14508
  struct pbuf *rx_chain; /* Chain of incoming data segments. */
14509
  size_t rx_offset; /* Offset within the first pbuf (if partially consumed) */
14510
  /* Last SSL write size, for retries. */
14511
  int last_ssl_write_size;
14512
  /* Whether MG_SIG_RECV is already pending for this connection */
14513
  int recv_pending : 1;
14514
  /* Whether the connection is about to close, just `rx_chain` needs to drain */
14515
  int draining_rx_chain : 1;
14516
};
14517

    
14518
enum mg_sig_type {
14519
  MG_SIG_CONNECT_RESULT = 1,
14520
  MG_SIG_RECV = 2,
14521
  MG_SIG_CLOSE_CONN = 3,
14522
  MG_SIG_TOMBSTONE = 4,
14523
  MG_SIG_ACCEPT = 5,
14524
};
14525

    
14526
void mg_lwip_post_signal(enum mg_sig_type sig, struct mg_connection *nc);
14527

    
14528
/* To be implemented by the platform. */
14529
void mg_lwip_mgr_schedule_poll(struct mg_mgr *mgr);
14530

    
14531
#endif /* MG_ENABLE_NET_IF_LWIP_LOW_LEVEL */
14532

    
14533
#endif /* CS_COMMON_PLATFORMS_LWIP_MG_NET_IF_LWIP_H_ */
14534
#ifdef MG_MODULE_LINES
14535
#line 1 "common/platforms/lwip/mg_lwip_net_if.c"
14536
#endif
14537
/*
14538
 * Copyright (c) 2014-2016 Cesanta Software Limited
14539
 * All rights reserved
14540
 */
14541

    
14542
#if MG_ENABLE_NET_IF_LWIP_LOW_LEVEL
14543

    
14544
/* Amalgamated: #include "common/mg_mem.h" */
14545

    
14546
#include <lwip/init.h>
14547
#include <lwip/pbuf.h>
14548
#include <lwip/tcp.h>
14549
#include <lwip/tcpip.h>
14550
#if ((LWIP_VERSION_MAJOR << 8) | LWIP_VERSION_MINOR) >= 0x0105
14551
#include <lwip/priv/tcp_priv.h> /* For tcp_seg */
14552
#else
14553
#include <lwip/tcp_impl.h>
14554
#endif
14555
#include <lwip/udp.h>
14556

    
14557
/* Amalgamated: #include "common/cs_dbg.h" */
14558

    
14559
/*
14560
 * Newest versions of LWIP have ip_2_ip4, older have ipX_2_ip,
14561
 * even older have nothing.
14562
 */
14563
#ifndef ip_2_ip4
14564
#ifdef ipX_2_ip
14565
#define ip_2_ip4(addr) ipX_2_ip(addr)
14566
#else
14567
#define ip_2_ip4(addr) (addr)
14568
#endif
14569
#endif
14570

    
14571
/*
14572
 * Depending on whether Mongoose is compiled with ipv6 support, use right
14573
 * lwip functions
14574
 */
14575
#if MG_ENABLE_IPV6
14576
#define TCP_NEW tcp_new_ip6
14577
#define TCP_BIND tcp_bind_ip6
14578
#define UDP_BIND udp_bind_ip6
14579
#define IPADDR_NTOA(x) ip6addr_ntoa((const ip6_addr_t *)(x))
14580
#define SET_ADDR(dst, src)                               \
14581
  memcpy((dst)->sin6.sin6_addr.s6_addr, (src)->ip6.addr, \
14582
         sizeof((dst)->sin6.sin6_addr.s6_addr))
14583
#else
14584
#define TCP_NEW tcp_new
14585
#define TCP_BIND tcp_bind
14586
#define UDP_BIND udp_bind
14587
#define IPADDR_NTOA ipaddr_ntoa
14588
#define SET_ADDR(dst, src) (dst)->sin.sin_addr.s_addr = ip_2_ip4(src)->addr
14589
#endif
14590

    
14591
#if NO_SYS
14592
#define tcpip_callback(fn, arg) (fn)(arg)
14593
typedef void (*tcpip_callback_fn)(void *arg);
14594
#endif
14595

    
14596
void mg_lwip_ssl_do_hs(struct mg_connection *nc);
14597
void mg_lwip_ssl_send(struct mg_connection *nc);
14598
void mg_lwip_ssl_recv(struct mg_connection *nc);
14599

    
14600
void mg_lwip_if_init(struct mg_iface *iface);
14601
void mg_lwip_if_free(struct mg_iface *iface);
14602
void mg_lwip_if_add_conn(struct mg_connection *nc);
14603
void mg_lwip_if_remove_conn(struct mg_connection *nc);
14604
time_t mg_lwip_if_poll(struct mg_iface *iface, int timeout_ms);
14605

    
14606
#if defined(RTOS_SDK) || defined(ESP_PLATFORM)
14607
extern void mgos_lock();
14608
extern void mgos_unlock();
14609
#else
14610
#define mgos_lock()
14611
#define mgos_unlock()
14612
#endif
14613

    
14614
static void mg_lwip_recv_common(struct mg_connection *nc, struct pbuf *p);
14615

    
14616
#if LWIP_TCP_KEEPALIVE
14617
void mg_lwip_set_keepalive_params(struct mg_connection *nc, int idle,
14618
                                  int interval, int count) {
14619
  if (nc->sock == INVALID_SOCKET || nc->flags & MG_F_UDP) {
14620
    return;
14621
  }
14622
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
14623
  struct tcp_pcb *tpcb = cs->pcb.tcp;
14624
  if (idle > 0 && interval > 0 && count > 0) {
14625
    tpcb->keep_idle = idle * 1000;
14626
    tpcb->keep_intvl = interval * 1000;
14627
    tpcb->keep_cnt = count;
14628
    tpcb->so_options |= SOF_KEEPALIVE;
14629
  } else {
14630
    tpcb->so_options &= ~SOF_KEEPALIVE;
14631
  }
14632
}
14633
#elif !defined(MG_NO_LWIP_TCP_KEEPALIVE)
14634
#warning LWIP TCP keepalive is disabled. Please consider enabling it.
14635
#endif /* LWIP_TCP_KEEPALIVE */
14636

    
14637
static err_t mg_lwip_tcp_conn_cb(void *arg, struct tcp_pcb *tpcb, err_t err) {
14638
  struct mg_connection *nc = (struct mg_connection *) arg;
14639
  DBG(("%p connect to %s:%u = %d", nc, IPADDR_NTOA(ipX_2_ip(&tpcb->remote_ip)),
14640
       tpcb->remote_port, err));
14641
  if (nc == NULL) {
14642
    tcp_abort(tpcb);
14643
    return ERR_ARG;
14644
  }
14645
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
14646
  cs->err = err;
14647
#if LWIP_TCP_KEEPALIVE
14648
  if (err == 0) mg_lwip_set_keepalive_params(nc, 60, 10, 6);
14649
#endif
14650
  mg_lwip_post_signal(MG_SIG_CONNECT_RESULT, nc);
14651
  return ERR_OK;
14652
}
14653

    
14654
static void mg_lwip_tcp_error_cb(void *arg, err_t err) {
14655
  struct mg_connection *nc = (struct mg_connection *) arg;
14656
  DBG(("%p conn error %d", nc, err));
14657
  if (nc == NULL) return;
14658
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
14659
  cs->pcb.tcp = NULL; /* Has already been deallocated */
14660
  if (nc->flags & MG_F_CONNECTING) {
14661
    cs->err = err;
14662
    mg_lwip_post_signal(MG_SIG_CONNECT_RESULT, nc);
14663
  } else {
14664
    mg_lwip_post_signal(MG_SIG_CLOSE_CONN, nc);
14665
  }
14666
}
14667

    
14668
static err_t mg_lwip_tcp_recv_cb(void *arg, struct tcp_pcb *tpcb,
14669
                                 struct pbuf *p, err_t err) {
14670
  struct mg_connection *nc = (struct mg_connection *) arg;
14671
  DBG(("%p %p %u %d", nc, tpcb, (p != NULL ? p->tot_len : 0), err));
14672
  if (p == NULL) {
14673
    if (nc != NULL && !(nc->flags & MG_F_CLOSE_IMMEDIATELY)) {
14674
      struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
14675
      if (cs->rx_chain != NULL) {
14676
        /*
14677
         * rx_chain still contains non-consumed data, don't close the
14678
         * connection
14679
         */
14680
        cs->draining_rx_chain = 1;
14681
      } else {
14682
        mg_lwip_post_signal(MG_SIG_CLOSE_CONN, nc);
14683
      }
14684
    } else {
14685
      /* Tombstoned connection, do nothing. */
14686
    }
14687
    return ERR_OK;
14688
  } else if (nc == NULL) {
14689
    tcp_abort(tpcb);
14690
    return ERR_ARG;
14691
  }
14692
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
14693
  /*
14694
   * If we get a chain of more than one segment at once, we need to bump
14695
   * refcount on the subsequent bufs to make them independent.
14696
   */
14697
  if (p->next != NULL) {
14698
    struct pbuf *q = p->next;
14699
    for (; q != NULL; q = q->next) pbuf_ref(q);
14700
  }
14701
  mgos_lock();
14702
  if (cs->rx_chain == NULL) {
14703
    cs->rx_offset = 0;
14704
  } else if (pbuf_clen(cs->rx_chain) >= 4) {
14705
    /* ESP SDK has a limited pool of 5 pbufs. We must not hog them all or RX
14706
     * will be completely blocked. We already have at least 4 in the chain,
14707
     * this one is, so we have to make a copy and release this one. */
14708
    struct pbuf *np = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
14709
    if (np != NULL) {
14710
      pbuf_copy(np, p);
14711
      pbuf_free(p);
14712
      p = np;
14713
    }
14714
  }
14715
  mgos_unlock();
14716
  mg_lwip_recv_common(nc, p);
14717
  return ERR_OK;
14718
}
14719

    
14720
static void mg_lwip_consume_rx_chain_tcp(struct mg_connection *nc) {
14721
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
14722
  if (cs->rx_chain == NULL) return;
14723
#if MG_ENABLE_SSL
14724
  if (nc->flags & MG_F_SSL) {
14725
    if (nc->flags & MG_F_SSL_HANDSHAKE_DONE) {
14726
      mg_lwip_ssl_recv(nc);
14727
    } else {
14728
      mg_lwip_ssl_do_hs(nc);
14729
    }
14730
    return;
14731
  }
14732
#endif
14733
  mgos_lock();
14734
  while (cs->rx_chain != NULL && nc->recv_mbuf.len < nc->recv_mbuf_limit) {
14735
    struct pbuf *seg = cs->rx_chain;
14736

    
14737
    size_t seg_len = (seg->len - cs->rx_offset);
14738
    size_t buf_avail = (nc->recv_mbuf_limit - nc->recv_mbuf.len);
14739
    size_t len = MIN(seg_len, buf_avail);
14740

    
14741
    char *data = (char *) MG_MALLOC(len);
14742
    if (data == NULL) {
14743
      mgos_unlock();
14744
      DBG(("OOM"));
14745
      return;
14746
    }
14747
    pbuf_copy_partial(seg, data, len, cs->rx_offset);
14748
    cs->rx_offset += len;
14749
    if (cs->rx_offset == cs->rx_chain->len) {
14750
      cs->rx_chain = pbuf_dechain(cs->rx_chain);
14751
      pbuf_free(seg);
14752
      cs->rx_offset = 0;
14753
    }
14754
    mgos_unlock();
14755
    mg_if_recv_tcp_cb(nc, data, len, 1 /* own */);
14756
    mgos_lock();
14757
  }
14758
  mgos_unlock();
14759
}
14760

    
14761
static void mg_lwip_handle_recv_tcp(struct mg_connection *nc) {
14762
  mg_lwip_consume_rx_chain_tcp(nc);
14763

    
14764
  if (nc->send_mbuf.len > 0) {
14765
    mg_lwip_mgr_schedule_poll(nc->mgr);
14766
  }
14767
}
14768

    
14769
static err_t mg_lwip_tcp_sent_cb(void *arg, struct tcp_pcb *tpcb,
14770
                                 u16_t num_sent) {
14771
  struct mg_connection *nc = (struct mg_connection *) arg;
14772
  DBG(("%p %p %u %p %p", nc, tpcb, num_sent, tpcb->unsent, tpcb->unacked));
14773
  if (nc == NULL) return ERR_OK;
14774
  if ((nc->flags & MG_F_SEND_AND_CLOSE) && !(nc->flags & MG_F_WANT_WRITE) &&
14775
      nc->send_mbuf.len == 0 && tpcb->unsent == NULL && tpcb->unacked == NULL) {
14776
    mg_lwip_post_signal(MG_SIG_CLOSE_CONN, nc);
14777
  }
14778
  return ERR_OK;
14779
}
14780

    
14781
struct mg_lwip_if_connect_tcp_ctx {
14782
  struct mg_connection *nc;
14783
  const union socket_address *sa;
14784
};
14785

    
14786
static void mg_lwip_if_connect_tcp_tcpip(void *arg) {
14787
  struct mg_lwip_if_connect_tcp_ctx *ctx =
14788
      (struct mg_lwip_if_connect_tcp_ctx *) arg;
14789
  struct mg_connection *nc = ctx->nc;
14790
  const union socket_address *sa = ctx->sa;
14791

    
14792
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
14793
  struct tcp_pcb *tpcb = TCP_NEW();
14794
  cs->pcb.tcp = tpcb;
14795
  ip_addr_t *ip = (ip_addr_t *) &sa->sin.sin_addr.s_addr;
14796
  u16_t port = ntohs(sa->sin.sin_port);
14797
  tcp_arg(tpcb, nc);
14798
  tcp_err(tpcb, mg_lwip_tcp_error_cb);
14799
  tcp_sent(tpcb, mg_lwip_tcp_sent_cb);
14800
  tcp_recv(tpcb, mg_lwip_tcp_recv_cb);
14801
  cs->err = TCP_BIND(tpcb, IP_ADDR_ANY, 0 /* any port */);
14802
  DBG(("%p tcp_bind = %d", nc, cs->err));
14803
  if (cs->err != ERR_OK) {
14804
    mg_lwip_post_signal(MG_SIG_CONNECT_RESULT, nc);
14805
    return;
14806
  }
14807
  cs->err = tcp_connect(tpcb, ip, port, mg_lwip_tcp_conn_cb);
14808
  DBG(("%p tcp_connect %p = %d", nc, tpcb, cs->err));
14809
  if (cs->err != ERR_OK) {
14810
    mg_lwip_post_signal(MG_SIG_CONNECT_RESULT, nc);
14811
    return;
14812
  }
14813
}
14814

    
14815
void mg_lwip_if_connect_tcp(struct mg_connection *nc,
14816
                            const union socket_address *sa) {
14817
  struct mg_lwip_if_connect_tcp_ctx ctx = {.nc = nc, .sa = sa};
14818
  tcpip_callback(mg_lwip_if_connect_tcp_tcpip, &ctx);
14819
}
14820

    
14821
/*
14822
 * Lwip included in the SDKs for nRF5x chips has different type for the
14823
 * callback of `udp_recv()`
14824
 */
14825
#if ((LWIP_VERSION_MAJOR << 8) | LWIP_VERSION_MINOR) >= 0x0105
14826
static void mg_lwip_udp_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p,
14827
                                const ip_addr_t *addr, u16_t port)
14828
#else
14829
static void mg_lwip_udp_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p,
14830
                                ip_addr_t *addr, u16_t port)
14831
#endif
14832
{
14833
  struct mg_connection *nc = (struct mg_connection *) arg;
14834
  DBG(("%p %s:%u %p %u %u", nc, IPADDR_NTOA(addr), port, p, p->ref, p->len));
14835
  /* Put address in a separate pbuf and tack it onto the packet. */
14836
  struct pbuf *sap =
14837
      pbuf_alloc(PBUF_RAW, sizeof(union socket_address), PBUF_RAM);
14838
  if (sap == NULL) {
14839
    pbuf_free(p);
14840
    return;
14841
  }
14842
  union socket_address *sa = (union socket_address *) sap->payload;
14843
#if ((LWIP_VERSION_MAJOR << 8) | LWIP_VERSION_MINOR) >= 0x0105
14844
  sa->sin.sin_addr.s_addr = ip_2_ip4(addr)->addr;
14845
#else
14846
  sa->sin.sin_addr.s_addr = addr->addr;
14847
#endif
14848
  sa->sin.sin_port = htons(port);
14849
  /* Logic in the recv handler requires that there be exactly one data pbuf. */
14850
  p = pbuf_coalesce(p, PBUF_RAW);
14851
  pbuf_chain(sap, p);
14852
  mg_lwip_recv_common(nc, sap);
14853
  (void) pcb;
14854
}
14855

    
14856
static void mg_lwip_recv_common(struct mg_connection *nc, struct pbuf *p) {
14857
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
14858
  mgos_lock();
14859
  if (cs->rx_chain == NULL) {
14860
    cs->rx_chain = p;
14861
  } else {
14862
    pbuf_chain(cs->rx_chain, p);
14863
  }
14864
  if (!cs->recv_pending) {
14865
    cs->recv_pending = 1;
14866
    mg_lwip_post_signal(MG_SIG_RECV, nc);
14867
  }
14868
  mgos_unlock();
14869
}
14870

    
14871
static void mg_lwip_handle_recv_udp(struct mg_connection *nc) {
14872
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
14873
  /*
14874
   * For UDP, RX chain consists of interleaved address and packet bufs:
14875
   * Address pbuf followed by exactly one data pbuf (recv_cb took care of that).
14876
   */
14877
  while (cs->rx_chain != NULL) {
14878
    struct pbuf *sap = cs->rx_chain;
14879
    struct pbuf *p = sap->next;
14880
    cs->rx_chain = pbuf_dechain(p);
14881
    size_t data_len = p->len;
14882
    char *data = (char *) MG_MALLOC(data_len);
14883
    if (data != NULL) {
14884
      pbuf_copy_partial(p, data, data_len, 0);
14885
      pbuf_free(p);
14886
      mg_if_recv_udp_cb(nc, data, data_len,
14887
                        (union socket_address *) sap->payload, sap->len);
14888
      pbuf_free(sap);
14889
    } else {
14890
      pbuf_free(p);
14891
      pbuf_free(sap);
14892
    }
14893
  }
14894
}
14895

    
14896
static void mg_lwip_if_connect_udp_tcpip(void *arg) {
14897
  struct mg_connection *nc = (struct mg_connection *) arg;
14898
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
14899
  struct udp_pcb *upcb = udp_new();
14900
  cs->err = UDP_BIND(upcb, IP_ADDR_ANY, 0 /* any port */);
14901
  DBG(("%p udp_bind %p = %d", nc, upcb, cs->err));
14902
  if (cs->err == ERR_OK) {
14903
    udp_recv(upcb, mg_lwip_udp_recv_cb, nc);
14904
    cs->pcb.udp = upcb;
14905
  } else {
14906
    udp_remove(upcb);
14907
  }
14908
  mg_lwip_post_signal(MG_SIG_CONNECT_RESULT, nc);
14909
}
14910

    
14911
void mg_lwip_if_connect_udp(struct mg_connection *nc) {
14912
  tcpip_callback(mg_lwip_if_connect_udp_tcpip, nc);
14913
}
14914

    
14915
void mg_lwip_accept_conn(struct mg_connection *nc, struct tcp_pcb *tpcb) {
14916
  union socket_address sa;
14917
  SET_ADDR(&sa, &tpcb->remote_ip);
14918
  sa.sin.sin_port = htons(tpcb->remote_port);
14919
  mg_if_accept_tcp_cb(nc, &sa, sizeof(sa.sin));
14920
}
14921

    
14922
static void tcp_close_tcpip(void *arg) {
14923
  tcp_close((struct tcp_pcb *) arg);
14924
}
14925

    
14926
void mg_lwip_handle_accept(struct mg_connection *nc) {
14927
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
14928
  if (cs->pcb.tcp == NULL) return;
14929
#if MG_ENABLE_SSL
14930
  if (cs->lc->flags & MG_F_SSL) {
14931
    if (mg_ssl_if_conn_accept(nc, cs->lc) != MG_SSL_OK) {
14932
      LOG(LL_ERROR, ("SSL error"));
14933
      tcpip_callback(tcp_close_tcpip, cs->pcb.tcp);
14934
    }
14935
  } else
14936
#endif
14937
  {
14938
    mg_lwip_accept_conn(nc, cs->pcb.tcp);
14939
  }
14940
}
14941

    
14942
static err_t mg_lwip_accept_cb(void *arg, struct tcp_pcb *newtpcb, err_t err) {
14943
  struct mg_connection *lc = (struct mg_connection *) arg, *nc;
14944
  struct mg_lwip_conn_state *lcs, *cs;
14945
  struct tcp_pcb_listen *lpcb;
14946
  LOG(LL_DEBUG,
14947
      ("%p conn %p from %s:%u", lc, newtpcb,
14948
       IPADDR_NTOA(ipX_2_ip(&newtpcb->remote_ip)), newtpcb->remote_port));
14949
  if (lc == NULL) {
14950
    tcp_abort(newtpcb);
14951
    return ERR_ABRT;
14952
  }
14953
  lcs = (struct mg_lwip_conn_state *) lc->sock;
14954
  lpcb = (struct tcp_pcb_listen *) lcs->pcb.tcp;
14955
#if TCP_LISTEN_BACKLOG
14956
  tcp_accepted(lpcb);
14957
#endif
14958
  nc = mg_if_accept_new_conn(lc);
14959
  if (nc == NULL) {
14960
    tcp_abort(newtpcb);
14961
    return ERR_ABRT;
14962
  }
14963
  cs = (struct mg_lwip_conn_state *) nc->sock;
14964
  cs->lc = lc;
14965
  cs->pcb.tcp = newtpcb;
14966
  /* We need to set up callbacks before returning because data may start
14967
   * arriving immediately. */
14968
  tcp_arg(newtpcb, nc);
14969
  tcp_err(newtpcb, mg_lwip_tcp_error_cb);
14970
  tcp_sent(newtpcb, mg_lwip_tcp_sent_cb);
14971
  tcp_recv(newtpcb, mg_lwip_tcp_recv_cb);
14972
#if LWIP_TCP_KEEPALIVE
14973
  mg_lwip_set_keepalive_params(nc, 60, 10, 6);
14974
#endif
14975
  mg_lwip_post_signal(MG_SIG_ACCEPT, nc);
14976
  (void) err;
14977
  (void) lpcb;
14978
  return ERR_OK;
14979
}
14980

    
14981
struct mg_lwip_if_listen_ctx {
14982
  struct mg_connection *nc;
14983
  union socket_address *sa;
14984
  int ret;
14985
};
14986

    
14987
static void mg_lwip_if_listen_tcp_tcpip(void *arg) {
14988
  struct mg_lwip_if_listen_ctx *ctx = (struct mg_lwip_if_listen_ctx *) arg;
14989
  struct mg_connection *nc = ctx->nc;
14990
  union socket_address *sa = ctx->sa;
14991
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
14992
  struct tcp_pcb *tpcb = TCP_NEW();
14993
  ip_addr_t *ip = (ip_addr_t *) &sa->sin.sin_addr.s_addr;
14994
  u16_t port = ntohs(sa->sin.sin_port);
14995
  cs->err = TCP_BIND(tpcb, ip, port);
14996
  DBG(("%p tcp_bind(%s:%u) = %d", nc, IPADDR_NTOA(ip), port, cs->err));
14997
  if (cs->err != ERR_OK) {
14998
    tcp_close(tpcb);
14999
    ctx->ret = -1;
15000
    return;
15001
  }
15002
  tcp_arg(tpcb, nc);
15003
  tpcb = tcp_listen(tpcb);
15004
  cs->pcb.tcp = tpcb;
15005
  tcp_accept(tpcb, mg_lwip_accept_cb);
15006
  ctx->ret = 0;
15007
}
15008

    
15009
int mg_lwip_if_listen_tcp(struct mg_connection *nc, union socket_address *sa) {
15010
  struct mg_lwip_if_listen_ctx ctx = {.nc = nc, .sa = sa};
15011
  tcpip_callback(mg_lwip_if_listen_tcp_tcpip, &ctx);
15012
  return ctx.ret;
15013
}
15014

    
15015
static void mg_lwip_if_listen_udp_tcpip(void *arg) {
15016
  struct mg_lwip_if_listen_ctx *ctx = (struct mg_lwip_if_listen_ctx *) arg;
15017
  struct mg_connection *nc = ctx->nc;
15018
  union socket_address *sa = ctx->sa;
15019
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15020
  struct udp_pcb *upcb = udp_new();
15021
  ip_addr_t *ip = (ip_addr_t *) &sa->sin.sin_addr.s_addr;
15022
  u16_t port = ntohs(sa->sin.sin_port);
15023
  cs->err = UDP_BIND(upcb, ip, port);
15024
  DBG(("%p udb_bind(%s:%u) = %d", nc, IPADDR_NTOA(ip), port, cs->err));
15025
  if (cs->err != ERR_OK) {
15026
    udp_remove(upcb);
15027
    ctx->ret = -1;
15028
  } else {
15029
    udp_recv(upcb, mg_lwip_udp_recv_cb, nc);
15030
    cs->pcb.udp = upcb;
15031
    ctx->ret = 0;
15032
  }
15033
}
15034

    
15035
int mg_lwip_if_listen_udp(struct mg_connection *nc, union socket_address *sa) {
15036
  struct mg_lwip_if_listen_ctx ctx = {.nc = nc, .sa = sa};
15037
  tcpip_callback(mg_lwip_if_listen_udp_tcpip, &ctx);
15038
  return ctx.ret;
15039
}
15040

    
15041
struct mg_lwip_tcp_write_ctx {
15042
  struct mg_connection *nc;
15043
  const void *data;
15044
  uint16_t len;
15045
  int ret;
15046
};
15047

    
15048
static void tcp_output_tcpip(void *arg) {
15049
  tcp_output((struct tcp_pcb *) arg);
15050
}
15051

    
15052
static void mg_lwip_tcp_write_tcpip(void *arg) {
15053
  struct mg_lwip_tcp_write_ctx *ctx = (struct mg_lwip_tcp_write_ctx *) arg;
15054
  struct mg_connection *nc = ctx->nc;
15055
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15056
  struct tcp_pcb *tpcb = cs->pcb.tcp;
15057
  size_t len = MIN(tpcb->mss, MIN(ctx->len, tpcb->snd_buf));
15058
  size_t unsent, unacked;
15059
  if (len == 0) {
15060
    DBG(("%p no buf avail %u %u %p %p", tpcb, tpcb->snd_buf, tpcb->snd_queuelen,
15061
         tpcb->unsent, tpcb->unacked));
15062
    tcpip_callback(tcp_output_tcpip, tpcb);
15063
    ctx->ret = 0;
15064
    return;
15065
  }
15066
  unsent = (tpcb->unsent != NULL ? tpcb->unsent->len : 0);
15067
  unacked = (tpcb->unacked != NULL ? tpcb->unacked->len : 0);
15068
/*
15069
 * On ESP8266 we only allow one TCP segment in flight at any given time.
15070
 * This may increase latency and reduce efficiency of tcp windowing,
15071
 * but memory is scarce and precious on that platform so we do this to
15072
 * reduce footprint.
15073
 */
15074
#if CS_PLATFORM == CS_P_ESP8266
15075
  if (unacked > 0) {
15076
    ctx->ret = 0;
15077
    return;
15078
  }
15079
  len = MIN(len, (TCP_MSS - unsent));
15080
#endif
15081
  cs->err = tcp_write(tpcb, ctx->data, len, TCP_WRITE_FLAG_COPY);
15082
  unsent = (tpcb->unsent != NULL ? tpcb->unsent->len : 0);
15083
  unacked = (tpcb->unacked != NULL ? tpcb->unacked->len : 0);
15084
  DBG(("%p tcp_write %u = %d, %u %u", tpcb, len, cs->err, unsent, unacked));
15085
  if (cs->err != ERR_OK) {
15086
    /*
15087
     * We ignore ERR_MEM because memory will be freed up when the data is sent
15088
     * and we'll retry.
15089
     */
15090
    ctx->ret = (cs->err == ERR_MEM ? 0 : -1);
15091
    return;
15092
  }
15093
  ctx->ret = len;
15094
}
15095

    
15096
static int mg_lwip_tcp_write(struct mg_connection *nc, const void *data,
15097
                             uint16_t len) {
15098
  struct mg_lwip_tcp_write_ctx ctx = {.nc = nc, .data = data, .len = len};
15099
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15100
  struct tcp_pcb *tpcb = cs->pcb.tcp;
15101
  if (tpcb == NULL) {
15102
    return -1;
15103
  }
15104
  tcpip_callback(mg_lwip_tcp_write_tcpip, &ctx);
15105
  return ctx.ret;
15106
}
15107

    
15108
struct udp_sendto_ctx {
15109
  struct udp_pcb *upcb;
15110
  struct pbuf *p;
15111
  ip_addr_t *ip;
15112
  uint16_t port;
15113
  int ret;
15114
};
15115

    
15116
static void udp_sendto_tcpip(void *arg) {
15117
  struct udp_sendto_ctx *ctx = (struct udp_sendto_ctx *) arg;
15118
  ctx->ret = udp_sendto(ctx->upcb, ctx->p, ctx->ip, ctx->port);
15119
}
15120

    
15121
static int mg_lwip_udp_send(struct mg_connection *nc, const void *data,
15122
                            uint16_t len) {
15123
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15124
  if (cs->pcb.udp == NULL) {
15125
    /*
15126
     * In case of UDP, this usually means, what
15127
     * async DNS resolve is still in progress and connection
15128
     * is not ready yet
15129
     */
15130
    DBG(("%p socket is not connected", nc));
15131
    return -1;
15132
  }
15133
  struct udp_pcb *upcb = cs->pcb.udp;
15134
  struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_RAM);
15135
#if defined(LWIP_IPV4) && LWIP_IPV4 && defined(LWIP_IPV6) && LWIP_IPV6
15136
  ip_addr_t ip = {.u_addr.ip4.addr = nc->sa.sin.sin_addr.s_addr, .type = 0};
15137
#else
15138
  ip_addr_t ip = {.addr = nc->sa.sin.sin_addr.s_addr};
15139
#endif
15140
  u16_t port = ntohs(nc->sa.sin.sin_port);
15141
  if (p == NULL) {
15142
    DBG(("OOM"));
15143
    return 0;
15144
  }
15145
  memcpy(p->payload, data, len);
15146
  struct udp_sendto_ctx ctx = {.upcb = upcb, .p = p, .ip = &ip, .port = port};
15147
  tcpip_callback(udp_sendto_tcpip, &ctx);
15148
  cs->err = ctx.ret;
15149
  pbuf_free(p);
15150
  return (cs->err == ERR_OK ? len : -1);
15151
}
15152

    
15153
static void mg_lwip_send_more(struct mg_connection *nc) {
15154
  int num_sent = 0;
15155
  if (nc->sock == INVALID_SOCKET) return;
15156
  if (nc->flags & MG_F_UDP) {
15157
    num_sent = mg_lwip_udp_send(nc, nc->send_mbuf.buf, nc->send_mbuf.len);
15158
    DBG(("%p mg_lwip_udp_send %u = %d", nc, nc->send_mbuf.len, num_sent));
15159
  } else {
15160
    num_sent = mg_lwip_tcp_write(nc, nc->send_mbuf.buf, nc->send_mbuf.len);
15161
    DBG(("%p mg_lwip_tcp_write %u = %d", nc, nc->send_mbuf.len, num_sent));
15162
  }
15163
  if (num_sent == 0) return;
15164
  if (num_sent > 0) {
15165
    mg_if_sent_cb(nc, num_sent);
15166
  } else {
15167
    mg_lwip_post_signal(MG_SIG_CLOSE_CONN, nc);
15168
  }
15169
}
15170

    
15171
void mg_lwip_if_tcp_send(struct mg_connection *nc, const void *buf,
15172
                         size_t len) {
15173
  mbuf_append(&nc->send_mbuf, buf, len);
15174
  mg_lwip_mgr_schedule_poll(nc->mgr);
15175
}
15176

    
15177
void mg_lwip_if_udp_send(struct mg_connection *nc, const void *buf,
15178
                         size_t len) {
15179
  mbuf_append(&nc->send_mbuf, buf, len);
15180
  mg_lwip_mgr_schedule_poll(nc->mgr);
15181
}
15182

    
15183
struct tcp_recved_ctx {
15184
  struct tcp_pcb *tpcb;
15185
  size_t len;
15186
};
15187

    
15188
void tcp_recved_tcpip(void *arg) {
15189
  struct tcp_recved_ctx *ctx = (struct tcp_recved_ctx *) arg;
15190
  tcp_recved(ctx->tpcb, ctx->len);
15191
}
15192

    
15193
void mg_lwip_if_recved(struct mg_connection *nc, size_t len) {
15194
  if (nc->flags & MG_F_UDP) return;
15195
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15196
  if (nc->sock == INVALID_SOCKET || cs->pcb.tcp == NULL) {
15197
    DBG(("%p invalid socket", nc));
15198
    return;
15199
  }
15200
  DBG(("%p %p %u %u", nc, cs->pcb.tcp, len,
15201
       (cs->rx_chain ? cs->rx_chain->tot_len : 0)));
15202
  struct tcp_recved_ctx ctx = {.tpcb = cs->pcb.tcp, .len = len};
15203
#if MG_ENABLE_SSL
15204
  if (!(nc->flags & MG_F_SSL)) {
15205
    tcpip_callback(tcp_recved_tcpip, &ctx);
15206
  } else {
15207
    /* Currently SSL acknowledges data immediately.
15208
     * TODO(rojer): Find a way to propagate mg_lwip_if_recved. */
15209
  }
15210
#else
15211
  tcpip_callback(tcp_recved_tcpip, &ctx);
15212
#endif
15213
  mbuf_trim(&nc->recv_mbuf);
15214
}
15215

    
15216
int mg_lwip_if_create_conn(struct mg_connection *nc) {
15217
  struct mg_lwip_conn_state *cs =
15218
      (struct mg_lwip_conn_state *) MG_CALLOC(1, sizeof(*cs));
15219
  if (cs == NULL) return 0;
15220
  cs->nc = nc;
15221
  nc->sock = (intptr_t) cs;
15222
  return 1;
15223
}
15224

    
15225
static void udp_remove_tcpip(void *arg) {
15226
  udp_remove((struct udp_pcb *) arg);
15227
}
15228

    
15229
void mg_lwip_if_destroy_conn(struct mg_connection *nc) {
15230
  if (nc->sock == INVALID_SOCKET) return;
15231
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15232
  if (!(nc->flags & MG_F_UDP)) {
15233
    struct tcp_pcb *tpcb = cs->pcb.tcp;
15234
    if (tpcb != NULL) {
15235
      tcp_arg(tpcb, NULL);
15236
      DBG(("%p tcp_close %p", nc, tpcb));
15237
      tcp_arg(tpcb, NULL);
15238
      tcpip_callback(tcp_close_tcpip, tpcb);
15239
    }
15240
    while (cs->rx_chain != NULL) {
15241
      struct pbuf *seg = cs->rx_chain;
15242
      cs->rx_chain = pbuf_dechain(cs->rx_chain);
15243
      pbuf_free(seg);
15244
    }
15245
    memset(cs, 0, sizeof(*cs));
15246
    MG_FREE(cs);
15247
  } else if (nc->listener == NULL) {
15248
    /* Only close outgoing UDP pcb or listeners. */
15249
    struct udp_pcb *upcb = cs->pcb.udp;
15250
    if (upcb != NULL) {
15251
      DBG(("%p udp_remove %p", nc, upcb));
15252
      tcpip_callback(udp_remove_tcpip, upcb);
15253
    }
15254
    memset(cs, 0, sizeof(*cs));
15255
    MG_FREE(cs);
15256
  }
15257
  nc->sock = INVALID_SOCKET;
15258
}
15259

    
15260
void mg_lwip_if_get_conn_addr(struct mg_connection *nc, int remote,
15261
                              union socket_address *sa) {
15262
  memset(sa, 0, sizeof(*sa));
15263
  if (nc == NULL || nc->sock == INVALID_SOCKET) return;
15264
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15265
  if (nc->flags & MG_F_UDP) {
15266
    struct udp_pcb *upcb = cs->pcb.udp;
15267
    if (remote) {
15268
      memcpy(sa, &nc->sa, sizeof(*sa));
15269
    } else if (upcb != NULL) {
15270
      sa->sin.sin_port = htons(upcb->local_port);
15271
      SET_ADDR(sa, &upcb->local_ip);
15272
    }
15273
  } else {
15274
    struct tcp_pcb *tpcb = cs->pcb.tcp;
15275
    if (remote) {
15276
      memcpy(sa, &nc->sa, sizeof(*sa));
15277
    } else if (tpcb != NULL) {
15278
      sa->sin.sin_port = htons(tpcb->local_port);
15279
      SET_ADDR(sa, &tpcb->local_ip);
15280
    }
15281
  }
15282
}
15283

    
15284
void mg_lwip_if_sock_set(struct mg_connection *nc, sock_t sock) {
15285
  nc->sock = sock;
15286
}
15287

    
15288
/* clang-format off */
15289
#define MG_LWIP_IFACE_VTABLE                                          \
15290
  {                                                                   \
15291
    mg_lwip_if_init,                                                  \
15292
    mg_lwip_if_free,                                                  \
15293
    mg_lwip_if_add_conn,                                              \
15294
    mg_lwip_if_remove_conn,                                           \
15295
    mg_lwip_if_poll,                                                  \
15296
    mg_lwip_if_listen_tcp,                                            \
15297
    mg_lwip_if_listen_udp,                                            \
15298
    mg_lwip_if_connect_tcp,                                           \
15299
    mg_lwip_if_connect_udp,                                           \
15300
    mg_lwip_if_tcp_send,                                              \
15301
    mg_lwip_if_udp_send,                                              \
15302
    mg_lwip_if_recved,                                                \
15303
    mg_lwip_if_create_conn,                                           \
15304
    mg_lwip_if_destroy_conn,                                          \
15305
    mg_lwip_if_sock_set,                                              \
15306
    mg_lwip_if_get_conn_addr,                                         \
15307
  }
15308
/* clang-format on */
15309

    
15310
const struct mg_iface_vtable mg_lwip_iface_vtable = MG_LWIP_IFACE_VTABLE;
15311
#if MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL
15312
const struct mg_iface_vtable mg_default_iface_vtable = MG_LWIP_IFACE_VTABLE;
15313
#endif
15314

    
15315
#endif /* MG_ENABLE_NET_IF_LWIP_LOW_LEVEL */
15316
#ifdef MG_MODULE_LINES
15317
#line 1 "common/platforms/lwip/mg_lwip_ev_mgr.c"
15318
#endif
15319
/*
15320
 * Copyright (c) 2014-2016 Cesanta Software Limited
15321
 * All rights reserved
15322
 */
15323

    
15324
#if MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL
15325

    
15326
#ifndef MG_SIG_QUEUE_LEN
15327
#define MG_SIG_QUEUE_LEN 32
15328
#endif
15329

    
15330
struct mg_ev_mgr_lwip_signal {
15331
  int sig;
15332
  struct mg_connection *nc;
15333
};
15334

    
15335
struct mg_ev_mgr_lwip_data {
15336
  struct mg_ev_mgr_lwip_signal sig_queue[MG_SIG_QUEUE_LEN];
15337
  int sig_queue_len;
15338
  int start_index;
15339
};
15340

    
15341
void mg_lwip_post_signal(enum mg_sig_type sig, struct mg_connection *nc) {
15342
  struct mg_ev_mgr_lwip_data *md =
15343
      (struct mg_ev_mgr_lwip_data *) nc->iface->data;
15344
  mgos_lock();
15345
  if (md->sig_queue_len >= MG_SIG_QUEUE_LEN) {
15346
    mgos_unlock();
15347
    return;
15348
  }
15349
  int end_index = (md->start_index + md->sig_queue_len) % MG_SIG_QUEUE_LEN;
15350
  md->sig_queue[end_index].sig = sig;
15351
  md->sig_queue[end_index].nc = nc;
15352
  md->sig_queue_len++;
15353
  mg_lwip_mgr_schedule_poll(nc->mgr);
15354
  mgos_unlock();
15355
}
15356

    
15357
void mg_ev_mgr_lwip_process_signals(struct mg_mgr *mgr) {
15358
  struct mg_ev_mgr_lwip_data *md =
15359
      (struct mg_ev_mgr_lwip_data *) mgr->ifaces[MG_MAIN_IFACE]->data;
15360
  while (md->sig_queue_len > 0) {
15361
    mgos_lock();
15362
    int sig = md->sig_queue[md->start_index].sig;
15363
    struct mg_connection *nc = md->sig_queue[md->start_index].nc;
15364
    struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15365
    md->start_index = (md->start_index + 1) % MG_SIG_QUEUE_LEN;
15366
    md->sig_queue_len--;
15367
    mgos_unlock();
15368
    if (nc->iface == NULL || nc->mgr == NULL) continue;
15369
    switch (sig) {
15370
      case MG_SIG_CONNECT_RESULT: {
15371
#if MG_ENABLE_SSL
15372
        if (cs->err == 0 && (nc->flags & MG_F_SSL) &&
15373
            !(nc->flags & MG_F_SSL_HANDSHAKE_DONE)) {
15374
          mg_lwip_ssl_do_hs(nc);
15375
        } else
15376
#endif
15377
        {
15378
          mg_if_connect_cb(nc, cs->err);
15379
        }
15380
        break;
15381
      }
15382
      case MG_SIG_CLOSE_CONN: {
15383
        nc->flags |= MG_F_SEND_AND_CLOSE;
15384
        mg_close_conn(nc);
15385
        break;
15386
      }
15387
      case MG_SIG_RECV: {
15388
        cs->recv_pending = 0;
15389
        if (nc->flags & MG_F_UDP) {
15390
          mg_lwip_handle_recv_udp(nc);
15391
        } else {
15392
          mg_lwip_handle_recv_tcp(nc);
15393
        }
15394
        break;
15395
      }
15396
      case MG_SIG_TOMBSTONE: {
15397
        break;
15398
      }
15399
      case MG_SIG_ACCEPT: {
15400
        mg_lwip_handle_accept(nc);
15401
        break;
15402
      }
15403
    }
15404
  }
15405
}
15406

    
15407
void mg_lwip_if_init(struct mg_iface *iface) {
15408
  LOG(LL_INFO, ("%p Mongoose init", iface));
15409
  iface->data = MG_CALLOC(1, sizeof(struct mg_ev_mgr_lwip_data));
15410
}
15411

    
15412
void mg_lwip_if_free(struct mg_iface *iface) {
15413
  MG_FREE(iface->data);
15414
  iface->data = NULL;
15415
}
15416

    
15417
void mg_lwip_if_add_conn(struct mg_connection *nc) {
15418
  (void) nc;
15419
}
15420

    
15421
void mg_lwip_if_remove_conn(struct mg_connection *nc) {
15422
  struct mg_ev_mgr_lwip_data *md =
15423
      (struct mg_ev_mgr_lwip_data *) nc->iface->data;
15424
  /* Walk the queue and null-out further signals for this conn. */
15425
  for (int i = 0; i < MG_SIG_QUEUE_LEN; i++) {
15426
    if (md->sig_queue[i].nc == nc) {
15427
      md->sig_queue[i].sig = MG_SIG_TOMBSTONE;
15428
    }
15429
  }
15430
}
15431

    
15432
time_t mg_lwip_if_poll(struct mg_iface *iface, int timeout_ms) {
15433
  struct mg_mgr *mgr = iface->mgr;
15434
  int n = 0;
15435
  double now = mg_time();
15436
  struct mg_connection *nc, *tmp;
15437
  double min_timer = 0;
15438
  int num_timers = 0;
15439
#if 0
15440
  DBG(("begin poll @%u", (unsigned int) (now * 1000)));
15441
#endif
15442
  mg_ev_mgr_lwip_process_signals(mgr);
15443
  for (nc = mgr->active_connections; nc != NULL; nc = tmp) {
15444
    struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15445
    tmp = nc->next;
15446
    n++;
15447
    if ((nc->flags & MG_F_CLOSE_IMMEDIATELY) ||
15448
        ((nc->flags & MG_F_SEND_AND_CLOSE) && (nc->flags & MG_F_UDP) &&
15449
         (nc->send_mbuf.len == 0))) {
15450
      mg_close_conn(nc);
15451
      continue;
15452
    }
15453
    mg_if_poll(nc, now);
15454
    mg_if_timer(nc, now);
15455
#if MG_ENABLE_SSL
15456
    if ((nc->flags & MG_F_SSL) && cs != NULL && cs->pcb.tcp != NULL &&
15457
        cs->pcb.tcp->state == ESTABLISHED) {
15458
      if (((nc->flags & MG_F_WANT_WRITE) ||
15459
           ((nc->send_mbuf.len > 0) &&
15460
            (nc->flags & MG_F_SSL_HANDSHAKE_DONE))) &&
15461
          cs->pcb.tcp->snd_buf > 0) {
15462
        /* Can write more. */
15463
        if (nc->flags & MG_F_SSL_HANDSHAKE_DONE) {
15464
          if (!(nc->flags & MG_F_CONNECTING)) mg_lwip_ssl_send(nc);
15465
        } else {
15466
          mg_lwip_ssl_do_hs(nc);
15467
        }
15468
      }
15469
      if (cs->rx_chain != NULL || (nc->flags & MG_F_WANT_READ)) {
15470
        if (nc->flags & MG_F_SSL_HANDSHAKE_DONE) {
15471
          if (!(nc->flags & MG_F_CONNECTING)) mg_lwip_ssl_recv(nc);
15472
        } else {
15473
          mg_lwip_ssl_do_hs(nc);
15474
        }
15475
      }
15476
    } else
15477
#endif /* MG_ENABLE_SSL */
15478
    {
15479
      if (nc->send_mbuf.len > 0 && !(nc->flags & MG_F_CONNECTING)) {
15480
        mg_lwip_send_more(nc);
15481
      }
15482
    }
15483
    if (nc->sock != INVALID_SOCKET &&
15484
        !(nc->flags & (MG_F_UDP | MG_F_LISTENING)) && cs->pcb.tcp != NULL &&
15485
        cs->pcb.tcp->unsent != NULL) {
15486
      tcpip_callback(tcp_output_tcpip, cs->pcb.tcp);
15487
    }
15488
    if (nc->ev_timer_time > 0) {
15489
      if (num_timers == 0 || nc->ev_timer_time < min_timer) {
15490
        min_timer = nc->ev_timer_time;
15491
      }
15492
      num_timers++;
15493
    }
15494

    
15495
    if (nc->sock != INVALID_SOCKET) {
15496
      /* Try to consume data from cs->rx_chain */
15497
      mg_lwip_consume_rx_chain_tcp(nc);
15498

    
15499
      /*
15500
       * If the connection is about to close, and rx_chain is finally empty,
15501
       * send the MG_SIG_CLOSE_CONN signal
15502
       */
15503
      if (cs->draining_rx_chain && cs->rx_chain == NULL) {
15504
        mg_lwip_post_signal(MG_SIG_CLOSE_CONN, nc);
15505
      }
15506
    }
15507
  }
15508
#if 0
15509
  DBG(("end poll @%u, %d conns, %d timers (min %u), next in %d ms",
15510
       (unsigned int) (now * 1000), n, num_timers,
15511
       (unsigned int) (min_timer * 1000), timeout_ms));
15512
#endif
15513
  (void) timeout_ms;
15514
  return now;
15515
}
15516

    
15517
uint32_t mg_lwip_get_poll_delay_ms(struct mg_mgr *mgr) {
15518
  struct mg_connection *nc;
15519
  double now;
15520
  double min_timer = 0;
15521
  int num_timers = 0;
15522
  mg_ev_mgr_lwip_process_signals(mgr);
15523
  for (nc = mg_next(mgr, NULL); nc != NULL; nc = mg_next(mgr, nc)) {
15524
    struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15525
    if (nc->ev_timer_time > 0) {
15526
      if (num_timers == 0 || nc->ev_timer_time < min_timer) {
15527
        min_timer = nc->ev_timer_time;
15528
      }
15529
      num_timers++;
15530
    }
15531
    if (nc->send_mbuf.len > 0
15532
#if MG_ENABLE_SSL
15533
        || (nc->flags & MG_F_WANT_WRITE)
15534
#endif
15535
            ) {
15536
      int can_send = 0;
15537
      /* We have stuff to send, but can we? */
15538
      if (nc->flags & MG_F_UDP) {
15539
        /* UDP is always ready for sending. */
15540
        can_send = (cs->pcb.udp != NULL);
15541
      } else {
15542
        can_send = (cs->pcb.tcp != NULL && cs->pcb.tcp->snd_buf > 0);
15543
      }
15544
      /* We want and can send, request a poll immediately. */
15545
      if (can_send) return 0;
15546
    }
15547
  }
15548
  uint32_t timeout_ms = ~0;
15549
  now = mg_time();
15550
  if (num_timers > 0) {
15551
    /* If we have a timer that is past due, do a poll ASAP. */
15552
    if (min_timer < now) return 0;
15553
    double timer_timeout_ms = (min_timer - now) * 1000 + 1 /* rounding */;
15554
    if (timer_timeout_ms < timeout_ms) {
15555
      timeout_ms = timer_timeout_ms;
15556
    }
15557
  }
15558
  return timeout_ms;
15559
}
15560

    
15561
#endif /* MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL */
15562
#ifdef MG_MODULE_LINES
15563
#line 1 "common/platforms/lwip/mg_lwip_ssl_if.c"
15564
#endif
15565
/*
15566
 * Copyright (c) 2014-2016 Cesanta Software Limited
15567
 * All rights reserved
15568
 */
15569

    
15570
#if MG_ENABLE_SSL && MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL
15571

    
15572
/* Amalgamated: #include "common/mg_mem.h" */
15573
/* Amalgamated: #include "common/cs_dbg.h" */
15574

    
15575
#include <lwip/pbuf.h>
15576
#include <lwip/tcp.h>
15577

    
15578
#ifndef MG_LWIP_SSL_IO_SIZE
15579
#define MG_LWIP_SSL_IO_SIZE 1024
15580
#endif
15581

    
15582
#ifndef MIN
15583
#define MIN(a, b) ((a) < (b) ? (a) : (b))
15584
#endif
15585

    
15586
void mg_lwip_ssl_do_hs(struct mg_connection *nc) {
15587
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15588
  int server_side = (nc->listener != NULL);
15589
  enum mg_ssl_if_result res;
15590
  if (nc->flags & MG_F_CLOSE_IMMEDIATELY) return;
15591
  res = mg_ssl_if_handshake(nc);
15592
  DBG(("%p %lu %d %d", nc, nc->flags, server_side, res));
15593
  if (res != MG_SSL_OK) {
15594
    if (res == MG_SSL_WANT_WRITE) {
15595
      nc->flags |= MG_F_WANT_WRITE;
15596
      cs->err = 0;
15597
    } else if (res == MG_SSL_WANT_READ) {
15598
      /*
15599
       * Nothing to do in particular, we are callback-driven.
15600
       * What we definitely do not need anymore is SSL reading (nothing left).
15601
       */
15602
      nc->flags &= ~MG_F_WANT_READ;
15603
      cs->err = 0;
15604
    } else {
15605
      cs->err = res;
15606
      if (server_side) {
15607
        mg_lwip_post_signal(MG_SIG_CLOSE_CONN, nc);
15608
      } else {
15609
        mg_lwip_post_signal(MG_SIG_CONNECT_RESULT, nc);
15610
      }
15611
    }
15612
  } else {
15613
    cs->err = 0;
15614
    nc->flags &= ~MG_F_WANT_WRITE;
15615
    /*
15616
     * Handshake is done. Schedule a read immediately to consume app data
15617
     * which may already be waiting.
15618
     */
15619
    nc->flags |= (MG_F_SSL_HANDSHAKE_DONE | MG_F_WANT_READ);
15620
    if (server_side) {
15621
      mg_lwip_accept_conn(nc, cs->pcb.tcp);
15622
    } else {
15623
      mg_lwip_post_signal(MG_SIG_CONNECT_RESULT, nc);
15624
    }
15625
  }
15626
}
15627

    
15628
void mg_lwip_ssl_send(struct mg_connection *nc) {
15629
  if (nc->sock == INVALID_SOCKET) {
15630
    DBG(("%p invalid socket", nc));
15631
    return;
15632
  }
15633
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15634
  /* It's ok if the buffer is empty. Return value of 0 may also be valid. */
15635
  int len = cs->last_ssl_write_size;
15636
  if (len == 0) {
15637
    len = MIN(MG_LWIP_SSL_IO_SIZE, nc->send_mbuf.len);
15638
  }
15639
  int ret = mg_ssl_if_write(nc, nc->send_mbuf.buf, len);
15640
  DBG(("%p SSL_write %u = %d", nc, len, ret));
15641
  if (ret > 0) {
15642
    mg_if_sent_cb(nc, ret);
15643
    cs->last_ssl_write_size = 0;
15644
  } else if (ret < 0) {
15645
    /* This is tricky. We must remember the exact data we were sending to retry
15646
     * exactly the same send next time. */
15647
    cs->last_ssl_write_size = len;
15648
  }
15649
  if (ret == len) {
15650
    nc->flags &= ~MG_F_WANT_WRITE;
15651
  } else if (ret == MG_SSL_WANT_WRITE) {
15652
    nc->flags |= MG_F_WANT_WRITE;
15653
  } else {
15654
    mg_lwip_post_signal(MG_SIG_CLOSE_CONN, nc);
15655
  }
15656
}
15657

    
15658
void mg_lwip_ssl_recv(struct mg_connection *nc) {
15659
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15660
  /* Don't deliver data before connect callback */
15661
  if (nc->flags & MG_F_CONNECTING) return;
15662
  while (nc->recv_mbuf.len < nc->recv_mbuf_limit) {
15663
    char *buf = (char *) MG_MALLOC(MG_LWIP_SSL_IO_SIZE);
15664
    if (buf == NULL) return;
15665
    int ret = mg_ssl_if_read(nc, buf, MG_LWIP_SSL_IO_SIZE);
15666
    DBG(("%p %p SSL_read %u = %d", nc, cs->rx_chain, MG_LWIP_SSL_IO_SIZE, ret));
15667
    if (ret <= 0) {
15668
      MG_FREE(buf);
15669
      if (ret == MG_SSL_WANT_WRITE) {
15670
        nc->flags |= MG_F_WANT_WRITE;
15671
        return;
15672
      } else if (ret == MG_SSL_WANT_READ) {
15673
        /*
15674
         * Nothing to do in particular, we are callback-driven.
15675
         * What we definitely do not need anymore is SSL reading (nothing left).
15676
         */
15677
        nc->flags &= ~MG_F_WANT_READ;
15678
        cs->err = 0;
15679
        return;
15680
      } else {
15681
        mg_lwip_post_signal(MG_SIG_CLOSE_CONN, nc);
15682
        return;
15683
      }
15684
    } else {
15685
      mg_if_recv_tcp_cb(nc, buf, ret, 1 /* own */);
15686
    }
15687
  }
15688
}
15689

    
15690
#ifdef KR_VERSION
15691

    
15692
ssize_t kr_send(int fd, const void *buf, size_t len) {
15693
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) fd;
15694
  int ret = mg_lwip_tcp_write(cs->nc, buf, len);
15695
  DBG(("%p mg_lwip_tcp_write %u = %d", cs->nc, len, ret));
15696
  if (ret == 0) ret = KR_IO_WOULDBLOCK;
15697
  return ret;
15698
}
15699

    
15700
ssize_t kr_recv(int fd, void *buf, size_t len) {
15701
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) fd;
15702
  struct pbuf *seg = cs->rx_chain;
15703
  if (seg == NULL) {
15704
    DBG(("%u - nothing to read", len));
15705
    return KR_IO_WOULDBLOCK;
15706
  }
15707
  size_t seg_len = (seg->len - cs->rx_offset);
15708
  DBG(("%u %u %u %u", len, cs->rx_chain->len, seg_len, cs->rx_chain->tot_len));
15709
  len = MIN(len, seg_len);
15710
  pbuf_copy_partial(seg, buf, len, cs->rx_offset);
15711
  cs->rx_offset += len;
15712
  tcp_recved(cs->pcb.tcp, len);
15713
  if (cs->rx_offset == cs->rx_chain->len) {
15714
    cs->rx_chain = pbuf_dechain(cs->rx_chain);
15715
    pbuf_free(seg);
15716
    cs->rx_offset = 0;
15717
  }
15718
  return len;
15719
}
15720

    
15721
#elif MG_SSL_IF == MG_SSL_IF_MBEDTLS
15722

    
15723
int ssl_socket_send(void *ctx, const unsigned char *buf, size_t len) {
15724
  struct mg_connection *nc = (struct mg_connection *) ctx;
15725
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15726
  int ret = mg_lwip_tcp_write(cs->nc, buf, len);
15727
  if (ret == 0) ret = MBEDTLS_ERR_SSL_WANT_WRITE;
15728
  LOG(LL_DEBUG, ("%p %d -> %d", nc, len, ret));
15729
  return ret;
15730
}
15731

    
15732
int ssl_socket_recv(void *ctx, unsigned char *buf, size_t len) {
15733
  struct mg_connection *nc = (struct mg_connection *) ctx;
15734
  struct mg_lwip_conn_state *cs = (struct mg_lwip_conn_state *) nc->sock;
15735
  struct pbuf *seg = cs->rx_chain;
15736
  if (seg == NULL) {
15737
    DBG(("%u - nothing to read", len));
15738
    return MBEDTLS_ERR_SSL_WANT_READ;
15739
  }
15740
  size_t seg_len = (seg->len - cs->rx_offset);
15741
  DBG(("%u %u %u %u", len, cs->rx_chain->len, seg_len, cs->rx_chain->tot_len));
15742
  mgos_lock();
15743
  len = MIN(len, seg_len);
15744
  pbuf_copy_partial(seg, buf, len, cs->rx_offset);
15745
  cs->rx_offset += len;
15746
  /* TCP PCB may be NULL if connection has already been closed
15747
   * but we still have data to deliver to SSL. */
15748
  if (cs->pcb.tcp != NULL) tcp_recved(cs->pcb.tcp, len);
15749
  if (cs->rx_offset == cs->rx_chain->len) {
15750
    cs->rx_chain = pbuf_dechain(cs->rx_chain);
15751
    pbuf_free(seg);
15752
    cs->rx_offset = 0;
15753
  }
15754
  mgos_unlock();
15755
  LOG(LL_DEBUG, ("%p <- %d", nc, (int) len));
15756
  return len;
15757
}
15758

    
15759
#endif
15760

    
15761
#endif /* MG_ENABLE_SSL && MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL */
15762
#ifdef MG_MODULE_LINES
15763
#line 1 "common/platforms/wince/wince_libc.c"
15764
#endif
15765
/*
15766
 * Copyright (c) 2016 Cesanta Software Limited
15767
 * All rights reserved
15768
 */
15769

    
15770
#ifdef WINCE
15771

    
15772
const char *strerror(int err) {
15773
  /*
15774
   * TODO(alashkin): there is no strerror on WinCE;
15775
   * look for similar wce_xxxx function
15776
   */
15777
  static char buf[10];
15778
  snprintf(buf, sizeof(buf), "%d", err);
15779
  return buf;
15780
}
15781

    
15782
int open(const char *filename, int oflag, int pmode) {
15783
  /*
15784
   * TODO(alashkin): mg_open function is not used in mongoose
15785
   * but exists in documentation as utility function
15786
   * Shall we delete it at all or implement for WinCE as well?
15787
   */
15788
  DebugBreak();
15789
  return 0; /* for compiler */
15790
}
15791

    
15792
int _wstati64(const wchar_t *path, cs_stat_t *st) {
15793
  DWORD fa = GetFileAttributesW(path);
15794
  if (fa == INVALID_FILE_ATTRIBUTES) {
15795
    return -1;
15796
  }
15797
  memset(st, 0, sizeof(*st));
15798
  if ((fa & FILE_ATTRIBUTE_DIRECTORY) == 0) {
15799
    HANDLE h;
15800
    FILETIME ftime;
15801
    st->st_mode |= _S_IFREG;
15802
    h = CreateFileW(path, GENERIC_READ, 0, NULL, OPEN_EXISTING,
15803
                    FILE_ATTRIBUTE_NORMAL, NULL);
15804
    if (h == INVALID_HANDLE_VALUE) {
15805
      return -1;
15806
    }
15807
    st->st_size = GetFileSize(h, NULL);
15808
    GetFileTime(h, NULL, NULL, &ftime);
15809
    st->st_mtime = (uint32_t)((((uint64_t) ftime.dwLowDateTime +
15810
                                ((uint64_t) ftime.dwHighDateTime << 32)) /
15811
                               10000000.0) -
15812
                              11644473600);
15813
    CloseHandle(h);
15814
  } else {
15815
    st->st_mode |= _S_IFDIR;
15816
  }
15817
  return 0;
15818
}
15819

    
15820
/* Windows CE doesn't have neither gmtime nor strftime */
15821
static void mg_gmt_time_string(char *buf, size_t buf_len, time_t *t) {
15822
  FILETIME ft;
15823
  SYSTEMTIME systime;
15824
  if (t != NULL) {
15825
    uint64_t filetime = (*t + 11644473600) * 10000000;
15826
    ft.dwLowDateTime = filetime & 0xFFFFFFFF;
15827
    ft.dwHighDateTime = (filetime & 0xFFFFFFFF00000000) >> 32;
15828
    FileTimeToSystemTime(&ft, &systime);
15829
  } else {
15830
    GetSystemTime(&systime);
15831
  }
15832
  /* There is no PRIu16 in WinCE SDK */
15833
  snprintf(buf, buf_len, "%d.%d.%d %d:%d:%d GMT", (int) systime.wYear,
15834
           (int) systime.wMonth, (int) systime.wDay, (int) systime.wHour,
15835
           (int) systime.wMinute, (int) systime.wSecond);
15836
}
15837

    
15838
#endif
15839
#ifdef MG_MODULE_LINES
15840
#line 1 "common/platforms/pic32/pic32_net_if.h"
15841
#endif
15842
/*
15843
 * Copyright (c) 2014-2016 Cesanta Software Limited
15844
 * All rights reserved
15845
 */
15846

    
15847
#ifndef CS_COMMON_PLATFORMS_PIC32_NET_IF_H_
15848
#define CS_COMMON_PLATFORMS_PIC32_NET_IF_H_
15849

    
15850
/* Amalgamated: #include "mongoose/src/net_if.h" */
15851

    
15852
#ifdef __cplusplus
15853
extern "C" {
15854
#endif /* __cplusplus */
15855

    
15856
#ifndef MG_ENABLE_NET_IF_PIC32
15857
#define MG_ENABLE_NET_IF_PIC32 MG_NET_IF == MG_NET_IF_PIC32
15858
#endif
15859

    
15860
extern const struct mg_iface_vtable mg_pic32_iface_vtable;
15861

    
15862
#ifdef __cplusplus
15863
}
15864
#endif /* __cplusplus */
15865

    
15866
#endif /* CS_COMMON_PLATFORMS_PIC32_NET_IF_H_ */
15867
#ifdef MG_MODULE_LINES
15868
#line 1 "common/platforms/pic32/pic32_net_if.c"
15869
#endif
15870
/*
15871
 * Copyright (c) 2014-2016 Cesanta Software Limited
15872
 * All rights reserved
15873
 */
15874

    
15875
#if MG_ENABLE_NET_IF_PIC32
15876

    
15877
int mg_pic32_if_create_conn(struct mg_connection *nc) {
15878
  (void) nc;
15879
  return 1;
15880
}
15881

    
15882
void mg_pic32_if_recved(struct mg_connection *nc, size_t len) {
15883
  (void) nc;
15884
  (void) len;
15885
}
15886

    
15887
void mg_pic32_if_add_conn(struct mg_connection *nc) {
15888
  (void) nc;
15889
}
15890

    
15891
void mg_pic32_if_init(struct mg_iface *iface) {
15892
  (void) iface;
15893
  (void) mg_get_errno(); /* Shutup compiler */
15894
}
15895

    
15896
void mg_pic32_if_free(struct mg_iface *iface) {
15897
  (void) iface;
15898
}
15899

    
15900
void mg_pic32_if_remove_conn(struct mg_connection *nc) {
15901
  (void) nc;
15902
}
15903

    
15904
void mg_pic32_if_destroy_conn(struct mg_connection *nc) {
15905
  if (nc->sock == INVALID_SOCKET) return;
15906
  /* For UDP, only close outgoing sockets or listeners. */
15907
  if (!(nc->flags & MG_F_UDP)) {
15908
    /* Close TCP */
15909
    TCPIP_TCP_Close((TCP_SOCKET) nc->sock);
15910
  } else if (nc->listener == NULL) {
15911
    /* Only close outgoing UDP or listeners. */
15912
    TCPIP_UDP_Close((UDP_SOCKET) nc->sock);
15913
  }
15914

    
15915
  nc->sock = INVALID_SOCKET;
15916
}
15917

    
15918
int mg_pic32_if_listen_udp(struct mg_connection *nc, union socket_address *sa) {
15919
  nc->sock = TCPIP_UDP_ServerOpen(
15920
      sa->sin.sin_family == AF_INET ? IP_ADDRESS_TYPE_IPV4
15921
                                    : IP_ADDRESS_TYPE_IPV6,
15922
      ntohs(sa->sin.sin_port),
15923
      sa->sin.sin_addr.s_addr == 0 ? 0 : (IP_MULTI_ADDRESS *) &sa->sin);
15924
  if (nc->sock == INVALID_SOCKET) {
15925
    return -1;
15926
  }
15927
  return 0;
15928
}
15929

    
15930
void mg_pic32_if_udp_send(struct mg_connection *nc, const void *buf,
15931
                          size_t len) {
15932
  mbuf_append(&nc->send_mbuf, buf, len);
15933
}
15934

    
15935
void mg_pic32_if_tcp_send(struct mg_connection *nc, const void *buf,
15936
                          size_t len) {
15937
  mbuf_append(&nc->send_mbuf, buf, len);
15938
}
15939

    
15940
int mg_pic32_if_listen_tcp(struct mg_connection *nc, union socket_address *sa) {
15941
  nc->sock = TCPIP_TCP_ServerOpen(
15942
      sa->sin.sin_family == AF_INET ? IP_ADDRESS_TYPE_IPV4
15943
                                    : IP_ADDRESS_TYPE_IPV6,
15944
      ntohs(sa->sin.sin_port),
15945
      sa->sin.sin_addr.s_addr == 0 ? 0 : (IP_MULTI_ADDRESS *) &sa->sin);
15946
  memcpy(&nc->sa, sa, sizeof(*sa));
15947
  if (nc->sock == INVALID_SOCKET) {
15948
    return -1;
15949
  }
15950
  return 0;
15951
}
15952

    
15953
static int mg_accept_conn(struct mg_connection *lc) {
15954
  struct mg_connection *nc;
15955
  TCP_SOCKET_INFO si;
15956
  union socket_address sa;
15957

    
15958
  nc = mg_if_accept_new_conn(lc);
15959

    
15960
  if (nc == NULL) {
15961
    return 0;
15962
  }
15963

    
15964
  nc->sock = lc->sock;
15965
  nc->flags &= ~MG_F_LISTENING;
15966

    
15967
  if (!TCPIP_TCP_SocketInfoGet((TCP_SOCKET) nc->sock, &si)) {
15968
    return 0;
15969
  }
15970

    
15971
  if (si.addressType == IP_ADDRESS_TYPE_IPV4) {
15972
    sa.sin.sin_family = AF_INET;
15973
    sa.sin.sin_port = htons(si.remotePort);
15974
    sa.sin.sin_addr.s_addr = si.remoteIPaddress.v4Add.Val;
15975
  } else {
15976
    /* TODO(alashkin): do something with _potential_ IPv6 */
15977
    memset(&sa, 0, sizeof(sa));
15978
  }
15979

    
15980
  mg_if_accept_tcp_cb(nc, (union socket_address *) &sa, sizeof(sa));
15981

    
15982
  return mg_pic32_if_listen_tcp(lc, &lc->sa) >= 0;
15983
}
15984

    
15985
char *inet_ntoa(struct in_addr in) {
15986
  static char addr[17];
15987
  snprintf(addr, sizeof(addr), "%d.%d.%d.%d", (int) in.S_un.S_un_b.s_b1,
15988
           (int) in.S_un.S_un_b.s_b2, (int) in.S_un.S_un_b.s_b3,
15989
           (int) in.S_un.S_un_b.s_b4);
15990
  return addr;
15991
}
15992

    
15993
static void mg_handle_send(struct mg_connection *nc) {
15994
  uint16_t bytes_written = 0;
15995
  if (nc->flags & MG_F_UDP) {
15996
    if (!TCPIP_UDP_RemoteBind(
15997
            (UDP_SOCKET) nc->sock,
15998
            nc->sa.sin.sin_family == AF_INET ? IP_ADDRESS_TYPE_IPV4
15999
                                             : IP_ADDRESS_TYPE_IPV6,
16000
            ntohs(nc->sa.sin.sin_port), (IP_MULTI_ADDRESS *) &nc->sa.sin)) {
16001
      nc->flags |= MG_F_CLOSE_IMMEDIATELY;
16002
      return;
16003
    }
16004
    bytes_written = TCPIP_UDP_TxPutIsReady((UDP_SOCKET) nc->sock, 0);
16005
    if (bytes_written >= nc->send_mbuf.len) {
16006
      if (TCPIP_UDP_ArrayPut((UDP_SOCKET) nc->sock,
16007
                             (uint8_t *) nc->send_mbuf.buf,
16008
                             nc->send_mbuf.len) != nc->send_mbuf.len) {
16009
        nc->flags |= MG_F_CLOSE_IMMEDIATELY;
16010
        bytes_written = 0;
16011
      }
16012
    }
16013
  } else {
16014
    bytes_written = TCPIP_TCP_FifoTxFreeGet((TCP_SOCKET) nc->sock);
16015
    if (bytes_written != 0) {
16016
      if (bytes_written > nc->send_mbuf.len) {
16017
        bytes_written = nc->send_mbuf.len;
16018
      }
16019
      if (TCPIP_TCP_ArrayPut((TCP_SOCKET) nc->sock,
16020
                             (uint8_t *) nc->send_mbuf.buf,
16021
                             bytes_written) != bytes_written) {
16022
        nc->flags |= MG_F_CLOSE_IMMEDIATELY;
16023
        bytes_written = 0;
16024
      }
16025
    }
16026
  }
16027

    
16028
  mg_if_sent_cb(nc, bytes_written);
16029
}
16030

    
16031
static void mg_handle_recv(struct mg_connection *nc) {
16032
  uint16_t bytes_read = 0;
16033
  uint8_t *buf = NULL;
16034
  if (nc->flags & MG_F_UDP) {
16035
    bytes_read = TCPIP_UDP_GetIsReady((UDP_SOCKET) nc->sock);
16036
    if (bytes_read != 0 &&
16037
        (nc->recv_mbuf_limit == -1 ||
16038
         nc->recv_mbuf.len + bytes_read < nc->recv_mbuf_limit)) {
16039
      buf = (uint8_t *) MG_MALLOC(bytes_read);
16040
      if (TCPIP_UDP_ArrayGet((UDP_SOCKET) nc->sock, buf, bytes_read) !=
16041
          bytes_read) {
16042
        nc->flags |= MG_F_CLOSE_IMMEDIATELY;
16043
        bytes_read = 0;
16044
        MG_FREE(buf);
16045
      }
16046
    }
16047
  } else {
16048
    bytes_read = TCPIP_TCP_GetIsReady((TCP_SOCKET) nc->sock);
16049
    if (bytes_read != 0) {
16050
      if (nc->recv_mbuf_limit != -1 &&
16051
          nc->recv_mbuf_limit - nc->recv_mbuf.len > bytes_read) {
16052
        bytes_read = nc->recv_mbuf_limit - nc->recv_mbuf.len;
16053
      }
16054
      buf = (uint8_t *) MG_MALLOC(bytes_read);
16055
      if (TCPIP_TCP_ArrayGet((TCP_SOCKET) nc->sock, buf, bytes_read) !=
16056
          bytes_read) {
16057
        nc->flags |= MG_F_CLOSE_IMMEDIATELY;
16058
        MG_FREE(buf);
16059
        bytes_read = 0;
16060
      }
16061
    }
16062
  }
16063

    
16064
  if (bytes_read != 0) {
16065
    mg_if_recv_tcp_cb(nc, buf, bytes_read, 1 /* own */);
16066
  }
16067
}
16068

    
16069
time_t mg_pic32_if_poll(struct mg_iface *iface, int timeout_ms) {
16070
  struct mg_mgr *mgr = iface->mgr;
16071
  double now = mg_time();
16072
  struct mg_connection *nc, *tmp;
16073

    
16074
  for (nc = mgr->active_connections; nc != NULL; nc = tmp) {
16075
    tmp = nc->next;
16076

    
16077
    if (nc->flags & MG_F_CONNECTING) {
16078
      /* processing connections */
16079
      if (nc->flags & MG_F_UDP ||
16080
          TCPIP_TCP_IsConnected((TCP_SOCKET) nc->sock)) {
16081
        mg_if_connect_cb(nc, 0);
16082
      }
16083
    } else if (nc->flags & MG_F_LISTENING) {
16084
      if (TCPIP_TCP_IsConnected((TCP_SOCKET) nc->sock)) {
16085
        /* accept new connections */
16086
        mg_accept_conn(nc);
16087
      }
16088
    } else {
16089
      if (nc->send_mbuf.len != 0) {
16090
        mg_handle_send(nc);
16091
      }
16092

    
16093
      if (nc->recv_mbuf_limit == -1 ||
16094
          nc->recv_mbuf.len < nc->recv_mbuf_limit) {
16095
        mg_handle_recv(nc);
16096
      }
16097
    }
16098
  }
16099

    
16100
  for (nc = mgr->active_connections; nc != NULL; nc = tmp) {
16101
    tmp = nc->next;
16102
    if ((nc->flags & MG_F_CLOSE_IMMEDIATELY) ||
16103
        (nc->send_mbuf.len == 0 && (nc->flags & MG_F_SEND_AND_CLOSE))) {
16104
      mg_close_conn(nc);
16105
    }
16106
  }
16107

    
16108
  return now;
16109
}
16110

    
16111
void mg_pic32_if_sock_set(struct mg_connection *nc, sock_t sock) {
16112
  nc->sock = sock;
16113
}
16114

    
16115
void mg_pic32_if_get_conn_addr(struct mg_connection *nc, int remote,
16116
                               union socket_address *sa) {
16117
  /* TODO(alaskin): not implemented yet */
16118
}
16119

    
16120
void mg_pic32_if_connect_tcp(struct mg_connection *nc,
16121
                             const union socket_address *sa) {
16122
  nc->sock = TCPIP_TCP_ClientOpen(
16123
      sa->sin.sin_family == AF_INET ? IP_ADDRESS_TYPE_IPV4
16124
                                    : IP_ADDRESS_TYPE_IPV6,
16125
      ntohs(sa->sin.sin_port), (IP_MULTI_ADDRESS *) &sa->sin);
16126
  nc->err = (nc->sock == INVALID_SOCKET) ? -1 : 0;
16127
}
16128

    
16129
void mg_pic32_if_connect_udp(struct mg_connection *nc) {
16130
  nc->sock = TCPIP_UDP_ClientOpen(IP_ADDRESS_TYPE_ANY, 0, NULL);
16131
  nc->err = (nc->sock == INVALID_SOCKET) ? -1 : 0;
16132
}
16133

    
16134
/* clang-format off */
16135
#define MG_PIC32_IFACE_VTABLE                                   \
16136
  {                                                             \
16137
    mg_pic32_if_init,                                           \
16138
    mg_pic32_if_free,                                           \
16139
    mg_pic32_if_add_conn,                                       \
16140
    mg_pic32_if_remove_conn,                                    \
16141
    mg_pic32_if_poll,                                           \
16142
    mg_pic32_if_listen_tcp,                                     \
16143
    mg_pic32_if_listen_udp,                                     \
16144
    mg_pic32_if_connect_tcp,                                    \
16145
    mg_pic32_if_connect_udp,                                    \
16146
    mg_pic32_if_tcp_send,                                       \
16147
    mg_pic32_if_udp_send,                                       \
16148
    mg_pic32_if_recved,                                         \
16149
    mg_pic32_if_create_conn,                                    \
16150
    mg_pic32_if_destroy_conn,                                   \
16151
    mg_pic32_if_sock_set,                                       \
16152
    mg_pic32_if_get_conn_addr,                                  \
16153
  }
16154
/* clang-format on */
16155

    
16156
const struct mg_iface_vtable mg_pic32_iface_vtable = MG_PIC32_IFACE_VTABLE;
16157
#if MG_NET_IF == MG_NET_IF_PIC32
16158
const struct mg_iface_vtable mg_default_iface_vtable = MG_PIC32_IFACE_VTABLE;
16159
#endif
16160

    
16161
#endif /* MG_ENABLE_NET_IF_PIC32 */
16162
#ifdef MG_MODULE_LINES
16163
#line 1 "common/platforms/windows/windows_direct.c"
16164
#endif
16165
/*
16166
 * Copyright (c) 2017 Cesanta Software Limited
16167
 * All rights reserved
16168
 */
16169

    
16170
#ifdef _WIN32
16171

    
16172
int rmdir(const char *dirname) {
16173
  return _rmdir(dirname);
16174
}
16175

    
16176
unsigned int sleep(unsigned int seconds) {
16177
  Sleep(seconds * 1000);
16178
  return 0;
16179
}
16180

    
16181
#endif /* _WIN32 */
(7-7/9)