# HG changeset patch # User jbe # Date 1428462739 -7200 # Node ID baa30bfec659d7ed5721e4ba1b89b8252fdfee89 # Parent df1ab25c6513e3e9dade00251e99a862622e6fdc Convert local and remote IP address to human readable string diff -r df1ab25c6513 -r baa30bfec659 moonbridge.c --- a/moonbridge.c Wed Apr 08 05:11:58 2015 +0200 +++ b/moonbridge.c Wed Apr 08 05:12:19 2015 +0200 @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -855,39 +856,63 @@ lua_pushvalue(L, -2); if (listener->proto == MOONBR_PROTO_TCP6) { struct sockaddr_in6 addr; + char addrstrbuf[INET6_ADDRSTRLEN]; + const char *addrstr; socklen_t addr_len = sizeof(struct sockaddr_in6); if (getsockname(fd, (struct sockaddr *)&addr, &addr_len)) { moonbr_child_log_errno("Could not get local IP address/port"); } else { - lua_pushlstring(L, (char *)addr.sin6_addr.s6_addr, 16); - lua_setfield(L, -2, "local_ip6"); + addrstr = inet_ntop(AF_INET6, addr.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf)); + if (!addrstr) { + moonbr_child_log_errno("Could not format local IP address"); + } else { + lua_pushstring(L, addrstr); + lua_setfield(L, -2, "local_ip6"); + } lua_pushinteger(L, ntohs(addr.sin6_port)); lua_setfield(L, -2, "local_tcpport"); } if (getpeername(fd, (struct sockaddr *)&addr, &addr_len)) { moonbr_child_log_errno("Could not get remote IP address/port"); } else { - lua_pushlstring(L, (char *)addr.sin6_addr.s6_addr, 16); - lua_setfield(L, -2, "remote_ip6"); + addrstr = inet_ntop(AF_INET6, addr.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf)); + if (!addrstr) { + moonbr_child_log_errno("Could not format remote IP address"); + } else { + lua_pushstring(L, addrstr); + lua_setfield(L, -2, "remote_ip6"); + } lua_pushinteger(L, ntohs(addr.sin6_port)); lua_setfield(L, -2, "remote_tcpport"); } } else if (listener->proto == MOONBR_PROTO_TCP4) { struct sockaddr_in addr; + char addrstrbuf[INET_ADDRSTRLEN]; + const char *addrstr; socklen_t addr_len = sizeof(struct sockaddr_in); if (getsockname(fd, (struct sockaddr *)&addr, &addr_len)) { moonbr_child_log_errno("Could not get local IP address/port"); } else { - lua_pushlstring(L, (char *)&addr.sin_addr.s_addr, 4); - lua_setfield(L, -2, "local_ip4"); + addrstr = inet_ntop(AF_INET, &addr.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf)); + if (!addrstr) { + moonbr_child_log_errno("Could not format local IP address"); + } else { + lua_pushstring(L, addrstr); + lua_setfield(L, -2, "local_ip4"); + } lua_pushinteger(L, ntohs(addr.sin_port)); lua_setfield(L, -2, "local_tcpport"); } if (getpeername(fd, (struct sockaddr *)&addr, &addr_len)) { moonbr_child_log_errno("Could not get remote IP address/port"); } else { - lua_pushlstring(L, (char *)&addr.sin_addr.s_addr, 4); - lua_setfield(L, -2, "remote_ip4"); + addrstr = inet_ntop(AF_INET, &addr.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf)); + if (!addrstr) { + moonbr_child_log_errno("Could not format remote IP address"); + } else { + lua_pushstring(L, addrstr); + lua_setfield(L, -2, "remote_ip4"); + } lua_pushinteger(L, ntohs(addr.sin_port)); lua_setfield(L, -2, "remote_tcpport"); }