TapDev -> TunTapDev

This commit is contained in:
Dennis Schwerdel 2015-11-21 17:06:57 +01:00
parent 8c414f9c23
commit 2fe004c074
6 changed files with 40 additions and 22 deletions

View File

@ -1,5 +1,5 @@
extern crate gcc;
fn main() {
gcc::Config::new().file("src/c/tapdev.c").include("src").compile("libtapdev.a");
}
gcc::Config::new().file("src/c/tuntap.c").include("src").compile("libtuntap.a");
}

View File

@ -1,9 +1,7 @@
default: libtapdev.a
tapdev.o: tapdev.c
gcc -Os -c tapdev.c
libtapdev.a: tapdev.o
ar rcs libtapdev.a tapdev.o
default: libtuntap.a
tapdev.o: tuntap.c
gcc -Os -c tuntap.c
libtapdev.a: tuntap.o
ar rcs libtuntap.a tuntap.o

View File

@ -4,12 +4,20 @@
#include <string.h>
#include <sys/ioctl.h>
int32_t setup_tap_device(int32_t fd, char *ifname) {
int32_t setup_device(int32_t fd, char *ifname, int32_t flags) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
ifr.ifr_flags = flags;
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) return 1;
strncpy(ifname, ifr.ifr_name, IFNAMSIZ);
return 0;
}
}
int32_t setup_tap_device(int32_t fd, char *ifname) {
return setup_device(fd, ifname, IFF_TAP | IFF_NO_PI);
}
int32_t setup_tun_device(int32_t fd, char *ifname) {
return setup_device(fd, ifname, IFF_TUN | IFF_NO_PI);
}

View File

@ -12,7 +12,7 @@ use epoll;
use super::{ethernet, udpmessage};
use super::udpmessage::{Options, Message};
use super::ethernet::MacTable;
use super::tapdev::TapDevice;
use super::tuntap::{TunTapDevice, DeviceType};
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
@ -109,7 +109,7 @@ pub struct EthCloud {
reconnect_peers: Vec<SocketAddr>,
mactable: MacTable,
socket: UdpSocket,
tapdev: TapDevice,
tapdev: TunTapDevice,
network_id: Option<NetworkId>,
next_peerlist: SteadyTime,
update_freq: Duration,
@ -123,7 +123,7 @@ impl EthCloud {
Ok(socket) => socket,
_ => panic!("Failed to open socket")
};
let tapdev = match TapDevice::new(device) {
let tapdev = match TunTapDevice::new(device, DeviceType::TapDevice) {
Ok(tapdev) => tapdev,
_ => panic!("Failed to open tap device")
};

View File

@ -6,7 +6,7 @@ extern crate epoll;
mod util;
mod udpmessage;
mod tapdev;
mod tuntap;
mod ethernet;
mod ip;
mod ethcloud;

View File

@ -4,22 +4,29 @@ use std::os::unix::io::{AsRawFd, RawFd};
extern {
fn setup_tap_device(fd: i32, ifname: *mut u8) -> i32;
fn setup_tun_device(fd: i32, ifname: *mut u8) -> i32;
}
pub struct TapDevice {
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)]
pub enum DeviceType {
TunDevice, TapDevice
}
pub struct TunTapDevice {
fd: fs::File,
ifname: String
ifname: String,
iftype: DeviceType
}
impl TapDevice {
pub fn new(ifname: &str) -> IoResult<TapDevice> {
impl TunTapDevice {
pub fn new(ifname: &str, iftype: DeviceType) -> IoResult<Self> {
let fd = try!(fs::OpenOptions::new().read(true).write(true).open("/dev/net/tun"));
let mut ifname_string = String::with_capacity(32);
ifname_string.push_str(ifname);
ifname_string.push('\0');
let mut ifname_c = ifname_string.into_bytes();
match unsafe { setup_tap_device(fd.as_raw_fd(), ifname_c.as_mut_ptr()) } {
0 => Ok(TapDevice{fd: fd, ifname: String::from_utf8(ifname_c).unwrap()}),
0 => Ok(TunTapDevice{fd: fd, ifname: String::from_utf8(ifname_c).unwrap(), iftype: iftype}),
_ => Err(IoError::last_os_error())
}
}
@ -29,6 +36,11 @@ impl TapDevice {
&self.ifname
}
#[inline(always)]
pub fn iftype(&self) -> DeviceType {
self.iftype
}
#[inline(always)]
pub fn read(&mut self, buffer: &mut [u8]) -> IoResult<usize> {
self.fd.read(buffer)
@ -40,7 +52,7 @@ impl TapDevice {
}
}
impl AsRawFd for TapDevice {
impl AsRawFd for TunTapDevice {
#[inline(always)]
fn as_raw_fd(&self) -> RawFd {
self.fd.as_raw_fd()