Skip to content

Commit 3672ef7

Browse files
authored
Rollup merge of rust-lang#59106 - LinusU:udp-peer-addr, r=kennytm
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!
2 parents 348ee6d + ca32fe4 commit 3672ef7

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

src/libstd/net/udp.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,37 @@ 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+
/// #![feature(udp_peer_addr)]
189+
/// use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4, UdpSocket};
190+
///
191+
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
192+
/// socket.connect("192.168.0.1:41203").expect("couldn't connect to address");
193+
/// assert_eq!(socket.peer_addr().unwrap(),
194+
/// SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 0, 1), 41203)));
195+
/// ```
196+
///
197+
/// If the socket isn't connected, it will return a [`NotConnected`] error.
198+
///
199+
/// [`NotConnected`]: ../../std/io/enum.ErrorKind.html#variant.NotConnected
200+
///
201+
/// ```no_run
202+
/// #![feature(udp_peer_addr)]
203+
/// use std::net::UdpSocket;
204+
///
205+
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
206+
/// assert_eq!(socket.peer_addr().unwrap_err().kind(),
207+
/// ::std::io::ErrorKind::NotConnected);
208+
/// ```
209+
#[unstable(feature = "udp_peer_addr", issue = "59127")]
210+
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
211+
self.0.peer_addr()
212+
}
213+
183214
/// Returns the socket address that this socket was created from.
184215
///
185216
/// # Examples
@@ -865,13 +896,23 @@ mod tests {
865896
}
866897

867898
#[test]
868-
fn socket_name_ip4() {
899+
fn socket_name() {
869900
each_ip(&mut |addr, _| {
870901
let server = t!(UdpSocket::bind(&addr));
871902
assert_eq!(addr, t!(server.local_addr()));
872903
})
873904
}
874905

906+
#[test]
907+
fn socket_peer() {
908+
each_ip(&mut |addr1, addr2| {
909+
let server = t!(UdpSocket::bind(&addr1));
910+
assert_eq!(server.peer_addr().unwrap_err().kind(), ErrorKind::NotConnected);
911+
t!(server.connect(&addr2));
912+
assert_eq!(addr2, t!(server.peer_addr()));
913+
})
914+
}
915+
875916
#[test]
876917
fn udp_clone_smoke() {
877918
each_ip(&mut |addr1, addr2| {

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

+4
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

+5
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

+4
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ impl UdpSocket {
249249
unsupported()
250250
}
251251

252+
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
253+
match self.0 {}
254+
}
255+
252256
pub fn socket_addr(&self) -> io::Result<SocketAddr> {
253257
match self.0 {}
254258
}

src/libstd/sys_common/net.rs

+6
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)