diff --git a/src/gui/src/ZeroconfService.cpp b/src/gui/src/ZeroconfService.cpp index 0c1ea9cc..25ebf7db 100644 --- a/src/gui/src/ZeroconfService.cpp +++ b/src/gui/src/ZeroconfService.cpp @@ -27,6 +27,12 @@ #include #include +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,10 +139,19 @@ bool ZeroconfService::registerService(bool server) else { m_pZeroconfRegister = new ZeroconfRegister(this); if (server) { - m_pZeroconfRegister->registerService( - ZeroconfRecord(tr("%1").arg(getLocalIPAddresses()), - QLatin1String(m_ServerServiceName), QString()), - m_zeroconfServer.serverPort()); + 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(localIP), + QLatin1String(m_ServerServiceName), QString()), + m_zeroconfServer.serverPort()); + } } else { m_pZeroconfRegister->registerService(