diff --git a/docs/README.md b/docs/README.md index c3fbffb0..cdf67761 100644 --- a/docs/README.md +++ b/docs/README.md @@ -628,13 +628,13 @@ void foo(struct mg_connection *c, const char *fmt, ...) { ### mg\_straddr ```c -char *mg_straddr(struct mg_connection *c, char *buf, size_t len); +char *mg_straddr(struct mg_addr *addr, char *buf, size_t len); ``` Write stringified IP address, associated with given connection to `buf` (maximum size `len`) Parameters: -- `c` - A connection pointer +- `addr` - A address pointer - `buf` - A pointer to a buffer that will hold stringified address - `len` - A buffer size @@ -643,8 +643,8 @@ Return value: `buf` value Usage example: ```c -char buf[1024]; -mg_straddr(c, buf, sizeof(buf)); // `buf` is now IP address string, like "127.0.0.1:8080" +char buf[100]; +LOG(LL_INFO, ("%s", mg_straddr(&c->peer, buf, sizeof(buf)))); ``` ### mg\_mkpipe() diff --git a/mongoose.c b/mongoose.c index ad8ecd0b..c32b831a 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2375,7 +2375,7 @@ int mg_printf(struct mg_connection *c, const char *fmt, ...) { return len; } -char *mg_addr_to_str(struct mg_addr *a, char *buf, size_t len) { +char *mg_straddr(struct mg_addr *a, char *buf, size_t len) { char tmp[30]; const char *fmt = a->is_ip6 ? "[%s]:%d" : "%s:%d"; mg_ntoa(a, tmp, sizeof(tmp)); @@ -2383,10 +2383,6 @@ char *mg_addr_to_str(struct mg_addr *a, char *buf, size_t len) { return buf; } -char *mg_straddr(struct mg_connection *c, char *buf, size_t len) { - return mg_addr_to_str(&c->peer, buf, len); -} - char *mg_ntoa(const struct mg_addr *addr, char *buf, size_t len) { if (addr->is_ip6) { uint16_t *p = (uint16_t *) addr->ip6; @@ -2985,8 +2981,8 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) { getsockname(FD(c), &usa.sa, &slen); tomgaddr(&usa, &a, c->peer.is_ip6); LOG(LL_INFO, ("\n-- %lu %s %s %s %s %ld\n%s", c->id, - mg_addr_to_str(&a, t1, sizeof(t1)), r ? "<-" : "->", - mg_addr_to_str(&c->peer, t2, sizeof(t2)), c->label, n, s)); + mg_straddr(&a, t1, sizeof(t1)), r ? "<-" : "->", + mg_straddr(&c->peer, t2, sizeof(t2)), c->label, n, s)); free(s); (void) t1, (void) t2; // Silence warnings for MG_ENABLE_LOG=0 } @@ -3200,10 +3196,10 @@ static void setsockopts(struct mg_connection *c) { } void mg_connect_resolved(struct mg_connection *c) { - char buf[40]; + // 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, buf, sizeof(buf)); + // mg_straddr(&c->peer, buf, sizeof(buf)); c->fd = S2PTR(socket(af, type, 0)); if (FD(c) == INVALID_SOCKET) { mg_error(c, "socket(): %d", MG_SOCK_ERRNO); @@ -3266,7 +3262,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { } else { char buf[40]; tomgaddr(&usa, &c->peer, sa_len != sizeof(usa.sin)); - mg_straddr(c, buf, sizeof(buf)); + mg_straddr(&c->peer, buf, sizeof(buf)); LOG(LL_DEBUG, ("%lu accepted %s", c->id, buf)); mg_set_non_blocking_mode(FD(c)); setsockopts(c); @@ -3428,8 +3424,9 @@ static void connect_conn(struct mg_connection *c) { if (rc == EAGAIN || rc == EWOULDBLOCK) rc = 0; c->is_connecting = 0; if (rc) { - char buf[40]; - mg_error(c, "error connecting to %s", mg_straddr(c, buf, sizeof(buf))); + char buf[50]; + mg_error(c, "error connecting to %s", + mg_straddr(&c->peer, 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 3343d7ff..1a95babb 100644 --- a/mongoose.h +++ b/mongoose.h @@ -860,8 +860,7 @@ void mg_connect_resolved(struct mg_connection *); bool mg_send(struct mg_connection *, const void *, size_t); int mg_printf(struct mg_connection *, const char *fmt, ...); int mg_vprintf(struct mg_connection *, const char *fmt, va_list ap); -char *mg_straddr(struct mg_connection *, char *, size_t); -char *mg_addr_to_str(struct mg_addr *, char *, size_t); +char *mg_straddr(struct mg_addr *, char *, size_t); bool mg_aton(struct mg_str str, struct mg_addr *addr); char *mg_ntoa(const struct mg_addr *addr, char *buf, size_t len); diff --git a/src/net.c b/src/net.c index b3b7cf77..82f806c8 100644 --- a/src/net.c +++ b/src/net.c @@ -19,7 +19,7 @@ int mg_printf(struct mg_connection *c, const char *fmt, ...) { return len; } -char *mg_addr_to_str(struct mg_addr *a, char *buf, size_t len) { +char *mg_straddr(struct mg_addr *a, char *buf, size_t len) { char tmp[30]; const char *fmt = a->is_ip6 ? "[%s]:%d" : "%s:%d"; mg_ntoa(a, tmp, sizeof(tmp)); @@ -27,10 +27,6 @@ char *mg_addr_to_str(struct mg_addr *a, char *buf, size_t len) { return buf; } -char *mg_straddr(struct mg_connection *c, char *buf, size_t len) { - return mg_addr_to_str(&c->peer, buf, len); -} - char *mg_ntoa(const struct mg_addr *addr, char *buf, size_t len) { if (addr->is_ip6) { uint16_t *p = (uint16_t *) addr->ip6; diff --git a/src/net.h b/src/net.h index 985679f0..55d582f2 100644 --- a/src/net.h +++ b/src/net.h @@ -71,8 +71,7 @@ void mg_connect_resolved(struct mg_connection *); bool mg_send(struct mg_connection *, const void *, size_t); int mg_printf(struct mg_connection *, const char *fmt, ...); int mg_vprintf(struct mg_connection *, const char *fmt, va_list ap); -char *mg_straddr(struct mg_connection *, char *, size_t); -char *mg_addr_to_str(struct mg_addr *, char *, size_t); +char *mg_straddr(struct mg_addr *, char *, size_t); bool mg_aton(struct mg_str str, struct mg_addr *addr); char *mg_ntoa(const struct mg_addr *addr, char *buf, size_t len); diff --git a/src/sock.c b/src/sock.c index b0dc13cf..a61de515 100644 --- a/src/sock.c +++ b/src/sock.c @@ -122,8 +122,8 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) { getsockname(FD(c), &usa.sa, &slen); tomgaddr(&usa, &a, c->peer.is_ip6); LOG(LL_INFO, ("\n-- %lu %s %s %s %s %ld\n%s", c->id, - mg_addr_to_str(&a, t1, sizeof(t1)), r ? "<-" : "->", - mg_addr_to_str(&c->peer, t2, sizeof(t2)), c->label, n, s)); + mg_straddr(&a, t1, sizeof(t1)), r ? "<-" : "->", + mg_straddr(&c->peer, t2, sizeof(t2)), c->label, n, s)); free(s); (void) t1, (void) t2; // Silence warnings for MG_ENABLE_LOG=0 } @@ -337,10 +337,10 @@ static void setsockopts(struct mg_connection *c) { } void mg_connect_resolved(struct mg_connection *c) { - char buf[40]; + // 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, buf, sizeof(buf)); + // mg_straddr(&c->peer, buf, sizeof(buf)); c->fd = S2PTR(socket(af, type, 0)); if (FD(c) == INVALID_SOCKET) { mg_error(c, "socket(): %d", MG_SOCK_ERRNO); @@ -403,7 +403,7 @@ static void accept_conn(struct mg_mgr *mgr, struct mg_connection *lsn) { } else { char buf[40]; tomgaddr(&usa, &c->peer, sa_len != sizeof(usa.sin)); - mg_straddr(c, buf, sizeof(buf)); + mg_straddr(&c->peer, buf, sizeof(buf)); LOG(LL_DEBUG, ("%lu accepted %s", c->id, buf)); mg_set_non_blocking_mode(FD(c)); setsockopts(c); @@ -565,8 +565,9 @@ static void connect_conn(struct mg_connection *c) { if (rc == EAGAIN || rc == EWOULDBLOCK) rc = 0; c->is_connecting = 0; if (rc) { - char buf[40]; - mg_error(c, "error connecting to %s", mg_straddr(c, buf, sizeof(buf))); + char buf[50]; + mg_error(c, "error connecting to %s", + mg_straddr(&c->peer, buf, sizeof(buf))); } else { if (c->is_tls_hs) mg_tls_handshake(c); mg_call(c, MG_EV_CONNECT, NULL);