1pub use net_declare_macros::std_ip;
11pub use net_declare_macros::std_ip_v4;
13pub use net_declare_macros::std_ip_v6;
15pub use net_declare_macros::std_socket_addr;
24pub use net_declare_macros::std_socket_addr_v4;
27pub use net_declare_macros::std_socket_addr_v6;
36
37pub use net_declare_macros::fidl_ip;
40pub use net_declare_macros::fidl_ip_v4;
43pub use net_declare_macros::fidl_ip_v4_with_prefix;
46pub use net_declare_macros::fidl_ip_v6;
49pub use net_declare_macros::fidl_ip_v6_with_prefix;
52pub use net_declare_macros::fidl_mac;
55pub use net_declare_macros::fidl_socket_addr;
64pub use net_declare_macros::fidl_socket_addr_v4;
67pub use net_declare_macros::fidl_socket_addr_v6;
76pub use net_declare_macros::fidl_subnet;
79
80pub use net_declare_macros::net_addr_subnet;
83pub use net_declare_macros::net_addr_subnet_v4;
86pub use net_declare_macros::net_addr_subnet_v6;
89pub use net_declare_macros::net_ip;
92pub use net_declare_macros::net_ip_v4;
94pub use net_declare_macros::net_ip_v6;
96pub use net_declare_macros::net_mac;
99pub use net_declare_macros::net_subnet_v4;
102pub use net_declare_macros::net_subnet_v6;
105
106pub use net_declare_macros::net_prefix_length_v4;
108
109pub use net_declare_macros::net_prefix_length_v6;
111
112pub 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
120pub 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
129pub 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 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}