Library to resolve text URLs to IP addresses (IPv4)
Dependents: NetworkSocketAPI NetworkSocketAPI Nucleo-AWS-IoT-mbed
Diff: DnsQuery.cpp
- Branch:
- api-changes
- Revision:
- 7:cbe1ef566314
- Parent:
- 6:5b28cbc8169c
- Child:
- 9:16e5208cc4ef
--- a/DnsQuery.cpp Mon Feb 22 19:58:10 2016 -0600 +++ b/DnsQuery.cpp Tue Feb 23 01:24:53 2016 -0600 @@ -46,6 +46,11 @@ bool DnsQuery::getHostByName(const char* hostname, char* resolvedIp) { + if (isIP(hostname)) { + strcpy(resolvedIp, hostname); + return true; + } + const char * dnsIPs[] = { "8.8.8.8", "209.244.0.3", @@ -64,6 +69,24 @@ } +bool DnsQuery::isIP(const char* hostname) const +{ + int i; + + for (i = 0; hostname[i]; i++) { + if (!(hostname[i] >= '0' && hostname[i] <= '9') && hostname[i] != '.') { + return false; + } + } + + // Ending with '.' garuntees hostname + if (i > 0 && hostname[i-1] == '.') { + return false; + } + + return true; +} + bool DnsQuery::getIP(const char* hostname, char* ipaddress) { INFO("%s", hostname);