zeroconf: add already prepared address strings to services

Change-Id: I16cfb5fd6a2c715760556ab9e14daf99941fcdd1
Reviewed-by: Christian Kandeler <christian.kandeler@nokia.com>
This commit is contained in:
Fawzi Mohamed
2012-05-31 14:42:27 +02:00
parent 7eb8a08c0b
commit 63f0a14fe5
2 changed files with 42 additions and 0 deletions

View File

@@ -298,6 +298,41 @@ QNetworkInterface Service::networkInterface() const
return QNetworkInterface::interfaceFromIndex(m_interfaceNr); return QNetworkInterface::interfaceFromIndex(m_interfaceNr);
} }
QStringList Service::addresses(Service::AddressStyle style) const
{
QStringList res;
if (host() == 0)
return res;
foreach (const QHostAddress &addr, host()->addresses()){
QString addrStr;
if (addr.protocol() == QAbstractSocket::IPv6Protocol) {
// Add the interface to use to the address <address>%<interface>
//
// This is required only for link local addresses (like fe80:*)
// but as we have it we do it (and most likely addresses here are
// link local).
//
// on windows only addresses like fe80::42%22 work
// on OSX 10.5 only things like fe80::42%en4 work
// on later OSX versions and linux both <address>%<interface number>
// and <address>%<interface name> work, we use the interface name as
// it looks better
#ifdef Q_OS_WIN
QString interfaceStr = QString::number(networkInterface().index());
#else
QString interfaceStr = networkInterface().name();
#endif
addrStr = QString::fromLatin1("%1%%2").arg(addr.toString()).arg(interfaceStr);
if (style == QuoteIPv6Adresses)
addrStr = QString::fromLatin1("[%1]").arg(addrStr);
} else {
addrStr = addr.toString();
}
res.append(addrStr);
}
return res;
}
bool Service::operator==(const Service &o) const { bool Service::operator==(const Service &o) const {
bool eq = m_fullName == o.m_fullName bool eq = m_fullName == o.m_fullName
&& m_name == o.m_name && m_type == o.m_type && m_name == o.m_name && m_type == o.m_type

View File

@@ -94,6 +94,12 @@ public:
typedef QSharedPointer<const Service> ConstPtr; typedef QSharedPointer<const Service> ConstPtr;
typedef QSharedPointer<Service> Ptr; typedef QSharedPointer<Service> Ptr;
enum AddressStyle
{
PlainAddresses,
QuoteIPv6Adresses
};
Service(const Service &o); Service(const Service &o);
Service(); Service();
~Service(); ~Service();
@@ -109,6 +115,7 @@ public:
const QHostInfo *host() const { return m_host; } const QHostInfo *host() const { return m_host; }
int interfaceNr() const { return m_interfaceNr; } int interfaceNr() const { return m_interfaceNr; }
QNetworkInterface networkInterface() const; QNetworkInterface networkInterface() const;
QStringList addresses(AddressStyle style = PlainAddresses) const;
bool operator==(const Service &o) const; bool operator==(const Service &o) const;
bool invalidate() { bool res = m_outdated; m_outdated = true; return res; } bool invalidate() { bool res = m_outdated; m_outdated = true; return res; }