forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp.rs
237 lines (197 loc) · 8 KB
/
udp.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::cell::UnsafeCell;
use crate::cmp;
use crate::io::{self, Error, ErrorKind, Result};
use crate::mem;
use crate::net::{SocketAddr, Ipv4Addr, Ipv6Addr};
use crate::path::Path;
use crate::sys::fs::{File, OpenOptions};
use crate::sys::syscall::TimeSpec;
use crate::sys_common::{AsInner, FromInner, IntoInner};
use crate::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: Result<&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: Result<&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(addr.to_string().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 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, "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 }
}