Skip to content

Commit

Permalink
Add from ip_addr_t conversion and better toString implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
me-no-dev committed Dec 18, 2023
1 parent b8cc73c commit 305d276
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
51 changes: 28 additions & 23 deletions cores/esp32/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "IPAddress.h"
#include "Print.h"
#include "lwip/netif.h"
#include "StreamString.h"

IPAddress::IPAddress() : IPAddress(IPv4) {}

Expand Down Expand Up @@ -103,31 +104,11 @@ IPAddress::IPAddress(const IPAddress& address)
*this = address;
}

String IPAddress::toString4() const
{
char szRet[16];
snprintf(szRet, sizeof(szRet), "%u.%u.%u.%u", _address.bytes[IPADDRESS_V4_BYTES_INDEX], _address.bytes[IPADDRESS_V4_BYTES_INDEX + 1], _address.bytes[IPADDRESS_V4_BYTES_INDEX + 2], _address.bytes[IPADDRESS_V4_BYTES_INDEX + 3]);
return String(szRet);
}

String IPAddress::toString6() const
{
char szRet[40];
snprintf(szRet, sizeof(szRet), "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
_address.bytes[0], _address.bytes[1], _address.bytes[2], _address.bytes[3],
_address.bytes[4], _address.bytes[5], _address.bytes[6], _address.bytes[7],
_address.bytes[8], _address.bytes[9], _address.bytes[10], _address.bytes[11],
_address.bytes[12], _address.bytes[13], _address.bytes[14], _address.bytes[15]);
return String(szRet);
}

String IPAddress::toString() const
{
if (_type == IPv4) {
return toString4();
} else {
return toString6();
}
StreamString s;
printTo(s);
return String(s);
}

bool IPAddress::fromString(const char *address) {
Expand Down Expand Up @@ -373,6 +354,13 @@ size_t IPAddress::printTo(Print& p) const
n += p.print(':');
}
}
// add a zone if zone-id is non-zero
if(_zone > 0){
n += p.print('%');
char if_name[NETIF_NAMESIZE];
netif_index_to_name(_zone, if_name);
n += p.print(if_name);
}
return n;
}

Expand Down Expand Up @@ -402,5 +390,22 @@ void IPAddress::to_ip_addr_t(ip_addr_t* addr){
}
}

IPAddress& IPAddress::from_ip_addr_t(ip_addr_t* addr){
if(addr->type == IPADDR_TYPE_V6){
_type = IPv6;
_address.dword[0] = addr->u_addr.ip6.addr[0];
_address.dword[1] = addr->u_addr.ip6.addr[1];
_address.dword[2] = addr->u_addr.ip6.addr[2];
_address.dword[3] = addr->u_addr.ip6.addr[3];
#if LWIP_IPV6_SCOPES
_zone = addr->u_addr.ip6.zone;
#endif /* LWIP_IPV6_SCOPES */
} else {
_type = IPv4;
_address.dword[IPADDRESS_V4_DWORD_INDEX] = addr->u_addr.ip4.addr;
}
return *this;
}

const IPAddress IN6ADDR_ANY(IPv6);
const IPAddress INADDR_NONE(0,0,0,0);
7 changes: 4 additions & 3 deletions cores/esp32/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,19 @@ class IPAddress : public Printable {

IPType type() const { return _type; }

void to_ip_addr_t(ip_addr_t* addr);
uint8_t zone() const { return (type() == IPv6)?_zone:0; }

// LwIP conversions
void to_ip_addr_t(ip_addr_t* addr);
IPAddress& from_ip_addr_t(ip_addr_t* addr);

friend class UDP;
friend class Client;
friend class Server;

protected:
bool fromString4(const char *address);
bool fromString6(const char *address);
String toString4() const;
String toString6() const;
};

extern const IPAddress IN6ADDR_ANY;
Expand Down
3 changes: 2 additions & 1 deletion cores/esp32/StreamString.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

#ifndef STREAMSTRING_H_
#define STREAMSTRING_H_

#include "Stream.h"
#include "WString.h"

class StreamString: public Stream, public String
{
Expand Down

0 comments on commit 305d276

Please sign in to comment.