mbed OS5
Fork of UIPEthernet by
utility/IPAddress.h@3:5b17e4656dd0, 2014-12-20 (annotated)
- Committer:
- hudakz
- Date:
- Sat Dec 20 11:10:40 2014 +0000
- Revision:
- 3:5b17e4656dd0
- Child:
- 8:4acb22344932
02 Name clash with "Ethernet" fixed for LPC1768
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 3:5b17e4656dd0 | 1 | /* |
hudakz | 3:5b17e4656dd0 | 2 | IPAddress.h - Base class that provides IPAddress |
hudakz | 3:5b17e4656dd0 | 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. |
hudakz | 3:5b17e4656dd0 | 4 | |
hudakz | 3:5b17e4656dd0 | 5 | Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com> |
hudakz | 3:5b17e4656dd0 | 6 | |
hudakz | 3:5b17e4656dd0 | 7 | This library is free software; you can redistribute it and/or |
hudakz | 3:5b17e4656dd0 | 8 | modify it under the terms of the GNU Lesser General Public |
hudakz | 3:5b17e4656dd0 | 9 | License as published by the Free Software Foundation; either |
hudakz | 3:5b17e4656dd0 | 10 | version 2.1 of the License, or (at your option) any later version. |
hudakz | 3:5b17e4656dd0 | 11 | |
hudakz | 3:5b17e4656dd0 | 12 | This library is distributed in the hope that it will be useful, |
hudakz | 3:5b17e4656dd0 | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hudakz | 3:5b17e4656dd0 | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
hudakz | 3:5b17e4656dd0 | 15 | Lesser General Public License for more details. |
hudakz | 3:5b17e4656dd0 | 16 | |
hudakz | 3:5b17e4656dd0 | 17 | You should have received a copy of the GNU Lesser General Public |
hudakz | 3:5b17e4656dd0 | 18 | License along with this library; if not, write to the Free Software |
hudakz | 3:5b17e4656dd0 | 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
hudakz | 3:5b17e4656dd0 | 20 | */ |
hudakz | 3:5b17e4656dd0 | 21 | #ifndef IPAddress_h |
hudakz | 3:5b17e4656dd0 | 22 | #define IPAddress_h |
hudakz | 3:5b17e4656dd0 | 23 | |
hudakz | 3:5b17e4656dd0 | 24 | #include <stdint.h> |
hudakz | 3:5b17e4656dd0 | 25 | |
hudakz | 3:5b17e4656dd0 | 26 | //#include <Printable.h> |
hudakz | 3:5b17e4656dd0 | 27 | |
hudakz | 3:5b17e4656dd0 | 28 | // A class to make it easier to handle and pass around IP addresses |
hudakz | 3:5b17e4656dd0 | 29 | |
hudakz | 3:5b17e4656dd0 | 30 | //class IPAddress : public Printable { |
hudakz | 3:5b17e4656dd0 | 31 | class IPAddress |
hudakz | 3:5b17e4656dd0 | 32 | { |
hudakz | 3:5b17e4656dd0 | 33 | private: |
hudakz | 3:5b17e4656dd0 | 34 | uint8_t _address[4]; // IPv4 address |
hudakz | 3:5b17e4656dd0 | 35 | |
hudakz | 3:5b17e4656dd0 | 36 | // Access the raw byte array containing the address. Because this returns a pointer |
hudakz | 3:5b17e4656dd0 | 37 | // to the internal structure rather than a copy of the address this function should only |
hudakz | 3:5b17e4656dd0 | 38 | // be used when you know that the usage of the returned uint8_t* will be transient and not |
hudakz | 3:5b17e4656dd0 | 39 | |
hudakz | 3:5b17e4656dd0 | 40 | // stored. |
hudakz | 3:5b17e4656dd0 | 41 | uint8_t* raw_address(void) { return _address; }; |
hudakz | 3:5b17e4656dd0 | 42 | public: |
hudakz | 3:5b17e4656dd0 | 43 | // Constructors |
hudakz | 3:5b17e4656dd0 | 44 | IPAddress(void); |
hudakz | 3:5b17e4656dd0 | 45 | IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); |
hudakz | 3:5b17e4656dd0 | 46 | IPAddress(uint32_t address); |
hudakz | 3:5b17e4656dd0 | 47 | IPAddress(const uint8_t* address); |
hudakz | 3:5b17e4656dd0 | 48 | |
hudakz | 3:5b17e4656dd0 | 49 | // Overloaded cast operator to allow IPAddress objects to be used where a pointer |
hudakz | 3:5b17e4656dd0 | 50 | // to a four-byte uint8_t array is expected |
hudakz | 3:5b17e4656dd0 | 51 | operator uint32_t(void) const { return *((uint32_t*)_address); }; |
hudakz | 3:5b17e4656dd0 | 52 | bool operator==(const IPAddress& addr) const { return(*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); }; |
hudakz | 3:5b17e4656dd0 | 53 | bool operator==(const uint8_t* addr) const; |
hudakz | 3:5b17e4656dd0 | 54 | |
hudakz | 3:5b17e4656dd0 | 55 | // Overloaded index operator to allow getting and setting individual octets of the address |
hudakz | 3:5b17e4656dd0 | 56 | uint8_t operator[](int index) const { return _address[index]; }; |
hudakz | 3:5b17e4656dd0 | 57 | uint8_t &operator[](int index) { return _address[index]; }; |
hudakz | 3:5b17e4656dd0 | 58 | |
hudakz | 3:5b17e4656dd0 | 59 | // Overloaded copy operators to allow initialisation of IPAddress objects from other types |
hudakz | 3:5b17e4656dd0 | 60 | IPAddress &operator =(const uint8_t* address); |
hudakz | 3:5b17e4656dd0 | 61 | IPAddress &operator =(uint32_t address); |
hudakz | 3:5b17e4656dd0 | 62 | |
hudakz | 3:5b17e4656dd0 | 63 | // virtual size_t printTo(Print& p) const; |
hudakz | 3:5b17e4656dd0 | 64 | friend class EthernetClass; |
hudakz | 3:5b17e4656dd0 | 65 | friend class UDP; |
hudakz | 3:5b17e4656dd0 | 66 | friend class Client; |
hudakz | 3:5b17e4656dd0 | 67 | friend class Server; |
hudakz | 3:5b17e4656dd0 | 68 | friend class DhcpClass; |
hudakz | 3:5b17e4656dd0 | 69 | friend class DNSClient; |
hudakz | 3:5b17e4656dd0 | 70 | }; |
hudakz | 3:5b17e4656dd0 | 71 | |
hudakz | 3:5b17e4656dd0 | 72 | const IPAddress INADDR_NONE(0, 0, 0, 0); |
hudakz | 3:5b17e4656dd0 | 73 | #endif |