mirror of
https://github.com/yibaiba/hideck.git
synced 2026-12-05 16:02:17 +00:00
fix(ims): wait for the protected SMS push flow
This commit is contained in:
1 parent
5c66109462
commit
74dbd5ae22
11 files changed
+117
-44
No files matched your search
@@ -16,8 +16,8 @@ func TestProtectedSIPStreamClosureInvalidatesRegistration(t *testing.T) {
|
||||
client, server := net.Pipe()
|
||||
defer server.Close()
|
||||
service.activateProtectedRegistrationTCP(client)
|
||||
if readiness := service.SMSReadiness(); !readiness.Ready {
|
||||
t.Fatalf("initial readiness = %+v", readiness)
|
||||
if readiness := service.SMSReadiness(); !readiness.TransportReady {
|
||||
t.Fatalf("initial transport readiness = %+v", readiness)
|
||||
}
|
||||
if err := server.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -367,8 +367,12 @@ func (s *Service) trackProtectedConnection(conn net.Conn) bool {
|
||||
return false
|
||||
default:
|
||||
s.protectedConns[conn] = struct{}{}
|
||||
changed := !s.portSPushReady.Swap(true)
|
||||
s.protectedConnMu.Unlock()
|
||||
s.cancelPortSReconnectWatch()
|
||||
if changed {
|
||||
s.notifySMSReadiness()
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -376,7 +380,12 @@ func (s *Service) trackProtectedConnection(conn net.Conn) bool {
|
||||
func (s *Service) untrackProtectedConnection(conn net.Conn) {
|
||||
s.protectedConnMu.Lock()
|
||||
delete(s.protectedConns, conn)
|
||||
empty := len(s.protectedConns) == 0
|
||||
changed := empty && s.portSPushReady.Swap(false)
|
||||
s.protectedConnMu.Unlock()
|
||||
if changed {
|
||||
s.notifySMSReadiness()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) readRegistrationStream(conn net.Conn) {
|
||||
|
||||
@@ -16,7 +16,9 @@ func (s *Service) maybeNotifySMSReady(reason string) {
|
||||
return
|
||||
}
|
||||
s.mu.Lock()
|
||||
pushReady := !s.protectedSMSPushRequiredLocked() || s.portSPushReady.Load()
|
||||
ready := !s.smsReadyNotified && s.smsReceiverReady &&
|
||||
pushReady &&
|
||||
strings.TrimSpace(s.cfg.SMSC) != "" &&
|
||||
s.regStatus.Load() == registrationRegistered
|
||||
if !ready {
|
||||
|
||||
@@ -144,6 +144,28 @@ func TestNotifySMSMemoryAvailableRetriesRejectedFinalResponses(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRejectedRPReportDoesNotSendMemoryAvailable(t *testing.T) {
|
||||
service, _, _ := newInboundSMSTestService(t)
|
||||
var kinds []smscodec.RPDUKind
|
||||
service.transport.SetSendFn(func(request string) error {
|
||||
body, err := rawSIPBody(request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kinds = append(kinds, smscodec.ClassifyRPDU(body).Kind)
|
||||
service.transport.DeliverResponse(registerResponseForRequest(request, 488, nil))
|
||||
return nil
|
||||
})
|
||||
raw := inboundSMSRequest(t, imsSMSContentType,
|
||||
inboundRPData(t, 0x41, "+447700900123", "rejected report"))
|
||||
service.sendRPReportWithRetry(rpReportRequest{
|
||||
Inbound: raw, Body: smscodec.BuildRPAck(0x41), RPMR: 0x41,
|
||||
})
|
||||
if len(kinds) != 1 || kinds[0] != smscodec.RPDUKindAck {
|
||||
t.Fatalf("outbound RPDU kinds = %v, want only RP-ACK", kinds)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotifySMSMemoryAvailableAbortOnStop(t *testing.T) {
|
||||
service, _, _ := newInboundSMSTestService(t)
|
||||
service.rememberSMSMemoryDenied(
|
||||
|
||||
@@ -44,9 +44,6 @@ const (
|
||||
rpReportInitialDelay = 0
|
||||
rpReportRetryDelay = time.Second
|
||||
rpReportMaxAttempts = 4
|
||||
// One prompt covers every message the SMSC releases in the same flush,
|
||||
// so rate limit it rather than sending one per rejected report.
|
||||
smmaPromptMinInterval = time.Minute
|
||||
)
|
||||
|
||||
type mtAckAudit struct {
|
||||
@@ -154,45 +151,9 @@ func (s *Service) sendRPReportWithRetry(report rpReportRequest) {
|
||||
"IMS RP report delivery failed",
|
||||
"device", deviceID, "attempts", attempts,
|
||||
"rp_mr", int(report.RPMR), "error", err)
|
||||
s.promptSMSCRedeliveryAfterRejectedReport(err)
|
||||
}
|
||||
}
|
||||
|
||||
// A report the gateway rejects leaves the SMSC believing the short message is
|
||||
// still outstanding. Measured on 2026-09-03: one message was redelivered three
|
||||
// times over 24 minutes on a rejection every time, on two different P-CSCF
|
||||
// instances and with a report byte-identical in structure to ones the same pool
|
||||
// accepts, and nothing else was delivered while it stayed at the head. RP-SMMA
|
||||
// is the only receiver-initiated way to ask for the queue (TS 24.011 clears
|
||||
// MCEF and alerts the service centre), so send one instead of waiting out a
|
||||
// retransmission timer that had already backed off past 18 minutes.
|
||||
func (s *Service) promptSMSCRedeliveryAfterRejectedReport(reportErr error) {
|
||||
if rpReportRejectStatus(reportErr) == 0 {
|
||||
return
|
||||
}
|
||||
if s == nil || s.stopped() || !s.reserveSMMAPrompt(time.Now()) {
|
||||
return
|
||||
}
|
||||
s.smsSendMu.Lock()
|
||||
err := s.sendRPSMMAWithRetryPolicy(rpReportInitialDelay, rpReportRetryDelay)
|
||||
s.smsSendMu.Unlock()
|
||||
if err != nil {
|
||||
logging.WarnRate("smsip-smma-prompt-"+s.DeviceID(), 30*time.Second,
|
||||
"IMS RP-SMMA redelivery prompt failed",
|
||||
"device", s.DeviceID(), "error", err)
|
||||
return
|
||||
}
|
||||
logging.Info("IMS RP-SMMA sent to release the SMSC queue", "device", s.DeviceID())
|
||||
}
|
||||
|
||||
func (s *Service) reserveSMMAPrompt(now time.Time) bool {
|
||||
last := s.smmaPromptLastAt.Load()
|
||||
if last != 0 && now.Sub(time.Unix(0, last)) < smmaPromptMinInterval {
|
||||
return false
|
||||
}
|
||||
return s.smmaPromptLastAt.CompareAndSwap(last, now.UnixNano())
|
||||
}
|
||||
|
||||
func (s *Service) sendRPReportWithRetryPolicy(
|
||||
report rpReportRequest,
|
||||
initialDelay time.Duration,
|
||||
|
||||
@@ -23,6 +23,9 @@ func (s *Service) SMSReadiness() SMSReadiness {
|
||||
strings.TrimSpace(s.regSession.contactUser) != ""
|
||||
transportReady := registered && s.registeredSIPTransportReadyLocked()
|
||||
receiverReady := s.smsReceiverReady
|
||||
if s.protectedSMSPushRequiredLocked() {
|
||||
receiverReady = receiverReady && s.portSPushReady.Load()
|
||||
}
|
||||
smsc := ""
|
||||
if s.cfg != nil {
|
||||
smsc = s.cfg.SMSC
|
||||
@@ -31,6 +34,13 @@ func (s *Service) SMSReadiness() SMSReadiness {
|
||||
return evaluateSMSReadiness(registered, profileReady, transportReady, receiverReady, smsc)
|
||||
}
|
||||
|
||||
func (s *Service) protectedSMSPushRequiredLocked() bool {
|
||||
return !s.externalTransport && !s.portSRecoveryRejected.Load() &&
|
||||
s.regSession != nil &&
|
||||
s.regSession.security != nil &&
|
||||
strings.TrimSpace(s.regSession.security.verifyHeader) != ""
|
||||
}
|
||||
|
||||
// SetOnSMSReadinessChanged installs the readiness observer and immediately
|
||||
// publishes a snapshot so callers cannot miss startup transitions.
|
||||
func (s *Service) SetOnSMSReadinessChanged(fn func(SMSReadiness)) {
|
||||
|
||||
@@ -2,11 +2,71 @@ package imscore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/iniwex5/vowifi-go/internal/vowifi/smsdelivery"
|
||||
)
|
||||
|
||||
func TestProtectedSMSReadinessRequiresPortSFlow(t *testing.T) {
|
||||
service := newProtectedKeepaliveTestService(t)
|
||||
registration, registrationPeer := net.Pipe()
|
||||
push, pushPeer := net.Pipe()
|
||||
t.Cleanup(func() {
|
||||
_ = registrationPeer.Close()
|
||||
_ = pushPeer.Close()
|
||||
})
|
||||
service.mu.Lock()
|
||||
service.registrationTCP = registration
|
||||
service.registrationTCPProtected = true
|
||||
service.mu.Unlock()
|
||||
|
||||
if got := service.SMSReadiness(); got.ReceiverReady || got.Ready {
|
||||
t.Fatalf("readiness without port-s = %+v", got)
|
||||
}
|
||||
if !service.trackProtectedConnection(push) {
|
||||
t.Fatal("track port-s")
|
||||
}
|
||||
if got := service.SMSReadiness(); !got.ReceiverReady || !got.Ready {
|
||||
t.Fatalf("readiness with port-s = %+v", got)
|
||||
}
|
||||
service.untrackProtectedConnection(push)
|
||||
if got := service.SMSReadiness(); got.ReceiverReady || got.Ready {
|
||||
t.Fatalf("readiness after port-s closed = %+v", got)
|
||||
}
|
||||
service.portSRecoveryRejected.Store(true)
|
||||
if got := service.SMSReadiness(); !got.ReceiverReady || !got.Ready {
|
||||
t.Fatalf("on-demand port-s listener readiness = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProtectedSMSReadyCallbackWaitsForPortSFlow(t *testing.T) {
|
||||
service := newProtectedKeepaliveTestService(t)
|
||||
service.regStatus.Store(registrationRegistered)
|
||||
registration, registrationPeer := net.Pipe()
|
||||
push, pushPeer := net.Pipe()
|
||||
t.Cleanup(func() {
|
||||
_ = registrationPeer.Close()
|
||||
_ = pushPeer.Close()
|
||||
})
|
||||
service.mu.Lock()
|
||||
service.registrationTCP = registration
|
||||
service.registrationTCPProtected = true
|
||||
service.mu.Unlock()
|
||||
|
||||
called := 0
|
||||
service.SetOnSMSReady(func() { called++ })
|
||||
if called != 0 {
|
||||
t.Fatalf("callback before port-s = %d", called)
|
||||
}
|
||||
if !service.trackProtectedConnection(push) {
|
||||
t.Fatal("track port-s")
|
||||
}
|
||||
if called != 1 {
|
||||
t.Fatalf("callback after port-s = %d, want 1", called)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateSMSReadinessRequiresEveryPrerequisite(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -110,7 +110,6 @@ type messagingRuntime struct {
|
||||
smmaSent map[string]time.Time
|
||||
|
||||
moSelfHealLastAt atomic.Int64
|
||||
smmaPromptLastAt atomic.Int64
|
||||
lastMTAckMu sync.Mutex
|
||||
}
|
||||
|
||||
|
||||
@@ -184,6 +184,7 @@ type Service struct {
|
||||
protectedConns map[net.Conn]struct{}
|
||||
portSReconnectGrace time.Duration
|
||||
portSLastReadAt atomic.Int64
|
||||
portSPushReady atomic.Bool
|
||||
portSRecoveryPending atomic.Bool
|
||||
portSRecoveryRejected atomic.Bool
|
||||
portSWatchMu sync.Mutex
|
||||
|
||||
@@ -236,6 +236,14 @@ func TestConfigureIMSTCPKeepalive(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIMSKeepaliveDeadFlowDetectionFitsSMSRecoveryWindow(t *testing.T) {
|
||||
detectionWindow := imsTCPKeepaliveIdle +
|
||||
imsTCPKeepaliveInterval*time.Duration(imsTCPKeepaliveProbes)
|
||||
if detectionWindow > time.Minute {
|
||||
t.Fatalf("dead-flow detection window = %s, want at most 1m", detectionWindow)
|
||||
}
|
||||
}
|
||||
|
||||
func assertIPv4UDPPacket(t *testing.T, packet []byte, destination net.IP, payload []byte) {
|
||||
t.Helper()
|
||||
if len(packet) < 28 || packet[0]>>4 != 4 {
|
||||
|
||||
@@ -22,9 +22,10 @@ const (
|
||||
imsIPv4LinkMTU = imsTCPMSS + header.IPv4MinimumSize + header.TCPMinimumSize
|
||||
imsIPv6LinkMTU = header.IPv6MinimumMTU
|
||||
tcpListenBacklog = 4096
|
||||
// Port-s has no SIP CRLF; keep both IMS TCP 5-tuples at 30s idle.
|
||||
// Port-s has no SIP CRLF. Probe after 30s of silence, then retry quickly
|
||||
// enough to expose a dead reverse flow before the SMSC attempts delivery.
|
||||
imsTCPKeepaliveIdle = 30 * time.Second
|
||||
imsTCPKeepaliveInterval = 30 * time.Second
|
||||
imsTCPKeepaliveInterval = 5 * time.Second
|
||||
imsTCPKeepaliveProbes = 3
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user