moonbridge

changeset 101:baa30bfec659

Convert local and remote IP address to human readable string
author jbe
date Wed Apr 08 05:12:19 2015 +0200 (2015-04-08)
parents df1ab25c6513
children 51ff6ad11677
files moonbridge.c
line diff
     1.1 --- a/moonbridge.c	Wed Apr 08 05:11:58 2015 +0200
     1.2 +++ b/moonbridge.c	Wed Apr 08 05:12:19 2015 +0200
     1.3 @@ -33,6 +33,7 @@
     1.4  #include <sys/socket.h>
     1.5  #include <sys/un.h>
     1.6  #include <netinet/in.h>
     1.7 +#include <arpa/inet.h>
     1.8  #include <poll.h>
     1.9  #include <signal.h>
    1.10  #include <sys/wait.h>
    1.11 @@ -855,39 +856,63 @@
    1.12        lua_pushvalue(L, -2);
    1.13        if (listener->proto == MOONBR_PROTO_TCP6) {
    1.14          struct sockaddr_in6 addr;
    1.15 +        char addrstrbuf[INET6_ADDRSTRLEN];
    1.16 +        const char *addrstr;
    1.17          socklen_t addr_len = sizeof(struct sockaddr_in6);
    1.18          if (getsockname(fd, (struct sockaddr *)&addr, &addr_len)) {
    1.19            moonbr_child_log_errno("Could not get local IP address/port");
    1.20          } else {
    1.21 -          lua_pushlstring(L, (char *)addr.sin6_addr.s6_addr, 16);
    1.22 -          lua_setfield(L, -2, "local_ip6");
    1.23 +          addrstr = inet_ntop(AF_INET6, addr.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
    1.24 +          if (!addrstr) {
    1.25 +            moonbr_child_log_errno("Could not format local IP address");
    1.26 +          } else {
    1.27 +            lua_pushstring(L, addrstr);
    1.28 +            lua_setfield(L, -2, "local_ip6");
    1.29 +          }
    1.30            lua_pushinteger(L, ntohs(addr.sin6_port));
    1.31            lua_setfield(L, -2, "local_tcpport");
    1.32          }
    1.33          if (getpeername(fd, (struct sockaddr *)&addr, &addr_len)) {
    1.34            moonbr_child_log_errno("Could not get remote IP address/port");
    1.35          } else {
    1.36 -          lua_pushlstring(L, (char *)addr.sin6_addr.s6_addr, 16);
    1.37 -          lua_setfield(L, -2, "remote_ip6");
    1.38 +          addrstr = inet_ntop(AF_INET6, addr.sin6_addr.s6_addr, addrstrbuf, sizeof(addrstrbuf));
    1.39 +          if (!addrstr) {
    1.40 +            moonbr_child_log_errno("Could not format remote IP address");
    1.41 +          } else {
    1.42 +            lua_pushstring(L, addrstr);
    1.43 +            lua_setfield(L, -2, "remote_ip6");
    1.44 +          }
    1.45            lua_pushinteger(L, ntohs(addr.sin6_port));
    1.46            lua_setfield(L, -2, "remote_tcpport");
    1.47          }
    1.48        } else if (listener->proto == MOONBR_PROTO_TCP4) {
    1.49          struct sockaddr_in addr;
    1.50 +        char addrstrbuf[INET_ADDRSTRLEN];
    1.51 +        const char *addrstr;
    1.52          socklen_t addr_len = sizeof(struct sockaddr_in);
    1.53          if (getsockname(fd, (struct sockaddr *)&addr, &addr_len)) {
    1.54            moonbr_child_log_errno("Could not get local IP address/port");
    1.55          } else {
    1.56 -          lua_pushlstring(L, (char *)&addr.sin_addr.s_addr, 4);
    1.57 -          lua_setfield(L, -2, "local_ip4");
    1.58 +          addrstr = inet_ntop(AF_INET, &addr.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
    1.59 +          if (!addrstr) {
    1.60 +            moonbr_child_log_errno("Could not format local IP address");
    1.61 +          } else {
    1.62 +            lua_pushstring(L, addrstr);
    1.63 +            lua_setfield(L, -2, "local_ip4");
    1.64 +          }
    1.65            lua_pushinteger(L, ntohs(addr.sin_port));
    1.66            lua_setfield(L, -2, "local_tcpport");
    1.67          }
    1.68          if (getpeername(fd, (struct sockaddr *)&addr, &addr_len)) {
    1.69            moonbr_child_log_errno("Could not get remote IP address/port");
    1.70          } else {
    1.71 -          lua_pushlstring(L, (char *)&addr.sin_addr.s_addr, 4);
    1.72 -          lua_setfield(L, -2, "remote_ip4");
    1.73 +          addrstr = inet_ntop(AF_INET, &addr.sin_addr.s_addr, addrstrbuf, sizeof(addrstrbuf));
    1.74 +          if (!addrstr) {
    1.75 +            moonbr_child_log_errno("Could not format remote IP address");
    1.76 +          } else {
    1.77 +            lua_pushstring(L, addrstr);
    1.78 +            lua_setfield(L, -2, "remote_ip4");
    1.79 +          }
    1.80            lua_pushinteger(L, ntohs(addr.sin_port));
    1.81            lua_setfield(L, -2, "remote_tcpport");
    1.82          }

Impressum / About Us