diff options
| author | Valentin <[email protected]> | 2018-06-15 18:57:24 +0200 |
|---|---|---|
| committer | FenrirWolf <[email protected]> | 2018-06-15 10:57:24 -0600 |
| commit | f2a90174bb36b9ad528e863ab34c02ebce002b02 (patch) | |
| tree | 959e8d67883d3a89e179b3549b1f30d28e51a87c /ctr-std/src/sys/redox/net | |
| parent | Merge pull request #68 from linouxis9/master (diff) | |
| download | ctru-rs-f2a90174bb36b9ad528e863ab34c02ebce002b02.tar.xz ctru-rs-f2a90174bb36b9ad528e863ab34c02ebce002b02.zip | |
Update for latest nightly 2018-06-09 (#70)
* Update for latest nightly 2018-06-09
* We now have a proper horizon os and sys modules in libstd
Diffstat (limited to 'ctr-std/src/sys/redox/net')
| -rw-r--r-- | ctr-std/src/sys/redox/net/dns/answer.rs | 22 | ||||
| -rw-r--r-- | ctr-std/src/sys/redox/net/dns/mod.rs | 215 | ||||
| -rw-r--r-- | ctr-std/src/sys/redox/net/dns/query.rs | 18 | ||||
| -rw-r--r-- | ctr-std/src/sys/redox/net/mod.rs | 115 | ||||
| -rw-r--r-- | ctr-std/src/sys/redox/net/netc.rs | 57 | ||||
| -rw-r--r-- | ctr-std/src/sys/redox/net/tcp.rs | 253 | ||||
| -rw-r--r-- | ctr-std/src/sys/redox/net/udp.rs | 242 |
7 files changed, 922 insertions, 0 deletions
diff --git a/ctr-std/src/sys/redox/net/dns/answer.rs b/ctr-std/src/sys/redox/net/dns/answer.rs new file mode 100644 index 0000000..8e6aaeb --- /dev/null +++ b/ctr-std/src/sys/redox/net/dns/answer.rs @@ -0,0 +1,22 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use string::String; +use vec::Vec; + +#[derive(Clone, Debug)] +pub struct DnsAnswer { + pub name: String, + pub a_type: u16, + pub a_class: u16, + pub ttl_a: u16, + pub ttl_b: u16, + pub data: Vec<u8> +} diff --git a/ctr-std/src/sys/redox/net/dns/mod.rs b/ctr-std/src/sys/redox/net/dns/mod.rs new file mode 100644 index 0000000..1a26257 --- /dev/null +++ b/ctr-std/src/sys/redox/net/dns/mod.rs @@ -0,0 +1,215 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub use self::answer::DnsAnswer; +pub use self::query::DnsQuery; + +use slice; +use u16; +use string::String; +use vec::Vec; + +mod answer; +mod query; + +#[unstable(feature = "n16", issue="0")] +#[allow(non_camel_case_types)] +#[derive(Copy, Clone, Debug, Default)] +#[repr(packed)] +pub struct n16 { + inner: u16 +} + +impl n16 { + #[unstable(feature = "n16", issue="0")] + pub fn as_bytes(&self) -> &[u8] { + unsafe { slice::from_raw_parts((&self.inner as *const u16) as *const u8, 2) } + } + + #[unstable(feature = "n16", issue="0")] + pub fn from_bytes(bytes: &[u8]) -> Self { + n16 { + inner: unsafe { slice::from_raw_parts(bytes.as_ptr() as *const u16, bytes.len()/2)[0] } + } + } +} + +#[unstable(feature = "n16", issue="0")] +impl From<u16> for n16 { + fn from(value: u16) -> Self { + n16 { + inner: value.to_be() + } + } +} + +#[unstable(feature = "n16", issue="0")] +impl From<n16> for u16 { + fn from(value: n16) -> Self { + u16::from_be(value.inner) + } +} + +#[derive(Clone, Debug)] +pub struct Dns { + pub transaction_id: u16, + pub flags: u16, + pub queries: Vec<DnsQuery>, + pub answers: Vec<DnsAnswer> +} + +impl Dns { + pub fn compile(&self) -> Vec<u8> { + let mut data = Vec::new(); + + macro_rules! push_u8 { + ($value:expr) => { + data.push($value); + }; + }; + + macro_rules! push_n16 { + ($value:expr) => { + data.extend_from_slice(n16::from($value).as_bytes()); + }; + }; + + push_n16!(self.transaction_id); + push_n16!(self.flags); + push_n16!(self.queries.len() as u16); + push_n16!(self.answers.len() as u16); + push_n16!(0); + push_n16!(0); + + for query in self.queries.iter() { + for part in query.name.split('.') { + push_u8!(part.len() as u8); + data.extend_from_slice(part.as_bytes()); + } + push_u8!(0); + push_n16!(query.q_type); + push_n16!(query.q_class); + } + + data + } + + pub fn parse(data: &[u8]) -> Result<Self, String> { + let name_ind = 0b11000000; + let mut i = 0; + + macro_rules! pop_u8 { + () => { + { + i += 1; + if i > data.len() { + return Err(format!("{}: {}: pop_u8", file!(), line!())); + } + data[i - 1] + } + }; + }; + + macro_rules! pop_n16 { + () => { + { + i += 2; + if i > data.len() { + return Err(format!("{}: {}: pop_n16", file!(), line!())); + } + u16::from(n16::from_bytes(&data[i - 2 .. i])) + } + }; + }; + + macro_rules! pop_data { + () => { + { + let mut data = Vec::new(); + + let data_len = pop_n16!(); + for _data_i in 0..data_len { + data.push(pop_u8!()); + } + + data + } + }; + }; + + macro_rules! pop_name { + () => { + { + let mut name = String::new(); + let old_i = i; + + loop { + let name_len = pop_u8!(); + if name_len & name_ind == name_ind { + i -= 1; + i = (pop_n16!() - ((name_ind as u16) << 8)) as usize; + continue; + } + if name_len == 0 { + break; + } + if ! name.is_empty() { + name.push('.'); + } + for _name_i in 0..name_len { + name.push(pop_u8!() as char); + } + } + + if i <= old_i { + i = old_i + 2; + } + + name + } + }; + }; + + let transaction_id = pop_n16!(); + let flags = pop_n16!(); + let queries_len = pop_n16!(); + let answers_len = pop_n16!(); + pop_n16!(); + pop_n16!(); + + let mut queries = Vec::new(); + for _query_i in 0..queries_len { + queries.push(DnsQuery { + name: pop_name!(), + q_type: pop_n16!(), + q_class: pop_n16!() + }); + } + + let mut answers = Vec::new(); + for _answer_i in 0..answers_len { + answers.push(DnsAnswer { + name: pop_name!(), + a_type: pop_n16!(), + a_class: pop_n16!(), + ttl_a: pop_n16!(), + ttl_b: pop_n16!(), + data: pop_data!() + }); + } + + Ok(Dns { + transaction_id, + flags, + queries, + answers, + }) + } +} diff --git a/ctr-std/src/sys/redox/net/dns/query.rs b/ctr-std/src/sys/redox/net/dns/query.rs new file mode 100644 index 0000000..b0dcdcb --- /dev/null +++ b/ctr-std/src/sys/redox/net/dns/query.rs @@ -0,0 +1,18 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use string::String; + +#[derive(Clone, Debug)] +pub struct DnsQuery { + pub name: String, + pub q_type: u16, + pub q_class: u16 +} diff --git a/ctr-std/src/sys/redox/net/mod.rs b/ctr-std/src/sys/redox/net/mod.rs new file mode 100644 index 0000000..0291d7f --- /dev/null +++ b/ctr-std/src/sys/redox/net/mod.rs @@ -0,0 +1,115 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use fs::File; +use io::{Error, Result, Read}; +use iter::Iterator; +use net::{Ipv4Addr, SocketAddr, SocketAddrV4}; +use str::FromStr; +use string::{String, ToString}; +use sys::syscall::EINVAL; +use time::{self, Duration}; +use vec::{IntoIter, Vec}; + +use self::dns::{Dns, DnsQuery}; + +pub use self::tcp::{TcpStream, TcpListener}; +pub use self::udp::UdpSocket; + +pub mod netc; + +mod dns; +mod tcp; +mod udp; + +pub struct LookupHost(IntoIter<SocketAddr>); + +impl Iterator for LookupHost { + type Item = SocketAddr; + fn next(&mut self) -> Option<Self::Item> { + self.0.next() + } +} + +pub fn lookup_host(host: &str) -> Result<LookupHost> { + let mut ip_string = String::new(); + File::open("/etc/net/ip")?.read_to_string(&mut ip_string)?; + let ip: Vec<u8> = ip_string.trim().split(".").map(|part| part.parse::<u8>() + .unwrap_or(0)).collect(); + + let mut dns_string = String::new(); + File::open("/etc/net/dns")?.read_to_string(&mut dns_string)?; + let dns: Vec<u8> = dns_string.trim().split(".").map(|part| part.parse::<u8>() + .unwrap_or(0)).collect(); + + if ip.len() == 4 && dns.len() == 4 { + let time = time::SystemTime::now().duration_since(time::UNIX_EPOCH).unwrap(); + let tid = (time.subsec_nanos() >> 16) as u16; + + let packet = Dns { + transaction_id: tid, + flags: 0x0100, + queries: vec![DnsQuery { + name: host.to_string(), + q_type: 0x0001, + q_class: 0x0001, + }], + answers: vec![] + }; + + let packet_data = packet.compile(); + + let my_ip = Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3]); + let dns_ip = Ipv4Addr::new(dns[0], dns[1], dns[2], dns[3]); + let socket = UdpSocket::bind(&SocketAddr::V4(SocketAddrV4::new(my_ip, 0)))?; + socket.set_read_timeout(Some(Duration::new(5, 0)))?; + socket.set_write_timeout(Some(Duration::new(5, 0)))?; + socket.connect(&SocketAddr::V4(SocketAddrV4::new(dns_ip, 53)))?; + socket.send(&packet_data)?; + + let mut buf = [0; 65536]; + let count = socket.recv(&mut buf)?; + + match Dns::parse(&buf[.. count]) { + Ok(response) => { + let mut addrs = vec![]; + for answer in response.answers.iter() { + if answer.a_type == 0x0001 && answer.a_class == 0x0001 + && answer.data.len() == 4 + { + let answer_ip = Ipv4Addr::new(answer.data[0], + answer.data[1], + answer.data[2], + answer.data[3]); + addrs.push(SocketAddr::V4(SocketAddrV4::new(answer_ip, 0))); + } + } + Ok(LookupHost(addrs.into_iter())) + }, + Err(_err) => Err(Error::from_raw_os_error(EINVAL)) + } + } else { + Err(Error::from_raw_os_error(EINVAL)) + } +} + +fn path_to_peer_addr(path_str: &str) -> SocketAddr { + let mut parts = path_str.split('/').next().unwrap_or("").split(':').skip(1); + let host = Ipv4Addr::from_str(parts.next().unwrap_or("")).unwrap_or(Ipv4Addr::new(0, 0, 0, 0)); + let port = parts.next().unwrap_or("").parse::<u16>().unwrap_or(0); + SocketAddr::V4(SocketAddrV4::new(host, port)) +} + +fn path_to_local_addr(path_str: &str) -> SocketAddr { + let mut parts = path_str.split('/').nth(1).unwrap_or("").split(':'); + let host = Ipv4Addr::from_str(parts.next().unwrap_or("")).unwrap_or(Ipv4Addr::new(0, 0, 0, 0)); + let port = parts.next().unwrap_or("").parse::<u16>().unwrap_or(0); + SocketAddr::V4(SocketAddrV4::new(host, port)) +} diff --git a/ctr-std/src/sys/redox/net/netc.rs b/ctr-std/src/sys/redox/net/netc.rs new file mode 100644 index 0000000..d443a4d --- /dev/null +++ b/ctr-std/src/sys/redox/net/netc.rs @@ -0,0 +1,57 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub type in_addr_t = u32; +pub type in_port_t = u16; + +pub type socklen_t = u32; +pub type sa_family_t = u16; + +pub const AF_INET: sa_family_t = 2; +pub const AF_INET6: sa_family_t = 23; + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct in_addr { + pub s_addr: in_addr_t, +} + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct in6_addr { + pub s6_addr: [u8; 16], + __align: [u32; 0], +} + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct sockaddr { + pub sa_family: sa_family_t, + pub sa_data: [u8; 14], +} + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct sockaddr_in { + pub sin_family: sa_family_t, + pub sin_port: in_port_t, + pub sin_addr: in_addr, + pub sin_zero: [u8; 8], +} + +#[derive(Copy, Clone)] +#[repr(C)] +pub struct sockaddr_in6 { + pub sin6_family: sa_family_t, + pub sin6_port: in_port_t, + pub sin6_flowinfo: u32, + pub sin6_addr: in6_addr, + pub sin6_scope_id: u32, +} diff --git a/ctr-std/src/sys/redox/net/tcp.rs b/ctr-std/src/sys/redox/net/tcp.rs new file mode 100644 index 0000000..b566490 --- /dev/null +++ b/ctr-std/src/sys/redox/net/tcp.rs @@ -0,0 +1,253 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use cmp; +use io::{self, Error, ErrorKind, Result}; +use mem; +use net::{SocketAddr, Shutdown}; +use path::Path; +use sys::fs::{File, OpenOptions}; +use sys::syscall::TimeSpec; +use sys_common::{AsInner, FromInner, IntoInner}; +use time::Duration; + +use super::{path_to_peer_addr, path_to_local_addr}; + +#[derive(Debug)] +pub struct TcpStream(File); + +impl TcpStream { + pub fn connect(addr: &SocketAddr) -> Result<TcpStream> { + let path = format!("tcp:{}", addr); + let mut options = OpenOptions::new(); + options.read(true); + options.write(true); + Ok(TcpStream(File::open(Path::new(path.as_str()), &options)?)) + } + + pub fn connect_timeout(_addr: &SocketAddr, _timeout: Duration) -> Result<TcpStream> { + Err(Error::new(ErrorKind::Other, "TcpStream::connect_timeout not implemented")) + } + + pub fn duplicate(&self) -> Result<TcpStream> { + Ok(TcpStream(self.0.dup(&[])?)) + } + + pub fn read(&self, buf: &mut [u8]) -> Result<usize> { + self.0.read(buf) + } + + pub fn write(&self, buf: &[u8]) -> Result<usize> { + self.0.write(buf) + } + + pub fn take_error(&self) -> Result<Option<Error>> { + Ok(None) + } + + pub fn peer_addr(&self) -> Result<SocketAddr> { + let path = self.0.path()?; + Ok(path_to_peer_addr(path.to_str().unwrap_or(""))) + } + + pub fn socket_addr(&self) -> Result<SocketAddr> { + let path = self.0.path()?; + Ok(path_to_local_addr(path.to_str().unwrap_or(""))) + } + + pub fn peek(&self, _buf: &mut [u8]) -> Result<usize> { + Err(Error::new(ErrorKind::Other, "TcpStream::peek not implemented")) + } + + pub fn shutdown(&self, _how: Shutdown) -> Result<()> { + Err(Error::new(ErrorKind::Other, "TcpStream::shutdown not implemented")) + } + + pub fn nodelay(&self) -> Result<bool> { + Err(Error::new(ErrorKind::Other, "TcpStream::nodelay not implemented")) + } + + pub fn nonblocking(&self) -> Result<bool> { + self.0.fd().nonblocking() + } + + pub fn only_v6(&self) -> Result<bool> { + Err(Error::new(ErrorKind::Other, "TcpStream::only_v6 not implemented")) + } + + pub fn ttl(&self) -> Result<u32> { + let mut ttl = [0]; + let file = self.0.dup(b"ttl")?; + file.read(&mut ttl)?; + Ok(ttl[0] as u32) + } + + pub fn read_timeout(&self) -> Result<Option<Duration>> { + let mut time = TimeSpec::default(); + let file = self.0.dup(b"read_timeout")?; + if file.read(&mut time)? >= mem::size_of::<TimeSpec>() { + Ok(Some(Duration::new(time.tv_sec as u64, time.tv_nsec as u32))) + } else { + Ok(None) + } + } + + pub fn write_timeout(&self) -> Result<Option<Duration>> { + let mut time = TimeSpec::default(); + let file = self.0.dup(b"write_timeout")?; + if file.read(&mut time)? >= mem::size_of::<TimeSpec>() { + Ok(Some(Duration::new(time.tv_sec as u64, time.tv_nsec as u32))) + } else { + Ok(None) + } + } + + pub fn set_nodelay(&self, _nodelay: bool) -> Result<()> { + Err(Error::new(ErrorKind::Other, "TcpStream::set_nodelay not implemented")) + } + + pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()> { + self.0.fd().set_nonblocking(nonblocking) + } + + pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> { + Err(Error::new(ErrorKind::Other, "TcpStream::set_only_v6 not implemented")) + } + + pub fn set_ttl(&self, ttl: u32) -> Result<()> { + let file = self.0.dup(b"ttl")?; + file.write(&[cmp::min(ttl, 255) as u8])?; + Ok(()) + } + + pub fn set_read_timeout(&self, duration_option: Option<Duration>) -> Result<()> { + let file = self.0.dup(b"read_timeout")?; + if let Some(duration) = duration_option { + if duration.as_secs() == 0 && duration.subsec_nanos() == 0 { + return Err(io::Error::new(io::ErrorKind::InvalidInput, + "cannot set a 0 duration timeout")); + } + file.write(&TimeSpec { + tv_sec: duration.as_secs() as i64, + tv_nsec: duration.subsec_nanos() as i32 + })?; + } else { + file.write(&[])?; + } + Ok(()) + } + + pub fn set_write_timeout(&self, duration_option: Option<Duration>) -> Result<()> { + let file = self.0.dup(b"write_timeout")?; + if let Some(duration) = duration_option { + if duration.as_secs() == 0 && duration.subsec_nanos() == 0 { + return Err(io::Error::new(io::ErrorKind::InvalidInput, + "cannot set a 0 duration timeout")); + } + file.write(&TimeSpec { + tv_sec: duration.as_secs() as i64, + tv_nsec: duration.subsec_nanos() as i32 + })?; + } else { + file.write(&[])?; + } + Ok(()) + } +} + +impl AsInner<File> for TcpStream { + fn as_inner(&self) -> &File { &self.0 } +} + +impl FromInner<File> for TcpStream { + fn from_inner(file: File) -> TcpStream { + TcpStream(file) + } +} + +impl IntoInner<File> for TcpStream { + fn into_inner(self) -> File { self.0 } +} + +#[derive(Debug)] +pub struct TcpListener(File); + +impl TcpListener { + pub fn bind(addr: &SocketAddr) -> Result<TcpListener> { + let path = format!("tcp:/{}", addr); + let mut options = OpenOptions::new(); + options.read(true); + options.write(true); + Ok(TcpListener(File::open(Path::new(path.as_str()), &options)?)) + } + + pub fn accept(&self) -> Result<(TcpStream, SocketAddr)> { + let file = self.0.dup(b"listen")?; + let path = file.path()?; + let peer_addr = path_to_peer_addr(path.to_str().unwrap_or("")); + Ok((TcpStream(file), peer_addr)) + } + + pub fn duplicate(&self) -> Result<TcpListener> { + Ok(TcpListener(self.0.dup(&[])?)) + } + + pub fn take_error(&self) -> Result<Option<Error>> { + Ok(None) + } + + pub fn socket_addr(&self) -> Result<SocketAddr> { + let path = self.0.path()?; + Ok(path_to_local_addr(path.to_str().unwrap_or(""))) + } + + pub fn nonblocking(&self) -> Result<bool> { + Err(Error::new(ErrorKind::Other, "TcpListener::nonblocking not implemented")) + } + + pub fn only_v6(&self) -> Result<bool> { + Err(Error::new(ErrorKind::Other, "TcpListener::only_v6 not implemented")) + } + + pub fn ttl(&self) -> Result<u32> { + let mut ttl = [0]; + let file = self.0.dup(b"ttl")?; + file.read(&mut ttl)?; + Ok(ttl[0] as u32) + } + + pub fn set_nonblocking(&self, _nonblocking: bool) -> Result<()> { + Err(Error::new(ErrorKind::Other, "TcpListener::set_nonblocking not implemented")) + } + + pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> { + Err(Error::new(ErrorKind::Other, "TcpListener::set_only_v6 not implemented")) + } + + pub fn set_ttl(&self, ttl: u32) -> Result<()> { + let file = self.0.dup(b"ttl")?; + file.write(&[cmp::min(ttl, 255) as u8])?; + Ok(()) + } +} + +impl AsInner<File> for TcpListener { + fn as_inner(&self) -> &File { &self.0 } +} + +impl FromInner<File> for TcpListener { + fn from_inner(file: File) -> TcpListener { + TcpListener(file) + } +} + +impl IntoInner<File> for TcpListener { + fn into_inner(self) -> File { self.0 } +} diff --git a/ctr-std/src/sys/redox/net/udp.rs b/ctr-std/src/sys/redox/net/udp.rs new file mode 100644 index 0000000..2ed67bd --- /dev/null +++ b/ctr-std/src/sys/redox/net/udp.rs @@ -0,0 +1,242 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use cell::UnsafeCell; +use cmp; +use io::{self, Error, ErrorKind, Result}; +use mem; +use net::{SocketAddr, Ipv4Addr, Ipv6Addr}; +use path::Path; +use sys::fs::{File, OpenOptions}; +use sys::syscall::TimeSpec; +use sys_common::{AsInner, FromInner, IntoInner}; +use time::Duration; + +use super::{path_to_peer_addr, path_to_local_addr}; + +#[derive(Debug)] +pub struct UdpSocket(File, UnsafeCell<Option<SocketAddr>>); + +impl UdpSocket { + pub fn bind(addr: &SocketAddr) -> Result<UdpSocket> { + let path = format!("udp:/{}", addr); + let mut options = OpenOptions::new(); + options.read(true); + options.write(true); + Ok(UdpSocket(File::open(Path::new(path.as_str()), &options)?, UnsafeCell::new(None))) + } + + fn get_conn(&self) -> &mut Option<SocketAddr> { + unsafe { &mut *(self.1.get()) } + } + + pub fn connect(&self, addr: &SocketAddr) -> Result<()> { + unsafe { *self.1.get() = Some(*addr) }; + Ok(()) + } + + pub fn duplicate(&self) -> Result<UdpSocket> { + let new_bind = self.0.dup(&[])?; + let new_conn = *self.get_conn(); + Ok(UdpSocket(new_bind, UnsafeCell::new(new_conn))) + } + + pub fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)> { + let from = self.0.dup(b"listen")?; + let path = from.path()?; + let peer_addr = path_to_peer_addr(path.to_str().unwrap_or("")); + let count = from.read(buf)?; + Ok((count, peer_addr)) + } + + pub fn recv(&self, buf: &mut [u8]) -> Result<usize> { + if let Some(addr) = *self.get_conn() { + let from = self.0.dup(format!("{}", addr).as_bytes())?; + from.read(buf) + } else { + Err(Error::new(ErrorKind::Other, "UdpSocket::recv not connected")) + } + } + + pub fn send_to(&self, buf: &[u8], addr: &SocketAddr) -> Result<usize> { + let to = self.0.dup(format!("{}", addr).as_bytes())?; + to.write(buf) + } + + pub fn send(&self, buf: &[u8]) -> Result<usize> { + if let Some(addr) = *self.get_conn() { + self.send_to(buf, &addr) + } else { + Err(Error::new(ErrorKind::Other, "UdpSocket::send not connected")) + } + } + + pub fn take_error(&self) -> Result<Option<Error>> { + Ok(None) + } + + pub fn socket_addr(&self) -> Result<SocketAddr> { + let path = self.0.path()?; + Ok(path_to_local_addr(path.to_str().unwrap_or(""))) + } + + pub fn peek(&self, _buf: &mut [u8]) -> Result<usize> { + Err(Error::new(ErrorKind::Other, "UdpSocket::peek not implemented")) + } + + pub fn peek_from(&self, _buf: &mut [u8]) -> Result<(usize, SocketAddr)> { + Err(Error::new(ErrorKind::Other, "UdpSocket::peek_from not implemented")) + } + + pub fn broadcast(&self) -> Result<bool> { + Err(Error::new(ErrorKind::Other, "UdpSocket::broadcast not implemented")) + } + + pub fn multicast_loop_v4(&self) -> Result<bool> { + Err(Error::new(ErrorKind::Other, "UdpSocket::multicast_loop_v4 not implemented")) + } + + pub fn multicast_loop_v6(&self) -> Result<bool> { + Err(Error::new(ErrorKind::Other, "UdpSocket::multicast_loop_v6 not implemented")) + } + + pub fn multicast_ttl_v4(&self) -> Result<u32> { + Err(Error::new(ErrorKind::Other, "UdpSocket::multicast_ttl_v4 not implemented")) + } + + pub fn nonblocking(&self) -> Result<bool> { + self.0.fd().nonblocking() + } + + pub fn only_v6(&self) -> Result<bool> { + Err(Error::new(ErrorKind::Other, "UdpSocket::only_v6 not implemented")) + } + + pub fn ttl(&self) -> Result<u32> { + let mut ttl = [0]; + let file = self.0.dup(b"ttl")?; + file.read(&mut ttl)?; + Ok(ttl[0] as u32) + } + + pub fn read_timeout(&self) -> Result<Option<Duration>> { + let mut time = TimeSpec::default(); + let file = self.0.dup(b"read_timeout")?; + if file.read(&mut time)? >= mem::size_of::<TimeSpec>() { + Ok(Some(Duration::new(time.tv_sec as u64, time.tv_nsec as u32))) + } else { + Ok(None) + } + } + + pub fn write_timeout(&self) -> Result<Option<Duration>> { + let mut time = TimeSpec::default(); + let file = self.0.dup(b"write_timeout")?; + if file.read(&mut time)? >= mem::size_of::<TimeSpec>() { + Ok(Some(Duration::new(time.tv_sec as u64, time.tv_nsec as u32))) + } else { + Ok(None) + } + } + + pub fn set_broadcast(&self, _broadcast: bool) -> Result<()> { + Err(Error::new(ErrorKind::Other, "UdpSocket::set_broadcast not implemented")) + } + + pub fn set_multicast_loop_v4(&self, _multicast_loop_v4: bool) -> Result<()> { + Err(Error::new(ErrorKind::Other, "UdpSocket::set_multicast_loop_v4 not implemented")) + } + + pub fn set_multicast_loop_v6(&self, _multicast_loop_v6: bool) -> Result<()> { + Err(Error::new(ErrorKind::Other, "UdpSocket::set_multicast_loop_v6 not implemented")) + } + + pub fn set_multicast_ttl_v4(&self, _multicast_ttl_v4: u32) -> Result<()> { + Err(Error::new(ErrorKind::Other, "UdpSocket::set_multicast_ttl_v4 not implemented")) + } + + pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()> { + self.0.fd().set_nonblocking(nonblocking) + } + + pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> { + Err(Error::new(ErrorKind::Other, "UdpSocket::set_only_v6 not implemented")) + } + + pub fn set_ttl(&self, ttl: u32) -> Result<()> { + let file = self.0.dup(b"ttl")?; + file.write(&[cmp::min(ttl, 255) as u8])?; + Ok(()) + } + + pub fn set_read_timeout(&self, duration_option: Option<Duration>) -> Result<()> { + let file = self.0.dup(b"read_timeout")?; + if let Some(duration) = duration_option { + if duration.as_secs() == 0 && duration.subsec_nanos() == 0 { + return Err(io::Error::new(io::ErrorKind::InvalidInput, + "cannot set a 0 duration timeout")); + } + file.write(&TimeSpec { + tv_sec: duration.as_secs() as i64, + tv_nsec: duration.subsec_nanos() as i32 + })?; + } else { + file.write(&[])?; + } + Ok(()) + } + + pub fn set_write_timeout(&self, duration_option: Option<Duration>) -> Result<()> { + let file = self.0.dup(b"write_timeout")?; + if let Some(duration) = duration_option { + if duration.as_secs() == 0 && duration.subsec_nanos() == 0 { + return Err(io::Error::new(io::ErrorKind::InvalidInput, + "cannot set a 0 duration timeout")); + } + file.write(&TimeSpec { + tv_sec: duration.as_secs() as i64, + tv_nsec: duration.subsec_nanos() as i32 + })?; + } else { + file.write(&[])?; + } + Ok(()) + } + + pub fn join_multicast_v4(&self, _multiaddr: &Ipv4Addr, _interface: &Ipv4Addr) -> Result<()> { + Err(Error::new(ErrorKind::Other, "UdpSocket::join_multicast_v4 not implemented")) + } + + pub fn join_multicast_v6(&self, _multiaddr: &Ipv6Addr, _interface: u32) -> Result<()> { + Err(Error::new(ErrorKind::Other, "UdpSocket::join_multicast_v6 not implemented")) + } + + pub fn leave_multicast_v4(&self, _multiaddr: &Ipv4Addr, _interface: &Ipv4Addr) -> Result<()> { + Err(Error::new(ErrorKind::Other, "UdpSocket::leave_multicast_v4 not implemented")) + } + + pub fn leave_multicast_v6(&self, _multiaddr: &Ipv6Addr, _interface: u32) -> Result<()> { + Err(Error::new(ErrorKind::Other, "UdpSocket::leave_multicast_v6 not implemented")) + } +} + +impl AsInner<File> for UdpSocket { + fn as_inner(&self) -> &File { &self.0 } +} + +impl FromInner<File> for UdpSocket { + fn from_inner(file: File) -> UdpSocket { + UdpSocket(file, UnsafeCell::new(None)) + } +} + +impl IntoInner<File> for UdpSocket { + fn into_inner(self) -> File { self.0 } +} |