mbed OS5

Fork of UIPEthernet by Zoltan Hudak

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IPAddress.h Source File

IPAddress.h

00001 /*
00002   IPAddress.h - Base class that provides IPAddress
00003   Copyright (c) 2011 Adrian McEwen.  All right reserved.
00004 
00005   Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com>
00006 
00007   This library is free software; you can redistribute it and/or
00008   modify it under the terms of the GNU Lesser General Public
00009   License as published by the Free Software Foundation; either
00010   version 2.1 of the License, or (at your option) any later version.
00011 
00012   This library is distributed in the hope that it will be useful,
00013   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015   Lesser General Public License for more details.
00016 
00017   You should have received a copy of the GNU Lesser General Public
00018   License along with this library; if not, write to the Free Software
00019   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 */
00021 #ifndef IPADDRESS_H
00022 #define IPADDRESS_H
00023 
00024 #include <stdint.h>
00025 
00026 // A class to make it easier to handle and pass around IP addresses
00027 
00028 class   IPAddress
00029 {
00030 private:
00031     uint8_t     _address[4];    // IPv4 address
00032 
00033     // Access the raw byte array containing the address.  Because this returns a pointer
00034     // to the internal structure rather than a copy of the address this function should only
00035     // be used when you know that the usage of the returned uint8_t* will be transient and not
00036 
00037     // stored.
00038     uint8_t*    raw_address(void)   { return _address; };
00039 public:
00040     // Constructors
00041     IPAddress(void);
00042     IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
00043     IPAddress(uint32_t address);
00044     IPAddress(const uint8_t* address);
00045 
00046     // Overloaded cast operator to allow IPAddress objects to be used where a pointer
00047     // to a four-byte uint8_t array is expected
00048     operator uint32_t(void) const    { return *((uint32_t*)_address); };
00049     bool operator==(const IPAddress& addr) const { return(*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
00050     bool operator==(const uint8_t* addr) const;
00051 
00052     // Overloaded index operator to allow getting and setting individual octets of the address
00053     uint8_t operator[](int index) const { return _address[index]; };
00054     uint8_t &operator[](int index)      { return _address[index]; };
00055 
00056     // Overloaded copy operators to allow initialisation of IPAddress objects from other types
00057     IPAddress &operator =(const uint8_t* address);
00058     IPAddress &operator =(uint32_t address);
00059 
00060     // Returns IP Address as string of char
00061     char* toString(void) {
00062         static char buff[16];
00063         uint8_t     i = 0;
00064         uint8_t     j = 0;
00065 
00066         for (i = 0; i < 3; i++) {
00067             j += sprintf(&buff[j], "%d", _address[i]);
00068             buff[j++] = '.';
00069         }
00070 
00071         j += sprintf(&buff[j], "%d", _address[i]);
00072         buff[j] = '\0';
00073         return buff;
00074     }
00075 
00076     //    virtual size_t printTo(Print& p) const;
00077     friend class        EthernetClass;
00078     friend class        UDP;
00079     friend class        Client;
00080     friend class        Server;
00081     friend class        DhcpClass;
00082     friend class        DNSClient;
00083 };
00084 
00085 const IPAddress INADDR_NONE(0, 0, 0, 0);
00086 #endif