Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This is a continuation on the topic of adding IPv6 Support to ESP32 Arduino #9016

Merged
merged 23 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7b42a93
IPv6 for Arduino 3.0.0
Jason2866 Nov 21, 2023
12be369
Fix warning in WifiUdp
s-hadinger Nov 21, 2023
5987211
remove comment / formating
Jason2866 Nov 22, 2023
b8cc73c
Add zone to IPAddress and update WiFiUDP and WiFiGeneric
me-no-dev Dec 18, 2023
305d276
Add from ip_addr_t conversion and better toString implementation
me-no-dev Dec 18, 2023
41fb1a1
Use constant for IPAddress offset
me-no-dev Dec 18, 2023
b37ce6c
Combine hostByName to support both IPv6 and IPv4 results
me-no-dev Dec 18, 2023
e333afb
implement logic to use v6 dns only when global v6 address is assigned…
me-no-dev Jan 11, 2024
c7e63e6
Rename softAPenableIPv6
me-no-dev Jan 11, 2024
55aaa6f
Rename mDNS methods
me-no-dev Jan 11, 2024
c4f366c
fix IPAddress method to work with const address
me-no-dev Jan 11, 2024
701d35f
Some cleanup and do not print zone in IPAddress
me-no-dev Jan 11, 2024
a1b3f16
Merge branch 'master' into feature/ipv6_support
me-no-dev Jan 11, 2024
cb381f2
rename WiFiMulti method
me-no-dev Jan 11, 2024
726496c
Fix AP DHCPS not properly working on recent IDF
me-no-dev Jan 11, 2024
9012f64
Add option to print the zone at the end of IPv6
me-no-dev Jan 11, 2024
58520a7
remove log prints from hostByName
me-no-dev Jan 11, 2024
aad1041
Use correct array length for listing IPv6 addresses
me-no-dev Jan 12, 2024
04a2034
Merge branch 'master' into feature/ipv6_support
me-no-dev Jan 12, 2024
a2a6bd8
Implement some Tasmota requirements
me-no-dev Jan 14, 2024
1fb442d
add 'const' to IPAddress::addr_type()
me-no-dev Jan 14, 2024
0df3aaa
Fix WiFiUdp not updating mapped v4 address
me-no-dev Jan 15, 2024
4161873
Update WiFiServer.cpp
me-no-dev Jan 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
326 changes: 182 additions & 144 deletions cores/esp32/IPAddress.cpp

Large diffs are not rendered by default.

89 changes: 45 additions & 44 deletions cores/esp32/IPAddress.h
Original file line number Diff line number Diff line change
@@ -1,105 +1,108 @@
/*
IPAddress.h - Base class that provides IPAddress
Copyright (c) 2011 Adrian McEwen. All right reserved.
IPAddress.h - Base class that provides IPAddress
Copyright (c) 2011 Adrian McEwen. All right reserved.

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.
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.

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.
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.

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
*/
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
*/

#ifndef IPAddress_h
#define IPAddress_h
#pragma once

#include <stdint.h>
#include <WString.h>
#include <Printable.h>

// A class to make it easier to handle and pass around IP addresses
#include "Printable.h"
#include "WString.h"
#include "lwip/ip_addr.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to have a separate IPAddressConverter class, that converts to & from LWIP? That would mean you don't need the dependency here. Just an idea.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it make more sense to have a constructor accepting the LWIP implementation and some set/get-functions?
If you want, you can also wrap those in some #ifdef or #if check.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep it now as it is. It has the conversion methods and is not a big deal to make them used in constructors, but I would rather not pollute the class with non-Arduino things.


#define IPADDRESS_V4_BYTES_INDEX 12
#define IPADDRESS_V4_DWORD_INDEX 3

enum IPType
{
// A class to make it easier to handle and pass around IP addresses

enum IPType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you define enum IPType : uint8_t {

By default enum is size of int and takes 32 bits, which would be a waste.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather leave it as-is. If it's OK to waste 24 bits per value on 2KB RAM m328p, then it should be OK on chips with 300+KB of RAM. This is original Arduino.cc code and I would rather modify as little as possible from it.

IPv4,
IPv6
};

class IPAddress: public Printable
{
class IPAddress : public Printable {
private:
union {
uint8_t bytes[16];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a discussion (and another PR... or maybe it was in ArduinoCore) about removing the union, as technically conversions between the different types in a union is undefined. Just have a single uint8_t bytes[16] array, and then cast as necessary. (although I'm pretty sure the standard C socket interface uses a very similar union).

Anyway, I don't think the explicit union adds a lot of value, especially as we are mostly just accessing it through Arduino methods.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also not a fan of union, but how it is used here, you also get the IPv4-as-IPv6-address implementation for free. (unless I'm making the same mistake as always where I mess up the bit order in my mind, exactly the reason why I'm not a fan of unions but still use them myself...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole class (both header and implementation) I copied directly from Arduino's Core API. Anything that is not to/from_ip_addr_t and _zone is a carbon copy of the official code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an effort to change the approach in Arduino, if that happens so, we will update the class here too.

uint32_t dword[4];
} _address;
IPType _type;
uint8_t _zone;
me-no-dev marked this conversation as resolved.
Show resolved Hide resolved

// 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.
uint8_t* raw_address()
{
return _type == IPv4 ? &_address.bytes[IPADDRESS_V4_BYTES_INDEX] : _address.bytes;
}
uint8_t* raw_address() { return _type == IPv4 ? &_address.bytes[IPADDRESS_V4_BYTES_INDEX] : _address.bytes; }

public:
// Constructors

// Default IPv4
IPAddress();
IPAddress(IPType ip_type);
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
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);
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
IPAddress(uint32_t address);
// Default IPv4
IPAddress(const uint8_t *address);
IPAddress(IPType ip_type, const uint8_t *address);
IPAddress(IPType ip_type, const uint8_t *address, uint8_t z=0);
// If IPv4 fails tries IPv6 see fromString function
IPAddress(const char *address);
virtual ~IPAddress() {}
IPAddress(const IPAddress& address);

bool fromString(const char *address);
bool fromString(const String &address) { return fromString(address.c_str()); }

// Overloaded cast operator to allow IPAddress objects to be used where a
// uint32_t is expected
operator uint32_t() const
{
return _type == IPv4 ? _address.dword[IPADDRESS_V4_DWORD_INDEX] : 0;
}
// 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; };

bool operator==(const IPAddress& addr) const;
bool operator!=(const IPAddress& addr) const { return !(*this == addr); };

// NOTE: IPv4 only; we don't know the length of the pointer
bool operator==(const uint8_t* addr) const;

// Overloaded index operator to allow getting and setting individual octets of the address
uint8_t operator[](int index) const;
uint8_t& operator[](int index);

// Overloaded copy operators to allow initialisation of IPAddress objects from other types
// NOTE: IPv4 only
IPAddress& operator=(const uint8_t *address);
// NOTE: IPv4 only; see implementation note
IPAddress& operator=(uint32_t address);
// If IPv4 fails tries IPv6 see fromString function
IPAddress& operator=(const char *address);
IPAddress& operator=(const IPAddress& address);

virtual size_t printTo(Print& p) const;
String toString() const;

IPType type() const { return _type; }

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

friend class UDP;
friend class Client;
friend class Server;
friend class DhcpClass;
friend class DNSClient;

protected:
bool fromString4(const char *address);
Expand All @@ -108,7 +111,5 @@ class IPAddress: public Printable
String toString6() const;
};

// changed to extern because const declaration creates copies in BSS of INADDR_NONE for each CPP unit that includes it
extern IPAddress INADDR_NONE;
extern IPAddress IN6ADDR_ANY;
#endif
extern const IPAddress IN6ADDR_ANY;
extern const IPAddress INADDR_NONE;
64 changes: 54 additions & 10 deletions libraries/WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
#include <lwip/netdb.h>
#include <errno.h>

#define IN6_IS_ADDR_V4MAPPED(a) \
((((__const uint32_t *) (a))[0] == 0) \
&& (((__const uint32_t *) (a))[1] == 0) \
&& (((__const uint32_t *) (a))[2] == htonl (0xffff)))

#define WIFI_CLIENT_DEF_CONN_TIMEOUT_MS (3000)
#define WIFI_CLIENT_MAX_WRITE_RETRY (10)
#define WIFI_CLIENT_SELECT_TIMEOUT_US (1000000)
Expand Down Expand Up @@ -219,22 +224,33 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
{
return connect(ip,port,_timeout);
}

int WiFiClient::connect(IPAddress ip, uint16_t port, int32_t timeout_ms)
{
struct sockaddr_storage serveraddr = {};
_timeout = timeout_ms;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
int sockfd = -1;

if (ip.type() == IPv6) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need a check of WIFI_WANT_IP6_BIT ... not sure if it is valid to try to connect to IPv6 if you don't have an IPv6 address. (But then, why would they even be passing it in). Probably okay to just let the connect fail.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... still brainstorming on that whole matrix of what you want, what you have and what can you actually get. Open to suggestions

struct sockaddr_in6 *tmpaddr = (struct sockaddr_in6 *)&serveraddr;
sockfd = socket(AF_INET6, SOCK_STREAM, 0);
tmpaddr->sin6_family = AF_INET6;
memcpy(tmpaddr->sin6_addr.un.u8_addr, &ip[0], 16);
tmpaddr->sin6_port = htons(port);
tmpaddr->sin6_scope_id = ip.zone();
} else {
struct sockaddr_in *tmpaddr = (struct sockaddr_in *)&serveraddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
tmpaddr->sin_family = AF_INET;
tmpaddr->sin_addr.s_addr = ip;
tmpaddr->sin_port = htons(port);
}
if (sockfd < 0) {
log_e("socket: %d", errno);
return 0;
}
fcntl( sockfd, F_SETFL, fcntl( sockfd, F_GETFL, 0 ) | O_NONBLOCK );

uint32_t ip_addr = ip;
struct sockaddr_in serveraddr;
memset((char *) &serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
memcpy((void *)&serveraddr.sin_addr.s_addr, (const void *)(&ip_addr), 4);
serveraddr.sin_port = htons(port);
fd_set fdset;
struct timeval tv;
FD_ZERO(&fdset);
Expand Down Expand Up @@ -303,6 +319,19 @@ int WiFiClient::connect(const char *host, uint16_t port)

int WiFiClient::connect(const char *host, uint16_t port, int32_t timeout_ms)
{
if (WiFiGenericClass::getStatusBits() & WIFI_WANT_IP6_BIT) {
ip_addr_t srv6;
if(!WiFiGenericClass::hostByName6(host, srv6)){
return 0;
}
if (srv6.type == IPADDR_TYPE_V4) {
IPAddress ip(srv6.u_addr.ip4.addr);
return connect(ip, port, timeout_ms);
} else {
IPAddress ip(IPv6, (uint8_t*)&srv6.u_addr.ip6.addr[0]);
return connect(ip, port, timeout_ms);
}
}
IPAddress srv((uint32_t)0);
if(!WiFiGenericClass::hostByName(host, srv)){
return 0;
Expand Down Expand Up @@ -574,8 +603,24 @@ IPAddress WiFiClient::remoteIP(int fd) const
struct sockaddr_storage addr;
socklen_t len = sizeof addr;
getpeername(fd, (struct sockaddr*)&addr, &len);
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
return IPAddress((uint32_t)(s->sin_addr.s_addr));

// IPv4 socket, old way
if (((struct sockaddr*)&addr)->sa_family == AF_INET) {
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
return IPAddress((uint32_t)(s->sin_addr.s_addr));
}

// IPv6, but it might be IPv4 mapped address
if (((struct sockaddr*)&addr)->sa_family == AF_INET6) {
struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)&addr;
if (IN6_IS_ADDR_V4MAPPED(saddr6->sin6_addr.un.u32_addr)) {
return IPAddress(IPv4, (uint8_t*)saddr6->sin6_addr.s6_addr+12);
me-no-dev marked this conversation as resolved.
Show resolved Hide resolved
} else {
return IPAddress(IPv6, (uint8_t*)(saddr6->sin6_addr.s6_addr), saddr6->sin6_scope_id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

}
}
log_e("WiFiClient::remoteIP Not AF_INET or AF_INET6?");
return (IPAddress(0,0,0,0));
}

uint16_t WiFiClient::remotePort(int fd) const
Expand Down Expand Up @@ -638,4 +683,3 @@ int WiFiClient::fd() const
return clientSocketHandle->fd();
}
}

63 changes: 59 additions & 4 deletions libraries/WiFi/src/WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,8 +1055,8 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
} else if(event->event_id == ARDUINO_EVENT_WIFI_STA_CONNECTED) {
WiFiSTAClass::_setStatus(WL_IDLE_STATUS);
setStatusBits(STA_CONNECTED_BIT);

//esp_netif_create_ip6_linklocal(esp_netifs[ESP_IF_WIFI_STA]);
if (getStatusBits() & WIFI_WANT_IP6_BIT)
esp_netif_create_ip6_linklocal(esp_netifs[ESP_IF_WIFI_STA]);
} else if(event->event_id == ARDUINO_EVENT_WIFI_STA_DISCONNECTED) {
uint8_t reason = event->event_info.wifi_sta_disconnected.reason;
// Reason 0 causes crash, use reason 1 (UNSPECIFIED) instead
Expand Down Expand Up @@ -1546,6 +1546,11 @@ bool WiFiGenericClass::setDualAntennaConfig(uint8_t gpio_ant1, uint8_t gpio_ant2
// ------------------------------------------------ Generic Network function ---------------------------------------------
// -----------------------------------------------------------------------------------------------------------------------

struct dns_api_msg6 {
ip_addr_t ip_addr;
int result;
};

/**
* DNS callback
* @param name
Expand Down Expand Up @@ -1577,8 +1582,27 @@ static esp_err_t wifi_gethostbyname_tcpip_ctx(void *param)
}

/**
* Resolve the given hostname to an IP address. If passed hostname is an IP address, it will be parsed into IPAddress structure.
* @param aHostname Name to be resolved or string containing IP address
* IPv6 compatible DNS callback
* @param name
* @param ipaddr
* @param callback_arg
*/
static void wifi_dns6_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
{
struct dns_api_msg6 *msg = (struct dns_api_msg6 *)callback_arg;

if(ipaddr && !msg->result) {
msg->ip_addr = *ipaddr;
msg->result = 1;
} else {
msg->result = -1;
}
xEventGroupSetBits(_arduino_event_group, WIFI_DNS_DONE_BIT);
}

/**
* Resolve the given hostname to an IP address.
* @param aHostname Name to be resolved
* @param aResult IPAddress structure to store the returned IP address
* @return 1 if aIPAddrString was successfully converted to an IP address,
* else error code
Expand Down Expand Up @@ -1608,6 +1632,37 @@ int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
return (uint32_t)aResult != 0;
}

/**
* Resolve the given hostname to an IP6 address.
* @param aHostname Name to be resolved
* @param aResult IPv6Address structure to store the returned IP address
* @return 1 if aHostname was successfully converted to an IP address,
* else error code
*/
int WiFiGenericClass::hostByName6(const char* aHostname, ip_addr_t& aResult)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think hostByName6 is not a good approach. See #9009

Backwards compatibility should simply leave IPv6 off. Basic support of IPv6 wants hostByName() to support both 6 & 4 (whichever is relevant). Advanced IPv6 wants multiple address support.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be changed. will be one version for all.

{
ip_addr_t addr;
struct dns_api_msg6 arg;

memset(&arg, 0x0, sizeof(arg));
waitStatusBits(WIFI_DNS_IDLE_BIT, 16000);
clearStatusBits(WIFI_DNS_IDLE_BIT | WIFI_DNS_DONE_BIT);

err_t err = dns_gethostbyname_addrtype(aHostname, &addr, &wifi_dns6_found_callback,
&arg, LWIP_DNS_ADDRTYPE_IPV6_IPV4);
if(err == ERR_OK) {
aResult = addr;
} else if(err == ERR_INPROGRESS) {
waitStatusBits(WIFI_DNS_DONE_BIT, 15000); //real internal timeout in lwip library is 14[s]
clearStatusBits(WIFI_DNS_DONE_BIT);
if (arg.result == 1) {
aResult = arg.ip_addr;
}
}
setStatusBits(WIFI_DNS_IDLE_BIT);
return (uint32_t)err == ERR_OK || (err == ERR_INPROGRESS && arg.result == 1);
}

IPAddress WiFiGenericClass::calculateNetworkID(IPAddress ip, IPAddress subnet) {
IPAddress networkID;

Expand Down
3 changes: 3 additions & 0 deletions libraries/WiFi/src/WiFiGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "esp_netif_types.h"
#include "esp_eth_driver.h"
#include "wifi_provisioning/manager.h"
#include "lwip/ip_addr.h"

ESP_EVENT_DECLARE_BASE(ARDUINO_EVENTS);

Expand Down Expand Up @@ -141,6 +142,7 @@ static const int WIFI_SCANNING_BIT = BIT11;
static const int WIFI_SCAN_DONE_BIT= BIT12;
static const int WIFI_DNS_IDLE_BIT = BIT13;
static const int WIFI_DNS_DONE_BIT = BIT14;
static const int WIFI_WANT_IP6_BIT = BIT15;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I can see this is an ETH_HAS_IP_BIT, and ETH_HAS_IP6_BIT ... so maybe we need some flags about whether we have been allocated an address of the relevant type (not just if we want one).

I don't know if HAS refers to an assigned address (e.g. prefix from a RA, DHCP assignment, etc), or if a self-assigned link-local address is sufficient.

Probably need a way to get localIPs() for all your local IPs and determine if you have a global IPv6, or only a link-local, or none at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly. this only means that we enabled IPv6 support, not that we have an address present.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you connect though, if WANT is on, then you do generate a link-local address, so you will usually have one of them ... but it is not very useful for connecting to a public address or anything. Maybe once this is merged, then next thing is to support returning multiple results for things like getHostsByName() and getLocalIPs() to get all of them, which would allow manual usage of IPv6, while the default (single return) is worked out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then there is Ethernet, which does not raise WIFI_WANT_IP6_BIT. Maybe we need bits that tell the actual state, like NET_HAVE_IP6_LINK_LOCAL_BIT and NET_HAVE_IP6_GLOBAL_BIT to signal when we can serve V6 queries. DNS would return GLOBAL and mDNS would return LINK_LOCAL if I am not mistaken.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you do get an event telling you received an IPv6 address.
Actually, you may even get 2 (a few seconds inbetween) if you also receive a global scope one.

Not sure if you can always expect DNS to return global, as some routers allow to serve local DNS queries based on the hostname.
For example Fritzbox can perfectly fine resolve just based on the hostname and Mikrotik often (by default) can resolve .lan queries.
Though the Fritzbox only seems to do this on those who are set to get a static lease (as in always the same IP on a DHCP request)
A quick test here always returned the global scope address, as all those with static leases also have a global scope IPv6 address.
But at least it does show that the global scope address is the preferred answer from the DNS server (which makes sense)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm pretty sure that happens behind the scenes. I do not see a way to add v6 DNS or gateway, yet it all works

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I said, it is likely not an issue to fix for now, but if you come accross some related code for these, keep in mind it might perhaps be useful to add. Though not sure if you really need to have it when only working on IPv6 link local.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DNS would return GLOBAL and mDNS would return LINK_LOCAL

No. Both return global addresses. mDNS operates in link-local scope, but returns global addresses.

A DNS server will return whatever addresses it is configured with. Also 'local' and link-local are different things.

e.g. On my local LAN I can do (DNS):

> host nazgul-qnap.lan
nazgul-qnap.lan has address 192.168.1.39
nazgul-qnap.lan has IPv6 address fd7c:e25e:67e8:40::1027
nazgul-qnap.lan has IPv6 address 2407:8800:bc61:1340::1027

Which is a local device, not available to the wider internet. I'm not sure if DNS supports link-local addresses.

mDNS is only the local LAN, i.e. multicast SCOPE is link-local), but it still returns global addresses (not link-local ADDRESSES).

> avahi-resolve --name nazgul.local
NAZGUL.local	2407:8800:bc61:1340::1027
> avahi-resolve --name nazgul.local -4
NAZGUL.local	192.168.1.39

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't see any functions to set IPv6 addresses for DNS server.

This generally happens behind the scenes... you also won't see any functions to set IPv4 DNS servers.

DNS servers are configured via various means. For IPv4 they are part of DHCP parameters.

For IPv6 they can either be part of RA (RA DNS), or RA can specify to look at DHCPv6. (DHCPv6 can either the stateful -giving out addresses, or stateless - where it only gives out DNS, etc). Some devices (e.g. Android phones) only use RA DNS and don't support DHCPv6, even for DNS options.

From a quick look at LwIP, it supports stateless DHCPv6, i.e. DHCPv6 for DNS (but not addresses).

The actual DNS servers provided can be a list of both IPv6 and IPv4.

e.g. my local LAN (both are just my router)

   DNS Servers . . . . . . . . . . . : fd7c:e25e:67e8:40::1
                                       192.168.1.1

And if you connect on either of those addresses you will get the same DNS response (with both IPv4 and IPv6 addresses, as relevant).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so this page on LwIP: https://www.nongnu.org/lwip/2_1_x/group__lwip__opts__dhcpv6.html#ga92c3bc242ad20a5f398c45d332864a29

Says that stateful DHCPv6 (for getting addresses) is not supported yet

But it does support stateless for DNS and any DNS servers received in DHCPv6 responses are passed to dns_setserver(). In the Neighbour Discovery section, just above, it also mentions RDNSS and getting any DNS servers advertised in IPv6 RA messages and also adding them to DNS as well.


typedef enum {
WIFI_RX_ANT0 = 0,
Expand Down Expand Up @@ -196,6 +198,7 @@ class WiFiGenericClass
static const char * getHostname();
static bool setHostname(const char * hostname);
static bool hostname(const String& aHostname) { return setHostname(aHostname.c_str()); }
static int hostByName6(const char *aHostname, ip_addr_t& aResult);

static esp_err_t _eventCallback(arduino_event_t *event);

Expand Down
Loading