diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-03-22 19:31:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-03-22 19:31:16 +0100 |
| commit | 5481b4e1d719a7d6de349c2e7a09574093710554 (patch) | |
| tree | e001fd07aebdb4b00cbfa02d1663e33ef5911e9e /src/libstd | |
| parent | ed196221d889ae8a87be677402e4319237ef67f4 (diff) | |
| parent | 81d5fb5c6fc65e947ff97c02997bfff9a1f6ce16 (diff) | |
| download | rust-5481b4e1d719a7d6de349c2e7a09574093710554.tar.gz rust-5481b4e1d719a7d6de349c2e7a09574093710554.zip | |
Rollup merge of #59106 - LinusU:udp-peer-addr, r=kennytm
Add peer_addr function to UdpSocket Fixes #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!
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/net/udp.rs | 43 | ||||
| -rw-r--r-- | src/libstd/sys/cloudabi/shims/net.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sys/redox/net/udp.rs | 5 | ||||
| -rw-r--r-- | src/libstd/sys/sgx/net.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sys/unix/l4re.rs | 5 | ||||
| -rw-r--r-- | src/libstd/sys/wasm/net.rs | 4 | ||||
| -rw-r--r-- | src/libstd/sys_common/net.rs | 6 |
7 files changed, 69 insertions, 2 deletions
diff --git a/src/libstd/net/udp.rs b/src/libstd/net/udp.rs index edc9d665444..b42a8123042 100644 --- a/src/libstd/net/udp.rs +++ b/src/libstd/net/udp.rs @@ -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 @@ -865,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())); @@ -873,6 +904,16 @@ mod tests { } #[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| { let sock1 = t!(UdpSocket::bind(&addr1)); diff --git a/src/libstd/sys/cloudabi/shims/net.rs b/src/libstd/sys/cloudabi/shims/net.rs index 6d2a4962ab4..4364a136544 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<SocketAddr> { + match self.0 {} + } + pub fn socket_addr(&self) -> io::Result<SocketAddr> { match self.0 {} } diff --git a/src/libstd/sys/redox/net/udp.rs b/src/libstd/sys/redox/net/udp.rs index b1a60b14570..274123dce4b 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<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(""))) diff --git a/src/libstd/sys/sgx/net.rs b/src/libstd/sys/sgx/net.rs index e167e917957..81f33c16294 100644 --- a/src/libstd/sys/sgx/net.rs +++ b/src/libstd/sys/sgx/net.rs @@ -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 {} } diff --git a/src/libstd/sys/unix/l4re.rs b/src/libstd/sys/unix/l4re.rs index b9e725371a3..b3dd1cf6aaa 100644 --- a/src/libstd/sys/unix/l4re.rs +++ b/src/libstd/sys/unix/l4re.rs @@ -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!(); } @@ -463,4 +467,3 @@ pub mod net { } } } - diff --git a/src/libstd/sys/wasm/net.rs b/src/libstd/sys/wasm/net.rs index a2ea2dfbbc0..c85dd000afe 100644 --- a/src/libstd/sys/wasm/net.rs +++ b/src/libstd/sys/wasm/net.rs @@ -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 {} } diff --git a/src/libstd/sys_common/net.rs b/src/libstd/sys_common/net.rs index b9505aaa69b..b77bcee4b9d 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<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) |
