Commit 1ccde179 authored by nanamicat's avatar nanamicat

route table

parent fd7351ee
...@@ -27,7 +27,7 @@ async fn main() -> anyhow::Result<()> { ...@@ -27,7 +27,7 @@ async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
let config: Settings = Config::builder().add_source(config::Environment::default()).build()?.try_deserialize()?; let config: Settings = Config::builder().add_source(config::Environment::default()).build()?.try_deserialize()?;
let routers_data = serde_json::from_slice::<Vec<RouterData>>(&fs::read("import/data/Router.json")?)?; let routers_data = serde_json::from_slice::<Vec<RouterData>>(&fs::read("import/data/Router.json")?)?;
let mut routers: BTreeMap<u8, Router> = routers_data.iter().map(|r| (r.id, Router::new(r, &config))).collect(); let mut routers: BTreeMap<u8, Router> = routers_data.into_iter().map(|r| (r.id, Router::new(r, &config))).collect();
let connections = serde_json::from_slice::<BTreeMap<u8, BTreeMap<u8, Connection>>>(&fs::read("import/connections.json")?)?; let connections = serde_json::from_slice::<BTreeMap<u8, BTreeMap<u8, Connection>>>(&fs::read("import/connections.json")?)?;
// let groups: Vec<GatewayGroup> = serde_json::from_slice(&fs::read("import/GatewayGroup.json")?)?; // let groups: Vec<GatewayGroup> = serde_json::from_slice(&fs::read("import/GatewayGroup.json")?)?;
...@@ -64,7 +64,7 @@ async fn main() -> anyhow::Result<()> { ...@@ -64,7 +64,7 @@ async fn main() -> anyhow::Result<()> {
peer.on_message(&hello); peer.on_message(&hello);
} else if addr.port() == config.server.port } else if addr.port() == config.server.port
&& let Ok((downlink, _)) = bincode::decode_from_slice(&buf[..len], bincode::config::standard()) && let Ok((downlink, _)) = bincode::decode_from_slice(&buf[..len], bincode::config::standard())
&& let Some(uplink) = server.on_message(downlink, &routers_data, &connections[&config.id], &config).await && let Some(uplink) = server.on_message(downlink, &routers, &connections[&config.id], &config).await
{ {
let len = bincode::encode_into_slice(uplink, &mut buf, bincode::config::standard())?; let len = bincode::encode_into_slice(uplink, &mut buf, bincode::config::standard())?;
let _ = socket.send_to(&buf[..len], addr).await; let _ = socket.send_to(&buf[..len], addr).await;
......
use crate::{ use crate::{data, data::Router as RouterData, protocol::{Hello, PeerQuality}, settings::{Settings, INTERVAL, WINDOW}};
data::Router as RouterData,
protocol::{Hello, PeerQuality},
settings::{Settings, INTERVAL, WINDOW},
};
use saturating_cast::SaturatingCast; use saturating_cast::SaturatingCast;
use std::{ use std::{
collections::BTreeMap, collections::BTreeMap,
...@@ -19,6 +15,7 @@ pub struct Router { ...@@ -19,6 +15,7 @@ pub struct Router {
receive: u64, receive: u64,
remote_time: u32, remote_time: u32,
local_time: Instant, local_time: Instant,
pub(crate) data: data::Router
} }
impl Router { impl Router {
...@@ -28,7 +25,7 @@ impl Router { ...@@ -28,7 +25,7 @@ impl Router {
SocketAddr::V6(_) => None, SocketAddr::V6(_) => None,
} }
} }
pub fn new(data: &RouterData, config: &Settings) -> Router { pub fn new(data: RouterData, config: &Settings) -> Router {
Router { Router {
link_address: SocketAddr::new(IpAddr::V4(Ipv4Addr::from([10, 200, data.id, config.id])), config.bind.port()), link_address: SocketAddr::new(IpAddr::V4(Ipv4Addr::from([10, 200, data.id, config.id])), config.bind.port()),
remote_time: rand::random(), remote_time: rand::random(),
...@@ -37,11 +34,12 @@ impl Router { ...@@ -37,11 +34,12 @@ impl Router {
prev_delay: 0, prev_delay: 0,
delay: 0, delay: 0,
local_time: Instant::now(), local_time: Instant::now(),
data
} }
} }
pub fn on_message(&mut self, data: &Hello) { pub fn on_message(&mut self, message: &Hello) {
let delta = (data.time.wrapping_sub(self.remote_time) as i32 as f32 / INTERVAL.as_millis() as f32).round() as i32; let delta = (message.time.wrapping_sub(self.remote_time) as i32 as f32 / INTERVAL.as_millis() as f32).round() as i32;
const WINDOW_MAX: i32 = WINDOW as i32; const WINDOW_MAX: i32 = WINDOW as i32;
const WINDOW_MIN: i32 = 1 - WINDOW_MAX; const WINDOW_MIN: i32 = 1 - WINDOW_MAX;
match delta { match delta {
...@@ -53,18 +51,18 @@ impl Router { ...@@ -53,18 +51,18 @@ impl Router {
0..WINDOW_MAX => { 0..WINDOW_MAX => {
// 0 可能是重启 // 0 可能是重启
self.receive = (self.receive << delta) | 1; self.receive = (self.receive << delta) | 1;
self.remote_time = data.time; self.remote_time = message.time;
self.local_time = Instant::now(); self.local_time = Instant::now();
} }
_ => { _ => {
// 可能是下线很久之后上线,也可能是别的异常情况,先承认这个报文,但是不计算丢包和延迟 // 可能是下线很久之后上线,也可能是别的异常情况,先承认这个报文,但是不计算丢包和延迟
self.remote_time = data.time; self.remote_time = message.time;
self.local_time = Instant::now(); self.local_time = Instant::now();
return; return;
} }
} }
let delay = (SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis() as u32).wrapping_sub(data.time) as i32; let delay = (SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis() as u32).wrapping_sub(message.time) as i32;
self.delay += (delay - self.delay) / 8; self.delay += (delay - self.delay) / 8;
......
use crate::connection::Connection;
use crate::protocol::{Downlink, MessageType, Uplink}; use crate::protocol::{Downlink, MessageType, Uplink};
use crate::router::Router; use crate::router::Router;
use crate::settings::Settings; use crate::settings::{ROUTE_PROTOCOL, Settings};
use crate::{connection::Connection, data}; use rtnetlink::RouteMessageBuilder;
use netlink_sys::{protocols::NETLINK_ROUTE, Socket, TokioSocket};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::os::unix::io::{AsRawFd, FromRawFd}; use std::net::Ipv4Addr;
pub struct Server { pub struct Server {
pub(crate) online: bool, pub(crate) online: bool,
...@@ -16,10 +16,7 @@ pub struct Server { ...@@ -16,10 +16,7 @@ pub struct Server {
impl Server { impl Server {
pub fn new(id: u8, routers: &BTreeMap<u8, Router>) -> Self { pub fn new(id: u8, routers: &BTreeMap<u8, Router>) -> Self {
let socket = std::mem::ManuallyDrop::new(Socket::new(NETLINK_ROUTE).unwrap()); let (connection, handle) = rtnetlink::new_connection().unwrap();
socket.set_netlink_get_strict_chk(true).unwrap();
let socket = unsafe { TokioSocket::from_raw_fd(socket.as_raw_fd()) };
let (connection, handle, _) = rtnetlink::from_socket(socket);
tokio::spawn(connection); tokio::spawn(connection);
Server { Server {
online: false, online: false,
...@@ -33,7 +30,7 @@ impl Server { ...@@ -33,7 +30,7 @@ impl Server {
pub async fn on_message( pub async fn on_message(
&mut self, &mut self,
mut message: Downlink, mut message: Downlink,
routers: &Vec<data::Router>, routers: &BTreeMap<u8, Router>,
connections: &BTreeMap<u8, Connection>, connections: &BTreeMap<u8, Connection>,
config: &Settings, // routers: &mut HashMap<u8, Router>, config: &Settings, // routers: &mut HashMap<u8, Router>,
// self_peer: &Hello, // self_peer: &Hello,
...@@ -45,8 +42,10 @@ impl Server { ...@@ -45,8 +42,10 @@ impl Server {
match (self.online, message.action) { match (self.online, message.action) {
(false, MessageType::Full) => { (false, MessageType::Full) => {
self.reset(routers, connections, config).await;
self.online = true; self.online = true;
for (to, via) in self.via.iter_mut() {
*via = *to;
}
self.via.append(&mut message.via); self.via.append(&mut message.via);
self.plan.append(&mut message.plan); self.plan.append(&mut message.plan);
Some(Uplink { Some(Uplink {
...@@ -82,30 +81,24 @@ impl Server { ...@@ -82,30 +81,24 @@ impl Server {
} }
} }
pub async fn reset(&self, routers: &Vec<data::Router>, connections: &BTreeMap<u8, Connection>, config: &Settings) { pub async fn write(&self, via: &BTreeMap<u8, u8>, routers: &BTreeMap<u8, Router>, connections: &BTreeMap<u8, Connection>, config: &Settings) {
println!("reset"); for (to_id, via_id) in via.iter() {
// let mut routes = self.handle.route().get(RouteMessageBuilder::<Ipv4Addr>::new().protocol(ROUTE_PROTOCOL).build()).execute(); let to = routers[to_id];
// while let Some(route) = routes.try_next().await.unwrap() { let via = routers[via_id];
// assert_eq!(route.header.protocol, ROUTE_PROTOCOL); if connections.contains_key(via_id) {
// if let Err(e) = self.handle.route().del(route).execute().await { self.handle
// panic!("Failed to delete route: {}", e); .route()
// } .add(
// } RouteMessageBuilder::<Ipv4Addr>::new()
// for router in routers.iter() { .destination_prefix(to.data.address, 32)
// if connections.contains_key(&router.id) { .gateway(via.link_address.ip())
// self.handle .protocol(ROUTE_PROTOCOL)
// .route() .build(),
// .add( )
// RouteMessageBuilder::<Ipv4Addr>::new() .execute()
// .destination_prefix(router.address, 32) .await
// .gateway(Router::link_address(config.id, router.id)) .unwrap();
// .protocol(ROUTE_PROTOCOL) }
// .build(), }
// )
// .execute()
// .await
// .unwrap();
// }
// }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment