aboutsummaryrefslogtreecommitdiff
path: root/src/util.rs
blob: 41ec24ed247809ae1a528afb71cba03275223130 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::{Error, Result};

use std::fs::File;
use std::time::{Duration, SystemTime};

use tokio::net::{ToSocketAddrs, UdpSocket};
use tokio::time::Instant;

use rsdsl_ip_config::DsConfig;
use rsdsl_pd_config::PdConfig;
use sysinfo::{ProcessExt, Signal, System, SystemExt};

pub fn expired(lease: &PdConfig) -> bool {
    let expiry = lease.timestamp + Duration::from_secs(lease.validlft.into());
    SystemTime::now() >= expiry
}

pub fn needs_rebind(lease: &PdConfig) -> bool {
    let expiry = lease.timestamp + Duration::from_secs(lease.t2.into());
    SystemTime::now() >= expiry
}

pub fn needs_renewal(lease: &PdConfig) -> bool {
    let expiry = lease.timestamp + Duration::from_secs(lease.t1.into());
    SystemTime::now() >= expiry
}

pub async fn send_to_exact<A: ToSocketAddrs>(
    sock: &UdpSocket,
    buf: &[u8],
    target: A,
) -> Result<()> {
    let n = sock.send_to(buf, target).await?;
    if n != buf.len() {
        Err(Error::PartialSend(buf.len(), n))
    } else {
        Ok(())
    }
}

pub fn inform() {
    for netlinkd in System::new_all().processes_by_exact_name("rsdsl_netlinkd") {
        netlinkd.kill_with(Signal::User1);
    }

    for dslite in System::new_all().processes_by_exact_name("rsdsl_dslite") {
        dslite.kill_with(Signal::User1);
    }
}

pub fn hexdump<A: AsRef<[u8]>>(data: A) -> String {
    data.as_ref()
        .iter()
        .map(|byte| format!("{:02x}", byte))
        .reduce(|acc, ch| acc + &ch)
        .unwrap_or(String::new())
}

pub fn sys_to_instant(sys: SystemTime) -> Result<Instant> {
    Ok(Instant::now() - sys.elapsed()?)
}

pub fn read_ds_config() -> Option<DsConfig> {
    let mut file = File::open(rsdsl_ip_config::LOCATION).ok()?;
    let ds_config = serde_json::from_reader(&mut file).ok()?;

    Some(ds_config)
}