diff --git a/cores/arduino/IPAddress.cpp b/cores/arduino/IPAddress.cpp index 68439d1a5a..d0b82f3adb 100644 --- a/cores/arduino/IPAddress.cpp +++ b/cores/arduino/IPAddress.cpp @@ -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); +} diff --git a/cores/arduino/IPAddress.h b/cores/arduino/IPAddress.h index c73f011982..b8b1092d3b 100644 --- a/cores/arduino/IPAddress.h +++ b/cores/arduino/IPAddress.h @@ -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; diff --git a/cores/arduino/IPv6Address.cpp b/cores/arduino/IPv6Address.cpp new file mode 100644 index 0000000000..c3d3efbf3e --- /dev/null +++ b/cores/arduino/IPv6Address.cpp @@ -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 +#include +#include + +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; +} diff --git a/cores/arduino/IPv6Address.h b/cores/arduino/IPv6Address.h new file mode 100644 index 0000000000..3f22ca3945 --- /dev/null +++ b/cores/arduino/IPv6Address.h @@ -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 +#include +#include + +// 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