diff --git a/build.rs b/build.rs index b9d7f44..d342a35 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,5 @@ extern crate gcc; fn main() { - gcc::Config::new().file("src/c/tapdev.c").include("src").compile("libtapdev.a"); -} \ No newline at end of file + gcc::Config::new().file("src/c/tuntap.c").include("src").compile("libtuntap.a"); +} diff --git a/src/c/Makefile b/src/c/Makefile index 6eed684..2bce4c6 100644 --- a/src/c/Makefile +++ b/src/c/Makefile @@ -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 diff --git a/src/c/tapdev.c b/src/c/tuntap.c similarity index 58% rename from src/c/tapdev.c rename to src/c/tuntap.c index 53698a3..cc6112e 100644 --- a/src/c/tapdev.c +++ b/src/c/tuntap.c @@ -4,12 +4,20 @@ #include #include -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; -} \ No newline at end of file +} + +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); +} diff --git a/src/ethcloud.rs b/src/ethcloud.rs index 7488d7b..9c6ae0f 100644 --- a/src/ethcloud.rs +++ b/src/ethcloud.rs @@ -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, mactable: MacTable, socket: UdpSocket, - tapdev: TapDevice, + tapdev: TunTapDevice, network_id: Option, 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") }; diff --git a/src/main.rs b/src/main.rs index d23403f..34189dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ extern crate epoll; mod util; mod udpmessage; -mod tapdev; +mod tuntap; mod ethernet; mod ip; mod ethcloud; diff --git a/src/tapdev.rs b/src/tuntap.rs similarity index 65% rename from src/tapdev.rs rename to src/tuntap.rs index bec6170..6d365fc 100644 --- a/src/tapdev.rs +++ b/src/tuntap.rs @@ -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 { +impl TunTapDevice { + pub fn new(ifname: &str, iftype: DeviceType) -> IoResult { 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 { 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()