Bug in Ethernet library, getHostByName

I found a bug in DNSClient::getHostByName() and fixed it. Can please someone confirm this and upload it to the repository?

I’m trying to connect to a local server, called ‘htpc’. The hostname is resolved by DNSClient::getHostByName(). The first thing this function does is “See if it’s a numeric IP address” by calling inet_aton(). It returns falsely 1 (=yes, it’s a numeric IP address) for hostname ‘htpc’

For most (longer) hostnames it will accidentally return 0. This is probably why it hasn’t been discovered before.

I’m using the 1.0.6 Ethernet library from the repository and made a fix for Dns.cpp

  • valid numeric digits have to be >= 0 AND <= 9 (instead of >=0 OR <=9)
  • valid IP addresses must have 4 segments (for eg. ‘htpc’ only 1 segment is calculated)
diff -Nur Dns.cpp.org Dns.cpp
--- Dns.cpp.org	2014-12-14 23:01:00.747836707 +0100
+++ Dns.cpp	2014-12-14 23:02:29.743839091 +0100
@@ -60,7 +60,7 @@
     // See if we've been given a valid IP address
     const char* p =aIPAddrString;
     while (*p &&
-           ( (*p == '.') || (*p >= '0') || (*p <= '9') ))
+           ( (*p == '.') || ((*p >= '0') && (*p <= '9')) ))
     {
         p++;
     }
@@ -97,10 +97,10 @@
         }
         // We've reached the end of address, but there'll still be the last
         // segment to deal with
-        if ((segmentValue > 255) || (segment > 3))
+        if ((segmentValue > 255) || (segment != 3))
         {
             // You can't have IP address segments that don't fit in a byte,
-            // or more than four segments
+            // or other than four segments
             return 0;
         }
         else

File your issue and fix here.

done https://github.com/arduino/Arduino/pull/2499