Multiple sip domains

I have an 11.0.1 asterisk box serving as a SIP to PRI PSTN gateway. My SIP end users are in different areas of the country and need different dial rules for local calling. The SIP end users are authenticating to a sipXecs system and in that system I set up the SIP trunk to connect to the 11.0.1 asterisk box for PSTN calling;

phone----->sipXecs------>11.0.1 asterisk------>PSTN

I cannot seem to get asterisk to get the call to the proper context. In my attached debug you can see that sipXecs sent the invite to the correct domain, but asterisk sent the call out the wrong context.

<— SIP read from UDP:172.16.56.239:5080 —>
INVITE sip:5551212@DNSname2.abc123.com;user=phone;transport=udp SIP/2.0
Call-ID: 98bcc3dc-f7fb1fb7-4fcd8762@172.18.24.14-0
CSeq: 1 INVITE
From: “sipxbridge” sip:4042221234@DNSname2.abc123.com;tag=2993627325638113177
To: sip:5551212@DNSname2.abc123.com;user=phone
Via: SIP/2.0/UDP 172.16.56.239:5080;branch=z9hG4bKb076c2bc2dc9929cbcd69ffcdfaa4494313738
Max-Forwards: 70
User-Agent: sipXecs/4.4.0 sipXecs/sipxbridge (Linux)
P-Asserted-Identity: sip:4042221234@voip.abc123.com
Contact: sip:4042221234@172.16.56.239:5080;transport=udp
Route: sip:172.16.56.229:5060;transport=udp;lr
Session-Expires: 1800;refresher=uac
References: 98bcc3dc-f7fb1fb7-4fcd8762@172.18.24.14;rel=chain;sipxecs-tag=request-invite-z9hg4bk-xx-0fe5mjsd8p9d4m3gp0mxltkkmg
Allow: INVITE,BYE,ACK,CANCEL,OPTIONS
Supported: timer
Content-Type: application/sdp
Content-Length: 259

v=0
o=sipxbridge 2687041629021632951 1 IN IP4 172.16.56.239
s=Polycom IP Phone
c=IN IP4 172.16.56.239
t=0 0
a=sendrecv
m=audio 30000 RTP/AVP 9 0 8 101
a=rtpmap:9 G722/8000
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:101 telephone-event/8000
<------------->
— (17 headers 11 lines) —
Sending to 172.16.56.239:5080 (no NAT)
Using INVITE request as basis request - 98bcc3dc-f7fb1fb7-4fcd8762@172.18.24.14-0
Found peer ‘DNSname1’ for ‘4042221234’ from 172.16.56.239:5080
== Using SIP RTP TOS bits 184
== Using SIP RTP CoS mark 5
Found RTP audio format 9
Found RTP audio format 0
Found RTP audio format 8
Found RTP audio format 101
Found audio description format G722 for ID 9
Found audio description format PCMU for ID 0
Found audio description format PCMA for ID 8
Found audio description format telephone-event for ID 101
Capabilities: us - (ulaw), peer - audio=(ulaw|alaw|g722)/video=(nothing)/text=(nothing), combined - (ulaw)
Non-codec capabilities (dtmf): us - 0x1 (telephone-event|), peer - 0x1 (telephone-event|), combined - 0x1 (telephone-event|)
Peer audio RTP is at port 172.16.56.239:30000
Looking for 5551212 in DNSname1 (domain DNSname2.abc123.com)
list_route: hop: sip:4042221234@172.16.56.239:5080;transport=udp

<— Transmitting (no NAT) to 172.16.56.239:5080 —>
SIP/2.0 100 Trying
Via: SIP/2.0/UDP 172.16.56.239:5080;branch=z9hG4bKb076c2bc2dc9929cbcd69ffcdfaa4494313738;received=172.16.56.239
From: “sipxbridge” sip:4042221234@DNSname2.abc123.com;tag=2993627325638113177
To: sip:5551212@DNSname2.abc123.com;user=phone
Call-ID: 98bcc3dc-f7fb1fb7-4fcd8762@172.18.24.14-0
CSeq: 1 INVITE
Server: Asterisk PBX 11.0.1
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH
Supported: replaces, timer
Session-Expires: 1800;refresher=uac
Contact: sip:5551212@172.16.56.229:5060
Content-Length: 0

<------------>
– Executing [5551212@DNSname1:1] Dial(“SIP/DNSname1-0000003c”, “DAHDI/r1/5551212”) in new stack
– Requested transfer capability: 0x00 - SPEECH
– Called DAHDI/r1/5551212
– DAHDI/i1/5551212-342 is proceeding passing it to SIP/DNSname1-0000003c

extensions.conf snippet;

[DNSname1]
exten => _NXXXXXX,1,Dial(DAHDI/r1/${EXTEN})
exten => _1NXXNXXXXXX,1,Dial(DAHDI/r1/${EXTEN})

[DNSname2]
exten => _NXXXXXX,1,Dial(DAHDI/r1/${EXTEN})
exten => _1NXXNXXXXXX,1,Dial(DAHDI/r1/${EXTEN})

sip.conf snippet;

[general]

port = 5060 ; Port to bind to (SIP is 5060)
bindaddr = 0.0.0.0 ; Address to bind to (all addresses on machine)
nat = no
localnet=172.16.0.0/12
tos_sip=ef
tos_audio=ef
;allowexternaldomains=no
;allowguest=no
;realm=asterisk
domain=DNSname2.abc123.com,DNSname2
domain=DNSname1.abc123.com,DNSname1
domain=172.16.56.229,internal

[DNSname2]
;From elessar.voip.abc123.com to this box
;domain=DNSname2.abc123.com,DNSname2
context=DNSname2
host=dynamic
secret=5220ast
canreinvite=yes
insecure=port,invite
qualify=yes
trunk=yes
dtmfmode=rfc2833
type=friend
disallow=all
allow=ulaw
ignoresdpversion=yes

[DNSname1]
;From elessar.voip.abc123.com to this box
;domain=DNSname1.abc123.com,DNSname1
context=DNSname1
host=dynamic
secret=5220ast
canreinvite=yes
insecure=port,invite
qualify=yes
trunk=yes
dtmfmode=rfc2833
type=friend
disallow=all
allow=ulaw
;ignoresdpversion=yes

If both registrations are from the same address, Asterisk will not be able to tell them apart, as it matches by IP address. You would probably have to do type=user matches, on the From address, and use some other means for CLID.

You might find it easier to modify the To address and route within a single dialplan context.

Note that insecure=invite makes very little sense with host=dynamic, as the remote system must know and use the password in order to register. Very few systems need insecure=port. I’m not clear why you might need host=dynamic; surely you know the address of the proxy?

If you can control the source port, you might be able to use that to discriminate, but you will definitely have to remove the insecure=port.