fix(libkernel): follow FreeBSD's text rules in inet_pton and inet_ntop on Windows #2768

Closed
husseinkad wants to merge 1 commit from upstream-pr/2768 into main
husseinkad commented 2026-10-10 13:38:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T13:38:52Z.


What

The guest inet_pton and inet_ntop called Winsock's InetPtonA and InetNtopA, whose text rules differ from FreeBSD's lib/libc/inet/inet_pton.c and inet_ntop.c (releng/11.4): inet_pton(AF_INET6, "::01.2.3.4") (and ::1.2.3.04, ::0.0.0.00) returned 1 where FreeBSD returns 0 (no leading zeros in a dotted part), inet_ntop printed ::ffff:0.0.0.1 as ::ffff:0:1, and ::ffff:0:1:0 with a dotted tail (::ffff:0:0.1.0.0) where FreeBSD prints ::ffff:0.0.0.1 and ::ffff:0:1:0.

Both functions are now implemented in AddressConversion.cpp without the host: it is written from the text grammar (RFC 4291 section 2.2, RFC 3493) and FreeBSD's observable rules, not copied from any BSD, glibc or musl source: inet_pton splits the text at :: and parses the two halves as group lists, inet_ntop finds the longest zero run in a separate pass. The rules: AF_INET is four decimal parts of 1 to 3 digits with no leading zero; AF_INET6 is up to eight groups of 1 to 4 hex digits with at most one :: standing for one or more zero groups, a dotted IPv4 tail only as the last 32 bits, no zone id; inet_ntop prints lowercase hex without leading zeros, compresses the first longest run of two or more zero groups, and prints a dotted tail only for ::a.b.c.d (not ::, ::1) and ::ffff:a.b.c.d. Return values, EAFNOSUPPORT (47) for a bad family, ENOSPC (28) for a short buffer and the existing NULL handling are unchanged, and the file no longer needs Winsock. sceNetInetPton and sceNetInetNtop in libSceNet still use the host functions and are noted in TechnicalDebt together with the assumption that this behaviour follows FreeBSD 11.4 and is unverified on a console.

Tested

Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release:

  • guest_addresses gains the leading-zero cases, the ::ffff: and ::2, ::, ::1, 1:: prints, invalid forms such as 1:2:3:4:5:6:7:8:: and a zone id, exact-fit and one-short buffers and the family errors; it fails without the change (exit 0xc0000409) and passes with it. ctest -R "guest_addresses|guest_udp" passes.
  • Outside the repo, differential runs against FreeBSD's inet_pton.c and inet_ntop.c (releng/11.4) compiled as a reference, comparing return value, errno, every destination byte and guard bytes past the buffer: more than 3 billion cases and 0 divergences, including all strings of length 7 or less over 0123456789.xX-+ a for IPv4 and of length 6 over the hex alphabet for IPv6, 153 million strings of length up to 9 over 0f:.1% 9, 20 million structured and 20 million mutated IPv6 forms, every 16-bit value in each group position, all {0, 1, ffff, 0x1234}^8 combinations, 1 billion random 16-byte patterns biased to zero runs, and 2^28 IPv4 patterns, with buffer sizes 0 to 50; a second run with an undefined-behaviour-trapping build of the new file found no trap.
  • tools/check_conventions.py --base origin/main passes; tools/pr_overlap.py --branch reports no overlap.
  • Linux not run (the file is now pure C++, shared with the POSIX hosts).

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4811607447 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2768). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T13:38:52Z. --- ### What The guest `inet_pton` and `inet_ntop` called Winsock's `InetPtonA` and `InetNtopA`, whose text rules differ from FreeBSD's `lib/libc/inet/inet_pton.c` and `inet_ntop.c` (releng/11.4): `inet_pton(AF_INET6, "::01.2.3.4")` (and `::1.2.3.04`, `::0.0.0.00`) returned 1 where FreeBSD returns 0 (no leading zeros in a dotted part), `inet_ntop` printed `::ffff:0.0.0.1` as `::ffff:0:1`, and `::ffff:0:1:0` with a dotted tail (`::ffff:0:0.1.0.0`) where FreeBSD prints `::ffff:0.0.0.1` and `::ffff:0:1:0`. Both functions are now implemented in `AddressConversion.cpp` without the host: it is written from the text grammar (RFC 4291 section 2.2, RFC 3493) and FreeBSD's observable rules, not copied from any BSD, glibc or musl source: `inet_pton` splits the text at `::` and parses the two halves as group lists, `inet_ntop` finds the longest zero run in a separate pass. The rules: `AF_INET` is four decimal parts of 1 to 3 digits with no leading zero; `AF_INET6` is up to eight groups of 1 to 4 hex digits with at most one `::` standing for one or more zero groups, a dotted IPv4 tail only as the last 32 bits, no zone id; `inet_ntop` prints lowercase hex without leading zeros, compresses the first longest run of two or more zero groups, and prints a dotted tail only for `::a.b.c.d` (not `::`, `::1`) and `::ffff:a.b.c.d`. Return values, `EAFNOSUPPORT` (47) for a bad family, `ENOSPC` (28) for a short buffer and the existing NULL handling are unchanged, and the file no longer needs Winsock. `sceNetInetPton` and `sceNetInetNtop` in `libSceNet` still use the host functions and are noted in TechnicalDebt together with the assumption that this behaviour follows FreeBSD 11.4 and is unverified on a console. ### Tested Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release: - `guest_addresses` gains the leading-zero cases, the `::ffff:` and `::2`, `::`, `::1`, `1::` prints, invalid forms such as `1:2:3:4:5:6:7:8::` and a zone id, exact-fit and one-short buffers and the family errors; it fails without the change (exit `0xc0000409`) and passes with it. `ctest -R "guest_addresses|guest_udp"` passes. - Outside the repo, differential runs against FreeBSD's `inet_pton.c` and `inet_ntop.c` (releng/11.4) compiled as a reference, comparing return value, `errno`, every destination byte and guard bytes past the buffer: more than 3 billion cases and 0 divergences, including all strings of length 7 or less over `0123456789.xX-+ a` for IPv4 and of length 6 over the hex alphabet for IPv6, 153 million strings of length up to 9 over `0f:.1% 9`, 20 million structured and 20 million mutated IPv6 forms, every 16-bit value in each group position, all `{0, 1, ffff, 0x1234}^8` combinations, 1 billion random 16-byte patterns biased to zero runs, and 2^28 IPv4 patterns, with buffer sizes 0 to 50; a second run with an undefined-behaviour-trapping build of the new file found no trap. - `tools/check_conventions.py --base origin/main` passes; `tools/pr_overlap.py --branch` reports no overlap. - Linux not run (the file is now pure C++, shared with the POSIX hosts). ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-10 13:39:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T13:39:00Z.


Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6098078293 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2768#issuecomment-6098078293). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T13:39:00Z. --- Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!2768
No description provided.