-
-
Save Nemo157/00787cd835f73bbf90b12cbf71122844 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git src/sockaddr.rs src/sockaddr.rs | |
index c1b8abd..c8340b3 100644 | |
--- src/sockaddr.rs | |
+++ src/sockaddr.rs | |
@@ -1,6 +1,7 @@ | |
use std::mem::{self, MaybeUninit}; | |
-use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6}; | |
+use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6, Ipv4Addr}; | |
use std::{fmt, ptr}; | |
+use libc::{in_addr, in_port_t}; | |
use crate::sys::{ | |
c_int, sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t, AF_INET, | |
@@ -64,8 +65,8 @@ impl SockAddr { | |
/// family. | |
pub fn as_inet(&self) -> Option<SocketAddrV4> { | |
if self.storage.ss_family as c_int == AF_INET { | |
- let storage: *const sockaddr_storage = (&self.storage) as *const _; | |
- Some(unsafe { *(storage as *const sockaddr_in as *const _) }) | |
+ let storage: *const sockaddr_in = (&self.storage) as *const _ as *const _; | |
+ Some(SocketAddrV4::new(Ipv4Addr::from(unsafe { u32::from_be((*storage).sin_addr.s_addr) }), unsafe { u16::from_be((*storage).sin_port) })) | |
} else { | |
None | |
} | |
@@ -95,9 +96,17 @@ impl From<SocketAddr> for SockAddr { | |
impl From<SocketAddrV4> for SockAddr { | |
fn from(addr: SocketAddrV4) -> SockAddr { | |
unsafe { | |
+ let storage = sockaddr_in { | |
+ sin_family: AF_INET as sa_family_t, | |
+ sin_port: addr.port().to_be() as in_port_t, | |
+ sin_addr: in_addr { | |
+ s_addr: u32::from_ne_bytes(addr.ip().octets()), | |
+ }, | |
+ sin_zero: [0; 8], | |
+ }; | |
SockAddr::from_raw_parts( | |
- &addr as *const _ as *const _, | |
- mem::size_of::<SocketAddrV4>() as socklen_t, | |
+ &storage as *const _ as *const _, | |
+ mem::size_of::<sockaddr_in>() as socklen_t, | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment