@@ -147,18 +147,35 @@ impl SockAddr {
147
147
pub fn as_std ( & self ) -> Option < SocketAddr > {
148
148
if self . storage . ss_family == AF_INET as sa_family_t {
149
149
let addr = unsafe { & * ( & self . storage as * const _ as * const sockaddr_in ) } ;
150
+
151
+ #[ cfg( unix) ]
150
152
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
+
151
158
let port = u16:: from_be ( addr. sin_port ) ;
152
159
Some ( SocketAddr :: V4 ( SocketAddrV4 :: new ( ip, port) ) )
153
160
} else if self . storage . ss_family == AF_INET6 as sa_family_t {
154
161
let addr = unsafe { & * ( & self . storage as * const _ as * const sockaddr_in6 ) } ;
162
+
163
+ #[ cfg( unix) ]
155
164
let ip = Ipv6Addr :: from ( addr. sin6_addr . s6_addr ) ;
165
+ #[ cfg( windows) ]
166
+ let ip = Ipv6Addr :: from ( * unsafe { addr. sin6_addr . u . Byte ( ) } ) ;
167
+
156
168
let port = u16:: from_be ( addr. sin6_port ) ;
157
169
Some ( SocketAddr :: V6 ( SocketAddrV6 :: new (
158
170
ip,
159
171
port,
160
172
addr. sin6_flowinfo ,
173
+ #[ cfg( unix) ]
161
174
addr. sin6_scope_id ,
175
+ #[ cfg( windows) ]
176
+ unsafe {
177
+ * addr. u . sin6_scope_id ( )
178
+ } ,
162
179
) ) )
163
180
} else {
164
181
None
@@ -185,12 +202,19 @@ impl From<SocketAddrV4> for SockAddr {
185
202
fn from ( addr : SocketAddrV4 ) -> SockAddr {
186
203
let mut storage = MaybeUninit :: < sockaddr_storage > :: uninit ( ) ;
187
204
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
+ } ;
188
214
* sockaddr_in = sockaddr_in {
189
215
sin_family : AF_INET as sa_family_t ,
190
216
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,
194
218
..unsafe { mem:: zeroed ( ) }
195
219
} ;
196
220
SockAddr {
@@ -204,14 +228,24 @@ impl From<SocketAddrV6> for SockAddr {
204
228
fn from ( addr : SocketAddrV6 ) -> SockAddr {
205
229
let mut storage = MaybeUninit :: < sockaddr_storage > :: uninit ( ) ;
206
230
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
+ } ;
207
240
* sockaddr_in6 = sockaddr_in6 {
208
241
sin6_family : AF_INET6 as sa_family_t ,
209
242
sin6_port : addr. port ( ) . to_be ( ) ,
210
- sin6_addr : in6_addr {
211
- s6_addr : addr. ip ( ) . octets ( ) ,
212
- } ,
243
+ sin6_addr,
213
244
sin6_flowinfo : addr. flowinfo ( ) ,
245
+ #[ cfg( unix) ]
214
246
sin6_scope_id : addr. scope_id ( ) ,
247
+ #[ cfg( windows) ]
248
+ u : unsafe { mem:: transmute ( addr. scope_id ( ) ) } ,
215
249
..unsafe { mem:: zeroed ( ) }
216
250
} ;
217
251
SockAddr {
0 commit comments