2022-02-23 05:35:03 +08:00
|
|
|
#include "dns.h"
|
2022-07-28 17:18:17 +08:00
|
|
|
#include "fmt.h"
|
2021-05-12 15:43:34 +08:00
|
|
|
#include "log.h"
|
2022-10-28 17:57:06 +08:00
|
|
|
#include "net.h"
|
2022-04-12 21:14:55 +08:00
|
|
|
#include "timer.h"
|
2022-02-23 11:06:02 +08:00
|
|
|
#include "tls.h"
|
2020-12-05 19:26:32 +08:00
|
|
|
|
2022-09-23 15:59:02 +08:00
|
|
|
size_t mg_vprintf(struct mg_connection *c, const char *fmt, va_list *ap) {
|
2022-06-30 00:42:55 +08:00
|
|
|
size_t old = c->send.len;
|
2022-09-23 15:59:02 +08:00
|
|
|
mg_vxprintf(mg_pfn_iobuf, &c->send, fmt, ap);
|
2022-06-30 00:42:55 +08:00
|
|
|
return c->send.len - old;
|
2020-12-05 19:26:32 +08:00
|
|
|
}
|
|
|
|
|
2022-02-11 01:11:03 +08:00
|
|
|
size_t mg_printf(struct mg_connection *c, const char *fmt, ...) {
|
|
|
|
size_t len = 0;
|
2020-12-05 19:26:32 +08:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2022-09-23 15:59:02 +08:00
|
|
|
len = mg_vprintf(c, fmt, &ap);
|
2020-12-05 19:26:32 +08:00
|
|
|
va_end(ap);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2020-12-22 17:44:59 +08:00
|
|
|
static bool mg_atonl(struct mg_str str, struct mg_addr *addr) {
|
2020-12-23 18:15:09 +08:00
|
|
|
if (mg_vcasecmp(&str, "localhost") != 0) return false;
|
2020-12-22 17:44:59 +08:00
|
|
|
addr->ip = mg_htonl(0x7f000001);
|
|
|
|
addr->is_ip6 = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-29 21:57:33 +08:00
|
|
|
static bool mg_atone(struct mg_str str, struct mg_addr *addr) {
|
|
|
|
if (str.len > 0) return false;
|
|
|
|
addr->ip = 0;
|
|
|
|
addr->is_ip6 = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-22 17:44:59 +08:00
|
|
|
static bool mg_aton4(struct mg_str str, struct mg_addr *addr) {
|
|
|
|
uint8_t data[4] = {0, 0, 0, 0};
|
|
|
|
size_t i, num_dots = 0;
|
|
|
|
for (i = 0; i < str.len; i++) {
|
|
|
|
if (str.ptr[i] >= '0' && str.ptr[i] <= '9') {
|
|
|
|
int octet = data[num_dots] * 10 + (str.ptr[i] - '0');
|
|
|
|
if (octet > 255) return false;
|
2021-05-29 01:30:42 +08:00
|
|
|
data[num_dots] = (uint8_t) octet;
|
2020-12-22 17:44:59 +08:00
|
|
|
} else if (str.ptr[i] == '.') {
|
|
|
|
if (num_dots >= 3 || i == 0 || str.ptr[i - 1] == '.') return false;
|
|
|
|
num_dots++;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (num_dots != 3 || str.ptr[i - 1] == '.') return false;
|
|
|
|
memcpy(&addr->ip, data, sizeof(data));
|
|
|
|
addr->is_ip6 = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-09-19 02:38:59 +08:00
|
|
|
static bool mg_v4mapped(struct mg_str str, struct mg_addr *addr) {
|
|
|
|
int i;
|
|
|
|
if (str.len < 14) return false;
|
|
|
|
if (str.ptr[0] != ':' || str.ptr[1] != ':' || str.ptr[6] != ':') return false;
|
|
|
|
for (i = 2; i < 6; i++) {
|
|
|
|
if (str.ptr[i] != 'f' && str.ptr[i] != 'F') return false;
|
|
|
|
}
|
|
|
|
if (!mg_aton4(mg_str_n(&str.ptr[7], str.len - 7), addr)) return false;
|
|
|
|
memset(addr->ip6, 0, sizeof(addr->ip6));
|
|
|
|
addr->ip6[10] = addr->ip6[11] = 255;
|
|
|
|
memcpy(&addr->ip6[12], &addr->ip, 4);
|
|
|
|
addr->is_ip6 = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-22 17:44:59 +08:00
|
|
|
static bool mg_aton6(struct mg_str str, struct mg_addr *addr) {
|
|
|
|
size_t i, j = 0, n = 0, dc = 42;
|
2021-09-21 15:10:27 +08:00
|
|
|
if (str.len > 2 && str.ptr[0] == '[') str.ptr++, str.len -= 2;
|
2021-09-19 02:38:59 +08:00
|
|
|
if (mg_v4mapped(str, addr)) return true;
|
2020-12-22 17:44:59 +08:00
|
|
|
for (i = 0; i < str.len; i++) {
|
|
|
|
if ((str.ptr[i] >= '0' && str.ptr[i] <= '9') ||
|
|
|
|
(str.ptr[i] >= 'a' && str.ptr[i] <= 'f') ||
|
|
|
|
(str.ptr[i] >= 'A' && str.ptr[i] <= 'F')) {
|
|
|
|
unsigned long val;
|
|
|
|
if (i > j + 3) return false;
|
2022-02-13 02:17:25 +08:00
|
|
|
// MG_DEBUG(("%zu %zu [%.*s]", i, j, (int) (i - j + 1), &str.ptr[j]));
|
2020-12-22 17:44:59 +08:00
|
|
|
val = mg_unhexn(&str.ptr[j], i - j + 1);
|
2021-09-19 02:38:59 +08:00
|
|
|
addr->ip6[n] = (uint8_t) ((val >> 8) & 255);
|
|
|
|
addr->ip6[n + 1] = (uint8_t) (val & 255);
|
2020-12-22 17:44:59 +08:00
|
|
|
} else if (str.ptr[i] == ':') {
|
|
|
|
j = i + 1;
|
|
|
|
if (i > 0 && str.ptr[i - 1] == ':') {
|
|
|
|
dc = n; // Double colon
|
|
|
|
if (i > 1 && str.ptr[i - 2] == ':') return false;
|
|
|
|
} else if (i > 0) {
|
|
|
|
n += 2;
|
2020-12-21 00:55:33 +08:00
|
|
|
}
|
2020-12-22 17:44:59 +08:00
|
|
|
if (n > 14) return false;
|
|
|
|
addr->ip6[n] = addr->ip6[n + 1] = 0; // For trailing ::
|
|
|
|
} else {
|
|
|
|
return false;
|
2020-12-21 00:55:33 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-22 17:44:59 +08:00
|
|
|
if (n < 14 && dc == 42) return false;
|
|
|
|
if (n < 14) {
|
|
|
|
memmove(&addr->ip6[dc + (14 - n)], &addr->ip6[dc], n - dc + 2);
|
|
|
|
memset(&addr->ip6[dc], 0, 14 - n);
|
|
|
|
}
|
|
|
|
addr->is_ip6 = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mg_aton(struct mg_str str, struct mg_addr *addr) {
|
2022-02-13 02:17:25 +08:00
|
|
|
// MG_INFO(("[%.*s]", (int) str.len, str.ptr));
|
2021-11-29 21:57:33 +08:00
|
|
|
return mg_atone(str, addr) || mg_atonl(str, addr) || mg_aton4(str, addr) ||
|
|
|
|
mg_aton6(str, addr);
|
2020-12-21 00:55:33 +08:00
|
|
|
}
|
2020-12-21 20:26:44 +08:00
|
|
|
|
2022-02-23 18:51:01 +08:00
|
|
|
struct mg_connection *mg_alloc_conn(struct mg_mgr *mgr) {
|
2022-05-05 00:18:35 +08:00
|
|
|
struct mg_connection *c =
|
|
|
|
(struct mg_connection *) calloc(1, sizeof(*c) + mgr->extraconnsize);
|
2022-02-23 05:14:29 +08:00
|
|
|
if (c != NULL) {
|
|
|
|
c->mgr = mgr;
|
2022-08-01 19:53:25 +08:00
|
|
|
c->send.align = c->recv.align = MG_IO_SIZE;
|
2022-02-23 05:14:29 +08:00
|
|
|
c->id = ++mgr->nextid;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
2022-02-23 11:06:02 +08:00
|
|
|
|
|
|
|
void mg_close_conn(struct mg_connection *c) {
|
|
|
|
mg_resolve_cancel(c); // Close any pending DNS query
|
|
|
|
LIST_DELETE(struct mg_connection, &c->mgr->conns, c);
|
|
|
|
if (c == c->mgr->dns4.c) c->mgr->dns4.c = NULL;
|
|
|
|
if (c == c->mgr->dns6.c) c->mgr->dns6.c = NULL;
|
|
|
|
// Order of operations is important. `MG_EV_CLOSE` event must be fired
|
|
|
|
// before we deallocate received data, see #1331
|
|
|
|
mg_call(c, MG_EV_CLOSE, NULL);
|
2022-08-26 21:19:13 +08:00
|
|
|
MG_DEBUG(("%lu %p closed", c->id, c->fd));
|
2022-02-23 11:06:02 +08:00
|
|
|
|
|
|
|
mg_tls_free(c);
|
|
|
|
mg_iobuf_free(&c->recv);
|
|
|
|
mg_iobuf_free(&c->send);
|
|
|
|
memset(c, 0, sizeof(*c));
|
|
|
|
free(c);
|
|
|
|
}
|
|
|
|
|
2022-02-23 05:27:08 +08:00
|
|
|
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
|
|
|
mg_event_handler_t fn, void *fn_data) {
|
|
|
|
struct mg_connection *c = NULL;
|
|
|
|
if (url == NULL || url[0] == '\0') {
|
|
|
|
MG_ERROR(("null url"));
|
2022-02-23 18:51:01 +08:00
|
|
|
} else if ((c = mg_alloc_conn(mgr)) == NULL) {
|
2022-02-23 05:27:08 +08:00
|
|
|
MG_ERROR(("OOM"));
|
|
|
|
} else {
|
|
|
|
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
|
|
|
|
c->is_udp = (strncmp(url, "udp:", 4) == 0);
|
2022-11-05 18:07:31 +08:00
|
|
|
c->fd = (void *) (size_t) MG_INVALID_SOCKET;
|
2022-02-23 05:27:08 +08:00
|
|
|
c->fn = fn;
|
2022-02-23 18:51:01 +08:00
|
|
|
c->is_client = true;
|
2022-02-23 05:27:08 +08:00
|
|
|
c->fn_data = fn_data;
|
2022-09-08 03:48:23 +08:00
|
|
|
MG_DEBUG(("%lu %p %s", c->id, c->fd, url));
|
2022-02-23 05:27:08 +08:00
|
|
|
mg_call(c, MG_EV_OPEN, NULL);
|
|
|
|
mg_resolve(c, url);
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
2022-02-23 05:14:29 +08:00
|
|
|
|
2022-02-23 11:06:02 +08:00
|
|
|
struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
|
|
|
|
mg_event_handler_t fn, void *fn_data) {
|
|
|
|
struct mg_connection *c = NULL;
|
2022-02-23 18:51:01 +08:00
|
|
|
if ((c = mg_alloc_conn(mgr)) == NULL) {
|
2022-02-23 11:06:02 +08:00
|
|
|
MG_ERROR(("OOM %s", url));
|
|
|
|
} else if (!mg_open_listener(c, url)) {
|
|
|
|
MG_ERROR(("Failed: %s, errno %d", url, errno));
|
|
|
|
free(c);
|
2022-05-31 00:56:50 +08:00
|
|
|
c = NULL;
|
2022-02-23 11:06:02 +08:00
|
|
|
} else {
|
|
|
|
c->is_listening = 1;
|
|
|
|
c->is_udp = strncmp(url, "udp:", 4) == 0;
|
|
|
|
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
|
|
|
|
c->fn = fn;
|
|
|
|
c->fn_data = fn_data;
|
|
|
|
mg_call(c, MG_EV_OPEN, NULL);
|
2022-04-01 22:40:53 +08:00
|
|
|
MG_DEBUG(("%lu %p %s", c->id, c->fd, url));
|
2022-02-23 11:06:02 +08:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2022-04-22 21:42:07 +08:00
|
|
|
struct mg_connection *mg_wrapfd(struct mg_mgr *mgr, int fd,
|
|
|
|
mg_event_handler_t fn, void *fn_data) {
|
|
|
|
struct mg_connection *c = mg_alloc_conn(mgr);
|
|
|
|
if (c != NULL) {
|
|
|
|
c->fd = (void *) (size_t) fd;
|
|
|
|
c->fn = fn;
|
|
|
|
c->fn_data = fn_data;
|
2022-08-03 22:07:16 +08:00
|
|
|
MG_EPOLL_ADD(c);
|
2022-04-22 21:42:07 +08:00
|
|
|
mg_call(c, MG_EV_OPEN, NULL);
|
|
|
|
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2022-04-12 21:14:55 +08:00
|
|
|
struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
|
|
|
|
unsigned flags, void (*fn)(void *), void *arg) {
|
|
|
|
struct mg_timer *t = (struct mg_timer *) calloc(1, sizeof(*t));
|
2022-09-21 00:54:17 +08:00
|
|
|
if (t != NULL) {
|
|
|
|
mg_timer_init(&mgr->timers, t, milliseconds, flags, fn, arg);
|
|
|
|
t->id = mgr->timerid++;
|
|
|
|
}
|
2022-04-12 21:14:55 +08:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2020-12-21 20:26:44 +08:00
|
|
|
void mg_mgr_free(struct mg_mgr *mgr) {
|
|
|
|
struct mg_connection *c;
|
2022-04-12 21:14:55 +08:00
|
|
|
struct mg_timer *tmp, *t = mgr->timers;
|
|
|
|
while (t != NULL) tmp = t->next, free(t), t = tmp;
|
2022-05-10 14:38:03 +08:00
|
|
|
mgr->timers = NULL; // Important. Next call to poll won't touch timers
|
2020-12-21 20:26:44 +08:00
|
|
|
for (c = mgr->conns; c != NULL; c = c->next) c->is_closing = 1;
|
|
|
|
mg_mgr_poll(mgr, 0);
|
2022-11-06 09:03:33 +08:00
|
|
|
#if MG_ENABLE_FREERTOS_TCP
|
2020-12-21 20:26:44 +08:00
|
|
|
FreeRTOS_DeleteSocketSet(mgr->ss);
|
|
|
|
#endif
|
2022-02-13 02:17:25 +08:00
|
|
|
MG_DEBUG(("All connections closed"));
|
2022-08-03 22:07:16 +08:00
|
|
|
#if MG_ENABLE_EPOLL
|
|
|
|
if (mgr->epoll_fd >= 0) close(mgr->epoll_fd), mgr->epoll_fd = -1;
|
|
|
|
#endif
|
2020-12-21 20:26:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mg_mgr_init(struct mg_mgr *mgr) {
|
2021-05-12 15:43:34 +08:00
|
|
|
memset(mgr, 0, sizeof(*mgr));
|
2022-08-03 22:07:16 +08:00
|
|
|
#if MG_ENABLE_EPOLL
|
|
|
|
if ((mgr->epoll_fd = epoll_create1(0)) < 0) MG_ERROR(("epoll: %d", errno));
|
|
|
|
#else
|
|
|
|
mgr->epoll_fd = -1;
|
|
|
|
#endif
|
2022-02-12 22:33:43 +08:00
|
|
|
#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK
|
2021-05-12 16:25:21 +08:00
|
|
|
// clang-format off
|
|
|
|
{ WSADATA data; WSAStartup(MAKEWORD(2, 2), &data); }
|
|
|
|
// clang-format on
|
2022-11-06 09:03:33 +08:00
|
|
|
#elif MG_ENABLE_FREERTOS_TCP
|
2020-12-21 20:26:44 +08:00
|
|
|
mgr->ss = FreeRTOS_CreateSocketSet();
|
|
|
|
#elif defined(__unix) || defined(__unix__) || defined(__APPLE__)
|
|
|
|
// Ignore SIGPIPE signal, so if client cancels the request, it
|
|
|
|
// won't kill the whole process.
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
mgr->dnstimeout = 3000;
|
2020-12-22 17:44:59 +08:00
|
|
|
mgr->dns4.url = "udp://8.8.8.8:53";
|
|
|
|
mgr->dns6.url = "udp://[2001:4860:4860::8888]:53";
|
2020-12-21 20:26:44 +08:00
|
|
|
}
|