Summary:ASTERISK-19719: Asterisk doesn't add Contact field in 200 OK when ACK for 401 response is out of order
Reporter:Andrey Solovyev (corruptor)Labels:
Date Opened:2012-04-13 09:03:21Date Closed:
Versions: 13.18.4 Frequency of
Environment:Attachments:( 0) pedantic_contact_bug.txt
( 1) pedantic_yes.txt
( 2) sip.conf
( 3) uac_auth_late_ack.xml
( 4) users.csv
Description:We have the Asterisk server which is used by many different SIP devices so we have to use pedantic=no because some devices can send some strange SIP packets.
One of our users has asterisk server and noticed that some calls are hanged up after answer by his asterisk (also 1.8.11) with error:
chan_sip.c: Invalid contact uri  (missing sip: or sips:), attempting to use anyway
chan_sip.c: Invalid host name in Contact: (can't resolve in DNS) : ''

I've started debugging and found out that our asterisk doesn't always send Contact field in the SIP 200 OK message! Actually about 15% calls were without Contact field in SIP Session progress and SIP OK messages which caused client's asterisk to hangup call because Contact field MUST be present (as per RFC).

I have found out that asterisk doesn't send Contact due to wrong packet order in authorization process.
Usually we have such sip dialog:
client  asterisk
1. --> Invite
2. <- 401 Unauthorized
3. -> ACK
4. -> Invite with auth
5. <-100 Trying
6. <- 183 Session Progress (or 180 Ringing or both)
7. <- 200 OK

In our particular issue due to some network latancy or maybe some unknown network issues 4th packet came earlier then 3rd and we've got this scenario (this is from our asterisk side, it always sends Trying before handling any other packet in the dialog)
1. -> Invite
2. <- 401 Unauthorized
4. -> Invite with auth
5. <- 100 Trying
3. -> ACK
6. <- 183 Session Progress (or 180 Ringing or both)
7. <- 200 OK

This is UDP and such situation can happen to anyone because client's "ACK" (3rd packet) and "Invite with auth" can be sent just microseconds away from each other and "Invite with auth" can reach the destination earlier than ACK.
So ACK comes after INVITE with auth. Then asterisk sends 183 and 200 OK packet without Contact field wich seems strange.
The Contact field is there if we enable pedantic  (pedantic = yes) in sip.conf general section, although packets order is the same.

I've managed to reproduce the situation above with SIPp scenario.
I've attached sip.conf which I've used. Basically it contains one sip friend with context test11.
test11 context simulates call with session progress and answer and contains:
exten => _X.,1,Noop()
      same => n,Wait(1)
      same => n,Progress()
      same => n,Wait(2)
      same => n,Answer()
      same => n,Playback(demo-congrats)

I've created SIPp scenario uac_auth_late_ack.xml wich simulates ACK after Invite with auth situation. It uses users.csv with auth credentials. In order to simulate call you have to execute this command
sipp -sf uac_auth_late_ack.xml -s 11111 -aa -d 5s -i -inf users.csv -r 1 -rp 1s -m 1 - is asterisk server - client's side (SIPp)

pedantic_contact_bug.txt contains asterisk DEBUG output with sip debug enabled. We can see that 200 OK from asterisk (line number 382) doesn't contain Contact field.
pedantic_yes.txt is same scenario with pedantic=yes. We can see that OK field contains Contact field.

The difference is that with pedantic=yes asterisk treats late ACK as "Invalid SIP message - rejected , no callid, len 309" (line 288 pedantic_yes.txt) and if we have pedantic=no asterisk ignores this packet "Ignoring too old SIP packet packet 1 (expecting >= 2) (pedantic_contact_bug.txt line 304).
(I would like to add that I've also tested scenario with normal packet order to be sure that all packets are right).

IMHO such scenario should work and this seems to be a bug.
Comments:By: Jan-Peter Koopmann (j.koopmann) 2012-10-01 12:42:32.731-0500

We see a similar behaviour and I second this being a bug.

By: Etienne Lessard (hexanol) 2013-05-07 13:47:13.647-0500

We are having the same issue (i.e. no Contact field in 200 OK response when ACK for 401 response is received after second INVITE is received).

In fact, I would say that this issue is the same as ASTERISK-17258, or at least it is strongly related.

Note that we are having this issue on asterisk 11.3.0.