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