[209] | 1 | /*
|
---|
| 2 | *
|
---|
| 3 | * MIT License:
|
---|
| 4 | * Copyright (c) 2011 Adrian McEwen
|
---|
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
| 6 | * of this software and associated documentation files (the "Software"), to deal
|
---|
| 7 | * in the Software without restriction, including without limitation the rights
|
---|
| 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
| 9 | * copies of the Software, and to permit persons to whom the Software is
|
---|
| 10 | * furnished to do so, subject to the following conditions:
|
---|
| 11 | *
|
---|
| 12 | * The above copyright notice and this permission notice shall be included in
|
---|
| 13 | * all copies or substantial portions of the Software.
|
---|
| 14 | *
|
---|
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
| 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
---|
| 21 | * THE SOFTWARE.
|
---|
| 22 | *
|
---|
| 23 | * adrianm@mcqn.com 1/1/2011
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | #ifndef IPAddress_h
|
---|
| 27 | #define IPAddress_h
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | // A class to make it easier to handle and pass around IP addresses
|
---|
| 31 |
|
---|
| 32 | class IPAddress {
|
---|
| 33 | private:
|
---|
| 34 | uint8_t _address[4]; // IPv4 address
|
---|
| 35 | // Access the raw byte array containing the address. Because this returns a pointer
|
---|
| 36 | // to the internal structure rather than a copy of the address this function should only
|
---|
| 37 | // be used when you know that the usage of the returned uint8_t* will be transient and not
|
---|
| 38 | // stored.
|
---|
| 39 | uint8_t* raw_address() { return _address; };
|
---|
| 40 |
|
---|
| 41 | public:
|
---|
| 42 | // Constructors
|
---|
| 43 | IPAddress();
|
---|
| 44 | IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
|
---|
| 45 | IPAddress(uint32_t address);
|
---|
| 46 | IPAddress(const uint8_t *address);
|
---|
| 47 |
|
---|
| 48 | // Overloaded cast operator to allow IPAddress objects to be used where a pointer
|
---|
| 49 | // to a four-byte uint8_t array is expected
|
---|
| 50 | operator uint32_t() { return *((uint32_t*)_address); };
|
---|
| 51 | bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
|
---|
| 52 | bool operator==(const uint8_t* addr);
|
---|
| 53 |
|
---|
| 54 | // Overloaded index operator to allow getting and setting individual octets of the address
|
---|
| 55 | uint8_t operator[](int index) const { return _address[index]; };
|
---|
| 56 | uint8_t& operator[](int index) { return _address[index]; };
|
---|
| 57 |
|
---|
| 58 | // Overloaded copy operators to allow initialisation of IPAddress objects from other types
|
---|
| 59 | IPAddress& operator=(const uint8_t *address);
|
---|
| 60 | IPAddress& operator=(uint32_t address);
|
---|
| 61 |
|
---|
| 62 |
|
---|
| 63 | friend class EthernetClass;
|
---|
| 64 | friend class UDP;
|
---|
| 65 | friend class Client;
|
---|
| 66 | friend class Server;
|
---|
| 67 | friend class DhcpClass;
|
---|
| 68 | friend class DNSClient;
|
---|
| 69 | };
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | #endif
|
---|