diff --git a/mongoose.c b/mongoose.c index f2bc4bbedd..7cc14b4774 100644 --- a/mongoose.c +++ b/mongoose.c @@ -7160,12 +7160,8 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) { c->is_closing = 1; // Termination. Don't call mg_error(): #1529 } else if (n > 0) { if (c->is_hexdumping) { - union usa usa; - socklen_t slen = sizeof(usa.sin); - if (getsockname(FD(c), &usa.sa, &slen) < 0) (void) 0; // Ignore result MG_INFO(("\n-- %lu %M %s %M %ld", c->id, mg_print_ip_port, &c->loc, r ? "<-" : "->", mg_print_ip_port, &c->rem, n)); - mg_hexdump(buf, (size_t) n); } if (r) { @@ -7393,6 +7389,7 @@ static void connect_conn(struct mg_connection *c) { // Use getpeername() to test whether we have connected if (getpeername(FD(c), &usa.sa, &n) == 0) { c->is_connecting = 0; + setlocaddr(FD(c), &c->loc); mg_call(c, MG_EV_CONNECT, NULL); MG_EPOLL_MOD(c, 0); if (c->is_tls_hs) mg_tls_handshake(c); @@ -7433,6 +7430,7 @@ void mg_connect_resolved(struct mg_connection *c) { if ((rc = bind(c->fd, &usa.sa, slen)) != 0) MG_ERROR(("bind: %d", MG_SOCK_ERR(rc))); #endif + setlocaddr(FD(c), &c->loc); mg_call(c, MG_EV_RESOLVE, NULL); mg_call(c, MG_EV_CONNECT, NULL); } else { @@ -7444,8 +7442,9 @@ void mg_connect_resolved(struct mg_connection *c) { mg_call(c, MG_EV_RESOLVE, NULL); rc = connect(FD(c), &usa.sa, slen); // Attempt to connect if (rc == 0) { // Success - mg_call(c, MG_EV_CONNECT, NULL); // Send MG_EV_CONNECT to the user - } else if (MG_SOCK_PENDING(rc)) { // Need to wait for TCP handshake + setlocaddr(FD(c), &c->loc); + mg_call(c, MG_EV_CONNECT, NULL); // Send MG_EV_CONNECT to the user + } else if (MG_SOCK_PENDING(rc)) { // Need to wait for TCP handshake MG_DEBUG(("%lu %ld -> %M pend", c->id, c->fd, mg_print_ip_port, &c->rem)); c->is_connecting = 1; } else { @@ -7757,11 +7756,11 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) { long n = 0; mg_call(c, MG_EV_READ, &n); } - MG_VERBOSE(("%lu %c%c %c%c%c%c%c %lu %lu", c->id, c->is_readable ? 'r' : '-', - c->is_writable ? 'w' : '-', c->is_tls ? 'T' : 't', - c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h', - c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c', - mg_tls_pending(c), c->rtls.len)); + MG_VERBOSE(("%lu %c%c %c%c%c%c%c %lu %lu", c->id, + c->is_readable ? 'r' : '-', c->is_writable ? 'w' : '-', + c->is_tls ? 'T' : 't', c->is_connecting ? 'C' : 'c', + c->is_tls_hs ? 'H' : 'h', c->is_resolving ? 'R' : 'r', + c->is_closing ? 'C' : 'c', mg_tls_pending(c), c->rtls.len)); if (c->is_resolving || c->is_closing) { // Do nothing } else if (c->is_listening && c->is_udp == 0) { diff --git a/src/sock.c b/src/sock.c index fdb52d2609..3aa80f9fe8 100644 --- a/src/sock.c +++ b/src/sock.c @@ -98,12 +98,8 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) { c->is_closing = 1; // Termination. Don't call mg_error(): #1529 } else if (n > 0) { if (c->is_hexdumping) { - union usa usa; - socklen_t slen = sizeof(usa.sin); - if (getsockname(FD(c), &usa.sa, &slen) < 0) (void) 0; // Ignore result MG_INFO(("\n-- %lu %M %s %M %ld", c->id, mg_print_ip_port, &c->loc, r ? "<-" : "->", mg_print_ip_port, &c->rem, n)); - mg_hexdump(buf, (size_t) n); } if (r) { @@ -331,6 +327,7 @@ static void connect_conn(struct mg_connection *c) { // Use getpeername() to test whether we have connected if (getpeername(FD(c), &usa.sa, &n) == 0) { c->is_connecting = 0; + setlocaddr(FD(c), &c->loc); mg_call(c, MG_EV_CONNECT, NULL); MG_EPOLL_MOD(c, 0); if (c->is_tls_hs) mg_tls_handshake(c); @@ -371,6 +368,7 @@ void mg_connect_resolved(struct mg_connection *c) { if ((rc = bind(c->fd, &usa.sa, slen)) != 0) MG_ERROR(("bind: %d", MG_SOCK_ERR(rc))); #endif + setlocaddr(FD(c), &c->loc); mg_call(c, MG_EV_RESOLVE, NULL); mg_call(c, MG_EV_CONNECT, NULL); } else { @@ -382,8 +380,9 @@ void mg_connect_resolved(struct mg_connection *c) { mg_call(c, MG_EV_RESOLVE, NULL); rc = connect(FD(c), &usa.sa, slen); // Attempt to connect if (rc == 0) { // Success - mg_call(c, MG_EV_CONNECT, NULL); // Send MG_EV_CONNECT to the user - } else if (MG_SOCK_PENDING(rc)) { // Need to wait for TCP handshake + setlocaddr(FD(c), &c->loc); + mg_call(c, MG_EV_CONNECT, NULL); // Send MG_EV_CONNECT to the user + } else if (MG_SOCK_PENDING(rc)) { // Need to wait for TCP handshake MG_DEBUG(("%lu %ld -> %M pend", c->id, c->fd, mg_print_ip_port, &c->rem)); c->is_connecting = 1; } else { @@ -695,11 +694,11 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) { long n = 0; mg_call(c, MG_EV_READ, &n); } - MG_VERBOSE(("%lu %c%c %c%c%c%c%c %lu %lu", c->id, c->is_readable ? 'r' : '-', - c->is_writable ? 'w' : '-', c->is_tls ? 'T' : 't', - c->is_connecting ? 'C' : 'c', c->is_tls_hs ? 'H' : 'h', - c->is_resolving ? 'R' : 'r', c->is_closing ? 'C' : 'c', - mg_tls_pending(c), c->rtls.len)); + MG_VERBOSE(("%lu %c%c %c%c%c%c%c %lu %lu", c->id, + c->is_readable ? 'r' : '-', c->is_writable ? 'w' : '-', + c->is_tls ? 'T' : 't', c->is_connecting ? 'C' : 'c', + c->is_tls_hs ? 'H' : 'h', c->is_resolving ? 'R' : 'r', + c->is_closing ? 'C' : 'c', mg_tls_pending(c), c->rtls.len)); if (c->is_resolving || c->is_closing) { // Do nothing } else if (c->is_listening && c->is_udp == 0) { diff --git a/test/unit_test.c b/test/unit_test.c index f17ba432ed..4eefc0ec5c 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -1249,6 +1249,7 @@ static void test_http_client(void) { MG_INFO(("%d", ok)); ASSERT(ok == 301); c->is_closing = 1; + ASSERT((c->loc.ip[0] != 0)); // Make sure that c->loc address is populated mg_mgr_poll(&mgr, 0); ok = 0; #if MG_TLS @@ -1402,7 +1403,7 @@ static void test_http_pipeline(void) { for (i = 0; i < 20; i++) mg_mgr_poll(&mgr, 1); ASSERT(ok == 1); ASSERT(ok2 == 1); - //MG_INFO(("-----> [%d] [%d]", ok, ok2)); + // MG_INFO(("-----> [%d] [%d]", ok, ok2)); mg_mgr_free(&mgr); ASSERT(mgr.conns == NULL); }