libkernel: inet_pton and inet_ntop use the Windows Winsock text rules, which differ from FreeBSD's (leading zeros in an IPv4 tail, ::ffff: and v4-compatible formatting) #3126

Closed
opened 2026-10-10 22:42:38 +00:00 by husseinkad · 3 comments
husseinkad commented 2026-10-10 22:42:38 +00:00 (Migrated from github.com)
Contributor

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


What

The guest inet_pton and inet_ntop (core/libs/prx/libkernel/Socket/src/AddressConversion.cpp) call Winsock's InetPtonA and InetNtopA on Windows, 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) returns 1 where FreeBSD returns 0: no leading zeros are allowed in a dotted-decimal part.
  • inet_ntop prints ::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.

A title that parses or prints addresses and compares the text (or accepts input the console rejects) sees a difference. Linux and macOS hosts already use the same algorithm as FreeBSD (glibc inherited it), so the change that matters is on Windows. sceNetInetPton and sceNetInetNtop in libSceNet/Export.cpp still use the host functions and have the same difference; they are not part of this issue.

Fix

A tested fix is on my fork: husseinkad:fix/libkernel-inet-text-rules (one commit on main; PR #2768, closed by the intake check). Both functions are implemented without the host, written from the text grammar (RFC 4291 section 2.2, RFC 3493) and FreeBSD's observable rules, not copied from BSD, glibc or musl sources (an earlier attempt of mine was a transcription of the ISC-licensed FreeBSD files, which I dropped over provenance): inet_pton splits 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 and ::ffff:a.b.c.d. Return values, EAFNOSUPPORT (47), ENOSPC (28) and the existing NULL handling are unchanged, and the file no longer needs Winsock. A provenance review compared the structure with FreeBSD's files side by side and found none of its distinctive identifiers or structure.

guest_addresses gains the leading-zero cases, the ::ffff: forms, ::2, ::, ::1, 1::, 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. Differential runs against FreeBSD's sources compiled as a reference, comparing return value, errno, every destination byte and guard bytes: more than 3 billion cases and 0 divergences (all strings of length 7 or less for IPv4, of length 6 for IPv6, 153 million strings of length up to 9, 40 million structured and 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, 2^28 IPv4 patterns, buffer sizes 0 to 50), and an undefined-behaviour-trapping build found no trap. The expected behaviour is assumed from FreeBSD 11.4 and unverified on a console; that is recorded in TechnicalDebt. Linux not run (the file is now pure C++ shared with the POSIX hosts).

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5795940424 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3126). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T22:42:38Z. --- ### What The guest `inet_pton` and `inet_ntop` (`core/libs/prx/libkernel/Socket/src/AddressConversion.cpp`) call Winsock's `InetPtonA` and `InetNtopA` on Windows, 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`) returns 1 where FreeBSD returns 0: no leading zeros are allowed in a dotted-decimal part. - `inet_ntop` prints `::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`. A title that parses or prints addresses and compares the text (or accepts input the console rejects) sees a difference. Linux and macOS hosts already use the same algorithm as FreeBSD (glibc inherited it), so the change that matters is on Windows. `sceNetInetPton` and `sceNetInetNtop` in `libSceNet/Export.cpp` still use the host functions and have the same difference; they are not part of this issue. ### Fix A tested fix is on my fork: [`husseinkad:fix/libkernel-inet-text-rules`](https://github.com/husseinkad/AnyPS5/tree/fix/libkernel-inet-text-rules) (one commit on `main`; PR #2768, closed by the intake check). Both functions are implemented without the host, **written from the text grammar (RFC 4291 section 2.2, RFC 3493) and FreeBSD's observable rules, not copied from BSD, glibc or musl sources** (an earlier attempt of mine was a transcription of the ISC-licensed FreeBSD files, which I dropped over provenance): `inet_pton` splits 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` and `::ffff:a.b.c.d`. Return values, `EAFNOSUPPORT` (47), `ENOSPC` (28) and the existing NULL handling are unchanged, and the file no longer needs Winsock. A provenance review compared the structure with FreeBSD's files side by side and found none of its distinctive identifiers or structure. `guest_addresses` gains the leading-zero cases, the `::ffff:` forms, `::2`, `::`, `::1`, `1::`, 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. Differential runs against FreeBSD's sources compiled as a reference, comparing return value, `errno`, every destination byte and guard bytes: more than 3 billion cases and 0 divergences (all strings of length 7 or less for IPv4, of length 6 for IPv6, 153 million strings of length up to 9, 40 million structured and 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, 2^28 IPv4 patterns, buffer sizes 0 to 50), and an undefined-behaviour-trapping build found no trap. The expected behaviour is assumed from FreeBSD 11.4 and unverified on a console; that is recorded in TechnicalDebt. Linux not run (the file is now pure C++ shared with the POSIX hosts). AI-assisted: yes.
MarcZX10 commented 2026-10-11 12:35:05 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T12:35:05Z.


The reviewed fix is now in PR #3530. It carries Hussein Kadhim's original commit with authorship preserved. On current main, the full Linux Release build passed and guest_addresses passed three runs.

Conventions and relinker CI jobs fail before evaluating the PR because checkout cannot resolve the existing 3rdparty/imgui gitlink on main; #3511 is tracking that repository-wide issue. The other applicable PR checks pass. I have not independently rerun the Windows differential suite or tested on console hardware.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6109075959 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3126#issuecomment-6109075959). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T12:35:05Z. --- The reviewed fix is now in [PR #3530](https://github.com/boykopovar/AnyPS5/pull/3530). It carries Hussein Kadhim's original commit with authorship preserved. On current `main`, the full Linux Release build passed and `guest_addresses` passed three runs. Conventions and relinker CI jobs fail before evaluating the PR because checkout cannot resolve the existing `3rdparty/imgui` gitlink on `main`; #3511 is tracking that repository-wide issue. The other applicable PR checks pass. I have not independently rerun the Windows differential suite or tested on console hardware.
husseinkad commented 2026-10-11 12:37:13 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-11T12:37:13Z.


Thank you @MarcZX100 for carrying this into #3530 with the authorship kept, and for the careful CI notes on the 3rdparty/imgui gitlink. For the record, the Windows differential I ran against FreeBSD's sources (more than 3 billion cases, 0 divergences) was on the branch's own commit, so it applies to the carried one if the commit is unchanged.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6109093785 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3126#issuecomment-6109093785). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-11T12:37:13Z. --- Thank you @MarcZX100 for carrying this into #3530 with the authorship kept, and for the careful CI notes on the `3rdparty/imgui` gitlink. For the record, the Windows differential I ran against FreeBSD's sources (more than 3 billion cases, 0 divergences) was on the branch's own commit, so it applies to the carried one if the commit is unchanged. AI-assisted: yes.
MarcZX10 commented 2026-10-11 12:47:13 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T12:47:13Z.


Update: #3511 has merged, removing the orphan gitlink from main. PR #3530 is rebased onto a11c8f7f; the full Linux Release build and guest_addresses (3/3) pass on the rebased head. CI is running again against the fixed base.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6109176138 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3126#issuecomment-6109176138). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T12:47:13Z. --- Update: #3511 has merged, removing the orphan gitlink from `main`. PR #3530 is rebased onto `a11c8f7f`; the full Linux Release build and `guest_addresses` (3/3) pass on the rebased head. CI is running again against the fixed base.
Sign in to join this conversation.
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#3126
No description provided.