Fix scopeid parsing from avahi -> QHostAddress

This commit is contained in:
Stefan Bühler 2010-07-25 15:01:28 +02:00
parent a429d92add
commit 8849f4f57c
2 changed files with 14 additions and 9 deletions

View File

@ -14,9 +14,7 @@ namespace QCP {
case 0: return m_service.hostname(); case 0: return m_service.hostname();
case 1: case 1:
if (m_service.address().protocol() == QAbstractSocket::IPv6Protocol) { if (m_service.address().protocol() == QAbstractSocket::IPv6Protocol) {
QString scope = m_service.address().scopeId(); return QString("[%1]:%2").arg(m_service.address().toString()).arg(m_service.port());
if (!scope.isEmpty()) scope = "%" + scope;
return QString("[%1%2]:%3").arg(m_service.address().toString(), scope).arg(m_service.port());
} else { } else {
return QString("%1:%2").arg(m_service.address().toString()).arg(m_service.port()); return QString("%1:%2").arg(m_service.address().toString()).arg(m_service.port());
} }

View File

@ -14,7 +14,17 @@
#include <QSize> #include <QSize>
#include <QtEndian> #include <QtEndian>
extern "C" {
#include <netinet/in.h> #include <netinet/in.h>
#include <net/if.h>
}
static QString ifidx2name(unsigned int ifindex) {
char buf[IF_NAMESIZE];
char *n = ::if_indextoname(ifindex, buf);
if (!n) return QString();
return QString::fromUtf8(n, qstrnlen(n, IF_NAMESIZE));
}
namespace Avahi { namespace Avahi {
@ -23,12 +33,9 @@ namespace Avahi {
case AVAHI_PROTO_INET: case AVAHI_PROTO_INET:
return QHostAddress(qFromBigEndian((quint32) address->data.ipv4.address)); return QHostAddress(qFromBigEndian((quint32) address->data.ipv4.address));
case AVAHI_PROTO_INET6: { case AVAHI_PROTO_INET6: {
struct sockaddr_in6 a; QHostAddress addr((quint8*) address->data.ipv6.address);
memset(&a, 0, sizeof(a)); if (interface >= 0) addr.setScopeId(ifidx2name(interface));
a.sin6_family = AF_INET6; return addr;
a.sin6_scope_id = interface;
memcpy(a.sin6_addr.s6_addr, address->data.ipv6.address, 16);
return QHostAddress((const sockaddr*) &a);
} }
case AVAHI_PROTO_UNSPEC: case AVAHI_PROTO_UNSPEC:
return QHostAddress(); return QHostAddress();