Skip to content

Commit 682b4b0

Browse files
committed
Update header
1 parent 3f69bcf commit 682b4b0

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

cores/esp32/IPAddress.h

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,55 +26,61 @@
2626

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

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+
2938
class IPAddress: public Printable
3039
{
3140
private:
3241
union {
33-
uint8_t bytes[4]; // IPv4 address
34-
uint32_t dword;
42+
uint8_t bytes[16];
43+
uint32_t dword[4];
3544
} _address;
45+
IPType _type;
3646

3747
// Access the raw byte array containing the address. Because this returns a pointer
3848
// to the internal structure rather than a copy of the address this function should only
3949
// be used when you know that the usage of the returned uint8_t* will be transient and not
4050
// stored.
4151
uint8_t* raw_address()
4252
{
43-
return _address.bytes;
53+
return _type == IPv4 ? &_address.bytes[IPADDRESS_V4_BYTES_INDEX] : _address.bytes;
4454
}
4555

4656
public:
4757
// Constructors
4858
IPAddress();
59+
IPAddress(IPType ip_type);
4960
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);
5062
IPAddress(uint32_t address);
5163
IPAddress(const uint8_t *address);
64+
IPAddress(IPType ip_type, const uint8_t *address);
5265
virtual ~IPAddress() {}
5366

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

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
5972
operator uint32_t() const
6073
{
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;
6675
}
76+
77+
bool operator==(const IPAddress& addr) const;
78+
bool operator!=(const IPAddress& addr) const { return !(*this == addr); };
6779
bool operator==(const uint8_t* addr) const;
6880

6981
// 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);
7884

7985
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
8086
IPAddress& operator=(const uint8_t *address);
@@ -83,14 +89,21 @@ class IPAddress: public Printable
8389
virtual size_t printTo(Print& p) const;
8490
String toString() const;
8591

92+
IPType type() { return _type; }
93+
8694
friend class EthernetClass;
8795
friend class UDP;
8896
friend class Client;
8997
friend class Server;
9098
friend class DhcpClass;
9199
friend class DNSClient;
100+
101+
protected:
102+
bool fromString4(const char *address);
103+
bool fromString6(const char *address);
92104
};
93105

94106
// changed to extern because const declaration creates copies in BSS of INADDR_NONE for each CPP unit that includes it
95107
extern IPAddress INADDR_NONE;
108+
extern IPAddress IN6ADDR_ANY;
96109
#endif

0 commit comments

Comments
 (0)