If you go to the page at https://blogs.asterisk.org/2018/02/07/identifying-endpoint-pjsip/ it states this:
With several endpoint identifiers available, res_pjsip asks each identifier in turn if can match an endpoint with the request. The first endpoint identified handles the request message. It is possible that more than one endpoint identifier could identify an endpoint for the request. To bring some predictability to which endpoint is recognized, you can specify the order endpoint identifiers check the request with the global endpoint_identifier_order option.
The endpoint_identifier_order option is a comma separated list of endpoint identifier names. The order of the list is the specified order the named identifiers check the request. Any named identifiers not listed are checked last in the order they are registered. Any identifiers that have no name are checked first in the order they are registered.
If you issue the CLI command “pjsip show identifiers” you get the list of endpoint identifiers available on your system in the order they are checked. You can list any of the named endpoint identifiers on the endpoint_identifier_order option.
|*CLI> pjsip show identifiers
name not specified
In fact when I run “pjsip show identifiers” I get the output shown above. It is the order in which they are checked that I believe causes so many problems in moving from Chan SIP to PJSIP. Most people do not want to match on IP address, and checking the IP address first is what probably leads to situations such as the one I first described in FREEPBX-17738, where I noted that if you try to have both a PJSIP trunk and a PJSIP extension coming from the SAME remote IP address, the PJSIP extension stops working. In that ticket, Rob Thomas said this:
"On re-reading, I think I may have figured out what you've done. You've got a phone on IP 188.8.131.52 that's talking to server A on 184.108.40.206 port 5060 as an extension You've then set up a trunk on port 220.127.116.11 port 5060 so that all calls coming from IP address 18.104.22.168 are treated as a trunk call.
Easiest way around that is to use chan_sip for one (port 5160) and pjsip for the other (5060). Then your machine will be able to differentiate."
However to me that seems like an admission of defeat. If you want PJSIP to eventually replace Chan SIP altogether, there has to be a way to deal with situations like this. And my suspicion is that it is this "IP matching first" policy that is the cause of all the problems, possibly even the problems that are being encountered by those trying to get more than one Google Voice account (using the new "sort of SIP" protocol used by Google Voice) to work in Asterisk. It would seem to me that you want endpoints (and trunks) to authenticate using username (or auth_username, not sure what the difference is) first. This will not only help keep trunks and extensions separate, but also help differentiate multiple extensions coming from the same IP address, if that is an issue.
So basically what I'm asking for here is a setting in the PJSIP settings that lets you reorder the Endpoint identification matching priority in Asterisk, so that users can set it to username or auth_username as the first thing to check. Now I may be barking up the wrong tree here, in that I have very little experience with PJSIP, but still it does not seem to me like using PJSIP for your extensions and Chan_SIP for your trunks is a viable long term solution, and it also doesn't address what happens if you want to have multiple endpoints at the same IP address. I don't know whose bright idea it was to try to reuse connections that come in from the same IP address but judging from the number of problems it seems to be causing, I'm not thinking it was a good idea. Of course the Asterisk people will try to say this is not an issue and there are ways to make it work, but I'm not using raw Asterisk, I'm using FreePBX, and this stuff should be easy (or at least as easy as it was with Chan SIP). Of course in Chan SIP you could distinguish between extensions by using separate ports (put extension 100 on port 5060 and extension 101 on port 5061) but PJSIP apparently doesn't let you do that, either.