Skip to content

pjsua: select dialog Via address toward the actual next hop#5060

Open
nanangizz wants to merge 1 commit into
masterfrom
feat/via-addr-route-set-aware
Open

pjsua: select dialog Via address toward the actual next hop#5060
nanangizz wants to merge 1 commit into
masterfrom
feat/via-addr-route-set-aware

Conversation

@nanangizz

@nanangizz nanangizz commented Jul 6, 2026

Copy link
Copy Markdown
Member

Follow-up to #5051 (now merged); this PR targets master and contains only its own change.

Problem

When an account is not using STUN/UPnP, pjsua selects the local Via sent-by for a dialog via pjsua_acc_get_uac_addr(). That function resolves the destination from the account's outbound proxy (acc->route_set) or the passed target URI. That reference is wrong for the request's actual next hop in two cases:

  • UAS dialogs (incoming call, incoming SUBSCRIBE): in-dialog requests (BYE, NOTIFY, re-INVITE) are routed via the dialog route set built from the incoming Record-Route, falling back to the remote target — never the account's outbound proxy.
  • Outgoing call (dlg_set_via): the target passed was acc->cfg.id (the account id URI), not the dialog's remote target. With no account proxy, the Via is computed toward the account domain instead of the actual callee.

On a multihomed host these can put the wrong local interface in the Via.

Change

Factor the address-resolution body of pjsua_acc_get_uac_addr() into a shared static core get_uac_addr_for_sip_uri()behavior for all existing UAC callers is unchanged — and add two dialog variants that compute the address toward the actual next hop and skip reliable transports (their Via is resolved from the real connection at send time; they return PJ_ENOTFOUND so the caller leaves the Via untouched):

  • pjsua_acc_get_uas_addr() — next hop = dlg->route_set top (from the incoming Record-Route), else dlg->target. Used by pjsua_call_on_incoming() and pres_on_rx_request().
  • pjsua_acc_get_uac_dlg_addr() — next hop = account outbound proxy when set, else the dialog's remote target (dlg->target) rather than acc->cfg.id. Used by dlg_set_via().

Keying the reliable-transport skip off the true next hop also fixes a corner case where a UDP next hop reached via a reliable target/proxy could get a STUN-mapped address in the Via (#1804 / #1412).

Notes

  • UDP behavior in the common case (no Record-Route, no account proxy) is unchanged — the address is still computed toward dlg->target.
  • No public API change; the two new functions are internal (pjsua_internal.h).

Testing

Non-sanitizer build:

  • 100_simple.py (UDP call) — pass
  • 200_tcp.py (TCP call) — pass

(The 100_peertopeer.py presence test fails identically on master and is a pre-existing environment issue, not related to this change.)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refines how PJSUA selects the local Via sent-by address when an account is not using STUN/UPnP, ensuring the selection is made toward the dialog’s actual next hop (route set / remote target) rather than the account’s outbound proxy or id URI, and avoiding reliable-transport probing that can create unused TCP/TLS connections.

Changes:

  • Refactors the core address-selection logic from pjsua_acc_get_uac_addr() into a shared static helper and adds two internal dialog-oriented variants.
  • Updates incoming-call and incoming-SUBSCRIBE paths to compute the Via address toward the dialog’s effective next hop (route set top entry, else remote target).
  • Updates outgoing-call dialog Via setup to use the dialog’s remote target (when no account outbound proxy is configured), while skipping reliable transports.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
pjsip/src/pjsua-lib/pjsua_pres.c Switches incoming SUBSCRIBE dialog Via selection to the new UAS next-hop-aware resolver.
pjsip/src/pjsua-lib/pjsua_call.c Updates outgoing dialog Via selection and incoming-call Via selection to use next-hop-aware dialog resolvers.
pjsip/src/pjsua-lib/pjsua_acc.c Factors out core SIP-URI-based address resolution and adds dialog (UAS/UAC) helpers that skip reliable transports.
pjsip/include/pjsua-lib/pjsua_internal.h Declares the new internal dialog-oriented address selection functions.

@nanangizz nanangizz changed the base branch from fix-incoming-call-spurious-tcp-connection to master July 7, 2026 07:58
The UAS (incoming call / incoming SUBSCRIBE) and UAC (outgoing call) paths
determined the local Via address via pjsua_acc_get_uac_addr(), which for a
UAS dialog resolves the destination from the account outbound proxy or the
passed target, and for the outgoing call passed acc->cfg.id (the account id
URI). Neither reflects the request's real next hop, so on a multihomed host
the Via could carry the wrong local interface.

Factor the address-resolution body of pjsua_acc_get_uac_addr() into a shared
static core get_uac_addr_for_sip_uri() (behavior for all existing UAC callers
is unchanged), and add two dialog variants that compute the address toward the
actual next hop and skip reliable transports (Via resolved at send time,
returning PJ_ENOTFOUND so the caller leaves the Via untouched):

- pjsua_acc_get_uas_addr(): next hop = dlg->route_set top (from the incoming
  Record-Route), else dlg->target. Used by pjsua_call_on_incoming() and
  pres_on_rx_request().
- pjsua_acc_get_uac_dlg_addr(): next hop = account outbound proxy when set,
  else the dialog's remote target (dlg->target) instead of acc->cfg.id. Used
  by dlg_set_via().

Keying the reliable-transport skip off the true next hop also fixes a corner
case where a UDP next hop reached via a reliable target/proxy could get a
STUN-mapped address in the Via (#1804 / #1412).

Co-Authored-By Claude Code
@nanangizz nanangizz force-pushed the feat/via-addr-route-set-aware branch from c80f067 to 501f10d Compare July 7, 2026 08:13
@nanangizz nanangizz requested review from sauwming and trengginas July 7, 2026 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants