Skip to content

Commit

Permalink
Rollup merge of rust-lang#59106 - LinusU:udp-peer-addr, r=kennytm
Browse files Browse the repository at this point in the history
Add peer_addr function to UdpSocket

Fixes rust-lang#59104

This is my first pull request to Rust, so opening early for some feedback.

My biggest question is: where do I add tests?

Any comments very much appreciated!
  • Loading branch information
Centril authored Mar 19, 2019
2 parents fce8f7c + 81d5fb5 commit be840cd
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 2 deletions.
43 changes: 42 additions & 1 deletion src/libstd/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,37 @@ impl UdpSocket {
}
}

/// Returns the socket address of the remote peer this socket was connected to.
///
/// # Examples
///
/// ```no_run
/// #![feature(udp_peer_addr)]
/// 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)));
/// ```
///
/// If the socket isn't connected, it will return a [`NotConnected`] error.
///
/// [`NotConnected`]: ../../std/io/enum.ErrorKind.html#variant.NotConnected
///
/// ```no_run
/// #![feature(udp_peer_addr)]
/// use std::net::UdpSocket;
///
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
/// assert_eq!(socket.peer_addr().unwrap_err().kind(),
/// ::std::io::ErrorKind::NotConnected);
/// ```
#[unstable(feature = "udp_peer_addr", issue = "59127")]
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
self.0.peer_addr()
}

/// Returns the socket address that this socket was created from.
///
/// # Examples
Expand Down Expand Up @@ -865,13 +896,23 @@ mod tests {
}

#[test]
fn socket_name_ip4() {
fn socket_name() {
each_ip(&mut |addr, _| {
let server = t!(UdpSocket::bind(&addr));
assert_eq!(addr, t!(server.local_addr()));
})
}

#[test]
fn socket_peer() {
each_ip(&mut |addr1, addr2| {
let server = t!(UdpSocket::bind(&addr1));
assert_eq!(server.peer_addr().unwrap_err().kind(), ErrorKind::NotConnected);
t!(server.connect(&addr2));
assert_eq!(addr2, t!(server.peer_addr()));
})
}

#[test]
fn udp_clone_smoke() {
each_ip(&mut |addr1, addr2| {
Expand Down
4 changes: 4 additions & 0 deletions src/libstd/sys/cloudabi/shims/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
Expand Down
5 changes: 5 additions & 0 deletions src/libstd/sys/redox/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("")))
Expand Down
4 changes: 4 additions & 0 deletions src/libstd/sys/sgx/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,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 {}
}
Expand Down
5 changes: 4 additions & 1 deletion src/libstd/sys/unix/l4re.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ pub mod net {

pub fn into_socket(self) -> Socket { self.inner }

pub fn peer_addr(&self) -> io::Result<SocketAddr> {
unimpl!();
}

pub fn socket_addr(&self) -> io::Result<SocketAddr> {
unimpl!();
}
Expand Down Expand Up @@ -463,4 +467,3 @@ pub mod net {
}
}
}

4 changes: 4 additions & 0 deletions src/libstd/sys/wasm/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,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 {}
}
Expand Down
6 changes: 6 additions & 0 deletions src/libstd/sys_common/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit be840cd

Please sign in to comment.