Why are my routes inverted?

I have a system with an fxo (connected to pstn), Asterisk on the same subnet as the fxo, and an fxs (connected to phone) on a different network from the others.

My sip.conf contains:

[test_fxs]
type=friend
username=test_fxs
secret=passwd
qualify=no
nat=no
;host=dynamic
host=10.1.10.23
canreinvite=no
context=outgoing

[fxo]
type=friend
username=fxo
secret=passwd
qualify=no
nat=no
;host=dynamic
host=192.168.1.23
canreinvite=no
insecure=invite
context=incoming

My extensions.conf contains:

[context]

[incoming]
;exten => 703XX,1,Dial(SIP/test_fxs) ; this failed the same way
exten => 70301,1,Dial(SIP/test_fxs)
exten => 70302,1,Dial(SIP/test_fxs)
exten => 70303,1,Dial(SIP/test_fxs)
exten => 70304,1,Dial(SIP/test_fxs)

[outgoing]
exten => 802XX,1,Dial(SIP/fxo)

When I call in from the pstn, I get a dialtone from the fxo, then dial the remote extension 70301. The fxo successfully sends an INVITE to * and authenticates, but then when * looks up that extension, it finds:

Looking for 70301 in incoming (domain 192.168.1.123)
list_route: hop: sip:80201@192.168.1.23

instead of finding 70301@10.1.10.23.

The end result is that * sends an INVITE to 10.1.10.23 (which is correct), but lists the dest extension as 80201, and the fxs rejects the INVITE with 404 Not Found…

Why is this happening?? I looked at the packet which was sent to the fxs, and the actual destination extension isn’t even included in the message!! What am I doing wrong??

Here are the messages which were exchanged by * and the fxs:

— (14 headers 15 lines)—
Using INVITE request as basis request - 4632561713120001253@192.168.1.23
Sending to 192.168.1.23 : 5060 (non-NAT)
Found peer 'fxo’
Found RTP audio format 4
Found RTP audio format 18
Found RTP audio format 0
Found RTP audio format 96
Peer audio RTP is at port 192.168.1.23:4000
Found description format g723
Found description format g729
Found description format pcmu
Found description format telephone-event
Capabilities: us - 0x8000e (gsm|ulaw|alaw|h263), peer - audio=0x105 (g723|ulaw|g729)/video=0x0 (nothing), combined - 0x4 (ulaw)
Non-codec capabilities: us - 0x1 (telephone-event), peer - 0x1 (telephone-event), combined - 0x1 (telephone-event)
Looking for 70301 in incoming (domain 192.168.1.123)
list_route: hop: sip:80201@192.168.1.23
Transmitting (no NAT) to 192.168.1.23:5060:
SIP/2.0 100 Trying
Via: SIP/2.0/UDP 192.168.1.23;branch=z9hG4bKac463304308;received=192.168.1.23
From: sip:80201@192.168.1.23;tag=1c463256436
To: sip:70301@192.168.1.123;user=phone
Call-ID: 4632561713120001253@192.168.1.23
CSeq: 2 INVITE
User-Agent: Asterisk PBX
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY
Max-Forwards: 70
Contact: sip:70301@192.168.1.123
Content-Length: 0


-- Executing Dial("SIP/fxo-df88", "SIP/test_fxs") in new stack

We’re at 192.168.1.123 port 10246
Adding codec 0x4 (ulaw) to SDP
Adding codec 0x2 (gsm) to SDP
Adding codec 0x8 (alaw) to SDP
Adding non-codec 0x1 (telephone-event) to SDP
13 headers, 12 lines
Reliably Transmitting (no NAT) to 10.1.10.23:5060:
INVITE sip:test_fxs@10.1.10.23 SIP/2.0
Via: SIP/2.0/UDP 192.168.1.123:5060;branch=z9hG4bK31d6a850;rport
From: “80201” sip:80201@192.168.1.123;tag=as6eff556d
To: sip:test_fxs@10.1.10.23
Contact: sip:80201@192.168.1.123
Call-ID: 54d021632de3fa2f316943a044c4aad5@192.168.1.123
CSeq: 102 INVITE
User-Agent: Asterisk PBX
Max-Forwards: 70
Date: Thu, 15 Dec 2005 20:27:34 GMT
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY
Content-Type: application/sdp
Content-Length: 261

v=0
o=root 981 981 IN IP4 192.168.1.123
s=session
c=IN IP4 192.168.1.123
t=0 0
m=audio 10246 RTP/AVP 0 3 8 101
a=rtpmap:0 PCMU/8000
a=rtpmap:3 GSM/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-16
a=silenceSupp:off - - - -


-- Called test_fxs

<-- SIP read from 10.1.10.23:5060:
SIP/2.0 404 Not Found
Via: SIP/2.0/UDP 192.168.1.123:5060;branch=z9hG4bK31d6a850;rport
From: “80201” sip:80201@192.168.1.123;tag=as6eff556d
To: sip:test_fxs@10.1.10.23;tag=1c2020529050
Call-ID: 54d021632de3fa2f316943a044c4aad5@192.168.1.123
CSeq: 102 INVITE
Contact: sip:10.1.10.23
Supported: em,timer,replaces,path
Allow: REGISTER,OPTIONS,INVITE,ACK,CANCEL,BYE,NOTIFY,PRACK,REFER,INFO,SUBSCRIBE,UPDATE
Server: Audiocodes-Sip-Gateway-MP-104 FXS/v.4.40.227.420
Content-Length: 0

— (11 headers 0 lines)—
Transmitting (no NAT) to 10.1.10.23:5060:
ACK sip:test_fxs@10.1.10.23 SIP/2.0
Via: SIP/2.0/UDP 192.168.1.123:5060;branch=z9hG4bK31d6a850;rport
From: “80201” sip:80201@192.168.1.123;tag=as6eff556d
To: sip:test_fxs@10.1.10.23;tag=1c2020529050
Contact: sip:80201@192.168.1.123
Call-ID: 54d021632de3fa2f316943a044c4aad5@192.168.1.123
CSeq: 102 ACK
User-Agent: Asterisk PBX
Max-Forwards: 70
Content-Length: 0


Well, I didn’t get any responses to this anyway, but I resolved what was going on. I didn’t include the destination in my exten statements, so * was just passing test_fxs@10.1.10.23 as the destination, and the fxs was rejecting this with 404 Not Found, which is reasonable I guess. Once I changed my exten statement to:
exten => 70301,1,Dial(SIP/70301@test_fxs)

instead of
exten => 70301,1,Dial(SIP/test_fxs)

my calls went through just fine.

Unfortunately, 703XX does not work, in spite of what the docs say; Asterisk itself then fails with 404 Not Found. That will be awkward when we have dozens of remote sites with 8 or 24 lines each (which we will have), but I’ll have to live with it…