From 55a59667c47d672399000a34a5d971998f05cb2f Mon Sep 17 00:00:00 2001 From: _index Date: Wed, 8 Jan 2025 19:11:06 +0100 Subject: [PATCH] needs fix --- .../esp_wireguard/include/esp_wireguard.h | 5 ++++- .../esp_wireguard/src/esp_wireguard.c | 20 ++++++++++++++----- src/wireguard/mod.rs | 8 +++++--- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/wireguard/esp_wireguard/esp_wireguard/include/esp_wireguard.h b/src/wireguard/esp_wireguard/esp_wireguard/include/esp_wireguard.h index 3a4b6b9..a1c5f46 100644 --- a/src/wireguard/esp_wireguard/esp_wireguard/include/esp_wireguard.h +++ b/src/wireguard/esp_wireguard/esp_wireguard/include/esp_wireguard.h @@ -38,6 +38,7 @@ extern "C" { #include #include #include +#include #define ESP_WIREGUARD_CONFIG_DEFAULT() { \ .private_key = NULL, \ @@ -76,6 +77,8 @@ typedef struct { struct netif* netif_default; /**< a pointer to the default netif. */ } wireguard_ctx_t; +extern const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_wg; + /** * @brief Initialize WireGuard * @@ -94,7 +97,7 @@ typedef struct { * - ESP_ERR_INVALID_ARG: given argument is invalid. * - ESP_FAIL: Other error. */ -esp_err_t esp_wireguard_init(wireguard_config_t *config, wireguard_ctx_t *ctx); +esp_err_t esp_wireguard_init(); /** * @brief Create a WireGuard interface and start establishing the connection diff --git a/src/wireguard/esp_wireguard/esp_wireguard/src/esp_wireguard.c b/src/wireguard/esp_wireguard/esp_wireguard/src/esp_wireguard.c index a23fcec..2d81d16 100644 --- a/src/wireguard/esp_wireguard/esp_wireguard/src/esp_wireguard.c +++ b/src/wireguard/esp_wireguard/esp_wireguard/src/esp_wireguard.c @@ -39,6 +39,7 @@ #include #include #include +#include #include "wireguard-platform.h" #include "wireguardif.h" @@ -58,6 +59,15 @@ static struct wireguardif_peer peer = {0}; static uint8_t wireguard_peer_index = WIREGUARDIF_INVALID_INDEX; static uint8_t preshared_key_decoded[WG_KEY_LEN]; +static const struct esp_netif_netstack_config s_wg_netif_config = { + .lwip = { + .init_fn = wireguardif_init, + .input_fn = ip_input + } +}; + +const esp_netif_netstack_config *_g_esp_netif_netstack_default_wg = &s_wg_netif_config; + static esp_err_t esp_wireguard_peer_init(const wireguard_config_t *config, struct wireguardif_peer *peer) { esp_err_t err; @@ -112,7 +122,7 @@ static esp_err_t esp_wireguard_peer_init(const wireguard_config_t *config, struc } /* resolve peer name or IP address */ - ESP_LOGI(TAG, "resolving ip address (dns).."); + ESP_LOGI(TAG, "getaddrinfo: Resolving ip address.."); { ip_addr_t endpoint_ip; memset(&endpoint_ip, 0, sizeof(endpoint_ip)); @@ -122,11 +132,11 @@ static esp_err_t esp_wireguard_peer_init(const wireguard_config_t *config, struc err = ESP_FAIL; /* XXX gai_strerror() is not implemented */ - ESP_LOGE(TAG, "getaddrinfo: unable to resolve `%s`", config->endpoint); + ESP_LOGE(TAG, "getaddrinfo: Unable to resolve `%s`", config->endpoint); goto fail; } - ESP_LOGI(TAG, "resolved ip address successfully!"); + ESP_LOGI(TAG, "Resolved ip address successfully!"); if (res->ai_family == AF_INET) { struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr; @@ -137,7 +147,7 @@ static esp_err_t esp_wireguard_peer_init(const wireguard_config_t *config, struc inet6_addr_to_ip6addr(ip_2_ip6(&endpoint_ip), &addr6); #endif } - ESP_LOGI(TAG, "setting endpoint.."); + ESP_LOGI(TAG, "Setting endpoint.."); peer->endpoint_ip = endpoint_ip; if (inet_ntop(res->ai_family, &(peer->endpoint_ip), addr_str, WG_ADDRSTRLEN) == NULL) { @@ -188,7 +198,7 @@ static esp_err_t esp_wireguard_netif_create(const wireguard_config_t *config) err = ESP_ERR_INVALID_ARG; goto fail; } - + ESP_LOGI(TAG, "attempting netif_add.."); /* Register the new WireGuard network interface with lwIP */ wg_netif = netif_add( diff --git a/src/wireguard/mod.rs b/src/wireguard/mod.rs index ebc8804..49a1f60 100644 --- a/src/wireguard/mod.rs +++ b/src/wireguard/mod.rs @@ -19,6 +19,8 @@ use esp_idf_svc::sys::{ use crate::utils::nvs::WgConfig; +use esp_idf_svc::sys::_g_esp_netif_default_netstack_wg + /// Handles the management of the global context for the wireguard tunnel. pub mod ctx; @@ -107,7 +109,7 @@ fn create_ctx_conf( secondary_dns: None, }), )), - stack: esp_idf_svc::netif::NetifStack::Eth, + stack: unsafe { _g_esp_netif_default_netstack_wg }, custom_mac: None, })?; @@ -146,12 +148,12 @@ pub fn start_tunnel(nvs: Arc>>) -> anyhow::Result<()> { return Ok(()); } - let (ctx, config) = create_ctx_conf(nvs)?; + let (wg_netif, ctx) = create_ctx_conf(nvs)?; unsafe { log::info!("Initializing wireguard.."); - esp!(esp_wireguard_init(config, ctx))?; + esp!(esp_wireguard_init())?; log::info!("Connecting to peer..");