Add support for setting timeouts
This commit is contained in:
parent
762b95e777
commit
3813999a6b
|
@ -9,13 +9,14 @@ use std::cell::RefCell;
|
|||
use std::fs::File;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::{mem, slice, str};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::os::unix::io::FromRawFd;
|
||||
use std::rc::Rc;
|
||||
|
||||
use event::EventQueue;
|
||||
use netutils::{n16, Ipv4, Ipv4Addr, Ipv4Header, Checksum};
|
||||
use netutils::udp::{Udp, UdpHeader};
|
||||
use syscall::data::Packet;
|
||||
use syscall::data::{Packet, TimeSpec};
|
||||
use syscall::error::{Error, Result, EACCES, EADDRINUSE, EBADF, EIO, EINVAL, EMSGSIZE, ENOTCONN, EWOULDBLOCK};
|
||||
use syscall::flag::{EVENT_READ, F_GETFL, F_SETFL, O_ACCMODE, O_CREAT, O_RDWR, O_NONBLOCK};
|
||||
use syscall::scheme::SchemeMut;
|
||||
|
@ -27,15 +28,29 @@ fn parse_socket(socket: &str) -> (Ipv4Addr, u16) {
|
|||
(host, port)
|
||||
}
|
||||
|
||||
struct Handle {
|
||||
struct UdpHandle {
|
||||
local: (Ipv4Addr, u16),
|
||||
remote: (Ipv4Addr, u16),
|
||||
flags: usize,
|
||||
events: usize,
|
||||
read_timeout: Option<TimeSpec>,
|
||||
write_timeout: Option<TimeSpec>,
|
||||
ttl: u8,
|
||||
data: VecDeque<Vec<u8>>,
|
||||
todo: VecDeque<Packet>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
enum SettingKind {
|
||||
Read,
|
||||
Write
|
||||
}
|
||||
|
||||
enum Handle {
|
||||
Udp(UdpHandle),
|
||||
Setting(usize, SettingKind),
|
||||
}
|
||||
|
||||
struct Udpd {
|
||||
scheme_file: File,
|
||||
udp_file: File,
|
||||
|
@ -69,8 +84,10 @@ impl Udpd {
|
|||
if packet.a == (-EWOULDBLOCK) as usize {
|
||||
packet.a = a;
|
||||
if let Some(mut handle) = self.handles.get_mut(&packet.b) {
|
||||
if let Handle::Udp(ref mut handle) = *handle {
|
||||
handle.todo.push_back(packet);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.scheme_file.write(&packet)?;
|
||||
}
|
||||
|
@ -89,6 +106,7 @@ impl Udpd {
|
|||
if let Some(ip) = Ipv4::from_bytes(&bytes[.. count]) {
|
||||
if let Some(udp) = Udp::from_bytes(&ip.data) {
|
||||
for (id, handle) in self.handles.iter_mut() {
|
||||
if let Handle::Udp(ref mut handle) = *handle {
|
||||
// Local address not set or IP dst matches or is broadcast
|
||||
if (handle.local.0 == Ipv4Addr::NULL || ip.header.dst == handle.local.0 || ip.header.dst == Ipv4Addr::BROADCAST)
|
||||
// Local port matches UDP dst
|
||||
|
@ -134,6 +152,7 @@ impl Udpd {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -164,33 +183,43 @@ impl SchemeMut for Udpd {
|
|||
let id = self.next_id;
|
||||
self.next_id += 1;
|
||||
|
||||
self.handles.insert(id, Handle {
|
||||
self.handles.insert(id, Handle::Udp(UdpHandle {
|
||||
local: local,
|
||||
remote: remote,
|
||||
flags: flags,
|
||||
events: 0,
|
||||
ttl: 64,
|
||||
read_timeout: None,
|
||||
write_timeout: None,
|
||||
data: VecDeque::new(),
|
||||
todo: VecDeque::new(),
|
||||
});
|
||||
}));
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
fn dup(&mut self, file: usize, buf: &[u8]) -> Result<usize> {
|
||||
let mut handle = {
|
||||
let handle = self.handles.get(&file).ok_or(Error::new(EBADF))?;
|
||||
Handle {
|
||||
let handle = match *self.handles.get(&file).ok_or(Error::new(EBADF))? {
|
||||
Handle::Udp(ref handle) => {
|
||||
let mut handle = UdpHandle {
|
||||
local: handle.local,
|
||||
remote: handle.remote,
|
||||
flags: handle.flags,
|
||||
events: 0,
|
||||
ttl: handle.ttl,
|
||||
read_timeout: handle.read_timeout,
|
||||
write_timeout: handle.write_timeout,
|
||||
data: handle.data.clone(),
|
||||
todo: VecDeque::new(),
|
||||
}
|
||||
};
|
||||
|
||||
let path = str::from_utf8(buf).or(Err(Error::new(EINVAL)))?;
|
||||
|
||||
if path == "read_timeout" {
|
||||
Handle::Setting(file, SettingKind::Read)
|
||||
} else if path == "write_timeout" {
|
||||
Handle::Setting(file, SettingKind::Write)
|
||||
} else {
|
||||
if handle.remote.0 == Ipv4Addr::NULL || handle.remote.1 == 0 {
|
||||
handle.remote = parse_socket(path);
|
||||
}
|
||||
|
@ -199,6 +228,14 @@ impl SchemeMut for Udpd {
|
|||
*port = *port + 1;
|
||||
}
|
||||
|
||||
Handle::Udp(handle)
|
||||
}
|
||||
},
|
||||
Handle::Setting(file, kind) => {
|
||||
Handle::Setting(file, kind)
|
||||
}
|
||||
};
|
||||
|
||||
let id = self.next_id;
|
||||
self.next_id += 1;
|
||||
|
||||
|
@ -208,10 +245,10 @@ impl SchemeMut for Udpd {
|
|||
}
|
||||
|
||||
fn read(&mut self, file: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
let mut handle = self.handles.get_mut(&file).ok_or(Error::new(EBADF))?;
|
||||
|
||||
let (file, kind) = match *self.handles.get_mut(&file).ok_or(Error::new(EBADF))? {
|
||||
Handle::Udp(ref mut handle) => {
|
||||
if handle.remote.0 == Ipv4Addr::NULL || handle.remote.1 == 0 {
|
||||
Err(Error::new(ENOTCONN))
|
||||
return Err(Error::new(ENOTCONN));
|
||||
} else if let Some(data) = handle.data.pop_front() {
|
||||
let mut i = 0;
|
||||
while i < buf.len() && i < data.len() {
|
||||
|
@ -219,21 +256,43 @@ impl SchemeMut for Udpd {
|
|||
i += 1;
|
||||
}
|
||||
|
||||
Ok(i)
|
||||
return Ok(i);
|
||||
} else if handle.flags & O_NONBLOCK == O_NONBLOCK {
|
||||
Ok(0)
|
||||
return Ok(0);
|
||||
} else {
|
||||
Err(Error::new(EWOULDBLOCK))
|
||||
return Err(Error::new(EWOULDBLOCK));
|
||||
}
|
||||
},
|
||||
Handle::Setting(file, kind) => {
|
||||
(file, kind)
|
||||
}
|
||||
};
|
||||
|
||||
if let Handle::Udp(ref mut handle) = *self.handles.get_mut(&file).ok_or(Error::new(EBADF))? {
|
||||
let timeout = match kind {
|
||||
SettingKind::Read => handle.read_timeout,
|
||||
SettingKind::Write => handle.write_timeout
|
||||
};
|
||||
|
||||
let count = if let Some(timespec) = timeout {
|
||||
timespec.deref().read(buf).map_err(|err| Error::new(err.raw_os_error().unwrap_or(EIO)))?
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
Ok(count)
|
||||
} else {
|
||||
Err(Error::new(EBADF))
|
||||
}
|
||||
}
|
||||
|
||||
fn write(&mut self, file: usize, buf: &[u8]) -> Result<usize> {
|
||||
let handle = self.handles.get(&file).ok_or(Error::new(EBADF))?;
|
||||
|
||||
let (file, kind) = match *self.handles.get(&file).ok_or(Error::new(EBADF))? {
|
||||
Handle::Udp(ref handle) => {
|
||||
if handle.remote.0 == Ipv4Addr::NULL || handle.remote.1 == 0 {
|
||||
Err(Error::new(ENOTCONN))
|
||||
return Err(Error::new(ENOTCONN));
|
||||
} else if buf.len() >= 65507 {
|
||||
Err(Error::new(EMSGSIZE))
|
||||
return Err(Error::new(EMSGSIZE));
|
||||
} else {
|
||||
let udp_data = buf.to_vec();
|
||||
|
||||
|
@ -256,7 +315,7 @@ impl SchemeMut for Udpd {
|
|||
len: n16::new((ip_data.len() + mem::size_of::<Ipv4Header>()) as u16),
|
||||
id: n16::new(self.rng.gen()),
|
||||
flags_fragment: n16::new(0),
|
||||
ttl: 127,
|
||||
ttl: handle.ttl,
|
||||
proto: 0x11,
|
||||
checksum: Checksum { data: 0 },
|
||||
src: handle.local.0,
|
||||
|
@ -266,13 +325,36 @@ impl SchemeMut for Udpd {
|
|||
data: ip_data
|
||||
};
|
||||
|
||||
self.udp_file.write(&ip.to_bytes()).map_err(|err| Error::new(err.raw_os_error().unwrap_or(EIO))).and(Ok(buf.len()))
|
||||
return self.udp_file.write(&ip.to_bytes()).map_err(|err| Error::new(err.raw_os_error().unwrap_or(EIO))).and(Ok(buf.len()));
|
||||
}
|
||||
},
|
||||
Handle::Setting(file, kind) => {
|
||||
(file, kind)
|
||||
}
|
||||
};
|
||||
|
||||
if let Handle::Udp(ref mut handle) = *self.handles.get_mut(&file).ok_or(Error::new(EBADF))? {
|
||||
let (count, timeout) = if buf.len() >= mem::size_of::<TimeSpec>() {
|
||||
let mut timespec = TimeSpec::default();
|
||||
let count = timespec.deref_mut().write(buf).map_err(|err| Error::new(err.raw_os_error().unwrap_or(EIO)))?;
|
||||
(count, Some(timespec))
|
||||
} else {
|
||||
(0, None)
|
||||
};
|
||||
|
||||
match kind {
|
||||
SettingKind::Read => handle.read_timeout = timeout,
|
||||
SettingKind::Write => handle.write_timeout = timeout
|
||||
}
|
||||
|
||||
Ok(count)
|
||||
} else {
|
||||
Err(Error::new(EBADF))
|
||||
}
|
||||
}
|
||||
|
||||
fn fcntl(&mut self, file: usize, cmd: usize, arg: usize) -> Result<usize> {
|
||||
let mut handle = self.handles.get_mut(&file).ok_or(Error::new(EBADF))?;
|
||||
|
||||
if let Handle::Udp(ref mut handle) = *self.handles.get_mut(&file).ok_or(Error::new(EBADF))? {
|
||||
match cmd {
|
||||
F_GETFL => Ok(handle.flags),
|
||||
F_SETFL => {
|
||||
|
@ -281,19 +363,22 @@ impl SchemeMut for Udpd {
|
|||
},
|
||||
_ => Err(Error::new(EINVAL))
|
||||
}
|
||||
} else {
|
||||
Err(Error::new(EBADF))
|
||||
}
|
||||
}
|
||||
|
||||
fn fevent(&mut self, file: usize, flags: usize) -> Result<usize> {
|
||||
let mut handle = self.handles.get_mut(&file).ok_or(Error::new(EBADF))?;
|
||||
|
||||
if let Handle::Udp(ref mut handle) = *self.handles.get_mut(&file).ok_or(Error::new(EBADF))? {
|
||||
handle.events = flags;
|
||||
|
||||
Ok(file)
|
||||
} else {
|
||||
Err(Error::new(EBADF))
|
||||
}
|
||||
}
|
||||
|
||||
fn fpath(&mut self, id: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
let handle = self.handles.get(&id).ok_or(Error::new(EBADF))?;
|
||||
|
||||
fn fpath(&mut self, file: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
if let Handle::Udp(ref mut handle) = *self.handles.get_mut(&file).ok_or(Error::new(EBADF))? {
|
||||
let path_string = format!("udp:{}:{}/{}:{}", handle.remote.0.to_string(), handle.remote.1, handle.local.0.to_string(), handle.local.1);
|
||||
let path = path_string.as_bytes();
|
||||
|
||||
|
@ -304,6 +389,9 @@ impl SchemeMut for Udpd {
|
|||
}
|
||||
|
||||
Ok(i)
|
||||
} else {
|
||||
Err(Error::new(EBADF))
|
||||
}
|
||||
}
|
||||
|
||||
fn fsync(&mut self, file: usize) -> Result<usize> {
|
||||
|
@ -315,6 +403,7 @@ impl SchemeMut for Udpd {
|
|||
fn close(&mut self, file: usize) -> Result<usize> {
|
||||
let handle = self.handles.remove(&file).ok_or(Error::new(EBADF))?;
|
||||
|
||||
if let Handle::Udp(ref handle) = handle {
|
||||
let remove = if let Some(mut port) = self.ports.get_mut(&handle.local.1) {
|
||||
*port = *port + 1;
|
||||
*port == 0
|
||||
|
@ -325,6 +414,7 @@ impl SchemeMut for Udpd {
|
|||
if remove {
|
||||
drop(self.ports.remove(&handle.local.1));
|
||||
}
|
||||
}
|
||||
|
||||
drop(handle);
|
||||
|
||||
|
|
Loading…
Reference in a new issue