-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Separate Ipv4Addr/Ipv6Addr types, new methods for inspecting addresses #20785
Conversation
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
7a3251b
to
dccb27b
Compare
added tests, rebased, fixed typos
|
This looks pretty awesome, thanks @ktossell! There's a few other issues you may be interested in as well:
It may be worth dealing with #15688 while rewriting the |
I was looking at #15688 and having a hard time trying to come to a conclusion about where the scope identifier should go. I'm leaning toward saying that it should be part of SockAddr, not Ipv6Addr. I'll see if I can make some progress and leave a comment over there. I'm most familiar with Python's IP address module: https://docs.python.org/3/library/ipaddress.html. I mostly copied its features. Some similar modules are https://golang.org/src/net/ip.go and http://www.rubydoc.info/gems/ipaddress/0.8.0/. I think it would also be good to have an IpNetwork (and Ipv4Network/Ipv6Network) and to extend IpAddr further, adding masking (x:IpAddr matches y:IpAddr up to the nth bit, z:IpAddr's 24-bit network address is w:IpAddr). I'm not sure IpNetwork would be totally appropriate to add to libstd by itself, since it's probably not directly required for other core features (like socket support requires IpAddr). But I think it's best to include network utilities (is-address-in-network, what-hosts-are-in-network, is-network-subnetwork-of...) when we're providing IpAddr. Without IpNetwork or comparison functions, IpAddr is much less useful. I guess I better bring all of this up in rust-lang/rfcs#576... |
This needs a rebase. |
…adding version-specific methods This adds several address inspection methods, such as `is_loopback`, `is_multicast`, and `is_private`, as well as improving formatting (It adds support for `::`-shortening addresses and fixes :: and ::1) and adding methods to convert between different IP versions. The variants `IpAddr::Ipv4Addr` and `Ipv6Addr` were limiting: There are `is_...` methods that make sense for either v4 or v6 addresses but not for both versions, and there's no way to define methods for only one variant of an enum, so the variants are promoted to independent types. The IpAddr enum now wraps the Ipv4Addr and Ipv6Addr types: enum IpAddr { V4(Ipv4Addr), V6(Ipv6Addr) } Addresses are created as: Ipv4Addr::new(1, 2, 3, 4) -> Ipv4Addr IpAddr::new_v4(1, 2, 3, 4) -> IpvAddr::V4(Ipv4Addr) or using FromStr for any of the three address types. IP addresses can be passed as their core types (IPvXAddr), as IpAddr, or using the trait ToIpAddr, which is implemented for Ipv4Addr, Ipv6Addr and IpAddr.
dccb27b
to
4d5166a
Compare
Rebased |
@steveklabnik Can you or someone else review this, please? It has been in the queue for quite a long time. |
Ping @alexcrichton (I realize I was initially assigned, but it looks like you already gave a first pass review). While this touches on IO reform, this was an area explicitly left unresolved in the RFCs, and I'm happy to land improvements here that we can carry over to the new modules. |
Sorry about being so slow on this @ktossell! At this point I've now opened an RFC for the upcoming Would you be willing to help out in writing up a bit summarizing the direction this PR takes? If not I can also try drafting up something and send it over to you for proofing! |
This commit is an implementation of [RFC 807][rfc] which adds a `std::net` module for basic neworking based on top of `std::io`. This module serves as a replacement for the `std::old_io::net` module and networking primitives in `old_io`. [rfc]: fillmein The major focus of this redesign is to cut back on the level of abstraction to the point that each of the networking types is just a bare socket. To this end functionality such as timeouts and cloning has been removed (although cloning can be done through `duplicate`, it may just yield an error). With this `net` module comes a new implementation of `SocketAddr` and `IpAddr`. This work is entirely based on rust-lang#20785 and the only changes were to alter the in-memory representation to match the `libc`-expected variants and to move from public fields to accessors.
This commit is an implementation of [RFC 807][rfc] which adds a `std::net` module for basic neworking based on top of `std::io`. This module serves as a replacement for the `std::old_io::net` module and networking primitives in `old_io`. [rfc]: fillmein The major focus of this redesign is to cut back on the level of abstraction to the point that each of the networking types is just a bare socket. To this end functionality such as timeouts and cloning has been removed (although cloning can be done through `duplicate`, it may just yield an error). With this `net` module comes a new implementation of `SocketAddr` and `IpAddr`. This work is entirely based on rust-lang#20785 and the only changes were to alter the in-memory representation to match the `libc`-expected variants and to move from public fields to accessors.
This adds several address inspection methods, such as
is_loopback
,is_multicast
, andis_private
, as well as improving formatting (It adds support for::
-shortening addresses andfixes
::
and::1
) and adding methods to convert between different IP versions.The variants
IpAddr::Ipv4Addr
andIpv6Addr
were limiting: There areis_...
methods thatmake sense for either v4 or v6 addresses but not for both versions, and there's no way to define
methods for only one variant of an enum, so the variants are promoted to independent types.
The IpAddr enum now wraps the
Ipv4Addr
andIpv6Addr
types:Addresses are created as:
or using
FromStr
for any of the three address types.IP addresses can be passed as their core types (
IPvXAddr
), asIpAddr
, or using the traitToIpAddr
, which is implemented forIpv4Addr
,Ipv6Addr
andIpAddr
.