]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/redox/net/udp.rs
New upstream version 1.15.0+dfsg1
[rustc.git] / src / libstd / sys / redox / net / udp.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use cell::UnsafeCell;
12 use io::{Error, ErrorKind, Result};
13 use net::{SocketAddr, Ipv4Addr, Ipv6Addr};
14 use path::Path;
15 use sys::fs::{File, OpenOptions};
16 use time::Duration;
17
18 use super::{path_to_peer_addr, path_to_local_addr};
19
20 #[derive(Debug)]
21 pub struct UdpSocket(File, UnsafeCell<Option<SocketAddr>>);
22
23 impl UdpSocket {
24 pub fn bind(addr: &SocketAddr) -> Result<UdpSocket> {
25 let path = format!("udp:/{}", addr);
26 let mut options = OpenOptions::new();
27 options.read(true);
28 options.write(true);
29 Ok(UdpSocket(File::open(&Path::new(path.as_str()), &options)?, UnsafeCell::new(None)))
30 }
31
32 fn get_conn(&self) -> &mut Option<SocketAddr> {
33 unsafe { &mut *(self.1.get()) }
34 }
35
36 pub fn connect(&self, addr: &SocketAddr) -> Result<()> {
37 unsafe { *self.1.get() = Some(*addr) };
38 Ok(())
39 }
40
41 pub fn duplicate(&self) -> Result<UdpSocket> {
42 let new_bind = self.0.dup(&[])?;
43 let new_conn = *self.get_conn();
44 Ok(UdpSocket(new_bind, UnsafeCell::new(new_conn)))
45 }
46
47 pub fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)> {
48 let from = self.0.dup(b"listen")?;
49 let path = from.path()?;
50 let peer_addr = path_to_peer_addr(path.to_str().unwrap_or(""));
51 let count = from.read(buf)?;
52 Ok((count, peer_addr))
53 }
54
55 pub fn recv(&self, buf: &mut [u8]) -> Result<usize> {
56 if let Some(addr) = *self.get_conn() {
57 let from = self.0.dup(format!("{}", addr).as_bytes())?;
58 from.read(buf)
59 } else {
60 Err(Error::new(ErrorKind::Other, "UdpSocket::recv not connected"))
61 }
62 }
63
64 pub fn send_to(&self, buf: &[u8], addr: &SocketAddr) -> Result<usize> {
65 let to = self.0.dup(format!("{}", addr).as_bytes())?;
66 to.write(buf)
67 }
68
69 pub fn send(&self, buf: &[u8]) -> Result<usize> {
70 if let Some(addr) = *self.get_conn() {
71 self.send_to(buf, &addr)
72 } else {
73 Err(Error::new(ErrorKind::Other, "UdpSocket::send not connected"))
74 }
75 }
76
77 pub fn take_error(&self) -> Result<Option<Error>> {
78 Ok(None)
79 }
80
81 pub fn socket_addr(&self) -> Result<SocketAddr> {
82 let path = self.0.path()?;
83 Ok(path_to_local_addr(path.to_str().unwrap_or("")))
84 }
85
86 pub fn broadcast(&self) -> Result<bool> {
87 Err(Error::new(ErrorKind::Other, "UdpSocket::broadcast not implemented"))
88 }
89
90 pub fn multicast_loop_v4(&self) -> Result<bool> {
91 Err(Error::new(ErrorKind::Other, "UdpSocket::multicast_loop_v4 not implemented"))
92 }
93
94 pub fn multicast_loop_v6(&self) -> Result<bool> {
95 Err(Error::new(ErrorKind::Other, "UdpSocket::multicast_loop_v6 not implemented"))
96 }
97
98 pub fn multicast_ttl_v4(&self) -> Result<u32> {
99 Err(Error::new(ErrorKind::Other, "UdpSocket::multicast_ttl_v4 not implemented"))
100 }
101
102 pub fn nonblocking(&self) -> Result<bool> {
103 self.0.fd().nonblocking()
104 }
105
106 pub fn only_v6(&self) -> Result<bool> {
107 Err(Error::new(ErrorKind::Other, "UdpSocket::only_v6 not implemented"))
108 }
109
110 pub fn ttl(&self) -> Result<u32> {
111 Err(Error::new(ErrorKind::Other, "UdpSocket::ttl not implemented"))
112 }
113
114 pub fn read_timeout(&self) -> Result<Option<Duration>> {
115 Err(Error::new(ErrorKind::Other, "UdpSocket::read_timeout not implemented"))
116 }
117
118 pub fn write_timeout(&self) -> Result<Option<Duration>> {
119 Err(Error::new(ErrorKind::Other, "UdpSocket::write_timeout not implemented"))
120 }
121
122 pub fn set_broadcast(&self, _broadcast: bool) -> Result<()> {
123 Err(Error::new(ErrorKind::Other, "UdpSocket::set_broadcast not implemented"))
124 }
125
126 pub fn set_multicast_loop_v4(&self, _multicast_loop_v4: bool) -> Result<()> {
127 Err(Error::new(ErrorKind::Other, "UdpSocket::set_multicast_loop_v4 not implemented"))
128 }
129
130 pub fn set_multicast_loop_v6(&self, _multicast_loop_v6: bool) -> Result<()> {
131 Err(Error::new(ErrorKind::Other, "UdpSocket::set_multicast_loop_v6 not implemented"))
132 }
133
134 pub fn set_multicast_ttl_v4(&self, _multicast_ttl_v4: u32) -> Result<()> {
135 Err(Error::new(ErrorKind::Other, "UdpSocket::set_multicast_ttl_v4 not implemented"))
136 }
137
138 pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()> {
139 self.0.fd().set_nonblocking(nonblocking)
140 }
141
142 pub fn set_only_v6(&self, _only_v6: bool) -> Result<()> {
143 Err(Error::new(ErrorKind::Other, "UdpSocket::set_only_v6 not implemented"))
144 }
145
146 pub fn set_ttl(&self, _ttl: u32) -> Result<()> {
147 Err(Error::new(ErrorKind::Other, "UdpSocket::set_ttl not implemented"))
148 }
149
150 pub fn set_read_timeout(&self, _dur: Option<Duration>) -> Result<()> {
151 Err(Error::new(ErrorKind::Other, "UdpSocket::set_read_timeout not implemented"))
152 }
153
154 pub fn set_write_timeout(&self, _dur: Option<Duration>) -> Result<()> {
155 Err(Error::new(ErrorKind::Other, "UdpSocket::set_write_timeout not implemented"))
156 }
157
158 pub fn join_multicast_v4(&self, _multiaddr: &Ipv4Addr, _interface: &Ipv4Addr) -> Result<()> {
159 Err(Error::new(ErrorKind::Other, "UdpSocket::join_multicast_v4 not implemented"))
160 }
161
162 pub fn join_multicast_v6(&self, _multiaddr: &Ipv6Addr, _interface: u32) -> Result<()> {
163 Err(Error::new(ErrorKind::Other, "UdpSocket::join_multicast_v6 not implemented"))
164 }
165
166 pub fn leave_multicast_v4(&self, _multiaddr: &Ipv4Addr, _interface: &Ipv4Addr) -> Result<()> {
167 Err(Error::new(ErrorKind::Other, "UdpSocket::leave_multicast_v4 not implemented"))
168 }
169
170 pub fn leave_multicast_v6(&self, _multiaddr: &Ipv6Addr, _interface: u32) -> Result<()> {
171 Err(Error::new(ErrorKind::Other, "UdpSocket::leave_multicast_v6 not implemented"))
172 }
173 }