summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>2009-09-02 00:48:16 +0000
committerdanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>2009-09-02 00:48:16 +0000
commitca749a589afc4f12de4bfb0f63591a49e3703372 (patch)
treefff42f1513ba7aaa7b6b1b385e74dbaeefd1e526 /src
parenta1f48c0d46d01415e9e93bfd8bfdba9d52ca795e (diff)
Add irc::sockets::satouser for user-readable IP/port pairs
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11627 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src')
-rw-r--r--src/socket.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/socket.cpp b/src/socket.cpp
index c4f8baee6..2eb8aeb7e 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -258,6 +258,43 @@ bool irc::sockets::satoap(const irc::sockets::sockaddrs* sa, std::string& addr,
return false;
}
+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::satouser(const irc::sockets::sockaddrs* sa) {
+ char buffer[MAXBUF];
+ if (sa->sa.sa_family == AF_INET)
+ {
+ if (sa->in4.sin_addr.s_addr == 0)
+ {
+ sprintf(buffer, "*:%u", ntohs(sa->in4.sin_port));
+ }
+ else
+ {
+ const uint8_t* bits = reinterpret_cast<const uint8_t*>(&sa->in4.sin_addr);
+ sprintf(buffer, "%d.%d.%d.%d:%u", bits[0], bits[1], bits[2], bits[3], ntohs(sa->in4.sin_port));
+ }
+ }
+ else if (sa->sa.sa_family == AF_INET6)
+ {
+ if (!memcmp(all_zero, &sa->in6.sin6_addr, 16))
+ {
+ sprintf(buffer, "*:%u", ntohs(sa->in6.sin6_port));
+ }
+ else
+ {
+ buffer[0] = '[';
+ if (!inet_ntop(AF_INET6, &sa->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(sa->in6.sin6_port));
+ }
+ }
+ else
+ return "<unknown>";
+ return std::string(buffer);
+}
+
int irc::sockets::sa_size(irc::sockets::sockaddrs& sa)
{
if (sa.sa.sa_family == AF_INET)