-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add octet-oriented interface to std::net::Ipv6Addr #1498
Conversation
I’d call the methods As far as I understand, in English, the word octet is used to be explicit about having 8 bits in contexts where a byte may have a different length. In the Rust standard library however, there is widespread precedent of the word "bytes" being used to designate Other than this naming bikeshed, +1 on the feature. |
I agree with @SimonSapin. It's pretty annoying to work with IPv6 addresses right now in comparison to IPv4 ones: https://github.com/sfackler/rust-socks/blob/master/src/v5.rs#L12-L29 |
@SimonSapin I see your point about "bytes," but note that |
Ah, ok. Given |
🔔 This RFC is now entering its week-long final comment period 🔔 |
I personally like the motivation for this RFC and I think we should definitely have these methods. Some nitpicks about the API I might have are:
|
@alexcrichton on the contrary, for use cases that involve getting IP addresses out of a network protocol, it's very reasonable to get a let mut buf = [0; 16];
try!(reader.read_all(&mut buf));
let ip = Ipv6Addr::from_octets(&buf); vs let ip = Ipv6Addr::from_octets(try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8()),
try!(reader.read_u8())); |
Agreed with @sfackler - I have not had trouble getting a |
Hm ok, if you end up having |
That'd work as well. I currently use the |
I suppose while we're at it we could also add |
It's a bit easier to get at a |
I think vital information is missing: What byte-order should get exposed: The host byte order (little endian most of the time) or network byte order (big endian)? (If that's already in the RFC, then I can't find it and it should probably be made more visible.) |
I'm afraid I don't understand your question - the interface exposes an array of 16 single bytes, so there isn't any question of byte order. If the interface exposed an array of 8 16-bit integers, or a single 128-bit integer (if Rust had such a type), then the byte order of those integers would need to be specified, but that's not the case here. |
It should be the same order as |
@alexcrichton @sfackler I like your suggestions of adding an I think I'll also change |
@AGWA yeah it's fine to just push an update to the RFC. |
@AGWA It's interesting in which order the bytes are, for an address starting with |
For an address starting with Starting with |
Sorry if it is clear in what order the bytes are, however it's not that clear to me. For me, an IPv6 address is composed of eight unsigned 16-bit integers. Now my question was whether these 16-bit integers are themselves in big-endian (as they're sent over the network) or little-endian order (how most hosts represent 16-bit integers). |
https://tools.ietf.org/html/rfc2460 only talks about IPv6 addresses being made of 128 bits. The grouping by 16 bits only exists for the purpose of hex serialization. |
Thank you, that clears it up! |
* `octets` no longer returns a reference, due to bad experiences with returning internal references to libc structures in the past. * Replace `from_octets` with an implementation of `From<[u8; 16]>` for `Ipv6Addr`. * For consistency, also implement `From<[u8; 4]>` for `Ipv4Addr`.
I just pushed an update to the RFC that reflects the feedback so far. |
Looks good to me, thanks @AGWA! |
The libs team discussed this RFC during triage yesterday and the conclusion was to merge. It was brought up that it's relatively odd to consume fixed-size arrays in the standard library, but as pointed out by @sfackler this is tenable even today in some situations. It was also brought up that we would likely inevitably want this functionality regardless as it's just an extra implementation of Tracking issue: rust-lang/rust#32313 |
Ah and of course, thanks again for the RFC @AGWA! |
Add an impl for From trait Converts a u8 slice to a Ipv4Addr More discussion on this here: rust-lang/rfcs#1498 (comment)
Summary: Add constructor and getter functions to
std::net::Ipv6Addr
that are oriented around octets.Rendered