[136] | 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 <Printable.h>
|
---|
| 24 |
|
---|
| 25 | // A class to make it easier to handle and pass around IP addresses
|
---|
| 26 |
|
---|
| 27 | class IPAddress : public Printable {
|
---|
| 28 | private:
|
---|
| 29 | uint8_t _address[4]; // IPv4 address
|
---|
| 30 | // Access the raw byte array containing the address. Because this returns a pointer
|
---|
| 31 | // to the internal structure rather than a copy of the address this function should only
|
---|
| 32 | // be used when you know that the usage of the returned uint8_t* will be transient and not
|
---|
| 33 | // stored.
|
---|
| 34 | uint8_t* raw_address() { return _address; };
|
---|
| 35 |
|
---|
| 36 | public:
|
---|
| 37 | // Constructors
|
---|
| 38 | IPAddress();
|
---|
| 39 | IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
|
---|
| 40 | IPAddress(uint32_t address);
|
---|
| 41 | IPAddress(const uint8_t *address);
|
---|
| 42 |
|
---|
| 43 | // Overloaded cast operator to allow IPAddress objects to be used where a pointer
|
---|
| 44 | // to a four-byte uint8_t array is expected
|
---|
| 45 | operator uint32_t() const { return *((uint32_t*)_address+0); };
|
---|
| 46 | bool operator==(const IPAddress& addr) const { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
|
---|
| 47 | bool operator==(const uint8_t* addr) const;
|
---|
| 48 |
|
---|
| 49 | // Overloaded index operator to allow getting and setting individual octets of the address
|
---|
| 50 | uint8_t operator[](int index) const { return _address[index]; };
|
---|
| 51 | uint8_t& operator[](int index) { return _address[index]; };
|
---|
| 52 |
|
---|
| 53 | // Overloaded copy operators to allow initialisation of IPAddress objects from other types
|
---|
| 54 | IPAddress& operator=(const uint8_t *address);
|
---|
| 55 | IPAddress& operator=(uint32_t address);
|
---|
| 56 |
|
---|
| 57 | virtual size_t printTo(Print& p) const;
|
---|
| 58 |
|
---|
| 59 | friend class EthernetClass;
|
---|
| 60 | friend class UDP;
|
---|
| 61 | friend class Client;
|
---|
| 62 | friend class Server;
|
---|
| 63 | friend class DhcpClass;
|
---|
| 64 | friend class DNSClient;
|
---|
| 65 | };
|
---|
| 66 |
|
---|
| 67 | const IPAddress INADDR_NONE(0,0,0,0);
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | #endif
|
---|