Skip to main content

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 ::std;
142    use fidl_fuchsia_net as fidl;
143    use net_declare_macros::net_prefix_length_v4;
144
145    #[test]
146    fn test_std_ip() {
147        assert_eq!(
148            std::net::IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 0, 1)),
149            std_ip!("192.168.0.1")
150        );
151        assert_eq!(
152            std::net::IpAddr::V6(std::net::Ipv6Addr::new(0xFF01, 0, 0, 0, 0, 0, 0, 0x0102)),
153            std_ip!("ff01::0102")
154        );
155    }
156
157    #[test]
158    fn test_std_ip_v4() {
159        assert_eq!(std::net::Ipv4Addr::new(192, 168, 0, 1), std_ip_v4!("192.168.0.1"));
160    }
161
162    #[test]
163    fn test_std_ip_v6() {
164        assert_eq!(
165            std::net::Ipv6Addr::new(0xFF01, 0, 0, 0, 0, 0, 0, 0x0102),
166            std_ip_v6!("ff01::0102")
167        );
168    }
169
170    #[test]
171    fn test_std_socket_addr() {
172        assert_eq!(
173            std::net::SocketAddr::V4(std::net::SocketAddrV4::new(
174                std::net::Ipv4Addr::new(192, 168, 0, 1),
175                8080
176            )),
177            std_socket_addr!("192.168.0.1:8080")
178        );
179        assert_eq!(
180            std::net::SocketAddr::V6(std::net::SocketAddrV6::new(
181                std::net::Ipv6Addr::new(0xFF01, 0, 0, 0, 0, 0, 0, 0x0102),
182                8080,
183                0,
184                0
185            )),
186            std_socket_addr!("[ff01::0102]:8080")
187        );
188    }
189
190    #[test]
191    fn test_std_socket_addr_v4() {
192        assert_eq!(
193            std::net::SocketAddrV4::new(std::net::Ipv4Addr::new(192, 168, 0, 1), 8080),
194            std_socket_addr_v4!("192.168.0.1:8080")
195        );
196    }
197
198    #[test]
199    fn test_std_socket_addr_v6() {
200        assert_eq!(
201            std::net::SocketAddrV6::new(
202                std::net::Ipv6Addr::new(0xFF01, 0, 0, 0, 0, 0, 0, 0x0102),
203                8080,
204                0,
205                0
206            ),
207            std_socket_addr_v6!("[ff01::0102]:8080")
208        );
209    }
210
211    #[test]
212    fn test_fidl_ip() {
213        assert_eq!(
214            fidl::IpAddress::Ipv4(fidl::Ipv4Address { addr: [192, 168, 0, 1] }),
215            fidl_ip!("192.168.0.1")
216        );
217
218        assert_eq!(
219            fidl::IpAddress::Ipv6(fidl::Ipv6Address {
220                addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
221            }),
222            fidl_ip!("ff01::0102")
223        );
224    }
225
226    #[test]
227    fn test_fidl_ip_v4() {
228        assert_eq!(fidl::Ipv4Address { addr: [192, 168, 0, 1] }, fidl_ip_v4!("192.168.0.1"));
229    }
230
231    #[test]
232    fn test_fidl_ip_v6() {
233        assert_eq!(
234            fidl::Ipv6Address {
235                addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
236            },
237            fidl_ip_v6!("ff01::0102")
238        );
239    }
240
241    #[test]
242    fn test_fidl_ip_v6_v4_mapped() {
243        assert_eq!(
244            fidl::Ipv6Address { addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 192, 168, 0, 1] },
245            fidl_ip_v6!("::ffff:192.168.0.1")
246        );
247    }
248
249    #[test]
250    fn test_fidl_socket_addr() {
251        assert_eq!(
252            fidl::SocketAddress::Ipv4(fidl::Ipv4SocketAddress {
253                address: fidl::Ipv4Address { addr: [192, 168, 0, 1] },
254                port: 8080
255            }),
256            fidl_socket_addr!("192.168.0.1:8080")
257        );
258
259        assert_eq!(
260            fidl::SocketAddress::Ipv6(fidl::Ipv6SocketAddress {
261                address: fidl::Ipv6Address {
262                    addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
263                },
264                port: 8080,
265                zone_index: 0,
266            }),
267            fidl_socket_addr!("[ff01::0102]:8080")
268        );
269    }
270
271    #[test]
272    fn test_fidl_socket_addr_v4() {
273        assert_eq!(
274            fidl::Ipv4SocketAddress {
275                address: fidl::Ipv4Address { addr: [192, 168, 0, 1] },
276                port: 8080
277            },
278            fidl_socket_addr_v4!("192.168.0.1:8080")
279        );
280    }
281
282    #[test]
283    fn test_fidl_socket_addr_v6() {
284        assert_eq!(
285            fidl::Ipv6SocketAddress {
286                address: fidl::Ipv6Address {
287                    addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
288                },
289                port: 8080,
290                zone_index: 0,
291            },
292            fidl_socket_addr_v6!("[ff01::0102]:8080")
293        );
294    }
295
296    #[test]
297    fn test_fidl_mac() {
298        assert_eq!(fidl::MacAddress { octets: [0, 1, 2, 3, 4, 5] }, fidl_mac!("00:01:02:03:04:05"));
299    }
300
301    #[test]
302    fn test_accept_quotes() {
303        // Rustfmt gets confused with this syntax sometimes, so we allow macros
304        // to receive what looks like a string literal as well.
305        assert_eq!(
306            fidl::MacAddress { octets: [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF] },
307            fidl_mac!("AA:BB:CC:DD:EE:FF")
308        );
309    }
310
311    #[test]
312    fn test_fidl_ip_v4_with_prefix() {
313        assert_eq!(
314            fidl::Ipv4AddressWithPrefix {
315                addr: fidl::Ipv4Address { addr: [192, 168, 0, 1] },
316                prefix_len: 24
317            },
318            fidl_ip_v4_with_prefix!("192.168.0.1/24")
319        );
320    }
321
322    #[test]
323    fn test_fidl_ip_v6_with_prefix() {
324        assert_eq!(
325            fidl::Ipv6AddressWithPrefix {
326                addr: fidl::Ipv6Address {
327                    addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
328                },
329                prefix_len: 64
330            },
331            fidl_ip_v6_with_prefix!("ff01::0102/64")
332        );
333    }
334
335    #[test]
336    fn test_fidl_subnet_v4() {
337        assert_eq!(
338            fidl::Subnet {
339                addr: fidl::IpAddress::Ipv4(fidl::Ipv4Address { addr: [192, 168, 0, 1] }),
340                prefix_len: 24
341            },
342            fidl_subnet!("192.168.0.1/24")
343        );
344    }
345
346    #[test]
347    fn test_fidl_subnet_v6() {
348        assert_eq!(
349            fidl::Subnet {
350                addr: fidl::IpAddress::Ipv6(fidl::Ipv6Address {
351                    addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
352                }),
353                prefix_len: 64
354            },
355            fidl_subnet!("ff01::0102/64")
356        );
357    }
358
359    #[test]
360    fn test_net_ip() {
361        assert_eq!(
362            net_types::ip::IpAddr::from(net_types::ip::Ipv4Addr::new([192, 168, 0, 1])),
363            net_ip!("192.168.0.1")
364        );
365
366        assert_eq!(
367            net_types::ip::IpAddr::from(net_types::ip::Ipv6Addr::new([
368                0xFF01, 0, 0, 0, 0, 0, 0, 0x0102
369            ])),
370            net_ip!("ff01::0102"),
371        );
372    }
373
374    #[test]
375    fn test_net_ip_v4() {
376        assert_eq!(net_types::ip::Ipv4Addr::new([192, 168, 0, 1]), net_ip_v4!("192.168.0.1"),);
377    }
378
379    #[test]
380    fn test_net_ip_v6() {
381        assert_eq!(
382            net_types::ip::Ipv6Addr::new([0xFF01, 0, 0, 0, 0, 0, 0, 0x0102]),
383            net_ip_v6!("ff01::0102"),
384        );
385    }
386
387    #[test]
388    fn test_net_ip_v6_v4_mapped() {
389        assert_eq!(
390            net_types::ip::Ipv6Addr::from_bytes([
391                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 192, 168, 0, 1
392            ]),
393            net_ip_v6!("::ffff:192.168.0.1"),
394        )
395    }
396
397    #[test]
398    fn test_net_mac() {
399        assert_eq!(
400            net_types::ethernet::Mac::new([0, 1, 2, 3, 4, 5]),
401            net_mac!("00:01:02:03:04:05")
402        );
403    }
404
405    #[test]
406    fn test_net_subnet_v4() {
407        assert_eq!(
408            net_types::ip::Subnet::new(net_types::ip::Ipv4Addr::new([18, 6, 0, 0]), 15).unwrap(),
409            net_subnet_v4!("18.6.0.0/15")
410        )
411    }
412
413    #[test]
414    fn test_net_subnet_v6() {
415        assert_eq!(
416            net_types::ip::Subnet::new(
417                net_types::ip::Ipv6Addr::new([0xff80, 0, 0, 0, 0, 0, 0, 0]),
418                12
419            )
420            .unwrap(),
421            net_subnet_v6!("ff80::/12")
422        )
423    }
424
425    #[test]
426    fn test_net_prefix_length_v4() {
427        assert_eq!(
428            net_types::ip::PrefixLength::<net_types::ip::Ipv4>::new(23).unwrap(),
429            net_prefix_length_v4!(23)
430        )
431    }
432
433    #[test]
434    fn test_net_prefix_length_v6() {
435        assert_eq!(
436            net_types::ip::PrefixLength::<net_types::ip::Ipv6>::new(125).unwrap(),
437            net_prefix_length_v6!(125)
438        )
439    }
440}