Set c->loc for accepted and UDP connections

This commit is contained in:
Sergey Lyubka committed 2022-05-06 20:19:40 +01:00
1 parent b5fc0d30a8
commit 6db0485c7b
6 files changed
+61 -27

No files matched your search

+1 -1
View File
@@ -163,7 +163,7 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
char buf[100];
dm.addr.port = d->c->rem.port; // Save port
d->c->rem = dm.addr; // Copy resolved address
MG_DEBUG(("%lu %s resolved to %s", d->c->id, dm.name,
MG_DEBUG(("%lu %s is %s", d->c->id, dm.name,
mg_ntoa(&d->c->rem, buf, sizeof(buf))));
mg_connect_resolved(d->c);
#if MG_ENABLE_IPV6
+3 -3
View File
@@ -33,8 +33,8 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) {
int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len);
if (milliseconds > 0) {
mg_call(c, MG_EV_SNTP_TIME, (uint64_t *) &milliseconds);
MG_DEBUG(("%u.%u", (unsigned) (milliseconds / 1000),
(unsigned) (milliseconds % 1000)));
MG_VERBOSE(("%u.%u", (unsigned) (milliseconds / 1000),
(unsigned) (milliseconds % 1000)));
}
c->recv.len = 0; // Clear receive buffer
} else if (ev == MG_EV_CONNECT) {
@@ -52,7 +52,7 @@ void mg_sntp_send(struct mg_connection *c, unsigned long utc) {
uint8_t buf[48] = {0};
buf[0] = (0 << 6) | (4 << 3) | 3;
mg_send(c, buf, sizeof(buf));
MG_DEBUG(("%lu ct %lu", c->id, utc));
(void) utc;
}
}
+11 -7
View File
@@ -99,6 +99,14 @@ bool mg_sock_conn_reset(void) {
#endif
}
static void setlocaddr(SOCKET fd, struct mg_addr *addr) {
union usa usa;
socklen_t n = sizeof(usa);
if (getsockname(fd, &usa.sa, &n) == 0) {
tomgaddr(&usa, addr, n != sizeof(usa.sin));
}
}
static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
if (n == 0) {
// Do nothing
@@ -139,6 +147,7 @@ static long mg_sock_send(struct mg_connection *c, const void *buf, size_t len) {
union usa usa;
socklen_t slen = tousa(&c->rem, &usa);
n = sendto(FD(c), (char *) buf, len, 0, &usa.sa, slen);
if (n > 0) setlocaddr(FD(c), &c->loc);
} else {
n = send(FD(c), (char *) buf, len, MSG_NONBLOCKING);
}
@@ -216,12 +225,7 @@ bool mg_open_listener(struct mg_connection *c, const char *url) {
// NOTE(lsm): FreeRTOS uses backlog value as a connection limit
(type == SOCK_DGRAM || listen(fd, MG_SOCK_LISTEN_BACKLOG_SIZE) == 0)) {
// In case port was set to 0, get the real port number
if (getsockname(fd, &usa.sa, &slen) == 0) {
c->loc.port = usa.sin.sin_port;
#if MG_ENABLE_IPV6
if (c->loc.is_ip6) c->loc.port = usa.sin6.sin6_port;
#endif
}
setlocaddr(fd, &c->loc);
mg_set_non_blocking_mode(fd);
} else if (fd != INVALID_SOCKET) {
s_err = MG_SOCK_ERRNO;
@@ -395,8 +399,8 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
setsockopts(c);
c->is_accepted = 1;
c->is_hexdumping = lsn->is_hexdumping;
c->pfn = lsn->pfn;
c->loc = lsn->loc;
c->pfn = lsn->pfn;
c->pfn_data = lsn->pfn_data;
c->fn = lsn->fn;
c->fn_data = lsn->fn_data;