1pub use fidl_fuchsia_net_common as fnet_common;
9
10pub use net_declare_macros::std_ip;
13pub use net_declare_macros::std_ip_v4;
15pub use net_declare_macros::std_ip_v6;
17pub use net_declare_macros::std_socket_addr;
26pub use net_declare_macros::std_socket_addr_v4;
29pub use net_declare_macros::std_socket_addr_v6;
38
39pub use net_declare_macros::fidl_ip;
42pub use net_declare_macros::fidl_ip_v4;
45pub use net_declare_macros::fidl_ip_v4_with_prefix;
48pub use net_declare_macros::fidl_ip_v6;
51pub use net_declare_macros::fidl_ip_v6_with_prefix;
54pub use net_declare_macros::fidl_mac;
57pub use net_declare_macros::fidl_socket_addr;
66pub use net_declare_macros::fidl_socket_addr_v4;
69pub use net_declare_macros::fidl_socket_addr_v6;
78pub use net_declare_macros::fidl_subnet;
81
82pub use net_declare_macros::net_addr_subnet;
85pub use net_declare_macros::net_addr_subnet_v4;
88pub use net_declare_macros::net_addr_subnet_v6;
91pub use net_declare_macros::net_ip;
94pub use net_declare_macros::net_ip_v4;
96pub use net_declare_macros::net_ip_v6;
98pub use net_declare_macros::net_mac;
101pub use net_declare_macros::net_subnet_v4;
104pub use net_declare_macros::net_subnet_v6;
107
108pub use net_declare_macros::net_prefix_length_v4;
110
111pub use net_declare_macros::net_prefix_length_v6;
113
114pub 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
122pub 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
131pub 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 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}