Skip to content

Commit

Permalink
Add apple tvos support
Browse files Browse the repository at this point in the history
  • Loading branch information
lcruz99 committed Oct 10, 2023
1 parent f5ab216 commit 07cac19
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/packetdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn main() {
match rx.next() {
Ok(packet) => {
let payload_offset;
if cfg!(any(target_os = "macos", target_os = "ios"))
if cfg!(any(target_os = "macos", target_os = "ios", target_os = "tvos"))
&& interface.is_up()
&& !interface.is_broadcast()
&& ((!interface.is_loopback() && interface.is_point_to_point())
Expand Down
18 changes: 14 additions & 4 deletions pnet_datalink/src/bindings/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ pub const DLT_NULL: libc::c_uint = 0;
target_os = "solaris"
))]
const BPF_ALIGNMENT: libc::c_int = SIZEOF_C_LONG;
#[cfg(any(target_os = "openbsd", target_os = "macos", target_os = "ios", windows))]
#[cfg(any(
target_os = "openbsd",
target_os = "macos",
target_os = "ios",
target_os = "tvos",
windows
))]
const BPF_ALIGNMENT: libc::c_int = 4;

pub fn BPF_WORDALIGN(x: isize) -> isize {
Expand All @@ -94,7 +100,8 @@ pub struct ifreq {
target_os = "illumos",
target_os = "solaris",
target_os = "macos",
target_os = "ios"
target_os = "ios",
target_os = "tvos"
))]
#[repr(C)]
pub struct sockaddr_dl {
Expand All @@ -115,7 +122,7 @@ pub struct sockaddr_dl {
target_os = "illumos",
target_os = "solaris",
all(
any(target_os = "macos", target_os = "ios"),
any(target_os = "macos", target_os = "ios", target_os = "tvos"),
target_pointer_width = "32"
),
windows
Expand All @@ -136,7 +143,10 @@ pub struct timeval32 {

#[cfg(any(
target_os = "openbsd",
all(any(target_os = "macos", target_os = "ios"), target_pointer_width = "64")
all(
any(target_os = "macos", target_os = "ios", target_os = "tvos"),
target_pointer_width = "64"
)
))]
#[repr(C)]
pub struct bpf_hdr {
Expand Down
1 change: 1 addition & 0 deletions pnet_datalink/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
target_os = "solaris",
target_os = "macos",
target_os = "ios",
target_os = "tvos",
windows
))]
pub mod bpf;
Expand Down
4 changes: 2 additions & 2 deletions pnet_datalink/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn channel(network_interface: &NetworkInterface, config: Config) -> io::Resu
}
}

#[cfg(any(target_os = "openbsd", target_os = "macos", target_os = "ios"))]
#[cfg(any(target_os = "openbsd", target_os = "macos", target_os = "ios", target_os = "tvos"))]
fn get_fd(attempts: usize) -> libc::c_int {
for i in 0..attempts {
let fd = unsafe {
Expand Down Expand Up @@ -129,7 +129,7 @@ pub fn channel(network_interface: &NetworkInterface, config: Config) -> io::Resu
Ok(())
}

#[cfg(any(target_os = "macos", target_os = "openbsd", target_os = "ios"))]
#[cfg(any(target_os = "macos", target_os = "openbsd", target_os = "ios", target_os = "tvos"))]
fn set_feedback(_fd: libc::c_int) -> io::Result<()> {
Ok(())
}
Expand Down
6 changes: 4 additions & 2 deletions pnet_datalink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ pub mod linux;
target_os = "illumos",
target_os = "solaris",
target_os = "macos",
target_os = "ios"
target_os = "ios",
target_os = "tvos"
)
))]
#[path = "bpf.rs"]
Expand All @@ -66,7 +67,8 @@ mod backend;
target_os = "illumos",
target_os = "solaris",
target_os = "macos",
target_os = "ios"
target_os = "ios",
target_os = "tvos"
))]
pub mod bpf;

Expand Down
3 changes: 2 additions & 1 deletion pnet_datalink/src/unix_interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ fn sockaddr_to_network_addr(sa: *const libc::sockaddr) -> (Option<MacAddr>, Opti
target_os = "illumos",
target_os = "solaris",
target_os = "macos",
target_os = "ios"
target_os = "ios",
target_os = "tvos"
))]
fn sockaddr_to_network_addr(sa: *const libc::sockaddr) -> (Option<MacAddr>, Option<IpAddr>) {
use crate::bindings::bpf;
Expand Down
2 changes: 1 addition & 1 deletion pnet_packet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ default = ["std"]

[dev-dependencies]
hex = "0.4.3"
criterion = { version = "0.4.0", features = ["html_reports"] } #added HTML feature becuase of the annoying warnings when running the tests
criterion = { version = "0.4.0", features = ["html_reports"] } #added HTML feature because of the annoying warnings when running the tests

[build-dependencies]
glob = "0.3.1"
Expand Down
14 changes: 12 additions & 2 deletions pnet_sys/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,19 @@ pub mod public {
pub type InAddr = libc::in_addr;
pub type In6Addr = libc::in6_addr;

#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "netbsd")))]
#[cfg(not(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "netbsd"
)))]
pub type TvUsecType = libc::c_long;
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "netbsd"))]
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "netbsd"
))]
pub type TvUsecType = libc::c_int;
#[cfg(not(any(target_os = "illumos", target_os = "solaris")))]
pub type InAddrType = libc::c_uint;
Expand Down
13 changes: 9 additions & 4 deletions pnet_transport/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ impl TransportSender {

#[cfg(all(
not(target_os = "freebsd"),
not(any(target_os = "macos", target_os = "ios"))
not(any(target_os = "macos", target_os = "ios", target_os = "tvos"))
))]
fn send_to_impl<T: Packet>(&mut self, packet: T, dst: IpAddr) -> io::Result<usize> {
self.send(packet, dst)
}

#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "ios"))]
#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "ios", target_os = "tvos"))]
fn send_to_impl<T: Packet>(&mut self, packet: T, dst: IpAddr) -> io::Result<usize> {
use pnet_packet::ipv4::MutableIpv4Packet;
use pnet_packet::MutablePacket;
Expand Down Expand Up @@ -328,7 +328,12 @@ macro_rules! transport_channel_iterator {
Err(e) => Err(e),
};

#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "ios"))]
#[cfg(any(
target_os = "freebsd",
target_os = "macos",
target_os = "ios",
target_os = "tvos"
))]
fn fixup_packet(buffer: &mut [u8]) {
use pnet_packet::ipv4::MutableIpv4Packet;

Expand All @@ -352,7 +357,7 @@ macro_rules! transport_channel_iterator {

#[cfg(all(
not(target_os = "freebsd"),
not(any(target_os = "macos", target_os = "ios"))
not(any(target_os = "macos", target_os = "ios", target_os = "tvos"))
))]
fn fixup_packet(_buffer: &mut [u8]) {}
}
Expand Down

0 comments on commit 07cac19

Please sign in to comment.