Skip to content

Commit 3bd810d

Browse files
committed
Merge branch 'ethernet-dns-fix' of https://github.com/cmaglie/Arduino
2 parents a1e2c4f + e3909b4 commit 3bd810d

File tree

5 files changed

+116
-46
lines changed

5 files changed

+116
-46
lines changed

hardware/arduino/avr/cores/arduino/IPAddress.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,48 @@ IPAddress::IPAddress(const uint8_t *address)
4343
memcpy(_address.bytes, address, sizeof(_address.bytes));
4444
}
4545

46+
bool IPAddress::fromString(const char *address)
47+
{
48+
// TODO: add support for "a", "a.b", "a.b.c" formats
49+
50+
uint16_t acc = 0; // Accumulator
51+
uint8_t dots = 0;
52+
53+
while (*address)
54+
{
55+
char c = *address++;
56+
if (c >= '0' && c <= '9')
57+
{
58+
acc = acc * 10 + (c - '0');
59+
if (acc > 255) {
60+
// Value out of [0..255] range
61+
return false;
62+
}
63+
}
64+
else if (c == '.')
65+
{
66+
if (dots == 3) {
67+
// Too much dots (there must be 3 dots)
68+
return false;
69+
}
70+
_address.bytes[dots++] = acc;
71+
acc = 0;
72+
}
73+
else
74+
{
75+
// Invalid char
76+
return false;
77+
}
78+
}
79+
80+
if (dots != 3) {
81+
// Too few dots (there must be 3 dots)
82+
return false;
83+
}
84+
_address.bytes[3] = acc;
85+
return true;
86+
}
87+
4688
IPAddress& IPAddress::operator=(const uint8_t *address)
4789
{
4890
memcpy(_address.bytes, address, sizeof(_address.bytes));

hardware/arduino/avr/cores/arduino/IPAddress.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class IPAddress : public Printable {
4545
IPAddress(uint32_t address);
4646
IPAddress(const uint8_t *address);
4747

48+
bool fromString(const char *address);
49+
bool fromString(const String &address) { return fromString(address.c_str()); }
50+
4851
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
4952
// to a four-byte uint8_t array is expected
5053
operator uint32_t() const { return _address.dword; };
@@ -71,5 +74,4 @@ class IPAddress : public Printable {
7174

7275
const IPAddress INADDR_NONE(0,0,0,0);
7376

74-
7577
#endif

hardware/arduino/sam/cores/arduino/IPAddress.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,48 @@ IPAddress::IPAddress(const uint8_t *address)
4343
memcpy(_address.bytes, address, sizeof(_address.bytes));
4444
}
4545

46+
bool IPAddress::fromString(const char *address)
47+
{
48+
// TODO: add support for "a", "a.b", "a.b.c" formats
49+
50+
uint16_t acc = 0; // Accumulator
51+
uint8_t dots = 0;
52+
53+
while (*address)
54+
{
55+
char c = *address++;
56+
if (c >= '0' && c <= '9')
57+
{
58+
acc = acc * 10 + (c - '0');
59+
if (acc > 255) {
60+
// Value out of [0..255] range
61+
return false;
62+
}
63+
}
64+
else if (c == '.')
65+
{
66+
if (dots == 3) {
67+
// Too much dots (there must be 3 dots)
68+
return false;
69+
}
70+
_address.bytes[dots++] = acc;
71+
acc = 0;
72+
}
73+
else
74+
{
75+
// Invalid char
76+
return false;
77+
}
78+
}
79+
80+
if (dots != 3) {
81+
// Too few dots (there must be 3 dots)
82+
return false;
83+
}
84+
_address.bytes[3] = acc;
85+
return true;
86+
}
87+
4688
IPAddress& IPAddress::operator=(const uint8_t *address)
4789
{
4890
memcpy(_address.bytes, address, sizeof(_address.bytes));

hardware/arduino/sam/cores/arduino/IPAddress.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class IPAddress : public Printable {
4545
IPAddress(uint32_t address);
4646
IPAddress(const uint8_t *address);
4747

48+
bool fromString(const char *address);
49+
bool fromString(const String &address) { return fromString(address.c_str()); }
50+
4851
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
4952
// to a four-byte uint8_t array is expected
5053
operator uint32_t() const { return _address.dword; };
@@ -71,5 +74,4 @@ class IPAddress : public Printable {
7174

7275
const IPAddress INADDR_NONE(0,0,0,0);
7376

74-
7577
#endif

libraries/Ethernet/src/Dns.cpp

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -55,64 +55,46 @@ void DNSClient::begin(const IPAddress& aDNSServer)
5555
}
5656

5757

58-
int DNSClient::inet_aton(const char* aIPAddrString, IPAddress& aResult)
58+
int DNSClient::inet_aton(const char* addr, IPAddress& result)
5959
{
60-
// See if we've been given a valid IP address
61-
const char* p =aIPAddrString;
62-
while (*p &&
63-
( (*p == '.') || (*p >= '0') || (*p <= '9') ))
64-
{
65-
p++;
66-
}
60+
// TODO: add support for "a", "a.b", "a.b.c" formats
61+
62+
uint16_t acc = 0; // Accumulator
63+
uint8_t dots = 0;
6764

68-
if (*p == '\0')
65+
while (*address)
6966
{
70-
// It's looking promising, we haven't found any invalid characters
71-
p = aIPAddrString;
72-
int segment =0;
73-
int segmentValue =0;
74-
while (*p && (segment < 4))
67+
char c = *address++;
68+
if (c >= '0' && c <= '9')
7569
{
76-
if (*p == '.')
77-
{
78-
// We've reached the end of a segment
79-
if (segmentValue > 255)
80-
{
81-
// You can't have IP address segments that don't fit in a byte
82-
return 0;
83-
}
84-
else
85-
{
86-
aResult[segment] = (byte)segmentValue;
87-
segment++;
88-
segmentValue = 0;
89-
}
70+
acc = acc * 10 + (c - '0');
71+
if (acc > 255) {
72+
// Value out of [0..255] range
73+
return 0;
9074
}
91-
else
92-
{
93-
// Next digit
94-
segmentValue = (segmentValue*10)+(*p - '0');
95-
}
96-
p++;
9775
}
98-
// We've reached the end of address, but there'll still be the last
99-
// segment to deal with
100-
if ((segmentValue > 255) || (segment > 3))
76+
else if (c == '.')
10177
{
102-
// You can't have IP address segments that don't fit in a byte,
103-
// or more than four segments
104-
return 0;
78+
if (dots == 3) {
79+
// Too much dots (there must be 3 dots)
80+
return 0;
81+
}
82+
result[dots++] = acc;
83+
acc = 0;
10584
}
10685
else
10786
{
108-
aResult[segment] = (byte)segmentValue;
109-
return 1;
87+
// Invalid char
88+
return 0;
11089
}
11190
}
112-
else
113-
{
91+
92+
if (dots != 3) {
93+
// Too few dots (there must be 3 dots)
11494
return 0;
11595
}
96+
result[3] = acc;
97+
return 1;
11698
}
11799

118100
int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult)

0 commit comments

Comments
 (0)