Skip to content

Add UCI network command OPEN_UDP_LOCAL ($0A) to open UDP from a fixed local port - #1016

Open
JC-000 wants to merge 5 commits into
GideonZ:masterfrom
JC-000:uci-open-udp-local
Open

JC-000 wants to merge 5 commits into
GideonZ:masterfrom
JC-000:uci-open-udp-local

Conversation

@JC-000

@JC-000 JC-000 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

For #962. Ready for review. @barryw has reviewed the UCI side, and his and Christian's points are addressed below. Questions Q5–Q7 and approval of the protocol are @GideonZ's decision; the design analysis below sets them out.

What it does

NET_CMD_OPEN_UDP_LOCAL ($0A) is OPEN_UDP with a local port you choose. The socket is bound to that port before it is connected, so a program can be reached at a known UDP port (a game server that peers connect back to, UDP hole punching after a STUN lookup).

$03 $0A <REMOTE_PORT_LSB> <REMOTE_PORT_MSB> <LOCAL_PORT_LSB> <LOCAL_PORT_MSB> <HOST> [$00]
  • Ports are little endian. Local port 0 means any free port (49152-65535), as OPEN_UDP does today.
  • <HOST> is a dotted IPv4 address or a name, 1 to 253 bytes, with at most one trailing $00 and nothing after it.
  • Reply data is 3 bytes: the socket handle, then the bound local port (LSB, MSB). The handle is byte 0, so a client that reads one byte after an open works unchanged. READ, WRITE, $16 (Spike for #807: NET_CMD_WRITE_SOCKET_CHUNK ($16), a payload larger than one command as one datagram #835) and CLOSE work on the handle as on any OPEN_UDP socket.
  • The socket is connected to the remote, like OPEN_UDP: it only receives datagrams from that address and port. Opened to 0.0.0.0, it receives from any address whose source port is the remote port, and a WRITE on it answers 12,SEND ERROR: <errno> (EDESTADDRREQ) and sends nothing (Q7 in the design analysis).
  • Order of checks: no-reply bit or malformed command (81), remote port 0 (82), a reserved local port (64, 68 or 53248: 89,PORT IN USE, Q3), name resolution (84), socket() (85), bind() (89,PORT IN USE on EADDRINUSE, else 85), connect() (the existing 11,ERROR ON CONNECT), read back the bound port (86 if that fails). The host is resolved before the bind, so a bad host never leaves a port bound.
  • New status 89,PORT IN USE. Status numbers are per target; 89 already means CONFIG FILE HAD ERRORS in the control target and NOT A DISK IMAGE in the DOS target. 89 is the only free number in 81-97 of the network target (Q2).
  • Support probe: remote port 0 is always refused with 82. A program can send $03 $0A $00 $00 $00 $00 "0" (any host of 1-253 bytes): new firmware answers 82,PARAMETER(S) OUT OF RANGE and opens nothing, older firmware answers 21,UNKNOWN COMMAND. An empty host answers 81, because the command format is checked first.
  • The no-reply bit ($80 on the target byte) is refused with 81: a handle nobody reads can never be closed, and a leaked fixed port would stay bound. Same rule as TLS client sockets for the U64, through its ESP32 #997's $21.
  • No SO_REUSEADDR. A port held by anything on the UDP list, including firmware services (ident on 64 while enabled, DHCP on 68 while the device uses DHCP), answers 89; this is by reading udp_bind, I did not open 64 or 68. TCP sockets are on a separate list and never clash. A C64 reset closes the socket and frees the port; CLOSE frees it at once.

OPEN_TCP and OPEN_UDP are unchanged: their call sites are untouched and open_socket gets two defaulted parameters.

Change

Tests

Added to net_target_test.py (profile DEEP, same helpers and both routes as the existing scenarios):

Scenario Checks
udp-local-fixed-port-echo a datagram sent to the device's fixed port is read; a WRITE leaves from exactly that port; the reply carries the port
udp-local-port-zero ephemeral port >= 49152, equal to the source port the host sees
udp-local-port-in-use a second open and a plain OPEN_UDP source port answer 89 with no data; the owner still works; reopen after close; 20 refusals leak no descriptor
udp-local-tcp-port-not-a-clash a TCP socket's port is free for UDP
udp-local-bad-params length 6, empty host, bytes after the trailing NUL, 254-byte host (81); remote port 0 (82); empty host with remote 0 (81, format first); no trailing NUL and a 253-byte host open
udp-local-bad-host-keeps-port-free an unresolvable host answers 84 and leaves the port free
udp-local-support-probe remote port 0 answers 82, not 21
udp-local-pcb-exhaustion with an IPv4 literal, running out of UDP sockets answers 85, never 89 or a hang
udp-local-reset-frees-port a C64 reset frees the port
udp-local-any-host-is-receive-only opened to 0.0.0.0 with the remote port of a host socket: a datagram from that port is read; a WRITE answers 12,SEND ERROR: 121 (EDESTADDRREQ) with FF FF, and the host sees no datagram within 3 s
udp-local-service-port-reserved with the ident service switched off (restored at the end), local ports 64 (ident) and 53248 (VIC stream palette) answer 89,PORT IN USE with no data; 68 is not tried, as DHCP holds it

Lossy datagram probes are retried as the file already does (PROBE_ATTEMPTS). Three scenarios (fixed-port-echo, port-zero, port-in-use) compare the device's port with the source port the host sees, so host and device must be on the same LAN with no NAT between them.

Hardware evidence

Current runs: red/green of the refusal and of the service-port reservation, on both devices. Images of master 99784768a (with #1018's route hook) plus #835 (full-size UDP, an open PR the bench images carry) plus this PR. RED is this PR's head before the refusal (43ef32cb5); GREEN is RED plus the refusal commit (b2ed34e0d, cherry-picked); the two differ only by that commit. Devices, both core 1.50: an Ultimate 64 Elite with master's ESP32 module, and a C64 Ultimate with a newer development build of its control module that was already on it (the updater kept it on the RED flash and, by the same version rule, on GREEN; not read back). Suites: uci-net-target over Ethernet, and the ten udp-local-* scenarios over WiFi, each on the rest and native routes.

Product Image Result
Ultimate 64 Elite RED only udp-local-any-host-is-receive-only fails, on Ethernet and WiFi, both routes: its WRITE answers 00,OK instead of 12,SEND ERROR: 121; everything else passes
Ultimate 64 Elite GREEN all pass: uci-net-target in one attempt, the ten udp-local-* over WiFi on both routes
C64 Ultimate RED the same single failure (two clean attempts; a first attempt overlapped a menu session on the device and is not counted)
C64 Ultimate GREEN all pass: uci-net-target in one attempt, the ten udp-local-* over WiFi on both routes

On GREEN the new scenario gets 12,SEND ERROR: 121 and the host receives no datagram within 3 s (the scenario's own check; no packet capture). This also covers the re-run of the udp-local-* scenarios on current master with #1018, on both interfaces.

Service-port reservation (Q3). RED is the GREEN image above, without the reservation; GREEN2 is it plus the reservation commit (269fecc7d, cherry-picked), the only difference. Same devices and modules.

Product Image Result
Ultimate 64 Elite RED udp-local-service-port-reserved fails on both routes: local port 64 with ident off answers 00,OK (the scenario stops at the first port)
Ultimate 64 Elite GREEN2 all pass: uci-net-target over Ethernet in one attempt, the eleven udp-local-* over WiFi on both routes; 64 and 53248 answer 89 with no data
C64 Ultimate RED the same failure
C64 Ultimate GREEN2 all pass, as on the Ultimate 64 Elite

Port 68 is not tried on a device: DHCP already holds it, so the in-use check answers 89 there anyway. The reservation covers it by code.

Earlier run, before the refusal and before #1018: exact firmware on a C64 Ultimate. The same test file and host throughout (this Mac, macOS 27, Python 3.13, wired on the device's subnet with no NAT). RED is the official C64 Ultimate firmware 1.1.0 (FPGA 122, core 1.49): all 18 udp-local-* scenario runs (nine scenarios x rest and native) fail, every first $0A answering 21,UNKNOWN COMMAND. GREEN is an image of master plus this PR plus #835, 80b7b5a8 (firmware 3.15a, FPGA 126, core 1.50, u64ctrl module 1.14 from master): the nine udp-local-* scenarios and the ten chunked-write-* scenarios of #835, both routes, 38 scenario runs, 129 checks, all OK. The GREEN run used the test file from the branch that merges #835; the udp-local-* scenarios in it are the ones in this PR. I did not run the standard profile.

Product Firmware Result
C64 Ultimate official 1.1.0 RED: 18/18 FAIL, 21
C64 Ultimate master + this PR + #835, 80b7b5a8 GREEN: 129 checks OK (19 scenarios x 2 routes)

Secondary evidence: bench images. Earlier runs of only the nine new scenarios (18 rows each), same host, on bench builds:

Product Firmware Result
C64 Ultimate bench 42d8134e (no $0A) RED: 18/18 FAIL, 21
C64 Ultimate bench b0b1978a (42d8134e + this change) GREEN: 18/18 OK, 65 checks
Ultimate 64 Elite bench f8e673df (no $0A) RED: 18/18 FAIL, 21
Ultimate 64 Elite bench 68fe10cf GREEN: 18/18 OK, 65 checks; the whole file also passed here (305 checks, 1011 s)

Disclosure for the bench rows: those images carry other open work besides this change: #835 and #953 on both, the WiFi modules on ESP-IDF 6 (#996, and a C64 Ultimate control-module port that is not proposed yet), and on the U64E also #997 and two bench-only merge fixes. This change was cherry-picked into them (b0b1978a, e084ce00) with patch lines identical to c6e1f854. The new scenarios use only $0A, OPEN_TCP/OPEN_UDP, READ, WRITE and CLOSE.

Not run on hardware: U2/U2+/U2+L and the plain U64 (the rig has a U64E and a C64U). The code is the same source for every target.

Targets built

Six buildable targets use this file. I built u64 (Nios II) and u64ii (RISC-V): no warning from network_target.cc, U64 1229.1 KiB of 1472.0 KiB (master 1228.7). CI builds the rest; the tightest, U2, has 34.4 KiB free at master. MicroBlaze (u2) is unbuildable at master since 7ea644d deleted software/lwip-1.4.1, which its makefile still builds from; the code does not depend on byte order anyway (the port is built with shifts and ntohs/PP_NTOHS).

Device-free suites on 43ef32cb: macOS (Python 3.13) and Kubuntu 24.04 (Python 3.12.3). On both, lint passes and registry fails identically at cc4af99e (directory_rename_test.py unregistered). On macOS the observability suite also fails identically at cc4af99e, with Homebrew ffmpeg 9.0.2 (-vsync removed). On Kubuntu 15 suites pass, including observability; the two manual suites web-theme and web-index fail because the image has no browser.

Design analysis

The need behind #962: two C64 Ultimates run the same lockstep game and exchange joystick inputs every frame (50/60 Hz, 17-20 ms), on one LAN, ideally also across the internet, and find each other without typing an IP. Line numbers are at master 99784768; lwIP is the software/lwip submodule.

The same need is being met in parallel on other 8-bit platforms. On the NES, nesp32 maps controller input to packets: patched two-player builds of Contra, Dr. Mario, Tetris and TMNT II exchange controller input over UDP, meet through a rendezvous, learn their public address from Cloudflare's (stun.cloudflare.com, 3478/udp, "free and unlimited") and Google's STUN servers, and punch through both routers (MULTIPLAYER.md). That is the setup in your 10-05 comment, each machine running its own game and exchanging inputs, and the hole punching $0A makes possible here: a fixed local port, so the address STUN reports is the one the peer can reach.

Could it be done without a firmware change?

What the firmware offers today: the UCI opens ($07, $08) do socket() and connect() with no bind() and reply with the handle only (network_target.cc:184-251); there is no listen, accept or recvfrom. A connected UDP socket accepts only datagrams from its remote's address and port (lwIP src/core/udp.c:289-291). Every UCI socket waits up to 40 ms on an empty READ (network_target.cc:247-250), and one task runs every UCI command (command_intf.cc:59,167), so that wait holds up the next WRITE too. Outside the UCI, the modem emulation listens on TCP (modem.cc:71, :891-892), and the DMA service and REST can write another machine's memory. Every UCI route needs the Command Interface, which is disabled by default (c64.cc:115).

Options with no firmware change
Option Works today for Limits
Relay server, both C64s on $08 (Michel ships one: mfidder2026/Commodore64_Gameserver) LAN and internet, any firmware, any NAT; the lobby does discovery someone runs a server; two hops per input; his server is LAN-only (no encryption or accounts)
Direct TCP through the modem emulation: the host maps the ACIA, the guest dials ATD host:3000 through its own ACIA, or opens $07 to it LAN, any firmware, no server; with ATD on both sides the game needs only a SwiftLink driver and no Command Interface; received bytes land in the ACIA's receive buffer (modem.cc:204-221), so a driver in IRQ or NMI mode has no READ to wait on one required setting (the ACIA mapping); the relay forwards what the C64 sent only after its recv() times out, so up to the Loop Delay (20 ms default, 10 ms minimum, modem.cc:89, :159-165, :186-262) on the sending side; the guest gets the connect banner on every connection, unless the Connect Text setting points to an existing empty file (modem.cc:287-289, :845-857); TCP holds back later inputs behind a lost segment; one connection at a time, so two players (modem.cc:280-281); internet only with a port forward
Direct UDP with $08 nothing neither side learns its own port, and READ doesn't report a sender, so neither can address the other. Ports come from one counter (lwIP udp.c:100-119), so a third party could help a program guess, but any other ephemeral open in between (a streamer, SNTP, another UCI socket) breaks the guess
Write inputs into the peer's RAM through the DMA service (TCP 64, enabled by default, network_config.cc:22) or REST writemem LAN, any firmware, no setting on default configuration; the receiver needs no READ (the data is in RAM) every peer gets arbitrary RAM write; the game depends on the service staying on and on a shared password if one is set (socket_dma.cc:109-116, :476-479); each write stops the peer's CPU for the transfer (c64_subsys.cc:589-591; not measured); the receiver can only poll its RAM
One machine runs the game; the remote player watches the video stream and sends input over REST (your #962 model) any existing game the remote player needs a PC or phone; the input round trip rules out most action games outside one LAN

Modem-route figures are from the code, not run.

So the relay works everywhere, and the modem emulation already gives a direct LAN link between two C64 Ultimates with one setting. The modem route's real costs are TCP's stall on loss, up to one Loop Delay on each send, two players at most, and a port forward for the internet.

Alternatives that need a change

Options with a firmware or setup change
Option For Against
UDP from a fixed local port ($0A, this PR) direct UDP with no server; same READ/WRITE/CLOSE as $08; hole punching after a STUN lookup behind endpoint-independent NATs; Michel's test program (C64U-H2H) and his games' head-to-head mode use it (Commodore64_Gameserver README) new firmware (old firmware answers 21, so a game can fall back); symmetric NATs still need a relay; the 40 ms READ wait applies, as on every UCI socket
Local port after the host's $00 on $08 no new number old firmware ignores bytes after the $00 (network_target.cc:196, :202) and opens and connects a socket on an ephemeral port before the program can see, from the 1-byte reply, that the port was not taken; and any existing program that sends bytes there would change behaviour on new firmware. Ruled out
TCP listener at $12-$15 (numbers already in xlar54's and barryw's libraries) reliable stream accept and a second handle make it larger; same stall on loss as the modem route; internet only with a port forward
Unconnected UDP (RECVFROM/SENDTO) symmetric NATs, one socket for STUN and several peers four or five commands for cases no program has yet
Give the UCI the modem listener, or its settings reuses a listener couples two subsystems and inherits the modem's polling; the TCP listener is the clean form
Netplay in firmware (firmware relays or runs lockstep) tiny C64 code puts each game's protocol in firmware
Other network hardware: WiC64 on the user port, or RR-Net Michel's framework already supports both (through his server, or RR-Net between two VICEs) a second network device for a machine that has one built in
A VPN between the two homes makes the internet case look like a LAN still needs a port the peer can reach, so it needs one of the above

Against your criteria, for the realistic choices:

Criterion (#962) Relay ($08) Modem emulation $0A TCP listener
Consistency with the architecture existing UCI API modem API used as a game link beside $07/$08, same socket model numbers already in client libraries
Intuitiveness a server address to enter AT commands, banner open with two ports, as Michel asked listen/accept
Modularity no firmware code no firmware code; the game link depends on the modem emulation one command and one status, in network_target.cc only four commands, a second handle
Maintainability the server is maintained outside the firmware nothing new to maintain one more open beside $07/$08 listen and accept state, a second handle
Simplicity (no overengineering) a server to run one setting, a SwiftLink driver +91/-7 larger
Performance, receive READ waits up to 40 ms when empty ACIA receive buffer, no wait (IRQ/NMI driver) READ waits up to 40 ms when empty READ waits up to 40 ms when empty
Performance, send two hops up to one Loop Delay (10-20 ms) one hop one hop
Testability needs a server beside the host peer needs the ACIA set up and a SwiftLink test program host peer on the LAN; 11 udp-local-* scenarios on both routes, run red/green on a U64 Elite and a C64 Ultimate host peer on the LAN
Also weighed:
Required setting Command Interface ACIA mapping Command Interface Command Interface
Loss the game detects loss and resends (C64 code) TCP stall the game detects loss and resends (C64 code) TCP stall
Old firmware works works 21: fall back 21: fall back
Internet any NAT port forward hole punching with a rendezvous server; relay for symmetric NATs port forward
Exposure none the listener is there whenever its port is set a port bound while the program holds it, accepting only the peer's address and port (only the port, when opened to 0.0.0.0 for discovery, Q7) a listening port while held

Answer: keep this PR at its current size. It is the smallest change that gives a UCI program a port its peer can reach, and it keeps the socket model games already use with a relay, so one game can do both. The modem emulation is a real alternative on a LAN, and it beats every UCI route on receive today, because the UCI's 40 ms READ wait hits $0A as hard as $08. That wait is a separate problem with its own follow-up in #962 (a READ that doesn't wait), not a reason to prefer TCP's stalls and the modem's send delay. $0A is not the whole internet answer: a rendezvous server is still needed, and symmetric NATs still need a relay.

The PR's own questions

Q1. Command number. Agreed by @barryw. $0A, next to the other opens. The alternative "a byte after the host's $00 on $08" is ruled out above; another free number gains nothing; one generic OPEN with a type byte mixes TCP's TIME_WAIT rebind rules into UDP. $0A keeps $0B free for an OPEN_TCP_LOCAL with the same layout. None of xlar54's, barryw's or Michel's libraries use $0A-$0F for anything else.

Q2. Port in use. Agreed by @barryw (status numbers are per target). 89,PORT IN USE. Alternatives: 10,ERROR ON BIND: <errno> (1x codes carry an errno, like 11 and 12, but the program would parse the errno for the one case it acts on); reusing 85 (can't be told apart from running out of sockets). 87 and 88 are taken by #953. Status numbers are already per target (87 and 88 differ between the control and DOS targets), so 89 meaning something else elsewhere only affects a library that maps numbers to text without the target.

Q3. Service ports. $0A refuses the three UDP ports the firmware binds itself, with 89,PORT IN USE and no data, on every product, whether that service is on or not: 64 (ident), 68 (DHCP client) and 53248 (the VIC stream's palette, U64 family). @barryw pointed out that an in-use check protects only a service that is already bound, and each of these can bind later: ident when switched on (socket_dma.cc:537-563), DHCP when a link comes up or DHCP is switched on (network_interface.cc:354-360, :426-438), the palette when a VIC stream with palette starts (data_streamer.cc:358-383). If a program held the port then, ident and the palette retry and recover once it is free. The DHCP client does not: lwIP ignores the failed bind (dhcp.c:288), connect then gives it a random port (udp.c:1084-1085), and it gets no lease until every DHCP interface goes down (from the code, not run). Every other port stays open, below 1024 too. TCP services can't collide: lwIP keeps UDP and TCP ports apart. udp-local-service-port-reserved switches ident off, checks that 64 and 53248 answer 89 with no data, and restores the setting. RED without the reservation (64 answers 00,OK) and GREEN with it, on a U64 Elite and a C64 Ultimate. Port 68 isn't tried on a device, because DHCP already holds it there and the in-use check answers 89 anyway; the reservation covers it by code.

Q4. Refusals. Agreed by @barryw. Remote port 0 → 82: a connected socket with remote port 0 matches only a sender using source port 0 (udp.c:289), and the refusal makes the support probe side-effect free. No-reply bit → 81: the reply is dropped (command_intf.cc:170-171), so the handle would never be seen and the port would stay bound until reset. Bytes after host $00 → 81: refusing keeps that space for a detectable option later, which $07/$08 lost by ignoring them.

Q5. MEMP_NUM_UDP_PCB = 8 (software/network/config/lwipopts.h:197). No change in this PR. A game needs 2 sockets (rendezvous and peer; the STUN lookup goes out from the peer's local port before that socket is opened to the peer; Michel's H2H holds 2 while searching). Counted from the code, the same lwipopts.h for the U64/U64E (Nios) and the Elite II/C64U (RISC-V):

UDP pcbs the firmware holds
User pcbs When
DHCP 1 one for all interfaces, Ethernet and WiFi (lwIP src/core/ipv4/dhcp.c:234-235, :275-293)
SNTP 1 on by default (network_config.cc:29, sntp_time.cc:21-31)
Ident, UDP 64 1 on by default (network_config.cc:21, socket_dma.cc:543)
Syslog 1 only when a server is set; empty by default (network_config.cc:26, syslog.cc:112)
VIC palette 1 only while the VIC stream runs with palette=1 (data_streamer.cc:358-388, :402-411)
VIC, audio and debug streams 0 the FPGA sends them, through the wired port only (data_streamer.cc:144); starting one opens a socket for an ARP probe and closes it (:325-356)
DNS 0 held one per lookup in flight, freed after it, up to 4, DNS_TABLE_SIZE (lwipopts.h:547; dns.c:146-152, :884-907, :1000-1013); with the pool empty a lookup shares another lookup's pcb if one is in flight, and otherwise fails (:919-948)
mDNS 0 mdns_resp_init() is never called, so its pcb is never made
WiFi 0 the ESP32 passes frames; the WiFi interface runs on the same lwIP, and DHCP shares its one pcb

On defaults that leaves 5, which matches the 5 a program could open on both test devices (measured, streams off). A DNS lookup in flight (SNTP's, or a stream started by name) or a stream's ARP probe takes one more for a moment, so a program could see 85 on its second open only if two of those moments overlap while every optional user is on. A pcb is 40 bytes on these 32-bit, IPv4-only builds (lwIP src/include/lwip/udp.h:81-113), so 16 would cost 320 bytes. Sockets stay capped by MEMP_NUM_NETCONN = 16 (lwipopts.h:255) after a raise. If you want that margin, I'll send it as its own one-line change.

With every optional user on (VIC palette, syslog), 3 are left (from the code), so 2 fit with one to spare. Measured on the C64U with the VIC, audio and debug streams running: still 5, the sixth open answers 85, so the streams hold none. The palette and syslog cases are counted from the code, not measured.

Q6. Wired and WiFi both up. Follow the route, as $07/$08 do; no change in this PR. $0A binds INADDR_ANY, so #1018's route hook applies (network_interface.cc:511-514, route_select.h): a peer on a local subnet goes by the wired interface first. For a peer on the internet the hook makes no choice and lwIP uses its default interface, the first one that is up with an address (network_interface.cc:221-233); interface 0 is the wired port (data_streamer.cc:144), so with both up an internet peer goes wired too. Same LAN or not, the wired link almost always gives lower and steadier latency than WiFi, which is why the route matters for games and why wired first is the right default. Pinning would belong to all opens at once (the disabled NET_CMD_SET_INTERFACE, network_target.cc:67-79).

Q7. Discovery: $0A to 0.0.0.0. Supported, receive-only. Michel's C64U-H2H finds the other C64 by broadcasting a HELLO from one $0A socket and listening on another opened to 0.0.0.0; neither side writes to its 0.0.0.0 socket (#962, src/main.c:472-580). What this means:

  • Open to 0.0.0.0 (or a host that resolves to it) with a remote port: the socket accepts a datagram from any address whose source port is that remote port, and drops the rest (lwIP src/core/udp.c:289-291). Remote port 0 is still refused (Q4), so there is no "any port" form.
  • READ works as on any $0A socket, and returns subnet broadcasts too: lwIP's per-socket broadcast filters are off on every product (IP_SOF_BROADCAST and IP_SOF_BROADCAST_RECV = 0, software/network/config/lwipopts.h:401, :407).
  • WRITE: this PR refuses it with 12,SEND ERROR: <errno> (EDESTADDRREQ), the existing WRITE-failure format, and sends nothing. Refusing at open instead would break the receive-only use. Without the refusal lwIP sends it, as $08 does today (observed on a C64U: an $08 WRITE to 0.0.0.0 returned 00,OK and left as an Ethernet broadcast with IP destination 0.0.0.0, which a Mac on the LAN delivered to its socket): udp_send() goes to the default interface (ip4.c:204-227), and etharp_output() treats 0.0.0.0 as a broadcast (ip4_addr.c:63-66, etharp.c:807-809), so the datagram leaves as an Ethernet broadcast with IP destination 0.0.0.0. RFC 1122 3.2.1.3(a): 0.0.0.0 "MUST NOT be sent, except as a source address"; 3.3.6 tolerates it only as an old BSD broadcast form, off by default. Fixing $08 is a separate change, and a fix must weigh that other lwIP stacks accept destination 0.0.0.0 as a broadcast, so an $08 program may rely on it. To announce, a program opens a second $0A to the subnet broadcast address, as Michel's host does.
  • Not the BSD meaning: on BSD and Linux, connect() to 0.0.0.0 means this host (FreeBSD sys/netinet/in_pcb.c: the primary local address; Linux net/ipv4/route.c: loopback). Here it means "any sender", from lwIP. The documentation will say so.

Still open: Q5, Q6 and Q7 (0.0.0.0 as receive-only), and approval of the protocol, from @GideonZ. Q1, Q2 and Q4 are agreed by @barryw for the UCI side, and Q3 is answered by the reserved ports (@barryw: say if this covers the race you meant).

Before it leaves draft: @GideonZ's decisions above, and the docs#37 update (the reserved ports, Q7's meaning, and the relay and modem routes as the options on older firmware).

Follow-ups (OPEN_TCP_LOCAL $0B, the TCP listener at $12-$15, unconnected UDP, a READ that doesn't wait) are discussed in #962 and not part of this PR. The nonblocking READ comes after #1016, with its own comparison of alternatives. Nothing that depends on this PR will be opened before it is merged.

Like OPEN_UDP, $0A resolves a host name with gethostbyname_r on the UCI Server task (software/io/network/network_target.cc:252), so a slow DNS lookup holds the UCI until it finishes. This PR does not change that. Moving network waits off the UCI Server task is the separate problem raised in #1043.

The same socket model is the base for encryption later. #997 (draft, U64 and U64 Elite) adds TLS client sockets: $21 opens one and returns an ordinary handle, the ESP32 module runs the handshake and the crypto, and READ, WRITE and CLOSE work on it unchanged (plan in #952, issue #995). #997 defines $21-$25; its own open is $21 OPEN_TLS. TLS from a fixed local port would be a future $0B (OPEN_TCP_LOCAL) followed by #997's $24 START_TLS, which upgrades a connected TCP socket. DTLS (encrypted UDP) on $0A-style sockets is planned, not built, and has no command number yet. Neither is part of this PR or promised by it.

AI use

Code, tests and docs were written with Claude Sonnet 5.5 in Claude Code, including the 0.0.0.0 WRITE refusal and the service-port reservation commits. The design analysis was written with Claude Opus 5.5 in Claude Code. Separate Claude Opus 5.5 sessions reviewed the spec, the code and tests, and this PR before it was opened, and their findings are fixed in it. I have read and can explain every line.

cc @barryw (UCI) @chrisgleissner (tests/) @GideonZ

🤖 Generated with Claude Code

JC-000 and others added 2 commits October 20, 2026 06:16
… port

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@chrisgleissner chrisgleissner added enhancement New functionality or an improvement to existing behavior, including formats and developer tooling. uci C64-side Ultimate Command Interface, command targets, and Ultimate DOS commands; REST is network. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. labels Oct 21, 2026
@enver-haase

Copy link
Copy Markdown
Contributor

@JC-000, thank you for including me, and for building a working prototype instead of just asking. I owe you a clarification, though: my network work (the Ethernet drivers) is finished, and I should have taken myself off the contacts list for software/io/network sooner. #1017 does that now, so please don't wait for me here. The go-ahead and Q1–Q4 are with @GideonZ and @barryw. Sorry for the time this has held you up.

@chrisgleissner

chrisgleissner commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Hi @JC-000, nice work! About your questions, I left a comment on this topic on the issue that sparked this PR.

It would be great if you could apply my comment and suggestion to your questions, carefully assess and compare the alternatives, and then suggest what the overall most defensible answers would be, and why.

This would make the decision process more defensible and help reviewers understand which alternatives you considered.

As part of this, it would also be interesting to think outside the box and check whether we could have addressed the user's original problem (playing games on C64U machines that communicate directly with each other) through other means, and what the pros and cons of those approaches would have been. Basically, taking a step back and assessing the overall design alternatives.

There are usually many ways to solve a problem, and comparing them systematically first can help us decide more objectively. It often also helps to prototype and compare different prototypes before settling on one.

I'm not saying that the approach you ended up taking isn't the ideal one, or that we will find a better one, but without further analysis, it's hard to say. It would be good to add this analysis to the PR description.

Many thanks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@barryw

barryw commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

@JC-000, thanks for building and testing this. @mfidder2026, thanks for testing direct play on two C64 Ultimates; that is useful evidence for the LAN game use case.

Answering your question in #962: feature/tls-coprocessor-test-merge is older experimental work (last commit August 30) with no upstream PR. It is not headed upstream in its present form. I would not carry its $12 allocation for OPEN_TLS forward. Current TLS draft #997 uses $21. I favor keeping $12–$15 for the listener command numbers already defined in client libraries; firmware does not implement them yet.

From the UCI side, $0A and the remote-port/local-port/host layout look sound. Existing OPEN_UDP needs another way to exchange its ephemeral source ports; a bound, connected UDP socket fits the measured LAN case without adding TCP accept semantics or a multi-peer UDP API. The fuller alternatives comparison Christian requested belongs in this PR description.

For Q2, status numbers are target-local, so 89,PORT IN USE does not conflict with other targets' use of 89. For Q4, rejecting no-reply, remote port 0, and bytes after the optional terminator looks right; remote port 0 also gives clients a safe support probe. For Q3, an in-use check protects services already bound but not one that binds later. I would address that specific service-port race rather than ban every port below 1024. Q5, Q6, and final protocol approval need Gideon's input.

I would leave the proposed nonblocking READ as a separate follow-up after #1016, with its alternatives compared as Christian suggested.

Ident (64), the DHCP client (68) and the VIC stream palette (53248) bind
their port at a time of their own: when switched on, when a link comes up,
when a stream starts. A socket holding the port then keeps the service
down. Ident and the palette retry; the DHCP client never re-binds port 68
(lwIP ignores udp_bind's result, and udp_connect then takes an ephemeral
port). OPEN_UDP_LOCAL now answers 89,PORT IN USE for those three ports on
every product, whether or not the service is up.

New E2E scenario udp-local-service-port-reserved switches ident off first,
so only the reservation can refuse port 64, then expects 89 for 64 and 53248.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@JC-000

JC-000 commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

@barryw @chrisgleissner Updated.

  • Q1, Q2 and Q4: as agreed, no change.
  • Q3: $0A now refuses the three UDP ports the firmware binds itself, 64 (ident), 68 (DHCP client) and 53248 (VIC stream palette), with 89,PORT IN USE, on every product, whether the service is on or not. Every other port stays open, below 1024 too. The bad case was DHCP: if a program held 68 when a link came up, the client would get no lease until every DHCP interface went down (from the code, not run). A new scenario, udp-local-service-port-reserved, ran red/green on a U64 Elite and a C64 Ultimate.
  • The alternatives comparison Christian asked for is in the description, under "Design analysis".
  • Q5, Q6 and Q7 (a $0A socket to 0.0.0.0 as receive-only; a WRITE on it is refused, also red/green on both devices), and approval of the protocol: @GideonZ.
  • The nonblocking READ comes after Add UCI network command OPEN_UDP_LOCAL ($0A) to open UDP from a fixed local port #1016, as its own follow-up with the alternatives compared.

On TLS: #997's $21 is OPEN_TLS; a fixed-port TLS client would use a future $0B and then #997's $24 START_TLS. Agreed on keeping $12-$15 for the listener.

Written with Claude Opus 5.5 in Claude Code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

# Conflicts:
#	software/io/network/network_target.cc
@JC-000

JC-000 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Merged the current master into this branch (596fc62). The conflicts were in network_target.cc. Master's new sliced connect and send now sit alongside this PR's local-port bind and its refusal of writes to a 0.0.0.0 remote, and both are kept. The socket is still tracked the way master requires, so an aborted OPEN_UDP_LOCAL closes its bound port. make host_tests passes and the U64 and U64-II firmware build. On an Ultimate 64 Elite running this head, net_target_test.py --test all --route all passes all 22 scenarios on both routes, including the 11 udp-local ones and reset-closes-uci-sockets.

Written with Claude Sonnet 5.5 and Claude Opus 5.5 in Claude Code.

@barryw

barryw commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

@JC-000, thanks for adding and testing the service-port reservation. Yes: refusing ports 64, 68 and 53248 even when their services are inactive addresses the future-bind race I raised for the firmware's current fixed UDP ports. The normal bind failure covers ports already in use. I consider Q3 resolved; Gideon's remaining protocol questions and approval are separate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New functionality or an improvement to existing behavior, including formats and developer tooling. network Networking and remote services: REST, Socket DMA, FTP, Telnet, and online access. uci C64-side Ultimate Command Interface, command targets, and Ultimate DOS commands; REST is network.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants