mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-27 01:30:44 +00:00
Add ioctlsocket FIONREAD and FIONBIO for loopback sockets
FIONREAD reports unread stream bytes or the size of the next datagram. FIONBIO accepts non-blocking mode; blocking mode returns WSAEOPNOTSUPP because loopback sockets never block. Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
This commit is contained in:
1 parent
16e0ff446a
commit
1fdefc7250
4 files changed
+114
No files matched your search
@@ -31,6 +31,7 @@ remains unsupported.
|
||||
- Added a Python 3 fallback for `timeout` in `scripts/test-gpu.sh`, so the native Metal suite runs on stock macOS without GNU coreutils while preserving bounded execution and the usual timeout exit status.
|
||||
|
||||
### Added
|
||||
- Winsock `ioctlsocket` with `FIONREAD` (unread stream bytes or next datagram size) and `FIONBIO` for non-blocking mode; requesting blocking mode returns `WSAEOPNOTSUPP` because loopback sockets never block.
|
||||
- Process-local Winsock TCP loopback: `listen`, `accept`, stream `connect`, `send`, `recv` and `shutdown` between guest sockets on 127.0.0.1, with 64 KiB of buffering per socket, end of file on shutdown or close, `WSAECONNRESET` after the peer closes, `POLLHUP` from `WSAPoll`, and separate TCP and UDP port spaces. No host socket is opened. Translated guest tests cover streaming, flow control, shutdown, closing, refusal and external-address rejection.
|
||||
- `MoveFileExW` on host mounts and the synthetic C: drive, with `MOVEFILE_REPLACE_EXISTING` (atomic replacement of a closed file, as in the write-then-replace save pattern), `MOVEFILE_WRITE_THROUGH` (flushes both host directories) and `MOVEFILE_COPY_ALLOWED`. Directories are never replaced, open files cannot be moved or replaced, and moves between drives report `ERROR_NOT_SAME_DEVICE` or, when a copy is allowed, `ERROR_NOT_SUPPORTED`.
|
||||
- `MoveFileW` renames closed files and directories within one writable host mount, including changes of case only. It uses an exclusive host rename (`renameatx_np` with `RENAME_EXCL` on macOS), so an existing name is never replaced. Moves between drives and of open files fail explicitly. A translated guest test covers renames, collisions, confinement and errors.
|
||||
|
||||
@@ -348,6 +348,7 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>
|
||||
register_socket_options(registry, state);
|
||||
register_poll(registry, state);
|
||||
register_stream(registry, state);
|
||||
register_ioctl(registry, state);
|
||||
|
||||
let get = Arc::clone(state);
|
||||
registry.register(MODULE, "WSAGetLastError", move |_, cpu, _| {
|
||||
@@ -1072,6 +1073,53 @@ fn register_stream(registry: &mut ImportRegistry, state: &Arc<ServicesState>) {
|
||||
);
|
||||
}
|
||||
|
||||
fn register_ioctl(registry: &mut ImportRegistry, state: &Arc<ServicesState>) {
|
||||
const FIONREAD: u32 = 0x4004_667f;
|
||||
const FIONBIO: u32 = 0x8004_667e;
|
||||
|
||||
let state = Arc::clone(state);
|
||||
registry.register_with_status(
|
||||
MODULE,
|
||||
"ioctlsocket",
|
||||
ServiceStatus::Partial,
|
||||
move |args, cpu, memory| {
|
||||
let net = state.winsock.lock().expect("guest Winsock state");
|
||||
let result = (|| {
|
||||
let socket = net.socket(args.rcx)?;
|
||||
let command = args.rdx as u32;
|
||||
if !matches!(command, FIONREAD | FIONBIO) {
|
||||
return Err(WSAEINVAL);
|
||||
}
|
||||
if args.r8 == 0 {
|
||||
return Err(WSAEFAULT);
|
||||
}
|
||||
if command == FIONBIO {
|
||||
// Every socket here is already non-blocking; blocking mode
|
||||
// is not supported rather than silently ignored.
|
||||
let enable = memory.read_u32(args.r8).map_err(|_| WSAEFAULT)?;
|
||||
return if enable != 0 {
|
||||
Ok(0)
|
||||
} else {
|
||||
Err(WSAEOPNOTSUPP)
|
||||
};
|
||||
}
|
||||
// FIONREAD: unread stream bytes, or the size of the next
|
||||
// datagram.
|
||||
let available = match socket.kind {
|
||||
Kind::Stream if socket.stream.listen.is_some() => return Err(WSAEINVAL),
|
||||
Kind::Stream => socket.stream.inbound.len(),
|
||||
Kind::Datagram => socket.pending.front().map_or(0, |(_, data)| data.len()),
|
||||
};
|
||||
memory
|
||||
.write_u32(args.r8, available as u32)
|
||||
.map_err(|_| WSAEFAULT)?;
|
||||
Ok(0)
|
||||
})();
|
||||
socket_result(&state, cpu.gs_base, result)
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[test]
|
||||
fn loopback_datagrams_preserve_boundaries_and_reject_external_addresses() {
|
||||
|
||||
@@ -27,6 +27,7 @@ const IMPORTS: &[(&str, &str)] = &[
|
||||
(WS2, "getsockname"),
|
||||
(WS2, "getsockopt"),
|
||||
(WS2, "WSAPoll"),
|
||||
(WS2, "ioctlsocket"),
|
||||
];
|
||||
|
||||
const ERROR: u64 = u32::MAX as u64;
|
||||
@@ -275,3 +276,63 @@ fn loopback_tcp_refuses_what_it_cannot_reach() {
|
||||
Err(10043) // WSAEPROTONOSUPPORT
|
||||
);
|
||||
}
|
||||
|
||||
/// ioctlsocket FIONREAD reports unread stream bytes and the size of the next
|
||||
/// datagram; FIONBIO accepts non-blocking mode only, since sockets here never
|
||||
/// block. Unknown commands and bad pointers fail.
|
||||
#[test]
|
||||
fn ioctlsocket_reports_readable_bytes_and_non_blocking_mode() {
|
||||
const FIONREAD: u64 = 0x4004_667f;
|
||||
const FIONBIO: u64 = 0x8004_667e;
|
||||
let mut net = Net::new();
|
||||
let listener = net.stream();
|
||||
assert_eq!(net.bind(listener, 5003), Ok(0));
|
||||
assert_eq!(net.call("listen", &[listener, 1], ERROR), Ok(0));
|
||||
let client = net.stream();
|
||||
assert_eq!(net.connect(client, [127, 0, 0, 1], 5003), Ok(0));
|
||||
let (server, _) = net.accept(listener).unwrap();
|
||||
let readable = |net: &mut Net, socket: u64| {
|
||||
net.0.memory.write_u32(VALUE, 0xdead_beef).unwrap();
|
||||
net.call("ioctlsocket", &[socket, FIONREAD, VALUE], ERROR)
|
||||
.map(|_| net.0.u32_at(VALUE))
|
||||
};
|
||||
assert_eq!(readable(&mut net, server), Ok(0));
|
||||
assert_eq!(net.send(client, b"abc"), Ok(3));
|
||||
assert_eq!(net.send(client, b"de"), Ok(2));
|
||||
assert_eq!(readable(&mut net, server), Ok(5));
|
||||
assert_eq!(net.recv(server, 4).unwrap(), b"abcd");
|
||||
assert_eq!(readable(&mut net, server), Ok(1));
|
||||
assert_eq!(readable(&mut net, listener), Err(WSAEINVAL));
|
||||
|
||||
let receiver = net.call("socket", &[2, 2, 17], INVALID_SOCKET).unwrap();
|
||||
let sender = net.call("socket", &[2, 2, 17], INVALID_SOCKET).unwrap();
|
||||
assert_eq!(net.bind(receiver, 5004), Ok(0));
|
||||
assert_eq!(net.connect(sender, [127, 0, 0, 1], 5004), Ok(0));
|
||||
assert_eq!(readable(&mut net, receiver), Ok(0));
|
||||
assert_eq!(net.send(sender, b"seven!!"), Ok(7));
|
||||
assert_eq!(net.send(sender, b"two"), Ok(3));
|
||||
assert_eq!(readable(&mut net, receiver), Ok(7));
|
||||
|
||||
net.0.memory.write_u32(VALUE, 1).unwrap();
|
||||
assert_eq!(
|
||||
net.call("ioctlsocket", &[server, FIONBIO, VALUE], ERROR),
|
||||
Ok(0)
|
||||
);
|
||||
net.0.memory.write_u32(VALUE, 0).unwrap();
|
||||
assert_eq!(
|
||||
net.call("ioctlsocket", &[server, FIONBIO, VALUE], ERROR),
|
||||
Err(WSAEOPNOTSUPP)
|
||||
);
|
||||
assert_eq!(
|
||||
net.call("ioctlsocket", &[server, FIONREAD, 0], ERROR),
|
||||
Err(10014)
|
||||
);
|
||||
assert_eq!(
|
||||
net.call("ioctlsocket", &[server, 0x1234, VALUE], ERROR),
|
||||
Err(WSAEINVAL)
|
||||
);
|
||||
assert_eq!(
|
||||
net.call("ioctlsocket", &[0x77, FIONREAD, VALUE], ERROR),
|
||||
Err(10038)
|
||||
);
|
||||
}
|
||||
@@ -313,6 +313,10 @@ connections it has not accepted. TCP and UDP ports are separate. `sendto`,
|
||||
`recvfrom` and socket options other than `SO_TYPE` are not supported on
|
||||
streams, and `shutdown` is not supported on datagram sockets. Raw sockets,
|
||||
IPv6, blocking waits on streams and host interfaces remain unsupported.
|
||||
`ioctlsocket` supports `FIONREAD` (unread stream bytes, or the size of the
|
||||
next datagram; `WSAEINVAL` on a listening socket) and `FIONBIO` with a nonzero
|
||||
argument. Every socket is already non-blocking, so asking for blocking mode
|
||||
returns `WSAEOPNOTSUPP` instead of being ignored.
|
||||
|
||||
### XAsync helpers
|
||||
|
||||
|
||||
Reference in new issue
Block a user