@@ -31,30 +31,24 @@ use crate::sys::socket::addr::sys_control::SysControlAddr;
31
31
pub use self :: datalink:: LinkAddr ;
32
32
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
33
33
pub use self :: vsock:: VsockAddr ;
34
- #[ cfg( feature = "net" ) ]
35
- use static_assertions:: const_assert_eq;
36
34
37
35
/// Convert a std::net::Ipv4Addr into the libc form.
38
- // In the standard library, this is a simple newtype, but it doesn't expose
39
- // the inner libc type, so we must use a pointer cast.
40
36
#[ cfg( feature = "net" ) ]
41
- pub ( crate ) const fn ipv4addr_to_libc ( addr : net:: Ipv4Addr ) -> libc:: in_addr {
42
- const_assert_eq ! ( mem:: size_of:: <libc:: in_addr>( ) ,
43
- mem:: size_of:: <net:: Ipv4Addr >( ) ) ;
44
- unsafe {
45
- * ( & addr as * const net:: Ipv4Addr as * const libc:: in_addr )
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 ) )
46
44
}
47
45
}
48
46
49
47
/// Convert a std::net::Ipv6Addr into the libc form.
50
- // In the standard library, this is a simple newtype, but it doesn't expose
51
- // the inner libc type, so we must use a pointer cast.
52
48
#[ cfg( feature = "net" ) ]
53
- pub ( crate ) const fn ipv6addr_to_libc ( addr : & net:: Ipv6Addr ) -> & libc:: in6_addr {
54
- const_assert_eq ! ( mem:: size_of:: <libc:: in6_addr>( ) ,
55
- mem:: size_of:: <net:: Ipv6Addr >( ) ) ;
56
- unsafe {
57
- & * ( addr as * const net:: Ipv6Addr as * const libc:: in6_addr )
49
+ pub ( crate ) const fn ipv6addr_to_libc ( addr : & net:: Ipv6Addr ) -> libc:: in6_addr {
50
+ libc:: in6_addr {
51
+ s6_addr : addr. octets ( )
58
52
}
59
53
}
60
54
@@ -1311,7 +1305,7 @@ impl From<net::SocketAddrV6> for SockaddrIn6 {
1311
1305
sin6_len : mem:: size_of :: < libc:: sockaddr_in6 > ( ) as u8 ,
1312
1306
sin6_family : AddressFamily :: Inet6 as sa_family_t ,
1313
1307
sin6_port : addr. port ( ) . to_be ( ) , // network byte order
1314
- sin6_addr : * ipv6addr_to_libc ( addr. ip ( ) ) ,
1308
+ sin6_addr : ipv6addr_to_libc ( addr. ip ( ) ) ,
1315
1309
sin6_flowinfo : addr. flowinfo ( ) , // host byte order
1316
1310
sin6_scope_id : addr. scope_id ( ) , // host byte order
1317
1311
.. unsafe { mem:: zeroed ( ) }
0 commit comments