/
udp.rs
599 lines (529 loc) · 18.7 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Copyright 2013 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.
//! UDP (User Datagram Protocol) network connections.
//!
//! This module contains the ability to open a UDP stream to a socket address.
//! The destination and binding addresses can either be an IPv4 or IPv6
//! address. There is no corresponding notion of a server because UDP is a
//! datagram protocol.
use clone::Clone;
use io::net::ip::{SocketAddr, IpAddr};
use io::{Reader, Writer, IoResult, IoError};
use kinds::Send;
use owned::Box;
use option::Option;
use result::{Ok, Err};
use rt::rtio::{RtioSocket, RtioUdpSocket, IoFactory, LocalIo};
use rt::rtio;
/// A User Datagram Protocol socket.
///
/// This is an implementation of a bound UDP socket. This supports both IPv4 and
/// IPv6 addresses, and there is no corresponding notion of a server because UDP
/// is a datagram protocol.
///
/// # Example
///
/// ```rust,no_run
/// # #![allow(unused_must_use)]
/// use std::io::net::udp::UdpSocket;
/// use std::io::net::ip::{Ipv4Addr, SocketAddr};
///
/// let addr = SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 34254 };
/// let mut socket = match UdpSocket::bind(addr) {
/// Ok(s) => s,
/// Err(e) => fail!("couldn't bind socket: {}", e),
/// };
///
/// let mut buf = [0, ..10];
/// match socket.recv_from(buf) {
/// Ok((amt, src)) => {
/// // Send a reply to the socket we received data from
/// let buf = buf.mut_slice_to(amt);
/// buf.reverse();
/// socket.send_to(buf, src);
/// }
/// Err(e) => println!("couldn't receive a datagram: {}", e)
/// }
/// drop(socket); // close the socket
/// ```
pub struct UdpSocket {
obj: Box<RtioUdpSocket + Send>,
}
impl UdpSocket {
/// Creates a UDP socket from the given socket address.
pub fn bind(addr: SocketAddr) -> IoResult<UdpSocket> {
let SocketAddr { ip, port } = addr;
LocalIo::maybe_raise(|io| {
let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };
io.udp_bind(addr).map(|s| UdpSocket { obj: s })
}).map_err(IoError::from_rtio_error)
}
/// Receives data from the socket. On success, returns the number of bytes
/// read and the address from whence the data came.
pub fn recv_from(&mut self, buf: &mut [u8])
-> IoResult<(uint, SocketAddr)> {
match self.obj.recv_from(buf) {
Ok((amt, rtio::SocketAddr { ip, port })) => {
Ok((amt, SocketAddr { ip: super::from_rtio(ip), port: port }))
}
Err(e) => Err(IoError::from_rtio_error(e)),
}
}
#[allow(missing_doc)]
#[deprecated = "renamed to `recv_from`"]
pub fn recvfrom(&mut self, buf: &mut [u8])
-> IoResult<(uint, SocketAddr)> {
self.recv_from(buf)
}
/// Sends data on the socket to the given address. Returns nothing on
/// success.
pub fn send_to(&mut self, buf: &[u8], dst: SocketAddr) -> IoResult<()> {
self.obj.send_to(buf, rtio::SocketAddr {
ip: super::to_rtio(dst.ip),
port: dst.port,
}).map_err(IoError::from_rtio_error)
}
#[allow(missing_doc)]
#[deprecated = "renamed to `send_to`"]
pub fn sendto(&mut self, buf: &[u8], dst: SocketAddr) -> IoResult<()> {
self.send_to(buf, dst)
}
/// Creates a `UdpStream`, which allows use of the `Reader` and `Writer`
/// traits to receive and send data from the same address. This transfers
/// ownership of the socket to the stream.
///
/// Note that this call does not perform any actual network communication,
/// because UDP is a datagram protocol.
pub fn connect(self, other: SocketAddr) -> UdpStream {
UdpStream {
socket: self,
connected_to: other,
}
}
/// Returns the socket address that this socket was created from.
pub fn socket_name(&mut self) -> IoResult<SocketAddr> {
match self.obj.socket_name() {
Ok(a) => Ok(SocketAddr { ip: super::from_rtio(a.ip), port: a.port }),
Err(e) => Err(IoError::from_rtio_error(e))
}
}
/// Joins a multicast IP address (becomes a member of it)
#[experimental]
pub fn join_multicast(&mut self, multi: IpAddr) -> IoResult<()> {
let e = self.obj.join_multicast(super::to_rtio(multi));
e.map_err(IoError::from_rtio_error)
}
/// Leaves a multicast IP address (drops membership from it)
#[experimental]
pub fn leave_multicast(&mut self, multi: IpAddr) -> IoResult<()> {
let e = self.obj.leave_multicast(super::to_rtio(multi));
e.map_err(IoError::from_rtio_error)
}
/// Set the multicast loop flag to the specified value
///
/// This lets multicast packets loop back to local sockets (if enabled)
#[experimental]
pub fn set_multicast_loop(&mut self, on: bool) -> IoResult<()> {
if on {
self.obj.loop_multicast_locally()
} else {
self.obj.dont_loop_multicast_locally()
}.map_err(IoError::from_rtio_error)
}
/// Sets the multicast TTL
#[experimental]
pub fn set_multicast_ttl(&mut self, ttl: int) -> IoResult<()> {
self.obj.multicast_time_to_live(ttl).map_err(IoError::from_rtio_error)
}
/// Sets this socket's TTL
#[experimental]
pub fn set_ttl(&mut self, ttl: int) -> IoResult<()> {
self.obj.time_to_live(ttl).map_err(IoError::from_rtio_error)
}
/// Sets the broadcast flag on or off
#[experimental]
pub fn set_broadcast(&mut self, broadcast: bool) -> IoResult<()> {
if broadcast {
self.obj.hear_broadcasts()
} else {
self.obj.ignore_broadcasts()
}.map_err(IoError::from_rtio_error)
}
/// Sets the broadcast flag on or off
#[deprecated="renamed to `set_broadcast`"]
pub fn set_broadast(&mut self, broadcast: bool) -> IoResult<()> {
self.set_broadcast(broadcast)
}
/// Sets the read/write timeout for this socket.
///
/// For more information, see `TcpStream::set_timeout`
#[experimental = "the timeout argument may change in type and value"]
pub fn set_timeout(&mut self, timeout_ms: Option<u64>) {
self.obj.set_timeout(timeout_ms)
}
/// Sets the read timeout for this socket.
///
/// For more information, see `TcpStream::set_timeout`
#[experimental = "the timeout argument may change in type and value"]
pub fn set_read_timeout(&mut self, timeout_ms: Option<u64>) {
self.obj.set_read_timeout(timeout_ms)
}
/// Sets the write timeout for this socket.
///
/// For more information, see `TcpStream::set_timeout`
#[experimental = "the timeout argument may change in type and value"]
pub fn set_write_timeout(&mut self, timeout_ms: Option<u64>) {
self.obj.set_write_timeout(timeout_ms)
}
}
impl Clone for UdpSocket {
/// Creates a new handle to this UDP socket, allowing for simultaneous
/// reads and writes of the socket.
///
/// The underlying UDP socket will not be closed until all handles to the
/// socket have been deallocated. Two concurrent reads will not receive
/// the same data. Instead, the first read will receive the first packet
/// received, and the second read will receive the second packet.
fn clone(&self) -> UdpSocket {
UdpSocket {
obj: self.obj.clone(),
}
}
}
/// A type that allows convenient usage of a UDP stream connected to one
/// address via the `Reader` and `Writer` traits.
pub struct UdpStream {
socket: UdpSocket,
connected_to: SocketAddr
}
impl UdpStream {
/// Allows access to the underlying UDP socket owned by this stream. This
/// is useful to, for example, use the socket to send data to hosts other
/// than the one that this stream is connected to.
pub fn as_socket<T>(&mut self, f: |&mut UdpSocket| -> T) -> T {
f(&mut self.socket)
}
/// Consumes this UDP stream and returns out the underlying socket.
pub fn disconnect(self) -> UdpSocket {
self.socket
}
}
impl Reader for UdpStream {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
let peer = self.connected_to;
self.as_socket(|sock| {
match sock.recv_from(buf) {
Ok((_nread, src)) if src != peer => Ok(0),
Ok((nread, _src)) => Ok(nread),
Err(e) => Err(e),
}
})
}
}
impl Writer for UdpStream {
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
let connected_to = self.connected_to;
self.as_socket(|sock| sock.send_to(buf, connected_to))
}
}
#[cfg(test)]
#[allow(experimental)]
mod test {
use super::*;
use io::net::ip::{SocketAddr};
// FIXME #11530 this fails on android because tests are run as root
iotest!(fn bind_error() {
let addr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 1 };
match UdpSocket::bind(addr) {
Ok(..) => fail!(),
Err(e) => assert_eq!(e.kind, PermissionDenied),
}
} #[ignore(cfg(windows))] #[ignore(cfg(target_os = "android"))])
iotest!(fn socket_smoke_test_ip4() {
let server_ip = next_test_ip4();
let client_ip = next_test_ip4();
let (tx1, rx1) = channel();
let (tx2, rx2) = channel();
spawn(proc() {
match UdpSocket::bind(client_ip) {
Ok(ref mut client) => {
rx1.recv();
client.send_to([99], server_ip).unwrap()
}
Err(..) => fail!()
}
tx2.send(());
});
match UdpSocket::bind(server_ip) {
Ok(ref mut server) => {
tx1.send(());
let mut buf = [0];
match server.recv_from(buf) {
Ok((nread, src)) => {
assert_eq!(nread, 1);
assert_eq!(buf[0], 99);
assert_eq!(src, client_ip);
}
Err(..) => fail!()
}
}
Err(..) => fail!()
}
rx2.recv();
})
iotest!(fn socket_smoke_test_ip6() {
let server_ip = next_test_ip6();
let client_ip = next_test_ip6();
let (tx, rx) = channel::<()>();
spawn(proc() {
match UdpSocket::bind(client_ip) {
Ok(ref mut client) => {
rx.recv();
client.send_to([99], server_ip).unwrap()
}
Err(..) => fail!()
}
});
match UdpSocket::bind(server_ip) {
Ok(ref mut server) => {
tx.send(());
let mut buf = [0];
match server.recv_from(buf) {
Ok((nread, src)) => {
assert_eq!(nread, 1);
assert_eq!(buf[0], 99);
assert_eq!(src, client_ip);
}
Err(..) => fail!()
}
}
Err(..) => fail!()
}
})
iotest!(fn stream_smoke_test_ip4() {
let server_ip = next_test_ip4();
let client_ip = next_test_ip4();
let (tx1, rx1) = channel();
let (tx2, rx2) = channel();
spawn(proc() {
match UdpSocket::bind(client_ip) {
Ok(client) => {
let client = box client;
let mut stream = client.connect(server_ip);
rx1.recv();
stream.write([99]).unwrap();
}
Err(..) => fail!()
}
tx2.send(());
});
match UdpSocket::bind(server_ip) {
Ok(server) => {
let server = box server;
let mut stream = server.connect(client_ip);
tx1.send(());
let mut buf = [0];
match stream.read(buf) {
Ok(nread) => {
assert_eq!(nread, 1);
assert_eq!(buf[0], 99);
}
Err(..) => fail!()
}
}
Err(..) => fail!()
}
rx2.recv();
})
iotest!(fn stream_smoke_test_ip6() {
let server_ip = next_test_ip6();
let client_ip = next_test_ip6();
let (tx1, rx1) = channel();
let (tx2, rx2) = channel();
spawn(proc() {
match UdpSocket::bind(client_ip) {
Ok(client) => {
let client = box client;
let mut stream = client.connect(server_ip);
rx1.recv();
stream.write([99]).unwrap();
}
Err(..) => fail!()
}
tx2.send(());
});
match UdpSocket::bind(server_ip) {
Ok(server) => {
let server = box server;
let mut stream = server.connect(client_ip);
tx1.send(());
let mut buf = [0];
match stream.read(buf) {
Ok(nread) => {
assert_eq!(nread, 1);
assert_eq!(buf[0], 99);
}
Err(..) => fail!()
}
}
Err(..) => fail!()
}
rx2.recv();
})
pub fn socket_name(addr: SocketAddr) {
let server = UdpSocket::bind(addr);
assert!(server.is_ok());
let mut server = server.unwrap();
// Make sure socket_name gives
// us the socket we binded to.
let so_name = server.socket_name();
assert!(so_name.is_ok());
assert_eq!(addr, so_name.unwrap());
}
iotest!(fn socket_name_ip4() {
socket_name(next_test_ip4());
})
iotest!(fn socket_name_ip6() {
socket_name(next_test_ip6());
})
iotest!(fn udp_clone_smoke() {
let addr1 = next_test_ip4();
let addr2 = next_test_ip4();
let mut sock1 = UdpSocket::bind(addr1).unwrap();
let sock2 = UdpSocket::bind(addr2).unwrap();
spawn(proc() {
let mut sock2 = sock2;
let mut buf = [0, 0];
assert_eq!(sock2.recv_from(buf), Ok((1, addr1)));
assert_eq!(buf[0], 1);
sock2.send_to([2], addr1).unwrap();
});
let sock3 = sock1.clone();
let (tx1, rx1) = channel();
let (tx2, rx2) = channel();
spawn(proc() {
let mut sock3 = sock3;
rx1.recv();
sock3.send_to([1], addr2).unwrap();
tx2.send(());
});
tx1.send(());
let mut buf = [0, 0];
assert_eq!(sock1.recv_from(buf), Ok((1, addr2)));
rx2.recv();
})
iotest!(fn udp_clone_two_read() {
let addr1 = next_test_ip4();
let addr2 = next_test_ip4();
let mut sock1 = UdpSocket::bind(addr1).unwrap();
let sock2 = UdpSocket::bind(addr2).unwrap();
let (tx1, rx) = channel();
let tx2 = tx1.clone();
spawn(proc() {
let mut sock2 = sock2;
sock2.send_to([1], addr1).unwrap();
rx.recv();
sock2.send_to([2], addr1).unwrap();
rx.recv();
});
let sock3 = sock1.clone();
let (done, rx) = channel();
spawn(proc() {
let mut sock3 = sock3;
let mut buf = [0, 0];
sock3.recv_from(buf).unwrap();
tx2.send(());
done.send(());
});
let mut buf = [0, 0];
sock1.recv_from(buf).unwrap();
tx1.send(());
rx.recv();
})
iotest!(fn udp_clone_two_write() {
let addr1 = next_test_ip4();
let addr2 = next_test_ip4();
let mut sock1 = UdpSocket::bind(addr1).unwrap();
let sock2 = UdpSocket::bind(addr2).unwrap();
let (tx, rx) = channel();
let (serv_tx, serv_rx) = channel();
spawn(proc() {
let mut sock2 = sock2;
let mut buf = [0, 1];
rx.recv();
match sock2.recv_from(buf) {
Ok(..) => {}
Err(e) => fail!("failed receive: {}", e),
}
serv_tx.send(());
});
let sock3 = sock1.clone();
let (done, rx) = channel();
let tx2 = tx.clone();
spawn(proc() {
let mut sock3 = sock3;
match sock3.send_to([1], addr2) {
Ok(..) => { let _ = tx2.send_opt(()); }
Err(..) => {}
}
done.send(());
});
match sock1.send_to([2], addr2) {
Ok(..) => { let _ = tx.send_opt(()); }
Err(..) => {}
}
drop(tx);
rx.recv();
serv_rx.recv();
})
iotest!(fn recv_from_timeout() {
let addr1 = next_test_ip4();
let addr2 = next_test_ip4();
let mut a = UdpSocket::bind(addr1).unwrap();
let (tx, rx) = channel();
let (tx2, rx2) = channel();
spawn(proc() {
let mut a = UdpSocket::bind(addr2).unwrap();
assert_eq!(a.recv_from([0]), Ok((1, addr1)));
assert_eq!(a.send_to([0], addr1), Ok(()));
rx.recv();
assert_eq!(a.send_to([0], addr1), Ok(()));
tx2.send(());
});
// Make sure that reads time out, but writes can continue
a.set_read_timeout(Some(20));
assert_eq!(a.recv_from([0]).err().unwrap().kind, TimedOut);
assert_eq!(a.recv_from([0]).err().unwrap().kind, TimedOut);
assert_eq!(a.send_to([0], addr2), Ok(()));
// Cloned handles should be able to block
let mut a2 = a.clone();
assert_eq!(a2.recv_from([0]), Ok((1, addr2)));
// Clearing the timeout should allow for receiving
a.set_timeout(None);
tx.send(());
assert_eq!(a2.recv_from([0]), Ok((1, addr2)));
// Make sure the child didn't die
rx2.recv();
})
iotest!(fn send_to_timeout() {
let addr1 = next_test_ip4();
let addr2 = next_test_ip4();
let mut a = UdpSocket::bind(addr1).unwrap();
let _b = UdpSocket::bind(addr2).unwrap();
a.set_write_timeout(Some(1000));
for _ in range(0u, 100) {
match a.send_to([0, ..4*1024], addr2) {
Ok(()) | Err(IoError { kind: ShortWrite(..), .. }) => {},
Err(IoError { kind: TimedOut, .. }) => break,
Err(e) => fail!("other error: {}", e),
}
}
})
}