From 3976433cc7758726b29313b9fa0996551b2480ec Mon Sep 17 00:00:00 2001 From: Thorvald Natvig Date: Mon, 22 Jun 2009 20:01:27 +0200 Subject: Multibind for nix --- src/Net.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'src/Net.cpp') diff --git a/src/Net.cpp b/src/Net.cpp index 43dc69bbc..fb90f767f 100644 --- a/src/Net.cpp +++ b/src/Net.cpp @@ -66,6 +66,21 @@ HostAddress::HostAddress(const QHostAddress &address) { } } +HostAddress::HostAddress(const sockaddr_storage &address) { + if (address.ss_family == AF_INET) { + const struct sockaddr_in *in = reinterpret_cast(&address); + addr[0] = 0ULL; + shorts[4] = 0; + shorts[5] = 0xffff; + hash[3] = in->sin_addr.s_addr; + } else if (address.ss_family == AF_INET6) { + const struct sockaddr_in6 *in6 = reinterpret_cast(&address); + memcpy(qip6.c, in6->sin6_addr.s6_addr, 16); + } else { + addr[0] = addr[1] = 0ULL; + } +} + bool HostAddress::operator < (const HostAddress &other) const { return memcmp(qip6.c, other.qip6.c, 16) < 0; } @@ -119,9 +134,13 @@ quint32 qHash(const HostAddress &ha) { QString HostAddress::toString() const { if (isV6()) { - QString qs; - qs.sprintf("[%x:%x:%x:%x:%x:%x:%x:%x]", ntohs(shorts[0]), ntohs(shorts[1]), ntohs(shorts[2]), ntohs(shorts[3]), ntohs(shorts[4]), ntohs(shorts[5]), ntohs(shorts[6]), ntohs(shorts[7])); - return qs.replace(QRegExp(QLatin1String("(:0)+")),QLatin1String(":")); + if (isValid()) { + QString qs; + qs.sprintf("[%x:%x:%x:%x:%x:%x:%x:%x]", ntohs(shorts[0]), ntohs(shorts[1]), ntohs(shorts[2]), ntohs(shorts[3]), ntohs(shorts[4]), ntohs(shorts[5]), ntohs(shorts[6]), ntohs(shorts[7])); + return qs.replace(QRegExp(QLatin1String("(:0)+")),QLatin1String(":")); + } else { + return QLatin1String("[::]"); + } } else { return QHostAddress(ntohl(hash[3])).toString(); } -- cgit v1.2.3