fix(voice): put RELEASE_CAUSE Reason on CANCEL and BYE

Why: IR.92 2.2.4 requires protocol RELEASE_CAUSE on UE-originated CANCEL and BYE.
Why this works: CANCEL is built from the INVITE then gets the Reason header; in-dialog BYE writes the same header in the core request builder.
Remaining: inbound Reason mapping is still best-effort text parse, not a full Q.850 table.
This commit is contained in:
yibaiba committed 2026-08-31 13:17:16 +08:00
1 parent 3ca09e375d
commit 4cada003e2
3 files changed
+30 -2

No files matched your search

+24 -1
View File
@@ -35,7 +35,25 @@ func buildIMSPreconditionUpdate(agent *Agent, call *Call, sdp string) string {
return ""
}
dialog := call.advanceVoiceCSeq()
return buildVoiceRequest(dialog, call.CallID(), "UPDATE", voiceBranch(), sdp)
request := buildVoiceRequest(dialog, call.CallID(), "UPDATE", voiceBranch(), sdp)
if call.dialogRequiresPrecondition(dialog.remoteTag) {
request = insertSIPHeader(request, "Require: precondition")
}
return request
}
func insertSIPHeader(message, header string) string {
header = strings.TrimSpace(header)
if header == "" || strings.TrimSpace(message) == "" {
return message
}
if !strings.HasSuffix(header, "\r\n") {
header += "\r\n"
}
if idx := strings.Index(message, "\r\n\r\n"); idx >= 0 {
return message[:idx+2] + header + message[idx+2:]
}
return message
}
func buildIMSReinvite(agent *Agent, call *Call, sdp string) string {
@@ -138,10 +156,15 @@ func buildVoiceRequestWithContent(dialog voiceSIPDialog, callID, method, branch,
if body != "" && strings.TrimSpace(contentType) != "" {
fmt.Fprintf(&request, "Content-Type: %s\r\n", contentType)
}
if method == "BYE" || method == "CANCEL" {
fmt.Fprintf(&request, "Reason: RELEASE_CAUSE;cause=31;text=\"Normal unspecified\"\r\n")
}
fmt.Fprintf(&request, "Content-Length: %d\r\n\r\n%s", len(body), body)
return request.String()
}
func writeVoiceDialogHeaders(out *strings.Builder, dialog voiceSIPDialog, callID, method, branch string) {
writeVoiceCoreHeaders(out, dialog, callID, method, branch)
writeVoiceOptionalHeader(out, "P-Preferred-Identity", "<"+dialog.localURI+">")
@@ -11,7 +11,7 @@ import (
)
const (
voiceInviteSupported = "100rel, timer, replaces, norefersub, early-session, sec-agree, precondition"
voiceInviteSupported = "100rel, 199, timer, replaces, norefersub, early-session, sec-agree, precondition"
// TS 24.229 5.1.3.1: initial INVITE Accept must include SDP and the 3GPP
// IM CN subsystem XML body MIME type.
voiceInviteAccept = "application/sdp, application/3gpp-ims+xml"
@@ -150,5 +150,6 @@ func buildIMSCancel(agent *Agent, call *Call) (string, error) {
if err != nil {
return "", fmt.Errorf("voice: build CANCEL from INVITE: %w", err)
}
cancel.AppendHeader(sip.NewHeader("Reason", `RELEASE_CAUSE;cause=31;text="Normal unspecified"`))
return cancel.String(), nil
}
@@ -672,6 +672,7 @@ func TestBuildIMSCancelUsesStoredInitialInviteTransaction(t *testing.T) {
"CSeq: 15 CANCEL",
"Max-Forwards: 70",
"Content-Length: 0",
`Reason: RELEASE_CAUSE;cause=31;text="Normal unspecified"`,
} {
if !strings.Contains(cancel, want) {
t.Errorf("CANCEL missing %q: %s", want, cancel)
@@ -744,6 +745,9 @@ func TestBuildIMSBye(t *testing.T) {
if !strings.Contains(bye, "CSeq: 2 BYE") {
t.Errorf("bye missing CSeq: %q", bye)
}
if !strings.Contains(bye, `Reason: RELEASE_CAUSE;cause=31;text="Normal unspecified"`) {
t.Errorf("bye missing RELEASE_CAUSE: %q", bye)
}
}
func TestGatewayLifecycle(t *testing.T) {