Skip to content

Commit

Permalink
needs fix
Browse files Browse the repository at this point in the history
  • Loading branch information
indexds committed Jan 8, 2025
1 parent ba74ffa commit 55a5966
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ extern "C" {
#include <stdint.h>
#include <esp_err.h>
#include <lwip/netif.h>
#include <lwip/esp_netif_net_stack.h>

#define ESP_WIREGUARD_CONFIG_DEFAULT() { \
.private_key = NULL, \
Expand Down Expand Up @@ -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
*
Expand All @@ -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
Expand Down
20 changes: 15 additions & 5 deletions src/wireguard/esp_wireguard/esp_wireguard/src/esp_wireguard.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <esp_log.h>
#include <esp_wireguard.h>
#include <mbedtls/base64.h>
#include <lwip/esp_netif_net_stack.h>

#include "wireguard-platform.h"
#include "wireguardif.h"
Expand All @@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 5 additions & 3 deletions src/wireguard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
})?;

Expand Down Expand Up @@ -146,12 +148,12 @@ pub fn start_tunnel(nvs: Arc<Mutex<EspNvs<NvsDefault>>>) -> 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..");

Expand Down

0 comments on commit 55a5966

Please sign in to comment.