1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-17 06:52:18 +01:00

Fixed DNSClient::inet_aton function.

Fixes #2500
This commit is contained in:
Cristian Maglie 2015-08-24 10:54:24 +02:00
parent 43d855c28b
commit 306eaa13c9

View File

@ -55,64 +55,46 @@ void DNSClient::begin(const IPAddress& aDNSServer)
} }
int DNSClient::inet_aton(const char* aIPAddrString, IPAddress& aResult) int DNSClient::inet_aton(const char* addr, IPAddress& result)
{ {
// See if we've been given a valid IP address // TODO: add support for "a", "a.b", "a.b.c" formats
const char* p =aIPAddrString;
while (*p &&
( (*p == '.') || (*p >= '0') || (*p <= '9') ))
{
p++;
}
if (*p == '\0') uint16_t acc = 0; // Accumulator
uint8_t dots = 0;
while (*address)
{ {
// It's looking promising, we haven't found any invalid characters char c = *address++;
p = aIPAddrString; if (c >= '0' && c <= '9')
int segment =0;
int segmentValue =0;
while (*p && (segment < 4))
{ {
if (*p == '.') acc = acc * 10 + (c - '0');
{ if (acc > 255) {
// We've reached the end of a segment // Value out of [0..255] range
if (segmentValue > 255) return 0;
{
// You can't have IP address segments that don't fit in a byte
return 0;
}
else
{
aResult[segment] = (byte)segmentValue;
segment++;
segmentValue = 0;
}
} }
else
{
// Next digit
segmentValue = (segmentValue*10)+(*p - '0');
}
p++;
} }
// We've reached the end of address, but there'll still be the last else if (c == '.')
// segment to deal with
if ((segmentValue > 255) || (segment > 3))
{ {
// You can't have IP address segments that don't fit in a byte, if (dots == 3) {
// or more than four segments // Too much dots (there must be 3 dots)
return 0; return 0;
}
result[dots++] = acc;
acc = 0;
} }
else else
{ {
aResult[segment] = (byte)segmentValue; // Invalid char
return 1; return 0;
} }
} }
else
{ if (dots != 3) {
// Too few dots (there must be 3 dots)
return 0; return 0;
} }
result[3] = acc;
return 1;
} }
int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult) int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult)