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