1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use io
::{Error, ErrorKind, Result}
;
13 use net
::{SocketAddr, Ipv4Addr, Ipv6Addr}
;
15 use sys
::fs
::{File, OpenOptions}
;
18 use super::{path_to_peer_addr, path_to_local_addr}
;
21 pub struct UdpSocket(File
, UnsafeCell
<Option
<SocketAddr
>>);
24 pub fn bind(addr
: &SocketAddr
) -> Result
<UdpSocket
> {
25 let path
= format
!("udp:/{}", addr
);
26 let mut options
= OpenOptions
::new();
29 Ok(UdpSocket(File
::open(&Path
::new(path
.as_str()), &options
)?
, UnsafeCell
::new(None
)))
32 fn get_conn(&self) -> &mut Option
<SocketAddr
> {
33 unsafe { &mut *(self.1.get()) }
36 pub fn connect(&self, addr
: &SocketAddr
) -> Result
<()> {
37 unsafe { *self.1.get() = Some(*addr) }
;
41 pub fn duplicate(&self) -> Result
<UdpSocket
> {
42 let new_bind
= self.0.dup(&[])?
;
43 let new_conn
= *self.get_conn();
44 Ok(UdpSocket(new_bind
, UnsafeCell
::new(new_conn
)))
47 pub fn recv_from(&self, buf
: &mut [u8]) -> Result
<(usize, SocketAddr
)> {
48 let from
= self.0.dup(b
"listen")?
;
49 let path
= from
.path()?
;
50 let peer_addr
= path_to_peer_addr(path
.to_str().unwrap_or(""));
51 let count
= from
.read(buf
)?
;
52 Ok((count
, peer_addr
))
55 pub fn recv(&self, buf
: &mut [u8]) -> Result
<usize> {
56 if let Some(addr
) = *self.get_conn() {
57 let from
= self.0.dup(format
!("{}", addr
).as_bytes())?
;
60 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::recv not connected"))
64 pub fn send_to(&self, buf
: &[u8], addr
: &SocketAddr
) -> Result
<usize> {
65 let to
= self.0.dup(format
!("{}", addr
).as_bytes())?
;
69 pub fn send(&self, buf
: &[u8]) -> Result
<usize> {
70 if let Some(addr
) = *self.get_conn() {
71 self.send_to(buf
, &addr
)
73 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::send not connected"))
77 pub fn take_error(&self) -> Result
<Option
<Error
>> {
81 pub fn socket_addr(&self) -> Result
<SocketAddr
> {
82 let path
= self.0.path()?
;
83 Ok(path_to_local_addr(path
.to_str().unwrap_or("")))
86 pub fn broadcast(&self) -> Result
<bool
> {
87 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::broadcast not implemented"))
90 pub fn multicast_loop_v4(&self) -> Result
<bool
> {
91 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::multicast_loop_v4 not implemented"))
94 pub fn multicast_loop_v6(&self) -> Result
<bool
> {
95 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::multicast_loop_v6 not implemented"))
98 pub fn multicast_ttl_v4(&self) -> Result
<u32> {
99 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::multicast_ttl_v4 not implemented"))
102 pub fn nonblocking(&self) -> Result
<bool
> {
103 self.0.fd
().nonblocking()
106 pub fn only_v6(&self) -> Result
<bool
> {
107 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::only_v6 not implemented"))
110 pub fn ttl(&self) -> Result
<u32> {
111 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::ttl not implemented"))
114 pub fn read_timeout(&self) -> Result
<Option
<Duration
>> {
115 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::read_timeout not implemented"))
118 pub fn write_timeout(&self) -> Result
<Option
<Duration
>> {
119 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::write_timeout not implemented"))
122 pub fn set_broadcast(&self, _broadcast
: bool
) -> Result
<()> {
123 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::set_broadcast not implemented"))
126 pub fn set_multicast_loop_v4(&self, _multicast_loop_v4
: bool
) -> Result
<()> {
127 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::set_multicast_loop_v4 not implemented"))
130 pub fn set_multicast_loop_v6(&self, _multicast_loop_v6
: bool
) -> Result
<()> {
131 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::set_multicast_loop_v6 not implemented"))
134 pub fn set_multicast_ttl_v4(&self, _multicast_ttl_v4
: u32) -> Result
<()> {
135 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::set_multicast_ttl_v4 not implemented"))
138 pub fn set_nonblocking(&self, nonblocking
: bool
) -> Result
<()> {
139 self.0.fd
().set_nonblocking(nonblocking
)
142 pub fn set_only_v6(&self, _only_v6
: bool
) -> Result
<()> {
143 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::set_only_v6 not implemented"))
146 pub fn set_ttl(&self, _ttl
: u32) -> Result
<()> {
147 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::set_ttl not implemented"))
150 pub fn set_read_timeout(&self, _dur
: Option
<Duration
>) -> Result
<()> {
151 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::set_read_timeout not implemented"))
154 pub fn set_write_timeout(&self, _dur
: Option
<Duration
>) -> Result
<()> {
155 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::set_write_timeout not implemented"))
158 pub fn join_multicast_v4(&self, _multiaddr
: &Ipv4Addr
, _interface
: &Ipv4Addr
) -> Result
<()> {
159 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::join_multicast_v4 not implemented"))
162 pub fn join_multicast_v6(&self, _multiaddr
: &Ipv6Addr
, _interface
: u32) -> Result
<()> {
163 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::join_multicast_v6 not implemented"))
166 pub fn leave_multicast_v4(&self, _multiaddr
: &Ipv4Addr
, _interface
: &Ipv4Addr
) -> Result
<()> {
167 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::leave_multicast_v4 not implemented"))
170 pub fn leave_multicast_v6(&self, _multiaddr
: &Ipv6Addr
, _interface
: u32) -> Result
<()> {
171 Err(Error
::new(ErrorKind
::Other
, "UdpSocket::leave_multicast_v6 not implemented"))