IPAddress.h
#include <IPAddress.h>
/*H*******************************************************
IPAddress.h - BASE CLASS THAT PROVIDES IPAddress
Copyright ( c) 2011 Adrian McEwen. All right reserved.
THIS LIBRARY IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/Or
MODIFY IT UNDER THE TERMS OF THE GNU lESSER GENERAL PUBLIC
lICENSE AS PUBLISHED BY THE fREE Software Foundation; EITHER
VERSION 2.1 OF THE lICENSE, OR ( AT YOUR OPTION) ANY LATER VERSION.
THIS LIBRARY IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY Of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
Lesser General Public License FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU Lesser General Public
License ALONG WITH THIS LIBRARY; IF NOT, WRITE TO THE FREE SOFTWARE
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
********************************************************/
#ifndef IPAddress_h
#define IPAddress_h
#include
#include
#include
// A CLASS TO MAKE IT EASIER TO HANDLE AND PASS AROUND IP ADDRESSES
class IPAddress: public Printable
{
private:
union {
uint8_t bytes[4]; // IPv4 ADDRESS
uint32_t dword;
} _address;
// ACCESS RAW BYTE ARRAY CONTAINING ADDRESS. BECAUSE THIS RETURNS A
// POINTER TO INTERNAL STRUCTURE RATHER THAN A COPY OF ADDRESS THIS
// FUNCTION SHOULD ONLY BE USED WHEN YOU KNOW THAT USAGE OF RETURNED
// uint8_t* WILL BE TRANSIENT AND NOT STORED.
uint8_t *raw_address()
{
return _address.bytes;
}
public:
// CONSTRUCTORS
IPAddress();
IPAddress( uint8_t first_octet, uint8_t second_octet, uint8_t third_octet
, uint8_t fourth_octet);
IPAddress( uint32_t address );
IPAddress( const uint8_t *address );
virtual ~IPAddress() {}
bool fromString( const char *address );
bool fromString( const String &address )
{ return( fromString( address.c_str()) ); }
// OVERLOADED CAST OPERATOR TO ALLOW IPAddress OBJECTS TO BE USED WHERE A
// POINTER TO A FOUR-BYTE uint8_t ARRAY IS EXPECTED
operator uint32_t() const
{
return( _address.dword );
}
bool operator == ( const IPAddress &addr) const
{
return _address.dword == addr._address.dword;
}
bool operator == ( const uint8_t *addr ) const;
// OVERLOADED INDEX OPERATOR TO ALLOW GETTING AND SETTING INDIVIDUAL
// OCTETS OF ADDRESS
uint8_t operator[]( int index ) const
{
return( _address.bytes[index] );
}
uint8_t& operator[]( int index )
{
return( _address.bytes[index] );
}
// OVERLOADED COPY OPERATORS TO ALLOW INITIALISATION OF IPAddress
// OBJECTS FROM OTHER TYPES
IPAddress& operator=( const uint8_t *address );
IPAddress& operator=( uint32_t address );
virtual size_t printTo( Print &p ) const;
String toString() const;
friend class EthernetClass;
friend class UDP;
friend class Client;
friend class Server;
friend class DhcpClass;
friend class DNSClient;
};
// CHANGED TO EXTERN BECAUSE CONST DECLARATION CREATES COPIES IN BSS OF
// INADDR_NONE FOR EACH CPP UNIT THAT INCLUDES IT
extern IPAddress INADDR_NONE;
#endif