LedUdpDevice now tries to parse the "host" as an IP address before trying to DNS resolve it (#205)

This commit is contained in:
penfold42 2016-08-27 22:05:09 +10:00 committed by brindosch
parent a6bf58c21e
commit 9cc3a2de2e

View File

@ -30,13 +30,24 @@ LedUdpDevice::~LedUdpDevice()
bool LedUdpDevice::setConfig(const Json::Value &deviceConfig) bool LedUdpDevice::setConfig(const Json::Value &deviceConfig)
{ {
QHostInfo info = QHostInfo::fromName( QString::fromStdString(deviceConfig["host"].asString()) ); if (_address.setAddress( QString::fromStdString(deviceConfig["host"].asString()) ) )
if (info.addresses().isEmpty())
{ {
throw std::runtime_error("invalid target address"); Debug( _log, "Successfully parsed %s as an ip address.", deviceConfig["host"].asString().c_str());
}
else
{
Debug( _log, "Failed to parse %s as an ip address.", deviceConfig["host"].asString().c_str());
QHostInfo info = QHostInfo::fromName( QString::fromStdString(deviceConfig["host"].asString()) );
if (info.addresses().isEmpty())
{
Debug( _log, "Failed to parse %s as a hostname.", deviceConfig["host"].asString().c_str());
throw std::runtime_error("invalid target address");
}
Debug( _log, "Successfully parsed %s as a hostname.", deviceConfig["host"].asString().c_str());
_address = info.addresses().first();
} }
_address = info.addresses().first();
_port = deviceConfig["port"].asUInt(); _port = deviceConfig["port"].asUInt();
Debug( _log, "UDP using %s:%d", _address.toString().toStdString().c_str() , _port );
return true; return true;
} }
@ -46,7 +57,7 @@ int LedUdpDevice::open()
QHostAddress localAddress = QHostAddress::Any; QHostAddress localAddress = QHostAddress::Any;
quint16 localPort = 0; quint16 localPort = 0;
WarningIf( !_udpSocket->bind(localAddress, localPort), _log, "Couldnt bind local address: %s", strerror(errno)); WarningIf( !_udpSocket->bind(localAddress, localPort), _log, "Could not bind local address: %s", strerror(errno));
return 0; return 0;
} }