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 ::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 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}