Skip to content

Commit 88f666a

Browse files
committed
Merge branch 'ethernet-dns-fix' of https://github.com/cmaglie/Arduino
2 parents ab75f49 + 8b9ee7f commit 88f666a

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

avr/cores/arduino/IPAddress.cpp

+42
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));

avr/cores/arduino/IPAddress.h

+3-1
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

0 commit comments

Comments
 (0)