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 typedef struct ip_addr ip_addr_t;
30 #define LWIP_IP_ADDR_T ip_addr_t
31 #endif
32 
37 class IpAddress : public Printable
38 {
39 private:
40  ip_addr_t address = {0};
41 
42  void fromString(const String& address);
43 
44 public:
45  // Constructors
47  {
48  }
49 
50  IpAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
51  {
52  IP4_ADDR(&address, first_octet, second_octet, third_octet, fourth_octet);
53  }
54 
55  IpAddress(uint32_t address)
56  {
57  this->address.addr = address;
58  }
59 
60  IpAddress(ip_addr address)
61  {
62  this->address.addr = address.addr;
63  }
64 
65 #if LWIP_VERSION_MAJOR == 2
66  IpAddress(ip_addr_t address)
67  {
68  this->address = address;
69  }
70 #endif
71 
72  IpAddress(const uint8_t* address)
73  {
74  IP4_ADDR(&this->address, address[0], address[1], address[2], address[3]);
75  }
76 
77  IpAddress(const String& address)
78  {
79  fromString(address);
80  }
81 
82  // Overloaded cast operator to allow IpAddress objects to be used where a pointer
83  // to a four-byte uint8_t array is expected
84  operator uint32_t() const
85  {
86  return address.addr;
87  }
88 
89  operator ip_addr() const
90  {
91  return {address.addr};
92  }
93 
94  operator ip_addr*()
95  {
96  return reinterpret_cast<ip_addr*>(&address);
97  }
98 
99 #if LWIP_VERSION_MAJOR == 2
100  operator ip_addr_t*()
101  {
102  return &address;
103  }
104 #endif
105 
106  bool operator==(const IpAddress& addr) const
107  {
108  return ip_addr_cmp(&address, &addr.address);
109  }
110 
111  bool operator==(const uint8_t* addr) const
112  {
113  return *this == IpAddress(addr);
114  }
115 
116  bool operator!=(const IpAddress& addr) const
117  {
118  return !ip_addr_cmp(&address, &addr.address);
119  }
120 
121  bool operator!=(const uint8_t* addr) const
122  {
123  return *this != IpAddress(addr);
124  }
125 
126  bool isNull() const
127  {
128  return ip_addr_isany(&address);
129  }
130 
131  String toString() const;
132 
133  bool compare(const IpAddress& addr, const IpAddress& mask) const
134  {
135  return ip_addr_netcmp(&address, &addr.address, &mask.address);
136  }
137 
138  // Overloaded index operator to allow getting and setting individual octets of the address
139  uint8_t operator[](int index) const
140  {
141  if(unsigned(index) >= sizeof(address)) {
142  abort();
143  }
144  return reinterpret_cast<const uint8_t*>(&address)[index];
145  }
146 
147  uint8_t& operator[](int index)
148  {
149  if(unsigned(index) >= sizeof(address)) {
150  abort();
151  }
152  return reinterpret_cast<uint8_t*>(&address)[index];
153  }
154 
155  // Overloaded copy operators to allow initialisation of IpAddress objects from other types
156  IpAddress& operator=(const uint8_t* address)
157  {
158  IP4_ADDR(&this->address, address[0], address[1], address[2], address[3]);
159  return *this;
160  }
161 
162  IpAddress& operator=(uint32_t address)
163  {
164  this->address.addr = address;
165  return *this;
166  }
167 
168  IpAddress& operator=(const String address)
169  {
170  fromString(address);
171  return *this;
172  }
173 
174  size_t printTo(Print& p) const override;
175 };
176 
179 
180 // Making this extern saves 100's of bytes; each usage otherwise incurs 4 bytes of BSS
181 #define INADDR_NONE IpAddress()
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:37
bool operator!=(const IpAddress &addr) const
Definition: IpAddress.h:116
IpAddress()
Definition: IpAddress.h:46
uint8_t & operator[](int index)
Definition: IpAddress.h:147
uint8_t operator[](int index) const
Definition: IpAddress.h:139
The String class.
Definition: WString.h:136
Provides formatted output to stream.
Definition: Print.h:36
bool operator==(const IpAddress &addr) const
Definition: IpAddress.h:106
bool operator!=(const uint8_t *addr) const
Definition: IpAddress.h:121
IpAddress(const uint8_t *address)
Definition: IpAddress.h:72
IpAddress(const String &address)
Definition: IpAddress.h:77
bool operator==(const uint8_t *addr) const
Definition: IpAddress.h:111
IpAddress(ip_addr address)
Definition: IpAddress.h:60
bool isNull() const
Definition: IpAddress.h:126
IpAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
Definition: IpAddress.h:50
#define SMING_DEPRECATED
Definition: sming_attr.h:30
IpAddress & operator=(const uint8_t *address)
Definition: IpAddress.h:156
bool compare(const IpAddress &addr, const IpAddress &mask) const
Definition: IpAddress.h:133
IpAddress & operator=(uint32_t address)
Definition: IpAddress.h:162
Definition: Printable.h:42
IpAddress(uint32_t address)
Definition: IpAddress.h:55
String toString() const
IpAddress & operator=(const String address)
Definition: IpAddress.h:168
size_t printTo(Print &p) const override
struct ip_addr ip_addr_t
Definition: IpAddress.h:29