1 | use crate::cmp::Ordering; |
2 | use crate::fmt::{self, Write}; |
3 | use crate::iter; |
4 | use crate::mem::transmute; |
5 | use crate::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not}; |
6 | |
7 | use super::display_buffer::DisplayBuffer; |
8 | |
9 | /// An IP address, either IPv4 or IPv6. |
10 | /// |
11 | /// This enum can contain either an [`Ipv4Addr`] or an [`Ipv6Addr`], see their |
12 | /// respective documentation for more details. |
13 | /// |
14 | /// # Examples |
15 | /// |
16 | /// ``` |
17 | /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
18 | /// |
19 | /// let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)); |
20 | /// let localhost_v6 = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); |
21 | /// |
22 | /// assert_eq!("127.0.0.1" .parse(), Ok(localhost_v4)); |
23 | /// assert_eq!("::1" .parse(), Ok(localhost_v6)); |
24 | /// |
25 | /// assert_eq!(localhost_v4.is_ipv6(), false); |
26 | /// assert_eq!(localhost_v4.is_ipv4(), true); |
27 | /// ``` |
28 | #[cfg_attr (not(test), rustc_diagnostic_item = "IpAddr" )] |
29 | #[stable (feature = "ip_addr" , since = "1.7.0" )] |
30 | #[derive (Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)] |
31 | pub enum IpAddr { |
32 | /// An IPv4 address. |
33 | #[stable (feature = "ip_addr" , since = "1.7.0" )] |
34 | V4(#[stable (feature = "ip_addr" , since = "1.7.0" )] Ipv4Addr), |
35 | /// An IPv6 address. |
36 | #[stable (feature = "ip_addr" , since = "1.7.0" )] |
37 | V6(#[stable (feature = "ip_addr" , since = "1.7.0" )] Ipv6Addr), |
38 | } |
39 | |
40 | /// An IPv4 address. |
41 | /// |
42 | /// IPv4 addresses are defined as 32-bit integers in [IETF RFC 791]. |
43 | /// They are usually represented as four octets. |
44 | /// |
45 | /// See [`IpAddr`] for a type encompassing both IPv4 and IPv6 addresses. |
46 | /// |
47 | /// [IETF RFC 791]: https://tools.ietf.org/html/rfc791 |
48 | /// |
49 | /// # Textual representation |
50 | /// |
51 | /// `Ipv4Addr` provides a [`FromStr`] implementation. The four octets are in decimal |
52 | /// notation, divided by `.` (this is called "dot-decimal notation"). |
53 | /// Notably, octal numbers (which are indicated with a leading `0`) and hexadecimal numbers (which |
54 | /// are indicated with a leading `0x`) are not allowed per [IETF RFC 6943]. |
55 | /// |
56 | /// [IETF RFC 6943]: https://tools.ietf.org/html/rfc6943#section-3.1.1 |
57 | /// [`FromStr`]: crate::str::FromStr |
58 | /// |
59 | /// # Examples |
60 | /// |
61 | /// ``` |
62 | /// use std::net::Ipv4Addr; |
63 | /// |
64 | /// let localhost = Ipv4Addr::new(127, 0, 0, 1); |
65 | /// assert_eq!("127.0.0.1" .parse(), Ok(localhost)); |
66 | /// assert_eq!(localhost.is_loopback(), true); |
67 | /// assert!("012.004.002.000" .parse::<Ipv4Addr>().is_err()); // all octets are in octal |
68 | /// assert!("0000000.0.0.0" .parse::<Ipv4Addr>().is_err()); // first octet is a zero in octal |
69 | /// assert!("0xcb.0x0.0x71.0x00" .parse::<Ipv4Addr>().is_err()); // all octets are in hex |
70 | /// ``` |
71 | #[derive (Copy, Clone, PartialEq, Eq, Hash)] |
72 | #[stable (feature = "rust1" , since = "1.0.0" )] |
73 | pub struct Ipv4Addr { |
74 | octets: [u8; 4], |
75 | } |
76 | |
77 | /// An IPv6 address. |
78 | /// |
79 | /// IPv6 addresses are defined as 128-bit integers in [IETF RFC 4291]. |
80 | /// They are usually represented as eight 16-bit segments. |
81 | /// |
82 | /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291 |
83 | /// |
84 | /// # Embedding IPv4 Addresses |
85 | /// |
86 | /// See [`IpAddr`] for a type encompassing both IPv4 and IPv6 addresses. |
87 | /// |
88 | /// To assist in the transition from IPv4 to IPv6 two types of IPv6 addresses that embed an IPv4 address were defined: |
89 | /// IPv4-compatible and IPv4-mapped addresses. Of these IPv4-compatible addresses have been officially deprecated. |
90 | /// |
91 | /// Both types of addresses are not assigned any special meaning by this implementation, |
92 | /// other than what the relevant standards prescribe. This means that an address like `::ffff:127.0.0.1`, |
93 | /// while representing an IPv4 loopback address, is not itself an IPv6 loopback address; only `::1` is. |
94 | /// To handle these so called "IPv4-in-IPv6" addresses, they have to first be converted to their canonical IPv4 address. |
95 | /// |
96 | /// ### IPv4-Compatible IPv6 Addresses |
97 | /// |
98 | /// IPv4-compatible IPv6 addresses are defined in [IETF RFC 4291 Section 2.5.5.1], and have been officially deprecated. |
99 | /// The RFC describes the format of an "IPv4-Compatible IPv6 address" as follows: |
100 | /// |
101 | /// ```text |
102 | /// | 80 bits | 16 | 32 bits | |
103 | /// +--------------------------------------+--------------------------+ |
104 | /// |0000..............................0000|0000| IPv4 address | |
105 | /// +--------------------------------------+----+---------------------+ |
106 | /// ``` |
107 | /// So `::a.b.c.d` would be an IPv4-compatible IPv6 address representing the IPv4 address `a.b.c.d`. |
108 | /// |
109 | /// To convert from an IPv4 address to an IPv4-compatible IPv6 address, use [`Ipv4Addr::to_ipv6_compatible`]. |
110 | /// Use [`Ipv6Addr::to_ipv4`] to convert an IPv4-compatible IPv6 address to the canonical IPv4 address. |
111 | /// |
112 | /// [IETF RFC 4291 Section 2.5.5.1]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.5.1 |
113 | /// |
114 | /// ### IPv4-Mapped IPv6 Addresses |
115 | /// |
116 | /// IPv4-mapped IPv6 addresses are defined in [IETF RFC 4291 Section 2.5.5.2]. |
117 | /// The RFC describes the format of an "IPv4-Mapped IPv6 address" as follows: |
118 | /// |
119 | /// ```text |
120 | /// | 80 bits | 16 | 32 bits | |
121 | /// +--------------------------------------+--------------------------+ |
122 | /// |0000..............................0000|FFFF| IPv4 address | |
123 | /// +--------------------------------------+----+---------------------+ |
124 | /// ``` |
125 | /// So `::ffff:a.b.c.d` would be an IPv4-mapped IPv6 address representing the IPv4 address `a.b.c.d`. |
126 | /// |
127 | /// To convert from an IPv4 address to an IPv4-mapped IPv6 address, use [`Ipv4Addr::to_ipv6_mapped`]. |
128 | /// Use [`Ipv6Addr::to_ipv4`] to convert an IPv4-mapped IPv6 address to the canonical IPv4 address. |
129 | /// Note that this will also convert the IPv6 loopback address `::1` to `0.0.0.1`. Use |
130 | /// [`Ipv6Addr::to_ipv4_mapped`] to avoid this. |
131 | /// |
132 | /// [IETF RFC 4291 Section 2.5.5.2]: https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.5.2 |
133 | /// |
134 | /// # Textual representation |
135 | /// |
136 | /// `Ipv6Addr` provides a [`FromStr`] implementation. There are many ways to represent |
137 | /// an IPv6 address in text, but in general, each segments is written in hexadecimal |
138 | /// notation, and segments are separated by `:`. For more information, see |
139 | /// [IETF RFC 5952]. |
140 | /// |
141 | /// [`FromStr`]: crate::str::FromStr |
142 | /// [IETF RFC 5952]: https://tools.ietf.org/html/rfc5952 |
143 | /// |
144 | /// # Examples |
145 | /// |
146 | /// ``` |
147 | /// use std::net::Ipv6Addr; |
148 | /// |
149 | /// let localhost = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1); |
150 | /// assert_eq!("::1" .parse(), Ok(localhost)); |
151 | /// assert_eq!(localhost.is_loopback(), true); |
152 | /// ``` |
153 | #[derive (Copy, Clone, PartialEq, Eq, Hash)] |
154 | #[stable (feature = "rust1" , since = "1.0.0" )] |
155 | pub struct Ipv6Addr { |
156 | octets: [u8; 16], |
157 | } |
158 | |
159 | /// Scope of an [IPv6 multicast address] as defined in [IETF RFC 7346 section 2]. |
160 | /// |
161 | /// # Stability Guarantees |
162 | /// |
163 | /// Not all possible values for a multicast scope have been assigned. |
164 | /// Future RFCs may introduce new scopes, which will be added as variants to this enum; |
165 | /// because of this the enum is marked as `#[non_exhaustive]`. |
166 | /// |
167 | /// # Examples |
168 | /// ``` |
169 | /// #![feature(ip)] |
170 | /// |
171 | /// use std::net::Ipv6Addr; |
172 | /// use std::net::Ipv6MulticastScope::*; |
173 | /// |
174 | /// // An IPv6 multicast address with global scope (`ff0e::`). |
175 | /// let address = Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0); |
176 | /// |
177 | /// // Will print "Global scope". |
178 | /// match address.multicast_scope() { |
179 | /// Some(InterfaceLocal) => println!("Interface-Local scope" ), |
180 | /// Some(LinkLocal) => println!("Link-Local scope" ), |
181 | /// Some(RealmLocal) => println!("Realm-Local scope" ), |
182 | /// Some(AdminLocal) => println!("Admin-Local scope" ), |
183 | /// Some(SiteLocal) => println!("Site-Local scope" ), |
184 | /// Some(OrganizationLocal) => println!("Organization-Local scope" ), |
185 | /// Some(Global) => println!("Global scope" ), |
186 | /// Some(_) => println!("Unknown scope" ), |
187 | /// None => println!("Not a multicast address!" ) |
188 | /// } |
189 | /// |
190 | /// ``` |
191 | /// |
192 | /// [IPv6 multicast address]: Ipv6Addr |
193 | /// [IETF RFC 7346 section 2]: https://tools.ietf.org/html/rfc7346#section-2 |
194 | #[derive (Copy, PartialEq, Eq, Clone, Hash, Debug)] |
195 | #[unstable (feature = "ip" , issue = "27709" )] |
196 | #[non_exhaustive ] |
197 | pub enum Ipv6MulticastScope { |
198 | /// Interface-Local scope. |
199 | InterfaceLocal, |
200 | /// Link-Local scope. |
201 | LinkLocal, |
202 | /// Realm-Local scope. |
203 | RealmLocal, |
204 | /// Admin-Local scope. |
205 | AdminLocal, |
206 | /// Site-Local scope. |
207 | SiteLocal, |
208 | /// Organization-Local scope. |
209 | OrganizationLocal, |
210 | /// Global scope. |
211 | Global, |
212 | } |
213 | |
214 | impl IpAddr { |
215 | /// Returns [`true`] for the special 'unspecified' address. |
216 | /// |
217 | /// See the documentation for [`Ipv4Addr::is_unspecified()`] and |
218 | /// [`Ipv6Addr::is_unspecified()`] for more details. |
219 | /// |
220 | /// # Examples |
221 | /// |
222 | /// ``` |
223 | /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
224 | /// |
225 | /// assert_eq!(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)).is_unspecified(), true); |
226 | /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).is_unspecified(), true); |
227 | /// ``` |
228 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
229 | #[stable (feature = "ip_shared" , since = "1.12.0" )] |
230 | #[must_use ] |
231 | #[inline ] |
232 | pub const fn is_unspecified(&self) -> bool { |
233 | match self { |
234 | IpAddr::V4(ip) => ip.is_unspecified(), |
235 | IpAddr::V6(ip) => ip.is_unspecified(), |
236 | } |
237 | } |
238 | |
239 | /// Returns [`true`] if this is a loopback address. |
240 | /// |
241 | /// See the documentation for [`Ipv4Addr::is_loopback()`] and |
242 | /// [`Ipv6Addr::is_loopback()`] for more details. |
243 | /// |
244 | /// # Examples |
245 | /// |
246 | /// ``` |
247 | /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
248 | /// |
249 | /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).is_loopback(), true); |
250 | /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)).is_loopback(), true); |
251 | /// ``` |
252 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
253 | #[stable (feature = "ip_shared" , since = "1.12.0" )] |
254 | #[must_use ] |
255 | #[inline ] |
256 | pub const fn is_loopback(&self) -> bool { |
257 | match self { |
258 | IpAddr::V4(ip) => ip.is_loopback(), |
259 | IpAddr::V6(ip) => ip.is_loopback(), |
260 | } |
261 | } |
262 | |
263 | /// Returns [`true`] if the address appears to be globally routable. |
264 | /// |
265 | /// See the documentation for [`Ipv4Addr::is_global()`] and |
266 | /// [`Ipv6Addr::is_global()`] for more details. |
267 | /// |
268 | /// # Examples |
269 | /// |
270 | /// ``` |
271 | /// #![feature(ip)] |
272 | /// |
273 | /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
274 | /// |
275 | /// assert_eq!(IpAddr::V4(Ipv4Addr::new(80, 9, 12, 3)).is_global(), true); |
276 | /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1)).is_global(), true); |
277 | /// ``` |
278 | #[rustc_const_unstable (feature = "const_ip" , issue = "76205" )] |
279 | #[unstable (feature = "ip" , issue = "27709" )] |
280 | #[must_use ] |
281 | #[inline ] |
282 | pub const fn is_global(&self) -> bool { |
283 | match self { |
284 | IpAddr::V4(ip) => ip.is_global(), |
285 | IpAddr::V6(ip) => ip.is_global(), |
286 | } |
287 | } |
288 | |
289 | /// Returns [`true`] if this is a multicast address. |
290 | /// |
291 | /// See the documentation for [`Ipv4Addr::is_multicast()`] and |
292 | /// [`Ipv6Addr::is_multicast()`] for more details. |
293 | /// |
294 | /// # Examples |
295 | /// |
296 | /// ``` |
297 | /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
298 | /// |
299 | /// assert_eq!(IpAddr::V4(Ipv4Addr::new(224, 254, 0, 0)).is_multicast(), true); |
300 | /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0)).is_multicast(), true); |
301 | /// ``` |
302 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
303 | #[stable (feature = "ip_shared" , since = "1.12.0" )] |
304 | #[must_use ] |
305 | #[inline ] |
306 | pub const fn is_multicast(&self) -> bool { |
307 | match self { |
308 | IpAddr::V4(ip) => ip.is_multicast(), |
309 | IpAddr::V6(ip) => ip.is_multicast(), |
310 | } |
311 | } |
312 | |
313 | /// Returns [`true`] if this address is in a range designated for documentation. |
314 | /// |
315 | /// See the documentation for [`Ipv4Addr::is_documentation()`] and |
316 | /// [`Ipv6Addr::is_documentation()`] for more details. |
317 | /// |
318 | /// # Examples |
319 | /// |
320 | /// ``` |
321 | /// #![feature(ip)] |
322 | /// |
323 | /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
324 | /// |
325 | /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_documentation(), true); |
326 | /// assert_eq!( |
327 | /// IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_documentation(), |
328 | /// true |
329 | /// ); |
330 | /// ``` |
331 | #[rustc_const_unstable (feature = "const_ip" , issue = "76205" )] |
332 | #[unstable (feature = "ip" , issue = "27709" )] |
333 | #[must_use ] |
334 | #[inline ] |
335 | pub const fn is_documentation(&self) -> bool { |
336 | match self { |
337 | IpAddr::V4(ip) => ip.is_documentation(), |
338 | IpAddr::V6(ip) => ip.is_documentation(), |
339 | } |
340 | } |
341 | |
342 | /// Returns [`true`] if this address is in a range designated for benchmarking. |
343 | /// |
344 | /// See the documentation for [`Ipv4Addr::is_benchmarking()`] and |
345 | /// [`Ipv6Addr::is_benchmarking()`] for more details. |
346 | /// |
347 | /// # Examples |
348 | /// |
349 | /// ``` |
350 | /// #![feature(ip)] |
351 | /// |
352 | /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
353 | /// |
354 | /// assert_eq!(IpAddr::V4(Ipv4Addr::new(198, 19, 255, 255)).is_benchmarking(), true); |
355 | /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0x2, 0, 0, 0, 0, 0, 0)).is_benchmarking(), true); |
356 | /// ``` |
357 | #[unstable (feature = "ip" , issue = "27709" )] |
358 | #[must_use ] |
359 | #[inline ] |
360 | pub const fn is_benchmarking(&self) -> bool { |
361 | match self { |
362 | IpAddr::V4(ip) => ip.is_benchmarking(), |
363 | IpAddr::V6(ip) => ip.is_benchmarking(), |
364 | } |
365 | } |
366 | |
367 | /// Returns [`true`] if this address is an [`IPv4` address], and [`false`] |
368 | /// otherwise. |
369 | /// |
370 | /// [`IPv4` address]: IpAddr::V4 |
371 | /// |
372 | /// # Examples |
373 | /// |
374 | /// ``` |
375 | /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
376 | /// |
377 | /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv4(), true); |
378 | /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv4(), false); |
379 | /// ``` |
380 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
381 | #[stable (feature = "ipaddr_checker" , since = "1.16.0" )] |
382 | #[must_use ] |
383 | #[inline ] |
384 | pub const fn is_ipv4(&self) -> bool { |
385 | matches!(self, IpAddr::V4(_)) |
386 | } |
387 | |
388 | /// Returns [`true`] if this address is an [`IPv6` address], and [`false`] |
389 | /// otherwise. |
390 | /// |
391 | /// [`IPv6` address]: IpAddr::V6 |
392 | /// |
393 | /// # Examples |
394 | /// |
395 | /// ``` |
396 | /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
397 | /// |
398 | /// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv6(), false); |
399 | /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv6(), true); |
400 | /// ``` |
401 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
402 | #[stable (feature = "ipaddr_checker" , since = "1.16.0" )] |
403 | #[must_use ] |
404 | #[inline ] |
405 | pub const fn is_ipv6(&self) -> bool { |
406 | matches!(self, IpAddr::V6(_)) |
407 | } |
408 | |
409 | /// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped IPv6 addresses, otherwise it |
410 | /// returns `self` as-is. |
411 | /// |
412 | /// # Examples |
413 | /// |
414 | /// ``` |
415 | /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
416 | /// |
417 | /// let localhost_v4 = Ipv4Addr::new(127, 0, 0, 1); |
418 | /// |
419 | /// assert_eq!(IpAddr::V4(localhost_v4).to_canonical(), localhost_v4); |
420 | /// assert_eq!(IpAddr::V6(localhost_v4.to_ipv6_mapped()).to_canonical(), localhost_v4); |
421 | /// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).to_canonical().is_loopback(), true); |
422 | /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).is_loopback(), false); |
423 | /// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).to_canonical().is_loopback(), true); |
424 | /// ``` |
425 | #[inline ] |
426 | #[must_use = "this returns the result of the operation, \ |
427 | without modifying the original" ] |
428 | #[stable (feature = "ip_to_canonical" , since = "1.75.0" )] |
429 | #[rustc_const_stable (feature = "ip_to_canonical" , since = "1.75.0" )] |
430 | pub const fn to_canonical(&self) -> IpAddr { |
431 | match self { |
432 | IpAddr::V4(_) => *self, |
433 | IpAddr::V6(v6) => v6.to_canonical(), |
434 | } |
435 | } |
436 | } |
437 | |
438 | impl Ipv4Addr { |
439 | /// Creates a new IPv4 address from four eight-bit octets. |
440 | /// |
441 | /// The result will represent the IP address `a`.`b`.`c`.`d`. |
442 | /// |
443 | /// # Examples |
444 | /// |
445 | /// ``` |
446 | /// use std::net::Ipv4Addr; |
447 | /// |
448 | /// let addr = Ipv4Addr::new(127, 0, 0, 1); |
449 | /// ``` |
450 | #[rustc_const_stable (feature = "const_ip_32" , since = "1.32.0" )] |
451 | #[stable (feature = "rust1" , since = "1.0.0" )] |
452 | #[must_use ] |
453 | #[inline ] |
454 | pub const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4Addr { |
455 | Ipv4Addr { octets: [a, b, c, d] } |
456 | } |
457 | |
458 | /// The size of an IPv4 address in bits. |
459 | /// |
460 | /// # Examples |
461 | /// |
462 | /// ``` |
463 | /// #![feature(ip_bits)] |
464 | /// use std::net::Ipv4Addr; |
465 | /// |
466 | /// assert_eq!(Ipv4Addr::BITS, 32); |
467 | /// ``` |
468 | #[unstable (feature = "ip_bits" , issue = "113744" )] |
469 | pub const BITS: u32 = 32; |
470 | |
471 | /// Converts an IPv4 address into a `u32` representation using native byte order. |
472 | /// |
473 | /// Although IPv4 addresses are big-endian, the `u32` value will use the target platform's |
474 | /// native byte order. That is, the `u32` value is an integer representation of the IPv4 |
475 | /// address and not an integer interpretation of the IPv4 address's big-endian bitstring. This |
476 | /// means that the `u32` value masked with `0xffffff00` will set the last octet in the address |
477 | /// to 0, regardless of the target platform's endianness. |
478 | /// |
479 | /// # Examples |
480 | /// |
481 | /// ``` |
482 | /// #![feature(ip_bits)] |
483 | /// use std::net::Ipv4Addr; |
484 | /// |
485 | /// let addr = Ipv4Addr::new(0x12, 0x34, 0x56, 0x78); |
486 | /// assert_eq!(0x12345678, addr.to_bits()); |
487 | /// ``` |
488 | /// |
489 | /// ``` |
490 | /// #![feature(ip_bits)] |
491 | /// use std::net::Ipv4Addr; |
492 | /// |
493 | /// let addr = Ipv4Addr::new(0x12, 0x34, 0x56, 0x78); |
494 | /// let addr_bits = addr.to_bits() & 0xffffff00; |
495 | /// assert_eq!(Ipv4Addr::new(0x12, 0x34, 0x56, 0x00), Ipv4Addr::from_bits(addr_bits)); |
496 | /// |
497 | /// ``` |
498 | #[rustc_const_unstable (feature = "ip_bits" , issue = "113744" )] |
499 | #[unstable (feature = "ip_bits" , issue = "113744" )] |
500 | #[must_use ] |
501 | #[inline ] |
502 | pub const fn to_bits(self) -> u32 { |
503 | u32::from_be_bytes(self.octets) |
504 | } |
505 | |
506 | /// Converts a native byte order `u32` into an IPv4 address. |
507 | /// |
508 | /// See [`Ipv4Addr::to_bits`] for an explanation on endianness. |
509 | /// |
510 | /// # Examples |
511 | /// |
512 | /// ``` |
513 | /// #![feature(ip_bits)] |
514 | /// use std::net::Ipv4Addr; |
515 | /// |
516 | /// let addr = Ipv4Addr::from(0x12345678); |
517 | /// assert_eq!(Ipv4Addr::new(0x12, 0x34, 0x56, 0x78), addr); |
518 | /// ``` |
519 | #[rustc_const_unstable (feature = "ip_bits" , issue = "113744" )] |
520 | #[unstable (feature = "ip_bits" , issue = "113744" )] |
521 | #[must_use ] |
522 | #[inline ] |
523 | pub const fn from_bits(bits: u32) -> Ipv4Addr { |
524 | Ipv4Addr { octets: bits.to_be_bytes() } |
525 | } |
526 | |
527 | /// An IPv4 address with the address pointing to localhost: `127.0.0.1` |
528 | /// |
529 | /// # Examples |
530 | /// |
531 | /// ``` |
532 | /// use std::net::Ipv4Addr; |
533 | /// |
534 | /// let addr = Ipv4Addr::LOCALHOST; |
535 | /// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1)); |
536 | /// ``` |
537 | #[stable (feature = "ip_constructors" , since = "1.30.0" )] |
538 | pub const LOCALHOST: Self = Ipv4Addr::new(127, 0, 0, 1); |
539 | |
540 | /// An IPv4 address representing an unspecified address: `0.0.0.0` |
541 | /// |
542 | /// This corresponds to the constant `INADDR_ANY` in other languages. |
543 | /// |
544 | /// # Examples |
545 | /// |
546 | /// ``` |
547 | /// use std::net::Ipv4Addr; |
548 | /// |
549 | /// let addr = Ipv4Addr::UNSPECIFIED; |
550 | /// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0)); |
551 | /// ``` |
552 | #[doc (alias = "INADDR_ANY" )] |
553 | #[stable (feature = "ip_constructors" , since = "1.30.0" )] |
554 | pub const UNSPECIFIED: Self = Ipv4Addr::new(0, 0, 0, 0); |
555 | |
556 | /// An IPv4 address representing the broadcast address: `255.255.255.255` |
557 | /// |
558 | /// # Examples |
559 | /// |
560 | /// ``` |
561 | /// use std::net::Ipv4Addr; |
562 | /// |
563 | /// let addr = Ipv4Addr::BROADCAST; |
564 | /// assert_eq!(addr, Ipv4Addr::new(255, 255, 255, 255)); |
565 | /// ``` |
566 | #[stable (feature = "ip_constructors" , since = "1.30.0" )] |
567 | pub const BROADCAST: Self = Ipv4Addr::new(255, 255, 255, 255); |
568 | |
569 | /// Returns the four eight-bit integers that make up this address. |
570 | /// |
571 | /// # Examples |
572 | /// |
573 | /// ``` |
574 | /// use std::net::Ipv4Addr; |
575 | /// |
576 | /// let addr = Ipv4Addr::new(127, 0, 0, 1); |
577 | /// assert_eq!(addr.octets(), [127, 0, 0, 1]); |
578 | /// ``` |
579 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
580 | #[stable (feature = "rust1" , since = "1.0.0" )] |
581 | #[must_use ] |
582 | #[inline ] |
583 | pub const fn octets(&self) -> [u8; 4] { |
584 | self.octets |
585 | } |
586 | |
587 | /// Returns [`true`] for the special 'unspecified' address (`0.0.0.0`). |
588 | /// |
589 | /// This property is defined in _UNIX Network Programming, Second Edition_, |
590 | /// W. Richard Stevens, p. 891; see also [ip7]. |
591 | /// |
592 | /// [ip7]: https://man7.org/linux/man-pages/man7/ip.7.html |
593 | /// |
594 | /// # Examples |
595 | /// |
596 | /// ``` |
597 | /// use std::net::Ipv4Addr; |
598 | /// |
599 | /// assert_eq!(Ipv4Addr::new(0, 0, 0, 0).is_unspecified(), true); |
600 | /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_unspecified(), false); |
601 | /// ``` |
602 | #[rustc_const_stable (feature = "const_ip_32" , since = "1.32.0" )] |
603 | #[stable (feature = "ip_shared" , since = "1.12.0" )] |
604 | #[must_use ] |
605 | #[inline ] |
606 | pub const fn is_unspecified(&self) -> bool { |
607 | u32::from_be_bytes(self.octets) == 0 |
608 | } |
609 | |
610 | /// Returns [`true`] if this is a loopback address (`127.0.0.0/8`). |
611 | /// |
612 | /// This property is defined by [IETF RFC 1122]. |
613 | /// |
614 | /// [IETF RFC 1122]: https://tools.ietf.org/html/rfc1122 |
615 | /// |
616 | /// # Examples |
617 | /// |
618 | /// ``` |
619 | /// use std::net::Ipv4Addr; |
620 | /// |
621 | /// assert_eq!(Ipv4Addr::new(127, 0, 0, 1).is_loopback(), true); |
622 | /// assert_eq!(Ipv4Addr::new(45, 22, 13, 197).is_loopback(), false); |
623 | /// ``` |
624 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
625 | #[stable (since = "1.7.0" , feature = "ip_17" )] |
626 | #[must_use ] |
627 | #[inline ] |
628 | pub const fn is_loopback(&self) -> bool { |
629 | self.octets()[0] == 127 |
630 | } |
631 | |
632 | /// Returns [`true`] if this is a private address. |
633 | /// |
634 | /// The private address ranges are defined in [IETF RFC 1918] and include: |
635 | /// |
636 | /// - `10.0.0.0/8` |
637 | /// - `172.16.0.0/12` |
638 | /// - `192.168.0.0/16` |
639 | /// |
640 | /// [IETF RFC 1918]: https://tools.ietf.org/html/rfc1918 |
641 | /// |
642 | /// # Examples |
643 | /// |
644 | /// ``` |
645 | /// use std::net::Ipv4Addr; |
646 | /// |
647 | /// assert_eq!(Ipv4Addr::new(10, 0, 0, 1).is_private(), true); |
648 | /// assert_eq!(Ipv4Addr::new(10, 10, 10, 10).is_private(), true); |
649 | /// assert_eq!(Ipv4Addr::new(172, 16, 10, 10).is_private(), true); |
650 | /// assert_eq!(Ipv4Addr::new(172, 29, 45, 14).is_private(), true); |
651 | /// assert_eq!(Ipv4Addr::new(172, 32, 0, 2).is_private(), false); |
652 | /// assert_eq!(Ipv4Addr::new(192, 168, 0, 2).is_private(), true); |
653 | /// assert_eq!(Ipv4Addr::new(192, 169, 0, 2).is_private(), false); |
654 | /// ``` |
655 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
656 | #[stable (since = "1.7.0" , feature = "ip_17" )] |
657 | #[must_use ] |
658 | #[inline ] |
659 | pub const fn is_private(&self) -> bool { |
660 | match self.octets() { |
661 | [10, ..] => true, |
662 | [172, b, ..] if b >= 16 && b <= 31 => true, |
663 | [192, 168, ..] => true, |
664 | _ => false, |
665 | } |
666 | } |
667 | |
668 | /// Returns [`true`] if the address is link-local (`169.254.0.0/16`). |
669 | /// |
670 | /// This property is defined by [IETF RFC 3927]. |
671 | /// |
672 | /// [IETF RFC 3927]: https://tools.ietf.org/html/rfc3927 |
673 | /// |
674 | /// # Examples |
675 | /// |
676 | /// ``` |
677 | /// use std::net::Ipv4Addr; |
678 | /// |
679 | /// assert_eq!(Ipv4Addr::new(169, 254, 0, 0).is_link_local(), true); |
680 | /// assert_eq!(Ipv4Addr::new(169, 254, 10, 65).is_link_local(), true); |
681 | /// assert_eq!(Ipv4Addr::new(16, 89, 10, 65).is_link_local(), false); |
682 | /// ``` |
683 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
684 | #[stable (since = "1.7.0" , feature = "ip_17" )] |
685 | #[must_use ] |
686 | #[inline ] |
687 | pub const fn is_link_local(&self) -> bool { |
688 | matches!(self.octets(), [169, 254, ..]) |
689 | } |
690 | |
691 | /// Returns [`true`] if the address appears to be globally reachable |
692 | /// as specified by the [IANA IPv4 Special-Purpose Address Registry]. |
693 | /// Whether or not an address is practically reachable will depend on your network configuration. |
694 | /// |
695 | /// Most IPv4 addresses are globally reachable; |
696 | /// unless they are specifically defined as *not* globally reachable. |
697 | /// |
698 | /// Non-exhaustive list of notable addresses that are not globally reachable: |
699 | /// |
700 | /// - The [unspecified address] ([`is_unspecified`](Ipv4Addr::is_unspecified)) |
701 | /// - Addresses reserved for private use ([`is_private`](Ipv4Addr::is_private)) |
702 | /// - Addresses in the shared address space ([`is_shared`](Ipv4Addr::is_shared)) |
703 | /// - Loopback addresses ([`is_loopback`](Ipv4Addr::is_loopback)) |
704 | /// - Link-local addresses ([`is_link_local`](Ipv4Addr::is_link_local)) |
705 | /// - Addresses reserved for documentation ([`is_documentation`](Ipv4Addr::is_documentation)) |
706 | /// - Addresses reserved for benchmarking ([`is_benchmarking`](Ipv4Addr::is_benchmarking)) |
707 | /// - Reserved addresses ([`is_reserved`](Ipv4Addr::is_reserved)) |
708 | /// - The [broadcast address] ([`is_broadcast`](Ipv4Addr::is_broadcast)) |
709 | /// |
710 | /// For the complete overview of which addresses are globally reachable, see the table at the [IANA IPv4 Special-Purpose Address Registry]. |
711 | /// |
712 | /// [IANA IPv4 Special-Purpose Address Registry]: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml |
713 | /// [unspecified address]: Ipv4Addr::UNSPECIFIED |
714 | /// [broadcast address]: Ipv4Addr::BROADCAST |
715 | |
716 | /// |
717 | /// # Examples |
718 | /// |
719 | /// ``` |
720 | /// #![feature(ip)] |
721 | /// |
722 | /// use std::net::Ipv4Addr; |
723 | /// |
724 | /// // Most IPv4 addresses are globally reachable: |
725 | /// assert_eq!(Ipv4Addr::new(80, 9, 12, 3).is_global(), true); |
726 | /// |
727 | /// // However some addresses have been assigned a special meaning |
728 | /// // that makes them not globally reachable. Some examples are: |
729 | /// |
730 | /// // The unspecified address (`0.0.0.0`) |
731 | /// assert_eq!(Ipv4Addr::UNSPECIFIED.is_global(), false); |
732 | /// |
733 | /// // Addresses reserved for private use (`10.0.0.0/8`, `172.16.0.0/12`, 192.168.0.0/16) |
734 | /// assert_eq!(Ipv4Addr::new(10, 254, 0, 0).is_global(), false); |
735 | /// assert_eq!(Ipv4Addr::new(192, 168, 10, 65).is_global(), false); |
736 | /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_global(), false); |
737 | /// |
738 | /// // Addresses in the shared address space (`100.64.0.0/10`) |
739 | /// assert_eq!(Ipv4Addr::new(100, 100, 0, 0).is_global(), false); |
740 | /// |
741 | /// // The loopback addresses (`127.0.0.0/8`) |
742 | /// assert_eq!(Ipv4Addr::LOCALHOST.is_global(), false); |
743 | /// |
744 | /// // Link-local addresses (`169.254.0.0/16`) |
745 | /// assert_eq!(Ipv4Addr::new(169, 254, 45, 1).is_global(), false); |
746 | /// |
747 | /// // Addresses reserved for documentation (`192.0.2.0/24`, `198.51.100.0/24`, `203.0.113.0/24`) |
748 | /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_global(), false); |
749 | /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_global(), false); |
750 | /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_global(), false); |
751 | /// |
752 | /// // Addresses reserved for benchmarking (`198.18.0.0/15`) |
753 | /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_global(), false); |
754 | /// |
755 | /// // Reserved addresses (`240.0.0.0/4`) |
756 | /// assert_eq!(Ipv4Addr::new(250, 10, 20, 30).is_global(), false); |
757 | /// |
758 | /// // The broadcast address (`255.255.255.255`) |
759 | /// assert_eq!(Ipv4Addr::BROADCAST.is_global(), false); |
760 | /// |
761 | /// // For a complete overview see the IANA IPv4 Special-Purpose Address Registry. |
762 | /// ``` |
763 | #[rustc_const_unstable (feature = "const_ipv4" , issue = "76205" )] |
764 | #[unstable (feature = "ip" , issue = "27709" )] |
765 | #[must_use ] |
766 | #[inline ] |
767 | pub const fn is_global(&self) -> bool { |
768 | !(self.octets()[0] == 0 // "This network" |
769 | || self.is_private() |
770 | || self.is_shared() |
771 | || self.is_loopback() |
772 | || self.is_link_local() |
773 | // addresses reserved for future protocols (`192.0.0.0/24`) |
774 | // .9 and .10 are documented as globally reachable so they're excluded |
775 | || ( |
776 | self.octets()[0] == 192 && self.octets()[1] == 0 && self.octets()[2] == 0 |
777 | && self.octets()[3] != 9 && self.octets()[3] != 10 |
778 | ) |
779 | || self.is_documentation() |
780 | || self.is_benchmarking() |
781 | || self.is_reserved() |
782 | || self.is_broadcast()) |
783 | } |
784 | |
785 | /// Returns [`true`] if this address is part of the Shared Address Space defined in |
786 | /// [IETF RFC 6598] (`100.64.0.0/10`). |
787 | /// |
788 | /// [IETF RFC 6598]: https://tools.ietf.org/html/rfc6598 |
789 | /// |
790 | /// # Examples |
791 | /// |
792 | /// ``` |
793 | /// #![feature(ip)] |
794 | /// use std::net::Ipv4Addr; |
795 | /// |
796 | /// assert_eq!(Ipv4Addr::new(100, 64, 0, 0).is_shared(), true); |
797 | /// assert_eq!(Ipv4Addr::new(100, 127, 255, 255).is_shared(), true); |
798 | /// assert_eq!(Ipv4Addr::new(100, 128, 0, 0).is_shared(), false); |
799 | /// ``` |
800 | #[rustc_const_unstable (feature = "const_ipv4" , issue = "76205" )] |
801 | #[unstable (feature = "ip" , issue = "27709" )] |
802 | #[must_use ] |
803 | #[inline ] |
804 | pub const fn is_shared(&self) -> bool { |
805 | self.octets()[0] == 100 && (self.octets()[1] & 0b1100_0000 == 0b0100_0000) |
806 | } |
807 | |
808 | /// Returns [`true`] if this address part of the `198.18.0.0/15` range, which is reserved for |
809 | /// network devices benchmarking. This range is defined in [IETF RFC 2544] as `192.18.0.0` |
810 | /// through `198.19.255.255` but [errata 423] corrects it to `198.18.0.0/15`. |
811 | /// |
812 | /// [IETF RFC 2544]: https://tools.ietf.org/html/rfc2544 |
813 | /// [errata 423]: https://www.rfc-editor.org/errata/eid423 |
814 | /// |
815 | /// # Examples |
816 | /// |
817 | /// ``` |
818 | /// #![feature(ip)] |
819 | /// use std::net::Ipv4Addr; |
820 | /// |
821 | /// assert_eq!(Ipv4Addr::new(198, 17, 255, 255).is_benchmarking(), false); |
822 | /// assert_eq!(Ipv4Addr::new(198, 18, 0, 0).is_benchmarking(), true); |
823 | /// assert_eq!(Ipv4Addr::new(198, 19, 255, 255).is_benchmarking(), true); |
824 | /// assert_eq!(Ipv4Addr::new(198, 20, 0, 0).is_benchmarking(), false); |
825 | /// ``` |
826 | #[rustc_const_unstable (feature = "const_ipv4" , issue = "76205" )] |
827 | #[unstable (feature = "ip" , issue = "27709" )] |
828 | #[must_use ] |
829 | #[inline ] |
830 | pub const fn is_benchmarking(&self) -> bool { |
831 | self.octets()[0] == 198 && (self.octets()[1] & 0xfe) == 18 |
832 | } |
833 | |
834 | /// Returns [`true`] if this address is reserved by IANA for future use. [IETF RFC 1112] |
835 | /// defines the block of reserved addresses as `240.0.0.0/4`. This range normally includes the |
836 | /// broadcast address `255.255.255.255`, but this implementation explicitly excludes it, since |
837 | /// it is obviously not reserved for future use. |
838 | /// |
839 | /// [IETF RFC 1112]: https://tools.ietf.org/html/rfc1112 |
840 | /// |
841 | /// # Warning |
842 | /// |
843 | /// As IANA assigns new addresses, this method will be |
844 | /// updated. This may result in non-reserved addresses being |
845 | /// treated as reserved in code that relies on an outdated version |
846 | /// of this method. |
847 | /// |
848 | /// # Examples |
849 | /// |
850 | /// ``` |
851 | /// #![feature(ip)] |
852 | /// use std::net::Ipv4Addr; |
853 | /// |
854 | /// assert_eq!(Ipv4Addr::new(240, 0, 0, 0).is_reserved(), true); |
855 | /// assert_eq!(Ipv4Addr::new(255, 255, 255, 254).is_reserved(), true); |
856 | /// |
857 | /// assert_eq!(Ipv4Addr::new(239, 255, 255, 255).is_reserved(), false); |
858 | /// // The broadcast address is not considered as reserved for future use by this implementation |
859 | /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_reserved(), false); |
860 | /// ``` |
861 | #[rustc_const_unstable (feature = "const_ipv4" , issue = "76205" )] |
862 | #[unstable (feature = "ip" , issue = "27709" )] |
863 | #[must_use ] |
864 | #[inline ] |
865 | pub const fn is_reserved(&self) -> bool { |
866 | self.octets()[0] & 240 == 240 && !self.is_broadcast() |
867 | } |
868 | |
869 | /// Returns [`true`] if this is a multicast address (`224.0.0.0/4`). |
870 | /// |
871 | /// Multicast addresses have a most significant octet between `224` and `239`, |
872 | /// and is defined by [IETF RFC 5771]. |
873 | /// |
874 | /// [IETF RFC 5771]: https://tools.ietf.org/html/rfc5771 |
875 | /// |
876 | /// # Examples |
877 | /// |
878 | /// ``` |
879 | /// use std::net::Ipv4Addr; |
880 | /// |
881 | /// assert_eq!(Ipv4Addr::new(224, 254, 0, 0).is_multicast(), true); |
882 | /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_multicast(), true); |
883 | /// assert_eq!(Ipv4Addr::new(172, 16, 10, 65).is_multicast(), false); |
884 | /// ``` |
885 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
886 | #[stable (since = "1.7.0" , feature = "ip_17" )] |
887 | #[must_use ] |
888 | #[inline ] |
889 | pub const fn is_multicast(&self) -> bool { |
890 | self.octets()[0] >= 224 && self.octets()[0] <= 239 |
891 | } |
892 | |
893 | /// Returns [`true`] if this is a broadcast address (`255.255.255.255`). |
894 | /// |
895 | /// A broadcast address has all octets set to `255` as defined in [IETF RFC 919]. |
896 | /// |
897 | /// [IETF RFC 919]: https://tools.ietf.org/html/rfc919 |
898 | /// |
899 | /// # Examples |
900 | /// |
901 | /// ``` |
902 | /// use std::net::Ipv4Addr; |
903 | /// |
904 | /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_broadcast(), true); |
905 | /// assert_eq!(Ipv4Addr::new(236, 168, 10, 65).is_broadcast(), false); |
906 | /// ``` |
907 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
908 | #[stable (since = "1.7.0" , feature = "ip_17" )] |
909 | #[must_use ] |
910 | #[inline ] |
911 | pub const fn is_broadcast(&self) -> bool { |
912 | u32::from_be_bytes(self.octets()) == u32::from_be_bytes(Self::BROADCAST.octets()) |
913 | } |
914 | |
915 | /// Returns [`true`] if this address is in a range designated for documentation. |
916 | /// |
917 | /// This is defined in [IETF RFC 5737]: |
918 | /// |
919 | /// - `192.0.2.0/24` (TEST-NET-1) |
920 | /// - `198.51.100.0/24` (TEST-NET-2) |
921 | /// - `203.0.113.0/24` (TEST-NET-3) |
922 | /// |
923 | /// [IETF RFC 5737]: https://tools.ietf.org/html/rfc5737 |
924 | /// |
925 | /// # Examples |
926 | /// |
927 | /// ``` |
928 | /// use std::net::Ipv4Addr; |
929 | /// |
930 | /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).is_documentation(), true); |
931 | /// assert_eq!(Ipv4Addr::new(198, 51, 100, 65).is_documentation(), true); |
932 | /// assert_eq!(Ipv4Addr::new(203, 0, 113, 6).is_documentation(), true); |
933 | /// assert_eq!(Ipv4Addr::new(193, 34, 17, 19).is_documentation(), false); |
934 | /// ``` |
935 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
936 | #[stable (since = "1.7.0" , feature = "ip_17" )] |
937 | #[must_use ] |
938 | #[inline ] |
939 | pub const fn is_documentation(&self) -> bool { |
940 | matches!(self.octets(), [192, 0, 2, _] | [198, 51, 100, _] | [203, 0, 113, _]) |
941 | } |
942 | |
943 | /// Converts this address to an [IPv4-compatible] [`IPv6` address]. |
944 | /// |
945 | /// `a.b.c.d` becomes `::a.b.c.d` |
946 | /// |
947 | /// Note that IPv4-compatible addresses have been officially deprecated. |
948 | /// If you don't explicitly need an IPv4-compatible address for legacy reasons, consider using `to_ipv6_mapped` instead. |
949 | /// |
950 | /// [IPv4-compatible]: Ipv6Addr#ipv4-compatible-ipv6-addresses |
951 | /// [`IPv6` address]: Ipv6Addr |
952 | /// |
953 | /// # Examples |
954 | /// |
955 | /// ``` |
956 | /// use std::net::{Ipv4Addr, Ipv6Addr}; |
957 | /// |
958 | /// assert_eq!( |
959 | /// Ipv4Addr::new(192, 0, 2, 255).to_ipv6_compatible(), |
960 | /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x2ff) |
961 | /// ); |
962 | /// ``` |
963 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
964 | #[stable (feature = "rust1" , since = "1.0.0" )] |
965 | #[must_use = "this returns the result of the operation, \ |
966 | without modifying the original" ] |
967 | #[inline ] |
968 | pub const fn to_ipv6_compatible(&self) -> Ipv6Addr { |
969 | let [a, b, c, d] = self.octets(); |
970 | Ipv6Addr { octets: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d] } |
971 | } |
972 | |
973 | /// Converts this address to an [IPv4-mapped] [`IPv6` address]. |
974 | /// |
975 | /// `a.b.c.d` becomes `::ffff:a.b.c.d` |
976 | /// |
977 | /// [IPv4-mapped]: Ipv6Addr#ipv4-mapped-ipv6-addresses |
978 | /// [`IPv6` address]: Ipv6Addr |
979 | /// |
980 | /// # Examples |
981 | /// |
982 | /// ``` |
983 | /// use std::net::{Ipv4Addr, Ipv6Addr}; |
984 | /// |
985 | /// assert_eq!(Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(), |
986 | /// Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x2ff)); |
987 | /// ``` |
988 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
989 | #[stable (feature = "rust1" , since = "1.0.0" )] |
990 | #[must_use = "this returns the result of the operation, \ |
991 | without modifying the original" ] |
992 | #[inline ] |
993 | pub const fn to_ipv6_mapped(&self) -> Ipv6Addr { |
994 | let [a, b, c, d] = self.octets(); |
995 | Ipv6Addr { octets: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, a, b, c, d] } |
996 | } |
997 | } |
998 | |
999 | #[stable (feature = "ip_addr" , since = "1.7.0" )] |
1000 | impl fmt::Display for IpAddr { |
1001 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
1002 | match self { |
1003 | IpAddr::V4(ip: &Ipv4Addr) => ip.fmt(fmt), |
1004 | IpAddr::V6(ip: &Ipv6Addr) => ip.fmt(fmt), |
1005 | } |
1006 | } |
1007 | } |
1008 | |
1009 | #[stable (feature = "ip_addr" , since = "1.7.0" )] |
1010 | impl fmt::Debug for IpAddr { |
1011 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
1012 | fmt::Display::fmt(self, f:fmt) |
1013 | } |
1014 | } |
1015 | |
1016 | #[stable (feature = "ip_from_ip" , since = "1.16.0" )] |
1017 | impl From<Ipv4Addr> for IpAddr { |
1018 | /// Copies this address to a new `IpAddr::V4`. |
1019 | /// |
1020 | /// # Examples |
1021 | /// |
1022 | /// ``` |
1023 | /// use std::net::{IpAddr, Ipv4Addr}; |
1024 | /// |
1025 | /// let addr = Ipv4Addr::new(127, 0, 0, 1); |
1026 | /// |
1027 | /// assert_eq!( |
1028 | /// IpAddr::V4(addr), |
1029 | /// IpAddr::from(addr) |
1030 | /// ) |
1031 | /// ``` |
1032 | #[inline ] |
1033 | fn from(ipv4: Ipv4Addr) -> IpAddr { |
1034 | IpAddr::V4(ipv4) |
1035 | } |
1036 | } |
1037 | |
1038 | #[stable (feature = "ip_from_ip" , since = "1.16.0" )] |
1039 | impl From<Ipv6Addr> for IpAddr { |
1040 | /// Copies this address to a new `IpAddr::V6`. |
1041 | /// |
1042 | /// # Examples |
1043 | /// |
1044 | /// ``` |
1045 | /// use std::net::{IpAddr, Ipv6Addr}; |
1046 | /// |
1047 | /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff); |
1048 | /// |
1049 | /// assert_eq!( |
1050 | /// IpAddr::V6(addr), |
1051 | /// IpAddr::from(addr) |
1052 | /// ); |
1053 | /// ``` |
1054 | #[inline ] |
1055 | fn from(ipv6: Ipv6Addr) -> IpAddr { |
1056 | IpAddr::V6(ipv6) |
1057 | } |
1058 | } |
1059 | |
1060 | #[stable (feature = "rust1" , since = "1.0.0" )] |
1061 | impl fmt::Display for Ipv4Addr { |
1062 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
1063 | let octets: [u8; 4] = self.octets(); |
1064 | |
1065 | // If there are no alignment requirements, write the IP address directly to `f`. |
1066 | // Otherwise, write it to a local buffer and then use `f.pad`. |
1067 | if fmt.precision().is_none() && fmt.width().is_none() { |
1068 | write!(fmt, " {}. {}. {}. {}" , octets[0], octets[1], octets[2], octets[3]) |
1069 | } else { |
1070 | const LONGEST_IPV4_ADDR: &str = "255.255.255.255" ; |
1071 | |
1072 | let mut buf: DisplayBuffer<_> = DisplayBuffer::<{ LONGEST_IPV4_ADDR.len() }>::new(); |
1073 | // Buffer is long enough for the longest possible IPv4 address, so this should never fail. |
1074 | write!(buf, " {}. {}. {}. {}" , octets[0], octets[1], octets[2], octets[3]).unwrap(); |
1075 | |
1076 | fmt.pad(buf.as_str()) |
1077 | } |
1078 | } |
1079 | } |
1080 | |
1081 | #[stable (feature = "rust1" , since = "1.0.0" )] |
1082 | impl fmt::Debug for Ipv4Addr { |
1083 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
1084 | fmt::Display::fmt(self, f:fmt) |
1085 | } |
1086 | } |
1087 | |
1088 | #[stable (feature = "ip_cmp" , since = "1.16.0" )] |
1089 | impl PartialEq<Ipv4Addr> for IpAddr { |
1090 | #[inline ] |
1091 | fn eq(&self, other: &Ipv4Addr) -> bool { |
1092 | match self { |
1093 | IpAddr::V4(v4: &Ipv4Addr) => v4 == other, |
1094 | IpAddr::V6(_) => false, |
1095 | } |
1096 | } |
1097 | } |
1098 | |
1099 | #[stable (feature = "ip_cmp" , since = "1.16.0" )] |
1100 | impl PartialEq<IpAddr> for Ipv4Addr { |
1101 | #[inline ] |
1102 | fn eq(&self, other: &IpAddr) -> bool { |
1103 | match other { |
1104 | IpAddr::V4(v4: &Ipv4Addr) => self == v4, |
1105 | IpAddr::V6(_) => false, |
1106 | } |
1107 | } |
1108 | } |
1109 | |
1110 | #[stable (feature = "rust1" , since = "1.0.0" )] |
1111 | impl PartialOrd for Ipv4Addr { |
1112 | #[inline ] |
1113 | fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> { |
1114 | Some(self.cmp(other)) |
1115 | } |
1116 | } |
1117 | |
1118 | #[stable (feature = "ip_cmp" , since = "1.16.0" )] |
1119 | impl PartialOrd<Ipv4Addr> for IpAddr { |
1120 | #[inline ] |
1121 | fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> { |
1122 | match self { |
1123 | IpAddr::V4(v4: &Ipv4Addr) => v4.partial_cmp(other), |
1124 | IpAddr::V6(_) => Some(Ordering::Greater), |
1125 | } |
1126 | } |
1127 | } |
1128 | |
1129 | #[stable (feature = "ip_cmp" , since = "1.16.0" )] |
1130 | impl PartialOrd<IpAddr> for Ipv4Addr { |
1131 | #[inline ] |
1132 | fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> { |
1133 | match other { |
1134 | IpAddr::V4(v4: &Ipv4Addr) => self.partial_cmp(v4), |
1135 | IpAddr::V6(_) => Some(Ordering::Less), |
1136 | } |
1137 | } |
1138 | } |
1139 | |
1140 | #[stable (feature = "rust1" , since = "1.0.0" )] |
1141 | impl Ord for Ipv4Addr { |
1142 | #[inline ] |
1143 | fn cmp(&self, other: &Ipv4Addr) -> Ordering { |
1144 | self.octets.cmp(&other.octets) |
1145 | } |
1146 | } |
1147 | |
1148 | #[stable (feature = "ip_u32" , since = "1.1.0" )] |
1149 | impl From<Ipv4Addr> for u32 { |
1150 | /// Uses [`Ipv4Addr::to_bits`] to convert an IPv4 address to a host byte order `u32`. |
1151 | #[inline ] |
1152 | fn from(ip: Ipv4Addr) -> u32 { |
1153 | ip.to_bits() |
1154 | } |
1155 | } |
1156 | |
1157 | #[stable (feature = "ip_u32" , since = "1.1.0" )] |
1158 | impl From<u32> for Ipv4Addr { |
1159 | /// Uses [`Ipv4Addr::from_bits`] to convert a host byte order `u32` into an IPv4 address. |
1160 | #[inline ] |
1161 | fn from(ip: u32) -> Ipv4Addr { |
1162 | Ipv4Addr::from_bits(ip) |
1163 | } |
1164 | } |
1165 | |
1166 | #[stable (feature = "from_slice_v4" , since = "1.9.0" )] |
1167 | impl From<[u8; 4]> for Ipv4Addr { |
1168 | /// Creates an `Ipv4Addr` from a four element byte array. |
1169 | /// |
1170 | /// # Examples |
1171 | /// |
1172 | /// ``` |
1173 | /// use std::net::Ipv4Addr; |
1174 | /// |
1175 | /// let addr = Ipv4Addr::from([13u8, 12u8, 11u8, 10u8]); |
1176 | /// assert_eq!(Ipv4Addr::new(13, 12, 11, 10), addr); |
1177 | /// ``` |
1178 | #[inline ] |
1179 | fn from(octets: [u8; 4]) -> Ipv4Addr { |
1180 | Ipv4Addr { octets } |
1181 | } |
1182 | } |
1183 | |
1184 | #[stable (feature = "ip_from_slice" , since = "1.17.0" )] |
1185 | impl From<[u8; 4]> for IpAddr { |
1186 | /// Creates an `IpAddr::V4` from a four element byte array. |
1187 | /// |
1188 | /// # Examples |
1189 | /// |
1190 | /// ``` |
1191 | /// use std::net::{IpAddr, Ipv4Addr}; |
1192 | /// |
1193 | /// let addr = IpAddr::from([13u8, 12u8, 11u8, 10u8]); |
1194 | /// assert_eq!(IpAddr::V4(Ipv4Addr::new(13, 12, 11, 10)), addr); |
1195 | /// ``` |
1196 | #[inline ] |
1197 | fn from(octets: [u8; 4]) -> IpAddr { |
1198 | IpAddr::V4(Ipv4Addr::from(octets)) |
1199 | } |
1200 | } |
1201 | |
1202 | impl Ipv6Addr { |
1203 | /// Creates a new IPv6 address from eight 16-bit segments. |
1204 | /// |
1205 | /// The result will represent the IP address `a:b:c:d:e:f:g:h`. |
1206 | /// |
1207 | /// # Examples |
1208 | /// |
1209 | /// ``` |
1210 | /// use std::net::Ipv6Addr; |
1211 | /// |
1212 | /// let addr = Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff); |
1213 | /// ``` |
1214 | #[rustc_const_stable (feature = "const_ip_32" , since = "1.32.0" )] |
1215 | #[stable (feature = "rust1" , since = "1.0.0" )] |
1216 | #[must_use ] |
1217 | #[inline ] |
1218 | pub const fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr { |
1219 | let addr16 = [ |
1220 | a.to_be(), |
1221 | b.to_be(), |
1222 | c.to_be(), |
1223 | d.to_be(), |
1224 | e.to_be(), |
1225 | f.to_be(), |
1226 | g.to_be(), |
1227 | h.to_be(), |
1228 | ]; |
1229 | Ipv6Addr { |
1230 | // All elements in `addr16` are big endian. |
1231 | // SAFETY: `[u16; 8]` is always safe to transmute to `[u8; 16]`. |
1232 | octets: unsafe { transmute::<_, [u8; 16]>(addr16) }, |
1233 | } |
1234 | } |
1235 | |
1236 | /// The size of an IPv6 address in bits. |
1237 | /// |
1238 | /// # Examples |
1239 | /// |
1240 | /// ``` |
1241 | /// #![feature(ip_bits)] |
1242 | /// use std::net::Ipv6Addr; |
1243 | /// |
1244 | /// assert_eq!(Ipv6Addr::BITS, 128); |
1245 | /// ``` |
1246 | #[unstable (feature = "ip_bits" , issue = "113744" )] |
1247 | pub const BITS: u32 = 128; |
1248 | |
1249 | /// Converts an IPv6 address into a `u128` representation using native byte order. |
1250 | /// |
1251 | /// Although IPv6 addresses are big-endian, the `u128` value will use the target platform's |
1252 | /// native byte order. That is, the `u128` value is an integer representation of the IPv6 |
1253 | /// address and not an integer interpretation of the IPv6 address's big-endian bitstring. This |
1254 | /// means that the `u128` value masked with `0xffffffffffffffffffffffffffff0000_u128` will set |
1255 | /// the last segment in the address to 0, regardless of the target platform's endianness. |
1256 | /// |
1257 | /// # Examples |
1258 | /// |
1259 | /// ``` |
1260 | /// #![feature(ip_bits)] |
1261 | /// use std::net::Ipv6Addr; |
1262 | /// |
1263 | /// let addr = Ipv6Addr::new( |
1264 | /// 0x1020, 0x3040, 0x5060, 0x7080, |
1265 | /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D, |
1266 | /// ); |
1267 | /// assert_eq!(0x102030405060708090A0B0C0D0E0F00D_u128, u128::from(addr)); |
1268 | /// ``` |
1269 | /// |
1270 | /// ``` |
1271 | /// #![feature(ip_bits)] |
1272 | /// use std::net::Ipv6Addr; |
1273 | /// |
1274 | /// let addr = Ipv6Addr::new( |
1275 | /// 0x1020, 0x3040, 0x5060, 0x7080, |
1276 | /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D, |
1277 | /// ); |
1278 | /// let addr_bits = addr.to_bits() & 0xffffffffffffffffffffffffffff0000_u128; |
1279 | /// assert_eq!( |
1280 | /// Ipv6Addr::new( |
1281 | /// 0x1020, 0x3040, 0x5060, 0x7080, |
1282 | /// 0x90A0, 0xB0C0, 0xD0E0, 0x0000, |
1283 | /// ), |
1284 | /// Ipv6Addr::from_bits(addr_bits)); |
1285 | /// |
1286 | /// ``` |
1287 | #[rustc_const_unstable (feature = "ip_bits" , issue = "113744" )] |
1288 | #[unstable (feature = "ip_bits" , issue = "113744" )] |
1289 | #[must_use ] |
1290 | #[inline ] |
1291 | pub const fn to_bits(self) -> u128 { |
1292 | u128::from_be_bytes(self.octets) |
1293 | } |
1294 | |
1295 | /// Converts a native byte order `u128` into an IPv6 address. |
1296 | /// |
1297 | /// See [`Ipv6Addr::to_bits`] for an explanation on endianness. |
1298 | /// |
1299 | /// # Examples |
1300 | /// |
1301 | /// ``` |
1302 | /// #![feature(ip_bits)] |
1303 | /// use std::net::Ipv6Addr; |
1304 | /// |
1305 | /// let addr = Ipv6Addr::from(0x102030405060708090A0B0C0D0E0F00D_u128); |
1306 | /// assert_eq!( |
1307 | /// Ipv6Addr::new( |
1308 | /// 0x1020, 0x3040, 0x5060, 0x7080, |
1309 | /// 0x90A0, 0xB0C0, 0xD0E0, 0xF00D, |
1310 | /// ), |
1311 | /// addr); |
1312 | /// ``` |
1313 | #[rustc_const_unstable (feature = "ip_bits" , issue = "113744" )] |
1314 | #[unstable (feature = "ip_bits" , issue = "113744" )] |
1315 | #[must_use ] |
1316 | #[inline ] |
1317 | pub const fn from_bits(bits: u128) -> Ipv6Addr { |
1318 | Ipv6Addr { octets: bits.to_be_bytes() } |
1319 | } |
1320 | |
1321 | /// An IPv6 address representing localhost: `::1`. |
1322 | /// |
1323 | /// This corresponds to constant `IN6ADDR_LOOPBACK_INIT` or `in6addr_loopback` in other |
1324 | /// languages. |
1325 | /// |
1326 | /// # Examples |
1327 | /// |
1328 | /// ``` |
1329 | /// use std::net::Ipv6Addr; |
1330 | /// |
1331 | /// let addr = Ipv6Addr::LOCALHOST; |
1332 | /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); |
1333 | /// ``` |
1334 | #[doc (alias = "IN6ADDR_LOOPBACK_INIT" )] |
1335 | #[doc (alias = "in6addr_loopback" )] |
1336 | #[stable (feature = "ip_constructors" , since = "1.30.0" )] |
1337 | pub const LOCALHOST: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1); |
1338 | |
1339 | /// An IPv6 address representing the unspecified address: `::` |
1340 | /// |
1341 | /// This corresponds to constant `IN6ADDR_ANY_INIT` or `in6addr_any` in other languages. |
1342 | /// |
1343 | /// # Examples |
1344 | /// |
1345 | /// ``` |
1346 | /// use std::net::Ipv6Addr; |
1347 | /// |
1348 | /// let addr = Ipv6Addr::UNSPECIFIED; |
1349 | /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)); |
1350 | /// ``` |
1351 | #[doc (alias = "IN6ADDR_ANY_INIT" )] |
1352 | #[doc (alias = "in6addr_any" )] |
1353 | #[stable (feature = "ip_constructors" , since = "1.30.0" )] |
1354 | pub const UNSPECIFIED: Self = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0); |
1355 | |
1356 | /// Returns the eight 16-bit segments that make up this address. |
1357 | /// |
1358 | /// # Examples |
1359 | /// |
1360 | /// ``` |
1361 | /// use std::net::Ipv6Addr; |
1362 | /// |
1363 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).segments(), |
1364 | /// [0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff]); |
1365 | /// ``` |
1366 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
1367 | #[stable (feature = "rust1" , since = "1.0.0" )] |
1368 | #[must_use ] |
1369 | #[inline ] |
1370 | pub const fn segments(&self) -> [u16; 8] { |
1371 | // All elements in `self.octets` must be big endian. |
1372 | // SAFETY: `[u8; 16]` is always safe to transmute to `[u16; 8]`. |
1373 | let [a, b, c, d, e, f, g, h] = unsafe { transmute::<_, [u16; 8]>(self.octets) }; |
1374 | // We want native endian u16 |
1375 | [ |
1376 | u16::from_be(a), |
1377 | u16::from_be(b), |
1378 | u16::from_be(c), |
1379 | u16::from_be(d), |
1380 | u16::from_be(e), |
1381 | u16::from_be(f), |
1382 | u16::from_be(g), |
1383 | u16::from_be(h), |
1384 | ] |
1385 | } |
1386 | |
1387 | /// Returns [`true`] for the special 'unspecified' address (`::`). |
1388 | /// |
1389 | /// This property is defined in [IETF RFC 4291]. |
1390 | /// |
1391 | /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291 |
1392 | /// |
1393 | /// # Examples |
1394 | /// |
1395 | /// ``` |
1396 | /// use std::net::Ipv6Addr; |
1397 | /// |
1398 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unspecified(), false); |
1399 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0).is_unspecified(), true); |
1400 | /// ``` |
1401 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
1402 | #[stable (since = "1.7.0" , feature = "ip_17" )] |
1403 | #[must_use ] |
1404 | #[inline ] |
1405 | pub const fn is_unspecified(&self) -> bool { |
1406 | u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::UNSPECIFIED.octets()) |
1407 | } |
1408 | |
1409 | /// Returns [`true`] if this is the [loopback address] (`::1`), |
1410 | /// as defined in [IETF RFC 4291 section 2.5.3]. |
1411 | /// |
1412 | /// Contrary to IPv4, in IPv6 there is only one loopback address. |
1413 | /// |
1414 | /// [loopback address]: Ipv6Addr::LOCALHOST |
1415 | /// [IETF RFC 4291 section 2.5.3]: https://tools.ietf.org/html/rfc4291#section-2.5.3 |
1416 | /// |
1417 | /// # Examples |
1418 | /// |
1419 | /// ``` |
1420 | /// use std::net::Ipv6Addr; |
1421 | /// |
1422 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_loopback(), false); |
1423 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1).is_loopback(), true); |
1424 | /// ``` |
1425 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
1426 | #[stable (since = "1.7.0" , feature = "ip_17" )] |
1427 | #[must_use ] |
1428 | #[inline ] |
1429 | pub const fn is_loopback(&self) -> bool { |
1430 | u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::LOCALHOST.octets()) |
1431 | } |
1432 | |
1433 | /// Returns [`true`] if the address appears to be globally reachable |
1434 | /// as specified by the [IANA IPv6 Special-Purpose Address Registry]. |
1435 | /// Whether or not an address is practically reachable will depend on your network configuration. |
1436 | /// |
1437 | /// Most IPv6 addresses are globally reachable; |
1438 | /// unless they are specifically defined as *not* globally reachable. |
1439 | /// |
1440 | /// Non-exhaustive list of notable addresses that are not globally reachable: |
1441 | /// - The [unspecified address] ([`is_unspecified`](Ipv6Addr::is_unspecified)) |
1442 | /// - The [loopback address] ([`is_loopback`](Ipv6Addr::is_loopback)) |
1443 | /// - IPv4-mapped addresses |
1444 | /// - Addresses reserved for benchmarking ([`is_benchmarking`](Ipv6Addr::is_benchmarking)) |
1445 | /// - Addresses reserved for documentation ([`is_documentation`](Ipv6Addr::is_documentation)) |
1446 | /// - Unique local addresses ([`is_unique_local`](Ipv6Addr::is_unique_local)) |
1447 | /// - Unicast addresses with link-local scope ([`is_unicast_link_local`](Ipv6Addr::is_unicast_link_local)) |
1448 | /// |
1449 | /// For the complete overview of which addresses are globally reachable, see the table at the [IANA IPv6 Special-Purpose Address Registry]. |
1450 | /// |
1451 | /// Note that an address having global scope is not the same as being globally reachable, |
1452 | /// and there is no direct relation between the two concepts: There exist addresses with global scope |
1453 | /// that are not globally reachable (for example unique local addresses), |
1454 | /// and addresses that are globally reachable without having global scope |
1455 | /// (multicast addresses with non-global scope). |
1456 | /// |
1457 | /// [IANA IPv6 Special-Purpose Address Registry]: https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml |
1458 | /// [unspecified address]: Ipv6Addr::UNSPECIFIED |
1459 | /// [loopback address]: Ipv6Addr::LOCALHOST |
1460 | /// |
1461 | /// # Examples |
1462 | /// |
1463 | /// ``` |
1464 | /// #![feature(ip)] |
1465 | /// |
1466 | /// use std::net::Ipv6Addr; |
1467 | /// |
1468 | /// // Most IPv6 addresses are globally reachable: |
1469 | /// assert_eq!(Ipv6Addr::new(0x26, 0, 0x1c9, 0, 0, 0xafc8, 0x10, 0x1).is_global(), true); |
1470 | /// |
1471 | /// // However some addresses have been assigned a special meaning |
1472 | /// // that makes them not globally reachable. Some examples are: |
1473 | /// |
1474 | /// // The unspecified address (`::`) |
1475 | /// assert_eq!(Ipv6Addr::UNSPECIFIED.is_global(), false); |
1476 | /// |
1477 | /// // The loopback address (`::1`) |
1478 | /// assert_eq!(Ipv6Addr::LOCALHOST.is_global(), false); |
1479 | /// |
1480 | /// // IPv4-mapped addresses (`::ffff:0:0/96`) |
1481 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_global(), false); |
1482 | /// |
1483 | /// // Addresses reserved for benchmarking (`2001:2::/48`) |
1484 | /// assert_eq!(Ipv6Addr::new(0x2001, 2, 0, 0, 0, 0, 0, 1,).is_global(), false); |
1485 | /// |
1486 | /// // Addresses reserved for documentation (`2001:db8::/32`) |
1487 | /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1).is_global(), false); |
1488 | /// |
1489 | /// // Unique local addresses (`fc00::/7`) |
1490 | /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 1).is_global(), false); |
1491 | /// |
1492 | /// // Unicast addresses with link-local scope (`fe80::/10`) |
1493 | /// assert_eq!(Ipv6Addr::new(0xfe81, 0, 0, 0, 0, 0, 0, 1).is_global(), false); |
1494 | /// |
1495 | /// // For a complete overview see the IANA IPv6 Special-Purpose Address Registry. |
1496 | /// ``` |
1497 | #[rustc_const_unstable (feature = "const_ipv6" , issue = "76205" )] |
1498 | #[unstable (feature = "ip" , issue = "27709" )] |
1499 | #[must_use ] |
1500 | #[inline ] |
1501 | pub const fn is_global(&self) -> bool { |
1502 | !(self.is_unspecified() |
1503 | || self.is_loopback() |
1504 | // IPv4-mapped Address (`::ffff:0:0/96`) |
1505 | || matches!(self.segments(), [0, 0, 0, 0, 0, 0xffff, _, _]) |
1506 | // IPv4-IPv6 Translat. (`64:ff9b:1::/48`) |
1507 | || matches!(self.segments(), [0x64, 0xff9b, 1, _, _, _, _, _]) |
1508 | // Discard-Only Address Block (`100::/64`) |
1509 | || matches!(self.segments(), [0x100, 0, 0, 0, _, _, _, _]) |
1510 | // IETF Protocol Assignments (`2001::/23`) |
1511 | || (matches!(self.segments(), [0x2001, b, _, _, _, _, _, _] if b < 0x200) |
1512 | && !( |
1513 | // Port Control Protocol Anycast (`2001:1::1`) |
1514 | u128::from_be_bytes(self.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0001 |
1515 | // Traversal Using Relays around NAT Anycast (`2001:1::2`) |
1516 | || u128::from_be_bytes(self.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0002 |
1517 | // AMT (`2001:3::/32`) |
1518 | || matches!(self.segments(), [0x2001, 3, _, _, _, _, _, _]) |
1519 | // AS112-v6 (`2001:4:112::/48`) |
1520 | || matches!(self.segments(), [0x2001, 4, 0x112, _, _, _, _, _]) |
1521 | // ORCHIDv2 (`2001:20::/28`) |
1522 | // Drone Remote ID Protocol Entity Tags (DETs) Prefix (`2001:30::/28`)` |
1523 | || matches!(self.segments(), [0x2001, b, _, _, _, _, _, _] if b >= 0x20 && b <= 0x3F) |
1524 | )) |
1525 | // 6to4 (`2002::/16`) – it's not explicitly documented as globally reachable, |
1526 | // IANA says N/A. |
1527 | || matches!(self.segments(), [0x2002, _, _, _, _, _, _, _]) |
1528 | || self.is_documentation() |
1529 | || self.is_unique_local() |
1530 | || self.is_unicast_link_local()) |
1531 | } |
1532 | |
1533 | /// Returns [`true`] if this is a unique local address (`fc00::/7`). |
1534 | /// |
1535 | /// This property is defined in [IETF RFC 4193]. |
1536 | /// |
1537 | /// [IETF RFC 4193]: https://tools.ietf.org/html/rfc4193 |
1538 | /// |
1539 | /// # Examples |
1540 | /// |
1541 | /// ``` |
1542 | /// #![feature(ip)] |
1543 | /// |
1544 | /// use std::net::Ipv6Addr; |
1545 | /// |
1546 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unique_local(), false); |
1547 | /// assert_eq!(Ipv6Addr::new(0xfc02, 0, 0, 0, 0, 0, 0, 0).is_unique_local(), true); |
1548 | /// ``` |
1549 | #[rustc_const_unstable (feature = "const_ipv6" , issue = "76205" )] |
1550 | #[unstable (feature = "ip" , issue = "27709" )] |
1551 | #[must_use ] |
1552 | #[inline ] |
1553 | pub const fn is_unique_local(&self) -> bool { |
1554 | (self.segments()[0] & 0xfe00) == 0xfc00 |
1555 | } |
1556 | |
1557 | /// Returns [`true`] if this is a unicast address, as defined by [IETF RFC 4291]. |
1558 | /// Any address that is not a [multicast address] (`ff00::/8`) is unicast. |
1559 | /// |
1560 | /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291 |
1561 | /// [multicast address]: Ipv6Addr::is_multicast |
1562 | /// |
1563 | /// # Examples |
1564 | /// |
1565 | /// ``` |
1566 | /// #![feature(ip)] |
1567 | /// |
1568 | /// use std::net::Ipv6Addr; |
1569 | /// |
1570 | /// // The unspecified and loopback addresses are unicast. |
1571 | /// assert_eq!(Ipv6Addr::UNSPECIFIED.is_unicast(), true); |
1572 | /// assert_eq!(Ipv6Addr::LOCALHOST.is_unicast(), true); |
1573 | /// |
1574 | /// // Any address that is not a multicast address (`ff00::/8`) is unicast. |
1575 | /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast(), true); |
1576 | /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_unicast(), false); |
1577 | /// ``` |
1578 | #[rustc_const_unstable (feature = "const_ipv6" , issue = "76205" )] |
1579 | #[unstable (feature = "ip" , issue = "27709" )] |
1580 | #[must_use ] |
1581 | #[inline ] |
1582 | pub const fn is_unicast(&self) -> bool { |
1583 | !self.is_multicast() |
1584 | } |
1585 | |
1586 | /// Returns `true` if the address is a unicast address with link-local scope, |
1587 | /// as defined in [RFC 4291]. |
1588 | /// |
1589 | /// A unicast address has link-local scope if it has the prefix `fe80::/10`, as per [RFC 4291 section 2.4]. |
1590 | /// Note that this encompasses more addresses than those defined in [RFC 4291 section 2.5.6], |
1591 | /// which describes "Link-Local IPv6 Unicast Addresses" as having the following stricter format: |
1592 | /// |
1593 | /// ```text |
1594 | /// | 10 bits | 54 bits | 64 bits | |
1595 | /// +----------+-------------------------+----------------------------+ |
1596 | /// |1111111010| 0 | interface ID | |
1597 | /// +----------+-------------------------+----------------------------+ |
1598 | /// ``` |
1599 | /// So while currently the only addresses with link-local scope an application will encounter are all in `fe80::/64`, |
1600 | /// this might change in the future with the publication of new standards. More addresses in `fe80::/10` could be allocated, |
1601 | /// and those addresses will have link-local scope. |
1602 | /// |
1603 | /// Also note that while [RFC 4291 section 2.5.3] mentions about the [loopback address] (`::1`) that "it is treated as having Link-Local scope", |
1604 | /// this does not mean that the loopback address actually has link-local scope and this method will return `false` on it. |
1605 | /// |
1606 | /// [RFC 4291]: https://tools.ietf.org/html/rfc4291 |
1607 | /// [RFC 4291 section 2.4]: https://tools.ietf.org/html/rfc4291#section-2.4 |
1608 | /// [RFC 4291 section 2.5.3]: https://tools.ietf.org/html/rfc4291#section-2.5.3 |
1609 | /// [RFC 4291 section 2.5.6]: https://tools.ietf.org/html/rfc4291#section-2.5.6 |
1610 | /// [loopback address]: Ipv6Addr::LOCALHOST |
1611 | /// |
1612 | /// # Examples |
1613 | /// |
1614 | /// ``` |
1615 | /// #![feature(ip)] |
1616 | /// |
1617 | /// use std::net::Ipv6Addr; |
1618 | /// |
1619 | /// // The loopback address (`::1`) does not actually have link-local scope. |
1620 | /// assert_eq!(Ipv6Addr::LOCALHOST.is_unicast_link_local(), false); |
1621 | /// |
1622 | /// // Only addresses in `fe80::/10` have link-local scope. |
1623 | /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), false); |
1624 | /// assert_eq!(Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true); |
1625 | /// |
1626 | /// // Addresses outside the stricter `fe80::/64` also have link-local scope. |
1627 | /// assert_eq!(Ipv6Addr::new(0xfe80, 0, 0, 1, 0, 0, 0, 0).is_unicast_link_local(), true); |
1628 | /// assert_eq!(Ipv6Addr::new(0xfe81, 0, 0, 0, 0, 0, 0, 0).is_unicast_link_local(), true); |
1629 | /// ``` |
1630 | #[rustc_const_unstable (feature = "const_ipv6" , issue = "76205" )] |
1631 | #[unstable (feature = "ip" , issue = "27709" )] |
1632 | #[must_use ] |
1633 | #[inline ] |
1634 | pub const fn is_unicast_link_local(&self) -> bool { |
1635 | (self.segments()[0] & 0xffc0) == 0xfe80 |
1636 | } |
1637 | |
1638 | /// Returns [`true`] if this is an address reserved for documentation |
1639 | /// (`2001:db8::/32`). |
1640 | /// |
1641 | /// This property is defined in [IETF RFC 3849]. |
1642 | /// |
1643 | /// [IETF RFC 3849]: https://tools.ietf.org/html/rfc3849 |
1644 | /// |
1645 | /// # Examples |
1646 | /// |
1647 | /// ``` |
1648 | /// #![feature(ip)] |
1649 | /// |
1650 | /// use std::net::Ipv6Addr; |
1651 | /// |
1652 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_documentation(), false); |
1653 | /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_documentation(), true); |
1654 | /// ``` |
1655 | #[rustc_const_unstable (feature = "const_ipv6" , issue = "76205" )] |
1656 | #[unstable (feature = "ip" , issue = "27709" )] |
1657 | #[must_use ] |
1658 | #[inline ] |
1659 | pub const fn is_documentation(&self) -> bool { |
1660 | (self.segments()[0] == 0x2001) && (self.segments()[1] == 0xdb8) |
1661 | } |
1662 | |
1663 | /// Returns [`true`] if this is an address reserved for benchmarking (`2001:2::/48`). |
1664 | /// |
1665 | /// This property is defined in [IETF RFC 5180], where it is mistakenly specified as covering the range `2001:0200::/48`. |
1666 | /// This is corrected in [IETF RFC Errata 1752] to `2001:0002::/48`. |
1667 | /// |
1668 | /// [IETF RFC 5180]: https://tools.ietf.org/html/rfc5180 |
1669 | /// [IETF RFC Errata 1752]: https://www.rfc-editor.org/errata_search.php?eid=1752 |
1670 | /// |
1671 | /// ``` |
1672 | /// #![feature(ip)] |
1673 | /// |
1674 | /// use std::net::Ipv6Addr; |
1675 | /// |
1676 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc613, 0x0).is_benchmarking(), false); |
1677 | /// assert_eq!(Ipv6Addr::new(0x2001, 0x2, 0, 0, 0, 0, 0, 0).is_benchmarking(), true); |
1678 | /// ``` |
1679 | #[unstable (feature = "ip" , issue = "27709" )] |
1680 | #[must_use ] |
1681 | #[inline ] |
1682 | pub const fn is_benchmarking(&self) -> bool { |
1683 | (self.segments()[0] == 0x2001) && (self.segments()[1] == 0x2) && (self.segments()[2] == 0) |
1684 | } |
1685 | |
1686 | /// Returns [`true`] if the address is a globally routable unicast address. |
1687 | /// |
1688 | /// The following return false: |
1689 | /// |
1690 | /// - the loopback address |
1691 | /// - the link-local addresses |
1692 | /// - unique local addresses |
1693 | /// - the unspecified address |
1694 | /// - the address range reserved for documentation |
1695 | /// |
1696 | /// This method returns [`true`] for site-local addresses as per [RFC 4291 section 2.5.7] |
1697 | /// |
1698 | /// ```no_rust |
1699 | /// The special behavior of [the site-local unicast] prefix defined in [RFC3513] must no longer |
1700 | /// be supported in new implementations (i.e., new implementations must treat this prefix as |
1701 | /// Global Unicast). |
1702 | /// ``` |
1703 | /// |
1704 | /// [RFC 4291 section 2.5.7]: https://tools.ietf.org/html/rfc4291#section-2.5.7 |
1705 | /// |
1706 | /// # Examples |
1707 | /// |
1708 | /// ``` |
1709 | /// #![feature(ip)] |
1710 | /// |
1711 | /// use std::net::Ipv6Addr; |
1712 | /// |
1713 | /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast_global(), false); |
1714 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_unicast_global(), true); |
1715 | /// ``` |
1716 | #[rustc_const_unstable (feature = "const_ipv6" , issue = "76205" )] |
1717 | #[unstable (feature = "ip" , issue = "27709" )] |
1718 | #[must_use ] |
1719 | #[inline ] |
1720 | pub const fn is_unicast_global(&self) -> bool { |
1721 | self.is_unicast() |
1722 | && !self.is_loopback() |
1723 | && !self.is_unicast_link_local() |
1724 | && !self.is_unique_local() |
1725 | && !self.is_unspecified() |
1726 | && !self.is_documentation() |
1727 | && !self.is_benchmarking() |
1728 | } |
1729 | |
1730 | /// Returns the address's multicast scope if the address is multicast. |
1731 | /// |
1732 | /// # Examples |
1733 | /// |
1734 | /// ``` |
1735 | /// #![feature(ip)] |
1736 | /// |
1737 | /// use std::net::{Ipv6Addr, Ipv6MulticastScope}; |
1738 | /// |
1739 | /// assert_eq!( |
1740 | /// Ipv6Addr::new(0xff0e, 0, 0, 0, 0, 0, 0, 0).multicast_scope(), |
1741 | /// Some(Ipv6MulticastScope::Global) |
1742 | /// ); |
1743 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).multicast_scope(), None); |
1744 | /// ``` |
1745 | #[rustc_const_unstable (feature = "const_ipv6" , issue = "76205" )] |
1746 | #[unstable (feature = "ip" , issue = "27709" )] |
1747 | #[must_use ] |
1748 | #[inline ] |
1749 | pub const fn multicast_scope(&self) -> Option<Ipv6MulticastScope> { |
1750 | if self.is_multicast() { |
1751 | match self.segments()[0] & 0x000f { |
1752 | 1 => Some(Ipv6MulticastScope::InterfaceLocal), |
1753 | 2 => Some(Ipv6MulticastScope::LinkLocal), |
1754 | 3 => Some(Ipv6MulticastScope::RealmLocal), |
1755 | 4 => Some(Ipv6MulticastScope::AdminLocal), |
1756 | 5 => Some(Ipv6MulticastScope::SiteLocal), |
1757 | 8 => Some(Ipv6MulticastScope::OrganizationLocal), |
1758 | 14 => Some(Ipv6MulticastScope::Global), |
1759 | _ => None, |
1760 | } |
1761 | } else { |
1762 | None |
1763 | } |
1764 | } |
1765 | |
1766 | /// Returns [`true`] if this is a multicast address (`ff00::/8`). |
1767 | /// |
1768 | /// This property is defined by [IETF RFC 4291]. |
1769 | /// |
1770 | /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291 |
1771 | /// |
1772 | /// # Examples |
1773 | /// |
1774 | /// ``` |
1775 | /// use std::net::Ipv6Addr; |
1776 | /// |
1777 | /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_multicast(), true); |
1778 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).is_multicast(), false); |
1779 | /// ``` |
1780 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
1781 | #[stable (since = "1.7.0" , feature = "ip_17" )] |
1782 | #[must_use ] |
1783 | #[inline ] |
1784 | pub const fn is_multicast(&self) -> bool { |
1785 | (self.segments()[0] & 0xff00) == 0xff00 |
1786 | } |
1787 | |
1788 | /// Returns [`true`] if the address is an IPv4-mapped address (`::ffff:0:0/96`). |
1789 | /// |
1790 | /// IPv4-mapped addresses can be converted to their canonical IPv4 address with |
1791 | /// [`to_ipv4_mapped`](Ipv6Addr::to_ipv4_mapped). |
1792 | /// |
1793 | /// # Examples |
1794 | /// ``` |
1795 | /// #![feature(ip)] |
1796 | /// |
1797 | /// use std::net::{Ipv4Addr, Ipv6Addr}; |
1798 | /// |
1799 | /// let ipv4_mapped = Ipv4Addr::new(192, 0, 2, 255).to_ipv6_mapped(); |
1800 | /// assert_eq!(ipv4_mapped.is_ipv4_mapped(), true); |
1801 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x2ff).is_ipv4_mapped(), true); |
1802 | /// |
1803 | /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_ipv4_mapped(), false); |
1804 | /// ``` |
1805 | #[rustc_const_unstable (feature = "const_ipv6" , issue = "76205" )] |
1806 | #[unstable (feature = "ip" , issue = "27709" )] |
1807 | #[must_use ] |
1808 | #[inline ] |
1809 | pub const fn is_ipv4_mapped(&self) -> bool { |
1810 | matches!(self.segments(), [0, 0, 0, 0, 0, 0xffff, _, _]) |
1811 | } |
1812 | |
1813 | /// Converts this address to an [`IPv4` address] if it's an [IPv4-mapped] address, |
1814 | /// as defined in [IETF RFC 4291 section 2.5.5.2], otherwise returns [`None`]. |
1815 | /// |
1816 | /// `::ffff:a.b.c.d` becomes `a.b.c.d`. |
1817 | /// All addresses *not* starting with `::ffff` will return `None`. |
1818 | /// |
1819 | /// [`IPv4` address]: Ipv4Addr |
1820 | /// [IPv4-mapped]: Ipv6Addr |
1821 | /// [IETF RFC 4291 section 2.5.5.2]: https://tools.ietf.org/html/rfc4291#section-2.5.5.2 |
1822 | /// |
1823 | /// # Examples |
1824 | /// |
1825 | /// ``` |
1826 | /// use std::net::{Ipv4Addr, Ipv6Addr}; |
1827 | /// |
1828 | /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4_mapped(), None); |
1829 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4_mapped(), |
1830 | /// Some(Ipv4Addr::new(192, 10, 2, 255))); |
1831 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4_mapped(), None); |
1832 | /// ``` |
1833 | #[inline ] |
1834 | #[must_use = "this returns the result of the operation, \ |
1835 | without modifying the original" ] |
1836 | #[stable (feature = "ipv6_to_ipv4_mapped" , since = "1.63.0" )] |
1837 | #[rustc_const_stable (feature = "const_ipv6_to_ipv4_mapped" , since = "1.75.0" )] |
1838 | pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> { |
1839 | match self.octets() { |
1840 | [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => { |
1841 | Some(Ipv4Addr::new(a, b, c, d)) |
1842 | } |
1843 | _ => None, |
1844 | } |
1845 | } |
1846 | |
1847 | /// Converts this address to an [`IPv4` address] if it is either |
1848 | /// an [IPv4-compatible] address as defined in [IETF RFC 4291 section 2.5.5.1], |
1849 | /// or an [IPv4-mapped] address as defined in [IETF RFC 4291 section 2.5.5.2], |
1850 | /// otherwise returns [`None`]. |
1851 | /// |
1852 | /// Note that this will return an [`IPv4` address] for the IPv6 loopback address `::1`. Use |
1853 | /// [`Ipv6Addr::to_ipv4_mapped`] to avoid this. |
1854 | /// |
1855 | /// `::a.b.c.d` and `::ffff:a.b.c.d` become `a.b.c.d`. `::1` becomes `0.0.0.1`. |
1856 | /// All addresses *not* starting with either all zeroes or `::ffff` will return `None`. |
1857 | /// |
1858 | /// [`IPv4` address]: Ipv4Addr |
1859 | /// [IPv4-compatible]: Ipv6Addr#ipv4-compatible-ipv6-addresses |
1860 | /// [IPv4-mapped]: Ipv6Addr#ipv4-mapped-ipv6-addresses |
1861 | /// [IETF RFC 4291 section 2.5.5.1]: https://tools.ietf.org/html/rfc4291#section-2.5.5.1 |
1862 | /// [IETF RFC 4291 section 2.5.5.2]: https://tools.ietf.org/html/rfc4291#section-2.5.5.2 |
1863 | /// |
1864 | /// # Examples |
1865 | /// |
1866 | /// ``` |
1867 | /// use std::net::{Ipv4Addr, Ipv6Addr}; |
1868 | /// |
1869 | /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).to_ipv4(), None); |
1870 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc00a, 0x2ff).to_ipv4(), |
1871 | /// Some(Ipv4Addr::new(192, 10, 2, 255))); |
1872 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4(), |
1873 | /// Some(Ipv4Addr::new(0, 0, 0, 1))); |
1874 | /// ``` |
1875 | #[rustc_const_stable (feature = "const_ip_50" , since = "1.50.0" )] |
1876 | #[stable (feature = "rust1" , since = "1.0.0" )] |
1877 | #[must_use = "this returns the result of the operation, \ |
1878 | without modifying the original" ] |
1879 | #[inline ] |
1880 | pub const fn to_ipv4(&self) -> Option<Ipv4Addr> { |
1881 | if let [0, 0, 0, 0, 0, 0 | 0xffff, ab, cd] = self.segments() { |
1882 | let [a, b] = ab.to_be_bytes(); |
1883 | let [c, d] = cd.to_be_bytes(); |
1884 | Some(Ipv4Addr::new(a, b, c, d)) |
1885 | } else { |
1886 | None |
1887 | } |
1888 | } |
1889 | |
1890 | /// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped addresses, otherwise it |
1891 | /// returns self wrapped in an `IpAddr::V6`. |
1892 | /// |
1893 | /// # Examples |
1894 | /// |
1895 | /// ``` |
1896 | /// use std::net::Ipv6Addr; |
1897 | /// |
1898 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).is_loopback(), false); |
1899 | /// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).to_canonical().is_loopback(), true); |
1900 | /// ``` |
1901 | #[inline ] |
1902 | #[must_use = "this returns the result of the operation, \ |
1903 | without modifying the original" ] |
1904 | #[stable (feature = "ip_to_canonical" , since = "1.75.0" )] |
1905 | #[rustc_const_stable (feature = "ip_to_canonical" , since = "1.75.0" )] |
1906 | pub const fn to_canonical(&self) -> IpAddr { |
1907 | if let Some(mapped) = self.to_ipv4_mapped() { |
1908 | return IpAddr::V4(mapped); |
1909 | } |
1910 | IpAddr::V6(*self) |
1911 | } |
1912 | |
1913 | /// Returns the sixteen eight-bit integers the IPv6 address consists of. |
1914 | /// |
1915 | /// ``` |
1916 | /// use std::net::Ipv6Addr; |
1917 | /// |
1918 | /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).octets(), |
1919 | /// [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); |
1920 | /// ``` |
1921 | #[rustc_const_stable (feature = "const_ip_32" , since = "1.32.0" )] |
1922 | #[stable (feature = "ipv6_to_octets" , since = "1.12.0" )] |
1923 | #[must_use ] |
1924 | #[inline ] |
1925 | pub const fn octets(&self) -> [u8; 16] { |
1926 | self.octets |
1927 | } |
1928 | } |
1929 | |
1930 | /// Write an Ipv6Addr, conforming to the canonical style described by |
1931 | /// [RFC 5952](https://tools.ietf.org/html/rfc5952). |
1932 | #[stable (feature = "rust1" , since = "1.0.0" )] |
1933 | impl fmt::Display for Ipv6Addr { |
1934 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
1935 | // If there are no alignment requirements, write the IP address directly to `f`. |
1936 | // Otherwise, write it to a local buffer and then use `f.pad`. |
1937 | if f.precision().is_none() && f.width().is_none() { |
1938 | let segments = self.segments(); |
1939 | |
1940 | if let Some(ipv4) = self.to_ipv4_mapped() { |
1941 | write!(f, "::ffff: {}" , ipv4) |
1942 | } else { |
1943 | #[derive (Copy, Clone, Default)] |
1944 | struct Span { |
1945 | start: usize, |
1946 | len: usize, |
1947 | } |
1948 | |
1949 | // Find the inner 0 span |
1950 | let zeroes = { |
1951 | let mut longest = Span::default(); |
1952 | let mut current = Span::default(); |
1953 | |
1954 | for (i, &segment) in segments.iter().enumerate() { |
1955 | if segment == 0 { |
1956 | if current.len == 0 { |
1957 | current.start = i; |
1958 | } |
1959 | |
1960 | current.len += 1; |
1961 | |
1962 | if current.len > longest.len { |
1963 | longest = current; |
1964 | } |
1965 | } else { |
1966 | current = Span::default(); |
1967 | } |
1968 | } |
1969 | |
1970 | longest |
1971 | }; |
1972 | |
1973 | /// Write a colon-separated part of the address |
1974 | #[inline ] |
1975 | fn fmt_subslice(f: &mut fmt::Formatter<'_>, chunk: &[u16]) -> fmt::Result { |
1976 | if let Some((first, tail)) = chunk.split_first() { |
1977 | write!(f, " {:x}" , first)?; |
1978 | for segment in tail { |
1979 | f.write_char(':' )?; |
1980 | write!(f, " {:x}" , segment)?; |
1981 | } |
1982 | } |
1983 | Ok(()) |
1984 | } |
1985 | |
1986 | if zeroes.len > 1 { |
1987 | fmt_subslice(f, &segments[..zeroes.start])?; |
1988 | f.write_str("::" )?; |
1989 | fmt_subslice(f, &segments[zeroes.start + zeroes.len..]) |
1990 | } else { |
1991 | fmt_subslice(f, &segments) |
1992 | } |
1993 | } |
1994 | } else { |
1995 | const LONGEST_IPV6_ADDR: &str = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" ; |
1996 | |
1997 | let mut buf = DisplayBuffer::<{ LONGEST_IPV6_ADDR.len() }>::new(); |
1998 | // Buffer is long enough for the longest possible IPv6 address, so this should never fail. |
1999 | write!(buf, " {}" , self).unwrap(); |
2000 | |
2001 | f.pad(buf.as_str()) |
2002 | } |
2003 | } |
2004 | } |
2005 | |
2006 | #[stable (feature = "rust1" , since = "1.0.0" )] |
2007 | impl fmt::Debug for Ipv6Addr { |
2008 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
2009 | fmt::Display::fmt(self, f:fmt) |
2010 | } |
2011 | } |
2012 | |
2013 | #[stable (feature = "ip_cmp" , since = "1.16.0" )] |
2014 | impl PartialEq<IpAddr> for Ipv6Addr { |
2015 | #[inline ] |
2016 | fn eq(&self, other: &IpAddr) -> bool { |
2017 | match other { |
2018 | IpAddr::V4(_) => false, |
2019 | IpAddr::V6(v6: &Ipv6Addr) => self == v6, |
2020 | } |
2021 | } |
2022 | } |
2023 | |
2024 | #[stable (feature = "ip_cmp" , since = "1.16.0" )] |
2025 | impl PartialEq<Ipv6Addr> for IpAddr { |
2026 | #[inline ] |
2027 | fn eq(&self, other: &Ipv6Addr) -> bool { |
2028 | match self { |
2029 | IpAddr::V4(_) => false, |
2030 | IpAddr::V6(v6: &Ipv6Addr) => v6 == other, |
2031 | } |
2032 | } |
2033 | } |
2034 | |
2035 | #[stable (feature = "rust1" , since = "1.0.0" )] |
2036 | impl PartialOrd for Ipv6Addr { |
2037 | #[inline ] |
2038 | fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> { |
2039 | Some(self.cmp(other)) |
2040 | } |
2041 | } |
2042 | |
2043 | #[stable (feature = "ip_cmp" , since = "1.16.0" )] |
2044 | impl PartialOrd<Ipv6Addr> for IpAddr { |
2045 | #[inline ] |
2046 | fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> { |
2047 | match self { |
2048 | IpAddr::V4(_) => Some(Ordering::Less), |
2049 | IpAddr::V6(v6: &Ipv6Addr) => v6.partial_cmp(other), |
2050 | } |
2051 | } |
2052 | } |
2053 | |
2054 | #[stable (feature = "ip_cmp" , since = "1.16.0" )] |
2055 | impl PartialOrd<IpAddr> for Ipv6Addr { |
2056 | #[inline ] |
2057 | fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> { |
2058 | match other { |
2059 | IpAddr::V4(_) => Some(Ordering::Greater), |
2060 | IpAddr::V6(v6: &Ipv6Addr) => self.partial_cmp(v6), |
2061 | } |
2062 | } |
2063 | } |
2064 | |
2065 | #[stable (feature = "rust1" , since = "1.0.0" )] |
2066 | impl Ord for Ipv6Addr { |
2067 | #[inline ] |
2068 | fn cmp(&self, other: &Ipv6Addr) -> Ordering { |
2069 | self.segments().cmp(&other.segments()) |
2070 | } |
2071 | } |
2072 | |
2073 | #[stable (feature = "i128" , since = "1.26.0" )] |
2074 | impl From<Ipv6Addr> for u128 { |
2075 | /// Uses [`Ipv6Addr::to_bits`] to convert an IPv6 address to a host byte order `u128`. |
2076 | #[inline ] |
2077 | fn from(ip: Ipv6Addr) -> u128 { |
2078 | ip.to_bits() |
2079 | } |
2080 | } |
2081 | #[stable (feature = "i128" , since = "1.26.0" )] |
2082 | impl From<u128> for Ipv6Addr { |
2083 | /// Uses [`Ipv6Addr::from_bits`] to convert a host byte order `u128` to an IPv6 address. |
2084 | #[inline ] |
2085 | fn from(ip: u128) -> Ipv6Addr { |
2086 | Ipv6Addr::from_bits(ip) |
2087 | } |
2088 | } |
2089 | |
2090 | #[stable (feature = "ipv6_from_octets" , since = "1.9.0" )] |
2091 | impl From<[u8; 16]> for Ipv6Addr { |
2092 | /// Creates an `Ipv6Addr` from a sixteen element byte array. |
2093 | /// |
2094 | /// # Examples |
2095 | /// |
2096 | /// ``` |
2097 | /// use std::net::Ipv6Addr; |
2098 | /// |
2099 | /// let addr = Ipv6Addr::from([ |
2100 | /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8, |
2101 | /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8, |
2102 | /// ]); |
2103 | /// assert_eq!( |
2104 | /// Ipv6Addr::new( |
2105 | /// 0x1918, 0x1716, |
2106 | /// 0x1514, 0x1312, |
2107 | /// 0x1110, 0x0f0e, |
2108 | /// 0x0d0c, 0x0b0a |
2109 | /// ), |
2110 | /// addr |
2111 | /// ); |
2112 | /// ``` |
2113 | #[inline ] |
2114 | fn from(octets: [u8; 16]) -> Ipv6Addr { |
2115 | Ipv6Addr { octets } |
2116 | } |
2117 | } |
2118 | |
2119 | #[stable (feature = "ipv6_from_segments" , since = "1.16.0" )] |
2120 | impl From<[u16; 8]> for Ipv6Addr { |
2121 | /// Creates an `Ipv6Addr` from an eight element 16-bit array. |
2122 | /// |
2123 | /// # Examples |
2124 | /// |
2125 | /// ``` |
2126 | /// use std::net::Ipv6Addr; |
2127 | /// |
2128 | /// let addr = Ipv6Addr::from([ |
2129 | /// 525u16, 524u16, 523u16, 522u16, |
2130 | /// 521u16, 520u16, 519u16, 518u16, |
2131 | /// ]); |
2132 | /// assert_eq!( |
2133 | /// Ipv6Addr::new( |
2134 | /// 0x20d, 0x20c, |
2135 | /// 0x20b, 0x20a, |
2136 | /// 0x209, 0x208, |
2137 | /// 0x207, 0x206 |
2138 | /// ), |
2139 | /// addr |
2140 | /// ); |
2141 | /// ``` |
2142 | #[inline ] |
2143 | fn from(segments: [u16; 8]) -> Ipv6Addr { |
2144 | let [a, b, c, d, e, f, g, h] = segments; |
2145 | Ipv6Addr::new(a, b, c, d, e, f, g, h) |
2146 | } |
2147 | } |
2148 | |
2149 | #[stable (feature = "ip_from_slice" , since = "1.17.0" )] |
2150 | impl From<[u8; 16]> for IpAddr { |
2151 | /// Creates an `IpAddr::V6` from a sixteen element byte array. |
2152 | /// |
2153 | /// # Examples |
2154 | /// |
2155 | /// ``` |
2156 | /// use std::net::{IpAddr, Ipv6Addr}; |
2157 | /// |
2158 | /// let addr = IpAddr::from([ |
2159 | /// 25u8, 24u8, 23u8, 22u8, 21u8, 20u8, 19u8, 18u8, |
2160 | /// 17u8, 16u8, 15u8, 14u8, 13u8, 12u8, 11u8, 10u8, |
2161 | /// ]); |
2162 | /// assert_eq!( |
2163 | /// IpAddr::V6(Ipv6Addr::new( |
2164 | /// 0x1918, 0x1716, |
2165 | /// 0x1514, 0x1312, |
2166 | /// 0x1110, 0x0f0e, |
2167 | /// 0x0d0c, 0x0b0a |
2168 | /// )), |
2169 | /// addr |
2170 | /// ); |
2171 | /// ``` |
2172 | #[inline ] |
2173 | fn from(octets: [u8; 16]) -> IpAddr { |
2174 | IpAddr::V6(Ipv6Addr::from(octets)) |
2175 | } |
2176 | } |
2177 | |
2178 | #[stable (feature = "ip_from_slice" , since = "1.17.0" )] |
2179 | impl From<[u16; 8]> for IpAddr { |
2180 | /// Creates an `IpAddr::V6` from an eight element 16-bit array. |
2181 | /// |
2182 | /// # Examples |
2183 | /// |
2184 | /// ``` |
2185 | /// use std::net::{IpAddr, Ipv6Addr}; |
2186 | /// |
2187 | /// let addr = IpAddr::from([ |
2188 | /// 525u16, 524u16, 523u16, 522u16, |
2189 | /// 521u16, 520u16, 519u16, 518u16, |
2190 | /// ]); |
2191 | /// assert_eq!( |
2192 | /// IpAddr::V6(Ipv6Addr::new( |
2193 | /// 0x20d, 0x20c, |
2194 | /// 0x20b, 0x20a, |
2195 | /// 0x209, 0x208, |
2196 | /// 0x207, 0x206 |
2197 | /// )), |
2198 | /// addr |
2199 | /// ); |
2200 | /// ``` |
2201 | #[inline ] |
2202 | fn from(segments: [u16; 8]) -> IpAddr { |
2203 | IpAddr::V6(Ipv6Addr::from(segments)) |
2204 | } |
2205 | } |
2206 | |
2207 | #[stable (feature = "ip_bitops" , since = "1.75.0" )] |
2208 | impl Not for Ipv4Addr { |
2209 | type Output = Ipv4Addr; |
2210 | |
2211 | #[inline ] |
2212 | fn not(mut self) -> Ipv4Addr { |
2213 | for octet: &mut u8 in &mut self.octets { |
2214 | *octet = !*octet; |
2215 | } |
2216 | self |
2217 | } |
2218 | } |
2219 | |
2220 | #[stable (feature = "ip_bitops" , since = "1.75.0" )] |
2221 | impl Not for &'_ Ipv4Addr { |
2222 | type Output = Ipv4Addr; |
2223 | |
2224 | #[inline ] |
2225 | fn not(self) -> Ipv4Addr { |
2226 | !*self |
2227 | } |
2228 | } |
2229 | |
2230 | #[stable (feature = "ip_bitops" , since = "1.75.0" )] |
2231 | impl Not for Ipv6Addr { |
2232 | type Output = Ipv6Addr; |
2233 | |
2234 | #[inline ] |
2235 | fn not(mut self) -> Ipv6Addr { |
2236 | for octet: &mut u8 in &mut self.octets { |
2237 | *octet = !*octet; |
2238 | } |
2239 | self |
2240 | } |
2241 | } |
2242 | |
2243 | #[stable (feature = "ip_bitops" , since = "1.75.0" )] |
2244 | impl Not for &'_ Ipv6Addr { |
2245 | type Output = Ipv6Addr; |
2246 | |
2247 | #[inline ] |
2248 | fn not(self) -> Ipv6Addr { |
2249 | !*self |
2250 | } |
2251 | } |
2252 | |
2253 | macro_rules! bitop_impls { |
2254 | ($( |
2255 | $(#[$attr:meta])* |
2256 | impl ($BitOp:ident, $BitOpAssign:ident) for $ty:ty = ($bitop:ident, $bitop_assign:ident); |
2257 | )*) => { |
2258 | $( |
2259 | $(#[$attr])* |
2260 | impl $BitOpAssign for $ty { |
2261 | fn $bitop_assign(&mut self, rhs: $ty) { |
2262 | for (lhs, rhs) in iter::zip(&mut self.octets, rhs.octets) { |
2263 | lhs.$bitop_assign(rhs); |
2264 | } |
2265 | } |
2266 | } |
2267 | |
2268 | $(#[$attr])* |
2269 | impl $BitOpAssign<&'_ $ty> for $ty { |
2270 | fn $bitop_assign(&mut self, rhs: &'_ $ty) { |
2271 | self.$bitop_assign(*rhs); |
2272 | } |
2273 | } |
2274 | |
2275 | $(#[$attr])* |
2276 | impl $BitOp for $ty { |
2277 | type Output = $ty; |
2278 | |
2279 | #[inline] |
2280 | fn $bitop(mut self, rhs: $ty) -> $ty { |
2281 | self.$bitop_assign(rhs); |
2282 | self |
2283 | } |
2284 | } |
2285 | |
2286 | $(#[$attr])* |
2287 | impl $BitOp<&'_ $ty> for $ty { |
2288 | type Output = $ty; |
2289 | |
2290 | #[inline] |
2291 | fn $bitop(mut self, rhs: &'_ $ty) -> $ty { |
2292 | self.$bitop_assign(*rhs); |
2293 | self |
2294 | } |
2295 | } |
2296 | |
2297 | $(#[$attr])* |
2298 | impl $BitOp<$ty> for &'_ $ty { |
2299 | type Output = $ty; |
2300 | |
2301 | #[inline] |
2302 | fn $bitop(self, rhs: $ty) -> $ty { |
2303 | let mut lhs = *self; |
2304 | lhs.$bitop_assign(rhs); |
2305 | lhs |
2306 | } |
2307 | } |
2308 | |
2309 | $(#[$attr])* |
2310 | impl $BitOp<&'_ $ty> for &'_ $ty { |
2311 | type Output = $ty; |
2312 | |
2313 | #[inline] |
2314 | fn $bitop(self, rhs: &'_ $ty) -> $ty { |
2315 | let mut lhs = *self; |
2316 | lhs.$bitop_assign(*rhs); |
2317 | lhs |
2318 | } |
2319 | } |
2320 | )* |
2321 | }; |
2322 | } |
2323 | |
2324 | bitop_impls! { |
2325 | #[stable (feature = "ip_bitops" , since = "1.75.0" )] |
2326 | impl (BitAnd, BitAndAssign) for Ipv4Addr = (bitand, bitand_assign); |
2327 | #[stable (feature = "ip_bitops" , since = "1.75.0" )] |
2328 | impl (BitOr, BitOrAssign) for Ipv4Addr = (bitor, bitor_assign); |
2329 | |
2330 | #[stable (feature = "ip_bitops" , since = "1.75.0" )] |
2331 | impl (BitAnd, BitAndAssign) for Ipv6Addr = (bitand, bitand_assign); |
2332 | #[stable (feature = "ip_bitops" , since = "1.75.0" )] |
2333 | impl (BitOr, BitOrAssign) for Ipv6Addr = (bitor, bitor_assign); |
2334 | } |
2335 | |