|
@@ -192,12 +192,18 @@ public class SipInviteSessionManager {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public PendingSipInvite matchPendingInvite(gov.nist.javax.sip.message.SIPResponse response) {
|
|
public PendingSipInvite matchPendingInvite(gov.nist.javax.sip.message.SIPResponse response) {
|
|
|
|
|
+ return matchPendingInvite(response, false);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** Final refusals are not 2xx, so rejection attribution validates correlation headers only. */
|
|
|
|
|
+ private PendingSipInvite matchPendingInvite(gov.nist.javax.sip.message.SIPResponse response, boolean rejection) {
|
|
|
String field = SipRequestBinding.key(response);
|
|
String field = SipRequestBinding.key(response);
|
|
|
if (field == null) return null;
|
|
if (field == null) return null;
|
|
|
Object raw = redisTemplate.opsForHash().get(recoveryKey("invite-request"), field);
|
|
Object raw = redisTemplate.opsForHash().get(recoveryKey("invite-request"), field);
|
|
|
if (raw == null) return null;
|
|
if (raw == null) return null;
|
|
|
PendingSipInvite pending = com.alibaba.fastjson2.JSON.parseObject((String) raw, PendingSipInvite.class);
|
|
PendingSipInvite pending = com.alibaba.fastjson2.JSON.parseObject((String) raw, PendingSipInvite.class);
|
|
|
- return pending.getBinding().matches(response) ? pending : null;
|
|
|
|
|
|
|
+ return rejection ? (pending.getBinding().matchesHeaders(response) ? pending : null)
|
|
|
|
|
+ : (pending.getBinding().matches(response) ? pending : null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/** Restore the actual To-tag and transaction from headers; malformed/absent SDP cannot erase a SIP dialog. */
|
|
/** Restore the actual To-tag and transaction from headers; malformed/absent SDP cannot erase a SIP dialog. */
|
|
@@ -212,10 +218,13 @@ public class SipInviteSessionManager {
|
|
|
SsrcTransaction stream = getSsrcTransactionByStream(owned.getApp(), owned.getStream());
|
|
SsrcTransaction stream = getSsrcTransactionByStream(owned.getApp(), owned.getStream());
|
|
|
SsrcTransaction call = getSsrcTransactionByCallId(owned.getCallId());
|
|
SsrcTransaction call = getSsrcTransactionByCallId(owned.getCallId());
|
|
|
if ((stream != null && !sameDialog(owned, stream)) || (call != null && !sameDialog(owned, call))) return null;
|
|
if ((stream != null && !sameDialog(owned, stream)) || (call != null && !sameDialog(owned, call))) return null;
|
|
|
|
|
+ // A real dialog supersedes any earlier provisional refusal of the same attempt.
|
|
|
|
|
+ Object rejected = redisTemplate.opsForHash().get(recoveryKey("invite-rejected"), owned.getApp() + ":" + owned.getStream());
|
|
|
return SipRedisCompareAndSet.apply(redisTemplate,
|
|
return SipRedisCompareAndSet.apply(redisTemplate,
|
|
|
new SipRedisCompareAndSet.Change(streamKey(), owned.getApp() + owned.getStream(), stream, owned),
|
|
new SipRedisCompareAndSet.Change(streamKey(), owned.getApp() + owned.getStream(), stream, owned),
|
|
|
new SipRedisCompareAndSet.Change(callKey(), owned.getCallId(), call, owned),
|
|
new SipRedisCompareAndSet.Change(callKey(), owned.getCallId(), call, owned),
|
|
|
new SipRedisCompareAndSet.Change(recoveryKey("bye-confirmed"), identity, null, null),
|
|
new SipRedisCompareAndSet.Change(recoveryKey("bye-confirmed"), identity, null, null),
|
|
|
|
|
+ new SipRedisCompareAndSet.Change(recoveryKey("invite-rejected"), owned.getApp() + ":" + owned.getStream(), rejected, null),
|
|
|
new SipRedisCompareAndSet.Change(recoveryKey("peer-bye"), identity, null, null)) ? owned : null;
|
|
new SipRedisCompareAndSet.Change(recoveryKey("peer-bye"), identity, null, null)) ? owned : null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -266,6 +275,39 @@ public class SipInviteSessionManager {
|
|
|
return isByeConfirmed(owned) ? owned : null;
|
|
return isByeConfirmed(owned) ? owned : null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /** Explicit final INVITE rejection means no dialog was established; timeouts and lost replies are NOT evidence. */
|
|
|
|
|
+ public boolean rememberInviteRejection(gov.nist.javax.sip.message.SIPResponse response) {
|
|
|
|
|
+ PendingSipInvite pending = matchPendingInvite(response, true);
|
|
|
|
|
+ if (pending == null) return false;
|
|
|
|
|
+ int status = response.getStatusCode();
|
|
|
|
|
+ // 401/407 are retryable challenges, not a final refusal of this generation.
|
|
|
|
|
+ if (status == javax.sip.message.Response.UNAUTHORIZED || status == javax.sip.message.Response.PROXY_AUTHENTICATION_REQUIRED) return false;
|
|
|
|
|
+ com.alibaba.fastjson2.JSONObject snapshot = new com.alibaba.fastjson2.JSONObject();
|
|
|
|
|
+ snapshot.put("binding", pending.getBinding()); snapshot.put("pending", pending); snapshot.put("status", status);
|
|
|
|
|
+ String key = recoveryKey("invite-rejected"), field = pending.getApp() + ":" + pending.getStream();
|
|
|
|
|
+ Object before = redisTemplate.opsForHash().get(key, field);
|
|
|
|
|
+ if (before != null) {
|
|
|
|
|
+ com.alibaba.fastjson2.JSONObject existing = com.alibaba.fastjson2.JSON.parseObject((String) before);
|
|
|
|
|
+ PendingSipInvite prior = existing.getObject("pending", PendingSipInvite.class);
|
|
|
|
|
+ // A different attempt on the same stream must not be overwritten by a stale rejection.
|
|
|
|
|
+ return prior != null && java.util.Objects.equals(prior.getBinding().getCallId(), pending.getBinding().getCallId())
|
|
|
|
|
+ && java.util.Objects.equals(prior.getTimeoutToken(), pending.getTimeoutToken());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (SipRedisCompareAndSet.apply(redisTemplate,
|
|
|
|
|
+ new SipRedisCompareAndSet.Change(key, field, null, snapshot.toJSONString()))) return true;
|
|
|
|
|
+ return isInviteRejected(pending.getNodeId(), pending.getApp(), pending.getStream(), pending.getTimeoutToken());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** Read-only: this exact allocation attempt was definitively refused before any dialog existed. */
|
|
|
|
|
+ public boolean isInviteRejected(String nodeId, String app, String stream, String allocationToken) {
|
|
|
|
|
+ Object raw = redisTemplate.opsForHash().get(recoveryKey("invite-rejected"), app + ":" + stream);
|
|
|
|
|
+ if (raw == null) return false;
|
|
|
|
|
+ PendingSipInvite prior = com.alibaba.fastjson2.JSON.parseObject((String) raw).getObject("pending", PendingSipInvite.class);
|
|
|
|
|
+ return prior != null && java.util.Objects.equals(prior.getNodeId(), nodeId)
|
|
|
|
|
+ && java.util.Objects.equals(prior.getApp(), app) && java.util.Objects.equals(prior.getStream(), stream)
|
|
|
|
|
+ && java.util.Objects.equals(prior.getTimeoutToken(), allocationToken);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/** A receipt proves this exact dialog, not a successful send or another allocation with the same Call-ID. */
|
|
/** A receipt proves this exact dialog, not a successful send or another allocation with the same Call-ID. */
|
|
|
public boolean isByeConfirmed(SsrcTransaction expected) {
|
|
public boolean isByeConfirmed(SsrcTransaction expected) {
|
|
|
if (expected == null || expected.getSipTransactionInfo() == null) return false;
|
|
if (expected == null || expected.getSipTransactionInfo() == null) return false;
|