Skip to content

Commit a8c8c50

Browse files
author
Virens
committed
add IPv6Address with IPV6
1 parent f9e33ed commit a8c8c50

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

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+
}

cores/arduino/IPv6Address.h

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
{
31+
private:
32+
union {
33+
uint8_t bytes[16]; // IPv4 address
34+
uint32_t dword[4];
35+
} _address;
36+
37+
// Access the raw byte array containing the address. Because this returns a pointer
38+
// to the internal structure rather than a copy of the address this function should only
39+
// be used when you know that the usage of the returned uint8_t* will be transient and not
40+
// stored.
41+
uint8_t* raw_address()
42+
{
43+
return _address.bytes;
44+
}
45+
46+
public:
47+
// Constructors
48+
IPv6Address();
49+
IPv6Address(const uint8_t *address);
50+
IPv6Address(const uint32_t *address);
51+
52+
bool fromString(const char *address);
53+
bool fromString(const String &address) { return fromString(address.c_str()); }
54+
55+
operator const uint8_t*() const
56+
{
57+
return _address.bytes;
58+
}
59+
operator const uint32_t*() const
60+
{
61+
return _address.dword;
62+
}
63+
bool operator==(const IPv6Address& addr) const
64+
{
65+
return (_address.dword[0] == addr._address.dword[0])
66+
&& (_address.dword[1] == addr._address.dword[1])
67+
&& (_address.dword[2] == addr._address.dword[2])
68+
&& (_address.dword[3] == addr._address.dword[3]);
69+
}
70+
bool operator==(const uint8_t* addr) const;
71+
72+
// Overloaded index operator to allow getting and setting individual octets of the address
73+
uint8_t operator[](int index) const
74+
{
75+
return _address.bytes[index];
76+
}
77+
uint8_t& operator[](int index)
78+
{
79+
return _address.bytes[index];
80+
}
81+
82+
// Overloaded copy operators to allow initialisation of IPv6Address objects from other types
83+
IPv6Address& operator=(const uint8_t *address);
84+
85+
virtual size_t printTo(Print& p) const;
86+
String toString() const;
87+
88+
friend class UDP;
89+
friend class Client;
90+
friend class Server;
91+
};
92+
93+
#endif

0 commit comments

Comments
 (0)