123
IpAddress.h@19:543c2d21e510, 2022-06-06 (annotated)
- Committer:
- advxolltm
- Date:
- Mon Jun 06 16:36:52 2022 +0000
- Revision:
- 19:543c2d21e510
- Parent:
- 16:269f652b4d0b
123
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 9:a156d3de5647 | 1 | /* |
hudakz | 9:a156d3de5647 | 2 | IpAddress.h - Base class that provides IPAddress |
hudakz | 9:a156d3de5647 | 3 | Copyright (c) 2011 Adrian McEwen. All right reserved. |
hudakz | 9:a156d3de5647 | 4 | |
hudakz | 9:a156d3de5647 | 5 | Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com> |
hudakz | 9:a156d3de5647 | 6 | |
hudakz | 9:a156d3de5647 | 7 | This library is free software; you can redistribute it and/or |
hudakz | 9:a156d3de5647 | 8 | modify it under the terms of the GNU Lesser General Public |
hudakz | 9:a156d3de5647 | 9 | License as published by the Free Software Foundation; either |
hudakz | 9:a156d3de5647 | 10 | version 2.1 of the License, or (at your option) any later version. |
hudakz | 9:a156d3de5647 | 11 | |
hudakz | 9:a156d3de5647 | 12 | This library is distributed in the hope that it will be useful, |
hudakz | 9:a156d3de5647 | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hudakz | 9:a156d3de5647 | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
hudakz | 9:a156d3de5647 | 15 | Lesser General Public License for more details. |
hudakz | 9:a156d3de5647 | 16 | |
hudakz | 9:a156d3de5647 | 17 | You should have received a copy of the GNU Lesser General Public |
hudakz | 9:a156d3de5647 | 18 | License along with this library; if not, write to the Free Software |
hudakz | 9:a156d3de5647 | 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
hudakz | 9:a156d3de5647 | 20 | */ |
hudakz | 11:647d53d146f1 | 21 | #ifndef IPADDRESS_h |
hudakz | 11:647d53d146f1 | 22 | #define IPADDRESS_h |
hudakz | 9:a156d3de5647 | 23 | |
hudakz | 9:a156d3de5647 | 24 | #include <stdio.h> |
hudakz | 16:269f652b4d0b | 25 | #include "SocketAddress.h" |
hudakz | 9:a156d3de5647 | 26 | |
hudakz | 9:a156d3de5647 | 27 | // A class to make it easier to handle and pass around IP addresses |
hudakz | 9:a156d3de5647 | 28 | |
hudakz | 9:a156d3de5647 | 29 | class IpAddress |
hudakz | 9:a156d3de5647 | 30 | { |
hudakz | 9:a156d3de5647 | 31 | private: |
hudakz | 9:a156d3de5647 | 32 | uint8_t _address[4]; // IPv4 address |
hudakz | 9:a156d3de5647 | 33 | |
hudakz | 9:a156d3de5647 | 34 | public: |
hudakz | 9:a156d3de5647 | 35 | // Constructors |
hudakz | 9:a156d3de5647 | 36 | IpAddress(void); |
hudakz | 9:a156d3de5647 | 37 | IpAddress(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4); |
hudakz | 9:a156d3de5647 | 38 | IpAddress(uint32_t address); |
hudakz | 9:a156d3de5647 | 39 | IpAddress(const uint8_t address[4]); |
hudakz | 11:647d53d146f1 | 40 | IpAddress(const char *str, size_t len); |
hudakz | 9:a156d3de5647 | 41 | |
hudakz | 9:a156d3de5647 | 42 | // Overloaded cast operator to allow IPAddress objects to be used where a pointer |
hudakz | 9:a156d3de5647 | 43 | // to a four-byte uint8_t array is expected |
hudakz | 9:a156d3de5647 | 44 | operator uint32_t(void) const { return *((uint32_t*)_address); } |
hudakz | 9:a156d3de5647 | 45 | bool operator==(const IpAddress& addr) const { return(*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); } |
hudakz | 9:a156d3de5647 | 46 | bool operator==(const uint8_t* addr) const; |
hudakz | 9:a156d3de5647 | 47 | |
hudakz | 9:a156d3de5647 | 48 | // Overloaded index operator to allow getting and setting individual octets of the address |
hudakz | 9:a156d3de5647 | 49 | uint8_t operator[](int index) const { return _address[index]; } |
hudakz | 9:a156d3de5647 | 50 | uint8_t &operator[](int index) { return _address[index]; } |
hudakz | 9:a156d3de5647 | 51 | |
hudakz | 9:a156d3de5647 | 52 | // Overloaded copy operators to allow initialisation of IPAddress objects from other types |
hudakz | 9:a156d3de5647 | 53 | IpAddress &operator =(uint32_t address); |
hudakz | 11:647d53d146f1 | 54 | IpAddress &operator =(const uint8_t* address); |
hudakz | 9:a156d3de5647 | 55 | |
hudakz | 9:a156d3de5647 | 56 | // Returns IP Address as string of char |
hudakz | 9:a156d3de5647 | 57 | const char* toString(char* buf); |
hudakz | 9:a156d3de5647 | 58 | |
hudakz | 16:269f652b4d0b | 59 | void toSocketAddress(SocketAddress* sockAddr) { sockAddr->set_ip_bytes((const void*)_address, NSAPI_IPv4); } |
hudakz | 16:269f652b4d0b | 60 | |
hudakz | 9:a156d3de5647 | 61 | // Access the raw byte array containing the address. Because this returns a pointer |
hudakz | 9:a156d3de5647 | 62 | // to the internal structure rather than a copy of the address this function should only |
hudakz | 9:a156d3de5647 | 63 | // be used when you know that the usage of the returned uint8_t* will be transient and not |
hudakz | 9:a156d3de5647 | 64 | // stored. |
hudakz | 9:a156d3de5647 | 65 | uint8_t* rawAddress(void) { return _address; } |
hudakz | 9:a156d3de5647 | 66 | |
hudakz | 9:a156d3de5647 | 67 | friend class UIPEthernet; |
hudakz | 9:a156d3de5647 | 68 | friend class UdpSocket; |
hudakz | 9:a156d3de5647 | 69 | friend class TcpClient; |
hudakz | 9:a156d3de5647 | 70 | friend class TcpServer; |
hudakz | 9:a156d3de5647 | 71 | friend class DhcpClient; |
hudakz | 9:a156d3de5647 | 72 | friend class DnsClient; |
hudakz | 9:a156d3de5647 | 73 | }; |
hudakz | 9:a156d3de5647 | 74 | |
hudakz | 9:a156d3de5647 | 75 | const IpAddress INADDR_NONE(0, 0, 0, 0); |
hudakz | 9:a156d3de5647 | 76 | #endif |