Pico-Arduino
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 #pragma once
21 
22 #include <stdint.h>
23 #include "Printable.h"
24 #include "WString.h"
25 
26 // forward declartions of global name space friend classes
27 class EthernetClass;
28 class DhcpClass;
29 class DNSClient;
30 
31 namespace arduino {
32 
33 // A class to make it easier to handle and pass around IP addresses
34 
35 class IPAddress : public Printable {
36 private:
37  union {
38  uint8_t bytes[4]; // IPv4 address
39  uint32_t dword;
40  } _address;
41 
42  // Access the raw byte array containing the address. Because this returns a pointer
43  // to the internal structure rather than a copy of the address this function should only
44  // be used when you know that the usage of the returned uint8_t* will be transient and not
45  // stored.
46  uint8_t* raw_address() { return _address.bytes; };
47 
48 public:
49  // Constructors
50  IPAddress();
51  IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
52  IPAddress(uint32_t address);
53  IPAddress(const uint8_t *address);
54 
55  bool fromString(const char *address);
56  bool fromString(const String &address) { return fromString(address.c_str()); }
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 { return _address.dword; };
61  bool operator==(const IPAddress& addr) const { return _address.dword == addr._address.dword; };
62  bool operator!=(const IPAddress& addr) const { return _address.dword != addr._address.dword; };
63  bool operator==(const uint8_t* addr) const;
64 
65  // Overloaded index operator to allow getting and setting individual octets of the address
66  uint8_t operator[](int index) const { return _address.bytes[index]; };
67  uint8_t& operator[](int index) { return _address.bytes[index]; };
68 
69  // Overloaded copy operators to allow initialisation of IPAddress objects from other types
70  IPAddress& operator=(const uint8_t *address);
71  IPAddress& operator=(uint32_t address);
72 
73  virtual size_t printTo(Print& p) const;
74 
75  friend class UDP;
76  friend class Client;
77  friend class Server;
78 
79  friend ::EthernetClass;
80  friend ::DhcpClass;
81  friend ::DNSClient;
82 };
83 
84 extern const IPAddress INADDR_NONE;
85 }
Definition: Client.h:27
Definition: IPAddress.h:35
Definition: Print.h:35
Definition: Printable.h:34
Definition: Server.h:26
Definition: Udp.h:42