common: Fix client IP address logging when IPv6 is enabled

g_tcp_accept() and g_sck_accept() should use sockadd_in6 when IPv6 is
enabled. The former code logs client IP address always "0.0.0.0" in such
case.

Fixes #412.
This commit is contained in:
Koichiro IWAO 2016-10-27 17:56:49 +09:00
parent 7c516d7d07
commit b2118450f4

View File

@ -1035,21 +1035,33 @@ g_sck_listen(int sck)
int APP_CC int APP_CC
g_tcp_accept(int sck) g_tcp_accept(int sck)
{ {
int ret ; int ret;
char ipAddr[256] ; char msg[256];
struct sockaddr_in s; #if defined(XRDP_ENABLE_IPV6)
socklen_t i; struct sockaddr_in6 sock;
#else
struct sockaddr_in sock;
#endif
socklen_t sock_len = sizeof(sock);
memset(&sock, 0, sock_len);
i = sizeof(struct sockaddr_in); ret = accept(sck, (struct sockaddr*) &sock, &sock_len);
memset(&s, 0, i);
ret = accept(sck, (struct sockaddr *)&s, &i); if (ret > 0)
if(ret>0)
{ {
snprintf(ipAddr, 255, "A connection received from: %s port %d", #if defined(XRDP_ENABLE_IPV6)
inet_ntoa(s.sin_addr), ntohs(s.sin_port)); char addr[256];
log_message(LOG_LEVEL_INFO, "%s", ipAddr); inet_ntop(AF_INET6, &sock.sin6_addr, addr, sizeof(addr));
snprintf(msg, sizeof(msg), "A connection received from %s port %d",
addr, ntohs(sock.sin6_port));
#else
snprintf(msg, sizeof(msg), "A connection received from %s port %d",
inet_ntoa(sock.sin_addr), ntohs(sock.sin_port));
#endif
log_message(LOG_LEVEL_INFO, "%s", msg);
} }
return ret ;
return ret;
} }
/*****************************************************************************/ /*****************************************************************************/
@ -1057,29 +1069,44 @@ int APP_CC
g_sck_accept(int sck, char *addr, int addr_bytes, char *port, int port_bytes) g_sck_accept(int sck, char *addr, int addr_bytes, char *port, int port_bytes)
{ {
int ret; int ret;
char ipAddr[256]; char msg[256];
struct sockaddr_in s; #if defined(XRDP_ENABLE_IPV6)
socklen_t i; struct sockaddr_in6 sock;
#else
struct sockaddr_in sock;
#endif
socklen_t sock_len = sizeof(sock);
memset(&sock, 0, sock_len);
ret = accept(sck, (struct sockaddr *)&sock, &sock_len);
i = sizeof(struct sockaddr_in);
memset(&s, 0, i);
ret = accept(sck, (struct sockaddr *)&s, &i);
if (ret > 0) if (ret > 0)
{ {
g_snprintf(ipAddr, 255, "A connection received from: %s port %d", #if defined(XRDP_ENABLE_IPV6)
inet_ntoa(s.sin_addr), ntohs(s.sin_port)); if (sock.sin6_family == AF_INET6)
log_message(LOG_LEVEL_INFO, "%s", ipAddr);
if (s.sin_family == AF_INET)
{ {
g_snprintf(addr, addr_bytes, "%s", inet_ntoa(s.sin_addr)); inet_ntop(AF_INET6, &sock.sin6_addr, addr, addr_bytes);
g_snprintf(port, port_bytes, "%d", ntohs(s.sin_port)); g_snprintf(port, port_bytes, "%d", ntohs(sock.sin6_port));
}
if (sock.sin6_family == AF_UNIX)
#else
if (sock.sin_family == AF_INET)
{
g_snprintf(addr, addr_bytes, "%s", inet_ntoa(sock.sin_addr));
g_snprintf(port, port_bytes, "%d", ntohs(sock.sin_port));
} }
if (s.sin_family == AF_UNIX) if (sock.sin_family == AF_UNIX)
#endif
{ {
g_strncpy(addr, "", addr_bytes - 1); g_strncpy(addr, "", addr_bytes - 1);
g_strncpy(port, "", port_bytes - 1); g_strncpy(port, "", port_bytes - 1);
} }
g_snprintf(msg, sizeof(msg), "A connection received from: %s port %s",
addr, port);
log_message(LOG_LEVEL_INFO, "%s", msg);
} }
return ret; return ret;
} }