Asterisk ignores challenge for digest auth, reg fails

I’m experiencing a weird problem trying to register to providers that use digest authentication for SIP.
Basically, sometimes it works, but after a while (from 10 mins to hours) registration will fail. After then, If I change the port to which Asterisk binds to for SIP, it will work again, and again fail after a while. And so on.

Trying to debug the problem, I found that registration fails because Asterisk does not respond to the first 401 Unauthorized with an Authorization header (required for digest auth), but keeps trying without it.

For clarity, here’s a snip of sip debug for both succesful and unsuccesful register negotiations (I highlighted interesting parts).

Failing registration:

asterisk will then keep trying to resend the same request (retransmitting #3, #4 and so on), and every time the server will answer 401 (obviously).

And here’s the succesful registration:

any idea about what may be responsible for this? Asterisk is behind NAT with no port forwarding, could this be the problem? if yes, why does registration succeed with a “fresh” port? and why doesn’t a router reset help? the only way I found to make it temporarily work is changing the SIP port. And I have no problems with providers that use proxy authentication.

Hmmm… try forwarding ports. Also, do you have in [general], externip= localnet= and nat=yes set?

Note that in the failed one it sets ‘contact’ as a 192.168 address, thats usually bad…