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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
pub mod sys;


use {
    std::{
        fmt,
        fs::{
            File,
            OpenOptions,
        },
        io::Read,
        os::unix::{
            fs::{
                OpenOptionsExt,
            },
            io::{
                AsRawFd,
                RawFd,
            },
        },
    },

    anyhow::{
        Context,
        Result,
    },

    nix::{
        ioctl_readwrite,
        ioctl_write_int_bad,
        request_code_none,
    },

    sys::*,
};


pub const EMPTY_MAC: &str = "00:00:00:00:00:00";
const MAC_SIZE: usize = EMPTY_MAC.len();


/// A reference to the network device
#[derive(Debug)]
pub struct NetDevice {
    adapter: u32,
    device: u32,

    file: File,
}


impl AsRawFd for NetDevice {
    #[inline]
    fn as_raw_fd(&self) -> RawFd { self.file.as_raw_fd() }
}


impl NetDevice {
    /// Attempts to open a network device in read-write mode
    pub fn open(adapter: u32, device: u32) -> Result<NetDevice> {
        let path = format!("/dev/dvb/adapter{}/net{}", adapter, device);
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .custom_flags(::nix::libc::O_NONBLOCK)
            .open(&path)
            .with_context(|| format!("NET: failed to open device {}", &path))?;

        let net = NetDevice {
            adapter,
            device,
            file,
        };

        Ok(net)
    }

    /// Creates a new network interface and returns interface number
    pub fn add_if(&self, pid: u16, feedtype: u8) -> Result<NetInterface> {
        let mut data = DvbNetIf {
            pid,
            if_num: 0,
            feedtype,
        };

        // NET_ADD_IF
        ioctl_readwrite!(#[inline] ioctl_call, b'o', 52, DvbNetIf);
        unsafe {
            ioctl_call(self.as_raw_fd(), &mut data as *mut _)
        }.context("NET: add if")?;

        Ok(NetInterface {
            net: self,
            if_num: data.if_num,
        })
    }

    /// Removes a network interface
    pub fn remove_if(&self, interface: NetInterface) -> Result<()> {
        // NET_REMOVE_IF
        ioctl_write_int_bad!(#[inline] ioctl_call, request_code_none!(b'o', 53));
        unsafe {
            ioctl_call(self.as_raw_fd(), i32::from(interface.if_num))
        }.context("NET: remove if")?;

        Ok(())
    }
}


pub struct NetInterface<'a> {
    net: &'a NetDevice,
    if_num: u16,
}


impl<'a> fmt::Display for NetInterface<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.net.device == 0 {
            write!(f, "dvb{}_{}", self.net.adapter, self.if_num)
        } else {
            write!(f, "dvb{}{}{}", self.net.adapter, self.net.device, self.if_num)
        }
    }
}


impl<'a> NetInterface<'a> {
    /// Returns interface mac address or empty mac on any error
    pub fn get_mac(&self) -> String {
        let path = format!("/sys/class/net/{}/address", self);
        let file = match File::open(&path) {
            Ok(v) => v,
            _ => return EMPTY_MAC.to_owned(),
        };

        let mut mac = String::with_capacity(MAC_SIZE);
        let result = file
            .take(MAC_SIZE as u64)
            .read_to_string(&mut mac);

        match result {
            Ok(MAC_SIZE) => mac,
            _ => EMPTY_MAC.to_owned(),
        }
    }
}