|
1 | 1 | /*
|
2 |
| - IPAddress.h - Base class that provides IPAddress |
3 |
| - Copyright (c) 2011 Adrian McEwen. All right reserved. |
| 2 | + IPAddress.h - Base class that provides IPAddress |
| 3 | + Copyright (c) 2011 Adrian McEwen. All right reserved. |
4 | 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. |
| 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 | 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. |
| 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 | 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 |
| -*/ |
| 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 | 19 |
|
20 | 20 | #ifndef IPAddress_h
|
21 | 21 | #define IPAddress_h
|
22 | 22 |
|
23 | 23 | #include <stdint.h>
|
24 |
| -#include "Printable.h" |
25 |
| -#include "WString.h" |
| 24 | +#include <WString.h> |
| 25 | +#include <Printable.h> |
| 26 | + |
| 27 | +#include <lwip/init.h> |
| 28 | +#include <lwip/ip_addr.h> |
| 29 | +#include <lwip/ip4_addr.h> |
| 30 | + |
| 31 | +#if !LWIP_IPV6 |
| 32 | +struct ip_addr: ipv4_addr { }; |
| 33 | +#endif // !LWIP_IPV6 |
| 34 | + |
| 35 | +// to display a netif id with printf: |
| 36 | +#define NETIFID_STR "%c%c%u" |
| 37 | +#define NETIFID_VAL(netif) \ |
| 38 | + ((netif)? (netif)->name[0]: '-'), \ |
| 39 | + ((netif)? (netif)->name[1]: '-'), \ |
| 40 | + ((netif)? netif_get_index(netif): 42) |
26 | 41 |
|
27 | 42 | // A class to make it easier to handle and pass around IP addresses
|
| 43 | +// IPv6 update: |
| 44 | +// IPAddress is now a decorator class for lwIP's ip_addr_t |
| 45 | +// fully backward compatible with legacy IPv4-only Arduino's |
| 46 | +// with unchanged footprint when IPv6 is disabled |
| 47 | + |
| 48 | +class IPAddress: public Printable { |
| 49 | + private: |
| 50 | + |
| 51 | + ip_addr_t _ip; |
| 52 | + |
| 53 | + // Access the raw byte array containing the address. Because this returns a pointer |
| 54 | + // to the internal structure rather than a copy of the address this function should only |
| 55 | + // be used when you know that the usage of the returned uint8_t* will be transient and not |
| 56 | + // stored. |
| 57 | + uint8_t* raw_address() { |
| 58 | + return reinterpret_cast<uint8_t*>(&v4()); |
| 59 | + } |
| 60 | + const uint8_t* raw_address() const { |
| 61 | + return reinterpret_cast<const uint8_t*>(&v4()); |
| 62 | + } |
| 63 | + |
| 64 | + public: |
| 65 | + IPAddress(); |
| 66 | + IPAddress(const IPAddress&); |
| 67 | + IPAddress(IPAddress&&); |
| 68 | + |
| 69 | + IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); |
| 70 | + IPAddress(uint32_t address) { *this = address; } |
| 71 | + IPAddress(unsigned long address) { *this = address; } |
| 72 | + IPAddress(int address) { *this = address; } |
| 73 | + IPAddress(const uint8_t *address) { *this = address; } |
| 74 | + |
| 75 | + bool fromString(const char *address); |
| 76 | + bool fromString(const String &address) { return fromString(address.c_str()); } |
| 77 | + |
| 78 | + // Overloaded cast operator to allow IPAddress objects to be used where a pointer |
| 79 | + // to a four-byte uint8_t array is expected |
| 80 | + operator uint32_t() const { return isV4()? v4(): (uint32_t)0; } |
| 81 | + operator uint32_t() { return isV4()? v4(): (uint32_t)0; } |
| 82 | + |
| 83 | + bool isSet () const; |
| 84 | + operator bool () const { return isSet(); } // <- |
| 85 | + operator bool () { return isSet(); } // <- both are needed |
| 86 | + |
| 87 | + // generic IPv4 wrapper to uint32-view like arduino loves to see it |
| 88 | + const uint32_t& v4() const { return ip_2_ip4(&_ip)->addr; } |
| 89 | + uint32_t& v4() { return ip_2_ip4(&_ip)->addr; } |
| 90 | + |
| 91 | + bool operator==(const IPAddress& addr) const { |
| 92 | + return ip_addr_cmp(&_ip, &addr._ip); |
| 93 | + } |
| 94 | + bool operator!=(const IPAddress& addr) const { |
| 95 | + return !ip_addr_cmp(&_ip, &addr._ip); |
| 96 | + } |
| 97 | + bool operator==(uint32_t addr) const { |
| 98 | + return isV4() && v4() == addr; |
| 99 | + } |
| 100 | + bool operator==(unsigned long addr) const { |
| 101 | + return isV4() && v4() == (uint32_t)addr; |
| 102 | + } |
| 103 | + bool operator!=(uint32_t addr) const { |
| 104 | + return !(isV4() && v4() == addr); |
| 105 | + } |
| 106 | + bool operator!=(unsigned long addr) const { |
| 107 | + return isV4() && v4() != (uint32_t)addr; |
| 108 | + } |
| 109 | + bool operator==(const uint8_t* addr) const; |
| 110 | + |
| 111 | + int operator>>(int n) const { |
| 112 | + return isV4()? v4() >> n: 0; |
| 113 | + } |
| 114 | + |
| 115 | + // Overloaded index operator to allow getting and setting individual octets of the address |
| 116 | + uint8_t operator[](int index) const { |
| 117 | + if (!isV4()) { |
| 118 | + return 0; |
| 119 | + } |
| 120 | + |
| 121 | + return ip4_addr_get_byte_val(*ip_2_ip4(&_ip), index); |
| 122 | + } |
| 123 | + |
| 124 | + uint8_t& operator[](int index) { |
| 125 | + setV4(); |
| 126 | + |
| 127 | + uint8_t* ptr = reinterpret_cast<uint8_t*>(&v4()); |
| 128 | + return *(ptr + index); |
| 129 | + } |
| 130 | + |
| 131 | + // Overloaded copy operators to allow initialisation of IPAddress objects from other types |
| 132 | + IPAddress& operator=(const uint8_t *address); |
| 133 | + IPAddress& operator=(uint32_t address); |
| 134 | + IPAddress& operator=(const IPAddress&) = default; |
| 135 | + |
| 136 | + virtual size_t printTo(Print& p) const; |
| 137 | + String toString() const; |
| 138 | + |
| 139 | + void clear(); |
| 140 | + |
| 141 | + /* |
| 142 | + check if input string(arg) is a valid IPV4 address or not. |
| 143 | + return true on valid. |
| 144 | + return false on invalid. |
| 145 | + */ |
| 146 | + static bool isValid(const String& arg); |
| 147 | + static bool isValid(const char* arg); |
| 148 | + |
| 149 | + friend class EthernetClass; |
| 150 | + friend class UDP; |
| 151 | + friend class Client; |
| 152 | + friend class Server; |
| 153 | + friend class DhcpClass; |
| 154 | + friend class DNSClient; |
| 155 | + |
| 156 | + /* |
| 157 | + lwIP address compatibility |
| 158 | + */ |
| 159 | + IPAddress(const ipv4_addr& fw_addr) { setV4(); v4() = fw_addr.addr; } |
| 160 | + IPAddress(const ipv4_addr* fw_addr) { setV4(); v4() = fw_addr->addr; } |
| 161 | + |
| 162 | + IPAddress& operator=(const ipv4_addr& fw_addr) { setV4(); v4() = fw_addr.addr; return *this; } |
| 163 | + IPAddress& operator=(const ipv4_addr* fw_addr) { setV4(); v4() = fw_addr->addr; return *this; } |
| 164 | + |
| 165 | + operator ip_addr_t () const { return _ip; } |
| 166 | + operator const ip_addr_t*() const { return &_ip; } |
| 167 | + operator ip_addr_t*() { return &_ip; } |
| 168 | + |
| 169 | + bool isV4() const { return IP_IS_V4_VAL(_ip); } |
| 170 | + void setV4() { IP_SET_TYPE_VAL(_ip, IPADDR_TYPE_V4); } |
| 171 | + |
| 172 | + bool isLocal () const { return ip_addr_islinklocal(&_ip); } |
| 173 | + |
| 174 | +#if LWIP_IPV6 |
| 175 | + |
| 176 | + IPAddress(const ip_addr_t& lwip_addr) { ip_addr_copy(_ip, lwip_addr); } |
| 177 | + IPAddress(const ip_addr_t* lwip_addr) { ip_addr_copy(_ip, *lwip_addr); } |
| 178 | + |
| 179 | + IPAddress& operator=(const ip_addr_t& lwip_addr) { ip_addr_copy(_ip, lwip_addr); return *this; } |
| 180 | + IPAddress& operator=(const ip_addr_t* lwip_addr) { ip_addr_copy(_ip, *lwip_addr); return *this; } |
| 181 | + |
| 182 | + uint16_t* raw6() |
| 183 | + { |
| 184 | + setV6(); |
| 185 | + return reinterpret_cast<uint16_t*>(ip_2_ip6(&_ip)); |
| 186 | + } |
| 187 | + |
| 188 | + const uint16_t* raw6() const |
| 189 | + { |
| 190 | + return isV6()? reinterpret_cast<const uint16_t*>(ip_2_ip6(&_ip)): nullptr; |
| 191 | + } |
| 192 | + |
| 193 | + // when not IPv6, ip_addr_t == ip4_addr_t so this one would be ambiguous |
| 194 | + // required otherwise |
| 195 | + operator const ip4_addr_t*() const { return isV4()? ip_2_ip4(&_ip): nullptr; } |
| 196 | + |
| 197 | + bool isV6() const { return IP_IS_V6_VAL(_ip); } |
| 198 | + void setV6() { IP_SET_TYPE_VAL(_ip, IPADDR_TYPE_V6); } |
| 199 | + |
| 200 | + protected: |
| 201 | + bool fromString6(const char *address); |
| 202 | + |
| 203 | +#else |
| 204 | + |
| 205 | + // allow portable code when IPv6 is not enabled |
| 206 | + |
| 207 | + uint16_t* raw6() { return nullptr; } |
| 208 | + const uint16_t* raw6() const { return nullptr; } |
| 209 | + bool isV6() const { return false; } |
| 210 | + void setV6() { } |
| 211 | + |
| 212 | +#endif |
| 213 | + |
| 214 | + protected: |
| 215 | + bool fromString4(const char *address); |
28 | 216 |
|
29 |
| -class IPAddress : public Printable { |
30 |
| -private: |
31 |
| - union { |
32 |
| - uint8_t bytes[4]; // IPv4 address |
33 |
| - uint32_t dword; |
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() { return _address.bytes; }; |
41 |
| - |
42 |
| -public: |
43 |
| - // Constructors |
44 |
| - IPAddress(); |
45 |
| - IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); |
46 |
| - IPAddress(uint32_t address); |
47 |
| - IPAddress(const uint8_t *address); |
48 |
| - |
49 |
| - bool fromString(const char *address); |
50 |
| - bool fromString(const String &address) { return fromString(address.c_str()); } |
51 |
| - |
52 |
| - // Overloaded cast operator to allow IPAddress objects to be used where a pointer |
53 |
| - // to a four-byte uint8_t array is expected |
54 |
| - operator uint32_t() const { return _address.dword; }; |
55 |
| - bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; }; |
56 |
| - bool operator==(const uint8_t* addr) const; |
57 |
| - |
58 |
| - // Overloaded index operator to allow getting and setting individual octets of the address |
59 |
| - uint8_t operator[](int index) const { return _address.bytes[index]; }; |
60 |
| - uint8_t& operator[](int index) { return _address.bytes[index]; }; |
61 |
| - |
62 |
| - // Overloaded copy operators to allow initialisation of IPAddress objects from other types |
63 |
| - IPAddress& operator=(const uint8_t *address); |
64 |
| - IPAddress& operator=(uint32_t address); |
65 |
| - |
66 |
| - virtual size_t printTo(Print& p) const; |
67 |
| - |
68 |
| - friend class EthernetClass; |
69 |
| - friend class UDP; |
70 |
| - friend class Client; |
71 |
| - friend class Server; |
72 |
| - friend class DhcpClass; |
73 |
| - friend class DNSClient; |
74 | 217 | };
|
75 | 218 |
|
76 |
| -const IPAddress INADDR_NONE(0,0,0,0); |
| 219 | +extern const IPAddress INADDR_ANY; |
| 220 | +extern const IPAddress INADDR_NONE; |
77 | 221 |
|
78 | 222 | #endif
|
0 commit comments