diff options
author | Koichiro IWAO <meta@vmeta.jp> | 2017-09-19 13:51:23 +0900 |
---|---|---|
committer | metalefty <meta@vmeta.jp> | 2017-09-26 10:54:08 +0900 |
commit | f9ab4df7f2d99c2381ff0228a82c5b5bafbcb29b (patch) | |
tree | 00075f2e03664f2c61f9426019966604ed799442 /common/os_calls.c | |
parent | 8ded88a0c62d0f9e796d1d640f5c08355cf9b06b (diff) | |
download | xrdp-proprietary-f9ab4df7f2d99c2381ff0228a82c5b5bafbcb29b.tar.gz xrdp-proprietary-f9ab4df7f2d99c2381ff0228a82c5b5bafbcb29b.zip |
common: fix g_write_ip_address() didn't return correct IP address
Fixes: #878.
Diffstat (limited to 'common/os_calls.c')
-rw-r--r-- | common/os_calls.c | 72 |
1 files changed, 58 insertions, 14 deletions
diff --git a/common/os_calls.c b/common/os_calls.c index 60ccda17..642ba4a0 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -1293,30 +1293,72 @@ g_sck_accept(int sck, char *addr, int addr_bytes, char *port, int port_bytes) } /*****************************************************************************/ +/* + * TODO: this function writes not only IP address, name is confusing + */ void g_write_ip_address(int rcv_sck, char *ip_address, int bytes) { - struct sockaddr_in s; - struct in_addr in; - socklen_t len; - int ip_port; + char *addr; + int port; int ok; + union + { + struct sockaddr sock_addr; + struct sockaddr_in sock_addr_in; +#if defined(XRDP_ENABLE_IPV6) + struct sockaddr_in6 sock_addr_in6; +#endif + struct sockaddr_un sock_addr_un; + } sock_info; + ok = 0; - memset(&s, 0, sizeof(s)); - len = sizeof(s); + socklen_t sock_len = sizeof(sock_info); + memset(&sock_info, 0, sock_len); +#if defined(XRDP_ENABLE_IPV6) + addr = (char *)g_malloc(INET6_ADDRSTRLEN, 1); +#else + addr = (char *)g_malloc(INET_ADDRSTRLEN, 1); +#endif - if (getpeername(rcv_sck, (struct sockaddr *)&s, &len) == 0) + if (getpeername(rcv_sck, (struct sockaddr *)&sock_info, &sock_len) == 0) { - memset(&in, 0, sizeof(in)); - in.s_addr = s.sin_addr.s_addr; - ip_port = ntohs(s.sin_port); + switch(sock_info.sock_addr.sa_family) + { + case AF_INET: + { + struct sockaddr_in *sock_addr_in = &sock_info.sock_addr_in; + g_snprintf(addr, INET_ADDRSTRLEN, "%s", inet_ntoa(sock_addr_in->sin_addr)); + port = ntohs(sock_addr_in->sin_port); + ok = 1; + break; + } + +#if defined(XRDP_ENABLE_IPV6) + + case AF_INET6: + { + struct sockaddr_in6 *sock_addr_in6 = &sock_info.sock_addr_in6; + inet_ntop(sock_addr_in6->sin6_family, + &sock_addr_in6->sin6_addr, addr, INET6_ADDRSTRLEN); + port = ntohs(sock_addr_in6->sin6_port); + ok = 1; + break; + } - if (ip_port != 0) +#endif + + default: + { + break; + } + + } + + if (ok) { - ok = 1; - snprintf(ip_address, bytes, "%s:%d - socket: %d", inet_ntoa(in), - ip_port, rcv_sck); + snprintf(ip_address, bytes, "%s:%d - socket: %d", addr, port, rcv_sck); } } @@ -1324,6 +1366,8 @@ g_write_ip_address(int rcv_sck, char *ip_address, int bytes) { snprintf(ip_address, bytes, "NULL:NULL - socket: %d", rcv_sck); } + + g_free(addr); } /*****************************************************************************/ |