@@ -76,6 +76,63 @@ func hashPassword(password string) string {
7676 return hex .EncodeToString (hash [:8 ]) // Use first 8 bytes for shorter hash
7777}
7878
79+ type inboundCallInfo struct {
80+ sync.Mutex
81+ cseq uint32
82+ cseqAuth uint32
83+ invites uint32
84+ invitesAuth uint32
85+ }
86+
87+ func inviteHasAuth (r * sip.Request ) bool {
88+ return r .GetHeader ("Proxy-Authorization" ) != nil ||
89+ r .GetHeader ("Authorization" ) != nil
90+ }
91+
92+ func (c * inboundCallInfo ) countInvite (log logger.Logger , req * sip.Request ) {
93+ hasAuth := inviteHasAuth (req )
94+ cseq := req .CSeq ()
95+ if cseq == nil {
96+ return
97+ }
98+ c .Lock ()
99+ defer c .Unlock ()
100+ cseqPtr := & c .cseq
101+ countPtr := & c .invites
102+ name := "invite"
103+ if hasAuth {
104+ cseqPtr = & c .cseqAuth
105+ countPtr = & c .invitesAuth
106+ name = "invite with auth"
107+ }
108+ if * cseqPtr == 0 {
109+ * cseqPtr = cseq .SeqNo
110+ }
111+ if cseq .SeqNo > * cseqPtr {
112+ return // reinvite
113+ }
114+ * countPtr ++
115+ if * countPtr > 1 {
116+ log .Warnw ("remote appears to be retrying an " + name , nil , "invites" , * countPtr , "cseq" , * cseqPtr )
117+ }
118+ }
119+
120+ func (s * Server ) getCallInfo (id string ) * inboundCallInfo {
121+ c , _ := s .infos .byCallID .Get (id )
122+ if c != nil {
123+ return c
124+ }
125+ s .infos .Lock ()
126+ defer s .infos .Unlock ()
127+ c , _ = s .infos .byCallID .Get (id )
128+ if c != nil {
129+ return c
130+ }
131+ c = & inboundCallInfo {}
132+ s .infos .byCallID .Add (id , c )
133+ return c
134+ }
135+
79136func (s * Server ) getInvite (sipCallID string ) * inProgressInvite {
80137 s .imu .Lock ()
81138 defer s .imu .Unlock ()
@@ -118,6 +175,8 @@ func (s *Server) handleInviteAuth(log logger.Logger, req *sip.Request, tx sip.Se
118175 if h := req .CallID (); h != nil {
119176 sipCallID = h .Value ()
120177 }
178+ ci := s .getCallInfo (sipCallID )
179+ ci .countInvite (log , req )
121180 inviteState := s .getInvite (sipCallID )
122181 log = log .WithValues ("inviteStateSipCallID" , sipCallID )
123182
0 commit comments