@@ -32,6 +32,26 @@ pub use self::datalink::LinkAddr;
3232#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
3333pub use self :: vsock:: VsockAddr ;
3434
35+ /// Convert a std::net::Ipv4Addr into the libc form.
36+ #[ cfg( feature = "net" ) ]
37+ pub ( crate ) fn ipv4addr_to_libc ( addr : net:: Ipv4Addr ) -> libc:: in_addr {
38+ let octets = addr. octets ( ) ;
39+ libc:: in_addr {
40+ s_addr : u32:: to_be ( ( ( octets[ 0 ] as u32 ) << 24 ) |
41+ ( ( octets[ 1 ] as u32 ) << 16 ) |
42+ ( ( octets[ 2 ] as u32 ) << 8 ) |
43+ ( octets[ 3 ] as u32 ) )
44+ }
45+ }
46+
47+ /// Convert a std::net::Ipv6Addr into the libc form.
48+ #[ cfg( feature = "net" ) ]
49+ pub ( crate ) const fn ipv6addr_to_libc ( addr : & net:: Ipv6Addr ) -> libc:: in6_addr {
50+ libc:: in6_addr {
51+ s6_addr : addr. octets ( )
52+ }
53+ }
54+
3555/// These constants specify the protocol family to be used
3656/// in [`socket`](fn.socket.html) and [`socketpair`](fn.socketpair.html)
3757///
@@ -497,14 +517,20 @@ impl fmt::Display for InetAddr {
497517 * ===== IpAddr =====
498518 *
499519 */
500- #[ allow( missing_docs) ] // https://github.com/nix-rust/nix/issues/1681
520+ #[ allow( missing_docs) ] // Since they're all deprecated anyway
521+ #[ allow( deprecated) ]
501522#[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
523+ #[ deprecated(
524+ since = "0.24.0" ,
525+ note = "Use std::net::IpAddr instead"
526+ ) ]
502527pub enum IpAddr {
503528 V4 ( Ipv4Addr ) ,
504529 V6 ( Ipv6Addr ) ,
505530}
506531
507- #[ allow( missing_docs) ] // https://github.com/nix-rust/nix/issues/1681
532+ #[ allow( deprecated) ]
533+ #[ allow( missing_docs) ] // Since they're all deprecated anyway
508534impl IpAddr {
509535 /// Create a new IpAddr that contains an IPv4 address.
510536 ///
@@ -537,6 +563,7 @@ impl IpAddr {
537563 }
538564}
539565
566+ #[ allow( deprecated) ]
540567impl fmt:: Display for IpAddr {
541568 fn fmt( & self , f: & mut fmt:: Formatter ) -> fmt:: Result {
542569 match * self {
@@ -552,12 +579,17 @@ impl fmt::Display for IpAddr {
552579 *
553580 */
554581
555- #[ allow( missing_docs) ] // https://github.com/nix-rust/nix/issues/1681
582+ #[ deprecated(
583+ since = "0.24.0" ,
584+ note = "Use std::net::Ipv4Addr instead"
585+ ) ]
586+ #[ allow( missing_docs) ] // Since they're all deprecated anyway
556587#[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
557588#[ repr( transparent) ]
558589pub struct Ipv4Addr ( pub libc:: in_addr) ;
559590
560- #[ allow( missing_docs) ] // https://github.com/nix-rust/nix/issues/1681
591+ #[ allow( deprecated) ]
592+ #[ allow( missing_docs) ] // Since they're all deprecated anyway
561593impl Ipv4Addr {
562594 #[ allow( clippy:: identity_op) ] // More readable this way
563595 pub const fn new( a: u8 , b: u8 , c: u8 , d: u8 ) -> Ipv4Addr {
@@ -591,6 +623,7 @@ impl Ipv4Addr {
591623 }
592624}
593625
626+ #[ allow( deprecated) ]
594627impl fmt:: Display for Ipv4Addr {
595628 fn fmt( & self , fmt: & mut fmt:: Formatter ) -> fmt:: Result {
596629 let octets = self . octets( ) ;
@@ -604,7 +637,11 @@ impl fmt::Display for Ipv4Addr {
604637 *
605638 */
606639
607- #[ allow( missing_docs) ] // https://github.com/nix-rust/nix/issues/1681
640+ #[ deprecated(
641+ since = "0.24.0" ,
642+ note = "Use std::net::Ipv6Addr instead"
643+ ) ]
644+ #[ allow( missing_docs) ] // Since they're all deprecated anyway
608645#[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
609646#[ repr( transparent) ]
610647pub struct Ipv6Addr ( pub libc:: in6_addr) ;
@@ -625,7 +662,8 @@ macro_rules! to_u16_array {
625662 }
626663}
627664
628- #[ allow( missing_docs) ] // https://github.com/nix-rust/nix/issues/1681
665+ #[ allow( deprecated) ]
666+ #[ allow( missing_docs) ] // Since they're all deprecated anyway
629667impl Ipv6Addr {
630668 #[ allow( clippy:: many_single_char_names) ]
631669 #[ allow( clippy:: too_many_arguments) ]
@@ -649,6 +687,7 @@ impl Ipv6Addr {
649687 }
650688}
651689
690+ #[ allow( deprecated) ]
652691impl fmt:: Display for Ipv6Addr {
653692 fn fmt( & self , fmt: & mut fmt:: Formatter ) -> fmt:: Result {
654693 self . to_std( ) . fmt( fmt)
@@ -1172,7 +1211,7 @@ impl From<net::SocketAddrV4> for SockaddrIn {
11721211 sin_len : mem:: size_of :: < libc:: sockaddr_in > ( ) as u8 ,
11731212 sin_family : AddressFamily :: Inet as sa_family_t ,
11741213 sin_port : addr. port ( ) . to_be ( ) , // network byte order
1175- sin_addr : Ipv4Addr :: from_std ( addr. ip ( ) ) . 0 ,
1214+ sin_addr : ipv4addr_to_libc ( * addr. ip ( ) ) ,
11761215 .. unsafe { mem:: zeroed ( ) }
11771216 } )
11781217 }
@@ -1266,7 +1305,7 @@ impl From<net::SocketAddrV6> for SockaddrIn6 {
12661305 sin6_len : mem:: size_of :: < libc:: sockaddr_in6 > ( ) as u8 ,
12671306 sin6_family : AddressFamily :: Inet6 as sa_family_t ,
12681307 sin6_port : addr. port ( ) . to_be ( ) , // network byte order
1269- sin6_addr : Ipv6Addr :: from_std ( addr. ip ( ) ) . 0 ,
1308+ sin6_addr : ipv6addr_to_libc ( addr. ip ( ) ) ,
12701309 sin6_flowinfo : addr. flowinfo ( ) , // host byte order
12711310 sin6_scope_id : addr. scope_id ( ) , // host byte order
12721311 .. unsafe { mem:: zeroed ( ) }
@@ -2545,6 +2584,24 @@ pub mod vsock {
25452584mod tests {
25462585 use super :: * ;
25472586
2587+ mod types {
2588+ use super :: * ;
2589+
2590+ #[ test]
2591+ fn test_ipv4addr_to_libc ( ) {
2592+ let s = std:: net:: Ipv4Addr :: new ( 1 , 2 , 3 , 4 ) ;
2593+ let l = ipv4addr_to_libc ( s) ;
2594+ assert_eq ! ( l. s_addr, u32 :: to_be( 0x01020304 ) ) ;
2595+ }
2596+
2597+ #[ test]
2598+ fn test_ipv6addr_to_libc ( ) {
2599+ let s = std:: net:: Ipv6Addr :: new ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ) ;
2600+ let l = ipv6addr_to_libc ( & s) ;
2601+ assert_eq ! ( l. s6_addr, [ 0 , 1 , 0 , 2 , 0 , 3 , 0 , 4 , 0 , 5 , 0 , 6 , 0 , 7 , 0 , 8 ] ) ;
2602+ }
2603+ }
2604+
25482605 mod link {
25492606 #[ cfg( any( target_os = "ios" ,
25502607 target_os = "macos" ,
0 commit comments