Skip to content

Commit

Permalink
lwip: Support IPv6 only mode
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Feb 27, 2023
1 parent 2c8c8bf commit 5f6cb31
Show file tree
Hide file tree
Showing 40 changed files with 336 additions and 80 deletions.
9 changes: 8 additions & 1 deletion components/esp-tls/esp_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,21 @@ static esp_err_t esp_tls_hostname_to_fd(const char *host, size_t hostlen, int po
return ESP_ERR_ESP_TLS_CANNOT_CREATE_SOCKET;
}

#if CONFIG_LWIP_IPV4
if (address_info->ai_family == AF_INET) {
struct sockaddr_in *p = (struct sockaddr_in *)address_info->ai_addr;
p->sin_port = htons(port);
ESP_LOGD(TAG, "[sock=%d] Resolved IPv4 address: %s", *fd, ipaddr_ntoa((const ip_addr_t*)&p->sin_addr.s_addr));
memcpy(address, p, sizeof(struct sockaddr ));
}
#endif

#if defined(CONFIG_LWIP_IPV4) && defined(CONFIG_LWIP_IPV6)
else
#endif

#if CONFIG_LWIP_IPV6
else if (address_info->ai_family == AF_INET6) {
if (address_info->ai_family == AF_INET6) {
struct sockaddr_in6 *p = (struct sockaddr_in6 *)address_info->ai_addr;
p->sin6_port = htons(port);
p->sin6_family = AF_INET6;
Expand Down
2 changes: 1 addition & 1 deletion components/esp_http_server/src/httpd_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static esp_err_t httpd_accept_conn(struct httpd_data *hd, int listen_fd)
}
}

struct sockaddr_in addr_from;
struct sockaddr_storage addr_from;
socklen_t addr_from_len = sizeof(addr_from);
int new_fd = accept(listen_fd, (struct sockaddr *)&addr_from, &addr_from_len);
if (new_fd < 0) {
Expand Down
33 changes: 24 additions & 9 deletions components/esp_http_server/src/util/ctrl_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ int cs_create_ctrl_sock(int port)
}

int ret;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_aton("127.0.0.1", &addr.sin_addr);
struct sockaddr_storage addr = {};
#ifdef CONFIG_LWIP_IPV4
struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
addr4->sin_family = AF_INET;
addr4->sin_port = htons(port);
inet_aton("127.0.0.1", &addr4->sin_addr);
#else
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
addr6->sin6_family = AF_INET6;
addr6->sin6_port = htons(port);
inet6_aton("::1", &addr6->sin6_addr);
#endif
ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
if (ret < 0) {
close(fd);
Expand All @@ -45,10 +52,18 @@ void cs_free_ctrl_sock(int fd)
int cs_send_to_ctrl_sock(int send_fd, int port, void *data, unsigned int data_len)
{
int ret;
struct sockaddr_in to_addr;
to_addr.sin_family = AF_INET;
to_addr.sin_port = htons(port);
inet_aton("127.0.0.1", &to_addr.sin_addr);
struct sockaddr_storage to_addr = {};
#ifdef CONFIG_LWIP_IPV4
struct sockaddr_in *addr4 = (struct sockaddr_in *)&to_addr;
addr4->sin_family = AF_INET;
addr4->sin_port = htons(port);
inet_aton("127.0.0.1", &addr4->sin_addr);
#else
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&to_addr;
addr6->sin6_family = AF_INET6;
addr6->sin6_port = htons(port);
inet6_aton("::1", &addr6->sin6_addr);
#endif
ret = sendto(send_fd, data, data_len, 0, (struct sockaddr *)&to_addr, sizeof(to_addr));

if (ret < 0) {
Expand Down
5 changes: 3 additions & 2 deletions components/esp_netif/esp_netif_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ void esp_netif_action_stop(void *esp_netif, esp_event_base_t base, int32_t event

void esp_netif_action_connected(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
{
esp_netif_dhcp_status_t status;

ESP_LOGD(TAG, "esp_netif action connected with netif%p from event_id=%d", esp_netif, event_id);
esp_netif_up(esp_netif);
Expand All @@ -43,7 +42,8 @@ void esp_netif_action_connected(void *esp_netif, esp_event_base_t base, int32_t
// No more actions for interfaces without DHCP client flag
return;
}

#if CONFIG_LWIP_IPV4
esp_netif_dhcp_status_t status;
ESP_NETIF_CALL_CHECK("connected action: dhcpc failed", esp_netif_dhcpc_get_status(esp_netif, &status), ESP_OK);
if (status == ESP_NETIF_DHCP_INIT) {
esp_netif_dhcpc_start(esp_netif);
Expand Down Expand Up @@ -76,6 +76,7 @@ void esp_netif_action_connected(void *esp_netif, esp_event_base_t base, int32_t
ESP_LOGE(TAG, "invalid static ip");
}
}
#endif
}

void esp_netif_action_disconnected(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data)
Expand Down
12 changes: 9 additions & 3 deletions components/esp_netif/include/esp_netif_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ extern "C" {
#define ESP_NETIF_DEFAULT_ARP_FLAGS (0)
#endif

#ifdef CONFIG_LWIP_IPV4
#define ESP_NETIF_IPV4_ONLY_FLAGS(flags) (flags)
#else
#define ESP_NETIF_IPV4_ONLY_FLAGS(flags) (0)
#endif

#define ESP_NETIF_INHERENT_DEFAULT_WIFI_STA() \
{ \
.flags = (esp_netif_flags_t)(ESP_NETIF_DHCP_CLIENT | ESP_NETIF_DEFAULT_ARP_FLAGS | ESP_NETIF_FLAG_EVENT_IP_MODIFIED), \
.flags = (esp_netif_flags_t)(ESP_NETIF_IPV4_ONLY_FLAGS(ESP_NETIF_DHCP_CLIENT) | ESP_NETIF_DEFAULT_ARP_FLAGS | ESP_NETIF_FLAG_EVENT_IP_MODIFIED), \
ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(mac) \
ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(ip_info) \
.get_ip_event = IP_EVENT_STA_GOT_IP, \
Expand All @@ -40,7 +46,7 @@ extern "C" {
#ifdef CONFIG_ESP_WIFI_SOFTAP_SUPPORT
#define ESP_NETIF_INHERENT_DEFAULT_WIFI_AP() \
{ \
.flags = (esp_netif_flags_t)(ESP_NETIF_DHCP_SERVER | ESP_NETIF_FLAG_AUTOUP), \
.flags = (esp_netif_flags_t)(ESP_NETIF_IPV4_ONLY_FLAGS(ESP_NETIF_DHCP_SERVER) | ESP_NETIF_FLAG_AUTOUP), \
ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(mac) \
.ip_info = &_g_esp_netif_soft_ap_ip, \
.get_ip_event = 0, \
Expand All @@ -54,7 +60,7 @@ extern "C" {

#define ESP_NETIF_INHERENT_DEFAULT_ETH() \
{ \
.flags = (esp_netif_flags_t)(ESP_NETIF_DHCP_CLIENT | ESP_NETIF_DEFAULT_ARP_FLAGS | ESP_NETIF_FLAG_EVENT_IP_MODIFIED), \
.flags = (esp_netif_flags_t)(ESP_NETIF_IPV4_ONLY_FLAGS(ESP_NETIF_DHCP_CLIENT) | ESP_NETIF_DEFAULT_ARP_FLAGS | ESP_NETIF_FLAG_EVENT_IP_MODIFIED), \
ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(mac) \
ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(ip_info) \
.get_ip_event = IP_EVENT_ETH_GOT_IP, \
Expand Down
14 changes: 14 additions & 0 deletions components/esp_netif/include/esp_netif_ip_addr.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <stdint.h>
#include <machine/endian.h>
#include "sdkconfig.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -81,6 +82,19 @@ extern "C" {
#define ESP_IP4ADDR_INIT(a, b, c, d) { .type = ESP_IPADDR_TYPE_V4, .u_addr = { .ip4 = { .addr = ESP_IP4TOADDR(a, b, c, d) }}};
#define ESP_IP6ADDR_INIT(a, b, c, d) { .type = ESP_IPADDR_TYPE_V6, .u_addr = { .ip6 = { .addr = { a, b, c, d }, .zone = 0 }}};

#ifndef IP4ADDR_STRLEN_MAX
#define IP4ADDR_STRLEN_MAX 16
#endif

#if defined(CONFIG_LWIP_IPV4) && defined(CONFIG_LWIP_IPV6)
#define ESP_IP_IS_ANY(addr) ((addr.type == ESP_IPADDR_TYPE_V4 && ip4_addr_isany_val(addr.u_addr.ip4)) || \
(addr.type == ESP_IPADDR_TYPE_V6 && ip6_addr_isany_val(addr.u_addr.ip6)))
#elif defined(CONFIG_LWIP_IPV4)
#define ESP_IP_IS_ANY(addr) (ip4_addr_isany_val(addr.u_addr.ip4))
#elif defined(CONFIG_LWIP_IPV6)
#define ESP_IP_IS_ANY(addr) (ip6_addr_isany_val(addr.u_addr.ip6))
#endif

/**
* @brief IPv6 address
*
Expand Down
Loading

0 comments on commit 5f6cb31

Please sign in to comment.