Skip to content

Commit

Permalink
Update generation of pcap and pktap symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusPettersson98 committed Aug 28, 2024
1 parent 27c4644 commit 43b276e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 265 deletions.
306 changes: 53 additions & 253 deletions talpid-core/src/split_tunnel/macos/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// automatically generated by rust-bindgen 0.69.2
/* automatically generated by rust-bindgen 0.70.1 */

pub const PTH_FLAG_DIR_OUT: u32 = 2;
pub type __int32_t = ::std::os::raw::c_int;
Expand All @@ -11,41 +11,13 @@ pub struct timeval32 {
pub tv_sec: __int32_t,
pub tv_usec: __int32_t,
}
#[test]
fn bindgen_test_layout_timeval32() {
const UNINIT: ::std::mem::MaybeUninit<timeval32> = ::std::mem::MaybeUninit::uninit();
let ptr = UNINIT.as_ptr();
assert_eq!(
::std::mem::size_of::<timeval32>(),
8usize,
concat!("Size of: ", stringify!(timeval32))
);
assert_eq!(
::std::mem::align_of::<timeval32>(),
4usize,
concat!("Alignment of ", stringify!(timeval32))
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).tv_sec) as usize - ptr as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(timeval32),
"::",
stringify!(tv_sec)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).tv_usec) as usize - ptr as usize },
4usize,
concat!(
"Offset of field: ",
stringify!(timeval32),
"::",
stringify!(tv_usec)
)
);
}
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
["Size of timeval32"][::std::mem::size_of::<timeval32>() - 8usize];
["Alignment of timeval32"][::std::mem::align_of::<timeval32>() - 4usize];
["Offset of field: timeval32::tv_sec"][::std::mem::offset_of!(timeval32, tv_sec) - 0usize];
["Offset of field: timeval32::tv_usec"][::std::mem::offset_of!(timeval32, tv_usec) - 4usize];
};
pub type uuid_t = __darwin_uuid_t;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
Expand All @@ -71,220 +43,48 @@ pub struct pktap_header {
pub pth_uuid: uuid_t,
pub pth_euuid: uuid_t,
}
#[test]
fn bindgen_test_layout_pktap_header() {
const UNINIT: ::std::mem::MaybeUninit<pktap_header> = ::std::mem::MaybeUninit::uninit();
let ptr = UNINIT.as_ptr();
assert_eq!(
::std::mem::size_of::<pktap_header>(),
156usize,
concat!("Size of: ", stringify!(pktap_header))
);
assert_eq!(
::std::mem::align_of::<pktap_header>(),
4usize,
concat!("Alignment of ", stringify!(pktap_header))
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_length) as usize - ptr as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_length)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_type_next) as usize - ptr as usize },
4usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_type_next)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_dlt) as usize - ptr as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_dlt)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_ifname) as usize - ptr as usize },
12usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_ifname)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_flags) as usize - ptr as usize },
36usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_flags)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_protocol_family) as usize - ptr as usize },
40usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_protocol_family)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_frame_pre_length) as usize - ptr as usize },
44usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_frame_pre_length)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_frame_post_length) as usize - ptr as usize },
48usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_frame_post_length)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_pid) as usize - ptr as usize },
52usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_pid)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_comm) as usize - ptr as usize },
56usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_comm)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_svc) as usize - ptr as usize },
76usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_svc)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_iftype) as usize - ptr as usize },
80usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_iftype)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_ifunit) as usize - ptr as usize },
82usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_ifunit)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_epid) as usize - ptr as usize },
84usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_epid)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_ecomm) as usize - ptr as usize },
88usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_ecomm)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_flowid) as usize - ptr as usize },
108usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_flowid)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_ipproto) as usize - ptr as usize },
112usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_ipproto)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_tstamp) as usize - ptr as usize },
116usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_tstamp)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_uuid) as usize - ptr as usize },
124usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_uuid)
)
);
assert_eq!(
unsafe { ::std::ptr::addr_of!((*ptr).pth_euuid) as usize - ptr as usize },
140usize,
concat!(
"Offset of field: ",
stringify!(pktap_header),
"::",
stringify!(pth_euuid)
)
);
}

pub const BIOCSWANTPKTAP: u64 = 3221504639;
#[allow(clippy::unnecessary_operation, clippy::identity_op)]
const _: () = {
["Size of pktap_header"][::std::mem::size_of::<pktap_header>() - 156usize];
["Alignment of pktap_header"][::std::mem::align_of::<pktap_header>() - 4usize];
["Offset of field: pktap_header::pth_length"]
[::std::mem::offset_of!(pktap_header, pth_length) - 0usize];
["Offset of field: pktap_header::pth_type_next"]
[::std::mem::offset_of!(pktap_header, pth_type_next) - 4usize];
["Offset of field: pktap_header::pth_dlt"]
[::std::mem::offset_of!(pktap_header, pth_dlt) - 8usize];
["Offset of field: pktap_header::pth_ifname"]
[::std::mem::offset_of!(pktap_header, pth_ifname) - 12usize];
["Offset of field: pktap_header::pth_flags"]
[::std::mem::offset_of!(pktap_header, pth_flags) - 36usize];
["Offset of field: pktap_header::pth_protocol_family"]
[::std::mem::offset_of!(pktap_header, pth_protocol_family) - 40usize];
["Offset of field: pktap_header::pth_frame_pre_length"]
[::std::mem::offset_of!(pktap_header, pth_frame_pre_length) - 44usize];
["Offset of field: pktap_header::pth_frame_post_length"]
[::std::mem::offset_of!(pktap_header, pth_frame_post_length) - 48usize];
["Offset of field: pktap_header::pth_pid"]
[::std::mem::offset_of!(pktap_header, pth_pid) - 52usize];
["Offset of field: pktap_header::pth_comm"]
[::std::mem::offset_of!(pktap_header, pth_comm) - 56usize];
["Offset of field: pktap_header::pth_svc"]
[::std::mem::offset_of!(pktap_header, pth_svc) - 76usize];
["Offset of field: pktap_header::pth_iftype"]
[::std::mem::offset_of!(pktap_header, pth_iftype) - 80usize];
["Offset of field: pktap_header::pth_ifunit"]
[::std::mem::offset_of!(pktap_header, pth_ifunit) - 82usize];
["Offset of field: pktap_header::pth_epid"]
[::std::mem::offset_of!(pktap_header, pth_epid) - 84usize];
["Offset of field: pktap_header::pth_ecomm"]
[::std::mem::offset_of!(pktap_header, pth_ecomm) - 88usize];
["Offset of field: pktap_header::pth_flowid"]
[::std::mem::offset_of!(pktap_header, pth_flowid) - 108usize];
["Offset of field: pktap_header::pth_ipproto"]
[::std::mem::offset_of!(pktap_header, pth_ipproto) - 112usize];
["Offset of field: pktap_header::pth_tstamp"]
[::std::mem::offset_of!(pktap_header, pth_tstamp) - 116usize];
["Offset of field: pktap_header::pth_uuid"]
[::std::mem::offset_of!(pktap_header, pth_uuid) - 124usize];
["Offset of field: pktap_header::pth_euuid"]
[::std::mem::offset_of!(pktap_header, pth_euuid) - 140usize];
};
8 changes: 0 additions & 8 deletions talpid-core/src/split_tunnel/macos/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ use std::{
};
use tokio::io::{unix::AsyncFd, AsyncRead, Interest, ReadBuf};

use super::bindings::BIOCSWANTPKTAP;

#[derive(thiserror::Error, Debug)]
pub enum Error {
/// Failed to open BPF device
Expand Down Expand Up @@ -167,12 +165,6 @@ impl Bpf {
unsafe { ioctl!(self.file.as_raw_fd(), BIOCSHDRCMPLT, &enable) }
}

pub fn set_want_pktap(&self, enable: bool) -> Result<(), Error> {
let enable: c_int = if enable { 1 } else { 0 };
// SAFETY: The fd is valid for the lifetime of `self`
unsafe { ioctl!(self.file.as_raw_fd(), BIOCSWANTPKTAP, &enable) }
}

pub fn set_buffer_size(&self, mut buffer_size: c_uint) -> Result<usize, Error> {
// SAFETY: The fd is valid for the lifetime of `self`
unsafe {
Expand Down
4 changes: 0 additions & 4 deletions talpid-core/src/split_tunnel/macos/generate-bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,5 @@ curl https://opensource.apple.com/source/libpcap/libpcap-67/libpcap/pcap/pcap.h
curl https://opensource.apple.com/source/xnu/xnu-3789.41.3/bsd/net/bpf.h -o include/bpf.h

bindgen "include/bindings.h" -o ./bindings.rs \
--allowlist-item "^pcap_create" \
--allowlist-item "^pcap_set_want_pktap" \
--allowlist-item "^pktap_header" \
--allowlist-item "PCAP_ERRBUF_SIZE" \
--allowlist-item "^BIOCSWANTPKTAP" \
--allowlist-item "^PTH_FLAG_DIR_OUT"

0 comments on commit 43b276e

Please sign in to comment.