From c68baddf6cf05e22dd25a563e35b1e4533ee7047 Mon Sep 17 00:00:00 2001
From: Peter Powell <petpow@saberuk.com>
Date: Sun, 19 May 2013 02:35:17 +0100
Subject: Rewrite irc::sockets::sockaddrs::str() completely.

- Replace use of MAXBUF with INET_ADDRSTRLEN/INET6_ADDRSTRLEN.
- Replace use of sprintf with InspIRCd::Format.
---
 src/socket.cpp | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

(limited to 'src')

diff --git a/src/socket.cpp b/src/socket.cpp
index 12fb632a7..9ec7544f2 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -221,24 +221,22 @@ static const char all_zero[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
 
 std::string irc::sockets::sockaddrs::str() const
 {
-	char buffer[MAXBUF];
 	if (sa.sa_family == AF_INET)
 	{
-		const uint8_t* bits = reinterpret_cast<const uint8_t*>(&in4.sin_addr);
-		sprintf(buffer, "%d.%d.%d.%d:%u", bits[0], bits[1], bits[2], bits[3], ntohs(in4.sin_port));
+		char ipaddr[INET_ADDRSTRLEN];
+		inet_ntop(AF_INET, &in4.sin_addr, ipaddr, sizeof(ipaddr));
+		return InspIRCd::Format("%s:%u", ipaddr, ntohs(in4.sin_port));
 	}
-	else if (sa.sa_family == AF_INET6)
+
+	if (sa.sa_family == AF_INET6)
 	{
-		buffer[0] = '[';
-		if (!inet_ntop(AF_INET6, &in6.sin6_addr, buffer+1, MAXBUF - 10))
-			return "<unknown>"; // should never happen, buffer is large enough
-		int len = strlen(buffer);
-		// no need for snprintf, buffer has at least 9 chars left, max short len = 5
-		sprintf(buffer + len, "]:%u", ntohs(in6.sin6_port));
+		char ipaddr[INET6_ADDRSTRLEN];
+		inet_ntop(AF_INET6, &in6.sin6_addr, ipaddr, sizeof(ipaddr));
+		return InspIRCd::Format("[%s]:%u", ipaddr, ntohs(in6.sin6_port));
 	}
-	else
-		return "<unknown>";
-	return std::string(buffer);
+
+	// This should never happen.
+	return "<unknown>";
 }
 
 int irc::sockets::sockaddrs::sa_size() const
-- 
cgit v1.2.3