From d742b6787877f7b152672b75c4bd479e7d267d17 Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Fri, 25 Mar 2016 14:58:34 +0000 Subject: [PATCH] Fix address stringification on CC3200 (SimpleLink) PUBLISHED_FROM=b741acc999d0d2160528b4cb805f315b926ac65b --- mongoose.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/mongoose.c b/mongoose.c index 923f2162..86b045e3 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3863,17 +3863,16 @@ int mg_socketpair(sock_t sp[2], int sock_type) { } #endif /* MG_DISABLE_SOCKETPAIR */ +#ifndef MG_CC3200 static void mg_sock_get_addr(sock_t sock, int remote, union socket_address *sa) { socklen_t slen = sizeof(*sa); memset(sa, 0, slen); -#ifndef MG_CC3200 if (remote) { getpeername(sock, &sa->sa, &slen); } else { getsockname(sock, &sa->sa, &slen); } -#endif } void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags) { @@ -3881,10 +3880,18 @@ void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags) { mg_sock_get_addr(sock, flags & MG_SOCK_STRINGIFY_REMOTE, &sa); mg_sock_addr_to_str(&sa, buf, len, flags); } +#endif void mg_if_get_conn_addr(struct mg_connection *nc, int remote, union socket_address *sa) { +#ifndef MG_CC3200 mg_sock_get_addr(nc->sock, remote, sa); +#else + /* SimpleLink does not provide a way to get socket's peer address after + * accept or connect. Address hould have been preserved in the connection, + * so we do our best here by using it. */ + if (remote) memcpy(sa, &nc->sa, sizeof(*sa)); +#endif } #endif /* !MG_DISABLE_SOCKET_IF */ @@ -6667,13 +6674,8 @@ static int mg_http_send_port_based_redirect( struct mg_str a, b; char local_port[20] = {'%'}; -#ifndef MG_ESP8266 - mg_sock_to_str(c->sock, local_port + 1, sizeof(local_port) - 1, - MG_SOCK_STRINGIFY_PORT); -#else - /* TODO(lsm): remove when mg_sock_to_str() is implemented in LWIP codepath */ - snprintf(local_port, sizeof(local_port), "%s", "%0"); -#endif + mg_conn_addr_to_str(c, local_port + 1, sizeof(local_port) - 1, + MG_SOCK_STRINGIFY_PORT); while ((rewrites = mg_next_comma_list_entry(rewrites, &a, &b)) != NULL) { if (mg_vcmp(&a, local_port) == 0) {