summaryrefslogtreecommitdiffhomepage
path: root/talpid-macos/src/net.rs
blob: bec8d53b8d3471da8437820e7f2db429c1778743 (plain)
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
use anyhow::{Context, anyhow, bail};
use std::net::IpAddr;
use tokio::process::Command;

/// Adds an alias to a network interface.
pub async fn add_alias(interface: &str, addr: IpAddr) -> anyhow::Result<()> {
    let context = || anyhow!("Failed to add interface {interface} alias {addr}");
    let output = Command::new("ifconfig")
        .args([interface, "alias", &format!("{addr}"), "up"])
        .output()
        .await
        .context("Failed to spawn ifconfig")
        .with_context(context)?;

    if !output.status.success() {
        bail!(
            "{}: Non-zero exit code from ifconfig: {}",
            context(),
            output.status
        );
    }

    Ok(())
}

/// Removes an alias from a network interface.
pub async fn remove_alias(interface: &str, addr: IpAddr) -> anyhow::Result<()> {
    let context = || anyhow!("Failed to remove interface {interface} alias {addr}");
    let output = Command::new("ifconfig")
        .args([interface, "delete", &format!("{addr}")])
        .output()
        .await
        .context("Failed to spawn ifconfig")
        .with_context(context)?;

    if !output.status.success() {
        bail!(
            "{}: Non-zero exit code from ifconfig: {}",
            context(),
            output.status
        );
    }

    Ok(())
}