26
26
27
27
// A class to make it easier to handle and pass around IP addresses
28
28
29
+ #define IPADDRESS_V4_BYTES_INDEX 12
30
+ #define IPADDRESS_V4_DWORD_INDEX 3
31
+
32
+ enum IPType
33
+ {
34
+ IPv4,
35
+ IPv6
36
+ };
37
+
29
38
class IPAddress : public Printable
30
39
{
31
40
private:
32
41
union {
33
- uint8_t bytes[4 ]; // IPv4 address
34
- uint32_t dword;
42
+ uint8_t bytes[16 ];
43
+ uint32_t dword[ 4 ] ;
35
44
} _address;
45
+ IPType _type;
36
46
37
47
// Access the raw byte array containing the address. Because this returns a pointer
38
48
// to the internal structure rather than a copy of the address this function should only
39
49
// be used when you know that the usage of the returned uint8_t* will be transient and not
40
50
// stored.
41
51
uint8_t * raw_address ()
42
52
{
43
- return _address.bytes ;
53
+ return _type == IPv4 ? &_address. bytes [IPADDRESS_V4_BYTES_INDEX] : _address.bytes ;
44
54
}
45
55
46
56
public:
47
57
// Constructors
48
58
IPAddress ();
59
+ IPAddress (IPType ip_type);
49
60
IPAddress (uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
61
+ IPAddress (uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5, uint8_t o6, uint8_t o7, uint8_t o8, uint8_t o9, uint8_t o10, uint8_t o11, uint8_t o12, uint8_t o13, uint8_t o14, uint8_t o15, uint8_t o16);
50
62
IPAddress (uint32_t address);
51
63
IPAddress (const uint8_t *address);
64
+ IPAddress (IPType ip_type, const uint8_t *address);
52
65
virtual ~IPAddress () {}
53
66
54
67
bool fromString (const char *address);
55
68
bool fromString (const String &address) { return fromString (address.c_str ()); }
56
69
57
- // Overloaded cast operator to allow IPAddress objects to be used where a pointer
58
- // to a four-byte uint8_t array is expected
70
+ // Overloaded cast operator to allow IPAddress objects to be used where a
71
+ // uint32_t is expected
59
72
operator uint32_t () const
60
73
{
61
- return _address.dword ;
62
- }
63
- bool operator ==(const IPAddress& addr) const
64
- {
65
- return _address.dword == addr._address .dword ;
74
+ return _type == IPv4 ? _address.dword [IPADDRESS_V4_DWORD_INDEX] : 0 ;
66
75
}
76
+
77
+ bool operator ==(const IPAddress& addr) const ;
78
+ bool operator !=(const IPAddress& addr) const { return !(*this == addr); };
67
79
bool operator ==(const uint8_t * addr) const ;
68
80
69
81
// Overloaded index operator to allow getting and setting individual octets of the address
70
- uint8_t operator [](int index) const
71
- {
72
- return _address.bytes [index];
73
- }
74
- uint8_t & operator [](int index)
75
- {
76
- return _address.bytes [index];
77
- }
82
+ uint8_t operator [](int index) const ;
83
+ uint8_t & operator [](int index);
78
84
79
85
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
80
86
IPAddress& operator =(const uint8_t *address);
@@ -83,14 +89,21 @@ class IPAddress: public Printable
83
89
virtual size_t printTo (Print& p) const ;
84
90
String toString () const ;
85
91
92
+ IPType type () { return _type; }
93
+
86
94
friend class EthernetClass ;
87
95
friend class UDP ;
88
96
friend class Client ;
89
97
friend class Server ;
90
98
friend class DhcpClass ;
91
99
friend class DNSClient ;
100
+
101
+ protected:
102
+ bool fromString4 (const char *address);
103
+ bool fromString6 (const char *address);
92
104
};
93
105
94
106
// changed to extern because const declaration creates copies in BSS of INADDR_NONE for each CPP unit that includes it
95
107
extern IPAddress INADDR_NONE;
108
+ extern IPAddress IN6ADDR_ANY;
96
109
#endif
0 commit comments