Skip to content

Commit a3a6c15

Browse files
committed
Winapi unions are a mess
1 parent e8c5da0 commit a3a6c15

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/sys/windows/tcp.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::os::windows::io::FromRawSocket;
77
use std::os::windows::raw::SOCKET as StdSocket; // winapi uses usize, stdlib uses u32/u64.
88

99
use winapi::ctypes::{c_char, c_int, c_ushort};
10-
use winapi::shared::ws2def::{SOCKADDR_STORAGE, AF_INET, SOCKADDR_IN};
10+
use winapi::shared::ws2def::{SOCKADDR_STORAGE, AF_INET, AF_INET6, SOCKADDR_IN};
1111
use winapi::shared::ws2ipdef::SOCKADDR_IN6;
1212

1313
use winapi::shared::minwindef::{BOOL, TRUE, FALSE};
@@ -109,7 +109,7 @@ pub(crate) fn get_reuseaddr(socket: TcpSocket) -> io::Result<bool> {
109109

110110
pub(crate) fn get_localaddr(socket: TcpSocket) -> io::Result<SocketAddr> {
111111
let mut storage: SOCKADDR_STORAGE = unsafe { std::mem::zeroed() };
112-
let mut length = std::mem::size_of_val(&addr) as c_int;
112+
let mut length = std::mem::size_of_val(&storage) as c_int;
113113

114114
match unsafe { getsockname(
115115
socket,
@@ -120,12 +120,13 @@ pub(crate) fn get_localaddr(socket: TcpSocket) -> io::Result<SocketAddr> {
120120
_ => {
121121
if storage.ss_family as c_int == AF_INET {
122122
let addr: &SOCKADDR_IN = &*(&storage as *const _ as *const SOCKADDR_IN);
123-
let ip = Ipv4Addr::from(addr.sin_addr.s_addr.to_ne_bytes());
123+
let ip_bytes = unsafe { addr.sin_addr.S_un.S_un_b() };
124+
let ip = Ipv4Addr::from([ip_bytes.s_b1, ip_bytes.s_b2, ip_bytes.s_b3, ip_bytes.s_b4]);
124125
let port = u16::from_be(addr.sin_port);
125126
Ok(SocketAddr::V4(SocketAddrV4::new(ip, port)))
126127
} else if storage.ss_family as c_int == AF_INET6 {
127128
let addr: &SOCKADDR_IN6 = &*(storage as *const _ as *const SOCKADDR_IN6);
128-
let ip = Ipv6Addr::from(addr.sin6_addr.s6_addr);
129+
let ip = Ipv6Addr::from(*unsafe { addr.sin6_addr.u.Byte() });
129130
let port = u16::from_be(addr.sin6_port);
130131
let scope_id = unsafe { *addr.u.sin6_scope_id() };
131132
Ok(SocketAddr::V6(SocketAddrV6::new(ip, port, addr.sin6_flowinfo, scope_id)))

0 commit comments

Comments
 (0)