futures_net/driver/sys/net/
udp.rs

1//! Primitives for working with UDP
2//!
3//! The types provided in this module are non-blocking by default and are
4//! designed to be portable across all supported Mio platforms. As long as the
5//! [portability guidelines] are followed, the behavior should be identical no
6//! matter the target platform.
7//!
8use iovec::IoVec;
9/// [portability guidelines]: ../struct.Poll.html#portability
10use std::fmt;
11use std::io;
12use std::net::{self, Ipv4Addr, Ipv6Addr, SocketAddr};
13
14use crate::driver::sys::event::{Evented, PollOpt, Ready};
15use crate::driver::sys::poll::SelectorId;
16use crate::driver::sys::{linux, Poll, Token};
17
18/// A User Datagram Protocol socket.
19///
20/// This is an implementation of a bound UDP socket. This supports both IPv4 and
21/// IPv6 addresses, and there is no corresponding notion of a server because UDP
22/// is a datagram protocol.
23///
24/// # Examples
25///
26/// ```
27/// # use std::error::Error;
28/// #
29/// # fn try_main() -> Result<(), Box<Error>> {
30/// // An Echo program:
31/// // SENDER -> sends a message.
32/// // ECHOER -> listens and prints the message received.
33///
34/// use futures_net::driver::sys::net::UdpSocket;
35/// use futures_net::driver::sys::{Poll, Token};
36/// use futures_net::driver::sys::event::{Events, Ready, PollOpt};
37/// use std::time::Duration;
38///
39/// const SENDER: Token = Token(0);
40/// const ECHOER: Token = Token(1);
41///
42/// // This operation will fail if the address is in use, so we select different ports for each
43/// // socket.
44/// let sender_socket = UdpSocket::bind(&"127.0.0.1:0".parse()?)?;
45/// let echoer_socket = UdpSocket::bind(&"127.0.0.1:0".parse()?)?;
46///
47/// // If we do not use connect here, SENDER and ECHOER would need to call send_to and recv_from
48/// // respectively.
49/// sender_socket.connect(echoer_socket.local_addr().unwrap())?;
50///
51/// // We need a Poll to check if SENDER is ready to be written into, and if ECHOER is ready to be
52/// // read from.
53/// let poll = Poll::new()?;
54///
55/// // We register our sockets here so that we can check if they are ready to be written/read.
56/// poll.register(&sender_socket, SENDER, Ready::writable(), PollOpt::edge())?;
57/// poll.register(&echoer_socket, ECHOER, Ready::readable(), PollOpt::edge())?;
58///
59/// let msg_to_send = [9; 9];
60/// let mut buffer = [0; 9];
61///
62/// let mut events = Events::with_capacity(128);
63/// loop {
64///     poll.poll(&mut events, Some(Duration::from_millis(100)))?;
65///     for event in events.iter() {
66///         match event.token() {
67///             // Our SENDER is ready to be written into.
68///             SENDER => {
69///                 let bytes_sent = sender_socket.send(&msg_to_send)?;
70///                 assert_eq!(bytes_sent, 9);
71///                 println!("sent {:?} -> {:?} bytes", msg_to_send, bytes_sent);
72///             },
73///             // Our ECHOER is ready to be read from.
74///             ECHOER => {
75///                 let num_recv = echoer_socket.recv(&mut buffer)?;
76///                 println!("echo {:?} -> {:?}", buffer, num_recv);
77///                 buffer = [0; 9];
78///                 # return Ok(());
79///             }
80///             _ => unreachable!()
81///         }
82///     }
83/// }
84/// #
85/// #   Ok(())
86/// # }
87/// #
88/// # fn main() {
89/// #   try_main().unwrap();
90/// # }
91/// ```
92pub struct UdpSocket {
93    sys: linux::UdpSocket,
94    selector_id: SelectorId,
95}
96
97impl UdpSocket {
98    /// Creates a UDP socket from the given address.
99    ///
100    /// # Examples
101    ///
102    /// ```
103    /// # use std::error::Error;
104    /// #
105    /// # fn try_main() -> Result<(), Box<Error>> {
106    /// use futures_net::driver::sys::net::UdpSocket;
107    ///
108    /// // We must bind it to an open address.
109    /// let socket = match UdpSocket::bind(&"127.0.0.1:0".parse()?) {
110    ///     Ok(new_socket) => new_socket,
111    ///     Err(fail) => {
112    ///         // We panic! here, but you could try to bind it again on another address.
113    ///         panic!("Failed to bind socket. {:?}", fail);
114    ///     }
115    /// };
116    ///
117    /// // Our socket was created, but we should not use it before checking it's readiness.
118    /// #    Ok(())
119    /// # }
120    /// #
121    /// # fn main() {
122    /// #   try_main().unwrap();
123    /// # }
124    /// ```
125    pub fn bind(addr: &SocketAddr) -> io::Result<UdpSocket> {
126        let socket = net::UdpSocket::bind(addr)?;
127        UdpSocket::from_socket(socket)
128    }
129
130    /// Creates a new mio-wrapped socket from an underlying and bound std
131    /// socket.
132    ///
133    /// This function requires that `socket` has previously been bound to an
134    /// address to work correctly, and returns an I/O object which can be used
135    /// with mio to send/receive UDP messages.
136    ///
137    /// This can be used in conjunction with net2's `UdpBuilder` interface to
138    /// configure a socket before it's handed off to mio, such as setting
139    /// options like `reuse_address` or binding to multiple addresses.
140    pub fn from_socket(socket: net::UdpSocket) -> io::Result<UdpSocket> {
141        Ok(UdpSocket {
142            sys: linux::UdpSocket::new(socket)?,
143            selector_id: SelectorId::new(),
144        })
145    }
146
147    /// Returns the socket address that this socket was created from.
148    ///
149    /// # Examples
150    ///
151    // This assertion is almost, but not quite, universal.  It fails on
152    // shared-IP FreeBSD jails.  It's hard for mio to know whether we're jailed,
153    // so simply disable the test on FreeBSD.
154    /// ```
155    /// # use std::error::Error;
156    /// #
157    /// # fn try_main() -> Result<(), Box<Error>> {
158    /// use futures_net::driver::sys::net::UdpSocket;
159    ///
160    /// let addr = "127.0.0.1:0".parse()?;
161    /// let socket = UdpSocket::bind(&addr)?;
162    /// #    Ok(())
163    /// # }
164    /// #
165    /// # fn main() {
166    /// #   try_main().unwrap();
167    /// # }
168    /// ```
169    pub fn local_addr(&self) -> io::Result<SocketAddr> {
170        self.sys.local_addr()
171    }
172
173    /// Creates a new independently owned handle to the underlying socket.
174    ///
175    /// The returned `UdpSocket` is a reference to the same socket that this
176    /// object references. Both handles will read and write the same port, and
177    /// options set on one socket will be propagated to the other.
178    ///
179    /// # Examples
180    ///
181    /// ```
182    /// # use std::error::Error;
183    /// #
184    /// # fn try_main() -> Result<(), Box<Error>> {
185    /// use futures_net::driver::sys::net::UdpSocket;
186    ///
187    /// // We must bind it to an open address.
188    /// let socket = UdpSocket::bind(&"127.0.0.1:0".parse()?)?;
189    /// let cloned_socket = socket.try_clone()?;
190    ///
191    /// assert_eq!(socket.local_addr()?, cloned_socket.local_addr()?);
192    ///
193    /// #    Ok(())
194    /// # }
195    /// #
196    /// # fn main() {
197    /// #   try_main().unwrap();
198    /// # }
199    /// ```
200    pub fn try_clone(&self) -> io::Result<UdpSocket> {
201        self.sys.try_clone().map(|s| UdpSocket {
202            sys: s,
203            selector_id: self.selector_id.clone(),
204        })
205    }
206
207    /// Sends data on the socket to the given address. On success, returns the
208    /// number of bytes written.
209    ///
210    /// Address type can be any implementor of `ToSocketAddrs` trait. See its
211    /// documentation for concrete examples.
212    ///
213    /// # Examples
214    ///
215    /// ```no_run
216    /// # use std::error::Error;
217    /// # fn try_main() -> Result<(), Box<Error>> {
218    /// use futures_net::driver::sys::net::UdpSocket;
219    ///
220    /// let socket = UdpSocket::bind(&"127.0.0.1:0".parse()?)?;
221    ///
222    /// // We must check if the socket is writable before calling send_to,
223    /// // or we could run into a WouldBlock error.
224    ///
225    /// let bytes_sent = socket.send_to(&[9; 9], &"127.0.0.1:11100".parse()?)?;
226    /// assert_eq!(bytes_sent, 9);
227    /// #
228    /// #    Ok(())
229    /// # }
230    /// #
231    /// # fn main() {
232    /// #   try_main().unwrap();
233    /// # }
234    /// ```
235    pub fn send_to(&self, buf: &[u8], target: &SocketAddr) -> io::Result<usize> {
236        self.sys.send_to(buf, target)
237    }
238
239    /// Receives data from the socket. On success, returns the number of bytes
240    /// read and the address from whence the data came.
241    ///
242    /// # Examples
243    ///
244    /// ```no_run
245    /// # use std::error::Error;
246    /// #
247    /// # fn try_main() -> Result<(), Box<Error>> {
248    /// use futures_net::driver::sys::net::UdpSocket;
249    ///
250    /// let socket = UdpSocket::bind(&"127.0.0.1:0".parse()?)?;
251    ///
252    /// // We must check if the socket is readable before calling recv_from,
253    /// // or we could run into a WouldBlock error.
254    ///
255    /// let mut buf = [0; 9];
256    /// let (num_recv, from_addr) = socket.recv_from(&mut buf)?;
257    /// println!("Received {:?} -> {:?} bytes from {:?}", buf, num_recv, from_addr);
258    /// #
259    /// #    Ok(())
260    /// # }
261    /// #
262    /// # fn main() {
263    /// #   try_main().unwrap();
264    /// # }
265    /// ```
266    pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
267        self.sys.recv_from(buf)
268    }
269
270    /// Sends data on the socket to the address previously bound via connect(). On success,
271    /// returns the number of bytes written.
272    pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
273        self.sys.send(buf)
274    }
275
276    /// Receives data from the socket previously bound with connect(). On success, returns
277    /// the number of bytes read.
278    pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
279        self.sys.recv(buf)
280    }
281
282    /// Connects the UDP socket setting the default destination for `send()`
283    /// and limiting packets that are read via `recv` from the address specified
284    /// in `addr`.
285    pub fn connect(&self, addr: SocketAddr) -> io::Result<()> {
286        self.sys.connect(addr)
287    }
288
289    /// Sets the value of the `SO_BROADCAST` option for this socket.
290    ///
291    /// When enabled, this socket is allowed to send packets to a broadcast
292    /// address.
293    ///
294    /// # Examples
295    ///
296    /// ```
297    /// # use std::error::Error;
298    /// #
299    /// # fn try_main() -> Result<(), Box<Error>> {
300    /// use futures_net::driver::sys::net::UdpSocket;
301    ///
302    /// let broadcast_socket = UdpSocket::bind(&"127.0.0.1:0".parse()?)?;
303    /// if broadcast_socket.broadcast()? == false {
304    ///     broadcast_socket.set_broadcast(true)?;
305    /// }
306    ///
307    /// assert_eq!(broadcast_socket.broadcast()?, true);
308    /// #
309    /// #    Ok(())
310    /// # }
311    /// #
312    /// # fn main() {
313    /// #   try_main().unwrap();
314    /// # }
315    /// ```
316    pub fn set_broadcast(&self, on: bool) -> io::Result<()> {
317        self.sys.set_broadcast(on)
318    }
319
320    /// Gets the value of the `SO_BROADCAST` option for this socket.
321    ///
322    /// For more information about this option, see
323    /// [`set_broadcast`][link].
324    ///
325    /// [link]: #method.set_broadcast
326    ///
327    /// # Examples
328    ///
329    /// ```
330    /// # use std::error::Error;
331    /// #
332    /// # fn try_main() -> Result<(), Box<Error>> {
333    /// use futures_net::driver::sys::net::UdpSocket;
334    ///
335    /// let broadcast_socket = UdpSocket::bind(&"127.0.0.1:0".parse()?)?;
336    /// assert_eq!(broadcast_socket.broadcast()?, false);
337    /// #
338    /// #    Ok(())
339    /// # }
340    /// #
341    /// # fn main() {
342    /// #   try_main().unwrap();
343    /// # }
344    /// ```
345    pub fn broadcast(&self) -> io::Result<bool> {
346        self.sys.broadcast()
347    }
348
349    /// Sets the value of the `IP_MULTICAST_LOOP` option for this socket.
350    ///
351    /// If enabled, multicast packets will be looped back to the local socket.
352    /// Note that this may not have any affect on IPv6 sockets.
353    pub fn set_multicast_loop_v4(&self, on: bool) -> io::Result<()> {
354        self.sys.set_multicast_loop_v4(on)
355    }
356
357    /// Gets the value of the `IP_MULTICAST_LOOP` option for this socket.
358    ///
359    /// For more information about this option, see
360    /// [`set_multicast_loop_v4`][link].
361    ///
362    /// [link]: #method.set_multicast_loop_v4
363    pub fn multicast_loop_v4(&self) -> io::Result<bool> {
364        self.sys.multicast_loop_v4()
365    }
366
367    /// Sets the value of the `IP_MULTICAST_TTL` option for this socket.
368    ///
369    /// Indicates the time-to-live value of outgoing multicast packets for
370    /// this socket. The default value is 1 which means that multicast packets
371    /// don't leave the local network unless explicitly requested.
372    ///
373    /// Note that this may not have any affect on IPv6 sockets.
374    pub fn set_multicast_ttl_v4(&self, ttl: u32) -> io::Result<()> {
375        self.sys.set_multicast_ttl_v4(ttl)
376    }
377
378    /// Gets the value of the `IP_MULTICAST_TTL` option for this socket.
379    ///
380    /// For more information about this option, see
381    /// [`set_multicast_ttl_v4`][link].
382    ///
383    /// [link]: #method.set_multicast_ttl_v4
384    pub fn multicast_ttl_v4(&self) -> io::Result<u32> {
385        self.sys.multicast_ttl_v4()
386    }
387
388    /// Sets the value of the `IPV6_MULTICAST_LOOP` option for this socket.
389    ///
390    /// Controls whether this socket sees the multicast packets it sends itself.
391    /// Note that this may not have any affect on IPv4 sockets.
392    pub fn set_multicast_loop_v6(&self, on: bool) -> io::Result<()> {
393        self.sys.set_multicast_loop_v6(on)
394    }
395
396    /// Gets the value of the `IPV6_MULTICAST_LOOP` option for this socket.
397    ///
398    /// For more information about this option, see
399    /// [`set_multicast_loop_v6`][link].
400    ///
401    /// [link]: #method.set_multicast_loop_v6
402    pub fn multicast_loop_v6(&self) -> io::Result<bool> {
403        self.sys.multicast_loop_v6()
404    }
405
406    /// Sets the value for the `IP_TTL` option on this socket.
407    ///
408    /// This value sets the time-to-live field that is used in every packet sent
409    /// from this socket.
410    ///
411    /// # Examples
412    ///
413    /// ```
414    /// # use std::error::Error;
415    /// #
416    /// # fn try_main() -> Result<(), Box<Error>> {
417    /// use futures_net::driver::sys::net::UdpSocket;
418    ///
419    /// let socket = UdpSocket::bind(&"127.0.0.1:0".parse()?)?;
420    /// if socket.ttl()? < 255 {
421    ///     socket.set_ttl(255)?;
422    /// }
423    ///
424    /// assert_eq!(socket.ttl()?, 255);
425    /// #
426    /// #    Ok(())
427    /// # }
428    /// #
429    /// # fn main() {
430    /// #   try_main().unwrap();
431    /// # }
432    /// ```
433    pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
434        self.sys.set_ttl(ttl)
435    }
436
437    /// Gets the value of the `IP_TTL` option for this socket.
438    ///
439    /// For more information about this option, see [`set_ttl`][link].
440    ///
441    /// [link]: #method.set_ttl
442    ///
443    /// # Examples
444    ///
445    /// ```
446    /// # use std::error::Error;
447    /// #
448    /// # fn try_main() -> Result<(), Box<Error>> {
449    /// use futures_net::driver::sys::net::UdpSocket;
450    ///
451    /// let socket = UdpSocket::bind(&"127.0.0.1:0".parse()?)?;
452    /// socket.set_ttl(255)?;
453    ///
454    /// assert_eq!(socket.ttl()?, 255);
455    /// #
456    /// #    Ok(())
457    /// # }
458    /// #
459    /// # fn main() {
460    /// #   try_main().unwrap();
461    /// # }
462    /// ```
463    pub fn ttl(&self) -> io::Result<u32> {
464        self.sys.ttl()
465    }
466
467    /// Executes an operation of the `IP_ADD_MEMBERSHIP` type.
468    ///
469    /// This function specifies a new multicast group for this socket to join.
470    /// The address must be a valid multicast address, and `interface` is the
471    /// address of the local interface with which the system should join the
472    /// multicast group. If it's equal to `INADDR_ANY` then an appropriate
473    /// interface is chosen by the system.
474    pub fn join_multicast_v4(
475        &self,
476        multiaddr: &Ipv4Addr,
477        interface: &Ipv4Addr,
478    ) -> io::Result<()> {
479        self.sys.join_multicast_v4(multiaddr, interface)
480    }
481
482    /// Executes an operation of the `IPV6_ADD_MEMBERSHIP` type.
483    ///
484    /// This function specifies a new multicast group for this socket to join.
485    /// The address must be a valid multicast address, and `interface` is the
486    /// index of the interface to join/leave (or 0 to indicate any interface).
487    pub fn join_multicast_v6(
488        &self,
489        multiaddr: &Ipv6Addr,
490        interface: u32,
491    ) -> io::Result<()> {
492        self.sys.join_multicast_v6(multiaddr, interface)
493    }
494
495    /// Executes an operation of the `IP_DROP_MEMBERSHIP` type.
496    ///
497    /// For more information about this option, see
498    /// [`join_multicast_v4`][link].
499    ///
500    /// [link]: #method.join_multicast_v4
501    pub fn leave_multicast_v4(
502        &self,
503        multiaddr: &Ipv4Addr,
504        interface: &Ipv4Addr,
505    ) -> io::Result<()> {
506        self.sys.leave_multicast_v4(multiaddr, interface)
507    }
508
509    /// Executes an operation of the `IPV6_DROP_MEMBERSHIP` type.
510    ///
511    /// For more information about this option, see
512    /// [`join_multicast_v6`][link].
513    ///
514    /// [link]: #method.join_multicast_v6
515    pub fn leave_multicast_v6(
516        &self,
517        multiaddr: &Ipv6Addr,
518        interface: u32,
519    ) -> io::Result<()> {
520        self.sys.leave_multicast_v6(multiaddr, interface)
521    }
522
523    /// Sets the value for the `IPV6_V6ONLY` option on this socket.
524    ///
525    /// If this is set to `true` then the socket is restricted to sending and
526    /// receiving IPv6 packets only. In this case two IPv4 and IPv6 applications
527    /// can bind the same port at the same time.
528    ///
529    /// If this is set to `false` then the socket can be used to send and
530    /// receive packets from an IPv4-mapped IPv6 address.
531    pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
532        self.sys.set_only_v6(only_v6)
533    }
534
535    /// Gets the value of the `IPV6_V6ONLY` option for this socket.
536    ///
537    /// For more information about this option, see [`set_only_v6`][link].
538    ///
539    /// [link]: #method.set_only_v6
540    pub fn only_v6(&self) -> io::Result<bool> {
541        self.sys.only_v6()
542    }
543
544    /// Get the value of the `SO_ERROR` option on this socket.
545    ///
546    /// This will retrieve the stored error in the underlying socket, clearing
547    /// the field in the process. This can be useful for checking errors between
548    /// calls.
549    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
550        self.sys.take_error()
551    }
552
553    /// Receives a single datagram message socket previously bound with connect.
554    ///
555    /// This operation will attempt to read bytes from this socket and place
556    /// them into the list of buffers provided. Note that each buffer is an
557    /// `IoVec` which can be created from a byte slice.
558    ///
559    /// The buffers provided will be filled sequentially. A buffer will be
560    /// entirely filled up before the next is written to.
561    ///
562    /// The number of bytes read is returned, if successful, or an error is
563    /// returned otherwise. If no bytes are available to be read yet then
564    /// a [`WouldBlock`][link] error is returned. This operation does not block.
565    ///
566    /// On Unix this corresponds to the `readv` syscall.
567    ///
568    /// [link]: https://doc.rust-lang.org/nightly/std/io/enum.ErrorKind.html#variant.WouldBlock
569    pub fn recv_bufs(&self, bufs: &mut [&mut IoVec]) -> io::Result<usize> {
570        self.sys.readv(bufs)
571    }
572
573    /// Sends data on the socket to the address previously bound via connect.
574    ///
575    /// This operation will attempt to send a list of byte buffers to this
576    /// socket in a single datagram. Note that each buffer is an `IoVec`
577    /// which can be created from a byte slice.
578    ///
579    /// The buffers provided will be written sequentially. A buffer will be
580    /// entirely written before the next is written.
581    ///
582    /// The number of bytes written is returned, if successful, or an error is
583    /// returned otherwise. If the socket is not currently writable then a
584    /// [`WouldBlock`][link] error is returned. This operation does not block.
585    ///
586    /// On Unix this corresponds to the `writev` syscall.
587    ///
588    /// [link]: https://doc.rust-lang.org/nightly/std/io/enum.ErrorKind.html#variant.WouldBlock
589    pub fn send_bufs(&self, bufs: &[&IoVec]) -> io::Result<usize> {
590        self.sys.writev(bufs)
591    }
592}
593
594impl Evented for UdpSocket {
595    fn register(
596        &self,
597        poll: &Poll,
598        token: Token,
599        interest: Ready,
600        opts: PollOpt,
601    ) -> io::Result<()> {
602        self.selector_id.associate_selector(poll)?;
603        self.sys.register(poll, token, interest, opts)
604    }
605
606    fn reregister(
607        &self,
608        poll: &Poll,
609        token: Token,
610        interest: Ready,
611        opts: PollOpt,
612    ) -> io::Result<()> {
613        self.sys.reregister(poll, token, interest, opts)
614    }
615
616    fn deregister(&self, poll: &Poll) -> io::Result<()> {
617        self.sys.deregister(poll)
618    }
619}
620
621impl fmt::Debug for UdpSocket {
622    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
623        fmt::Debug::fmt(&self.sys, f)
624    }
625}
626
627/*
628 *
629 * ===== UNIX ext =====
630 *
631 */
632
633use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
634
635impl IntoRawFd for UdpSocket {
636    fn into_raw_fd(self) -> RawFd {
637        self.sys.into_raw_fd()
638    }
639}
640
641impl AsRawFd for UdpSocket {
642    fn as_raw_fd(&self) -> RawFd {
643        self.sys.as_raw_fd()
644    }
645}
646
647impl FromRawFd for UdpSocket {
648    unsafe fn from_raw_fd(fd: RawFd) -> UdpSocket {
649        UdpSocket {
650            sys: FromRawFd::from_raw_fd(fd),
651            selector_id: SelectorId::new(),
652        }
653    }
654}