Skip to content

Commit a20e7ff

Browse files
authored
Rollup merge of #131790 - nmathewson:doc_socketaddr_representation, r=tgross35
Document textual format of SocketAddrV{4,6} This commit adds new "Textual representation" documentation sections to SocketAddrV4 and SocketAddrV6, by analogy to the existing "textual representation" sections of Ipv4Addr and Ipv6Addr. Rationale: Without documentation about which formats are actually accepted, it's hard for a programmer to be sure that their code will actually behave as expected when implementing protocols that require support (or rejection) for particular representations. This lack of clarity can in turn can lead to ambiguities and security problems like those discussed in RFC 6942. (I've tried to describe the governing RFCs or standards where I could, but it's possible that the actual implementers had something else in mind. I could not find any standards that corresponded _exactly_ to the one implemented in SocketAddrv6, but I have linked the relevant documents that I could find.)
2 parents f61306d + 0e5c5a2 commit a20e7ff

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

library/core/src/net/socket_addr.rs

+39
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ pub enum SocketAddr {
4949
/// [IETF RFC 793]: https://tools.ietf.org/html/rfc793
5050
/// [`IPv4` address]: Ipv4Addr
5151
///
52+
/// # Textual representation
53+
///
54+
/// `SocketAddrV4` provides a [`FromStr`](crate::str::FromStr) implementation.
55+
/// It accepts an IPv4 address in its [textual representation], followed by a
56+
/// single `:`, followed by the port encoded as a decimal integer. Other
57+
/// formats are not accepted.
58+
///
59+
/// [textual representation]: Ipv4Addr#textual-representation
60+
///
5261
/// # Examples
5362
///
5463
/// ```
@@ -82,6 +91,32 @@ pub struct SocketAddrV4 {
8291
/// [IETF RFC 2553, Section 3.3]: https://tools.ietf.org/html/rfc2553#section-3.3
8392
/// [`IPv6` address]: Ipv6Addr
8493
///
94+
/// # Textual representation
95+
///
96+
/// `SocketAddrV6` provides a [`FromStr`](crate::str::FromStr) implementation,
97+
/// based on the bracketed format recommended by [IETF RFC 5952],
98+
/// with scope identifiers based on those specified in [IETF RFC 4007].
99+
///
100+
/// It accepts addresses consisting of the following elements, in order:
101+
/// - A left square bracket (`[`)
102+
/// - The [textual representation] of an IPv6 address
103+
/// - _Optionally_, a percent sign (`%`) followed by the scope identifier
104+
/// encoded as a decimal integer
105+
/// - A right square bracket (`]`)
106+
/// - A colon (`:`)
107+
/// - The port, encoded as a decimal integer.
108+
///
109+
/// For example, the string `[2001:db8::413]:443` represents a `SocketAddrV6`
110+
/// with the address `2001:db8::413` and port `443`. The string
111+
/// `[2001:db8::413%612]:443` represents the same address and port, with a
112+
/// scope identifier of `612`.
113+
///
114+
/// Other formats are not accepted.
115+
///
116+
/// [IETF RFC 5952]: https://tools.ietf.org/html/rfc5952#section-6
117+
/// [IETF RFC 4007]: https://tools.ietf.org/html/rfc4007#section-11
118+
/// [textual representation]: Ipv6Addr#textual-representation
119+
///
85120
/// # Examples
86121
///
87122
/// ```
@@ -92,6 +127,10 @@ pub struct SocketAddrV4 {
92127
/// assert_eq!("[2001:db8::1]:8080".parse(), Ok(socket));
93128
/// assert_eq!(socket.ip(), &Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1));
94129
/// assert_eq!(socket.port(), 8080);
130+
///
131+
/// let mut with_scope = socket.clone();
132+
/// with_scope.set_scope_id(3);
133+
/// assert_eq!("[2001:db8::1%3]:8080".parse(), Ok(with_scope));
95134
/// ```
96135
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
97136
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)