Skip to content

Commit 49ade22

Browse files
committed
Parse SocketAddrV6::scope_id
1 parent a093957 commit 49ade22

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

library/std/src/net/parser.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ macro_rules! impl_helper {
3232
})*)
3333
}
3434

35-
impl_helper! { u8 u16 }
35+
impl_helper! { u8 u16 u32 }
3636

3737
struct Parser<'a> {
3838
// parsing as ASCII, so can use byte array
@@ -219,6 +219,14 @@ impl<'a> Parser<'a> {
219219
})
220220
}
221221

222+
/// Read a % followed by a scope id in base 10.
223+
fn read_scope_id(&mut self) -> Option<u32> {
224+
self.read_atomically(|p| {
225+
p.read_given_char('%')?;
226+
p.read_number(10, None)
227+
})
228+
}
229+
222230
/// Read an IPV4 address with a port
223231
fn read_socket_addr_v4(&mut self) -> Option<SocketAddrV4> {
224232
self.read_atomically(|p| {
@@ -233,10 +241,11 @@ impl<'a> Parser<'a> {
233241
self.read_atomically(|p| {
234242
p.read_given_char('[')?;
235243
let ip = p.read_ipv6_addr()?;
244+
let scope_id = p.read_scope_id().unwrap_or(0);
236245
p.read_given_char(']')?;
237246

238247
let port = p.read_port()?;
239-
Some(SocketAddrV6::new(ip, port, 0, 0))
248+
Some(SocketAddrV6::new(ip, port, 0, scope_id))
240249
})
241250
}
242251

library/std/src/net/parser/tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAdd
33
use crate::str::FromStr;
44

55
const PORT: u16 = 8080;
6+
const SCOPE_ID: u32 = 1337;
67

78
const IPV4: Ipv4Addr = Ipv4Addr::new(192, 168, 0, 1);
89
const IPV4_STR: &str = "192.168.0.1";
@@ -13,6 +14,7 @@ const IPV6_STR_FULL: &str = "2001:db8:0:0:0:0:c0a8:1";
1314
const IPV6_STR_COMPRESS: &str = "2001:db8::c0a8:1";
1415
const IPV6_STR_V4: &str = "2001:db8::192.168.0.1";
1516
const IPV6_STR_PORT: &str = "[2001:db8::c0a8:1]:8080";
17+
const IPV6_STR_PORT_SCOPE_ID: &str = "[2001:db8::c0a8:1%1337]:8080";
1618

1719
#[test]
1820
fn parse_ipv4() {
@@ -74,8 +76,8 @@ fn parse_socket_v4() {
7476

7577
#[test]
7678
fn parse_socket_v6() {
77-
let result: SocketAddrV6 = IPV6_STR_PORT.parse().unwrap();
78-
assert_eq!(result, SocketAddrV6::new(IPV6, PORT, 0, 0));
79+
assert_eq!(IPV6_STR_PORT.parse(), Ok(SocketAddrV6::new(IPV6, PORT, 0, 0)));
80+
assert_eq!(IPV6_STR_PORT_SCOPE_ID.parse(), Ok(SocketAddrV6::new(IPV6, PORT, 0, SCOPE_ID)));
7981

8082
assert!(SocketAddrV6::from_str(IPV4_STR).is_err());
8183
assert!(SocketAddrV6::from_str(IPV4_STR_PORT).is_err());

0 commit comments

Comments
 (0)