aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-03-13 19:25:47 +0100
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-03-13 19:25:47 +0100
commitec0e0f72f13ccb987691b2fc6dd4da45bea7168c (patch)
tree96ed466acad46ede2ac8a7daf1a4de5a2899021c
parent6dfbd88368e5ce809c36d55ec3fd00ec71c280a0 (diff)
remember ip configuration and make it available to the outside world
-rw-r--r--src/client.rs62
-rw-r--r--src/error.rs6
2 files changed, 68 insertions, 0 deletions
diff --git a/src/client.rs b/src/client.rs
index b8b9a16..97bfac7 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -23,6 +23,25 @@ use pppoe::Tag;
const BROADCAST: [u8; 6] = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
+#[derive(Clone, Debug, PartialEq)]
+pub struct IpConfig {
+ pub addr: Ipv4Addr,
+ pub dns1: Ipv4Addr,
+ pub dns2: Ipv4Addr,
+}
+
+impl Default for IpConfig {
+ fn default() -> Self {
+ let all_zero = Ipv4Addr::new(0, 0, 0, 0);
+
+ Self {
+ addr: all_zero,
+ dns1: all_zero,
+ dns2: all_zero,
+ }
+ }
+}
+
#[derive(Clone, Copy, Debug, PartialEq)]
enum State {
Idle,
@@ -54,6 +73,7 @@ impl Client {
peer: BROADCAST,
magic_number: rand::random(),
error: String::new(),
+ ip_config: IpConfig::default(),
})),
})
}
@@ -145,6 +165,14 @@ impl Client {
self.inner.lock().unwrap().magic_number
}
+ pub fn ip_config(&self) -> IpConfig {
+ self.inner.lock().unwrap().ip_config.clone()
+ }
+
+ fn set_ip_config(&self, ip_config: IpConfig) {
+ self.inner.lock().unwrap().ip_config = ip_config;
+ }
+
fn new_discovery_packet(&self, buf: &mut [u8]) -> Result<()> {
let local_mac = self.inner.lock().unwrap().socket.mac_address();
@@ -537,6 +565,39 @@ impl Client {
let opts: Vec<ipcp::ConfigOption> =
ipcp::ConfigOptionIterator::new(ipcp.payload()).collect();
+ self.set_ip_config(IpConfig {
+ addr: *opts
+ .iter()
+ .find_map(|opt| {
+ if let ipcp::ConfigOption::IpAddress(addr) = opt {
+ Some(addr)
+ } else {
+ None
+ }
+ })
+ .ok_or(Error::MissingIpAddr)?,
+ dns1: *opts
+ .iter()
+ .find_map(|opt| {
+ if let ipcp::ConfigOption::PrimaryDns(addr) = opt {
+ Some(addr)
+ } else {
+ None
+ }
+ })
+ .ok_or(Error::MissingPrimaryDns)?,
+ dns2: *opts
+ .iter()
+ .find_map(|opt| {
+ if let ipcp::ConfigOption::SecondaryDns(addr) = opt {
+ Some(addr)
+ } else {
+ None
+ }
+ })
+ .ok_or(Error::MissingSecondaryDns)?,
+ });
+
println!("ip configuration acknowledged by peer, options: {:?}", opts);
Ok(())
}
@@ -705,4 +766,5 @@ struct ClientRef {
peer: [u8; 6],
magic_number: u32,
error: String,
+ ip_config: IpConfig,
}
diff --git a/src/error.rs b/src/error.rs
index beb11de..d3737d4 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -37,6 +37,12 @@ pub enum Error {
InvalidChapCode(u8),
#[error("invalid IPCP code {0}")]
InvalidIpcpCode(u8),
+ #[error("peer did not assign us an IP address")]
+ MissingIpAddr,
+ #[error("peer did not send us a primary DNS server")]
+ MissingPrimaryDns,
+ #[error("peer did not send us a secondary DNS server")]
+ MissingSecondaryDns,
#[error("io error")]
Io(#[from] io::Error),
#[error("failed to convert string from UTF-8")]