Inbound route restricted to only xyz area code

I am looking at a way to restrict calls to my 800# dids to only callers in the canada area codes. I have read a few posts on this but none has been helpful so i figured I would ask. I am running freepbx 2.7 on asterisk 1.6 (PBXinaFlash)

I have tried setting the cid field to _arecodeNXXXXXX though ti says not a proper cid and I need to add about 20 of them to cover all canada.

Can anyone help shed some light on this.

Thanks

First of all I’d advise you to think long and hard before you do this. Today you absolutely cannot tell where a person is located by the area code of the calling number. With both VoIP and cell phone service, it’s possible for a call to come in from an area code that’s nowhere near the actual location of the caller. People get a cell phone in one area code, then move across the country but keep their original number because everyone knows that number. And yes, they may even be coming in from a U.S. area code although they are physically located in Canada.

That said, this is probably easiest to do in /etc/asterisk/extensions_custom.conf. You’ll have to know the pattern of Caller ID numbers as they arrive from your provider - are they ten digits, eleven digits, or eleven digits with a + in front? Assuming they are ten digits (judging by what you’ve already tried) then you could do something like this:

Create a new custom context that looks something like this:

[from-tollfree-custom]
exten => _X!,1,NoOp(Incoming toll free call from area code ${CALLERID(num):0:3})
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “204”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “226”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “250”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “289”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “306”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “343”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “403”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “416”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “418”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “438”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “450”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “506”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “514”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “519”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “581”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “587”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “604”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “613”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “647”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “705”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “709”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “778”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “780”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “807”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “819”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “867”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “902”]?canadacall)
exten => _X!,n,GotoIf($["${CALLERID(num):0:3}" = “905”]?canadacall)
exten => _X!,n,Goto(app-blackhole,congestion,1)
exten => _X!,n(canadacall),Goto(from-trunk,${EXTEN},1)
exten => h,1,Macro(hangupcall,)

After you have added this to extensions_custom.conf, change the context on your trunk from from-trunk to from-tollfree-custom.

Note that the above is slightly inefficient because it re-evaluates the Caller ID number to extract the area code in every line. I think you could make it a bit faster by adding an additional line right after the top line:

exten => _X!,n,Set(AreaCode=${CALLERID(num):0:3})

And then in all the following lines, change all instances of ${CALLERID(num):0:3} to ${AreaCode}. I don’t know if the time saved would be worth the effort, though.

If you REALLY need speed it might be possible to speed this up even further by doing an AGI call, but that’s a bit more involved.

If for some reason this isn’t helpful then you may want to ask in the PiaF forums, since I’ve observed you’re often more likely to get an answer there.

I will give this a shout tonight. This is the best answer I got. The one I got on the PBXinaFlash one was ask your provider to do it. Which I did but they can only block canada.

you should be able to do this with an inbound route using the CID field and a pattern, though you are correct there will be a lot routes.

Also … if you have a catchall route, it will end up catching this so you would want to have one last route for this specific DID with no CID and make sure to route it somewhere like Terminate Call.

However, for this sort of thing, I tend to agree with sir_sip that it is easier done in a custom configuration file, it creates a lot less clutter in the GUI.

You may also want to check a couple of the inbound routing modules in the contributed_modules directory though, they may help here, as might the bulkdid module if you choose to just create/maintain a bunch of routes.