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