Asterisk 401 Unauthorized when trying to register sip clients

asterisksip

Trying to register a sip client to my asterisk server often (just about 90% of the times, not always, weirdly) results in 401 Unauthorized errors.

This is the config for one of the extensions:

[11]
deny=0.0.0.0/0.0.0.0
secret=xxxxxxxxxxxxxxxxxxxx
dtmfmode=rfc2833
canreinvite=no
context=from-internal
host=dynamic
trustrpid=yes
sendrpid=no
type=friend
nat=no
port=5060
qualify=yes
qualifyfreq=60
transport=udp
avpf=no
icesupport=no
encryption=no
callgroup=
pickupgroup=
dial=SIP/11
mailbox=11@default
permit=0.0.0.0/0.0.0.0
callerid=Caller <11>
callcounter=yes
faxdetect=no

Excerpt from the log:

<--- SIP read from UDP:192.168.178.69:60686 --->
REGISTER sip:192.168.178.26 SIP/2.0
Via: SIP/2.0/UDP 192.168.178.69:60686;rport;branch=z9hG4bKPj7IVefnk0j6Wn9oUM78ubmcURGDehvKEc
Route: <sip:192.168.178.26;lr>
Max-Forwards: 70
From: <sip:12@192.168.178.26>;tag=XOO-LeGIwZmwa2UROKMXEhZGA5mKcY0b
To: <sip:12@192.168.178.26>
Call-ID: p8gpcmxSdWwcM5xV89nm2LkEbcTPUdT1
CSeq: 62833 REGISTER
User-Agent: CSipSimple_g2-17/r2330
Contact: <sip:12@192.168.178.69:60686;ob>
Expires: 900
Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
Content-Length: 0

<------------->
--- (13 headers 0 lines) ---
[2014-02-08 14:54:00] DEBUG[2922]: acl.c:979 ast_ouraddrfor: For destination '192.168.178.69', our source address is '192.168.178.26'.
[2014-02-08 14:54:00] DEBUG[2922]: chan_sip.c:4031 ast_sip_ouraddrfor: Setting SIP_TRANSPORT_UDP with address 192.168.178.26:5060
Sending to 192.168.178.69:60686 (NAT)
[2014-02-08 14:54:00] DEBUG[2922]: chan_sip.c:8764 sip_alloc: Allocating new SIP dialog for p8gpcmxSdWwcM5xV89nm2LkEbcTPUdT1 - REGISTER (No RTP)
Sending to 192.168.178.69:60686 (NAT)

<--- Transmitting (no NAT) to 192.168.178.69:60686 --->
SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP 192.168.178.69:60686;branch=z9hG4bKPj7IVefnk0j6Wn9oUM78ubmcURGDehvKEc;received=192.168.178.69;rport=60686
From: <sip:12@192.168.178.26>;tag=XOO-LeGIwZmwa2UROKMXEhZGA5mKcY0b
To: <sip:12@192.168.178.26>;tag=as68275e50
Call-ID: p8gpcmxSdWwcM5xV89nm2LkEbcTPUdT1
CSeq: 62833 REGISTER
Server: FPBX-2.11.0(11.6.0)
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH
Supported: replaces, timer
WWW-Authenticate: Digest algorithm=MD5, realm="asterisk", nonce="7cdb20a0"
Content-Length: 0

Complete log of one registration attempt until the timeout message appears on the client:
http://pastebin.com/sqjAqa2T

I have already tried the methods mentioned in this question: Asterisk SIP/2.0 401 Unauthorized

How can I make the registrations work properly?

Best Answer

You don't have a username= set in your extension (often, just the extension number - it doesn't matter what it is, it just needs to match the client)

The log sample you have shows an extension 12 trying to register, and not sending any Authorization header, which looks odd. CSipSimple works fine for me using the extention number as the username (and of course the password - whatever secret= is set to for that extension).