net_declare/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Macros for constructing networking address types from the standard library,
//! FIDL, and [`net_types`] from human-readable representations.

/// Declares an [`std::net::IpAddr`] from a parsable IP address (either V4 or
/// V6) string.
pub use net_declare_macros::std_ip;
/// Declares an [`std::net::Ipv4Addr`] from a parsable IPv4 address string.
pub use net_declare_macros::std_ip_v4;
/// Declares an [`std::net::Ipv6Addr`] from a parsable IPv6 address string.
pub use net_declare_macros::std_ip_v6;
/// Declares an [`std::net::SocketAddr`] from a parsable IP address + port
/// string (either V4 or V6).
///
/// NOTE: `std::net::SocketAddrV6` does not support parsing scope_id from
/// strings, meaning the generated IPv6 socket address will always have
/// `scope_id=0`. See [Rust issue 1992].
///
/// [Rust issue 1992]: https://github.com/rust-lang/rfcs/issues/1992
pub use net_declare_macros::std_socket_addr;
/// Declares an [`std::net::SocketAddrV4`] from a parsable IPv4 address + port
/// in the form `addr:port`.
pub use net_declare_macros::std_socket_addr_v4;
/// Declares an [`std::net::SocketAddrV6`] from a parsable IPv6 address + port
/// in the form `[addr]:port`.
///
/// NOTE: `std::net::SocketAddrV6` does not support parsing scope_id from
/// strings, meaning the generated IPv6 socket address will always have
/// `scope_id=0`. See [Rust issue 1992].
///
/// [Rust issue 1992]: https://github.com/rust-lang/rfcs/issues/1992
pub use net_declare_macros::std_socket_addr_v6;

/// Declares a [`fidl_fuchsia_net::IpAddress`] from a parsable IP address
/// (either V4 or V6) string.
pub use net_declare_macros::fidl_ip;
/// Declares a [`fidl_fuchsia_net::Ipv4Address`] from a parsable IPv4 address
/// string.
pub use net_declare_macros::fidl_ip_v4;
/// Declares a [`fidl_fuchsia_net::Ipv4AddressWithPrefix`] from a parsable IPv4
/// + prefix length string, e.g. `192.168.0.1/24`.
pub use net_declare_macros::fidl_ip_v4_with_prefix;
/// Declares a [`fidl_fuchsia_net::Ipv6Address`] from a parsable IPv6 address
/// string.
pub use net_declare_macros::fidl_ip_v6;
/// Declares a [`fidl_fuchsia_net::Ipv6AddressWithPrefix`] from a parsable IPv6
/// + prefix length string, e.g. `ff08::1/88`.
pub use net_declare_macros::fidl_ip_v6_with_prefix;
/// Declares a [`fidl_fuchsia_net::MacAddress`] from a parsable MAC address in
/// the form `aa:bb:cc:dd:ee:ff`.
pub use net_declare_macros::fidl_mac;
/// Declares an [`fidl_fuchsia_net::SocketAddress`] from a parsable IP address +
/// port string (either V4 or V6).
///
/// NOTE: `std::net::SocketAddrV6` does not support parsing scope_id from
/// strings, meaning the generated IPv6 socket address will always have
/// `zone_index=0`. See [Rust issue 1992].
///
/// [Rust issue 1992]: https://github.com/rust-lang/rfcs/issues/1992
pub use net_declare_macros::fidl_socket_addr;
/// Declares a [`fidl_fuchsia_net::Ipv4SocketAddress`] from a parsable IPv4
/// address + port in the form `addr:port`.
pub use net_declare_macros::fidl_socket_addr_v4;
/// Declares a [`fidl_fuchsia_net::Ipv6SocketAddress`] from a parsable IPv6
/// address + port in the form `[addr]:port`.
///
/// NOTE: `std::net::SocketAddrV6` does not support parsing scope_id from
/// strings, meaning the generated IPv6 socket address will always have
/// `scope_id=0`. See [Rust issue 1992].
///
/// [Rust issue 1992]: https://github.com/rust-lang/rfcs/issues/1992
pub use net_declare_macros::fidl_socket_addr_v6;
/// Declares a [`fidl_fuchsia_net::Subnet`] from a parsable CIDR address string
/// in the form `addr/prefix`, e.g. `192.168.0.1/24` or `ff08::1/64`.
pub use net_declare_macros::fidl_subnet;

/// Declares a [`net_types::ip::AddrSubnetEither`] from a parsable IP address +
/// prefix length string.
pub use net_declare_macros::net_addr_subnet;
/// Declares a [`net_types::ip::AddrSubnet<Ipv4Addr>`] from a parsable IPv4
/// address + prefix length string.
pub use net_declare_macros::net_addr_subnet_v4;
/// Declares a [`net_types::ip::AddrSubnet<Ipv6Addr>`] from a parsable IPv6
/// address + prefix length string.
pub use net_declare_macros::net_addr_subnet_v6;
/// Declares a [`net_types::ip::IpAddr`] from a parsable IP address (either V4
/// or V6) string.
pub use net_declare_macros::net_ip;
/// Declares a [`net_types::ip::Ipv4Addr`] from a parsable IPv4 address string.
pub use net_declare_macros::net_ip_v4;
/// Declares a [`net_types::ip::Ipv6Addr`] from a parsable IPv6 address string.
pub use net_declare_macros::net_ip_v6;
/// Declares a [`net_types::ethernet::Mac`] from a parsable MAC address in
/// the form `aa:bb:cc:dd:ee:ff`.
pub use net_declare_macros::net_mac;
/// Declares a [`net_types::ip::Subnet<Ipv4Addr>`] from a parsable IPv4 CIDR
/// address string.
pub use net_declare_macros::net_subnet_v4;
/// Declares a [`net_types::ip::Subnet<Ipv6Addr>`] from a parsable IPv6 CIDR
/// address string.
pub use net_declare_macros::net_subnet_v6;

/// Declares a [`net_types::ip::PrefixLength<Ipv4>`] from an integer literal.
pub use net_declare_macros::net_prefix_length_v4;

/// Declares a [`net_types::ip::PrefixLength<Ipv6>`] from an integer literal.
pub use net_declare_macros::net_prefix_length_v6;

/// Redeclaration of macros to generate `std` types.
pub mod std {
    pub use super::{
        std_ip as ip, std_ip_v4 as ip_v4, std_ip_v6 as ip_v6, std_socket_addr as socket_addr,
        std_socket_addr_v4 as socket_addr_v4, std_socket_addr_v6 as socket_addr_v6,
    };
}

/// Redeclaration of macros to generate `fidl` types.
pub mod fidl {
    pub use super::{
        fidl_ip as ip, fidl_ip_v4 as ip_v4, fidl_ip_v6 as ip_v6, fidl_mac as mac,
        fidl_socket_addr as socket_addr, fidl_socket_addr_v4 as socket_addr_v4,
        fidl_socket_addr_v6 as socket_addr_v6, fidl_subnet as subnet,
    };
}

/// Redeclaration of macros to generate `net_types` types.
pub mod net {
    pub use super::{
        net_ip as ip, net_ip_v4 as ip_v4, net_ip_v6 as ip_v6, net_mac as mac,
        net_prefix_length_v4 as prefix_length_v4, net_prefix_length_v6 as prefix_length_v6,
        net_subnet_v4 as subnet_v4, net_subnet_v6 as subnet_v6,
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use net_declare_macros::net_prefix_length_v4;
    use {::std, fidl_fuchsia_net as fidl};

    #[test]
    fn test_std_ip() {
        assert_eq!(
            std::net::IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 0, 1)),
            std_ip!("192.168.0.1")
        );
        assert_eq!(
            std::net::IpAddr::V6(std::net::Ipv6Addr::new(0xFF01, 0, 0, 0, 0, 0, 0, 0x0102)),
            std_ip!("ff01::0102")
        );
    }

    #[test]
    fn test_std_ip_v4() {
        assert_eq!(std::net::Ipv4Addr::new(192, 168, 0, 1), std_ip_v4!("192.168.0.1"));
    }

    #[test]
    fn test_std_ip_v6() {
        assert_eq!(
            std::net::Ipv6Addr::new(0xFF01, 0, 0, 0, 0, 0, 0, 0x0102),
            std_ip_v6!("ff01::0102")
        );
    }

    #[test]
    fn test_std_socket_addr() {
        assert_eq!(
            std::net::SocketAddr::V4(std::net::SocketAddrV4::new(
                std::net::Ipv4Addr::new(192, 168, 0, 1),
                8080
            )),
            std_socket_addr!("192.168.0.1:8080")
        );
        assert_eq!(
            std::net::SocketAddr::V6(std::net::SocketAddrV6::new(
                std::net::Ipv6Addr::new(0xFF01, 0, 0, 0, 0, 0, 0, 0x0102),
                8080,
                0,
                0
            )),
            std_socket_addr!("[ff01::0102]:8080")
        );
    }

    #[test]
    fn test_std_socket_addr_v4() {
        assert_eq!(
            std::net::SocketAddrV4::new(std::net::Ipv4Addr::new(192, 168, 0, 1), 8080),
            std_socket_addr_v4!("192.168.0.1:8080")
        );
    }

    #[test]
    fn test_std_socket_addr_v6() {
        assert_eq!(
            std::net::SocketAddrV6::new(
                std::net::Ipv6Addr::new(0xFF01, 0, 0, 0, 0, 0, 0, 0x0102),
                8080,
                0,
                0
            ),
            std_socket_addr_v6!("[ff01::0102]:8080")
        );
    }

    #[test]
    fn test_fidl_ip() {
        assert_eq!(
            fidl::IpAddress::Ipv4(fidl::Ipv4Address { addr: [192, 168, 0, 1] }),
            fidl_ip!("192.168.0.1")
        );

        assert_eq!(
            fidl::IpAddress::Ipv6(fidl::Ipv6Address {
                addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
            }),
            fidl_ip!("ff01::0102")
        );
    }

    #[test]
    fn test_fidl_ip_v4() {
        assert_eq!(fidl::Ipv4Address { addr: [192, 168, 0, 1] }, fidl_ip_v4!("192.168.0.1"));
    }

    #[test]
    fn test_fidl_ip_v6() {
        assert_eq!(
            fidl::Ipv6Address {
                addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
            },
            fidl_ip_v6!("ff01::0102")
        );
    }

    #[test]
    fn test_fidl_ip_v6_v4_mapped() {
        assert_eq!(
            fidl::Ipv6Address { addr: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 192, 168, 0, 1] },
            fidl_ip_v6!("::ffff:192.168.0.1")
        );
    }

    #[test]
    fn test_fidl_socket_addr() {
        assert_eq!(
            fidl::SocketAddress::Ipv4(fidl::Ipv4SocketAddress {
                address: fidl::Ipv4Address { addr: [192, 168, 0, 1] },
                port: 8080
            }),
            fidl_socket_addr!("192.168.0.1:8080")
        );

        assert_eq!(
            fidl::SocketAddress::Ipv6(fidl::Ipv6SocketAddress {
                address: fidl::Ipv6Address {
                    addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
                },
                port: 8080,
                zone_index: 0,
            }),
            fidl_socket_addr!("[ff01::0102]:8080")
        );
    }

    #[test]
    fn test_fidl_socket_addr_v4() {
        assert_eq!(
            fidl::Ipv4SocketAddress {
                address: fidl::Ipv4Address { addr: [192, 168, 0, 1] },
                port: 8080
            },
            fidl_socket_addr_v4!("192.168.0.1:8080")
        );
    }

    #[test]
    fn test_fidl_socket_addr_v6() {
        assert_eq!(
            fidl::Ipv6SocketAddress {
                address: fidl::Ipv6Address {
                    addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
                },
                port: 8080,
                zone_index: 0,
            },
            fidl_socket_addr_v6!("[ff01::0102]:8080")
        );
    }

    #[test]
    fn test_fidl_mac() {
        assert_eq!(fidl::MacAddress { octets: [0, 1, 2, 3, 4, 5] }, fidl_mac!("00:01:02:03:04:05"));
    }

    #[test]
    fn test_accept_quotes() {
        // Rustfmt gets confused with this syntax sometimes, so we allow macros
        // to receive what looks like a string literal as well.
        assert_eq!(
            fidl::MacAddress { octets: [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF] },
            fidl_mac!("AA:BB:CC:DD:EE:FF")
        );
    }

    #[test]
    fn test_fidl_ip_v4_with_prefix() {
        assert_eq!(
            fidl::Ipv4AddressWithPrefix {
                addr: fidl::Ipv4Address { addr: [192, 168, 0, 1] },
                prefix_len: 24
            },
            fidl_ip_v4_with_prefix!("192.168.0.1/24")
        );
    }

    #[test]
    fn test_fidl_ip_v6_with_prefix() {
        assert_eq!(
            fidl::Ipv6AddressWithPrefix {
                addr: fidl::Ipv6Address {
                    addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
                },
                prefix_len: 64
            },
            fidl_ip_v6_with_prefix!("ff01::0102/64")
        );
    }

    #[test]
    fn test_fidl_subnet_v4() {
        assert_eq!(
            fidl::Subnet {
                addr: fidl::IpAddress::Ipv4(fidl::Ipv4Address { addr: [192, 168, 0, 1] }),
                prefix_len: 24
            },
            fidl_subnet!("192.168.0.1/24")
        );
    }

    #[test]
    fn test_fidl_subnet_v6() {
        assert_eq!(
            fidl::Subnet {
                addr: fidl::IpAddress::Ipv6(fidl::Ipv6Address {
                    addr: [0xFF, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02]
                }),
                prefix_len: 64
            },
            fidl_subnet!("ff01::0102/64")
        );
    }

    #[test]
    fn test_net_ip() {
        assert_eq!(
            net_types::ip::IpAddr::from(net_types::ip::Ipv4Addr::new([192, 168, 0, 1])),
            net_ip!("192.168.0.1")
        );

        assert_eq!(
            net_types::ip::IpAddr::from(net_types::ip::Ipv6Addr::new([
                0xFF01, 0, 0, 0, 0, 0, 0, 0x0102
            ])),
            net_ip!("ff01::0102"),
        );
    }

    #[test]
    fn test_net_ip_v4() {
        assert_eq!(net_types::ip::Ipv4Addr::new([192, 168, 0, 1]), net_ip_v4!("192.168.0.1"),);
    }

    #[test]
    fn test_net_ip_v6() {
        assert_eq!(
            net_types::ip::Ipv6Addr::new([0xFF01, 0, 0, 0, 0, 0, 0, 0x0102]),
            net_ip_v6!("ff01::0102"),
        );
    }

    #[test]
    fn test_net_ip_v6_v4_mapped() {
        assert_eq!(
            net_types::ip::Ipv6Addr::from_bytes([
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 192, 168, 0, 1
            ]),
            net_ip_v6!("::ffff:192.168.0.1"),
        )
    }

    #[test]
    fn test_net_mac() {
        assert_eq!(
            net_types::ethernet::Mac::new([0, 1, 2, 3, 4, 5]),
            net_mac!("00:01:02:03:04:05")
        );
    }

    #[test]
    fn test_net_subnet_v4() {
        assert_eq!(
            net_types::ip::Subnet::new(net_types::ip::Ipv4Addr::new([18, 6, 0, 0]), 15).unwrap(),
            net_subnet_v4!("18.6.0.0/15")
        )
    }

    #[test]
    fn test_net_subnet_v6() {
        assert_eq!(
            net_types::ip::Subnet::new(
                net_types::ip::Ipv6Addr::new([0xff80, 0, 0, 0, 0, 0, 0, 0]),
                12
            )
            .unwrap(),
            net_subnet_v6!("ff80::/12")
        )
    }

    #[test]
    fn test_net_prefix_length_v4() {
        assert_eq!(
            net_types::ip::PrefixLength::<net_types::ip::Ipv4>::new(23).unwrap(),
            net_prefix_length_v4!(23)
        )
    }

    #[test]
    fn test_net_prefix_length_v6() {
        assert_eq!(
            net_types::ip::PrefixLength::<net_types::ip::Ipv6>::new(125).unwrap(),
            net_prefix_length_v6!(125)
        )
    }
}