Skip to content

Commit 8422bfa

Browse files
committed
socket: Respect IPv6 flowinfo and scope_id in InetAddr::from_std
Fixes #329
1 parent a09d07b commit 8422bfa

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/sys/socket/addr.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,26 @@ pub enum InetAddr {
3636

3737
impl InetAddr {
3838
pub fn from_std(std: &net::SocketAddr) -> InetAddr {
39-
let ip = match *std {
40-
net::SocketAddr::V4(ref addr) => IpAddr::V4(Ipv4Addr::from_std(&addr.ip())),
41-
net::SocketAddr::V6(ref addr) => IpAddr::V6(Ipv6Addr::from_std(&addr.ip())),
42-
};
43-
44-
InetAddr::new(ip, std.port())
39+
match *std {
40+
net::SocketAddr::V4(ref addr) => {
41+
InetAddr::V4(libc::sockaddr_in {
42+
sin_family: AddressFamily::Inet as sa_family_t,
43+
sin_port: addr.port().to_be(), // network byte order
44+
sin_addr: Ipv4Addr::from_std(addr.ip()).0,
45+
.. unsafe { mem::zeroed() }
46+
})
47+
}
48+
net::SocketAddr::V6(ref addr) => {
49+
InetAddr::V6(libc::sockaddr_in6 {
50+
sin6_family: AddressFamily::Inet6 as sa_family_t,
51+
sin6_port: addr.port().to_be(), // network byte order
52+
sin6_addr: Ipv6Addr::from_std(addr.ip()).0,
53+
sin6_flowinfo: addr.flowinfo(), // host byte order
54+
sin6_scope_id: addr.scope_id(), // host byte order
55+
.. unsafe { mem::zeroed() }
56+
})
57+
}
58+
}
4559
}
4660

4761
pub fn new(ip: IpAddr, port: u16) -> InetAddr {

0 commit comments

Comments
 (0)