Commit Graph
168 Commits
Author SHA1 Message Date
Ondra97 c415375041 fix(net): finish blocking stream sends after short writes 2026-10-21 01:53:43 +02:00
Dean b753508271 Merge pull request #8454 from hypermoist/feat/libkernel-byte-order
feat(libkernel): export htonl, ntohl, htons and ntohs
2026-10-21 12:40:32 +13:00
Ondra97 1b182e7fb2 fix(net): keep MSG_DONTWAIT transfers nonblocking on Windows 2026-10-20 21:47:08 +02:00
MarcZX10 2299284a69 fix(libkernel): keep socket descriptors within FD_SETSIZE 2026-10-20 17:41:40 +00:00
Adrià 4036fab1b2 Merge pull request #7437 from lucaslima430/fix/libkernel-socket-fcntl-duplication
fix(libkernel): support socket fcntl duplication
2026-10-20 17:40:06 +02:00
hypermistandClaude Opus 5.5 14dbed4ed1 feat(libkernel): export htonl, ntohl, htons and ntohs
libkernel exports the byte order conversions; they swap the bytes of
their 32- or 16-bit argument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
2026-10-21 01:48:48 +13:00
Queuze bbcce1736e fix(libkernel): support socket fcntl duplication 2026-10-20 13:40:22 +02:00
Alexej Szpevák 92e136a129 feat(libkernel): report the write end of a pipe to equeue descriptor polls
GuestSockets::Ready only knew the read end of a pipe, and WaitAny asked the host for POLLIN on every descriptor that is not a socket, so a write interest on a pipe could never be reported or wake a wait. PipeWriteState gives the free space of the host buffer (F_GETPIPE_SZ less FIONREAD on Linux, the write quota on Windows), EOF once the read end is closed, and whether the host takes a write now; Ready reports the write end when it is writable or at EOF, and WaitAny asks for POLLOUT on it. macOS has no call for the capacity of a pipe and throws.
2026-10-20 13:34:50 +02:00
Adrià f1d13abca4 Merge pull request #7559 from mugurc/fix/libkernel-listening-socket-macos
fix(libkernel): recognize listening sockets on macOS hosts
2026-10-20 12:56:12 +02:00
Dean 1e3efa7ccf Merge pull request #8132 from Retr0G28/fix/socketpair-cloexec
fix(libkernel): mark both socketpair ends close-on-exec for SOCK_CLOEXEC
2026-10-20 18:31:15 +13:00
Dean 564c75b289 Merge pull request #7956 from Lindamea1/fix/libkernel-ioctl-malformed-enotty
fix(libkernel): fail a malformed ioctl request with ENOTTY
2026-10-20 17:49:59 +13:00
Dean 4405703fa6 Merge pull request #8186 from xXPBSXx/fix/socket-dontwait-windows-fb
fix(libkernel): honour MSG_DONTWAIT in the socket send and receive calls on Windows
2026-10-20 16:22:35 +13:00
Dean d19c3d6db8 Merge pull request #8188 from xXPBSXx/fix/listen-connected-socket-fb
fix(libkernel): fail listen on a connected socket with EINVAL
2026-10-20 16:22:22 +13:00
Dean 7020db3098 Merge pull request #8156 from xXPBSXx/fix/socket-call-on-non-socket-v4
fix(libkernel): fail a socket call on an open non-socket descriptor with ENOTSOCK
2026-10-20 15:53:25 +13:00
xXPBSXxandClaude Sonnet 5.5 d672e8d54a fix(libkernel): fail listen on a connected socket with EINVAL
listen on a connected stream socket failed with EISCONN, the Winsock error.
FreeBSD 11.4 solisten_proto_check fails a socket that is connected,
connecting or disconnecting with EINVAL. The guest listen now reports EINVAL
for the host's EISCONN, and the connect-listening test checks it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-20 03:48:18 +02:00
xXPBSXxandClaude Sonnet 5.5 45940f27cc fix(libkernel): honour MSG_DONTWAIT in the socket send and receive calls on Windows
send, sendto, sendmsg, recv, recvfrom and recvmsg failed with EOPNOTSUPP for
MSG_DONTWAIT on Windows, where FreeBSD 11.4 sosend_generic and
soreceive_generic make the one call non-blocking and fail with EWOULDBLOCK
when it would wait. Winsock has no per-call flag, so the call now asks WSAPoll
with a zero timeout whether the socket is ready and fails with EAGAIN when it
is not. The Windows branches of the socket tests that expected EOPNOTSUPP now
run the same checks as the other hosts.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-20 03:43:59 +02:00
xXPBSXxandClaude Sonnet 5.5 d5d17788ae fix(libkernel): fail a socket call on an open non-socket descriptor with ENOTSOCK
bind, listen, accept, connect, shutdown, getsockname, getsockopt, setsockopt, send, recv and the other calls that look up a guest socket answered EBADF for a descriptor that is open but not a socket, for example a pipe end. FreeBSD 11.4's getsock_cap fails these with ENOTSOCK and keeps EBADF for a closed or invalid descriptor.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-20 02:44:17 +02:00
xXPBSXxandClaude Sonnet 5.5 cd09badb2b fix(libkernel): fail a short or long socket address like FreeBSD
bind, connect and sendto answered EFAULT for an address length below 2 and EAFNOSUPPORT for an AF_INET address below 16 or an AF_INET6 address below 28 bytes. FreeBSD 11.4's getsockaddr fails a length below 2 with EINVAL and above SOCK_MAXADDRLEN (255) with ENAMETOOLONG, and the protocols compare the length with sizeof(struct sockaddr_in) or sockaddr_in6 and fail with EINVAL.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-20 02:30:55 +02:00
Retr0 04c16c74f9 fix(libkernel): mark both socketpair ends close-on-exec for SOCK_CLOEXEC
socketpair accepted SOCK_CLOEXEC but left both descriptors out of the
close-on-exec set, so F_GETFD read 0 on them. FreeBSD 11's
kern_socketpair opens both files with O_CLOEXEC when the type carries
SOCK_CLOEXEC, as socket already does here.
2026-10-20 02:26:15 +03:00
Adrià 309ab11eaf Merge pull request #7539 from ionut27001/fix/libkernel-inet-ntop-pad
fix(libkernel): match inet_ntop and inet_pton padding and null pointers as measured on a PS5
2026-10-20 00:51:02 +02:00
Dean 6b981a212c Merge pull request #7675 from valenciana-nc/fix/recvmsg-guest-validation
fix(libkernel): fail recvmsg with EFAULT on guest memory it cannot read or write
2026-10-20 09:21:54 +13:00
Hussein Kadhim baa6d4aeee fix(libkernel): fail a malformed ioctl request with ENOTTY before the descriptor is looked at
FreeBSD's sys_ioctl rejects a request with no direction bit, an output request with no length, or a void request whose length is not an int before it reads the argument or resolves the descriptor. ioctl threw on such a request for a file, pipe or directory, failed with EBADF for a closed descriptor and with EFAULT for a socket given no argument.

(cherry picked from commit 501518f321ac5d0b45ce1090af39c680927edf49)
(cherry picked from commit 3eb7edd9cf)
2026-10-19 16:25:42 +01:00
Alexej Szpevák cb9a4c7417 feat(libkernel): support EV_CLEAR on sockets and pipes in kevent on Linux
kevent threw for EV_CLEAR on a socket or the read end of a pipe; Boost.Asio's kqueue reactor, among others, registers every socket that way. On Linux such a knote now watches its host descriptor through an edge watch. It is armed when added, on every later EV_ADD or EV_ENABLE while enabled, and by each host wakeup; when armed it is checked with the level-triggered readiness test and reported with the current data and EV_EOF, or disarmed if no longer ready, as kqueue_scan re-runs the filter; reporting it disarms it until the next edge. Waits block on the watch's epoll descriptor, so a socket that stays readable does not wake them. Windows and macOS still throw. GuestSockets::NativeDescriptor exposes the host socket behind a guest one for the watch.
2026-10-19 15:05:28 +02:00
valenciana-nc f7fd9dbf9f fix(libkernel): check recvmsg guest copies on macOS 2026-10-19 06:33:13 -06:00
valenciana-nc 7ef31e91cf fix(libkernel): check only the guest bytes recvmsg reads and writes 2026-10-19 06:33:13 -06:00
valenciana-nc 370f48be82 fix(libkernel): validate recvmsg guest pointer ranges are committed
Address review: check the msghdr, iovec array, iovec bases and name
are mapped before MessageLength or the receive loop touches them,
pairing the HostWrite guard with committed-range validation. Add
regression tests for forged iovec-array and output pointers.
2026-10-19 06:33:13 -06:00
valenciana-nc fe8782adc3 fix(libkernel): validate recvmsg guest output buffers in the guest arena
recvmsg_nid_postfix wrote the msghdr, every iovec base and the sender
address with only null checks, unlike recv_nid_postfix and the
sceNetRecvfrom sender outputs which all gate on GuestArena::HostWrite.
Validate the message, each iovec base and the name buffer the same way
so a forged pointer fails with EFAULT instead of writing received bytes
to an arbitrary host address.
2026-10-19 06:33:13 -06:00
Adrià 48f436406b Merge pull request #6663 from karimibrahim07/feat/libkernel-ipv6-v6only
feat(libkernel): set and read IPV6_V6ONLY on a guest socket
2026-10-19 10:44:37 +02:00
MarcZX100 00ca965c57 fix(libkernel): reject connects on listening sockets first 2026-10-18 18:25:48 +00:00
MarcZX100 0751dffcb1 fix(libkernel): match FreeBSD port-zero stream connects 2026-10-18 18:23:39 +00:00
Hussein Kadhim f571b37f5a fix(libkernel): limit the port 0 check to connect on an unconnected datagram socket 2026-10-18 18:23:39 +00:00
Hussein Kadhim b10f1310df fix(libkernel): fail connect and sendto to port 0 with EADDRNOTAVAIL 2026-10-18 18:23:38 +00:00
Adrià bb86ca38b8 Merge pull request #5992 from Get-fix/ps4/libkernel-underscore-io
fix(libkernel): export _readv, _writev, _fcntl, _fstat, __sys_socketex
2026-10-18 16:45:19 +02:00
ionut27001andClaude Opus 5.5 213a741eb2 fix(libkernel): match inet_ntop and inet_pton padding and null pointers as measured on a PS5
Measured from a payload process on a PS5 (firmware 12.70), where
__inet_ntop/__inet_pton and the plain inet_ntop/inet_pton exports
resolve to the same functions:
- inet_ntop follows an AF_INET6 result with zero bytes up to size (sizes
  4 to 65536 sampled) and never writes past size; AF_INET writes only the
  text and its NUL.
- An unsupported family returns EAFNOSUPPORT first, even with null
  pointers.
- A null source or text raises SIGSEGV during the call, before the size
  is checked; here it throws instead of returning EFAULT.
- A null destination is reached only after the size check and the text
  parse: ENOSPC for a size that is too small, 0 for text that does not
  parse, and SIGSEGV (a throw here) otherwise.
The test pins the measured inet_pton and inet_ntop rows, including all
inputs the existing assertions use. The text-form debt entry is replaced
by one that records what a payload process measured (the text forms, the
order of the checks and the padding) and what is still assumed: the
sizes that were not sampled, sizes above 65536, and a title process.
inet_ntoa_r calls __inet_ntop, so a null inet_ntoa_r buffer with room
for the text now throws, and a null buffer that is too small sets ENOSPC
instead of EFAULT; the libSceLibcInternal debt entry says so.

AI-assisted: yes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 17:02:40 +03:00
mugurc 6f2a61ade0 fix(libkernel): recognize listening sockets on macOS hosts
macOS does not support getsockopt with SO_ACCEPTCONN; it fails with
ENOPROTOOPT. So on macOS the guest's getsockopt of SO_ACCEPTCONN failed,
connect did not see that a stream socket was listening (an address of
another family failed with EAFNOSUPPORT instead of EOPNOTSUPP), and the
read event of a listening socket reported FIONREAD's 0 instead of the
waiting connection and did not ignore its low watermark.

On macOS the listening state is read from the SO_ACCEPTCONN flag of the
socket options that proc_pidfdinfo reports, and the three places that ask
go through NativeListening. Other hosts keep getsockopt.
2026-10-18 16:13:50 +03:00
Alexej Szpevák b40e0bfb80 fix(libkernel): report the accept queue length for a listening socket's read event
A read event on a listening socket took its data from FIONREAD, which a listening socket refuses on Linux, so kevent and sceKernelAddReadEvent reported 0 connections while saying one was ready. FreeBSD 11.4 gives a listening socket filt_solisten, which reports so_qlen, the completed connections waiting for accept. Ready now checks SO_ACCEPTCONN and on Linux reads a TCP listener's accept backlog from TCP_INFO, where tcp_get_info puts it in tcpi_unacked; with no host call for the count (Windows, macOS, a listener that is not TCP) it reports 1, the lower bound the readiness already implies.
2026-10-18 13:35:15 +02:00
Dean d704b808e3 Merge pull request #6828 from loyalburrito/claude/brave-ride-lz92qi
feat(ssl): run TLS 1.2 client connections over Mbed TLS
2026-10-18 18:58:26 +13:00
Dean Galvin 2edaf44a64 Merge main into claude/brave-ride-lz92qi 2026-10-18 17:20:58 +13:00
Dean Galvin 67e7043332 Merge main into feat/poll-files 2026-10-18 16:04:36 +13:00
HQasperandClaude Opus 5.5 6d4c7c3150 fix(libkernel): select on kqueues as kqueue_poll answers
A kqueue descriptor is readable while one of its events is triggered and
is never writable or exceptional, as FreeBSD's kqueue_poll answers, on
both hosts. On Windows select threw for it, and on Linux its /dev/null
placeholder was reported readable and writable.

A descriptor that stops being a socket, a kqueue or a pipe during the
wait now fails the call with EBADF when it was closed and is selected
again with the rest of the timeout when its number was reused, as
FreeBSD's kern_select looks every descriptor up again in each pass,
instead of throwing. On Windows a host descriptor that is not a pipe
throws when select starts, before waiting.

Depends on #7139.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 00:45:57 +02:00
Alexej Szpevák 74629458d3 feat(libkernel): poll descriptors 0 to 2 with the console model select uses
select takes descriptors 0 to 2 as a console title's terminal with no input: 0 is never readable and 1 and 2 are always writable. poll threw for them unless the host happened to attach a pipe, so the same title could select on its standard streams but not poll them. poll now gives the same answer, with POLLOUT, the one bit ttydev_poll reports for a writable terminal, for 1 and 2 and nothing for 0.
2026-10-18 00:45:56 +02:00
Alexej Szpevák aabd0e77aa fix(libkernel): answer select on files and the random device as FreeBSD's poll routines do
selscan asks a descriptor once, with the flags of every set it is in (POLLRDNORM for the read set, POLLWRNORM for the write set, POLLRDBAND for the exception set, plus POLLHUP and POLLERR), and marks each set whose flags the answer shares. A seeded random device's randomdev_poll narrows a request with POLLRDNORM to that bit and returns any other request whole, so in the read set it is readable and in no other set, and otherwise it is in each write and exception set it was asked in; select reported it readable and writable together and never exceptional. A regular file or directory went to the host poll, which on Windows threw; vop_stdpoll makes it readable and writable and never exceptional, which select now answers in process on every host, as poll does.
2026-10-18 00:45:55 +02:00
Alexej Szpevák d0c338561a feat(libkernel): poll the random device as randomdev_poll does
poll threw for /dev/random and /dev/urandom, which select already treats as ready. FreeBSD 11.4's randomdev_poll, once the device is seeded, narrows a request that has POLLIN or POLLRDNORM to those bits and returns any other request whole, POLLOUT and the band bits included. poll now answers that, and the test that relied on the random device throwing uses a kqueue descriptor, which poll still does not support.
2026-10-18 00:45:54 +02:00
Alexej Szpevák 7e26c1a4ea feat(libkernel): poll regular files and directories as vop_stdpoll does
poll threw for any descriptor that is neither a socket nor a pipe, so a title polling a file it also reads stopped there. FreeBSD 11.4 hands a vnode to VOP_POLL, and both vop_stdpoll and vop_nopoll return POLLIN, POLLRDNORM and POLLOUT of the requested events for every event poll accepts and never POLLPRI, POLLRDBAND or POLLWRBAND. A file, a directory or a file read from app0.zar now answers that without a host call. File::IsVnode tells such a descriptor apart from the host file type without throwing, so a host socket lent by libSceNet still reaches its own path.
2026-10-18 00:45:27 +02:00
Dean dacd2e991c Merge pull request #7151 from Bonjourcestmatt/fix/socket-type-eprototype
fix(libkernel): return EPROTOTYPE for a socket type inet does not have
2026-10-18 09:51:50 +13:00
Dean 64df590f4c Merge pull request #7076 from AlexOfTheForest/fix/socket-write-space
fix(libkernel): report free send space for socket write events on Linux
2026-10-18 09:44:37 +13:00
mattogepetoandClaude Opus 5.5 a6c123ca4a fix(libkernel): return EPROTOTYPE for a socket type inet does not have
socket() returned EPROTONOSUPPORT for every socket type it does not create.
FreeBSD 11.4's socreate returns EPROTOTYPE when no protocol is found, the
protocol is 0 and the type is not 0, and EPROTONOSUPPORT when a protocol was
given. Its inet domain (in_proto.c) has SOCK_RAW entries in every kernel and
a SOCK_SEQPACKET entry only with SCTP, and no SOCK_RDM or other type.

Return EPROTOTYPE for a type the inet domain never has, such as SOCK_RDM,
with protocol 0. SOCK_RAW and SOCK_SEQPACKET keep EPROTONOSUPPORT, since
whether the console creates them is not known. Test the unknown types and
mismatched protocols for AF_INET and AF_INET6, and list the socket types and
families that are not created in the library's technical debt file.

Written with AI assistance (Claude).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-17 16:00:45 +02:00
Alexej Szpevák acad67d946 feat(libkernel): poll descriptors 0 to 2 with the console model select uses
select takes descriptors 0 to 2 as a console title's terminal with no input: 0 is never readable and 1 and 2 are always writable. poll threw for them unless the host happened to attach a pipe, so the same title could select on its standard streams but not poll them. poll now gives the same answer, with POLLOUT, the one bit ttydev_poll reports for a writable terminal, for 1 and 2 and nothing for 0.
2026-10-17 15:21:34 +02:00
Alexej Szpevák 35f7466e50 fix(libkernel): answer select on files and the random device as FreeBSD's poll routines do
selscan asks a descriptor once, with the flags of every set it is in (POLLRDNORM for the read set, POLLWRNORM for the write set, POLLRDBAND for the exception set, plus POLLHUP and POLLERR), and marks each set whose flags the answer shares. A seeded random device's randomdev_poll narrows a request with POLLRDNORM to that bit and returns any other request whole, so in the read set it is readable and in no other set, and otherwise it is in each write and exception set it was asked in; select reported it readable and writable together and never exceptional. A regular file or directory went to the host poll, which on Windows threw; vop_stdpoll makes it readable and writable and never exceptional, which select now answers in process on every host, as poll does.
2026-10-17 15:21:34 +02:00
Alexej Szpevák 03ae66bea4 feat(libkernel): poll the random device as randomdev_poll does
poll threw for /dev/random and /dev/urandom, which select already treats as ready. FreeBSD 11.4's randomdev_poll, once the device is seeded, narrows a request that has POLLIN or POLLRDNORM to those bits and returns any other request whole, POLLOUT and the band bits included. poll now answers that, and the test that relied on the random device throwing uses a kqueue descriptor, which poll still does not support.
2026-10-17 15:18:34 +02:00