MySensors Library & Examples  2.3.2-36-g2e00bf6
IPAddress.h
1 /*
2  * IPAddress.h - Base class that provides IPAddress
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  * Modified by Marcelo Aquino <[email protected]> for MySensors use
21  */
22 
23 #ifndef IPAddress_h
24 #define IPAddress_h
25 
26 #include <stdint.h>
27 #include <string>
28 
32 class IPAddress
33 {
34 private:
35  union {
36  uint8_t bytes[4];
37  uint32_t dword;
38  } _address;
39 
49  uint8_t* raw_address()
50  {
51  return _address.bytes;
52  }
53 
54 public:
58  IPAddress();
67  IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
73  explicit IPAddress(uint32_t address);
79  explicit IPAddress(const uint8_t *address);
85  bool fromString(const char *address);
91  bool fromString(const std::string &address)
92  {
93  return fromString(address.c_str());
94  }
100  operator uint32_t() const
101  {
102  return _address.dword;
103  }
108  bool operator==(const IPAddress& addr) const
109  {
110  return _address.dword == addr._address.dword;
111  }
116  bool operator==(uint32_t addr) const
117  {
118  return _address.dword == addr;
119  }
124  bool operator==(const uint8_t* addr) const;
131  uint8_t operator[](int index) const
132  {
133  return _address.bytes[index];
134  }
139  uint8_t& operator[](int index)
140  {
141  return _address.bytes[index];
142  }
148  IPAddress& operator=(const uint8_t *address);
154  IPAddress& operator=(uint32_t address);
160  std::string toString();
161 
162  friend class Client;
163 };
164 
165 #endif
IPAddress::toString
std::string toString()
Convert the IP address to a string.
IPAddress::fromString
bool fromString(const char *address)
Set the IP from a array of characters.
IPAddress::operator[]
uint8_t & operator[](int index)
Overloaded index operator.
Definition: IPAddress.h:139
IPAddress::operator==
bool operator==(uint32_t addr) const
Overloaded cast operator.
Definition: IPAddress.h:116
IPAddress::IPAddress
IPAddress()
IPAddress constructor.
IPAddress::bytes
uint8_t bytes[4]
IPv4 address as an array.
Definition: IPAddress.h:36
IPAddress::operator==
bool operator==(const IPAddress &addr) const
Overloaded cast operator.
Definition: IPAddress.h:108
IPAddress::operator[]
uint8_t operator[](int index) const
Overloaded index operator.
Definition: IPAddress.h:131
IPAddress::operator=
IPAddress & operator=(const uint8_t *address)
Overloaded copy operators.
IPAddress::fromString
bool fromString(const std::string &address)
Set the IP from a string class type.
Definition: IPAddress.h:91
IPAddress
A class to make it easier to handle and pass around IP addresses.
Definition: IPAddress.h:32
IPAddress::dword
uint32_t dword
IPv4 address in 32 bits format.
Definition: IPAddress.h:37