aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2023-03-09 16:58:16 +0100
committerAlex Auvolat <alex@adnab.me>2023-03-09 16:58:43 +0100
commit65e979de801daa5f6ef77ed875e6505aa902fd9c (patch)
tree845cf16df4139a19bd27b9d71f51c063b6b2d899 /src/main.rs
parentc96aec520dbf18f11455001f1db9b4f068c10528 (diff)
downloadwgautomesh-65e979de801daa5f6ef77ed875e6505aa902fd9c.tar.gz
wgautomesh-65e979de801daa5f6ef77ed875e6505aa902fd9c.zip
reannounce persisted data
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs
index 04e655c..60881a2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -192,13 +192,6 @@ impl Daemon {
let socket = UdpSocket::bind(SocketAddr::new("0.0.0.0".parse()?, config.gossip_port))?;
socket.set_broadcast(true)?;
- let gossip = config
- .persist_file
- .as_ref()
- .and_then(|x| std::fs::read(x).ok())
- .and_then(|x| bincode::deserialize(&x).ok())
- .unwrap_or_default();
-
Ok(Daemon {
config,
gossip_key,
@@ -207,7 +200,7 @@ impl Daemon {
socket,
state: Mutex::new(State {
peers: HashMap::new(),
- gossip,
+ gossip: HashMap::new(),
}),
})
}
@@ -217,6 +210,10 @@ impl Daemon {
error!("Error while initializing Wireguard configuration: {}", e);
}
+ if let Err(e) = self.load_persisted_data() {
+ error!("Error while loading/reannouncing persisted data: {}", e);
+ }
+
let request = self.make_packet(&Gossip::Request)?;
for peer in self.config.peers.iter() {
let addr = SocketAddr::new(peer.address, self.config.gossip_port);
@@ -242,6 +239,17 @@ impl Daemon {
Ok(())
}
+ fn load_persisted_data(&self) -> Result<()> {
+ if let Some(file) = &self.config.persist_file {
+ let mut state = self.state.lock().unwrap();
+ let persisted: HashMap<_, _> = bincode::deserialize(&std::fs::read(file)?)?;
+ for (pubkey, addrs) in persisted.into_iter() {
+ state.handle_announce(self, pubkey, addrs)?;
+ }
+ }
+ Ok(())
+ }
+
fn wg_loop(&self) -> ! {
let mut i = 0;
loop {