From 02f26e3690ad380223a24078656752d152c4f4f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Mon, 11 Mar 2019 16:07:31 +0000 Subject: [PATCH 1/5] Add peer_addr function to UdpSocket --- src/libstd/net/udp.rs | 17 +++++++++++++++++ src/libstd/sys/cloudabi/shims/net.rs | 4 ++++ src/libstd/sys/redox/net/udp.rs | 5 +++++ src/libstd/sys/sgx/net.rs | 4 ++++ src/libstd/sys_common/net.rs | 6 ++++++ 5 files changed, 36 insertions(+) diff --git a/src/libstd/net/udp.rs b/src/libstd/net/udp.rs index edc9d665444a0..c7ccf45b953e7 100644 --- a/src/libstd/net/udp.rs +++ b/src/libstd/net/udp.rs @@ -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 { + self.0.peer_addr() + } + /// Returns the socket address that this socket was created from. /// /// # Examples diff --git a/src/libstd/sys/cloudabi/shims/net.rs b/src/libstd/sys/cloudabi/shims/net.rs index 50d72dc7b240b..1185d7cfb2c4c 100644 --- a/src/libstd/sys/cloudabi/shims/net.rs +++ b/src/libstd/sys/cloudabi/shims/net.rs @@ -159,6 +159,10 @@ impl UdpSocket { unsupported() } + pub fn peer_addr(&self) -> io::Result { + match self.0 {} + } + pub fn socket_addr(&self) -> io::Result { match self.0 {} } diff --git a/src/libstd/sys/redox/net/udp.rs b/src/libstd/sys/redox/net/udp.rs index b1a60b1457083..274123dce4b58 100644 --- a/src/libstd/sys/redox/net/udp.rs +++ b/src/libstd/sys/redox/net/udp.rs @@ -72,6 +72,11 @@ impl UdpSocket { Ok(None) } + pub fn peer_addr(&self) -> Result { + let path = self.0.path()?; + Ok(path_to_peer_addr(path.to_str().unwrap_or(""))) + } + pub fn socket_addr(&self) -> Result { let path = self.0.path()?; Ok(path_to_local_addr(path.to_str().unwrap_or(""))) diff --git a/src/libstd/sys/sgx/net.rs b/src/libstd/sys/sgx/net.rs index ab8b2681393f8..8e693dc7ac25d 100644 --- a/src/libstd/sys/sgx/net.rs +++ b/src/libstd/sys/sgx/net.rs @@ -257,6 +257,10 @@ impl UdpSocket { unsupported() } + pub fn peer_addr(&self) -> io::Result { + match self.0 {} + } + pub fn socket_addr(&self) -> io::Result { match self.0 {} } diff --git a/src/libstd/sys_common/net.rs b/src/libstd/sys_common/net.rs index 36721171b1733..3f88d3e0e4e14 100644 --- a/src/libstd/sys_common/net.rs +++ b/src/libstd/sys_common/net.rs @@ -472,6 +472,12 @@ impl UdpSocket { pub fn into_socket(self) -> Socket { self.inner } + pub fn peer_addr(&self) -> io::Result { + sockname(|buf, len| unsafe { + c::getpeername(*self.inner.as_inner(), buf, len) + }) + } + pub fn socket_addr(&self) -> io::Result { sockname(|buf, len| unsafe { c::getsockname(*self.inner.as_inner(), buf, len) From df2dce326a979ab178bb2e39a74310e205f0242c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Tue, 12 Mar 2019 10:32:23 +0000 Subject: [PATCH 2/5] Mark UdpSocket peer_addr unstable w/ tracking issue --- src/libstd/net/udp.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libstd/net/udp.rs b/src/libstd/net/udp.rs index c7ccf45b953e7..164039b303230 100644 --- a/src/libstd/net/udp.rs +++ b/src/libstd/net/udp.rs @@ -185,6 +185,7 @@ impl UdpSocket { /// # 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"); @@ -192,7 +193,7 @@ impl UdpSocket { /// assert_eq!(socket.peer_addr().unwrap(), /// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 0, 1), 41203))); /// ``` - #[stable(feature = "rust1", since = "1.0.0")] + #[unstable(feature = "udp_peer_addr", issue = "59127")] pub fn peer_addr(&self) -> io::Result { self.0.peer_addr() } From 54bf8e02943daa746286ad03b6d745e03f0bf3f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Tue, 12 Mar 2019 11:19:33 +0000 Subject: [PATCH 3/5] Document UdpSocket peer_addr NotConnected error --- src/libstd/net/udp.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/libstd/net/udp.rs b/src/libstd/net/udp.rs index 164039b303230..79e5ae79e4c01 100644 --- a/src/libstd/net/udp.rs +++ b/src/libstd/net/udp.rs @@ -193,6 +193,19 @@ impl UdpSocket { /// 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 { self.0.peer_addr() From 79695ea5b51bbee32092966625513c540523b470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Tue, 12 Mar 2019 16:50:00 +0000 Subject: [PATCH 4/5] Add test for UdpSocket peer_addr --- src/libstd/net/udp.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libstd/net/udp.rs b/src/libstd/net/udp.rs index 79e5ae79e4c01..f3f65034f4256 100644 --- a/src/libstd/net/udp.rs +++ b/src/libstd/net/udp.rs @@ -903,6 +903,16 @@ mod tests { }) } + #[test] + fn socket_peer_ip4() { + 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| { From ca32fe4c0afc8a564a87d700c379adf2d5a2da15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Tue, 12 Mar 2019 17:18:07 +0000 Subject: [PATCH 5/5] Fix test names regarding ip version --- src/libstd/net/udp.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/net/udp.rs b/src/libstd/net/udp.rs index f3f65034f4256..b42a812304269 100644 --- a/src/libstd/net/udp.rs +++ b/src/libstd/net/udp.rs @@ -896,7 +896,7 @@ 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())); @@ -904,7 +904,7 @@ mod tests { } #[test] - fn socket_peer_ip4() { + fn socket_peer() { each_ip(&mut |addr1, addr2| { let server = t!(UdpSocket::bind(&addr1)); assert_eq!(server.peer_addr().unwrap_err().kind(), ErrorKind::NotConnected);