net_declare/
lib.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Macros for constructing networking address types from the standard library,
6//! FIDL, and [`net_types`] from human-readable representations.
7
8/// Declares an [`std::net::IpAddr`] from a parsable IP address (either V4 or
9/// V6) string.
10pub use net_declare_macros::std_ip;
11/// Declares an [`std::net::Ipv4Addr`] from a parsable IPv4 address string.
12pub use net_declare_macros::std_ip_v4;
13/// Declares an [`std::net::Ipv6Addr`] from a parsable IPv6 address string.
14pub use net_declare_macros::std_ip_v6;
15/// Declares an [`std::net::SocketAddr`] from a parsable IP address + port
16/// string (either V4 or V6).
17///
18/// NOTE: `std::net::SocketAddrV6` does not support parsing scope_id from
19/// strings, meaning the generated IPv6 socket address will always have
20/// `scope_id=0`. See [Rust issue 1992].
21///
22/// [Rust issue 1992]: https://github.com/rust-lang/rfcs/issues/1992
23pub use net_declare_macros::std_socket_addr;
24/// Declares an [`std::net::SocketAddrV4`] from a parsable IPv4 address + port
25/// in the form `addr:port`.
26pub use net_declare_macros::std_socket_addr_v4;
27/// Declares an [`std::net::SocketAddrV6`] from a parsable IPv6 address + port
28/// in the form `[addr]:port`.
29///
30/// NOTE: `std::net::SocketAddrV6` does not support parsing scope_id from
31/// strings, meaning the generated IPv6 socket address will always have
32/// `scope_id=0`. See [Rust issue 1992].
33///
34/// [Rust issue 1992]: https://github.com/rust-lang/rfcs/issues/1992
35pub use net_declare_macros::std_socket_addr_v6;
36
37/// Declares a [`fidl_fuchsia_net::IpAddress`] from a parsable IP address
38/// (either V4 or V6) string.
39pub use net_declare_macros::fidl_ip;
40/// Declares a [`fidl_fuchsia_net::Ipv4Address`] from a parsable IPv4 address
41/// string.
42pub use net_declare_macros::fidl_ip_v4;
43/// Declares a [`fidl_fuchsia_net::Ipv4AddressWithPrefix`] from a parsable IPv4
44/// + prefix length string, e.g. `192.168.0.1/24`.
45pub use net_declare_macros::fidl_ip_v4_with_prefix;
46/// Declares a [`fidl_fuchsia_net::Ipv6Address`] from a parsable IPv6 address
47/// string.
48pub use net_declare_macros::fidl_ip_v6;
49/// Declares a [`fidl_fuchsia_net::Ipv6AddressWithPrefix`] from a parsable IPv6
50/// + prefix length string, e.g. `ff08::1/88`.
51pub use net_declare_macros::fidl_ip_v6_with_prefix;
52/// Declares a [`fidl_fuchsia_net::MacAddress`] from a parsable MAC address in
53/// the form `aa:bb:cc:dd:ee:ff`.
54pub use net_declare_macros::fidl_mac;
55/// Declares an [`fidl_fuchsia_net::SocketAddress`] from a parsable IP address +
56/// port string (either V4 or V6).
57///
58/// NOTE: `std::net::SocketAddrV6` does not support parsing scope_id from
59/// strings, meaning the generated IPv6 socket address will always have
60/// `zone_index=0`. See [Rust issue 1992].
61///
62/// [Rust issue 1992]: https://github.com/rust-lang/rfcs/issues/1992
63pub use net_declare_macros::fidl_socket_addr;
64/// Declares a [`fidl_fuchsia_net::Ipv4SocketAddress`] from a parsable IPv4
65/// address + port in the form `addr:port`.
66pub use net_declare_macros::fidl_socket_addr_v4;
67/// Declares a [`fidl_fuchsia_net::Ipv6SocketAddress`] from a parsable IPv6
68/// address + port in the form `[addr]:port`.
69///
70/// NOTE: `std::net::SocketAddrV6` does not support parsing scope_id from
71/// strings, meaning the generated IPv6 socket address will always have
72/// `scope_id=0`. See [Rust issue 1992].
73///
74/// [Rust issue 1992]: https://github.com/rust-lang/rfcs/issues/1992
75pub use net_declare_macros::fidl_socket_addr_v6;
76/// Declares a [`fidl_fuchsia_net::Subnet`] from a parsable CIDR address string
77/// in the form `addr/prefix`, e.g. `192.168.0.1/24` or `ff08::1/64`.
78pub use net_declare_macros::fidl_subnet;
79
80/// Declares a [`net_types::ip::AddrSubnetEither`] from a parsable IP address +
81/// prefix length string.
82pub use net_declare_macros::net_addr_subnet;
83/// Declares a [`net_types::ip::AddrSubnet<Ipv4Addr>`] from a parsable IPv4
84/// address + prefix length string.
85pub use net_declare_macros::net_addr_subnet_v4;
86/// Declares a [`net_types::ip::AddrSubnet<Ipv6Addr>`] from a parsable IPv6
87/// address + prefix length string.
88pub use net_declare_macros::net_addr_subnet_v6;
89/// Declares a [`net_types::ip::IpAddr`] from a parsable IP address (either V4
90/// or V6) string.
91pub use net_declare_macros::net_ip;
92/// Declares a [`net_types::ip::Ipv4Addr`] from a parsable IPv4 address string.
93pub use net_declare_macros::net_ip_v4;
94/// Declares a [`net_types::ip::Ipv6Addr`] from a parsable IPv6 address string.
95pub use net_declare_macros::net_ip_v6;
96/// Declares a [`net_types::ethernet::Mac`] from a parsable MAC address in
97/// the form `aa:bb:cc:dd:ee:ff`.
98pub use net_declare_macros::net_mac;
99/// Declares a [`net_types::ip::Subnet<Ipv4Addr>`] from a parsable IPv4 CIDR
100/// address string.
101pub use net_declare_macros::net_subnet_v4;
102/// Declares a [`net_types::ip::Subnet<Ipv6Addr>`] from a parsable IPv6 CIDR
103/// address string.
104pub use net_declare_macros::net_subnet_v6;
105
106/// Declares a [`net_types::ip::PrefixLength<Ipv4>`] from an integer literal.
107pub use net_declare_macros::net_prefix_length_v4;
108
109/// Declares a [`net_types::ip::PrefixLength<Ipv6>`] from an integer literal.
110pub use net_declare_macros::net_prefix_length_v6;
111
112/// Redeclaration of macros to generate `std` types.
113pub mod std {
114    pub use super::{
115        std_ip as ip, std_ip_v4 as ip_v4, std_ip_v6 as ip_v6, std_socket_addr as socket_addr,
116        std_socket_addr_v4 as socket_addr_v4, std_socket_addr_v6 as socket_addr_v6,
117    };
118}
119
120/// Redeclaration of macros to generate `fidl` types.
121pub mod fidl {
122    pub use super::{
123        fidl_ip as ip, fidl_ip_v4 as ip_v4, fidl_ip_v6 as ip_v6, fidl_mac as mac,
124        fidl_socket_addr as socket_addr, fidl_socket_addr_v4 as socket_addr_v4,
125        fidl_socket_addr_v6 as socket_addr_v6, fidl_subnet as subnet,
126    };
127}
128
129/// Redeclaration of macros to generate `net_types` types.
130pub mod net {
131    pub use super::{
132        net_ip as ip, net_ip_v4 as ip_v4, net_ip_v6 as ip_v6, net_mac as mac,
133        net_prefix_length_v4 as prefix_length_v4, net_prefix_length_v6 as prefix_length_v6,
134        net_subnet_v4 as subnet_v4, net_subnet_v6 as subnet_v6,
135    };
136}
137
138#[cfg(test)]
139mod tests {
140    use super::*;
141    use net_declare_macros::net_prefix_length_v4;
142    use {::std, fidl_fuchsia_net as fidl};
143
144    #[test]
145    fn test_std_ip() {
146        assert_eq!(
147            std::net::IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 0, 1)),
148            std_ip!("192.168.0.1")
149        );
150        assert_eq!(
151            std::net::IpAddr::V6(std::net::Ipv6Addr::new(0xFF01, 0, 0, 0, 0, 0, 0, 0x0102)),
152            std_ip!("ff01::0102")
153        );
154    }
155
156    #[test]
157    fn test_std_ip_v4() {
158        assert_eq!(std::net::Ipv4Addr::new(192, 168, 0, 1), std_ip_v4!("192.168.0.1"));
159    }
160
161    #[test]
162    fn test_std_ip_v6() {
163        assert_eq!(
164            std::net::Ipv6Addr::new(0xFF01, 0, 0, 0, 0, 0, 0, 0x0102),
165            std_ip_v6!("ff01::0102")
166        );
167    }
168
169    #[test]
170    fn test_std_socket_addr() {
171        assert_eq!(
172            std::net::SocketAddr::V4(std::net::SocketAddrV4::new(
173                std::net::Ipv4Addr::new(192, 168, 0, 1),
174                8080
175            )),
176            std_socket_addr!("192.168.0.1:8080")
177        );
178        assert_eq!(
179            std::net::SocketAddr::V6(std::net::SocketAddrV6::new(
180                std::net::Ipv6Addr::new(0xFF01, 0, 0, 0, 0, 0, 0, 0x0102),
181                8080,
182                0,
183                0
184            )),
185            std_socket_addr!("[ff01::0102]:8080")
186        );
187    }
188
189    #[test]
190    fn test_std_socket_addr_v4() {
191        assert_eq!(
192            std::net::SocketAddrV4::new(std::net::Ipv4Addr::new(192, 168, 0, 1), 8080),
193            std_socket_addr_v4!("192.168.0.1:8080")
194        );
195    }
196
197    #[test]
198    fn test_std_socket_addr_v6() {
199        assert_eq!(
200            std::net::SocketAddrV6::new(
201                std::net::Ipv6Addr::new(0xFF01, 0, 0, 0, 0, 0, 0, 0x0102),
202                8080,
203                0,
204                0
205            ),
206            std_socket_addr_v6!("[ff01::0102]:8080")
207        );
208    }
209
210    #[test]
211    fn test_fidl_ip() {
212        assert_eq!(
213            fidl::IpAddress::Ipv4(fidl::Ipv4Address { addr: [192, 168, 0, 1] }),
214            fidl_ip!("192.168.0.1")
215        );
216
217        assert_eq!(
218            fidl::IpAddress::Ipv6(fidl::Ipv6Address {
219                addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
220            }),
221            fidl_ip!("ff01::0102")
222        );
223    }
224
225    #[test]
226    fn test_fidl_ip_v4() {
227        assert_eq!(fidl::Ipv4Address { addr: [192, 168, 0, 1] }, fidl_ip_v4!("192.168.0.1"));
228    }
229
230    #[test]
231    fn test_fidl_ip_v6() {
232        assert_eq!(
233            fidl::Ipv6Address {
234                addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
235            },
236            fidl_ip_v6!("ff01::0102")
237        );
238    }
239
240    #[test]
241    fn test_fidl_ip_v6_v4_mapped() {
242        assert_eq!(
243            fidl::Ipv6Address { addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 192, 168, 0, 1] },
244            fidl_ip_v6!("::ffff:192.168.0.1")
245        );
246    }
247
248    #[test]
249    fn test_fidl_socket_addr() {
250        assert_eq!(
251            fidl::SocketAddress::Ipv4(fidl::Ipv4SocketAddress {
252                address: fidl::Ipv4Address { addr: [192, 168, 0, 1] },
253                port: 8080
254            }),
255            fidl_socket_addr!("192.168.0.1:8080")
256        );
257
258        assert_eq!(
259            fidl::SocketAddress::Ipv6(fidl::Ipv6SocketAddress {
260                address: fidl::Ipv6Address {
261                    addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
262                },
263                port: 8080,
264                zone_index: 0,
265            }),
266            fidl_socket_addr!("[ff01::0102]:8080")
267        );
268    }
269
270    #[test]
271    fn test_fidl_socket_addr_v4() {
272        assert_eq!(
273            fidl::Ipv4SocketAddress {
274                address: fidl::Ipv4Address { addr: [192, 168, 0, 1] },
275                port: 8080
276            },
277            fidl_socket_addr_v4!("192.168.0.1:8080")
278        );
279    }
280
281    #[test]
282    fn test_fidl_socket_addr_v6() {
283        assert_eq!(
284            fidl::Ipv6SocketAddress {
285                address: fidl::Ipv6Address {
286                    addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
287                },
288                port: 8080,
289                zone_index: 0,
290            },
291            fidl_socket_addr_v6!("[ff01::0102]:8080")
292        );
293    }
294
295    #[test]
296    fn test_fidl_mac() {
297        assert_eq!(fidl::MacAddress { octets: [0, 1, 2, 3, 4, 5] }, fidl_mac!("00:01:02:03:04:05"));
298    }
299
300    #[test]
301    fn test_accept_quotes() {
302        // Rustfmt gets confused with this syntax sometimes, so we allow macros
303        // to receive what looks like a string literal as well.
304        assert_eq!(
305            fidl::MacAddress { octets: [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF] },
306            fidl_mac!("AA:BB:CC:DD:EE:FF")
307        );
308    }
309
310    #[test]
311    fn test_fidl_ip_v4_with_prefix() {
312        assert_eq!(
313            fidl::Ipv4AddressWithPrefix {
314                addr: fidl::Ipv4Address { addr: [192, 168, 0, 1] },
315                prefix_len: 24
316            },
317            fidl_ip_v4_with_prefix!("192.168.0.1/24")
318        );
319    }
320
321    #[test]
322    fn test_fidl_ip_v6_with_prefix() {
323        assert_eq!(
324            fidl::Ipv6AddressWithPrefix {
325                addr: fidl::Ipv6Address {
326                    addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
327                },
328                prefix_len: 64
329            },
330            fidl_ip_v6_with_prefix!("ff01::0102/64")
331        );
332    }
333
334    #[test]
335    fn test_fidl_subnet_v4() {
336        assert_eq!(
337            fidl::Subnet {
338                addr: fidl::IpAddress::Ipv4(fidl::Ipv4Address { addr: [192, 168, 0, 1] }),
339                prefix_len: 24
340            },
341            fidl_subnet!("192.168.0.1/24")
342        );
343    }
344
345    #[test]
346    fn test_fidl_subnet_v6() {
347        assert_eq!(
348            fidl::Subnet {
349                addr: fidl::IpAddress::Ipv6(fidl::Ipv6Address {
350                    addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
351                }),
352                prefix_len: 64
353            },
354            fidl_subnet!("ff01::0102/64")
355        );
356    }
357
358    #[test]
359    fn test_net_ip() {
360        assert_eq!(
361            net_types::ip::IpAddr::from(net_types::ip::Ipv4Addr::new([192, 168, 0, 1])),
362            net_ip!("192.168.0.1")
363        );
364
365        assert_eq!(
366            net_types::ip::IpAddr::from(net_types::ip::Ipv6Addr::new([
367                0xFF01, 0, 0, 0, 0, 0, 0, 0x0102
368            ])),
369            net_ip!("ff01::0102"),
370        );
371    }
372
373    #[test]
374    fn test_net_ip_v4() {
375        assert_eq!(net_types::ip::Ipv4Addr::new([192, 168, 0, 1]), net_ip_v4!("192.168.0.1"),);
376    }
377
378    #[test]
379    fn test_net_ip_v6() {
380        assert_eq!(
381            net_types::ip::Ipv6Addr::new([0xFF01, 0, 0, 0, 0, 0, 0, 0x0102]),
382            net_ip_v6!("ff01::0102"),
383        );
384    }
385
386    #[test]
387    fn test_net_ip_v6_v4_mapped() {
388        assert_eq!(
389            net_types::ip::Ipv6Addr::from_bytes([
390                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 192, 168, 0, 1
391            ]),
392            net_ip_v6!("::ffff:192.168.0.1"),
393        )
394    }
395
396    #[test]
397    fn test_net_mac() {
398        assert_eq!(
399            net_types::ethernet::Mac::new([0, 1, 2, 3, 4, 5]),
400            net_mac!("00:01:02:03:04:05")
401        );
402    }
403
404    #[test]
405    fn test_net_subnet_v4() {
406        assert_eq!(
407            net_types::ip::Subnet::new(net_types::ip::Ipv4Addr::new([18, 6, 0, 0]), 15).unwrap(),
408            net_subnet_v4!("18.6.0.0/15")
409        )
410    }
411
412    #[test]
413    fn test_net_subnet_v6() {
414        assert_eq!(
415            net_types::ip::Subnet::new(
416                net_types::ip::Ipv6Addr::new([0xff80, 0, 0, 0, 0, 0, 0, 0]),
417                12
418            )
419            .unwrap(),
420            net_subnet_v6!("ff80::/12")
421        )
422    }
423
424    #[test]
425    fn test_net_prefix_length_v4() {
426        assert_eq!(
427            net_types::ip::PrefixLength::<net_types::ip::Ipv4>::new(23).unwrap(),
428            net_prefix_length_v4!(23)
429        )
430    }
431
432    #[test]
433    fn test_net_prefix_length_v6() {
434        assert_eq!(
435            net_types::ip::PrefixLength::<net_types::ip::Ipv6>::new(125).unwrap(),
436            net_prefix_length_v6!(125)
437        )
438    }
439}