IpAddress.h
Go to the documentation of this file.
1 /*
2  IpAddress.h - Base class that provides IP Address
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 #pragma once
21 
22 #include <lwip/init.h>
23 #include <lwip/ip_addr.h>
24 #include "Printable.h"
25 #include "WString.h"
26 
27 #if LWIP_VERSION_MAJOR == 2
28 #define LWIP_IP_ADDR_T const ip_addr_t
29 #else
30 using ip_addr_t = struct ip_addr;
32 #define IP_ADDR4(IP, A, B, C, D) IP4_ADDR(IP, A, B, C, D)
33 #define ip_addr_set_ip4_u32(IP, U32) ip4_addr_set_u32(IP, U32)
34 #define ip_addr_get_ip4_u32(IP) ip4_addr_get_u32(IP)
35 #define ip_2_ip4(IP) (IP)
36 #define ip4_addr_netcmp(A, B, C) ip_addr_netcmp(A, B, C)
37 #define LWIP_IP_ADDR_T ip_addr_t
38 #endif
39 
44 class IpAddress
45 {
46 private:
47  ip_addr_t address{0};
48 
49  void fromString(const String& address);
50 
51 public:
52  // Constructors
54  {
55  }
56 
57  IpAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
58  {
59  IP_ADDR4(&address, first_octet, second_octet, third_octet, fourth_octet);
60  }
61 
62  IpAddress(uint32_t address)
63  {
64  ip_addr_set_ip4_u32(&this->address, address);
65  }
66 
67  IpAddress(ip_addr_t& addr) : address(addr)
68  {
69  }
70 
71  IpAddress(const ip_addr_t& addr) : address(addr)
72  {
73  }
74 
75 #if LWIP_VERSION_MAJOR == 2 && LWIP_IPV6
76  IpAddress(ip4_addr_t& addr)
77  {
78  ip_addr_copy_from_ip4(address, addr);
79  }
80 #endif
81 
85  IpAddress(const uint8_t address[4])
86  {
87  IP_ADDR4(&this->address, address[0], address[1], address[2], address[3]);
88  }
89 
90  IpAddress(const String& address)
91  {
92  fromString(address);
93  }
94 
95  operator uint32_t() const
96  {
97  return ip_addr_get_ip4_u32(&address);
98  }
99 
100  operator ip_addr_t() const
101  {
102  return address;
103  }
104 
105  operator ip_addr_t*()
106  {
107  return &address;
108  }
109 
110 #if LWIP_VERSION_MAJOR == 2 && LWIP_IPV6
111  operator ip4_addr_t() const
112  {
113  return *ip_2_ip4(&address);
114  }
115 
116  operator ip4_addr_t*()
117  {
118  return ip_2_ip4(&address);
119  }
120 #endif
121 
122  bool operator==(const IpAddress& otherAddress) const
123  {
124  return ip_addr_cmp(&address, &otherAddress.address);
125  }
126 
127  bool operator==(const uint8_t addr[4]) const
128  {
129  return *this == IpAddress(addr);
130  }
131 
132  bool operator!=(const IpAddress& otherAddress) const
133  {
134  return !ip_addr_cmp(&address, &otherAddress.address);
135  }
136 
137  bool operator!=(const uint8_t addr[4]) const
138  {
139  return *this != IpAddress(addr);
140  }
141 
142  bool isNull() const
143  {
144  return ip_addr_isany(&address);
145  }
146 
147  String toString() const;
148 
149  bool compare(const IpAddress& addr, const IpAddress& mask) const
150  {
151  return ip_addr_netcmp(&address, &addr.address, ip_2_ip4(&mask.address));
152  }
153 
154  // Overloaded index operator to allow getting and setting individual octets of the address
155  uint8_t operator[](int index) const
156  {
157  if(unsigned(index) >= sizeof(ip4_addr_t)) {
158  abort();
159  }
160 
161  return reinterpret_cast<const uint8_t*>(&ip_2_ip4(&address)->addr)[index];
162  }
163 
164  uint8_t& operator[](int index)
165  {
166  if(unsigned(index) >= sizeof(ip4_addr_t)) {
167  abort();
168  }
169 
170  return reinterpret_cast<uint8_t*>(&ip_2_ip4(&address)->addr)[index];
171  }
172 
173  // Overloaded copy operators to allow initialisation of IpAddress objects from other types
174  IpAddress& operator=(const uint8_t address[4])
175  {
176  IP_ADDR4(&this->address, address[0], address[1], address[2], address[3]);
177  return *this;
178  }
179 
180  IpAddress& operator=(uint32_t address)
181  {
182  ip_addr_set_ip4_u32(&this->address, address);
183  return *this;
184  }
185 
186  IpAddress& operator=(const String& address)
187  {
188  fromString(address);
189  return *this;
190  }
191 
192  size_t printTo(Print& p) const;
193 };
194 
195 inline String toString(IpAddress address)
196 {
197  return address.toString();
198 }
199 
200 // Making this extern saves 100's of bytes; each usage otherwise incurs 4 bytes of BSS
201 #define INADDR_NONE IpAddress()
ip_addr_t ip4_addr_t
Definition: IpAddress.h:31
#define ip_addr_get_ip4_u32(IP)
Definition: IpAddress.h:34
struct ip_addr ip_addr_t
Definition: IpAddress.h:30
#define ip_addr_set_ip4_u32(IP, U32)
Definition: IpAddress.h:33
String toString(IpAddress address)
Definition: IpAddress.h:195
#define IP_ADDR4(IP, A, B, C, D)
Definition: IpAddress.h:32
#define ip_2_ip4(IP)
Definition: IpAddress.h:35
bool fromString(const char *s, Uuid &uuid)
Definition: Uuid.h:149
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:45
bool isNull() const
Definition: IpAddress.h:142
bool operator!=(const uint8_t addr[4]) const
Definition: IpAddress.h:137
uint8_t operator[](int index) const
Definition: IpAddress.h:155
IpAddress & operator=(uint32_t address)
Definition: IpAddress.h:180
bool operator==(const uint8_t addr[4]) const
Definition: IpAddress.h:127
IpAddress & operator=(const uint8_t address[4])
Definition: IpAddress.h:174
uint8_t & operator[](int index)
Definition: IpAddress.h:164
IpAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
Definition: IpAddress.h:57
String toString() const
bool compare(const IpAddress &addr, const IpAddress &mask) const
Definition: IpAddress.h:149
IpAddress(uint32_t address)
Definition: IpAddress.h:62
IpAddress(ip_addr_t &addr)
Definition: IpAddress.h:67
IpAddress(const ip_addr_t &addr)
Definition: IpAddress.h:71
IpAddress(const uint8_t address[4])
Definition: IpAddress.h:85
IpAddress & operator=(const String &address)
Definition: IpAddress.h:186
bool operator!=(const IpAddress &otherAddress) const
Definition: IpAddress.h:132
size_t printTo(Print &p) const
bool operator==(const IpAddress &otherAddress) const
Definition: IpAddress.h:122
IpAddress()
Definition: IpAddress.h:53
IpAddress(const String &address)
Definition: IpAddress.h:90
Provides formatted output to stream.
Definition: Print.h:37
The String class.
Definition: WString.h:137