Skip to content

Commit 6ed274e

Browse files
committed
fix(host): translate POSIX poll flags to Windows WSAPoll flags
The guest sends POSIX poll flags (POLLIN=1, POLLOUT=4, POLLERR=8) but WSAPoll uses different values (POLLRDNORM=256, POLLWRNORM=16, POLLERR=1). Without translation, POSIX POLLIN was interpreted as Windows POLLERR, preventing connection readiness detection on Windows. Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent fa0d7bb commit 6ed274e

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

host/src/lib.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,47 @@ fn handle_net_poll(
17051705
) -> Result<serde_json::Value> {
17061706
use serde_json::json;
17071707
use std::os::windows::io::AsRawSocket;
1708-
use windows_sys::Win32::Networking::WinSock::{WSAPoll, POLLNVAL, WSAPOLLFD};
1708+
use windows_sys::Win32::Networking::WinSock::{
1709+
WSAPoll, POLLERR as W_POLLERR, POLLHUP as W_POLLHUP, POLLNVAL as W_POLLNVAL, POLLRDNORM,
1710+
POLLWRNORM, WSAPOLLFD,
1711+
};
1712+
1713+
const POSIX_POLLIN: i16 = 0x0001;
1714+
const POSIX_POLLOUT: i16 = 0x0004;
1715+
const POSIX_POLLERR: i16 = 0x0008;
1716+
const POSIX_POLLHUP: i16 = 0x0010;
1717+
const POSIX_POLLNVAL: i16 = 0x0020;
1718+
1719+
fn posix_to_win(posix: i16) -> i16 {
1720+
let mut win: i16 = 0;
1721+
if posix & POSIX_POLLIN != 0 {
1722+
win |= POLLRDNORM;
1723+
}
1724+
if posix & POSIX_POLLOUT != 0 {
1725+
win |= POLLWRNORM;
1726+
}
1727+
win
1728+
}
1729+
1730+
fn win_to_posix(win: i16) -> i16 {
1731+
let mut posix: i16 = 0;
1732+
if win & POLLRDNORM != 0 {
1733+
posix |= POSIX_POLLIN;
1734+
}
1735+
if win & POLLWRNORM != 0 {
1736+
posix |= POSIX_POLLOUT;
1737+
}
1738+
if win & W_POLLERR != 0 {
1739+
posix |= POSIX_POLLERR;
1740+
}
1741+
if win & W_POLLHUP != 0 {
1742+
posix |= POSIX_POLLHUP;
1743+
}
1744+
if win & W_POLLNVAL != 0 {
1745+
posix |= POSIX_POLLNVAL;
1746+
}
1747+
posix
1748+
}
17091749

17101750
let fds_val = args
17111751
.get("fds")
@@ -1731,7 +1771,7 @@ fn handle_net_poll(
17311771
if !(0..=i16::MAX as i64).contains(&raw_events) {
17321772
return Err(anyhow!("net_poll: events {raw_events} out of i16 range"));
17331773
}
1734-
let events = raw_events as i16;
1774+
let events = posix_to_win(raw_events as i16);
17351775
if let Ok(sock) = tbl.get_socket(fd) {
17361776
pollfds.push(WSAPOLLFD {
17371777
fd: sock.as_raw_socket() as usize,
@@ -1740,7 +1780,7 @@ fn handle_net_poll(
17401780
});
17411781
guest_fds.push(fd);
17421782
} else {
1743-
ready.push(json!({ "fd": fd, "revents": POLLNVAL as i64 }));
1783+
ready.push(json!({ "fd": fd, "revents": POSIX_POLLNVAL as i64 }));
17441784
}
17451785
}
17461786
drop(tbl);
@@ -1760,7 +1800,7 @@ fn handle_net_poll(
17601800
if pfd.revents != 0 {
17611801
ready.push(json!({
17621802
"fd": guest_fds[i],
1763-
"revents": pfd.revents as i64,
1803+
"revents": win_to_posix(pfd.revents) as i64,
17641804
}));
17651805
}
17661806
}

0 commit comments

Comments
 (0)