Skip to content

Commit

Permalink
fail gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
indexds committed Dec 27, 2024
1 parent dba7106 commit 5a823c7
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions src/wireguard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use esp_idf_svc::sys::wg::{
};

const MAX_SNTP_RETRIES: u32 = 10;
const MAX_WG_RETRIES: u32 = 10;

pub fn sync_sntp(wifi: Arc<Mutex<EspWifi<'static>>>) -> anyhow::Result<()> {
let wifi = wifi.lock().unwrap();
Expand Down Expand Up @@ -86,7 +87,18 @@ pub fn start_wg_tunnel(nvs: Arc<Mutex<EspNvs<NvsDefault>>>) -> anyhow::Result<()

esp!(esp_netif_tcpip_exec(Some(wg_connect_wrapper), ctx as *mut core::ffi::c_void))?;

loop {
for i in 0..=MAX_WG_RETRIES {
if i == MAX_WG_RETRIES {
log::error!("Max retries reached, cleaning up.");

// While we're not connected yet, this allows us to fail gracefully by
// deinitializing the entire stack to start from a clean slate
// next time we make an attempt to connect to a peer.
esp!(esp_wireguard_disconnect(ctx))?;

return Err(anyhow::anyhow!("Failed to connect to peer, cleaning up."));
}

match esp!(esp_wireguardif_peer_is_up(ctx)) {
Ok(_) => {
log::info!("Peer is up!");
Expand Down Expand Up @@ -136,24 +148,3 @@ pub fn end_wg_tunnel() -> anyhow::Result<()> {

Ok(())
}

// use esp_idf_svc::eth::{EspEth, RmiiEth};
// use esp_idf_svc::handle::RawHandle;
// use esp_idf_svc::sys::wg::wireguard_ctx_t;

// use crate::wireguard::ctx::WG_CTX;

// pub fn _start_bridge(eth_netif: EspEth<'static, RmiiEth>) ->
// anyhow::Result<()> { let mut lock = WG_CTX.lock().unwrap();

// let ctx: *mut wireguard_ctx_t = lock.as_mut().unwrap().get_raw();

// unsafe {
// let _wg_netif = ctx.as_ref().unwrap().netif;
// let _eth_netif = eth_netif.netif().handle();

// // create callbacks between both netifs to bridge them?
// }

// Ok(())
// }

0 comments on commit 5a823c7

Please sign in to comment.