Skip to content

Commit

Permalink
Fill c->loc for outbound connections
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Feb 14, 2024
1 parent d745c41 commit 5baa4d9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
21 changes: 10 additions & 11 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 10 additions & 11 deletions src/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion test/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 5baa4d9

Please sign in to comment.