Skip to content

Commit 6451fa1

Browse files
author
Virens
committed
add "toString" function with IPAddress
1 parent 641f3d8 commit 6451fa1

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

Diff for: cores/arduino/IPAddress.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,9 @@ size_t IPAddress::printTo(Print &p) const
107107
return n;
108108
}
109109

110+
String IPAddress::toString() const
111+
{
112+
char szRet[16];
113+
sprintf(szRet, "%u.%u.%u.%u", _address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3]);
114+
return String(szRet);
115+
}

Diff for: cores/arduino/IPAddress.h

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class IPAddress : public Printable {
8282
IPAddress &operator=(uint32_t address);
8383

8484
virtual size_t printTo(Print &p) const;
85+
String toString() const;
8586

8687
friend class EthernetClass;
8788
friend class UDP;

Diff for: cores/arduino/IPv6Address.cpp

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
IPv6Address.cpp - Base class that provides IPv6Address
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <Arduino.h>
21+
#include <IPv6Address.h>
22+
#include <Print.h>
23+
24+
IPv6Address::IPv6Address()
25+
{
26+
memset(_address.bytes, 0, sizeof(_address.bytes));
27+
}
28+
29+
IPv6Address::IPv6Address(const uint8_t *address)
30+
{
31+
memcpy(_address.bytes, address, sizeof(_address.bytes));
32+
}
33+
34+
IPv6Address::IPv6Address(const uint32_t *address)
35+
{
36+
memcpy(_address.bytes, (const uint8_t *)address, sizeof(_address.bytes));
37+
}
38+
39+
IPv6Address &IPv6Address::operator=(const uint8_t *address)
40+
{
41+
memcpy(_address.bytes, address, sizeof(_address.bytes));
42+
return *this;
43+
}
44+
45+
bool IPv6Address::operator==(const uint8_t *addr) const
46+
{
47+
return memcmp(addr, _address.bytes, sizeof(_address.bytes)) == 0;
48+
}
49+
50+
size_t IPv6Address::printTo(Print &p) const
51+
{
52+
size_t n = 0;
53+
for (int i = 0; i < 16; i += 2) {
54+
if(i) {
55+
n += p.print(':');
56+
}
57+
n += p.printf("%02x", _address.bytes[i]);
58+
n += p.printf("%02x", _address.bytes[i + 1]);
59+
60+
}
61+
return n;
62+
}
63+
64+
String IPv6Address::toString() const
65+
{
66+
char szRet[40];
67+
sprintf(szRet, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
68+
_address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3],
69+
_address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7],
70+
_address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11],
71+
_address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]);
72+
return String(szRet);
73+
}
74+
75+
bool IPv6Address::fromString(const char *address)
76+
{
77+
//format 0011:2233:4455:6677:8899:aabb:ccdd:eeff
78+
if (strlen(address) != 39) {
79+
return false;
80+
}
81+
char * pos = (char *)address;
82+
size_t i = 0;
83+
for (i = 0; i < 16; i += 2) {
84+
if (!sscanf(pos, "%2hhx", &_address.bytes[i]) || !sscanf(pos + 2, "%2hhx", &_address.bytes[i + 1])) {
85+
return false;
86+
}
87+
pos += 5;
88+
}
89+
return true;
90+
}

Diff for: cores/arduino/IPv6Address.h

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
IPv6Address.h - Base class that provides IPv6Address
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef IPv6Address_h
21+
#define IPv6Address_h
22+
23+
#include <stdint.h>
24+
#include <WString.h>
25+
#include <Printable.h>
26+
27+
// A class to make it easier to handle and pass around IP addresses
28+
29+
class IPv6Address: public Printable {
30+
private:
31+
union {
32+
uint8_t bytes[16]; // IPv4 address
33+
uint32_t dword[4];
34+
} _address;
35+
36+
// Access the raw byte array containing the address. Because this returns a pointer
37+
// to the internal structure rather than a copy of the address this function should only
38+
// be used when you know that the usage of the returned uint8_t* will be transient and not
39+
// stored.
40+
uint8_t *raw_address()
41+
{
42+
return _address.bytes;
43+
}
44+
45+
public:
46+
// Constructors
47+
IPv6Address();
48+
IPv6Address(const uint8_t *address);
49+
IPv6Address(const uint32_t *address);
50+
51+
bool fromString(const char *address);
52+
bool fromString(const String &address)
53+
{
54+
return fromString(address.c_str());
55+
}
56+
57+
operator const uint8_t *() const
58+
{
59+
return _address.bytes;
60+
}
61+
operator const uint32_t *() const
62+
{
63+
return _address.dword;
64+
}
65+
bool operator==(const IPv6Address &addr) const
66+
{
67+
return (_address.dword[0] == addr._address.dword[0])
68+
&& (_address.dword[1] == addr._address.dword[1])
69+
&& (_address.dword[2] == addr._address.dword[2])
70+
&& (_address.dword[3] == addr._address.dword[3]);
71+
}
72+
bool operator==(const uint8_t *addr) const;
73+
74+
// Overloaded index operator to allow getting and setting individual octets of the address
75+
uint8_t operator[](int index) const
76+
{
77+
return _address.bytes[index];
78+
}
79+
uint8_t &operator[](int index)
80+
{
81+
return _address.bytes[index];
82+
}
83+
84+
// Overloaded copy operators to allow initialisation of IPv6Address objects from other types
85+
IPv6Address &operator=(const uint8_t *address);
86+
87+
virtual size_t printTo(Print &p) const;
88+
String toString() const;
89+
90+
friend class UDP;
91+
friend class Client;
92+
friend class Server;
93+
};
94+
95+
#endif

0 commit comments

Comments
 (0)