mbed OS5
Fork of UIPEthernet by
utility/IPAddress.h@9:e55652bed36c, 2017-08-06 (annotated)
- Committer:
- pilotak
- Date:
- Sun Aug 06 16:01:26 2017 +0000
- Revision:
- 9:e55652bed36c
- Parent:
- 8:4acb22344932
mBed OS5
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 | 8:4acb22344932 | 21 | #ifndef IPADDRESS_H |
hudakz | 8:4acb22344932 | 22 | #define IPADDRESS_H |
hudakz | 3:5b17e4656dd0 | 23 | |
hudakz | 8:4acb22344932 | 24 | #include <stdint.h> |
hudakz | 3:5b17e4656dd0 | 25 | |
hudakz | 3:5b17e4656dd0 | 26 | // A class to make it easier to handle and pass around IP addresses |
hudakz | 3:5b17e4656dd0 | 27 | |
hudakz | 3:5b17e4656dd0 | 28 | class IPAddress |
hudakz | 3:5b17e4656dd0 | 29 | { |
hudakz | 3:5b17e4656dd0 | 30 | private: |
hudakz | 3:5b17e4656dd0 | 31 | uint8_t _address[4]; // IPv4 address |
hudakz | 3:5b17e4656dd0 | 32 | |
hudakz | 3:5b17e4656dd0 | 33 | // Access the raw byte array containing the address. Because this returns a pointer |
hudakz | 3:5b17e4656dd0 | 34 | // to the internal structure rather than a copy of the address this function should only |
hudakz | 3:5b17e4656dd0 | 35 | // be used when you know that the usage of the returned uint8_t* will be transient and not |
hudakz | 3:5b17e4656dd0 | 36 | |
hudakz | 3:5b17e4656dd0 | 37 | // stored. |
hudakz | 3:5b17e4656dd0 | 38 | uint8_t* raw_address(void) { return _address; }; |
hudakz | 3:5b17e4656dd0 | 39 | public: |
hudakz | 3:5b17e4656dd0 | 40 | // Constructors |
hudakz | 3:5b17e4656dd0 | 41 | IPAddress(void); |
hudakz | 3:5b17e4656dd0 | 42 | IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet); |
hudakz | 3:5b17e4656dd0 | 43 | IPAddress(uint32_t address); |
hudakz | 3:5b17e4656dd0 | 44 | IPAddress(const uint8_t* address); |
hudakz | 3:5b17e4656dd0 | 45 | |
hudakz | 3:5b17e4656dd0 | 46 | // Overloaded cast operator to allow IPAddress objects to be used where a pointer |
hudakz | 3:5b17e4656dd0 | 47 | // to a four-byte uint8_t array is expected |
hudakz | 3:5b17e4656dd0 | 48 | operator uint32_t(void) const { return *((uint32_t*)_address); }; |
hudakz | 3:5b17e4656dd0 | 49 | bool operator==(const IPAddress& addr) const { return(*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); }; |
hudakz | 3:5b17e4656dd0 | 50 | bool operator==(const uint8_t* addr) const; |
hudakz | 3:5b17e4656dd0 | 51 | |
hudakz | 3:5b17e4656dd0 | 52 | // Overloaded index operator to allow getting and setting individual octets of the address |
hudakz | 3:5b17e4656dd0 | 53 | uint8_t operator[](int index) const { return _address[index]; }; |
hudakz | 3:5b17e4656dd0 | 54 | uint8_t &operator[](int index) { return _address[index]; }; |
hudakz | 3:5b17e4656dd0 | 55 | |
hudakz | 3:5b17e4656dd0 | 56 | // Overloaded copy operators to allow initialisation of IPAddress objects from other types |
hudakz | 3:5b17e4656dd0 | 57 | IPAddress &operator =(const uint8_t* address); |
hudakz | 3:5b17e4656dd0 | 58 | IPAddress &operator =(uint32_t address); |
hudakz | 3:5b17e4656dd0 | 59 | |
hudakz | 8:4acb22344932 | 60 | // Returns IP Address as string of char |
hudakz | 8:4acb22344932 | 61 | char* toString(void) { |
hudakz | 8:4acb22344932 | 62 | static char buff[16]; |
hudakz | 8:4acb22344932 | 63 | uint8_t i = 0; |
hudakz | 8:4acb22344932 | 64 | uint8_t j = 0; |
hudakz | 8:4acb22344932 | 65 | |
hudakz | 8:4acb22344932 | 66 | for (i = 0; i < 3; i++) { |
hudakz | 8:4acb22344932 | 67 | j += sprintf(&buff[j], "%d", _address[i]); |
hudakz | 8:4acb22344932 | 68 | buff[j++] = '.'; |
hudakz | 8:4acb22344932 | 69 | } |
hudakz | 8:4acb22344932 | 70 | |
hudakz | 8:4acb22344932 | 71 | j += sprintf(&buff[j], "%d", _address[i]); |
hudakz | 8:4acb22344932 | 72 | buff[j] = '\0'; |
hudakz | 8:4acb22344932 | 73 | return buff; |
hudakz | 8:4acb22344932 | 74 | } |
hudakz | 8:4acb22344932 | 75 | |
hudakz | 3:5b17e4656dd0 | 76 | // virtual size_t printTo(Print& p) const; |
hudakz | 3:5b17e4656dd0 | 77 | friend class EthernetClass; |
hudakz | 3:5b17e4656dd0 | 78 | friend class UDP; |
hudakz | 3:5b17e4656dd0 | 79 | friend class Client; |
hudakz | 3:5b17e4656dd0 | 80 | friend class Server; |
hudakz | 3:5b17e4656dd0 | 81 | friend class DhcpClass; |
hudakz | 3:5b17e4656dd0 | 82 | friend class DNSClient; |
hudakz | 3:5b17e4656dd0 | 83 | }; |
hudakz | 3:5b17e4656dd0 | 84 | |
hudakz | 3:5b17e4656dd0 | 85 | const IPAddress INADDR_NONE(0, 0, 0, 0); |
hudakz | 3:5b17e4656dd0 | 86 | #endif |