Skip to content

Commit 02f26e3

Browse files
committed
Add peer_addr function to UdpSocket
1 parent cf3c9a7 commit 02f26e3

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

src/libstd/net/udp.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ impl UdpSocket {
180180
}
181181
}
182182

183+
/// Returns the socket address of the remote peer this socket was connected to.
184+
///
185+
/// # Examples
186+
///
187+
/// ```no_run
188+
/// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
189+
///
190+
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
191+
/// socket.connect("192.168.0.1:41203").expect("couldn't connect to address");
192+
/// assert_eq!(socket.peer_addr().unwrap(),
193+
/// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 0, 1), 41203)));
194+
/// ```
195+
#[stable(feature = "rust1", since = "1.0.0")]
196+
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
197+
self.0.peer_addr()
198+
}
199+
183200
/// Returns the socket address that this socket was created from.
184201
///
185202
/// # Examples

src/libstd/sys/cloudabi/shims/net.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ impl UdpSocket {
159159
unsupported()
160160
}
161161

162+
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
163+
match self.0 {}
164+
}
165+
162166
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
163167
match self.0 {}
164168
}

src/libstd/sys/redox/net/udp.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ impl UdpSocket {
7272
Ok(None)
7373
}
7474

75+
pub fn peer_addr(&self) -> Result<SocketAddr> {
76+
let path = self.0.path()?;
77+
Ok(path_to_peer_addr(path.to_str().unwrap_or("")))
78+
}
79+
7580
pub fn socket_addr(&self) -> Result<SocketAddr> {
7681
let path = self.0.path()?;
7782
Ok(path_to_local_addr(path.to_str().unwrap_or("")))

src/libstd/sys/sgx/net.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ impl UdpSocket {
257257
unsupported()
258258
}
259259

260+
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
261+
match self.0 {}
262+
}
263+
260264
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
261265
match self.0 {}
262266
}

src/libstd/sys_common/net.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,12 @@ impl UdpSocket {
472472

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

475+
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
476+
sockname(|buf, len| unsafe {
477+
c::getpeername(*self.inner.as_inner(), buf, len)
478+
})
479+
}
480+
475481
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
476482
sockname(|buf, len| unsafe {
477483
c::getsockname(*self.inner.as_inner(), buf, len)

0 commit comments

Comments
 (0)