Commit adee2ef2

adee2ef2d900Merged

Ori Pekelman <[email protected]> · 5 months ago · 5 files · +4 added · ~1 modified

Description

sp_net: POSIX TCP/poll/process/shell primitives in libspinel_rt.a

Add lib/sp_net.{c,h} -- a vendored, audit-sized POSIX-runtime helper (TCP / poll(2) / prefork / shell), archived into libspinel_rt.a alongside sp_crypto. Same precedent + spirit as sp_crypto (#514): pure C, no spinel-runtime dependency, no OpenSSL/libsodium, static return buffers, single-threaded server model.

Motivation: HTTP-shaped Spinel frameworks (tep, roundhouse) each carry this same ~250-LoC POSIX core. tep's lib/tep/sphttp.c is the source of this extraction (see OriPekelman/tep#12, matz/spinel#1054); the surface has been stable for months. HTTP framing, WebSocket accessors, and TLS stay framework-side -- TLS deliberately so, to keep libspinel_rt.a OpenSSL-free (an optional sp_net_tls can layer over these fds later; see #1054 for the A/B discussion this PR implements option A of).

Surface (sp_net_ prefix): install_term_handlers / shutdown_requested (SIGTERM/SIGINT for prefork graceful shutdown; accept honors the flag) listen / accept / accept_nb / connect / close / set_nonblock recv_some / recv_all / write_str / write_bytes poll_reset / poll_add / poll_run / poll_ready fork / exit / getpid / wait_any shell_capture

Consumers declare the FFI surface their side (as tep does for sphttp_* today) and build their HTTP layer on top; auto-linked from the archive like sp_crypto, so no ffi_lib needed.

Makefile: build/sp_net.o rule + add to the libspinel_rt.a archive + install lib/sp_net.h. test/sp_net_basic.rb: deterministic smoke (shell_capture + a 127.0.0.1 TCP loopback exercising listen/connect/ accept_nb/write/recv/close + getpid); auto-discovered by make test.

Open question for review: naming (sp_net vs sp_posix_net vs a separate libspinel_net.a), and whether the term-signal trio belongs here or framework-side. Happy to adjust.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
adee2ef2d900
Parent
9a43cb152fd8

MakefilemodifiedView file

@@ -288,9 +288,13 @@
288288 @mkdir -p build
289289 $(CC) -c -O2 -Wno-all $(SEC_FLAGS) -Ilib lib/sp_core.c -o build/sp_core.o
290290
291+build/sp_net.o: lib/sp_net.c lib/sp_net.h
292+ @mkdir -p build
293+ $(CC) -c -O2 -Wno-all $(SEC_FLAGS) -Ilib lib/sp_net.c -o build/sp_net.o
294+
291295 SP_RT_LIB = lib/libspinel_rt.a
292296
293-$(SP_RT_LIB): $(RE_OBJ) build/sp_bigint.o build/sp_crypto.o build/sp_pack.o build/sp_strscan.o build/sp_time.o build/sp_core.o
297+$(SP_RT_LIB): $(RE_OBJ) build/sp_bigint.o build/sp_crypto.o build/sp_pack.o build/sp_strscan.o build/sp_time.o build/sp_core.o build/sp_net.o
294298 ar rcs $@ $^
295299
296300 regexp: $(SP_RT_LIB)
@@ -594,6 +598,7 @@
594598 install -m 644 lib/sp_runtime.h $(SPNLDIR)/lib/
595599 install -m 644 lib/sp_core.h $(SPNLDIR)/lib/
596600 install -m 644 lib/sp_time.h $(SPNLDIR)/lib/
601+ install -m 644 lib/sp_net.h $(SPNLDIR)/lib/
597602 install -m 644 lib/*.rb $(SPNLDIR)/lib/
598603 install -d $(PREFIX)/bin
599604 ln -sf $(SPNLDIR)/spinel $(PREFIX)/bin/spinel