diff --git a/docs/README.md b/docs/README.md index 2a7eadae..cae2da72 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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 diff --git a/examples/udp-ssdp-search/main.c b/examples/udp-ssdp-search/main.c index 18af08c6..90a0291f 100644 --- a/examples/udp-ssdp-search/main.c +++ b/examples/udp-ssdp-search/main.c @@ -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)); } } diff --git a/mongoose.c b/mongoose.c index acb70b72..70f1a66b 100644 --- a/mongoose.c +++ b/mongoose.c @@ -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); diff --git a/mongoose.h b/mongoose.h index 5ed05c7c..159519ef 100644 --- a/mongoose.h +++ b/mongoose.h @@ -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 diff --git a/src/dns.c b/src/dns.c index e7215210..b7f8ef6c 100644 --- a/src/dns.c +++ b/src/dns.c @@ -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 { diff --git a/src/net.h b/src/net.h index 4f45a174..b917fe76 100644 --- a/src/net.h +++ b/src/net.h @@ -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 diff --git a/src/sock.c b/src/sock.c index 8c954329..0c913d6a 100644 --- a/src/sock.c +++ b/src/sock.c @@ -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); diff --git a/test/unit_test.c b/test/unit_test.c index 529011eb..1326c713 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -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));