Skip to content

Commit badc801

Browse files
committed
Don't assume representation of Ipv*Addr types
Also, remove two uses of unsafe :)
1 parent 2f0dc86 commit badc801

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

quinn/src/platform/unix.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{
22
io,
33
io::IoSliceMut,
44
mem::{self, MaybeUninit},
5-
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
5+
net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
66
os::unix::io::AsRawFd,
77
ptr,
88
task::{Context, Poll},
@@ -332,6 +332,12 @@ pub fn caps() -> UdpCapabilities {
332332

333333
const CMSG_LEN: usize = 80;
334334

335+
fn in_addr(addr: &Ipv4Addr) -> libc::in_addr {
336+
libc::in_addr {
337+
s_addr: u32::from_ne_bytes(addr.octets()),
338+
}
339+
}
340+
335341
fn prepare_msg(
336342
transmit: &Transmit,
337343
hdr: &mut libc::msghdr,
@@ -375,18 +381,16 @@ fn prepare_msg(
375381
IpAddr::V4(v4) => {
376382
let pktinfo = libc::in_pktinfo {
377383
ipi_ifindex: 0,
378-
ipi_spec_dst: unsafe {
379-
*(v4 as *const Ipv4Addr as *const () as *const libc::in_addr)
380-
},
384+
ipi_spec_dst: in_addr(v4),
381385
ipi_addr: libc::in_addr { s_addr: 0 },
382386
};
383387
encoder.push(libc::IPPROTO_IP, libc::IP_PKTINFO, pktinfo);
384388
}
385389
IpAddr::V6(v6) => {
386390
let pktinfo = libc::in6_pktinfo {
387391
ipi6_ifindex: 0,
388-
ipi6_addr: unsafe {
389-
*(v6 as *const Ipv6Addr as *const () as *const libc::in6_addr)
392+
ipi6_addr: libc::in6_addr {
393+
s6_addr: v6.octets(),
390394
},
391395
};
392396
encoder.push(libc::IPPROTO_IPV6, libc::IPV6_PKTINFO, pktinfo);

0 commit comments

Comments
 (0)