2016-10-06 11:21:30 +00:00
|
|
|
/*
|
2024-01-15 13:24:34 +00:00
|
|
|
IPAddress.h - Base class that provides IPAddress
|
|
|
|
Copyright (c) 2011 Adrian McEwen. All right reserved.
|
2016-10-06 11:21:30 +00:00
|
|
|
|
2024-01-15 13:24:34 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2016-10-06 11:21:30 +00:00
|
|
|
|
2024-01-15 13:24:34 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
2016-10-06 11:21:30 +00:00
|
|
|
|
2024-01-15 13:24:34 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2016-10-06 11:21:30 +00:00
|
|
|
|
2024-01-15 13:24:34 +00:00
|
|
|
#pragma once
|
2016-10-06 11:21:30 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2024-01-15 13:24:34 +00:00
|
|
|
#include "Printable.h"
|
|
|
|
#include "WString.h"
|
|
|
|
#include "lwip/ip_addr.h"
|
|
|
|
#include "esp_netif_ip_addr.h"
|
2016-10-06 11:21:30 +00:00
|
|
|
|
2023-12-18 12:19:12 +00:00
|
|
|
#define IPADDRESS_V4_BYTES_INDEX 12
|
|
|
|
#define IPADDRESS_V4_DWORD_INDEX 3
|
|
|
|
|
2024-01-15 13:24:34 +00:00
|
|
|
// A class to make it easier to handle and pass around IP addresses
|
|
|
|
|
|
|
|
enum IPType {
|
2023-12-18 12:19:12 +00:00
|
|
|
IPv4,
|
|
|
|
IPv6
|
|
|
|
};
|
|
|
|
|
2024-01-15 13:24:34 +00:00
|
|
|
class IPAddress : public Printable {
|
2016-10-06 11:21:30 +00:00
|
|
|
private:
|
|
|
|
union {
|
2023-12-18 12:19:12 +00:00
|
|
|
uint8_t bytes[16];
|
|
|
|
uint32_t dword[4];
|
2016-10-06 11:21:30 +00:00
|
|
|
} _address;
|
2023-12-18 12:19:12 +00:00
|
|
|
IPType _type;
|
2024-01-15 13:24:34 +00:00
|
|
|
uint8_t _zone;
|
2016-10-06 11:21:30 +00:00
|
|
|
|
|
|
|
// Access the raw byte array containing the address. Because this returns a pointer
|
|
|
|
// to the internal structure rather than a copy of the address this function should only
|
|
|
|
// be used when you know that the usage of the returned uint8_t* will be transient and not
|
|
|
|
// stored.
|
2024-01-15 13:24:34 +00:00
|
|
|
uint8_t* raw_address() { return _type == IPv4 ? &_address.bytes[IPADDRESS_V4_BYTES_INDEX] : _address.bytes; }
|
2016-10-06 11:21:30 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Constructors
|
2024-01-15 13:24:34 +00:00
|
|
|
|
|
|
|
// Default IPv4
|
2016-10-06 11:21:30 +00:00
|
|
|
IPAddress();
|
2023-12-18 12:19:12 +00:00
|
|
|
IPAddress(IPType ip_type);
|
2016-10-06 11:21:30 +00:00
|
|
|
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
|
2024-01-15 13:24:34 +00:00
|
|
|
IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5, uint8_t o6, uint8_t o7, uint8_t o8, uint8_t o9, uint8_t o10, uint8_t o11, uint8_t o12, uint8_t o13, uint8_t o14, uint8_t o15, uint8_t o16, uint8_t z=0);
|
|
|
|
// IPv4; see implementation note
|
2016-10-06 11:21:30 +00:00
|
|
|
IPAddress(uint32_t address);
|
2024-01-15 13:24:34 +00:00
|
|
|
// Default IPv4
|
2016-10-06 11:21:30 +00:00
|
|
|
IPAddress(const uint8_t *address);
|
2024-01-15 13:24:34 +00:00
|
|
|
IPAddress(IPType ip_type, const uint8_t *address, uint8_t z=0);
|
2023-12-18 12:19:12 +00:00
|
|
|
// If IPv4 fails tries IPv6 see fromString function
|
|
|
|
IPAddress(const char *address);
|
2024-01-15 13:24:34 +00:00
|
|
|
IPAddress(const IPAddress& address);
|
2016-10-06 11:21:30 +00:00
|
|
|
|
2016-11-24 07:46:11 +00:00
|
|
|
bool fromString(const char *address);
|
|
|
|
bool fromString(const String &address) { return fromString(address.c_str()); }
|
|
|
|
|
2024-01-15 13:24:34 +00:00
|
|
|
// Overloaded cast operator to allow IPAddress objects to be used where a uint32_t is expected
|
|
|
|
// NOTE: IPv4 only; see implementation note
|
|
|
|
operator uint32_t() const { return _type == IPv4 ? _address.dword[IPADDRESS_V4_DWORD_INDEX] : 0; };
|
2023-12-18 12:19:12 +00:00
|
|
|
|
|
|
|
bool operator==(const IPAddress& addr) const;
|
2024-01-15 13:24:34 +00:00
|
|
|
bool operator!=(const IPAddress& addr) const { return !(*this == addr); };
|
|
|
|
|
|
|
|
// NOTE: IPv4 only; we don't know the length of the pointer
|
2016-10-06 11:21:30 +00:00
|
|
|
bool operator==(const uint8_t* addr) const;
|
|
|
|
|
|
|
|
// Overloaded index operator to allow getting and setting individual octets of the address
|
2023-12-18 12:19:12 +00:00
|
|
|
uint8_t operator[](int index) const;
|
|
|
|
uint8_t& operator[](int index);
|
2016-10-06 11:21:30 +00:00
|
|
|
|
|
|
|
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
|
2024-01-15 13:24:34 +00:00
|
|
|
// NOTE: IPv4 only
|
2016-10-06 11:21:30 +00:00
|
|
|
IPAddress& operator=(const uint8_t *address);
|
2024-01-15 13:24:34 +00:00
|
|
|
// NOTE: IPv4 only; see implementation note
|
2016-10-06 11:21:30 +00:00
|
|
|
IPAddress& operator=(uint32_t address);
|
2023-12-18 12:19:12 +00:00
|
|
|
// If IPv4 fails tries IPv6 see fromString function
|
|
|
|
IPAddress& operator=(const char *address);
|
2024-01-15 13:24:34 +00:00
|
|
|
IPAddress& operator=(const IPAddress& address);
|
2016-10-06 11:21:30 +00:00
|
|
|
|
|
|
|
virtual size_t printTo(Print& p) const;
|
2024-01-15 13:24:34 +00:00
|
|
|
String toString(bool includeZone = false) const;
|
2016-10-06 11:21:30 +00:00
|
|
|
|
2023-12-18 12:19:12 +00:00
|
|
|
IPType type() const { return _type; }
|
|
|
|
|
2024-01-15 13:24:34 +00:00
|
|
|
// Espresif LwIP conversions
|
|
|
|
IPAddress(const ip_addr_t *addr);
|
|
|
|
void to_ip_addr_t(ip_addr_t* addr) const;
|
|
|
|
IPAddress& from_ip_addr_t(const ip_addr_t* addr);
|
|
|
|
esp_ip6_addr_type_t addr_type() const;
|
|
|
|
uint8_t zone() const { return (type() == IPv6)?_zone:0; }
|
|
|
|
size_t printTo(Print& p, bool includeZone) const;
|
|
|
|
|
2016-10-06 11:21:30 +00:00
|
|
|
friend class UDP;
|
|
|
|
friend class Client;
|
|
|
|
friend class Server;
|
2023-12-18 12:19:12 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool fromString4(const char *address);
|
|
|
|
bool fromString6(const char *address);
|
2016-10-06 11:21:30 +00:00
|
|
|
};
|
|
|
|
|
2024-01-15 13:24:34 +00:00
|
|
|
extern const IPAddress IN6ADDR_ANY;
|
|
|
|
extern const IPAddress INADDR_NONE;
|