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
use std::net::SocketAddr;
use std::io;
use futures::{Async, Future, Poll, Stream};
use BufDnsStreamHandle;
use DnsStreamHandle;
use error::*;
use udp::UdpStream;
#[must_use = "futures do nothing unless polled"]
pub struct UdpClientStream {
name_server: SocketAddr,
udp_stream: UdpStream,
}
impl UdpClientStream {
pub fn new<E>(
name_server: SocketAddr,
) -> (
Box<Future<Item = UdpClientStream, Error = io::Error> + Send>,
Box<DnsStreamHandle<Error = E> + Send>,
)
where
E: FromProtoError + Send + 'static,
{
let (stream_future, sender) = UdpStream::new(name_server);
let new_future =
Box::new(stream_future.map(move |udp_stream| {
UdpClientStream {
name_server: name_server,
udp_stream: udp_stream,
}
}));
let sender = Box::new(BufDnsStreamHandle::new(name_server, sender));
(new_future, sender)
}
}
impl Stream for UdpClientStream {
type Item = Vec<u8>;
type Error = io::Error;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
match try_ready!(self.udp_stream.poll()) {
Some((buffer, src_addr)) => {
if src_addr != self.name_server {
debug!(
"{} does not match name_server: {}",
src_addr,
self.name_server
)
}
Ok(Async::Ready(Some(buffer)))
}
None => Ok(Async::Ready(None)),
}
}
}
#[cfg(test)]
use std::net::{IpAddr, Ipv4Addr};
#[cfg(not(target_os = "linux"))]
#[cfg(test)]
use std::net::Ipv6Addr;
#[test]
fn test_udp_client_stream_ipv4() {
udp_client_stream_test(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)))
}
#[test]
#[cfg(not(target_os = "linux"))]
fn test_udp_client_stream_ipv6() {
udp_client_stream_test(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)))
}
#[cfg(test)]
fn udp_client_stream_test(server_addr: IpAddr) {
use tokio::runtime::current_thread::Runtime;
use std;
let succeeded = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let succeeded_clone = succeeded.clone();
std::thread::Builder::new()
.name("thread_killer".to_string())
.spawn(move || {
let succeeded = succeeded_clone.clone();
for _ in 0..15 {
std::thread::sleep(std::time::Duration::from_secs(1));
if succeeded.load(std::sync::atomic::Ordering::Relaxed) {
return;
}
}
panic!("timeout");
})
.unwrap();
let server = std::net::UdpSocket::bind(SocketAddr::new(server_addr, 0)).unwrap();
server
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.unwrap();
server
.set_write_timeout(Some(std::time::Duration::from_secs(5)))
.unwrap();
let server_addr = server.local_addr().unwrap();
let test_bytes: &'static [u8; 8] = b"DEADBEEF";
let send_recv_times = 4;
let server_handle = std::thread::Builder::new()
.name("test_udp_client_stream_ipv4:server".to_string())
.spawn(move || {
let mut buffer = [0_u8; 512];
for _ in 0..send_recv_times {
let (len, addr) = server.recv_from(&mut buffer).expect("receive failed");
assert_eq!(&buffer[0..len], test_bytes);
assert_eq!(
server.send_to(&buffer[0..len], addr).expect("send failed"),
len
);
}
})
.unwrap();
let mut io_loop = Runtime::new().unwrap();
let (stream, mut sender) = UdpClientStream::new::<ProtoError>(server_addr);
let mut stream: UdpClientStream = io_loop.block_on(stream).ok().unwrap();
for _ in 0..send_recv_times {
sender.send(test_bytes.to_vec()).unwrap();
let (buffer, stream_tmp) = io_loop.block_on(stream.into_future()).ok().unwrap();
stream = stream_tmp;
assert_eq!(&buffer.expect("no buffer received"), test_bytes);
}
succeeded.store(true, std::sync::atomic::Ordering::Relaxed);
server_handle.join().expect("server thread failed");
}