summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTingPing <tngpng@gmail.com>2013-06-09 10:42:42 -0300
committerTingPing <tngpng@gmail.com>2013-06-09 10:42:42 -0300
commit08cd36bef0159e3a0482a1fcd4ea7d4f2ea2e9b3 (patch)
treeb94501e9036fb240d61b4b29a4efd5ba4d1ea639
parent5d0599976ef3393d9ea8767cae9dcba7da32df1e (diff)
Fix printing ipv6 addresses with identd
Closes #639
-rw-r--r--src/common/identd.c28
1 files changed, 7 insertions, 21 deletions
diff --git a/src/common/identd.c b/src/common/identd.c
index 6dde6a5f..c4050929 100644
--- a/src/common/identd.c
+++ b/src/common/identd.c
@@ -36,6 +36,7 @@ identd (char *username)
 	char *p;
 	char buf[256];
 	char outbuf[256];
+	char ipbuf[INET_ADDRSTRLEN];
 	struct sockaddr_in addr;
 
 	sok = socket (AF_INET, SOCK_STREAM, 0);
@@ -80,7 +81,8 @@ identd (char *username)
 #if 0	/* causes random crashes, probably due to CreateThread */
 	EMIT_SIGNAL (XP_TE_IDENTD, current_sess, inet_ntoa (addr.sin_addr), username, NULL, NULL, 0);
 #endif
-	snprintf (outbuf, sizeof (outbuf), "*\tServicing ident request from %s as %s\n", inet_ntoa (addr.sin_addr), username);
+	inet_ntop (AF_INET, &addr.sin_addr, ipbuf, sizeof (ipbuf));
+	snprintf (outbuf, sizeof (outbuf), "*\tServicing ident request from %s as %s\n", ipbuf, username);
 	PrintText (current_sess, outbuf);
 
 	recv (read_sok, buf, sizeof (buf) - 1, 0);
@@ -103,23 +105,20 @@ identd (char *username)
 }
 
 #ifdef USE_IPV6
-#define IPV6BUFLEN 60
 static int
 identd_ipv6 (char *username)
 {
 	int sok, read_sok, len;
 	char *p;
 	char buf[256];
-	char outbuf[256];	
-	LPSTR ipv6buf = (LPSTR) malloc (IPV6BUFLEN);
+	char outbuf[256];
+	char ipbuf[INET6_ADDRSTRLEN];
 	struct sockaddr_in6 addr;
 
 	sok = socket (AF_INET6, SOCK_STREAM, 0);
-
 	if (sok == INVALID_SOCKET)
 	{
 		free (username);
-		free (ipv6buf);
 		return 0;
 	}
 
@@ -134,7 +133,6 @@ identd_ipv6 (char *username)
 	{
 		closesocket (sok);
 		free (username);
-		free (ipv6buf);
 		return 0;
 	}
 
@@ -142,39 +140,28 @@ identd_ipv6 (char *username)
 	{
 		closesocket (sok);
 		free (username);
-		free (ipv6buf);
 		return 0;
 	}
 
 	len = sizeof (addr);
 	read_sok = accept (sok, (struct sockaddr *) &addr, &len);
 	closesocket (sok);
-
 	if (read_sok == INVALID_SOCKET)
 	{
 		free (username);
-		free (ipv6buf);
 		return 0;
 	}
 
 	identd_ipv6_is_running = FALSE;
 
-	if (WSAAddressToString ((struct sockaddr *) &addr, sizeof (addr), NULL, ipv6buf, (LPDWORD) IPV6BUFLEN) == SOCKET_ERROR)
-	{
-		snprintf (ipv6buf, sizeof (ipv6buf) - 1, "[SOCKET ERROR: 0x%X]", WSAGetLastError ());
-	}
-
-#if 0	/* causes random crashes, probably due to CreateThread */
-	EMIT_SIGNAL (XP_TE_IDENTD, current_sess, ipv6buf, username, NULL, NULL, 0);
-#endif
-	snprintf (outbuf, sizeof (outbuf), "*\tServicing ident request from %s as %s\n", ipv6buf, username);
+	inet_ntop (AF_INET6, &addr.sin6_addr, ipbuf, sizeof (ipbuf));
+	snprintf (outbuf, sizeof (outbuf), "*\tServicing ident request from %s as %s\n", ipbuf, username);
 	PrintText (current_sess, outbuf);
 
 	recv (read_sok, buf, sizeof (buf) - 1, 0);
 	buf[sizeof (buf) - 1] = 0;	  /* ensure null termination */
 
 	p = strchr (buf, ',');
-
 	if (p)
 	{
 		snprintf (outbuf, sizeof (outbuf) - 1, "%d, %d : USERID : UNIX : %s\r\n", atoi (buf), atoi (p + 1), username);
@@ -185,7 +172,6 @@ identd_ipv6 (char *username)
 	sleep (1);
 	closesocket (read_sok);
 	free (username);
-	free (ipv6buf);
 
 	return 0;
 }