Commit Graph
2513 Commits
Author SHA1 Message Date
Max Lv 5f264333a0 Remove the stream ciphers
The unauthenticated stream ciphers (aes-*-cfb, aes-*-ctr, camellia-*-cfb,
chacha20, chacha20-ietf, salsa20) are gone from every build, together
with the SS_ENABLE_LEGACY option, the C++ stream codecs, their C test
oracle and the stream-only tests and configurations. A configuration
naming one of them, or RC4/table, now fails at startup with a message
pointing to an AEAD method, and test_cli checks that.

Shell completions list only the supported methods (and now include the
2022 ones), and the docs and the compatibility contract say the stream
ciphers were removed.
2026-10-15 00:34:33 +08:00
Max Lv 582a7e18d3 Fix the documentation build after moving the codecs
utils.cpp's \snippet aead.cpp and stream.cpp now live in src/crypto/,
which was not on Doxygen's EXAMPLE_PATH, so the man1 and HTML builds
failed. List the documentation sources explicitly instead of guessing
their extension.
2026-10-15 00:34:33 +08:00
Max Lv ca724e00f8 Use the Mbed TLS compat wrappers for MD5 key derivation 2026-10-15 00:34:32 +08:00
Max Lv 4651184e1f test.py: accept host:port for --dns
The tunnel test hard-coded port 53, so it could only use a public
resolver or root. With a port it runs against a local DNS stand-in.
2026-10-15 00:34:32 +08:00
Max Lv 8d8f41929f Include <algorithm> for std::min in crypto/keys.cpp (CLANG64) 2026-10-15 00:34:32 +08:00
Max Lv 5e4f6a6fa3 Time client-side TCP connections when idle, not only while connecting
ss-local, ss-tunnel and ss-redir only timed the upstream connect (as the
C relays did), so a client that never finished its SOCKS handshake, or a
stream whose peer vanished without a FIN, held a pool slot forever. With
the pool now a hard cap that pauses accepting when full, enough of them
stopped the listener for good. Every connection now starts with the idle
deadline, connected() re-arms it, and traffic refreshes it for all kinds
(the idle timeout keeps its 24 h floor, MIN_TCP_IDLE_TIMEOUT).
2026-10-15 00:34:32 +08:00
Max Lv 4eb6a7415b Leave pool room for UDP sessions that are still closing
An evicted session keeps its pool slot until its socket's close callback
runs, so with pool capacity equal to the table's the acquire after an
eviction failed: a full table dropped new-session packets after evicting
a live session for nothing. The AEAD-2022 shared-session pool had the
same problem with entries still referenced by relay sessions.
2026-10-15 00:34:32 +08:00
Max Lv af4e7a02af Fall back to the system heap when a library realloc cannot be served
heap_realloc returned NULL when a block outgrew the largest size class or
the budget, and libuv aborts when growing its watcher array fails: a poll
on fd 8190 or above (about 4,000 connections) killed the process.
Move the block to the system heap instead, as malloc and calloc already
do. test_heap_guard_cpp polls fd 16380 with the heaps installed.
2026-10-15 00:34:32 +08:00
Max Lv 62cbdb6619 Fix the fully static image and the build image for the C++ sources
docker/static: the C++ unit tests link the static libstdc++, whose error
paths need an unwinder that --rtlib=compiler-rt does not provide; link
llvm-libunwind. The programs themselves still need no C++ runtime.

docker/build: build in the default bundled mode with CMake instead of
against libev/udns (Alpine's Mbed TLS 4 is not supported by system
mode).
2026-10-15 00:34:32 +08:00
Max Lv de54d92c92 Fix the MinGW build of ss-server: stat_resolver is POSIX-only 2026-10-15 00:34:32 +08:00
Max Lv 462df80307 Report the C++20 rewrite: allocation, size, throughput and memory
docs/performance.md compares master (C) with the C++ engine on macOS
arm64: zero heap allocations per connection or datagram (from 27-89),
TCP throughput within noise, and about 20 MiB more RSS at 1,000 open
connections from the inline per-connection buffers. docs/cpp-core.md
and CLAUDE.md now describe the finished layout.
2026-10-15 00:34:32 +08:00
Max Lv db5e14c1e8 Add --max-dns-queries / max_dns_queries
Sizes the resolver's query pool, the last of the capacity settings the
no-heap-after-startup design calls for. Defaults are unchanged: the
connection limit (plus 64 on ss-server, which also resolves for its
statistics).
2026-10-15 00:34:31 +08:00
Max Lv 680b3a3a62 Keep clang-tidy clean in the new C++ tests 2026-10-15 00:34:31 +08:00
Max Lv ac29c37270 Describe the C++20/libuv build in the Debian and snap metadata 2026-10-15 00:34:31 +08:00
Max Lv 53adaf380e Replace uthash and ss_list with the C++ containers
The C cache (now used only by the reference codecs) moves to
tests/reference and src/uthash.h is deleted. ACL rules and ss-manager's
servers are ListNodes on IntrusiveLists; destroying an element unlinks
it, so the free paths no longer have to remember to. core.h keeps only
ss_ip. test_rule is ported to C++ and also covers literal rules.
2026-10-15 00:34:31 +08:00
Max Lv 34da0def95 Move the C codecs to tests/reference and drop uthash from ss-manager
The programs now use only the C++ codecs. The C stream/AEAD/AEAD-2022
implementations stay as the reference the differential tests and the C
unit tests compare against. Key derivation and the cipher tables used by
the doc snippets move to src/crypto/keys.{hpp,cpp}.

ss-manager keeps its servers in an ss_list (at most MAX_PORT_NUM,
looked up only on control messages) instead of a uthash table, and
tests/test_manager.py covers add/ping/list/remove end to end.
2026-10-15 00:34:31 +08:00
Max Lv d990f65805 Reinitialize the event loop after daemonize()
The loop is created before the programs fork into the background, and
kqueue/epoll descriptors do not survive fork(), so ss-server -f (which
ss-manager always uses) died as soon as it ran. Call uv_loop_fork() in
the child.
2026-10-15 00:34:31 +08:00
Max Lv 3efcb79010 Remove ss_event and the C resolver shim
Each program's loop, signals, plugin watcher and stats timer now go
through ss::event::Program (src/event/program.hpp), held on main()'s
stack; ss-manager uses it too. ss-server resolves the manager address
with the relay resolver. With no C caller left, ss_event.cpp/.h,
resolv.cpp/.h and their C tests go; test_resolver_cpp.cpp replaces the
resolver tests against the same delayed local DNS server, and ss_ev is
now an interface target over libuv.
2026-10-15 00:34:31 +08:00
Max Lv 335eb10742 Add an embedding API: ss_local_start/stop/wait and a C++ wrapper
<shadowsocks/control.h> starts ss-local on a library-owned thread,
stops it from any thread through a libuv async handle and waits for it;
<shadowsocks/shadowsocks.hpp> wraps that in a move-only, header-only
shadowsocks::LocalServer whose destructor stops and waits. The library
exports only C symbols, shadowsocks.h is unchanged, and the shared
library version becomes 2.1.0 (SOVERSION 2). test_local_api_cpp covers
the lifecycle; the installed-package consumer gains a C++ program.
2026-10-15 00:34:30 +08:00
Max Lv 282f3606d7 Add --max-connections and --max-udp-sessions
The TCP connection pool and the UDP session table are sized from these
options (or max_connections / max_udp_sessions in the configuration
file) at startup instead of fixed constants; the DNS query pool follows
the connection limit. test_limits.py checks that a connection beyond
the limit is refused and that accepting resumes once one closes.
2026-10-15 00:34:30 +08:00
Max Lv 6a1500ddd7 Fix GCC -Wsign-compare errors in the converted C modules
GCC enables -Wsign-compare with -Wall for C++ (clang does not), which
turned a few signed/unsigned comparisons in base64, netutils, server and
manager into errors; udp.cpp needed <ctime>.
2026-10-15 00:34:30 +08:00
Max Lv c69fb4c4d9 Seal the heap: bounded library heaps and zero-allocation program tests
The programs point libuv, c-ares and Mbed TLS (MBEDTLS_PLATFORM_MEMORY in
the bundled build) at their own FixedHeap before anything runs, mark the
end of startup with heap_seal(), and in verbose mode report heap use and
any allocation after it at exit. The library build keeps the host's
allocator.

measure_alloc.py gains --udp, and the alloc_steady_programs_* tests now
fail on any malloc per relayed TCP connection (by name and by address)
or UDP association, for every cipher family.
2026-10-15 00:34:30 +08:00
Max Lv e0047c9cef Keep clang-tidy clean in the relay engine 2026-10-15 00:30:46 +08:00
Max Lv b30ae18a0b Build the UDP relay with Clang on musl
musl's CMSG_NXTHDR compares a size_t with a ptrdiff_t, which Clang's
-Wsign-compare rejects in C++. Call it from one helper that suppresses
the warning there.
2026-10-14 22:53:10 +08:00
Max Lv 1a7b11e2b8 Include <ctime> for std::time in the UDP relay (libstdc++) 2026-10-14 22:53:10 +08:00
Max Lv 5f03b994dd Copy the literal ACL address without strncpy
GCC 13 at -O3 rejects the strncpy from the 256-byte host into the
INET6_ADDRSTRLEN buffer (-Wstringop-truncation) on glibc and UCRT64.
2026-10-14 22:53:09 +08:00
Max Lv 29b1983409 Let the allocation audit target an IP so DNS costs show separately 2026-10-14 22:53:09 +08:00
Max Lv bde90e4ae4 Run ss-redir and every UDP relay on the C++ engines
ss-redir's TCP side moves onto the relay engine (per-listener TOS for the
DSCP ports). src/relay/udp replaces udprelay.cpp: sessions keyed by
client address own their upstream socket and live in a fixed LRU,
AEAD-2022 server sessions are shared through RefPtr between the
session-ID table and the relays using them, and datagrams waiting for
DNS sit in pool slots. Packets are received into and built in
relay-owned buffers instead of a 64 KiB allocation per datagram. The
programs no longer call crypto_init(); Method sets up libsodium and the
replay filter.
2026-10-14 22:53:09 +08:00
Max Lv c5c8e5ebaf Fix the build with GCC and libstdc++
crypto.h's min/max macros broke <algorithm> in C++ sources that include
it first, netutils.h's linux/tcp.h clashed with the netinet/tcp.h libuv
includes, and GCC rejects memcpy into crypto_t. Apple clang accepted all
three.
2026-10-14 22:53:09 +08:00
Max Lv 46f5ddcf80 Run ss-server on the C++ TCP relay engine
The request header, outbound ACL, target resolution through the relay's
resolver, the anti-probing stop stage and the manager traffic counters
are engine code now; the connmark-to-TOS tracker becomes a pair of relay
hooks. server.cpp keeps option parsing, nftables and the stats timer.
2026-10-14 22:53:09 +08:00
Max Lv c8311a1c35 Run ss-local and libshadowsocks-c on the C++ TCP relay engine
The SOCKS5 handshake, the ACL bypass decision (with its own resolver for
host-name rules) and the short wait for a first payload are now engine
code; local.cpp keeps only option parsing and startup. The library entry
points start the same engine.
2026-10-14 22:53:09 +08:00
Max Lv 892b45d1e5 Add the C++ TCP relay engine and run ss-tunnel on it
One engine (src/relay/tcp) for the four TCP relays: connections live in a
fixed pool owned through PoolPtr, handshake, cipher state and buffers are
inline, timeouts are intrusive deadline lists on one timer and DNS answers
come back through generation-checked handles. A full pool refuses new
connections and pauses accepting until a slot frees.

ss-tunnel is the first user; its hand-written relay callbacks are gone.
tests/tunnel_interop.py covers every method against ss-server and
shadowsocks-rust.
2026-10-14 22:53:09 +08:00
Max Lv 78ca0af745 Rewrite the resolver in C++ on a fixed query pool
Queries live in pool slots with their first A and AAAA answer inline,
c-ares sockets are watched from a fixed table, and answers reach the
waiter through a callback plus opaque words instead of a back-pointer.
resolv.cpp keeps the C API as a shim. The event Loop can now adopt the
ss_event loop, and a Poll can stop watching a socket it does not own.
2026-10-14 22:53:09 +08:00
Max Lv ca359160b2 Include C++ headers before crypto.h in test_crypto_aead2022_cpp.cpp
crypto.h defines min/max macros for the C sources; libstdc++ headers
included after it fail to parse.
2026-10-14 22:53:08 +08:00
Max Lv f92f578cce Include <algorithm> for std::min in test_crypto_aead2022_cpp.cpp 2026-10-14 22:53:08 +08:00
Max Lv 47163a57fd Add allocation-free C++ SIP022 codecs
TCP request/response streams with the salt echo between the two
directions, an exact-match salt pool on FixedLru, and UDP packets sealed
and opened in place with caller-owned session state. Differentially
tested against aead2022.cpp in both roles; the steady phase allocates
nothing.
2026-10-14 22:53:08 +08:00
Max Lv 98eda0bdff Include <netinet/in.h> in the UDP eviction test client (FreeBSD)
FreeBSD's <arpa/inet.h> does not define struct sockaddr_in.
2026-10-14 22:53:08 +08:00
Max Lv a6113cec4b Own AEAD-2022 UDP sessions by reference so eviction cannot free a live session
The session table and the relay contexts each hold a reference; evicting a
session from the full table no longer frees memory a relay still uses.
Ported from the parked fix/aead2022-udp-session-eviction work.
2026-10-14 22:53:08 +08:00
Max Lv 503f0f1936 Include C++ headers before crypto.h in test_crypto_stream_cpp.cpp
crypto.h defines min/max macros for the C sources; libstdc++ headers
included after it fail to parse.
2026-10-14 22:53:07 +08:00
Max Lv 8cf10ba890 Do not memcpy from an empty span in the stream decoder
An empty read before the IV arrived passed a null source to memcpy,
which UBSan reports (and the ASan CI job turns into a failure).
2026-10-14 22:53:07 +08:00
Max Lv bf5bc2f216 Include <algorithm> for std::min in test_crypto_stream_cpp.cpp 2026-10-14 22:53:07 +08:00
Max Lv 0e6f0ca980 Document the crypto codecs, cache rewrite and allocation progress 2026-10-14 22:53:07 +08:00
Max Lv d91ef14f76 Add allocation-free C++ stream cipher codecs
src/crypto/stream.hpp implements the legacy stream methods this build
supports (AES CFB/CTR, Camellia CFB, Salsa20, ChaCha20, ChaCha20-IETF)
as value types. The AES and Camellia contexts are held by value through
Mbed TLS's block-cipher APIs instead of an allocated
mbedtls_cipher_context_t, and the libsodium ciphers track their
keystream position, finishing partial 64-byte blocks from a stack block
instead of the padding copies stream.cpp makes.

test_crypto_stream_cpp runs TCP streams (with fragments that split
keystream blocks) and UDP packets between the C and C++
implementations in both directions for all twelve methods and checks
IV replay; alloc_steady_crypto_stream requires zero allocations for new
connections and packets.
2026-10-14 22:53:07 +08:00
Max Lv ed75faf80f Rewrite the cache on a fixed slab without per-entry allocation
cache.cpp keeps its C API but replaces uthash and the two mallocs per
entry with one slab of nodes holding their keys inline, linked in
last-use order, and an open-addressing index of node ids that hashes
through the slab with a per-cache random seed. Both blocks are reserved
by cache_create() and touched only as entries are used, so a large,
mostly empty cache costs no resident memory.

Semantics are unchanged: an insert evicts the least recently used entry
once the cache holds capacity entries (the AEAD-2022 session table's
4095/4096 boundary), lookups refresh recency and age, cache_clear()
drops stale entries, and free_cb may re-enter the cache. Re-inserting a
key now replaces its data instead of shadowing it. cache_create_keyed()
sizes keys per cache; the AEAD-2022 salt pool and session table use it.

test_cache_cpp covers the boundary, ordering, replacement, ageing,
re-entrant callbacks and limits; alloc_steady_cache requires zero
allocations across 100,000 inserts on a full cache. AEAD-2022 ss-server
drops from 84 to 80 allocations per relayed connection.
2026-10-14 22:53:07 +08:00
Max Lv 110459eba8 Disable the zero-allocation checks with a system Mbed TLS
They hold for the bundled Mbed TLS (MBEDTLS_BLOCK_CIPHER_C); a system
one may allocate in GCM key setup, as the Debian package build shows.
2026-10-14 22:53:06 +08:00
Max Lv ab365b8fff Include C++ headers before crypto.h in test_crypto_aead_cpp.cpp
crypto.h defines min/max macros for the C sources; libstdc++ headers
included after it fail to parse.
2026-10-14 22:51:57 +08:00
Max Lv 0ff1fa6d66 Build the C++ AEAD codecs against Mbed TLS 2.x
Mbed TLS 2 returns void from mbedtls_sha1_starts/update/finish (the int
versions are the _ret variants), which the system-library build on
Ubuntu (2.28) rejects. crypto/mbedtls_compat.hpp gives one int-returning
spelling for both.
2026-10-14 22:51:57 +08:00
Max Lv 3e662faae7 Include <algorithm> for std::min in test_crypto_aead_cpp.cpp 2026-10-14 22:51:57 +08:00
Max Lv 556c1f0696 Add allocation-free C++ AEAD codecs
src/crypto/aead.hpp implements the SIP004/SIP007 AEAD ciphers as value
types: AeadEngine holds the libsodium or Mbed TLS GCM state by value,
AeadTcpEncryptor/AeadTcpDecryptor frame the chunk stream with a fixed
reassembly buffer, and aead_udp_seal/open handle packets. HKDF-SHA1 runs
on stack SHA-1 state instead of Mbed TLS's allocating HMAC API.

The bundled Mbed TLS config now enables MBEDTLS_BLOCK_CIPHER_C, which
keeps the AES context inside mbedtls_gcm_context, so GCM key setup no
longer allocates. That alone saves the C programs two allocations per
connection.

test_crypto_aead_cpp checks HKDF against RFC 5869 and crypto_hkdf, runs
TCP streams and UDP packets between the C and C++ implementations in
both directions for all five AEAD methods, and covers tampering,
framing and salt replay. alloc_steady_crypto_aead runs it under the
malloc counter and requires zero allocations across 600 new
connections and 600 packets (tests/alloc_audit/check_steady.py).

ss_strict_warnings() now applies -Wextra per source file so targets
mixing new C++ with converted C modules keep the latter at -Wall, and
Result gains move assignment.
2026-10-14 22:51:56 +08:00
Max Lv df7a6dc13f Add the C++ event layer over libuv
src/event/loop.hpp wraps uv_loop/poll/timer/signal/async handles that
live by value inside their owner and dispatch to an owner member
function chosen at compile time. A Poll owns its socket and carries
read and write interest on one handle, closing the descriptor in
libuv's close callback. CloseGroup collects the asynchronous closes of
an owner's handles and reports completion afterwards, which is how
pooled owners implement AsyncClosable. DeadlineList drives the idle
timeouts of many owners from one timer.

test_event_cpp covers readiness, closing a peer from inside a
callback, recycling a pooled owner only after its handles closed,
repeating timers, deadline ordering and cross-thread wakeups.
2026-10-14 22:51:38 +08:00