feat(libSceNetCtl): implement sceNetCtlGetNatInfo #10790

Merged
leorossi2005 merged 6 commits from upstream-pr/10790 into main 2026-10-27 07:38:33 +00:00
leorossi2005 commented 2026-10-25 00:50:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by leorossi2005 on 2026-10-25T00:50:47Z.


What

Declare the NetCtlNatInfo structure layout (size, stun_status, nat_type, mapped_addr) and implement sceNetCtlGetNatInfo by performing RFC 5389 STUN binding discovery over UDP from the active host IPv4 interface against stun.playstation.net and fallback public STUN servers (stun.cloudflare.com, stun.l.google.com, stun1.l.google.com), with a test hook (NetCtlSetStunServers_nid_no_patch) for offline testing:

  • Validates initialization state (SCE_NET_CTL_ERROR_NOT_INITIALIZED), null pointer (SCE_NET_CTL_ERROR_INVALID_ADDR), info->size == 16 (SCE_NET_CTL_ERROR_INVALID_SIZE), and active IPv4 connectivity (SCE_NET_CTL_ERROR_NOT_CONNECTED).
  • Bounds each DiscoverNatInfo call to a 1000 ms total time budget (up to 600 ms for DNS resolution and 300 ms per UDP probe). Caches only successful results (stun_status == 2) for 60 s per local IPv4 address; failures are not cached and re-probe on the next call.
  • Populates NetCtlNatInfo matching retail PS5 call sites (Borderlands 4 PPSA01494, MXGP 26 PPSA30839, Super Meat Boy 3D PPSA31162) and physical PS5 console packet captures (ps5_nat_captures.zip):
    • stun_status = 1, nat_type = 0, mapped_addr = 0 when discovery fails or times out.
    • stun_status = 2, nat_type = 1 when the mapped IPv4 endpoint matches the local socket endpoint.
    • stun_status = 2, nat_type = 2 when the mapped IPv4 endpoint is consistent across two distinct STUN endpoints from the same local socket.
    • stun_status = 2, nat_type = 3 when the mapped port changes across distinct STUN endpoints from the same local socket.

Tested

  • Windows 11, Aliens: Fireteam Elite (PPSA03393), passes sceNetCtlGetNatInfo check and advances into game loading routines.
  • ctest --test-dir build -R guest_net_ctl -V: guest_net_ctl_tests passes 100% (validates pre-init, null pointer, invalid struct size, offline state, silent-server timeout followed by re-probe on responding server, mismatched transaction ID rejection, single-server incomplete NAT failure, NAT type 1, NAT type 2, cache reuse, and NAT type 3 via CHANGED-ADDRESS).
  • Physical PS5 hardware packet captures (ps5_nat_captures.zip) confirming NAT type 2, NAT type 3, and STUN failure (nat_type = 0).
  • Untested on console hardware: NAT type 1 on a physical console with a direct public IPv4 address on the interface (covered by loopback unit tests in guest_net_ctl_tests).

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 under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: PPSA03393
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5000579393 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10790). Originally posted by [leorossi2005](https://github.com/leorossi2005) on 2026-10-25T00:50:47Z. --- ### What Declare the `NetCtlNatInfo` structure layout (`size`, `stun_status`, `nat_type`, `mapped_addr`) and implement `sceNetCtlGetNatInfo` by performing RFC 5389 STUN binding discovery over UDP from the active host IPv4 interface against `stun.playstation.net` and fallback public STUN servers (`stun.cloudflare.com`, `stun.l.google.com`, `stun1.l.google.com`), with a test hook (`NetCtlSetStunServers_nid_no_patch`) for offline testing: - Validates initialization state (`SCE_NET_CTL_ERROR_NOT_INITIALIZED`), null pointer (`SCE_NET_CTL_ERROR_INVALID_ADDR`), `info->size == 16` (`SCE_NET_CTL_ERROR_INVALID_SIZE`), and active IPv4 connectivity (`SCE_NET_CTL_ERROR_NOT_CONNECTED`). - Bounds each `DiscoverNatInfo` call to a 1000 ms total time budget (up to 600 ms for DNS resolution and 300 ms per UDP probe). Caches only successful results (`stun_status == 2`) for 60 s per local IPv4 address; failures are not cached and re-probe on the next call. - Populates `NetCtlNatInfo` matching retail PS5 call sites (*Borderlands 4* `PPSA01494`, *MXGP 26* `PPSA30839`, *Super Meat Boy 3D* `PPSA31162`) and physical PS5 console packet captures (`ps5_nat_captures.zip`): - `stun_status = 1`, `nat_type = 0`, `mapped_addr = 0` when discovery fails or times out. - `stun_status = 2`, `nat_type = 1` when the mapped IPv4 endpoint matches the local socket endpoint. - `stun_status = 2`, `nat_type = 2` when the mapped IPv4 endpoint is consistent across two distinct STUN endpoints from the same local socket. - `stun_status = 2`, `nat_type = 3` when the mapped port changes across distinct STUN endpoints from the same local socket. ### Tested - Windows 11, Aliens: Fireteam Elite (PPSA03393), passes `sceNetCtlGetNatInfo` check and advances into game loading routines. - `ctest --test-dir build -R guest_net_ctl -V`: `guest_net_ctl_tests` passes 100% (validates pre-init, null pointer, invalid struct size, offline state, silent-server timeout followed by re-probe on responding server, mismatched transaction ID rejection, single-server incomplete NAT failure, NAT type 1, NAT type 2, cache reuse, and NAT type 3 via `CHANGED-ADDRESS`). - Physical PS5 hardware packet captures ([ps5_nat_captures.zip](https://github.com/user-attachments/files/33979450/ps5_nat_captures.zip)) confirming NAT type 2, NAT type 3, and STUN failure (`nat_type = 0`). - Untested on console hardware: NAT type 1 on a physical console with a direct public IPv4 address on the interface (covered by loopback unit tests in `guest_net_ctl_tests`). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: PPSA03393 - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-25 00:53:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:53:14Z.

Review: CHANGES_REQUESTED


The changed source and test translation units pass GCC 12.2 syntax checks with -Wall -Wextra -Werror -Wno-unused-parameter; git diff --check and the current-main conventions check also pass. The hosted path-filtered jobs skip the product test, so I could not verify the reported title run or the integrated test locally.

I found a blocking correctness issue in the reported values. FindHostIpv4Interface only establishes that the host has a local IPv4 interface; this implementation then reports STUN success and NAT type 2 without performing STUN or measuring the NAT mapping, and copies that local interface address into mapped_addr. A local interface address does not establish the externally mapped address or NAT type. The test currently compares mapped_addr to the same host-interface address and asserts the two hard-coded status values, so it does not validate those claims. The documented field layout and meanings are consistent with VitaSDK's SceNetCtlNatInfo definition.

Please either implement and test actual NAT discovery/classification, or leave this export unimplemented (with the gap recorded as technical debt) until the values can be reported truthfully. Please also avoid claiming STUN success or a NAT type from interface presence alone.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640833697 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10790#pullrequestreview-5640833697). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T00:53:14Z. **Review:** CHANGES_REQUESTED --- The changed source and test translation units pass GCC 12.2 syntax checks with `-Wall -Wextra -Werror -Wno-unused-parameter`; `git diff --check` and the current-main conventions check also pass. The hosted path-filtered jobs skip the product test, so I could not verify the reported title run or the integrated test locally. I found a blocking correctness issue in the reported values. `FindHostIpv4Interface` only establishes that the host has a local IPv4 interface; this implementation then reports STUN success and NAT type 2 without performing STUN or measuring the NAT mapping, and copies that local interface address into `mapped_addr`. A local interface address does not establish the externally mapped address or NAT type. The test currently compares `mapped_addr` to the same host-interface address and asserts the two hard-coded status values, so it does not validate those claims. The documented field layout and meanings are consistent with [VitaSDK's `SceNetCtlNatInfo` definition](https://docs.vitasdk.org/netctl_8h_source.html#l166). Please either implement and test actual NAT discovery/classification, or leave this export unimplemented (with the gap recorded as technical debt) until the values can be reported truthfully. Please also avoid claiming STUN success or a NAT type from interface presence alone.
MarcZX10 commented 2026-10-25 02:20:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T02:20:40Z.

Review: CHANGES_REQUESTED


The revision fixes the original issue: it now sends real STUN Binding Requests and only reports success after receiving a response. The changed files pass git diff --check, and the hosted conventions, eligibility, labels, and report checks pass. The product build/test jobs are skipped by the repository's PR path filters; I did not run the Windows title test or the integrated guest_net_ctl test locally.

I found a blocking issue in the NAT classification evidence. RFC 5780's mapping procedure requires an initial response with OTHER-ADDRESS, followed by requests to the alternate address at the primary port and, when needed, the alternate address and port. Here, the code compares the initial mapping with a request to the OTHER-ADDRESS endpoint (which uses both the alternate IP and port), or with any other configured STUN service. The tests use two independent loopback servers and assert only equality/difference of their synthetic mappings. This does not establish the mapping behavior described by RFC 5780, nor that those behaviors correspond to PlayStation's nat_type values 1/2/3.

Please either provide a source or hardware evidence for the PS5 nat_type classification and implement/test the corresponding probes, or report only the measured mapped address/STUN result while leaving the NAT type unknown until it can be established. RFC 5780's mapping procedure is in section 4.3; VitaSDK confirms the SceNetCtlNatInfo layout but does not document the field semantics.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641147636 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10790#pullrequestreview-5641147636). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T02:20:40Z. **Review:** CHANGES_REQUESTED --- The revision fixes the original issue: it now sends real STUN Binding Requests and only reports success after receiving a response. The changed files pass `git diff --check`, and the hosted conventions, eligibility, labels, and report checks pass. The product build/test jobs are skipped by the repository's PR path filters; I did not run the Windows title test or the integrated `guest_net_ctl` test locally. I found a blocking issue in the NAT classification evidence. RFC 5780's mapping procedure requires an initial response with `OTHER-ADDRESS`, followed by requests to the alternate address at the primary port and, when needed, the alternate address and port. Here, the code compares the initial mapping with a request to the OTHER-ADDRESS endpoint (which uses both the alternate IP and port), or with any other configured STUN service. The tests use two independent loopback servers and assert only equality/difference of their synthetic mappings. This does not establish the mapping behavior described by RFC 5780, nor that those behaviors correspond to PlayStation's `nat_type` values 1/2/3. Please either provide a source or hardware evidence for the PS5 `nat_type` classification and implement/test the corresponding probes, or report only the measured mapped address/STUN result while leaving the NAT type unknown until it can be established. RFC 5780's mapping procedure is in [section 4.3](https://www.rfc-editor.org/rfc/rfc5780.html#section-4.3); VitaSDK confirms the `SceNetCtlNatInfo` layout but does not document the field semantics.
MarcZX10 commented 2026-10-25 03:00:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T03:00:10Z.

Review: DISMISSED


The latest revision addresses my prior NAT-type concern: it now leaves nat_type unknown and reports only a successful STUN result plus the mapped IPv4 address. I found a separate protocol/security issue in ProbeStunServer: g_stunTransactionCounter starts at 1 and the 96-bit transaction ID is deterministically derived from that counter. The current STUN specification requires a randomly selected 96-bit transaction ID, which is used to correlate responses and contributes to resistance against spoofed responses (RFC 8489, Section 5). Please generate transaction IDs with a suitable OS cryptographic random source and add a test that confirms the request uses a fresh transaction ID and ignores a response with a mismatched ID. Hosted conventions, eligibility, labels and report pass, while product test jobs are skipped; I did not independently run guest_net_ctl on this head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641258682 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10790#pullrequestreview-5641258682). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T03:00:10Z. **Review:** DISMISSED --- The latest revision addresses my prior NAT-type concern: it now leaves `nat_type` unknown and reports only a successful STUN result plus the mapped IPv4 address. I found a separate protocol/security issue in `ProbeStunServer`: `g_stunTransactionCounter` starts at 1 and the 96-bit transaction ID is deterministically derived from that counter. The current STUN specification requires a randomly selected 96-bit transaction ID, which is used to correlate responses and contributes to resistance against spoofed responses ([RFC 8489, Section 5](https://www.rfc-editor.org/rfc/rfc8489.html#section-5)). Please generate transaction IDs with a suitable OS cryptographic random source and add a test that confirms the request uses a fresh transaction ID and ignores a response with a mismatched ID. Hosted conventions, eligibility, labels and report pass, while product test jobs are skipped; I did not independently run `guest_net_ctl` on this head.
adriadam10 commented 2026-10-26 22:32:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-26T22:32:16Z.

Review: CHANGES_REQUESTED


Thanks for the rework; the layout, the status/NAT values and the STUN parsing look right to me, and the transaction-ID and mismatch tests pin real behaviour. Two things need changing before merge:

  • core/libs/prx/libSceNetCtl/Export.cpp:918-931 caches every result, failures included, keyed only on the local address and the config generation. One timeout (Wi-Fi not settled yet, a dropped UDP packet, DNS not ready) makes stun_status = 1 / nat_type = 0 permanent for the whole process, and a router change that doesn't alter the host IPv4 address is never seen. Please cache only successful results and give them a bounded lifetime (or re-probe on failure), and add a test: a first call against a silent server, then a second call against a responding one, must report the second result.
  • Export.cpp:574-587, :735 and :776: the call does a blocking getaddrinfo for four hosts and then probes the servers one after another with 300 ms each, plus the secondaries, all on the guest thread with no overall limit. Please put a total time budget on DiscoverNatInfo (resolution included) so the worst case is bounded and stated in docs/dev/debt/libSceNetCtl.md.

Smaller points:

  • The default list contacts Cloudflare and Google STUN servers from a guest call, while your own captures show the console only talks to *.np.stun.playstation.net. That is acceptable as a documented deviation, but the debt line should say that these third-party hosts receive the host's IP on the first call.
  • The debt line says NAT type 1 is not measured on a console; fine, but then it also belongs in the PR's Tested section as untested rather than listed with the covered cases.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5659996600 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10790#pullrequestreview-5659996600). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-26T22:32:16Z. **Review:** CHANGES_REQUESTED --- Thanks for the rework; the layout, the status/NAT values and the STUN parsing look right to me, and the transaction-ID and mismatch tests pin real behaviour. Two things need changing before merge: - `core/libs/prx/libSceNetCtl/Export.cpp:918-931` caches every result, failures included, keyed only on the local address and the config generation. One timeout (Wi-Fi not settled yet, a dropped UDP packet, DNS not ready) makes `stun_status = 1` / `nat_type = 0` permanent for the whole process, and a router change that doesn't alter the host IPv4 address is never seen. Please cache only successful results and give them a bounded lifetime (or re-probe on failure), and add a test: a first call against a silent server, then a second call against a responding one, must report the second result. - `Export.cpp:574-587`, `:735` and `:776`: the call does a blocking `getaddrinfo` for four hosts and then probes the servers one after another with 300 ms each, plus the secondaries, all on the guest thread with no overall limit. Please put a total time budget on `DiscoverNatInfo` (resolution included) so the worst case is bounded and stated in `docs/dev/debt/libSceNetCtl.md`. Smaller points: - The default list contacts Cloudflare and Google STUN servers from a guest call, while your own captures show the console only talks to `*.np.stun.playstation.net`. That is acceptable as a documented deviation, but the debt line should say that these third-party hosts receive the host's IP on the first call. - The debt line says NAT type 1 is not measured on a console; fine, but then it also belongs in the PR's `Tested` section as untested rather than listed with the covered cases.
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!10790
No description provided.