Resolve zeroconf uses bogus IPs #4091

This commit is contained in:
Xinyu Hou 2014-11-13 17:36:29 +00:00
parent 8d7f6e146d
commit fa1092d665
1 changed files with 33 additions and 6 deletions

View File

@ -27,6 +27,12 @@
#include <stdint.h>
#include <dns_sd.h>
static const QStringList preferedIPAddress(
QStringList() <<
"192.168." <<
"10." <<
"172.");
const char* ZeroconfService:: m_ServerServiceName = "_synergyServerZeroconf._tcp";
const char* ZeroconfService:: m_ClientServiceName = "_synergyClientZeroconf._tcp";
@ -100,10 +106,22 @@ void ZeroconfService::errorHandle(DNSServiceErrorType errorCode)
QString ZeroconfService::getLocalIPAddresses()
{
QStringList addresses;
foreach (const QHostAddress& address, QNetworkInterface::allAddresses()) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
return address.toString();
if (address.protocol() == QAbstractSocket::IPv4Protocol &&
address != QHostAddress(QHostAddress::LocalHost)) {
addresses.append(address.toString());
}
}
foreach (const QString& preferedIP, preferedIPAddress) {
foreach (const QString& address, addresses) {
if (address.startsWith(preferedIP)) {
return address;
}
}
}
return "";
}
@ -121,11 +139,20 @@ bool ZeroconfService::registerService(bool server)
else {
m_pZeroconfRegister = new ZeroconfRegister(this);
if (server) {
QString localIP = getLocalIPAddresses();
if (localIP.isEmpty()) {
QMessageBox::warning(m_pMainWindow, tr("Synergy"),
tr("Failed to get local IP address. "
"Please manually type in server address "
"on your clients"));
}
else {
m_pZeroconfRegister->registerService(
ZeroconfRecord(tr("%1").arg(getLocalIPAddresses()),
ZeroconfRecord(tr("%1").arg(localIP),
QLatin1String(m_ServerServiceName), QString()),
m_zeroconfServer.serverPort());
}
}
else {
m_pZeroconfRegister->registerService(
ZeroconfRecord(tr("%1").arg(m_pMainWindow->getScreenName()),