redox/schemes/ethernetd/src/scheme.rs

165 lines
4.6 KiB
Rust
Raw Normal View History

2016-10-23 23:26:36 +02:00
use std::collections::{BTreeMap, VecDeque};
use std::fs::File;
use std::io::{self, Read, Write};
2016-10-23 23:26:36 +02:00
use std::os::unix::io::AsRawFd;
use std::{cmp, str, u16};
use netutils::{getcfg, MacAddr, EthernetII};
use syscall;
use syscall::error::{Error, Result, EACCES, EBADF, EINVAL, EWOULDBLOCK};
use syscall::flag::O_NONBLOCK;
2016-10-23 23:26:36 +02:00
use syscall::scheme::SchemeMut;
2016-10-23 23:26:36 +02:00
#[derive(Clone)]
pub struct Handle {
/// The flags this handle was opened with
flags: usize,
2016-10-23 23:26:36 +02:00
/// The Host's MAC address
pub host_addr: MacAddr,
/// The ethernet type
pub ethertype: u16,
/// The data
pub frames: VecDeque<EthernetII>,
}
pub struct EthernetScheme {
network: File,
next_id: usize,
pub handles: BTreeMap<usize, Handle>
}
impl EthernetScheme {
pub fn new(network: File) -> EthernetScheme {
EthernetScheme {
network: network,
next_id: 1,
handles: BTreeMap::new(),
}
}
2016-10-23 23:26:36 +02:00
//TODO: Minimize allocation
//TODO: Reduce iteration cost (use BTreeMap of ethertype to handle?)
pub fn input(&mut self) -> io::Result<usize> {
let mut total = 0;
loop {
let mut bytes = [0; 65536];
let count = self.network.read(&mut bytes)?;
if count == 0 {
break;
}
if let Some(frame) = EthernetII::from_bytes(&bytes[.. count]) {
for (_id, handle) in self.handles.iter_mut() {
if frame.header.ethertype.get() == handle.ethertype {
handle.frames.push_back(frame.clone());
}
2016-10-23 23:26:36 +02:00
}
total += count;
2016-10-23 23:26:36 +02:00
}
}
Ok(total)
2016-10-23 23:26:36 +02:00
}
}
2016-10-23 23:26:36 +02:00
impl SchemeMut for EthernetScheme {
fn open(&mut self, url: &[u8], flags: usize, uid: u32, _gid: u32) -> Result<usize> {
if uid == 0 {
2016-10-23 23:26:36 +02:00
let mac_addr = MacAddr::from_str(&getcfg("mac").map_err(|err| err.into_sys())?);
let path = try!(str::from_utf8(url).or(Err(Error::new(EINVAL))));
let ethertype = u16::from_str_radix(path, 16).unwrap_or(0);
let next_id = self.next_id;
self.next_id += 1;
self.handles.insert(next_id, Handle {
flags: flags,
host_addr: mac_addr,
ethertype: ethertype,
frames: VecDeque::new()
});
Ok(next_id)
} else {
Err(Error::new(EACCES))
}
}
2016-10-23 23:26:36 +02:00
fn dup(&mut self, id: usize, _buf: &[u8]) -> Result<usize> {
2016-10-23 23:26:36 +02:00
let next_id = self.next_id;
self.next_id += 1;
let handle = {
let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;
handle.clone()
};
self.handles.insert(next_id, handle);
Ok(next_id)
}
fn read(&mut self, id: usize, buf: &mut [u8]) -> Result<usize> {
let handle = self.handles.get_mut(&id).ok_or(Error::new(EBADF))?;
if let Some(frame) = handle.frames.pop_front() {
let data = frame.to_bytes();
for (b, d) in buf.iter_mut().zip(data.iter()) {
2016-10-23 23:26:36 +02:00
*b = *d;
}
Ok(cmp::min(buf.len(), data.len()))
} else if handle.flags & O_NONBLOCK == O_NONBLOCK {
Ok(0)
2016-10-23 23:26:36 +02:00
} else {
Err(Error::new(EWOULDBLOCK))
}
}
fn write(&mut self, id: usize, buf: &[u8]) -> Result<usize> {
let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;
if let Some(mut frame) = EthernetII::from_bytes(buf) {
frame.header.src = handle.host_addr;
frame.header.ethertype.set(handle.ethertype);
self.network.write(&frame.to_bytes()).map_err(|err| err.into_sys())
} else {
Err(Error::new(EINVAL))
2016-10-23 23:26:36 +02:00
}
}
fn fevent(&mut self, id: usize, _flags: usize) -> Result<usize> {
let _handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;
Ok(id)
}
fn fpath(&mut self, id: usize, buf: &mut [u8]) -> Result<usize> {
let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;
let path_string = format!("ethernet:{:X}", handle.ethertype);
2016-10-23 23:26:36 +02:00
let path = path_string.as_bytes();
let mut i = 0;
while i < buf.len() && i < path.len() {
buf[i] = path[i];
i += 1;
2016-10-23 23:26:36 +02:00
}
Ok(i)
2016-10-23 23:26:36 +02:00
}
fn fsync(&mut self, id: usize) -> Result<usize> {
let _handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;
syscall::fsync(self.network.as_raw_fd())
}
fn close(&mut self, id: usize) -> Result<usize> {
let handle = self.handles.remove(&id).ok_or(Error::new(EBADF))?;
2016-10-23 23:26:36 +02:00
drop(handle);
2016-10-23 23:26:36 +02:00
Ok(0)
}
}