Merge pull request #2609 from cesanta/loc

Fill c->loc for outbound connections
This commit is contained in:
Sergio R. Caprile 2024-02-14 09:51:10 -03:00 committed by GitHub
commit 2c2c7f6894
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 23 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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);
}