VoIPowering Your Office with Asterisk: SOHO VoIP, Part 3

In Parts
and 2
we got as far as connecting four PSTN lines to our Asterisk server, setting
up extensions for three users, and routing inbound calls to one of the users.
Today we’ll configure outbound calling. This must be set up with care, because
you don’t want to leave your network open to anyone who wants free long-distance
at your expense. We’ll build on what we did in the first two parts, but with
a few changes.

New contexts
An important function of Asterisk’s contexts is to define and limit calling
privileges. At a minimum, you need two contexts: one inbound, and one for your
internal users. Do not allow external calling privileges on inbound contexts,
unless you wish to provide free toll calling to the world.

We’re going to make a clean new extensions.conf. The sample file is full of useful comments and is good to study. But it’s also full of stuff we don’t want.

Contexts can be named anything you want, with two exceptions: [globals] and
[general] are special reserved context names. [globals] and [general] are not
required. The [globals] context is for defining your own variables, which we’ll
do in future articles. Permitted characters for context names are the letters
A through Z, both uppercase and lowercase, the numbers 0 through 9, and the
hyphen and underscore. Spaces are not allowed. Here is a simple default context:

exten => s,1,Dial(SIP/250,30)
exten => s,n,Voicemail(250)
exten => s,n,Hangup

That’s right, poor old Alrac still has to play receptionist, just like last
episode. Don’t worry, we’ll fix that next week with a nice new digital receptionist.

Now create a context for outbound calling:

exten => _9NXXXXXX,1,Dial(Zap/g1/${EXTEN})
exten => _91NXXNXXXXXX,1,Dial(Zap/g1/${EXTEN})
exten => 911,1,Dial(Zap/g1/911)
exten => 9911,1,Dial(Zap/g1/911)

Next, create a context that lists your local users and defines their calling privileges:

exten => 250,1,Dial(SIP/alrac)
exten => 251,1,Dial(SIP/stan)
exten => 252,1,Dial(SIP/ollie)
exten => _2XX,1,Dial(SIP/${EXTEN},30)
exten => _2XX,n,Voicemail(${EXTEN})
exten => _2XX,n,Hangup
include => outbound
include => default

Go back to the /etc/zapata.conf created last week and make sure the context is default. In last week’s sip.conf change Alrac, Stan, and Ollie’s contexts to local-users. Restart Asterisk to activate the changes, then make some outbound calls to test it.

Let’s take a look at what these new configurations do:

The default context sends all incoming calls to extension 250. If no
one answers it goes to voicemail. This is not quite satisfactory for serving
several users, but we’ll tweak it next week.

The outbound context allows both local and long-distance calling, as
well as 911 calls. Users must dial 9 first. This is not necessary, but most
hardened office workers are in the habit of dialing 9 to get an outside line,
so we’ll accommodate them.

This shows how to use wildcards, which are also called pattern-matching. The underscore tells Asterisk that it’s a wildcard, not a literal extension. X matches any single digit from 0 to 9. N matches any single digit from 2 to 9. Why use N? The outbound context in our example will work for any local or long-distance number in North America. If you live somewhere else you’ll have to adjust this for your own calling area.

Always make sure you have 911 service. The example allows both 911 and 9911 to work.

The local-users context defines the local telephone extensions and allows internal users to call each other with their three-digit extensions.

include statements are how contexts get access to other contexts. This lets you take a sort of object-oriented approach and include other contexts instead of re-typing things a lot. You can define different groups of users and grant them different levels of privileges, like allowing toll calls only to a select group.

Channel variables
${EXTEN} is a built-in Asterisk channel variable. See Asterisk variables for a list of built-in global, channel, and environment variables, plus a lot of useful information that explains all the mysterious things you see in other people’s dialplans.

Come back next week and we’ll set up a nice digital receptionist to route incoming calls, screen out unwanted callers, or even more fun, snare them in voicemail hell.

VoIPowering Your Office with Asterisk: SOHO VoIP
VoIPowering Your Office with Asterisk: SOHO VoIP part 2
NANPA: North American Numbering Plan Administration

Latest Articles

Follow Us On Social Media

Explore More