Add peer_addr function to UdpSocket

This commit is contained in:
Linus Unnebäck 2019-03-11 16:07:31 +00:00
parent 52e885628e
commit a7bd36c9e8
No known key found for this signature in database
GPG key ID: CE70CEAE9C0FA66F
5 changed files with 36 additions and 0 deletions

View file

@ -180,6 +180,23 @@ impl UdpSocket {
}
}
/// Returns the socket address of the remote peer this socket was connected to.
///
/// # Examples
///
/// ```no_run
/// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
///
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
/// socket.connect("192.168.0.1:41203").expect("couldn't connect to address");
/// assert_eq!(socket.peer_addr().unwrap(),
/// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 0, 1), 41203)));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
self.0.peer_addr()
}
/// Returns the socket address that this socket was created from.
///
/// # Examples

View file

@ -159,6 +159,10 @@ impl UdpSocket {
unsupported()
}
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
match self.0 {}
}
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
match self.0 {}
}

View file

@ -72,6 +72,11 @@ impl UdpSocket {
Ok(None)
}
pub fn peer_addr(&self) -> Result<SocketAddr> {
let path = self.0.path()?;
Ok(path_to_peer_addr(path.to_str().unwrap_or("")))
}
pub fn socket_addr(&self) -> Result<SocketAddr> {
let path = self.0.path()?;
Ok(path_to_local_addr(path.to_str().unwrap_or("")))

View file

@ -257,6 +257,10 @@ impl UdpSocket {
unsupported()
}
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
match self.0 {}
}
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
match self.0 {}
}

View file

@ -472,6 +472,12 @@ impl UdpSocket {
pub fn into_socket(self) -> Socket { self.inner }
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
sockname(|buf, len| unsafe {
c::getpeername(*self.inner.as_inner(), buf, len)
})
}
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
sockname(|buf, len| unsafe {
c::getsockname(*self.inner.as_inner(), buf, len)