mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-01-13 01:07:34 +00:00
net_util: implement Read, Write, and Pollable for Tap
BUG=none TEST=none Change-Id: I1baf242a605ee6ff8e3bcc26e49042c3c2dd8cbb Signed-off-by: Stephen Barber <smbarber@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/604935 Reviewed-by: Dylan Reid <dgreid@chromium.org>
This commit is contained in:
parent
2fb1969902
commit
604d989d7e
1 changed files with 26 additions and 0 deletions
|
@ -7,11 +7,13 @@ extern crate net_sys;
|
||||||
extern crate sys_util;
|
extern crate sys_util;
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use std::io::{Read, Write, Result as IoResult};
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::net;
|
use std::net;
|
||||||
use std::os::raw::*;
|
use std::os::raw::*;
|
||||||
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
|
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
|
||||||
|
|
||||||
|
use sys_util::Pollable;
|
||||||
use sys_util::{ioctl_with_val, ioctl_with_ref, ioctl_with_mut_ref};
|
use sys_util::{ioctl_with_val, ioctl_with_ref, ioctl_with_mut_ref};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -212,12 +214,36 @@ impl Tap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Read for Tap {
|
||||||
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
|
||||||
|
self.tap_file.read(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Write for Tap {
|
||||||
|
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
|
||||||
|
self.tap_file.write(&buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) -> IoResult<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl AsRawFd for Tap {
|
impl AsRawFd for Tap {
|
||||||
fn as_raw_fd(&self) -> RawFd {
|
fn as_raw_fd(&self) -> RawFd {
|
||||||
self.tap_file.as_raw_fd()
|
self.tap_file.as_raw_fd()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Safe since the tap fd's lifetime lasts as long as this trait object, and the
|
||||||
|
// tap fd is pollable.
|
||||||
|
unsafe impl Pollable for Tap {
|
||||||
|
fn pollable_fd(&self) -> RawFd {
|
||||||
|
self.tap_file.as_raw_fd()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
Loading…
Reference in a new issue