tokio_core/net/udp/mod.rs
1use std::io;
2use std::net::{self, SocketAddr, Ipv4Addr, Ipv6Addr};
3use std::fmt;
4
5use futures::{Async, Future, Poll};
6use mio;
7
8use reactor::{Handle, PollEvented2};
9
10/// An I/O object representing a UDP socket.
11pub struct UdpSocket {
12 io: PollEvented2<mio::net::UdpSocket>,
13}
14
15mod frame;
16pub use self::frame::{UdpFramed, UdpCodec};
17
18impl UdpSocket {
19 /// Create a new UDP socket bound to the specified address.
20 ///
21 /// This function will create a new UDP socket and attempt to bind it to the
22 /// `addr` provided. If the result is `Ok`, the socket has successfully bound.
23 pub fn bind(addr: &SocketAddr, handle: &Handle) -> io::Result<UdpSocket> {
24 let udp = try!(mio::net::UdpSocket::bind(addr));
25 UdpSocket::new(udp, handle)
26 }
27
28 fn new(socket: mio::net::UdpSocket, handle: &Handle) -> io::Result<UdpSocket> {
29 let io = try!(PollEvented2::new_with_handle(socket, handle.new_tokio_handle()));
30 Ok(UdpSocket { io: io })
31 }
32
33 /// Creates a new `UdpSocket` from the previously bound socket provided.
34 ///
35 /// The socket given will be registered with the event loop that `handle` is
36 /// associated with. This function requires that `socket` has previously
37 /// been bound to an address to work correctly.
38 ///
39 /// This can be used in conjunction with net2's `UdpBuilder` interface to
40 /// configure a socket before it's handed off, such as setting options like
41 /// `reuse_address` or binding to multiple addresses.
42 pub fn from_socket(socket: net::UdpSocket,
43 handle: &Handle) -> io::Result<UdpSocket> {
44 let udp = try!(mio::net::UdpSocket::from_socket(socket));
45 UdpSocket::new(udp, handle)
46 }
47
48 /// Provides a `Stream` and `Sink` interface for reading and writing to this
49 /// `UdpSocket` object, using the provided `UdpCodec` to read and write the
50 /// raw data.
51 ///
52 /// Raw UDP sockets work with datagrams, but higher-level code usually
53 /// wants to batch these into meaningful chunks, called "frames". This
54 /// method layers framing on top of this socket by using the `UdpCodec`
55 /// trait to handle encoding and decoding of messages frames. Note that
56 /// the incoming and outgoing frame types may be distinct.
57 ///
58 /// This function returns a *single* object that is both `Stream` and
59 /// `Sink`; grouping this into a single object is often useful for layering
60 /// things which require both read and write access to the underlying
61 /// object.
62 ///
63 /// If you want to work more directly with the streams and sink, consider
64 /// calling `split` on the `UdpFramed` returned by this method, which will
65 /// break them into separate objects, allowing them to interact more
66 /// easily.
67 pub fn framed<C: UdpCodec>(self, codec: C) -> UdpFramed<C> {
68 frame::new(self, codec)
69 }
70
71 /// Returns the local address that this stream is bound to.
72 pub fn local_addr(&self) -> io::Result<SocketAddr> {
73 self.io.get_ref().local_addr()
74 }
75
76 /// Connects the UDP socket setting the default destination for send() and
77 /// limiting packets that are read via recv from the address specified in addr.
78 pub fn connect(&self, addr: &SocketAddr) -> io::Result<()> {
79 self.io.get_ref().connect(*addr)
80 }
81
82 /// Sends data on the socket to the address previously bound via connect().
83 /// On success, returns the number of bytes written.
84 pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
85 if let Async::NotReady = self.io.poll_write_ready()? {
86 return Err(io::ErrorKind::WouldBlock.into())
87 }
88 match self.io.get_ref().send(buf) {
89 Ok(n) => Ok(n),
90 Err(e) => {
91 if e.kind() == io::ErrorKind::WouldBlock {
92 self.io.clear_write_ready()?;
93 }
94 Err(e)
95 }
96 }
97 }
98
99 /// Receives data from the socket previously bound with connect().
100 /// On success, returns the number of bytes read.
101 pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
102 if let Async::NotReady = self.io.poll_read_ready(mio::Ready::readable())? {
103 return Err(io::ErrorKind::WouldBlock.into())
104 }
105 match self.io.get_ref().recv(buf) {
106 Ok(n) => Ok(n),
107 Err(e) => {
108 if e.kind() == io::ErrorKind::WouldBlock {
109 self.io.clear_read_ready(mio::Ready::readable())?;
110 }
111 Err(e)
112 }
113 }
114 }
115
116 /// Test whether this socket is ready to be read or not.
117 ///
118 /// If the socket is *not* readable then the current task is scheduled to
119 /// get a notification when the socket does become readable. That is, this
120 /// is only suitable for calling in a `Future::poll` method and will
121 /// automatically handle ensuring a retry once the socket is readable again.
122 pub fn poll_read(&self) -> Async<()> {
123 self.io.poll_read_ready(mio::Ready::readable())
124 .map(|r| {
125 if r.is_ready() {
126 Async::Ready(())
127 } else {
128 Async::NotReady
129 }
130 })
131 .unwrap_or(().into())
132 }
133
134 /// Test whether this socket is ready to be written to or not.
135 ///
136 /// If the socket is *not* writable then the current task is scheduled to
137 /// get a notification when the socket does become writable. That is, this
138 /// is only suitable for calling in a `Future::poll` method and will
139 /// automatically handle ensuring a retry once the socket is writable again.
140 pub fn poll_write(&self) -> Async<()> {
141 self.io.poll_write_ready()
142 .map(|r| {
143 if r.is_ready() {
144 Async::Ready(())
145 } else {
146 Async::NotReady
147 }
148 })
149 .unwrap_or(().into())
150 }
151
152 /// Sends data on the socket to the given address. On success, returns the
153 /// number of bytes written.
154 ///
155 /// Address type can be any implementer of `ToSocketAddrs` trait. See its
156 /// documentation for concrete examples.
157 pub fn send_to(&self, buf: &[u8], target: &SocketAddr) -> io::Result<usize> {
158 if let Async::NotReady = self.io.poll_write_ready()? {
159 return Err(io::ErrorKind::WouldBlock.into())
160 }
161 match self.io.get_ref().send_to(buf, target) {
162 Ok(n) => Ok(n),
163 Err(e) => {
164 if e.kind() == io::ErrorKind::WouldBlock {
165 self.io.clear_write_ready()?;
166 }
167 Err(e)
168 }
169 }
170 }
171
172 /// Creates a future that will write the entire contents of the buffer
173 /// `buf` provided as a datagram to this socket.
174 ///
175 /// The returned future will return after data has been written to the
176 /// outbound socket. The future will resolve to the stream as well as the
177 /// buffer (for reuse if needed).
178 ///
179 /// Any error which happens during writing will cause both the stream and
180 /// the buffer to get destroyed. Note that failure to write the entire
181 /// buffer is considered an error for the purposes of sending a datagram.
182 ///
183 /// The `buf` parameter here only requires the `AsRef<[u8]>` trait, which
184 /// should be broadly applicable to accepting data which can be converted
185 /// to a slice. The `Window` struct is also available in this crate to
186 /// provide a different window into a slice if necessary.
187 pub fn send_dgram<T>(self, buf: T, addr: SocketAddr) -> SendDgram<T>
188 where T: AsRef<[u8]>,
189 {
190 SendDgram(Some((self, buf, addr)))
191 }
192
193 /// Receives data from the socket. On success, returns the number of bytes
194 /// read and the address from whence the data came.
195 pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
196 if let Async::NotReady = self.io.poll_read_ready(mio::Ready::readable())? {
197 return Err(io::ErrorKind::WouldBlock.into())
198 }
199 match self.io.get_ref().recv_from(buf) {
200 Ok(n) => Ok(n),
201 Err(e) => {
202 if e.kind() == io::ErrorKind::WouldBlock {
203 self.io.clear_read_ready(mio::Ready::readable())?;
204 }
205 Err(e)
206 }
207 }
208 }
209
210 /// Creates a future that receive a datagram to be written to the buffer
211 /// provided.
212 ///
213 /// The returned future will return after a datagram has been received on
214 /// this socket. The future will resolve to the socket, the buffer, the
215 /// amount of data read, and the address the data was received from.
216 ///
217 /// An error during reading will cause the socket and buffer to get
218 /// destroyed and the socket will be returned.
219 ///
220 /// The `buf` parameter here only requires the `AsMut<[u8]>` trait, which
221 /// should be broadly applicable to accepting data which can be converted
222 /// to a slice. The `Window` struct is also available in this crate to
223 /// provide a different window into a slice if necessary.
224 pub fn recv_dgram<T>(self, buf: T) -> RecvDgram<T>
225 where T: AsMut<[u8]>,
226 {
227 RecvDgram(Some((self, buf)))
228 }
229
230 /// Gets the value of the `SO_BROADCAST` option for this socket.
231 ///
232 /// For more information about this option, see
233 /// [`set_broadcast`][link].
234 ///
235 /// [link]: #method.set_broadcast
236 pub fn broadcast(&self) -> io::Result<bool> {
237 self.io.get_ref().broadcast()
238 }
239
240 /// Sets the value of the `SO_BROADCAST` option for this socket.
241 ///
242 /// When enabled, this socket is allowed to send packets to a broadcast
243 /// address.
244 pub fn set_broadcast(&self, on: bool) -> io::Result<()> {
245 self.io.get_ref().set_broadcast(on)
246 }
247
248 /// Gets the value of the `IP_MULTICAST_LOOP` option for this socket.
249 ///
250 /// For more information about this option, see
251 /// [`set_multicast_loop_v4`][link].
252 ///
253 /// [link]: #method.set_multicast_loop_v4
254 pub fn multicast_loop_v4(&self) -> io::Result<bool> {
255 self.io.get_ref().multicast_loop_v4()
256 }
257
258 /// Sets the value of the `IP_MULTICAST_LOOP` option for this socket.
259 ///
260 /// If enabled, multicast packets will be looped back to the local socket.
261 /// Note that this may not have any affect on IPv6 sockets.
262 pub fn set_multicast_loop_v4(&self, on: bool) -> io::Result<()> {
263 self.io.get_ref().set_multicast_loop_v4(on)
264 }
265
266 /// Gets the value of the `IP_MULTICAST_TTL` option for this socket.
267 ///
268 /// For more information about this option, see
269 /// [`set_multicast_ttl_v4`][link].
270 ///
271 /// [link]: #method.set_multicast_ttl_v4
272 pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
273 self.io.get_ref().multicast_ttl_v4()
274 }
275
276 /// Sets the value of the `IP_MULTICAST_TTL` option for this socket.
277 ///
278 /// Indicates the time-to-live value of outgoing multicast packets for
279 /// this socket. The default value is 1 which means that multicast packets
280 /// don't leave the local network unless explicitly requested.
281 ///
282 /// Note that this may not have any affect on IPv6 sockets.
283 pub fn set_multicast_ttl_v4(&self, ttl: u32) -> io::Result<()> {
284 self.io.get_ref().set_multicast_ttl_v4(ttl)
285 }
286
287 /// Gets the value of the `IPV6_MULTICAST_LOOP` option for this socket.
288 ///
289 /// For more information about this option, see
290 /// [`set_multicast_loop_v6`][link].
291 ///
292 /// [link]: #method.set_multicast_loop_v6
293 pub fn multicast_loop_v6(&self) -> io::Result<bool> {
294 self.io.get_ref().multicast_loop_v6()
295 }
296
297 /// Sets the value of the `IPV6_MULTICAST_LOOP` option for this socket.
298 ///
299 /// Controls whether this socket sees the multicast packets it sends itself.
300 /// Note that this may not have any affect on IPv4 sockets.
301 pub fn set_multicast_loop_v6(&self, on: bool) -> io::Result<()> {
302 self.io.get_ref().set_multicast_loop_v6(on)
303 }
304
305 /// Gets the value of the `IP_TTL` option for this socket.
306 ///
307 /// For more information about this option, see [`set_ttl`][link].
308 ///
309 /// [link]: #method.set_ttl
310 pub fn ttl(&self) -> io::Result<u32> {
311 self.io.get_ref().ttl()
312 }
313
314 /// Sets the value for the `IP_TTL` option on this socket.
315 ///
316 /// This value sets the time-to-live field that is used in every packet sent
317 /// from this socket.
318 pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
319 self.io.get_ref().set_ttl(ttl)
320 }
321
322 /// Executes an operation of the `IP_ADD_MEMBERSHIP` type.
323 ///
324 /// This function specifies a new multicast group for this socket to join.
325 /// The address must be a valid multicast address, and `interface` is the
326 /// address of the local interface with which the system should join the
327 /// multicast group. If it's equal to `INADDR_ANY` then an appropriate
328 /// interface is chosen by the system.
329 pub fn join_multicast_v4(&self,
330 multiaddr: &Ipv4Addr,
331 interface: &Ipv4Addr) -> io::Result<()> {
332 self.io.get_ref().join_multicast_v4(multiaddr, interface)
333 }
334
335 /// Executes an operation of the `IPV6_ADD_MEMBERSHIP` type.
336 ///
337 /// This function specifies a new multicast group for this socket to join.
338 /// The address must be a valid multicast address, and `interface` is the
339 /// index of the interface to join/leave (or 0 to indicate any interface).
340 pub fn join_multicast_v6(&self,
341 multiaddr: &Ipv6Addr,
342 interface: u32) -> io::Result<()> {
343 self.io.get_ref().join_multicast_v6(multiaddr, interface)
344 }
345
346 /// Executes an operation of the `IP_DROP_MEMBERSHIP` type.
347 ///
348 /// For more information about this option, see
349 /// [`join_multicast_v4`][link].
350 ///
351 /// [link]: #method.join_multicast_v4
352 pub fn leave_multicast_v4(&self,
353 multiaddr: &Ipv4Addr,
354 interface: &Ipv4Addr) -> io::Result<()> {
355 self.io.get_ref().leave_multicast_v4(multiaddr, interface)
356 }
357
358 /// Executes an operation of the `IPV6_DROP_MEMBERSHIP` type.
359 ///
360 /// For more information about this option, see
361 /// [`join_multicast_v6`][link].
362 ///
363 /// [link]: #method.join_multicast_v6
364 pub fn leave_multicast_v6(&self,
365 multiaddr: &Ipv6Addr,
366 interface: u32) -> io::Result<()> {
367 self.io.get_ref().leave_multicast_v6(multiaddr, interface)
368 }
369
370 /// Sets the value for the `IPV6_V6ONLY` option on this socket.
371 ///
372 /// If this is set to `true` then the socket is restricted to sending and
373 /// receiving IPv6 packets only. In this case two IPv4 and IPv6 applications
374 /// can bind the same port at the same time.
375 ///
376 /// If this is set to `false` then the socket can be used to send and
377 /// receive packets from an IPv4-mapped IPv6 address.
378 pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
379 self.io.get_ref().set_only_v6(only_v6)
380 }
381
382 /// Gets the value of the `IPV6_V6ONLY` option for this socket.
383 ///
384 /// For more information about this option, see [`set_only_v6`][link].
385 ///
386 /// [link]: #method.set_only_v6
387 pub fn only_v6(&self) -> io::Result<bool> {
388 self.io.get_ref().only_v6()
389 }
390}
391
392impl fmt::Debug for UdpSocket {
393 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
394 self.io.get_ref().fmt(f)
395 }
396}
397
398/// A future used to write the entire contents of some data to a UDP socket.
399///
400/// This is created by the `UdpSocket::send_dgram` method.
401#[must_use = "futures do nothing unless polled"]
402pub struct SendDgram<T>(Option<(UdpSocket, T, SocketAddr)>);
403
404fn incomplete_write(reason: &str) -> io::Error {
405 io::Error::new(io::ErrorKind::Other, reason)
406}
407
408impl<T> Future for SendDgram<T>
409 where T: AsRef<[u8]>,
410{
411 type Item = (UdpSocket, T);
412 type Error = io::Error;
413
414 fn poll(&mut self) -> Poll<(UdpSocket, T), io::Error> {
415 {
416 let (ref sock, ref buf, ref addr) =
417 *self.0.as_ref().expect("SendDgram polled after completion");
418 let n = try_nb!(sock.send_to(buf.as_ref(), addr));
419 if n != buf.as_ref().len() {
420 return Err(incomplete_write("failed to send entire message \
421 in datagram"))
422 }
423 }
424
425 let (sock, buf, _addr) = self.0.take().unwrap();
426 Ok(Async::Ready((sock, buf)))
427 }
428}
429
430/// A future used to receive a datagram from a UDP socket.
431///
432/// This is created by the `UdpSocket::recv_dgram` method.
433#[must_use = "futures do nothing unless polled"]
434pub struct RecvDgram<T>(Option<(UdpSocket, T)>);
435
436impl<T> Future for RecvDgram<T>
437 where T: AsMut<[u8]>,
438{
439 type Item = (UdpSocket, T, usize, SocketAddr);
440 type Error = io::Error;
441
442 fn poll(&mut self) -> Poll<Self::Item, io::Error> {
443 let (n, addr) = {
444 let (ref socket, ref mut buf) =
445 *self.0.as_mut().expect("RecvDgram polled after completion");
446
447 try_nb!(socket.recv_from(buf.as_mut()))
448 };
449
450 let (socket, buf) = self.0.take().unwrap();
451 Ok(Async::Ready((socket, buf, n, addr)))
452 }
453}
454
455#[cfg(all(unix, not(target_os = "fuchsia")))]
456mod sys {
457 use std::os::unix::prelude::*;
458 use super::UdpSocket;
459
460 impl AsRawFd for UdpSocket {
461 fn as_raw_fd(&self) -> RawFd {
462 self.io.get_ref().as_raw_fd()
463 }
464 }
465}
466
467#[cfg(windows)]
468mod sys {
469 // TODO: let's land these upstream with mio and then we can add them here.
470 //
471 // use std::os::windows::prelude::*;
472 // use super::UdpSocket;
473 //
474 // impl AsRawHandle for UdpSocket {
475 // fn as_raw_handle(&self) -> RawHandle {
476 // self.io.get_ref().as_raw_handle()
477 // }
478 // }
479}