Skip to content

Commit

Permalink
Adapt to winapi 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
faern committed Nov 14, 2020
1 parent 2783715 commit 27b77cc
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use std::os::windows::prelude::*;

use net2::TcpBuilder;
use winapi::*;
use winapi::shared::inaddr::{in_addr_S_un, IN_ADDR};
use winapi::shared::in6addr::{in6_addr_u, IN6_ADDR};
use winapi::inaddr::IN_ADDR;
use winapi::ws2def::SOCKADDR_IN;
use winapi::ws2ipdef::{in6_addr, sockaddr_in6};
use ws2_32::*;

/// A type to represent a buffer in which a socket address will be stored.
Expand Down Expand Up @@ -487,7 +488,7 @@ fn cvt(i: c_int, size: DWORD) -> io::Result<Option<usize>> {
#[repr(C)]
pub(crate) union SocketAddrCRepr {
v4: SOCKADDR_IN,
v6: SOCKADDR_IN6_LH,
v6: sockaddr_in6,
}

impl SocketAddrCRepr {
Expand All @@ -499,44 +500,27 @@ impl SocketAddrCRepr {
fn socket_addr_to_ptrs(addr: &SocketAddr) -> (SocketAddrCRepr, c_int) {
match *addr {
SocketAddr::V4(ref a) => {
let sin_addr = unsafe {
let mut s_un = mem::zeroed::<in_addr_S_un>();
*s_un.S_addr_mut() = u32::from_ne_bytes(a.ip().octets());
IN_ADDR { S_un: s_un }
};

let sockaddr_in = SOCKADDR_IN {
sin_family: AF_INET as ADDRESS_FAMILY,
sin_port: a.port().to_be(),
sin_addr,
sin_addr: IN_ADDR { S_un: u32::from_ne_bytes(a.ip().octets()) },
sin_zero: [0; 8],
};

let sockaddr = SocketAddrCRepr { v4: sockaddr_in };
(sockaddr, mem::size_of::<SOCKADDR_IN>() as c_int)
}
SocketAddr::V6(ref a) => {
let sin6_addr = unsafe {
let mut u = mem::zeroed::<in6_addr_u>();
*u.Byte_mut() = a.ip().octets();
IN6_ADDR { u }
};
let u = unsafe {
let mut u = mem::zeroed::<SOCKADDR_IN6_LH_u>();
*u.sin6_scope_id_mut() = a.scope_id();
u
};

let sockaddr_in6 = SOCKADDR_IN6_LH {
sin6_family: AF_INET6 as ADDRESS_FAMILY,
let sockaddr_in6 = sockaddr_in6 {
sin6_family: AF_INET6 as i16,
sin6_port: a.port().to_be(),
sin6_addr,
sin6_addr: in6_addr { s6_addr: a.ip().octets() },
sin6_flowinfo: a.flowinfo(),
u,
sin6_scope_id: a.scope_id(),
};

let sockaddr = SocketAddrCRepr { v6: sockaddr_in6 };
(sockaddr, mem::size_of::<SOCKADDR_IN6_LH>() as c_int)
(sockaddr, mem::size_of::<sockaddr_in6>() as c_int)
}
}
}
Expand Down

0 comments on commit 27b77cc

Please sign in to comment.