Introduce local address

This commit is contained in:
Sergey Lyubka 2022-02-22 22:00:55 +00:00
parent 4012a86bd8
commit e99973d4b7
8 changed files with 53 additions and 50 deletions

View File

@ -391,7 +391,8 @@ with some housekeeping information.
struct mg_connection {
struct mg_connection *next; // Linkage in struct mg_mgr :: connections
struct mg_mgr *mgr; // Our container
struct mg_addr peer; // Remote address. For listeners, local address
struct mg_addr loc; // Local address
struct mg_addr rem; // Remote address
void *fd; // Connected socket, or LWIP data
unsigned long id; // Auto-incrementing unique connection ID
struct mg_iobuf recv; // Incoming data

View File

@ -10,14 +10,14 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_OPEN) {
c->is_hexdumping = 1;
} else if (ev == MG_EV_RESOLVE) {
// c->peer gets populated with multicast address. Store it in c->label
memcpy(c->label, &c->peer, sizeof(c->peer));
// c->rem gets populated with multicast address. Store it in c->label
memcpy(c->label, &c->rem, sizeof(c->rem));
} else if (ev == MG_EV_READ) {
// Each response to the SSDP socket will change c->peer.
// Each response to the SSDP socket will change c->rem.
// We can now do mg_printf(c, "haha"); to respond back to the remote side.
// But in our case, we should restore the multicast address in order
// to have next search to go to the multicast address
memcpy(&c->peer, c->label, sizeof(c->peer));
memcpy(&c->rem, c->label, sizeof(c->rem));
}
}

View File

@ -267,10 +267,10 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
if (d->c->is_resolving) {
if (dm.resolved) {
char buf[100];
dm.addr.port = d->c->peer.port; // Save port
d->c->peer = dm.addr; // Copy resolved address
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_ntoa(&d->c->peer, buf, sizeof(buf))));
mg_ntoa(&d->c->rem, buf, sizeof(buf))));
mg_connect_resolved(d->c);
#if MG_ENABLE_IPV6
} else if (dm.addr.is_ip6 == false && dm.name[0] != '\0') {
@ -358,16 +358,15 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
d->c = c;
c->is_resolving = 1;
MG_VERBOSE(("%lu resolving %.*s @ %s, txnid %hu", c->id, (int) name->len,
name->ptr, mg_ntoa(&dnsc->c->peer, buf, sizeof(buf)),
d->txnid));
name->ptr, mg_ntoa(&dnsc->c->rem, buf, sizeof(buf)), d->txnid));
mg_dns_send(dnsc->c, name, d->txnid, ipv6);
}
}
void mg_resolve(struct mg_connection *c, const char *url) {
struct mg_str host = mg_url_host(url);
c->peer.port = mg_htons(mg_url_port(url));
if (mg_aton(host, &c->peer)) {
c->rem.port = mg_htons(mg_url_port(url));
if (mg_aton(host, &c->rem)) {
// host is an IP address, do not fire name resolution
mg_connect_resolved(c);
} else {
@ -3251,10 +3250,10 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
memset(&usa, 0, sizeof(usa));
memset(&a, 0, sizeof(a));
getsockname(FD(c), &usa.sa, &slen);
tomgaddr(&usa, &a, c->peer.is_ip6);
tomgaddr(&usa, &a, c->rem.is_ip6);
MG_INFO(("\n-- %lu %s %s %s %s %ld\n%s", c->id,
mg_straddr(&a, t1, sizeof(t1)), r ? "<-" : "->",
mg_straddr(&c->peer, t2, sizeof(t2)), c->label, n, s));
mg_straddr(&c->rem, t2, sizeof(t2)), c->label, n, s));
free(s);
(void) t1, (void) t2; // Silence warnings for MG_ENABLE_LOG=0
}
@ -3274,7 +3273,7 @@ static long mg_sock_send(struct mg_connection *c, const void *buf, size_t len) {
long n;
if (c->is_udp) {
union usa usa;
socklen_t slen = tousa(&c->peer, &usa);
socklen_t slen = tousa(&c->rem, &usa);
n = sendto(FD(c), (char *) buf, len, 0, &usa.sa, slen);
} else {
n = send(FD(c), (char *) buf, len, MSG_NONBLOCKING);
@ -3377,9 +3376,9 @@ static long mg_sock_recv(struct mg_connection *c, void *buf, size_t len) {
long n = 0;
if (c->is_udp) {
union usa usa;
socklen_t slen = tousa(&c->peer, &usa);
socklen_t slen = tousa(&c->rem, &usa);
n = recvfrom(FD(c), (char *) buf, len, 0, &usa.sa, &slen);
if (n > 0) tomgaddr(&usa, &c->peer, slen != sizeof(usa.sin));
if (n > 0) tomgaddr(&usa, &c->rem, slen != sizeof(usa.sin));
} else {
n = recv(FD(c), (char *) buf, len, MSG_NONBLOCKING);
}
@ -3465,8 +3464,8 @@ static void setsockopts(struct mg_connection *c) {
void mg_connect_resolved(struct mg_connection *c) {
// char buf[40];
int type = c->is_udp ? SOCK_DGRAM : SOCK_STREAM;
int rc, af = c->peer.is_ip6 ? AF_INET6 : AF_INET;
// mg_straddr(&c->peer, buf, sizeof(buf));
int rc, af = c->rem.is_ip6 ? AF_INET6 : AF_INET;
// mg_straddr(&c->rem, buf, sizeof(buf));
c->fd = S2PTR(socket(af, type, 0));
c->is_resolving = 0;
if (FD(c) == INVALID_SOCKET) {
@ -3476,7 +3475,7 @@ void mg_connect_resolved(struct mg_connection *c) {
mg_call(c, MG_EV_CONNECT, NULL);
} else {
union usa usa;
socklen_t slen = tousa(&c->peer, &usa);
socklen_t slen = tousa(&c->rem, &usa);
mg_set_non_blocking_mode(FD(c));
setsockopts(c);
mg_call(c, MG_EV_RESOLVE, NULL);
@ -3513,8 +3512,8 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
closesocket(fd);
} else {
char buf[40];
tomgaddr(&usa, &c->peer, sa_len != sizeof(usa.sin));
mg_straddr(&c->peer, buf, sizeof(buf));
tomgaddr(&usa, &c->rem, sa_len != sizeof(usa.sin));
mg_straddr(&c->rem, buf, sizeof(buf));
MG_DEBUG(("%lu accepted %s", c->id, buf));
mg_set_non_blocking_mode(FD(c));
setsockopts(c);
@ -3522,6 +3521,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
c->is_accepted = 1;
c->is_hexdumping = lsn->is_hexdumping;
c->pfn = lsn->pfn;
c->loc = lsn->loc;
c->pfn_data = lsn->pfn_data;
c->fn = lsn->fn;
c->fn_data = lsn->fn_data;
@ -3584,7 +3584,7 @@ struct mg_connection *mg_mkpipe(struct mg_mgr *mgr, mg_event_handler_t fn,
MG_ERROR(("OOM"));
} else {
MG_DEBUG(("pipe %lu", (unsigned long) sp[0]));
tomgaddr(&usa[0], &c->peer, false);
tomgaddr(&usa[0], &c->rem, false);
c->is_udp = 1;
c->pfn = pf1;
c->pfn_data = (void *) (size_t) sp[0];
@ -3608,7 +3608,7 @@ struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
MG_ERROR(("OOM %s", url));
closesocket(fd);
} else {
memcpy(&c->peer, &addr, sizeof(struct mg_addr));
memcpy(&c->loc, &addr, sizeof(struct mg_addr));
c->fd = S2PTR(fd);
c->is_listening = 1;
c->is_udp = is_udp;
@ -3616,7 +3616,7 @@ struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
c->fn = fn;
c->fn_data = fn_data;
mg_call(c, MG_EV_OPEN, NULL);
MG_DEBUG(("%lu %s port %u", c->id, url, mg_ntohs(c->peer.port)));
MG_DEBUG(("%lu %s port %u", c->id, url, mg_ntohs(c->rem.port)));
}
return c;
}
@ -3681,7 +3681,7 @@ static void connect_conn(struct mg_connection *c) {
if (rc) {
char buf[50];
mg_error(c, "error connecting to %s",
mg_straddr(&c->peer, buf, sizeof(buf)));
mg_straddr(&c->rem, buf, sizeof(buf)));
} else {
if (c->is_tls_hs) mg_tls_handshake(c);
mg_call(c, MG_EV_CONNECT, NULL);

View File

@ -836,7 +836,8 @@ struct mg_mgr {
struct mg_connection {
struct mg_connection *next; // Linkage in struct mg_mgr :: connections
struct mg_mgr *mgr; // Our container
struct mg_addr peer; // Remote address. For listeners, local address
struct mg_addr loc; // Local address
struct mg_addr rem; // Remote address
void *fd; // Connected socket, or LWIP data
unsigned long id; // Auto-incrementing unique connection ID
struct mg_iobuf recv; // Incoming data

View File

@ -155,10 +155,10 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data,
if (d->c->is_resolving) {
if (dm.resolved) {
char buf[100];
dm.addr.port = d->c->peer.port; // Save port
d->c->peer = dm.addr; // Copy resolved address
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_ntoa(&d->c->peer, buf, sizeof(buf))));
mg_ntoa(&d->c->rem, buf, sizeof(buf))));
mg_connect_resolved(d->c);
#if MG_ENABLE_IPV6
} else if (dm.addr.is_ip6 == false && dm.name[0] != '\0') {
@ -246,16 +246,15 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
d->c = c;
c->is_resolving = 1;
MG_VERBOSE(("%lu resolving %.*s @ %s, txnid %hu", c->id, (int) name->len,
name->ptr, mg_ntoa(&dnsc->c->peer, buf, sizeof(buf)),
d->txnid));
name->ptr, mg_ntoa(&dnsc->c->rem, buf, sizeof(buf)), d->txnid));
mg_dns_send(dnsc->c, name, d->txnid, ipv6);
}
}
void mg_resolve(struct mg_connection *c, const char *url) {
struct mg_str host = mg_url_host(url);
c->peer.port = mg_htons(mg_url_port(url));
if (mg_aton(host, &c->peer)) {
c->rem.port = mg_htons(mg_url_port(url));
if (mg_aton(host, &c->rem)) {
// host is an IP address, do not fire name resolution
mg_connect_resolved(c);
} else {

View File

@ -32,7 +32,8 @@ struct mg_mgr {
struct mg_connection {
struct mg_connection *next; // Linkage in struct mg_mgr :: connections
struct mg_mgr *mgr; // Our container
struct mg_addr peer; // Remote address. For listeners, local address
struct mg_addr loc; // Local address
struct mg_addr rem; // Remote address
void *fd; // Connected socket, or LWIP data
unsigned long id; // Auto-incrementing unique connection ID
struct mg_iobuf recv; // Incoming data

View File

@ -121,10 +121,10 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
memset(&usa, 0, sizeof(usa));
memset(&a, 0, sizeof(a));
getsockname(FD(c), &usa.sa, &slen);
tomgaddr(&usa, &a, c->peer.is_ip6);
tomgaddr(&usa, &a, c->rem.is_ip6);
MG_INFO(("\n-- %lu %s %s %s %s %ld\n%s", c->id,
mg_straddr(&a, t1, sizeof(t1)), r ? "<-" : "->",
mg_straddr(&c->peer, t2, sizeof(t2)), c->label, n, s));
mg_straddr(&c->rem, t2, sizeof(t2)), c->label, n, s));
free(s);
(void) t1, (void) t2; // Silence warnings for MG_ENABLE_LOG=0
}
@ -144,7 +144,7 @@ static long mg_sock_send(struct mg_connection *c, const void *buf, size_t len) {
long n;
if (c->is_udp) {
union usa usa;
socklen_t slen = tousa(&c->peer, &usa);
socklen_t slen = tousa(&c->rem, &usa);
n = sendto(FD(c), (char *) buf, len, 0, &usa.sa, slen);
} else {
n = send(FD(c), (char *) buf, len, MSG_NONBLOCKING);
@ -247,9 +247,9 @@ static long mg_sock_recv(struct mg_connection *c, void *buf, size_t len) {
long n = 0;
if (c->is_udp) {
union usa usa;
socklen_t slen = tousa(&c->peer, &usa);
socklen_t slen = tousa(&c->rem, &usa);
n = recvfrom(FD(c), (char *) buf, len, 0, &usa.sa, &slen);
if (n > 0) tomgaddr(&usa, &c->peer, slen != sizeof(usa.sin));
if (n > 0) tomgaddr(&usa, &c->rem, slen != sizeof(usa.sin));
} else {
n = recv(FD(c), (char *) buf, len, MSG_NONBLOCKING);
}
@ -335,8 +335,8 @@ static void setsockopts(struct mg_connection *c) {
void mg_connect_resolved(struct mg_connection *c) {
// char buf[40];
int type = c->is_udp ? SOCK_DGRAM : SOCK_STREAM;
int rc, af = c->peer.is_ip6 ? AF_INET6 : AF_INET;
// mg_straddr(&c->peer, buf, sizeof(buf));
int rc, af = c->rem.is_ip6 ? AF_INET6 : AF_INET;
// mg_straddr(&c->rem, buf, sizeof(buf));
c->fd = S2PTR(socket(af, type, 0));
c->is_resolving = 0;
if (FD(c) == INVALID_SOCKET) {
@ -346,7 +346,7 @@ void mg_connect_resolved(struct mg_connection *c) {
mg_call(c, MG_EV_CONNECT, NULL);
} else {
union usa usa;
socklen_t slen = tousa(&c->peer, &usa);
socklen_t slen = tousa(&c->rem, &usa);
mg_set_non_blocking_mode(FD(c));
setsockopts(c);
mg_call(c, MG_EV_RESOLVE, NULL);
@ -383,8 +383,8 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
closesocket(fd);
} else {
char buf[40];
tomgaddr(&usa, &c->peer, sa_len != sizeof(usa.sin));
mg_straddr(&c->peer, buf, sizeof(buf));
tomgaddr(&usa, &c->rem, sa_len != sizeof(usa.sin));
mg_straddr(&c->rem, buf, sizeof(buf));
MG_DEBUG(("%lu accepted %s", c->id, buf));
mg_set_non_blocking_mode(FD(c));
setsockopts(c);
@ -392,6 +392,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) {
c->is_accepted = 1;
c->is_hexdumping = lsn->is_hexdumping;
c->pfn = lsn->pfn;
c->loc = lsn->loc;
c->pfn_data = lsn->pfn_data;
c->fn = lsn->fn;
c->fn_data = lsn->fn_data;
@ -454,7 +455,7 @@ struct mg_connection *mg_mkpipe(struct mg_mgr *mgr, mg_event_handler_t fn,
MG_ERROR(("OOM"));
} else {
MG_DEBUG(("pipe %lu", (unsigned long) sp[0]));
tomgaddr(&usa[0], &c->peer, false);
tomgaddr(&usa[0], &c->rem, false);
c->is_udp = 1;
c->pfn = pf1;
c->pfn_data = (void *) (size_t) sp[0];
@ -478,7 +479,7 @@ struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
MG_ERROR(("OOM %s", url));
closesocket(fd);
} else {
memcpy(&c->peer, &addr, sizeof(struct mg_addr));
memcpy(&c->loc, &addr, sizeof(struct mg_addr));
c->fd = S2PTR(fd);
c->is_listening = 1;
c->is_udp = is_udp;
@ -486,7 +487,7 @@ struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
c->fn = fn;
c->fn_data = fn_data;
mg_call(c, MG_EV_OPEN, NULL);
MG_DEBUG(("%lu %s port %u", c->id, url, mg_ntohs(c->peer.port)));
MG_DEBUG(("%lu %s port %u", c->id, url, mg_ntohs(c->rem.port)));
}
return c;
}
@ -551,7 +552,7 @@ static void connect_conn(struct mg_connection *c) {
if (rc) {
char buf[50];
mg_error(c, "error connecting to %s",
mg_straddr(&c->peer, buf, sizeof(buf)));
mg_straddr(&c->rem, buf, sizeof(buf)));
} else {
if (c->is_tls_hs) mg_tls_handshake(c);
mg_call(c, MG_EV_CONNECT, NULL);

View File

@ -797,7 +797,7 @@ static void f3(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_CONNECT) {
// c->is_hexdumping = 1;
mg_printf(c, "GET / HTTP/1.0\r\nHost: %s\r\n\r\n",
c->peer.is_ip6 ? "ipv6.google.com" : "cesanta.com");
c->rem.is_ip6 ? "ipv6.google.com" : "cesanta.com");
} else if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
// MG_INFO(("-->[%.*s]", (int) hm->message.len, hm->message.ptr));