-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 a way to parse IP addresses without having to utf8-validate #94821
Comments
Hi @jyn514, where is the utf8 validation happening? As you said, the string slice gets converted to a byte slice but then it looks like the parser just works with |
@kckeiks it happens before you ever pass the string to |
|
I don't have an array. I have a dynamically sized &[u8], and I'm not sure whether it's a valid IP address or not. |
Gotcha. If this |
@rustbot claim |
4 and 16 are misleading I think. This is a normal IP address, like "127.0.0.1", it just happens to have come from the network and not be utf8 validated. "127.0.0.1" is 9 bytes, and in general, IP addresses can be variable length. |
If the address, say "127.0.0.1", comes from the network, as inside an IPv4 header, then it must be a 32 bit value, it would be of the form |
We could add something like this and add in the docs that this parses IP addresses and assumes that they're utf8 encoded? impl<'a> TryFrom<&'a [u8]> for IpAddr {
type Error = AddrParseError;
fn try_from(b: &'a [u8]) -> Result<IpAddr, Self::Error> {
Parser::from_bytes(b).parse_with(|p| p.read_ip_addr())
}
} |
It doesn't. No part of that code requires |
I disagree. The slice of bytes must represent an IP address string in dot notation so the slice of bytes "encodes" this string; some comment should be added to the docs to explain what the slice of bytes is. For instance, this slice of bytes is bad and would cause a parse error. It doesn't contain non-utf8 byte but it doesn't encode an IP address string in dot notation.
instead it should be
This is what rust/library/std/src/net/parser.rs Line 44 in 5f4e067
|
Oh, I see what you mean - you're saying that it should be a byte string, and not an array of the ip numbers. Yes, I agree that's what the code is doing. |
So is this issue for exposing a new way to parse ip byte strings or a slice of the ip numbers? Not sure how to move forward. Thanks for working with me on this btw. |
I don't understand the question. I have |
This made me think that you were taking about an array/slice of the numbers of the ip address and not a byte string.
|
… r=joshtriplett Support parsing IP addresses from a byte string Fixes rust-lang#94821 The goal is to be able to parse addresses from a byte string without requiring to do any utf8 validation. Since internally the parser already works on byte strings, this should be possible and I personally already needed this in the past too. ~~I used the proposed approach from the issue by implementing `TryFrom<&'a [u8]>` for all 6 address types (3 ip address types and 3 socket address types). I believe implementing stable traits for stable types is insta-stable so this will probably need an FCP?~~ Switched to an unstable inherent method approach called `parse_ascii` as requested. cc `@jyn514`
… r=joshtriplett Support parsing IP addresses from a byte string Fixes rust-lang#94821 The goal is to be able to parse addresses from a byte string without requiring to do any utf8 validation. Since internally the parser already works on byte strings, this should be possible and I personally already needed this in the past too. ~~I used the proposed approach from the issue by implementing `TryFrom<&'a [u8]>` for all 6 address types (3 ip address types and 3 socket address types). I believe implementing stable traits for stable types is insta-stable so this will probably need an FCP?~~ Switched to an unstable inherent method approach called `parse_ascii` as requested. cc ``@jyn514``
… r=joshtriplett Support parsing IP addresses from a byte string Fixes rust-lang#94821 The goal is to be able to parse addresses from a byte string without requiring to do any utf8 validation. Since internally the parser already works on byte strings, this should be possible and I personally already needed this in the past too. ~~I used the proposed approach from the issue by implementing `TryFrom<&'a [u8]>` for all 6 address types (3 ip address types and 3 socket address types). I believe implementing stable traits for stable types is insta-stable so this will probably need an FCP?~~ Switched to an unstable inherent method approach called `parse_ascii` as requested. cc `@jyn514`
… r=joshtriplett Support parsing IP addresses from a byte string Fixes rust-lang#94821 The goal is to be able to parse addresses from a byte string without requiring to do any utf8 validation. Since internally the parser already works on byte strings, this should be possible and I personally already needed this in the past too. ~~I used the proposed approach from the issue by implementing `TryFrom<&'a [u8]>` for all 6 address types (3 ip address types and 3 socket address types). I believe implementing stable traits for stable types is insta-stable so this will probably need an FCP?~~ Switched to an unstable inherent method approach called `parse_ascii` as requested. cc ``@jyn514``
… r=joshtriplett Support parsing IP addresses from a byte string Fixes rust-lang#94821 The goal is to be able to parse addresses from a byte string without requiring to do any utf8 validation. Since internally the parser already works on byte strings, this should be possible and I personally already needed this in the past too. ~~I used the proposed approach from the issue by implementing `TryFrom<&'a [u8]>` for all 6 address types (3 ip address types and 3 socket address types). I believe implementing stable traits for stable types is insta-stable so this will probably need an FCP?~~ Switched to an unstable inherent method approach called `parse_ascii` as requested. cc ```@jyn514```
… r=joshtriplett Support parsing IP addresses from a byte string Fixes rust-lang#94821 The goal is to be able to parse addresses from a byte string without requiring to do any utf8 validation. Since internally the parser already works on byte strings, this should be possible and I personally already needed this in the past too. ~~I used the proposed approach from the issue by implementing `TryFrom<&'a [u8]>` for all 6 address types (3 ip address types and 3 socket address types). I believe implementing stable traits for stable types is insta-stable so this will probably need an FCP?~~ Switched to an unstable inherent method approach called `parse_ascii` as requested. cc ````@jyn514````
… r=joshtriplett Support parsing IP addresses from a byte string Fixes rust-lang#94821 The goal is to be able to parse addresses from a byte string without requiring to do any utf8 validation. Since internally the parser already works on byte strings, this should be possible and I personally already needed this in the past too. ~~I used the proposed approach from the issue by implementing `TryFrom<&'a [u8]>` for all 6 address types (3 ip address types and 3 socket address types). I believe implementing stable traits for stable types is insta-stable so this will probably need an FCP?~~ Switched to an unstable inherent method approach called `parse_ascii` as requested. cc `````@jyn514`````
The only standard way I've found to parse an IP address is
IpAddr::from_str
. That function immediately converts the string to bytes:rust/library/std/src/net/parser.rs
Line 44 in 5f4e067
It would be great to expose some way (not necessarily
Parser::new
, but something similar) that doesn't require doing unnecessary utf8 validation.@rustbot label: +T-libs +C-enhancement
The text was updated successfully, but these errors were encountered: