Repository navigation
Conversation
Lt-Flash
force-pushed
the
feature/th-stored-encoding
branch
from
November 22, 2026 13:43
6c92d23 to
9ad0c3d
Compare
Contributor
Author
|
I've rebased this onto current master and pushed a round of fixes. While going over the branch again I found a few gaps in how the stored state behaves in real dialogs, so I'd rather they land before anyone spends review time on it:
Each fix is its own commit with a test, and the description is updated to match. With the default config, master and this branch still produce byte-identical Contacts. Thanks again for your time! |
With the new 'S' encoding type of th_contact_encode_param_password, the dialog-less state is kept in a cachedb backend (th_state_url) and only a 17 characters key travels in the Contact URI param: '_' followed by 16 hex digits, which the word64/word32 encodings can never produce. Some user agents truncate long Contact URI params, which breaks the matching of their sequential requests, and a SUBSCRIBE never gets a dialog to keep its state in. The key is the MD5 of the param's password, the Call-ID and the tag of the party whose Contact is hidden, so each refresh of a leg overwrites its state in place. The state lives as long as its dialog says (expires + 30 s for SUBSCRIBE/NOTIFY, Session-Expires + 30 s for INVITE/UPDATE, th_state_ttl_short for transaction-only methods), a reply saying nothing takes the lifetime of its request, the requests routed through a state refresh it, and the end of a subscription drops it. What cannot be stored stays inline, compact encoded: an INVITE without session timers, a message lacking its Call-ID or tag, and an in-dialog message which is not a target refresh, as it would overwrite its leg's state. A dialog keeps the encoding it started with, so a param can be switched between C and S in place. The value is kept as printable length-prefixed fields, as it never travels on the wire. A miss in the backend is logged as a warning, a backend failure as an error. Without an 'S' entry and th_state_url nothing changes. The new test/test_stored_state.py drives SUBSCRIBE/NOTIFY and INVITE flows through a proxy against a local redis-server.
A SUBSCRIBE leaves its subscriber's state behind on the way in, stored for as long as the subscription was asked to last. When the initial SUBSCRIBE gets a final reply of 300 or above, no dialog ever comes of it, while the subscriber typically retries with a fresh Call-ID and leaves one more state behind each time. Drop that state as soon as such a reply is relayed. A 401/407 drops it too: the retry carries the same Call-ID and From tag, so it simply stores it again under the same key. A failed refresh is left alone, as it does not necessarily end its subscription (RFC 6665 4.1.2.2).
An OPTIONS is no target refresh, so no request is ever routed to the Contact of an OPTIONS or of its reply, yet its hidden Contact is encoded like any other, and stored with the stored encoding type. With th_state_options set to 0, the Contact of an OPTIONS and of its reply is just the proxy's own address, with no encoded param, and nothing is stored. The default, 1, keeps the current behaviour.
The stored states are only ever removed by their expiration, and cachedb_mongodb ignores the expiration given to set(), so they would pile up in it forever. The cachedb API has no capability telling which backends honour it, so the mongodb scheme is refused at startup.
A working configuration in samples/: dialog-less topology hiding with the 'S' encoding type kept in Redis, OPTIONS without any state, and the sequential requests matched by topology_hiding_match(). The README now also tells which URLs th_state_url takes, and where the backends differ: cachedb_local is per node unless its collection is replicated, and cachedb_sql and cachedb_dynamodb may still return a state for a while after it expired.
With TH_TEST_DIR set, test_stored_state.py writes its cfgs and the proxy logs there and leaves them in place, so a caller (a CI job scanning them for sanitizer reports, say) can read them even when all the checks passed.
…re it While the backend is down every write fails, and the Contact was left half rewritten, so the request went out with a broken Contact and the reply with an unhidden one. Such a state now travels compact encoded in the Contact, the same as one which is not stored, and each process warns about the failed writes at most once a minute.
The lifetimes taken from Expires, Subscription-State and Session-Expires come from the far end: a huge value kept a state for decades, values near INT_MAX overflowed into a negative TTL, and memcached reads an expiration beyond 30 days as an absolute time. They are now cut down to th_state_ttl_max (default 1 day, at most 30 days).
The Contact ;expires parameter only has a meaning for a REGISTER (RFC 3261 10.2.1.1), yet it came before the Expires header of a SUBSCRIBE: a refresh carrying ;expires=0 was taken for an unsubscribe. A REGISTER, on the other hand, got th_state_ttl whatever its expires said.
The key hashed the password, Call-ID and tag run together, so a Call-ID and tag split at another point gave the same key, and anyone knowing a leg's Call-ID and tag could overwrite its state. A request could also reach any state by putting its key in the R-URI, whatever its own dialog. The lengths now go into the hash, a fetched key must be the one of the request's own Call-ID and To tag, a reply carrying its caller's tag as To tag is not stored, and a key must be hex.
Matching a request on its stored state removed that state when the request ended a subscription, and refreshed it with the copy just read otherwise, all before the request was even forwarded. A retransmission or the retry of a challenged unsubscribe then found nothing, one notifier of a forked SUBSCRIBE ended the dialogs of all the others (they share the subscriber's key), and the write-back could undo a newer state stored meanwhile. The far end's reply re-stores the state with the lifetime its dialog now has, so nothing is done on match anymore, and what the subscriber side stores never gets below th_state_ttl.
All the usages of a dialog share its keys, so a REFER and its NOTIFYs inside a call stored the call's states with subscription lifetimes and the final NOTIFY dropped one, cutting the call a minute later. Replies to sequential requests also decided anew, which switched an inline call to stored on its first in-dialog NOTIFY. The kind of dialog now goes with the stored state: only the messages of that kind touch it, the others go inline, a sequential reply keeps what its dialog came in with, and a re-INVITE or UPDATE without session timers goes inline rather than resetting the call's lifetime to th_state_ttl.
A stored INVITE kept its caller's state for its own Session-Expires plus 30 s, whatever the answer said. A callee without session timers answers without one, and then nothing ever refreshes the call (RFC 4028 7.2), so the caller's state expired under the call; a long ringing could also outlive a short interval. The 2xx now sets that lifetime again: its own Session-Expires plus 30 s, or th_state_ttl when it has none.
When a request went through topology hiding twice with the same password and backend - a spiral, or two nodes in a row - the second pass derived the same key from the same Call-ID and tag and stored the first pass's hidden Contact under it, so the key pointed at itself and the real Contact was lost. A Contact already carrying one of our keys now gets its state inline, which nests the way C and L always did.
The stored decoder never set $TH_decoded_contact and $TH_decoded_routes(_count), so a script saw them only for the S dialogs which fell back inline. It also took non numeric flags for 0 and went on with no socket when the stored one did not parse, where the inline decoder refuses the state.
Any failure of the stored encoding fell back to the inline one, so a bad Contact was refused and logged by both, and a state too long to store logged an ERROR on every message although it then went inline. A failed allocation after the write also left a stored state behind. Only a write which failed (backend down, state too long) now goes inline, counted in the rate limited warning, and the state is written last.
A th_state_url set to "" aborted the startup, while the cachedb dependency took it for no storage at all. A backend which could not be connected at startup stopped OpenSIPS although the states can go inline meanwhile, and that process then never connected again. "" now means no storage, and a failed connection is retried every 5 seconds.
A rejected initial SUBSCRIBE only had its subscriber state dropped when the rejection was relayed. A final reply of our own - the 408 of a timeout, or a t_reply() - left it to expire with the full subscription lifetime.
Some checks passed without what they named: an inline check also took a missing thinfo, the C->S switch and the OPTIONS checks left the reply out. A proxy is now waited for with a probe rather than a fixed sleep, a missing key no longer raises a TypeError in a check's detail, a failed compact setup no longer raises at all, and the UA sockets drop SO_REUSEADDR so that a port left behind fails loudly.
Lt-Flash
force-pushed
the
feature/th-stored-encoding
branch
from
December 1, 2026 09:51
9ad0c3d to
4d8fc47
Compare
The README still said a used state was refreshed or dropped on the spot, that a stored INVITE depended on the callee's session timers, and gave no word on the usages inside a dialog, the twice hidden Contacts, the key check, a backend down or restarted, and an empty th_state_url.
Lt-Flash
force-pushed
the
feature/th-stored-encoding
branch
from
December 1, 2026 18:07
4d8fc47 to
cb9a249
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A third encoding type for
th_contact_encode_param_password, next toCandL:S(stored). The dialog-less state goes into a cachedb backend (newth_state_url), and only a 17-character key travels in the Contact param. This is the smaller follow-up I promised when closing #4114.Details
Some user agents, Cisco CP-88xx phones in my case, truncate long Contact params, and then their sequential requests no longer match. A SUBSCRIBE can't fall back on a dialog, since
dlg_create_dialog()only handles INVITEs. Param length I measured:C, charsS, charsSolution
_+ 16 hex chars of MD5(password, Call-ID, tag of the hidden party), with the field lengths hashed in._never comes out of word64/word32, each refresh of a leg lands on the same key, and a request is only routed by the key of its own Call-ID and To tag.th_state_ttl_shortfor transaction-only methods), capped byth_state_ttl_max. Nothing happens on match: the far end's reply stores the state again. The 2xx to an INVITE sets the caller's lifetime (th_state_ttlwithout session timers). The subscriber's state never goes belowth_state_ttl, since forked notifier dialogs share it. A rejected initial SUBSCRIBE drops it.th_state_options = 0sends OPTIONS with no state at all.mongodb:is refused, as it ignores expiry.th_state_urltakes any cachedb URL (redis:,memcached:,local://, ...); the README notes wherelocal,sqlanddynamodbdiffer. A key is recognised under any entry whileth_state_urlis set, and a dialog keeps the encoding it started with, so an entry can be switched betweenCandSin place. A working sample is insamples/.Compatibility
Nothing changes unless
Sorth_state_urlis configured: with the default config, master and this branch produce byte-identical Contacts. The state is stored in the clear, so the backend must be trusted, and it must be shared by all nodes. Tested with cachedb_redis viamodules/topology_hiding/test/test_stored_state.py, and the sample with cachedb_local too; builds clean with gcc 14 and clang 19-Werror.Thanks for having a look!
Closing issues
None; follows up on #4114.