Initial support for UDP options.

This commit is contained in:
Egor Karavaev
2017-09-20 00:23:22 +03:00
parent 05650efcb1
commit a15184ce1f
2 changed files with 130 additions and 85 deletions
Generated
+37 -46
View File
@@ -6,22 +6,14 @@ dependencies = [
"netutils 0.1.0 (git+https://github.com/redox-os/netutils.git)",
"rand 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_event 0.1.0 (git+https://github.com/redox-os/event.git)",
"redox_syscall 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
"smoltcp 0.4.0-pre (git+https://github.com/batonius/smoltcp.git?branch=default_route)",
]
[[package]]
name = "arg_parser"
version = "0.1.0"
source = "git+https://github.com/redox-os/arg-parser.git#1b6a9505a1e9c39af1836ecbee293a987619a539"
[[package]]
name = "base64"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
source = "git+https://github.com/redox-os/arg-parser.git#288d2fd9ae27ed2c7a3aaf5a77cf07e2b2bd356c"
[[package]]
name = "base64"
@@ -81,12 +73,12 @@ source = "git+https://github.com/redox-os/libextra.git#402932084acd5fef481294588
[[package]]
name = "futures"
version = "0.1.14"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "gcc"
version = "0.3.53"
version = "0.3.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@@ -96,10 +88,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "hyper"
version = "0.10.12"
version = "0.10.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"base64 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"base64 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"httparse 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -117,7 +109,7 @@ name = "hyper-rustls"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"hyper 0.10.12 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)",
"rustls 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"webpki-roots 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -153,7 +145,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.29"
version = "0.2.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@@ -199,17 +191,17 @@ dependencies = [
[[package]]
name = "netutils"
version = "0.1.0"
source = "git+https://github.com/redox-os/netutils.git#849b72c5b9e77a654a51521276dc6f2223a67aec"
source = "git+https://github.com/redox-os/netutils.git#ec1a835a9d4a95a8e41c4e994a540d298d897cd4"
dependencies = [
"arg_parser 0.1.0 (git+https://github.com/redox-os/arg-parser.git)",
"extra 0.1.0 (git+https://github.com/redox-os/libextra.git)",
"hyper 0.10.12 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper-rustls 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
"ntpclient 0.0.1 (git+https://github.com/willem66745/ntpclient-rust)",
"pbr 1.0.0 (git+https://github.com/a8m/pb)",
"redox_event 0.1.0 (git+https://github.com/redox-os/event.git)",
"redox_syscall 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -227,7 +219,7 @@ name = "num_cpus"
version = "1.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -236,7 +228,7 @@ version = "1.0.0"
source = "git+https://github.com/a8m/pb#e9369ed2b94df4f554fe79c2643de41f7338475f"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -252,7 +244,7 @@ name = "rand"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
"magenta 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -270,9 +262,9 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"coco 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 1.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -282,12 +274,12 @@ name = "redox_event"
version = "0.1.0"
source = "git+https://github.com/redox-os/event.git#42d552e4765efbfb4ec39ce174900d3f48548a70"
dependencies = [
"redox_syscall 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "redox_syscall"
version = "0.1.30"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@@ -295,7 +287,7 @@ name = "redox_termios"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"redox_syscall 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -303,11 +295,11 @@ name = "ring"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.53 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"untrusted 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"untrusted 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -319,7 +311,7 @@ dependencies = [
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
"ring 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)",
"untrusted 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"untrusted 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"webpki 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -348,8 +340,8 @@ name = "termion"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -359,8 +351,8 @@ version = "0.1.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -397,7 +389,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "untrusted"
version = "0.5.0"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@@ -422,7 +414,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ring 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)",
"untrusted 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"untrusted 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -430,7 +422,7 @@ name = "webpki-roots"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"untrusted 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"untrusted 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"webpki 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -446,7 +438,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum arg_parser 0.1.0 (git+https://github.com/redox-os/arg-parser.git)" = "<none>"
"checksum base64 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "30e93c03064e7590d0466209155251b90c22e37fab1daf2771582598b5827557"
"checksum base64 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "96434f987501f0ed4eb336a411e0631ecd1afa11574fe148587adc4ff96143c9"
"checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d"
"checksum byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0fc10e8cc6b2580fda3f36eb6dc5316657f812a3df879a44a66fc9f0fdbc4855"
@@ -456,16 +447,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum custom_derive 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "ef8ae57c4978a2acd8b869ce6b9ca1dfe817bff704c220209fdef2c0b75a01b9"
"checksum either 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18785c1ba806c258137c937e44ada9ee7e69a37e3c72077542cd2f069d78562a"
"checksum extra 0.1.0 (git+https://github.com/redox-os/libextra.git)" = "<none>"
"checksum futures 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "4b63a4792d4f8f686defe3b39b92127fea6344de5d38202b2ee5a11bbbf29d6a"
"checksum gcc 0.3.53 (registry+https://github.com/rust-lang/crates.io-index)" = "e8310f7e9c890398b0e80e301c4f474e9918d2b27fca8f48486ca775fa9ffc5a"
"checksum futures 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "05a23db7bd162d4e8265968602930c476f688f0c180b44bdaf55e0cb2c687558"
"checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb"
"checksum httparse 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "af2f2dd97457e8fb1ae7c5a420db346af389926e36f43768b96f101546b04a07"
"checksum hyper 0.10.12 (registry+https://github.com/rust-lang/crates.io-index)" = "0f01e4a20f5dfa5278d7762b7bdb7cab96e24378b9eca3889fbd4b5e94dc7063"
"checksum hyper 0.10.13 (registry+https://github.com/rust-lang/crates.io-index)" = "368cb56b2740ebf4230520e2b90ebb0461e69034d85d1945febd9b3971426db2"
"checksum hyper-rustls 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "04535774f79684c99528944ebdb89756c945c027e55ce52faa245879d836c8fb"
"checksum idna 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "014b298351066f1512874135335d62a789ffe78a9974f94b43ed5621951eaf7d"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a"
"checksum lazy_static 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3b37545ab726dd833ec6420aaba8231c5b320814b9029ad585555d2a03e94fbf"
"checksum libc 0.2.29 (registry+https://github.com/rust-lang/crates.io-index)" = "8a014d9226c2cc402676fbe9ea2e15dd5222cd1dd57f576b5b283178c944a264"
"checksum libc 0.2.31 (registry+https://github.com/rust-lang/crates.io-index)" = "d1419b2939a0bc44b77feb34661583c7546b532b192feab36249ab584b86856c"
"checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b"
"checksum magenta 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4bf0336886480e671965f794bc9b6fce88503563013d1bfb7a502c81fe3ac527"
"checksum magenta-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "40d014c7011ac470ae28e2f76a02bfea4a8480f73e701353b49ad7a8d75f4699"
@@ -481,7 +472,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum rayon 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a77c51c07654ddd93f6cb543c7a849863b03abc7e82591afda6dc8ad4ac3ac4a"
"checksum rayon-core 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7febc28567082c345f10cddc3612c6ea020fc3297a1977d472cf9fdb73e6e493"
"checksum redox_event 0.1.0 (git+https://github.com/redox-os/event.git)" = "<none>"
"checksum redox_syscall 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)" = "8312fba776a49cf390b7b62f3135f9b294d8617f7a7592cfd0ac2492b658cd7b"
"checksum redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "8dde11f18c108289bef24469638a04dce49da56084f2d50618b226e47eb04509"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum ring 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2a6dc7fc06a05e6de183c5b97058582e9da2de0c136eafe49609769c507724"
"checksum rustls 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17727f4b991294da2c84d75a43c003151ff58072212768800f66c56ee46dca43"
@@ -495,7 +486,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum unicase 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7f4765f83163b74f957c797ad9253caf97f103fb064d3999aea9568d09fc8a33"
"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5"
"checksum unicode-normalization 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "51ccda9ef9efa3f7ef5d91e8f9b83bbe6955f9bf86aec89d5cce2c874625920f"
"checksum untrusted 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6b65243989ef6aacd9c0d6bd2b822765c3361d8ed352185a6f3a41f3a718c673"
"checksum untrusted 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f392d7819dbe58833e26872f5f6f0d68b7bbbe90fc3667e98731c4a15ad9a7ae"
"checksum url 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb819346883532a271eb626deb43c4a1bb4c4dd47c519bd78137c3e72a4fe27"
"checksum version_check 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6b772017e347561807c1aa192438c5fd74242a670a6cffacc40f2defd1dc069d"
"checksum webpki 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e499345fc4c6b7c79a5b8756d4592c4305510a13512e79efafe00dfbd67bbac6"
+93 -39
View File
@@ -2,22 +2,43 @@ use netutils::Ipv4Addr;
use smoltcp::socket::{AsSocket, SocketHandle, UdpPacketBuffer, UdpSocket, UdpSocketBuffer};
use smoltcp::wire::{IpAddress, IpEndpoint, Ipv4Address};
use std::collections::BTreeMap;
use std::str;
use syscall;
use device::NetworkDevice;
use error::Result;
use super::{Smolnetd, SocketSet};
pub struct UdpHandle {
enum Setting {
Ttl,
ReadTimeout,
WriteTimeout,
}
struct UdpHandle {
flags: usize,
events: usize,
remote_endpoint: IpEndpoint,
pub socket_handle: SocketHandle,
socket_handle: SocketHandle,
}
enum FdHandle {
Setting(SocketHandle, Setting),
Socket(UdpHandle),
}
impl FdHandle {
fn socket_handle(&self) -> SocketHandle {
match *self {
FdHandle::Socket(UdpHandle { socket_handle, .. }) => socket_handle,
FdHandle::Setting(socket_handle, _) => socket_handle,
}
}
}
pub struct UdpScheme {
next_udp_fd: usize,
udp_sockets: BTreeMap<usize, UdpHandle>,
udp_fds: BTreeMap<usize, FdHandle>,
socket_set: SocketSet,
}
@@ -25,17 +46,19 @@ impl UdpScheme {
pub fn new(socket_set: SocketSet) -> UdpScheme {
UdpScheme {
next_udp_fd: 1,
udp_sockets: BTreeMap::new(),
udp_fds: BTreeMap::new(),
socket_set,
}
}
pub fn notify_ready_sockets<F: FnMut(usize) -> Result<()>>(&self, mut f: F) -> Result<()> {
for (&fd, ref handle) in &self.udp_sockets {
let mut socket_set = self.socket_set.borrow_mut();
let socket: &mut UdpSocket = socket_set.get_mut(handle.socket_handle).as_socket();
if socket.can_send() {
f(fd)?
for (&fd, handle) in &self.udp_fds {
if let &FdHandle::Socket(UdpHandle { socket_handle, .. }) = handle {
let mut socket_set = self.socket_set.borrow_mut();
let socket: &mut UdpSocket = socket_set.get_mut(socket_handle).as_socket();
if socket.can_send() {
f(fd)?
}
}
}
Ok(())
@@ -44,8 +67,6 @@ impl UdpScheme {
impl syscall::SchemeMut for UdpScheme {
fn open(&mut self, url: &[u8], flags: usize, _uid: u32, _gid: u32) -> syscall::Result<usize> {
use std::str;
let path = str::from_utf8(url).or_else(|_| Err(syscall::Error::new(syscall::EINVAL)))?;
trace!("Udp open {} ", path);
let mut parts = path.split("/");
@@ -75,9 +96,9 @@ impl syscall::SchemeMut for UdpScheme {
let socket_handle = self.socket_set.borrow_mut().add(udp_socket);
let id = self.next_udp_fd;
self.udp_sockets.insert(
self.udp_fds.insert(
id,
UdpHandle {
FdHandle::Socket(UdpHandle {
flags,
events: 0,
socket_handle,
@@ -85,7 +106,7 @@ impl syscall::SchemeMut for UdpScheme {
IpAddress::Ipv4(Ipv4Address::from_bytes(&remote_ip.bytes)),
remote_port,
),
},
}),
);
self.next_udp_fd += 1;
trace!("Udp open fd {} ", id);
@@ -95,61 +116,94 @@ impl syscall::SchemeMut for UdpScheme {
fn close(&mut self, fd: usize) -> syscall::Result<usize> {
trace!("Upd close {}", fd);
let socket_handle = {
let handle = self.udp_sockets
let handle = self.udp_fds
.get(&fd)
.ok_or_else(|| syscall::Error::new(syscall::EBADF))?;
handle.socket_handle
handle.socket_handle()
};
self.udp_sockets.remove(&fd);
self.socket_set.borrow_mut().remove(socket_handle);
self.udp_fds.remove(&fd);
let mut socket_set = self.socket_set.borrow_mut();
socket_set.release(socket_handle);
//TODO: removing sockets in release should make prune unnecessary
socket_set.prune();
Ok(0)
}
fn write(&mut self, fd: usize, buf: &[u8]) -> syscall::Result<usize> {
trace!("Upd write {} len {}", fd, buf.len());
let handle = self.udp_sockets
let handle = self.udp_fds
.get(&fd)
.ok_or_else(|| syscall::Error::new(syscall::EBADF))?;
let mut socket_set = self.socket_set.borrow_mut();
let socket: &mut UdpSocket = socket_set.get_mut(handle.socket_handle).as_socket();
socket
.send_slice(buf, handle.remote_endpoint)
.expect("Can't send slice");
match *handle {
FdHandle::Setting(_, _) => {
//TODO: udp settings
// pretend we've accepted
}
FdHandle::Socket(ref handle) => {
let mut socket_set = self.socket_set.borrow_mut();
let socket: &mut UdpSocket = socket_set.get_mut(handle.socket_handle).as_socket();
socket
.send_slice(buf, handle.remote_endpoint)
.expect("Can't send slice");
}
}
Ok(buf.len())
}
fn read(&mut self, fd: usize, buf: &mut [u8]) -> syscall::Result<usize> {
let handle = self.udp_sockets.get(&fd).ok_or_else(|| {
let handle = self.udp_fds.get(&fd).ok_or_else(|| {
trace!("UDP read EBADF {}", fd);
syscall::Error::new(syscall::EBADF)
})?;
let mut socket_set = self.socket_set.borrow_mut();
let socket: &mut UdpSocket = socket_set.get_mut(handle.socket_handle).as_socket();
if socket.can_recv() {
let (length, _) = socket.recv_slice(buf).expect("Can't receive slice");
trace!("Upd read {}", fd);
Ok(length)
} else if handle.flags & syscall::O_NONBLOCK == syscall::O_NONBLOCK {
Ok(0)
} else {
Err(syscall::Error::new(syscall::EWOULDBLOCK))
match *handle {
FdHandle::Setting(_, _) => {
//TODO: udp settings
Ok(0)
}
FdHandle::Socket(ref handle) => {
let mut socket_set = self.socket_set.borrow_mut();
let socket: &mut UdpSocket = socket_set.get_mut(handle.socket_handle).as_socket();
if socket.can_recv() {
let (length, _) = socket.recv_slice(buf).expect("Can't receive slice");
trace!("Upd read {}", fd);
Ok(length)
} else if handle.flags & syscall::O_NONBLOCK == syscall::O_NONBLOCK {
Ok(0)
} else {
Err(syscall::Error::new(syscall::EWOULDBLOCK))
}
}
}
}
fn dup(&mut self, fd: usize, buf: &[u8]) -> syscall::Result<usize> {
let handle = self.udp_fds
.get_mut(&fd)
.ok_or_else(|| syscall::Error::new(syscall::EBADF))?;
let path = str::from_utf8(buf).or_else(|_| Err(syscall::Error::new(syscall::EINVAL)))?;
trace!("udp dup {} {}", fd, path);
Ok(0)
}
fn fevent(&mut self, fd: usize, events: usize) -> syscall::Result<usize> {
trace!("udp fevent {}", fd);
let handle = self.udp_sockets
let handle = self.udp_fds
.get_mut(&fd)
.ok_or_else(|| syscall::Error::new(syscall::EBADF))?;
handle.events = events;
Ok(fd)
match *handle {
FdHandle::Setting(_, _) => Err(syscall::Error::new(syscall::EBADF)),
FdHandle::Socket(ref mut handle) => {
handle.events = events;
Ok(fd)
}
}
}
fn fsync(&mut self, fd: usize) -> syscall::Result<usize> {
trace!("udp fsync {}", fd);
{
let _handle = self.udp_sockets
let _handle = self.udp_fds
.get_mut(&fd)
.ok_or_else(|| syscall::Error::new(syscall::EBADF))?;
}