@@ -147,18 +147,35 @@ impl SockAddr {
147147 pub fn as_std ( & self ) -> Option < SocketAddr > {
148148 if self . storage . ss_family == AF_INET as sa_family_t {
149149 let addr = unsafe { & * ( & self . storage as * const _ as * const sockaddr_in ) } ;
150+
151+ #[ cfg( unix) ]
150152 let ip = Ipv4Addr :: from ( addr. sin_addr . s_addr . to_ne_bytes ( ) ) ;
153+ #[ cfg( windows) ]
154+ let ip_bytes = unsafe { addr. sin_addr . S_un . S_un_b ( ) } ;
155+ #[ cfg( windows) ]
156+ let ip = Ipv4Addr :: from ( [ ip_bytes. s_b1 , ip_bytes. s_b2 , ip_bytes. s_b3 , ip_bytes. s_b4 ] ) ;
157+
151158 let port = u16:: from_be ( addr. sin_port ) ;
152159 Some ( SocketAddr :: V4 ( SocketAddrV4 :: new ( ip, port) ) )
153160 } else if self . storage . ss_family == AF_INET6 as sa_family_t {
154161 let addr = unsafe { & * ( & self . storage as * const _ as * const sockaddr_in6 ) } ;
162+
163+ #[ cfg( unix) ]
155164 let ip = Ipv6Addr :: from ( addr. sin6_addr . s6_addr ) ;
165+ #[ cfg( windows) ]
166+ let ip = Ipv6Addr :: from ( * unsafe { addr. sin6_addr . u . Byte ( ) } ) ;
167+
156168 let port = u16:: from_be ( addr. sin6_port ) ;
157169 Some ( SocketAddr :: V6 ( SocketAddrV6 :: new (
158170 ip,
159171 port,
160172 addr. sin6_flowinfo ,
173+ #[ cfg( unix) ]
161174 addr. sin6_scope_id ,
175+ #[ cfg( windows) ]
176+ unsafe {
177+ * addr. u . sin6_scope_id ( )
178+ } ,
162179 ) ) )
163180 } else {
164181 None
@@ -185,12 +202,19 @@ impl From<SocketAddrV4> for SockAddr {
185202 fn from ( addr : SocketAddrV4 ) -> SockAddr {
186203 let mut storage = MaybeUninit :: < sockaddr_storage > :: uninit ( ) ;
187204 let sockaddr_in = unsafe { & mut * ( storage. as_mut_ptr ( ) as * mut sockaddr_in ) } ;
205+
206+ #[ cfg( unix) ]
207+ let sin_addr = in_addr {
208+ s_addr : u32:: from_ne_bytes ( addr. ip ( ) . octets ( ) ) ,
209+ } ;
210+ #[ cfg( windows) ]
211+ let sin_addr = in_addr {
212+ S_un : unsafe { mem:: transmute ( u32:: from_ne_bytes ( addr. ip ( ) . octets ( ) ) ) } ,
213+ } ;
188214 * sockaddr_in = sockaddr_in {
189215 sin_family : AF_INET as sa_family_t ,
190216 sin_port : addr. port ( ) . to_be ( ) ,
191- sin_addr : in_addr {
192- s_addr : u32:: from_ne_bytes ( addr. ip ( ) . octets ( ) ) ,
193- } ,
217+ sin_addr,
194218 ..unsafe { mem:: zeroed ( ) }
195219 } ;
196220 SockAddr {
@@ -204,14 +228,24 @@ impl From<SocketAddrV6> for SockAddr {
204228 fn from ( addr : SocketAddrV6 ) -> SockAddr {
205229 let mut storage = MaybeUninit :: < sockaddr_storage > :: uninit ( ) ;
206230 let sockaddr_in6 = unsafe { & mut * ( storage. as_mut_ptr ( ) as * mut sockaddr_in6 ) } ;
231+
232+ #[ cfg( windows) ]
233+ let sin6_addr = in6_addr {
234+ u : unsafe { mem:: transmute ( addr. ip ( ) . octets ( ) ) } ,
235+ } ;
236+ #[ cfg( unix) ]
237+ let sin6_addr = in6_addr {
238+ s6_addr : addr. ip ( ) . octets ( ) ,
239+ } ;
207240 * sockaddr_in6 = sockaddr_in6 {
208241 sin6_family : AF_INET6 as sa_family_t ,
209242 sin6_port : addr. port ( ) . to_be ( ) ,
210- sin6_addr : in6_addr {
211- s6_addr : addr. ip ( ) . octets ( ) ,
212- } ,
243+ sin6_addr,
213244 sin6_flowinfo : addr. flowinfo ( ) ,
245+ #[ cfg( unix) ]
214246 sin6_scope_id : addr. scope_id ( ) ,
247+ #[ cfg( windows) ]
248+ u : unsafe { mem:: transmute ( addr. scope_id ( ) ) } ,
215249 ..unsafe { mem:: zeroed ( ) }
216250 } ;
217251 SockAddr {
0 commit comments