Skip to content

add "toString" function with IPAddress #2304

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

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 6 additions & 0 deletions cores/arduino/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,9 @@ size_t IPAddress::printTo(Print &p) const
return n;
}

String IPAddress::toString() const
{
char szRet[16];
sprintf(szRet, "%u.%u.%u.%u", _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3]);
return String(szRet);
}
1 change: 1 addition & 0 deletions cores/arduino/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class IPAddress : public Printable {
IPAddress &operator=(uint32_t address);

virtual size_t printTo(Print &p) const;
String toString() const;

friend class EthernetClass;
friend class UDP;
Expand Down
90 changes: 90 additions & 0 deletions cores/arduino/IPv6Address.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
IPv6Address.cpp - Base class that provides IPv6Address
Copyright (c) 2011 Adrian McEwen. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <Arduino.h>
#include <IPv6Address.h>
#include <Print.h>

IPv6Address::IPv6Address()
{
memset(_address.bytes, 0, sizeof(_address.bytes));
}

IPv6Address::IPv6Address(const uint8_t *address)
{
memcpy(_address.bytes, address, sizeof(_address.bytes));
}

IPv6Address::IPv6Address(const uint32_t *address)
{
memcpy(_address.bytes, (const uint8_t *)address, sizeof(_address.bytes));
}

IPv6Address &IPv6Address::operator=(const uint8_t *address)
{
memcpy(_address.bytes, address, sizeof(_address.bytes));
return *this;
}

bool IPv6Address::operator==(const uint8_t *addr) const
{
return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
}

size_t IPv6Address::printTo(Print &p) const
{
size_t n = 0;
for (int i = 0; i < 16; i += 2) {
if(i) {
n += p.print(':');
}
n += p.printf("%02x", _address.bytes[i]);
n += p.printf("%02x", _address.bytes[i + 1]);

}
return n;
}

String IPv6Address::toString() const
{
char szRet[40];
sprintf(szRet, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
_address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3],
_address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7],
_address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11],
_address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]);
return String(szRet);
}

bool IPv6Address::fromString(const char *address)
{
//format 0011:2233:4455:6677:8899:aabb:ccdd:eeff
if (strlen(address) != 39) {
return false;
}
char * pos = (char *)address;
size_t i = 0;
for (i = 0; i < 16; i += 2) {
if (!sscanf(pos, "%2hhx", &_address.bytes[i]) || !sscanf(pos + 2, "%2hhx", &_address.bytes[i + 1])) {
return false;
}
pos += 5;
}
return true;
}
95 changes: 95 additions & 0 deletions cores/arduino/IPv6Address.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
IPv6Address.h - Base class that provides IPv6Address
Copyright (c) 2011 Adrian McEwen. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef IPv6Address_h
#define IPv6Address_h

#include <stdint.h>
#include <WString.h>
#include <Printable.h>

// A class to make it easier to handle and pass around IP addresses

class IPv6Address: public Printable {
private:
union {
uint8_t bytes[16]; // IPv4 address
uint32_t dword[4];
} _address;

// Access the raw byte array containing the address. Because this returns a pointer
// to the internal structure rather than a copy of the address this function should only
// be used when you know that the usage of the returned uint8_t* will be transient and not
// stored.
uint8_t *raw_address()
{
return _address.bytes;
}

public:
// Constructors
IPv6Address();
IPv6Address(const uint8_t *address);
IPv6Address(const uint32_t *address);

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

operator const uint8_t *() const
{
return _address.bytes;
}
operator const uint32_t *() const
{
return _address.dword;
}
bool operator==(const IPv6Address &addr) const
{
return (_address.dword[0] == addr._address.dword[0])
&& (_address.dword[1] == addr._address.dword[1])
&& (_address.dword[2] == addr._address.dword[2])
&& (_address.dword[3] == addr._address.dword[3]);
}
bool operator==(const uint8_t *addr) const;

// Overloaded index operator to allow getting and setting individual octets of the address
uint8_t operator[](int index) const
{
return _address.bytes[index];
}
uint8_t &operator[](int index)
{
return _address.bytes[index];
}

// Overloaded copy operators to allow initialisation of IPv6Address objects from other types
IPv6Address &operator=(const uint8_t *address);

virtual size_t printTo(Print &p) const;
String toString() const;

friend class UDP;
friend class Client;
friend class Server;
};

#endif
Loading