I have been trying to figure this out for three nights now and it’s driving me crazy. Under Chan_SIP, I can have an extension AND two trunks associated with the SAME Obihai device at the SAME local IP address. Each of the three has a unique username, the extension is an extension number and is treated like a regular extension, whereas the trunks are also identified by unique usernames which are (in Chan_SIP) matched in the Trunk Name settings. Both trunks are used for incoming calls to the PBX but are handled very differently (for example one goes to the from-internal context while the other goes to a custom context that eventually winds up at the from-pstn-e164-us context). I mention this only to point out that the trunks cannot be combined, but I don’t think that’s the real problem anyway.
If I convert the extension to PJSIP and disable the trunks it works exactly as it should for calls to and from the extension. But the minute I add in PJSIP trunks to replace the Chan_SIP ones, the extension either stops working altogether or won’t receive incoming calls. If you attempt to place a call from the extension, it tries to go the the trunk that winds up at the from-pstn-e164-us context and that fails because that’s the wrong context. I’m still not sure why calls to the extension fail, but they do. I have tried changing various settings but as far as I can tell the big problem is that PJSIP cannot properly determine whether an incoming call or outgoing call from/to that IP address should be considered to be a call from/to the extension or one of the two trunks. My suspicion is that once one of the trunks is enabled it just totally fails to reccognize the extension anymore.
In trying to figure this out I stumbled across an interesting configuration issue. There is a file in the /etc/asterisk directory called pjsip_identify.conf that is generated by FreePBX, and for extensions there are no entries whatsoever. But every PJSIP trunk has an entry such as this:
[trunk_name]
type=identify
endpoint=trunk_name
match=sip_server_from_pjsip_settings
If I am understanding this correctly, it is telling Asterisk that if it sees a call from the address specified in the match field, it should associate it with the trunk named in the context name and endpoint setting. It appears this context is auto-generated when you create a FreePBX trunk and there is no way to change it. And if it is doing what I think it is, then it is ignoring and overriding both the default Endpoint Identifier Order specified in the Chan PJSIP Settings, as well as any expliit “Match Inbound Authentication” setting in the trunk itself (in both cases I have set those to match on “Auth Username” first, also tried using “Username” first).
The problem is that I can’t really test this theory for two reasons: First, it appears that FreePBX neither lets you remove or modify those generated entries in pjsip_identify.conf. But even if it did, I can’t find any reference that shows how you’d match on “Auth Username” or “Username” (not sure which is technically correct) in the pjsip_identify.conf file. FreePBX does have pjsip_identify_custom.conf and pjsip_identify_custom_override.conf files, both of which are currently empty, but again without knowing what a proper entry should look like to match on the trunk name rather than the IP address I don’t know how you’d use those.
I think that when creating the pjsip_identify.conf file, either FreePBX should honor the default Endpoint Identifier Order specified in the Chan PJSIP Settings or any expliit “Match Inbound Authentication” setting in the trunk itself, or perhaps maybe there should be a new setting to explicitly set the match type in the pjsip_identify.conf file. Unless I am totally barking up the wrong tree here, in which case I have difficulty understanding why PJSIP has so much trouble sorting out traffic from the same local IP address based on extension name/trunk name, when Chan_SIP handles that same situation like it’s a walk in the park. I do note that if you have two extensions coming from the same device (a 2-line VoIP adapter) the PBX has no issue keeping those straight, which again is what makes me think those entries in the pjsip_identify.conf file that appear for trunks only may be the problem.
Does anyone know how I can resolve this so that Asterisk can keep the traffic straight? Is there a way to keep FreePBX from generating that entry in pjsip_identify.conf for a particular trunk, or to modify it so it matches on the trunk name? Or, is there a better reference for PJSIP that actually explains how to use the pjsip_identify*.conf files (particularly pjsip_identify_custom.conf and pjsip_identify_custom_override.conf) in a way that might give me a workaround for this situation?
Edit: I created an issue here: https://issues.freepbx.org/browse/FREEPBX-20801