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

+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;