Skip to content

Commit 1501f33

Browse files
committed
Auto merge of #23711 - alexcrichton:ip-addr, r=aturon
This commits adds back an `IpAddr` enum matching the `SocketAddr` enum, but without a port. The enumeration is `#[unstable]`. The `lookup_host` function and iterator are also destabilized behind a new feature gate due to questions around the semantics of returning `SocketAddr` values.
2 parents b0fd67b + 8165bc1 commit 1501f33

File tree

5 files changed

+68
-21
lines changed

5 files changed

+68
-21
lines changed

src/libstd/net/addr.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use hash;
1515
use io;
1616
use libc::{self, socklen_t, sa_family_t};
1717
use mem;
18-
use net::{lookup_host, ntoh, hton, Ipv4Addr, Ipv6Addr};
18+
use net::{lookup_host, ntoh, hton, IpAddr, Ipv4Addr, Ipv6Addr};
1919
use option;
2020
use sys_common::{FromInner, AsInner, IntoInner};
2121
use vec;
@@ -47,6 +47,15 @@ pub struct SocketAddrV4 { inner: libc::sockaddr_in }
4747
pub struct SocketAddrV6 { inner: libc::sockaddr_in6 }
4848

4949
impl SocketAddr {
50+
/// Gets the IP address associated with this socket address.
51+
#[unstable(feature = "ip_addr", reason = "recent addition")]
52+
pub fn ip(&self) -> IpAddr {
53+
match *self {
54+
SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()),
55+
SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()),
56+
}
57+
}
58+
5059
/// Gets the port number associated with this socket address
5160
#[stable(feature = "rust1", since = "1.0.0")]
5261
pub fn port(&self) -> u16 {
@@ -333,6 +342,18 @@ impl ToSocketAddrs for SocketAddrV6 {
333342
}
334343
}
335344

345+
#[stable(feature = "rust1", since = "1.0.0")]
346+
impl ToSocketAddrs for (IpAddr, u16) {
347+
type Iter = option::IntoIter<SocketAddr>;
348+
fn to_socket_addrs(&self) -> io::Result<option::IntoIter<SocketAddr>> {
349+
let (ip, port) = *self;
350+
match ip {
351+
IpAddr::V4(ref a) => (*a, port).to_socket_addrs(),
352+
IpAddr::V6(ref a) => (*a, port).to_socket_addrs(),
353+
}
354+
}
355+
}
356+
336357
#[stable(feature = "rust1", since = "1.0.0")]
337358
impl ToSocketAddrs for (Ipv4Addr, u16) {
338359
type Iter = option::IntoIter<SocketAddr>;

src/libstd/net/ip.rs

+20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ use libc;
2121
use sys_common::{AsInner, FromInner};
2222
use net::{hton, ntoh};
2323

24+
/// An IP address, either a IPv4 or IPv6 address.
25+
#[unstable(feature = "ip_addr", reason = "recent addition")]
26+
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash, PartialOrd, Ord)]
27+
pub enum IpAddr {
28+
/// Representation of an IPv4 address.
29+
V4(Ipv4Addr),
30+
/// Representation of an IPv6 address.
31+
V6(Ipv6Addr),
32+
}
33+
2434
/// Representation of an IPv4 address.
2535
#[derive(Copy)]
2636
#[stable(feature = "rust1", since = "1.0.0")]
@@ -139,6 +149,16 @@ impl Ipv4Addr {
139149

140150
}
141151

152+
#[stable(feature = "rust1", since = "1.0.0")]
153+
impl fmt::Display for IpAddr {
154+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
155+
match *self {
156+
IpAddr::V4(ref a) => a.fmt(fmt),
157+
IpAddr::V6(ref a) => a.fmt(fmt),
158+
}
159+
}
160+
}
161+
142162
#[stable(feature = "rust1", since = "1.0.0")]
143163
impl fmt::Display for Ipv4Addr {
144164
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {

src/libstd/net/mod.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use io::{self, Error, ErrorKind};
2121
use num::Int;
2222
use sys_common::net2 as net_imp;
2323

24-
pub use self::ip::{Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
24+
pub use self::ip::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
2525
pub use self::addr::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
2626
pub use self::tcp::{TcpStream, TcpListener};
2727
pub use self::udp::UdpSocket;
@@ -74,10 +74,14 @@ fn each_addr<A: ToSocketAddrs, F, T>(addr: A, mut f: F) -> io::Result<T>
7474
}
7575

7676
/// An iterator over `SocketAddr` values returned from a host lookup operation.
77-
#[stable(feature = "rust1", since = "1.0.0")]
77+
#[unstable(feature = "lookup_host", reason = "unsure about the returned \
78+
iterator and returning socket \
79+
addresses")]
7880
pub struct LookupHost(net_imp::LookupHost);
7981

80-
#[stable(feature = "rust1", since = "1.0.0")]
82+
#[unstable(feature = "lookup_host", reason = "unsure about the returned \
83+
iterator and returning socket \
84+
addresses")]
8185
impl Iterator for LookupHost {
8286
type Item = io::Result<SocketAddr>;
8387
fn next(&mut self) -> Option<io::Result<SocketAddr>> { self.0.next() }
@@ -91,7 +95,7 @@ impl Iterator for LookupHost {
9195
/// # Examples
9296
///
9397
/// ```no_run
94-
/// # #![feature(net)]
98+
/// # #![feature(lookup_host)]
9599
/// use std::net;
96100
///
97101
/// # fn foo() -> std::io::Result<()> {
@@ -101,7 +105,9 @@ impl Iterator for LookupHost {
101105
/// # Ok(())
102106
/// # }
103107
/// ```
104-
#[stable(feature = "rust1", since = "1.0.0")]
108+
#[unstable(feature = "lookup_host", reason = "unsure about the returned \
109+
iterator and returning socket \
110+
addresses")]
105111
pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
106112
net_imp::lookup_host(host).map(LookupHost)
107113
}

src/libstd/net/udp.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use prelude::v1::*;
1515

1616
use io::{self, Error, ErrorKind};
17-
use net::{ToSocketAddrs, SocketAddr};
17+
use net::{ToSocketAddrs, SocketAddr, IpAddr};
1818
use sys_common::net2 as net_imp;
1919
use sys_common::AsInner;
2020

@@ -116,12 +116,12 @@ impl UdpSocket {
116116
}
117117

118118
/// Joins a multicast IP address (becomes a member of it)
119-
pub fn join_multicast(&self, multi: &SocketAddr) -> io::Result<()> {
119+
pub fn join_multicast(&self, multi: &IpAddr) -> io::Result<()> {
120120
self.0.join_multicast(multi)
121121
}
122122

123123
/// Leaves a multicast IP address (drops membership from it)
124-
pub fn leave_multicast(&self, multi: &SocketAddr) -> io::Result<()> {
124+
pub fn leave_multicast(&self, multi: &IpAddr) -> io::Result<()> {
125125
self.0.leave_multicast(multi)
126126
}
127127

src/libstd/sys/common/net2.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ffi::CString;
1414
use io::{self, Error, ErrorKind};
1515
use libc::{self, c_int, c_char, c_void, socklen_t};
1616
use mem;
17-
use net::{SocketAddr, Shutdown};
17+
use net::{SocketAddr, Shutdown, IpAddr};
1818
use sys::c;
1919
use sys::net::{cvt, cvt_r, cvt_gai, Socket, init, wrlen_t};
2020
use sys_common::{AsInner, FromInner, IntoInner};
@@ -334,39 +334,39 @@ impl UdpSocket {
334334
libc::IP_MULTICAST_LOOP, on as c_int)
335335
}
336336

337-
pub fn join_multicast(&self, multi: &SocketAddr) -> io::Result<()> {
337+
pub fn join_multicast(&self, multi: &IpAddr) -> io::Result<()> {
338338
match *multi {
339-
SocketAddr::V4(..) => {
339+
IpAddr::V4(..) => {
340340
self.set_membership(multi, libc::IP_ADD_MEMBERSHIP)
341341
}
342-
SocketAddr::V6(..) => {
342+
IpAddr::V6(..) => {
343343
self.set_membership(multi, libc::IPV6_ADD_MEMBERSHIP)
344344
}
345345
}
346346
}
347-
pub fn leave_multicast(&self, multi: &SocketAddr) -> io::Result<()> {
347+
pub fn leave_multicast(&self, multi: &IpAddr) -> io::Result<()> {
348348
match *multi {
349-
SocketAddr::V4(..) => {
349+
IpAddr::V4(..) => {
350350
self.set_membership(multi, libc::IP_DROP_MEMBERSHIP)
351351
}
352-
SocketAddr::V6(..) => {
352+
IpAddr::V6(..) => {
353353
self.set_membership(multi, libc::IPV6_DROP_MEMBERSHIP)
354354
}
355355
}
356356
}
357-
fn set_membership(&self, addr: &SocketAddr, opt: c_int) -> io::Result<()> {
357+
fn set_membership(&self, addr: &IpAddr, opt: c_int) -> io::Result<()> {
358358
match *addr {
359-
SocketAddr::V4(ref addr) => {
359+
IpAddr::V4(ref addr) => {
360360
let mreq = libc::ip_mreq {
361-
imr_multiaddr: *addr.ip().as_inner(),
361+
imr_multiaddr: *addr.as_inner(),
362362
// interface == INADDR_ANY
363363
imr_interface: libc::in_addr { s_addr: 0x0 },
364364
};
365365
setsockopt(&self.inner, libc::IPPROTO_IP, opt, mreq)
366366
}
367-
SocketAddr::V6(ref addr) => {
367+
IpAddr::V6(ref addr) => {
368368
let mreq = libc::ip6_mreq {
369-
ipv6mr_multiaddr: *addr.ip().as_inner(),
369+
ipv6mr_multiaddr: *addr.as_inner(),
370370
ipv6mr_interface: 0,
371371
};
372372
setsockopt(&self.inner, libc::IPPROTO_IPV6, opt, mreq)

0 commit comments

Comments
 (0)