Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cast_bytes() #39

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/bsd/ifconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
os::fd::AsRawFd,
};

use libc::{c_char, kld_load};
use libc::{c_char, kld_load, IF_NAMESIZE};
use nix::{ioctl_readwrite, ioctl_write_ptr, sys::socket::AddressFamily};

use super::{
Expand Down Expand Up @@ -31,17 +31,17 @@ ioctl_write_ptr!(del_addr_if_in6, b'i', 25, IfReq6);
/// Represent `struct ifreq` as defined in `net/if.h`.
#[repr(C)]
pub struct IfReq {
ifr_name: [u8; 16],
ifr_name: [u8; IF_NAMESIZE],
ifr_ifru: SockAddrIn,
}

impl IfReq {
#[must_use]
pub(super) fn new(if_name: &str) -> Self {
let mut ifr_name = [0u8; 16];
let mut ifr_name = [0u8; IF_NAMESIZE];
if_name
.bytes()
.take(15)
.take(IF_NAMESIZE - 1)
.enumerate()
.for_each(|(i, b)| ifr_name[i] = b);

Expand Down Expand Up @@ -94,17 +94,17 @@ impl IfReq {
/// Represent `struct in6_ifreq` as defined in `netinet6/in6_var.h`.
#[repr(C)]
pub struct IfReq6 {
ifr_name: [u8; 16],
ifr_name: [u8; IF_NAMESIZE],
ifr_ifru: SockAddrIn6,
}

impl IfReq6 {
#[must_use]
pub(super) fn new(if_name: &str) -> Self {
let mut ifr_name = [0u8; 16];
let mut ifr_name = [0u8; IF_NAMESIZE];
if_name
.bytes()
.take(15)
.take(IF_NAMESIZE - 1)
.enumerate()
.for_each(|(i, b)| ifr_name[i] = b);

Expand All @@ -129,7 +129,7 @@ impl IfReq6 {
/// Respresent `in_aliasreq` as defined in <netinet/in_var.h>.
#[repr(C)]
pub struct InAliasReq {
ifr_name: [u8; 16],
ifr_name: [u8; IF_NAMESIZE],
ifra_addr: SockAddrIn,
ifra_broadaddr: SockAddrIn,
ifra_mask: SockAddrIn,
Expand All @@ -144,10 +144,10 @@ impl InAliasReq {
broadcast: &Ipv4Addr,
mask: &Ipv4Addr,
) -> Self {
let mut ifr_name = [0u8; 16];
let mut ifr_name = [0u8; IF_NAMESIZE];
if_name
.bytes()
.take(15)
.take(IF_NAMESIZE - 1)
.enumerate()
.for_each(|(i, b)| ifr_name[i] = b);

Expand All @@ -174,7 +174,7 @@ impl InAliasReq {
/// Respresent `in6_aliasreq` as defined in <netinet/in6_var.h>.
#[repr(C)]
pub struct In6AliasReq {
ifr_name: [u8; 16],
ifr_name: [u8; IF_NAMESIZE],
ifra_addr: SockAddrIn6,
ifra_dstaddr: SockAddrIn6,
ifra_prefixmask: SockAddrIn6,
Expand All @@ -195,10 +195,10 @@ impl In6AliasReq {
dstaddr: &Ipv6Addr,
prefixmask: &Ipv6Addr,
) -> Self {
let mut ifr_name = [0u8; 16];
let mut ifr_name = [0u8; IF_NAMESIZE];
if_name
.bytes()
.take(15)
.take(IF_NAMESIZE - 1)
.enumerate()
.for_each(|(i, b)| ifr_name[i] = b);

Expand Down
6 changes: 2 additions & 4 deletions src/bsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ mod timespec;
mod wgio;

use std::{
collections::HashMap, mem::size_of, net::IpAddr, os::fd::OwnedFd, ptr::addr_of,
slice::from_raw_parts,
collections::HashMap, mem::size_of, net::IpAddr, os::fd::OwnedFd, slice::from_raw_parts,
};

use nix::{
Expand Down Expand Up @@ -58,8 +57,7 @@ unsafe fn cast_ref<T>(bytes: &[u8]) -> &T {

/// Cast `T' to bytes.
unsafe fn cast_bytes<T: Sized>(p: &T) -> &[u8] {
let ptr = addr_of!(p).cast::<u8>();
from_raw_parts(ptr, size_of::<T>())
from_raw_parts((p as *const T) as *const u8, size_of::<T>())
}

/// Create socket for ioctl communication.
Expand Down
86 changes: 82 additions & 4 deletions src/bsd/sockaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,96 @@ mod tests {
use super::*;

#[test]
fn ip4() {
fn pack_ip4() {
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(192, 168, 12, 34)), 7301);
let buf = pack_sockaddr(&addr);
assert_eq!(
buf,
[16, 2, 28, 133, 192, 168, 12, 34, 0, 0, 0, 0, 0, 0, 0, 0]
);
}

#[test]
fn unpack_ip4() {
let buf = [16, 2, 28, 133, 192, 168, 12, 34, 0, 0, 0, 0, 0, 0, 0, 0];
let addr = unpack_sockaddr(&buf).unwrap();
assert_eq!(addr.port(), 7301);
assert_eq!(addr.ip(), IpAddr::V4(Ipv4Addr::new(192, 168, 12, 34)));
}

#[test]
fn ip6() {
fn pack_ip6() {
let addr = SocketAddr::new(
IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc0a8, 0x0c22)),
7301,
);
let buf = pack_sockaddr(&addr);
assert_eq!(
buf,
[
28,
AF_INET6 as u8,
28,
133,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
255,
255,
192,
168,
12,
34,
0,
0,
0,
0,
]
);
}

#[test]
fn unpack_ip6() {
let buf = [
28, 30, 28, 133, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 192, 168, 12, 34,
0, 0, 0, 0,
28,
AF_INET6 as u8,
28,
133,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
255,
255,
192,
168,
12,
34,
0,
0,
0,
0,
];
let addr = unpack_sockaddr(&buf).unwrap();
assert_eq!(addr.port(), 7301);
Expand Down
7 changes: 4 additions & 3 deletions src/bsd/wgio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
slice::from_raw_parts,
};

use libc::IF_NAMESIZE;
use nix::{ioctl_readwrite, sys::socket::AddressFamily};

use super::{create_socket, IoError};
Expand All @@ -17,7 +18,7 @@ ioctl_readwrite!(read_wireguard_data, b'i', 211, WgDataIo);
/// https://github.com/freebsd/freebsd-src/blob/main/sys/dev/wg/if_wg.h
#[repr(C)]
pub struct WgDataIo {
pub(super) wgd_name: [u8; 16],
pub(super) wgd_name: [u8; IF_NAMESIZE],
pub(super) wgd_data: *mut u8, // *void
pub(super) wgd_size: usize,
}
Expand All @@ -26,10 +27,10 @@ impl WgDataIo {
/// Create `WgDataIo` without data buffer.
#[must_use]
pub fn new(if_name: &str) -> Self {
let mut wgd_name = [0u8; 16];
let mut wgd_name = [0u8; IF_NAMESIZE];
if_name
.bytes()
.take(15)
.take(IF_NAMESIZE - 1)
.enumerate()
.for_each(|(i, b)| wgd_name[i] = b);
Self {
Expand Down
2 changes: 1 addition & 1 deletion src/wgapi_freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl WireguardInterfaceApi for WireguardApiFreebsd {
info!("Removing interface {}", &self.ifname);
bsd::delete_interface(&self.ifname)?;

clear_dns(&self.ifname);
clear_dns(&self.ifname)?;
Ok(())
}

Expand Down