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