Skip to content

DNSClient::inet_aton fix and new IPAddress::fromString method #3780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions hardware/arduino/avr/cores/arduino/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,48 @@ IPAddress::IPAddress(const uint8_t *address)
memcpy(_address.bytes, address, sizeof(_address.bytes));
}

bool IPAddress::fromString(const char *address)
{
// TODO: add support for "a", "a.b", "a.b.c" formats

uint16_t acc = 0; // Accumulator
uint8_t dots = 0;

while (*address)
{
char c = *address++;
if (c >= '0' && c <= '9')
{
acc = acc * 10 + (c - '0');
if (acc > 255) {
// Value out of [0..255] range
return false;
}
}
else if (c == '.')
{
if (dots == 3) {
// Too much dots (there must be 3 dots)
return false;
}
_address.bytes[dots++] = acc;
acc = 0;
}
else
{
// Invalid char
return false;
}
}

if (dots != 3) {
// Too few dots (there must be 3 dots)
return false;
}
_address.bytes[3] = acc;
return true;
}

IPAddress& IPAddress::operator=(const uint8_t *address)
{
memcpy(_address.bytes, address, sizeof(_address.bytes));
Expand Down
4 changes: 3 additions & 1 deletion hardware/arduino/avr/cores/arduino/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class IPAddress : public Printable {
IPAddress(uint32_t address);
IPAddress(const uint8_t *address);

bool fromString(const char *address);
bool fromString(const String &address) { return fromString(address.c_str()); }

// Overloaded cast operator to allow IPAddress objects to be used where a pointer
// to a four-byte uint8_t array is expected
operator uint32_t() const { return _address.dword; };
Expand All @@ -71,5 +74,4 @@ class IPAddress : public Printable {

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


#endif
42 changes: 42 additions & 0 deletions hardware/arduino/sam/cores/arduino/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,48 @@ IPAddress::IPAddress(const uint8_t *address)
memcpy(_address.bytes, address, sizeof(_address.bytes));
}

bool IPAddress::fromString(const char *address)
{
// TODO: add support for "a", "a.b", "a.b.c" formats

uint16_t acc = 0; // Accumulator
uint8_t dots = 0;

while (*address)
{
char c = *address++;
if (c >= '0' && c <= '9')
{
acc = acc * 10 + (c - '0');
if (acc > 255) {
// Value out of [0..255] range
return false;
}
}
else if (c == '.')
{
if (dots == 3) {
// Too much dots (there must be 3 dots)
return false;
}
_address.bytes[dots++] = acc;
acc = 0;
}
else
{
// Invalid char
return false;
}
}

if (dots != 3) {
// Too few dots (there must be 3 dots)
return false;
}
_address.bytes[3] = acc;
return true;
}

IPAddress& IPAddress::operator=(const uint8_t *address)
{
memcpy(_address.bytes, address, sizeof(_address.bytes));
Expand Down
4 changes: 3 additions & 1 deletion hardware/arduino/sam/cores/arduino/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class IPAddress : public Printable {
IPAddress(uint32_t address);
IPAddress(const uint8_t *address);

bool fromString(const char *address);
bool fromString(const String &address) { return fromString(address.c_str()); }

// Overloaded cast operator to allow IPAddress objects to be used where a pointer
// to a four-byte uint8_t array is expected
operator uint32_t() const { return _address.dword; };
Expand All @@ -71,5 +74,4 @@ class IPAddress : public Printable {

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


#endif
70 changes: 26 additions & 44 deletions libraries/Ethernet/src/Dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
const char* p =aIPAddrString;
while (*p &&
( (*p == '.') || (*p >= '0') || (*p <= '9') ))
{
p++;
}
// TODO: add support for "a", "a.b", "a.b.c" formats

uint16_t acc = 0; // Accumulator
uint8_t dots = 0;

if (*p == '\0')
while (*address)
{
// It's looking promising, we haven't found any invalid characters
p = aIPAddrString;
int segment =0;
int segmentValue =0;
while (*p && (segment < 4))
char c = *address++;
if (c >= '0' && c <= '9')
{
if (*p == '.')
{
// We've reached the end of a segment
if (segmentValue > 255)
{
// You can't have IP address segments that don't fit in a byte
return 0;
}
else
{
aResult[segment] = (byte)segmentValue;
segment++;
segmentValue = 0;
}
acc = acc * 10 + (c - '0');
if (acc > 255) {
// Value out of [0..255] range
return 0;
}
else
{
// Next digit
segmentValue = (segmentValue*10)+(*p - '0');
}
p++;
}
// We've reached the end of address, but there'll still be the last
// segment to deal with
if ((segmentValue > 255) || (segment > 3))
else if (c == '.')
{
// You can't have IP address segments that don't fit in a byte,
// or more than four segments
return 0;
if (dots == 3) {
// Too much dots (there must be 3 dots)
return 0;
}
result[dots++] = acc;
acc = 0;
}
else
{
aResult[segment] = (byte)segmentValue;
return 1;
// Invalid char
return 0;
}
}
else
{

if (dots != 3) {
// Too few dots (there must be 3 dots)
return 0;
}
result[3] = acc;
return 1;
}

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