Skip to content

Commit cccf341

Browse files
committed
Adapting the pty, tun and udp drivers for FreeBSD.
1 parent 150a717 commit cccf341

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

rust/linkfd/src/pty_dev.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,44 @@ pub(crate) struct PtyDev{
2525
}
2626

2727
impl PtyDev {
28+
#[cfg(not(target_os = "linux"))]
29+
pub fn new() -> Option<Self> {
30+
let mut ptyname: [u8;10] = [0u8; 10];
31+
{
32+
let const_ptyname = "/dev/ptyXY".as_bytes();
33+
for i in 0..const_ptyname.len() {
34+
ptyname[i] = const_ptyname[i];
35+
}
36+
}
37+
let ch = "pqrstuvwxyz".as_bytes();
38+
let digit = "0123456789abcdefghijklmnopqrstuv".as_bytes();
39+
40+
/* This algorithm should work for almost all standard Unices */
41+
for l in 0..ch.len() {
42+
for m in 0..digit.len() {
43+
ptyname[8 as usize] = ch[l];
44+
ptyname[9 as usize] = digit[m];
45+
/* Open the master */
46+
let mr_fd= unsafe { libc::open(ptyname.as_ptr() as *const i8, libc::O_RDWR) };
47+
if mr_fd < 0 {
48+
continue;
49+
}
50+
/* Check the slave */
51+
ptyname[5 as usize] = 't' as u8;
52+
if unsafe { libc::access(ptyname.as_ptr() as *const i8, libc::R_OK | libc::W_OK) } < 0 {
53+
unsafe { libc::close(mr_fd); }
54+
ptyname[5 as usize] = 'p' as u8;
55+
continue;
56+
}
57+
return Some(Self {
58+
fd: mr_fd,
59+
ptyname: Box::new(str::from_utf8(&ptyname).unwrap().to_string())
60+
});
61+
}
62+
}
63+
None
64+
}
65+
#[cfg(target_os = "linux")]
2866
pub fn new() -> Option<Self> {
2967
/*
3068
* At first it appears that getpt is available on if not all,

rust/linkfd/src/tun_dev.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ impl TunDev {
101101
pub fn new_from_fd(fd: i32, dev: &str) -> TunDev {
102102
TunDev { name: Some(Box::new(dev.to_string())), fd: Some(fd) }
103103
}
104+
105+
#[cfg(not(target_os = "linux"))]
106+
fn linux_prep(&mut self, _name: Option<&str>, _dev_type: TunDevType) -> bool {
107+
false
108+
}
104109
#[cfg(target_os = "linux")]
105110
fn linux_prep(&mut self, name: Option<&str>, dev_type: TunDevType) -> bool {
106111
let mut ifr_flags: libc::c_short;

rust/linkfd/src/udp_proto.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ pub(crate) struct UdpProto {
2424
pub is_rmt_fd_connected: bool
2525
}
2626

27+
impl UdpProto {
28+
#[cfg(not(target_os = "linux"))] /* freebsd */
29+
fn create_sockaddr_in() -> libc::sockaddr_in {
30+
libc::sockaddr_in {
31+
sin_family: 0,
32+
sin_port: 0,
33+
sin_addr: libc::in_addr { s_addr: 0 },
34+
sin_zero: [0i8; 8],
35+
sin_len: 0
36+
}
37+
}
38+
#[cfg(target_os = "linux")] /* linux */
39+
fn create_sockaddr_in() -> libc::sockaddr_in {
40+
libc::sockaddr_in {
41+
sin_family: 0,
42+
sin_port: 0,
43+
sin_addr: libc::in_addr { s_addr: 0 },
44+
sin_zero: [0u8; 8],
45+
}
46+
}
47+
}
48+
2749
impl driver::NetworkDriver for UdpProto {
2850
fn write(&self, buf: &mut Vec<u8>, flags: u16) -> Option<usize> {
2951
let payloadlen = buf.len();
@@ -56,18 +78,14 @@ impl driver::NetworkDriver for UdpProto {
5678
return Some(wlen as usize);
5779
}
5880
}
81+
5982
fn read(&mut self, buf: &mut Vec<u8>) -> Option<u16> {
6083
//unsigned short hdr, flen;
6184
let mut hdrbuf: [u8; 2] = [0u8; 2];
6285
let mut iv: [libc::iovec; 2] = [libc::iovec { iov_base: null_mut(), iov_len: 0 }; 2];
6386
//register int rlen;
6487
//struct sockaddr_in from;
65-
let mut from: libc::sockaddr_in = libc::sockaddr_in {
66-
sin_family: 0,
67-
sin_port: 0,
68-
sin_addr: libc::in_addr { s_addr: 0 },
69-
sin_zero: [0u8; 8],
70-
};
88+
let mut from: libc::sockaddr_in = UdpProto::create_sockaddr_in();
7189
let mut fromlen: libc::socklen_t = size_of::<libc::sockaddr_in>() as libc::socklen_t;
7290

7391
/* Late connect (NAT hack enabled) */

0 commit comments

Comments
 (0)