RUI3 (RAK Unified Interface 3) - RAK4631
IPAddress.h
Go to the documentation of this file.
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 #ifndef IPAddress_h
21 #define IPAddress_h
22 
23 #include <stdint.h>
24 #include "Printable.h"
25 #include "WString.h"
26 
27 // A class to make it easier to handle and pass around IP addresses
28 
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()
41  {
42  return _address.bytes;
43  };
44 
45  public:
46  // Constructors
47  IPAddress();
48  IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
49  IPAddress(uint32_t address);
50  IPAddress(const uint8_t *address);
51 
52  bool fromString(const char *address);
53  bool fromString(const String &address)
54  {
55  return fromString(address.c_str());
56  }
57 
58  // Overloaded cast operator to allow IPAddress objects to be used where a pointer
59  // to a four-byte uint8_t array is expected
60  operator uint32_t() const
61  {
62  return _address.dword;
63  };
64  bool operator==(const IPAddress &addr) const
65  {
66  return _address.dword == addr._address.dword;
67  };
68  bool operator==(const uint8_t *addr) const;
69 
70  // Overloaded index operator to allow getting and setting individual octets of the address
71  uint8_t operator[](int index) const
72  {
73  return _address.bytes[index];
74  };
75  uint8_t &operator[](int index)
76  {
77  return _address.bytes[index];
78  };
79 
80  // Overloaded copy operators to allow initialisation of IPAddress objects from other types
81  IPAddress &operator=(const uint8_t *address);
82  IPAddress &operator=(uint32_t address);
83 
84  virtual size_t printTo(Print &p) const;
85 
86  friend class EthernetClass;
87  friend class UDP;
88  friend class Client;
89  friend class Server;
90  friend class DhcpClass;
91  friend class DNSClient;
92 };
93 
94 const IPAddress INADDR_NONE(0, 0, 0, 0);
95 
96 #endif
bool fromString(const char *address)
friend class DhcpClass
Definition: IPAddress.h:90
Definition: Server.h:25
uint8_t & operator[](int index)
Definition: IPAddress.h:75
Definition: Udp.h:41
uint8_t bytes[4]
Definition: IPAddress.h:32
Definition: WString.h:22
uint32_t dword
Definition: IPAddress.h:33
uint8_t operator[](int index) const
Definition: IPAddress.h:71
Definition: Print.h:22
friend class DNSClient
Definition: IPAddress.h:91
bool operator==(const IPAddress &addr) const
Definition: IPAddress.h:64
Definition: Client.h:26
Definition: Printable.h:8
bool fromString(const String &address)
Definition: IPAddress.h:53
virtual size_t printTo(Print &p) const
const char * c_str() const
Definition: WString.h:111
Definition: IPAddress.h:29
friend class EthernetClass
Definition: IPAddress.h:86
IPAddress & operator=(const uint8_t *address)
const IPAddress INADDR_NONE(0, 0, 0, 0)