2016-02-05 15:58:32 +00:00
|
|
|
// VpnCloud - Peer-to-Peer VPN
|
|
|
|
// Copyright (C) 2015-2016 Dennis Schwerdel
|
|
|
|
// This software is licensed under GPL-3 or newer (see LICENSE.md)
|
|
|
|
|
2015-11-22 18:00:56 +00:00
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
|
|
|
use std::io::{Result as IoResult, Error as IoError, Read, Write};
|
|
|
|
use std::fs;
|
|
|
|
|
2015-11-24 11:12:15 +00:00
|
|
|
use super::types::{Error, Type};
|
2015-11-22 18:00:56 +00:00
|
|
|
|
|
|
|
extern {
|
|
|
|
fn setup_tap_device(fd: i32, ifname: *mut u8) -> i32;
|
|
|
|
fn setup_tun_device(fd: i32, ifname: *mut u8) -> i32;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-23 00:40:47 +00:00
|
|
|
pub struct Device {
|
|
|
|
fd: fs::File,
|
|
|
|
ifname: String
|
2015-11-22 18:00:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 00:40:47 +00:00
|
|
|
impl Device {
|
|
|
|
pub fn new(ifname: &str, type_: Type) -> IoResult<Self> {
|
|
|
|
let fd = try!(fs::OpenOptions::new().read(true).write(true).open("/dev/net/tun"));
|
2015-11-22 18:00:56 +00:00
|
|
|
let mut ifname_string = String::with_capacity(32);
|
|
|
|
ifname_string.push_str(ifname);
|
|
|
|
ifname_string.push('\0');
|
2015-11-23 14:58:55 +00:00
|
|
|
assert!(ifname_string.len() <= 32);
|
2015-11-22 18:00:56 +00:00
|
|
|
let mut ifname_c = ifname_string.into_bytes();
|
2015-11-23 00:40:47 +00:00
|
|
|
let res = match type_ {
|
|
|
|
Type::Tun => unsafe { setup_tun_device(fd.as_raw_fd(), ifname_c.as_mut_ptr()) },
|
|
|
|
Type::Tap => unsafe { setup_tap_device(fd.as_raw_fd(), ifname_c.as_mut_ptr()) }
|
|
|
|
};
|
2015-11-22 18:00:56 +00:00
|
|
|
match res {
|
2015-12-22 21:40:26 +00:00
|
|
|
0 => {
|
|
|
|
while ifname_c.last() == Some(&0) {
|
|
|
|
ifname_c.pop();
|
|
|
|
}
|
|
|
|
Ok(Device{fd: fd, ifname: String::from_utf8(ifname_c).unwrap()})
|
|
|
|
},
|
2015-11-22 18:00:56 +00:00
|
|
|
_ => Err(IoError::last_os_error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 19:37:06 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn dummy(ifname: &str, path: &str) -> IoResult<Self> {
|
|
|
|
Ok(Device{fd: try!(fs::OpenOptions::new().create(true).read(true).write(true).open(path)), ifname: ifname.to_string()})
|
|
|
|
}
|
|
|
|
|
2015-11-22 18:00:56 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn ifname(&self) -> &str {
|
|
|
|
&self.ifname
|
|
|
|
}
|
|
|
|
|
2015-11-25 20:05:11 +00:00
|
|
|
#[inline]
|
2015-11-24 11:12:15 +00:00
|
|
|
pub fn read(&mut self, mut buffer: &mut [u8]) -> Result<usize, Error> {
|
2015-11-22 18:00:56 +00:00
|
|
|
self.fd.read(&mut buffer).map_err(|_| Error::TunTapDevError("Read error"))
|
|
|
|
}
|
|
|
|
|
2015-11-25 20:05:11 +00:00
|
|
|
#[inline]
|
2015-11-24 11:12:15 +00:00
|
|
|
pub fn write(&mut self, data: &[u8]) -> Result<(), Error> {
|
2015-11-22 18:00:56 +00:00
|
|
|
match self.fd.write_all(&data) {
|
2015-12-03 08:38:14 +00:00
|
|
|
Ok(_) => self.fd.flush().map_err(|_| Error::TunTapDevError("Flush error")),
|
2015-11-22 18:00:56 +00:00
|
|
|
Err(_) => Err(Error::TunTapDevError("Write error"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-24 11:12:15 +00:00
|
|
|
|
|
|
|
impl AsRawFd for Device {
|
2015-11-25 20:05:11 +00:00
|
|
|
#[inline(always)]
|
2015-11-24 11:12:15 +00:00
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
|
|
self.fd.as_raw_fd()
|
|
|
|
}
|
|
|
|
}
|