Get Firefox! Viewable with any browser! Valid HTML 4.01! Valid CSS! OpenOffice.org
Text size: 

Getting your Asterisk setup to talk to your VoIP provider

Getting your home PBX to connect to the outside

I recently signed up with a VoIP (Voice over Internet Protocol) provider, not so much in order to place cheap calls but more so that I could set up a system whereby contacts of mine in two European countries could contact me far cheaper than having to make an international call, which is never cheap (especially using a cellphone). Of course, the account with the VoIP provider (IdeaSIP in my case) does allow me to make cheap calls anyway, which is more of a cool side-effect than the main reason, but a cool side-effect that got me thinking about all this VoIP stuff and looking at the bigger picture.

Now, it just so happens that I have several devices that allow me to register with a VoIP provider using SIP (Session Initiation Protocol), but obviously not all at the same time because I do only have one account. To start with, I have a Grandstream Budge Tone 100 SIP phone, which is the easiest device to use since it is just a phone and nothing else. I also have a Nokia N95 cellphone, which has a built-in SIP client that can register with a VoIP provider either over the cellco's data plan or using a WiFi (802.11b or g) hotspot. Finally, I have a cheap solution in the form of a soft-phone, which is just a program allowing me to use my computer as a phone. The application I use for that here is kphone.

Building and installing Asterisk and getting one or more SIP phones (hard or soft) to talk to it will not be covered here. There are countless documents already on the 'Net explaining how to do this so I won't bother reinventing the wheel. What I will do is go through the specifics of getting your Asterisk setup to register with an off-site VoIP provider so that you can use your SIP phones to communicate with the outside world. We'll then do a bit of nifty stuff with a dialplan so that you can dial local, long-distance and international numbers on your SIP phone in the same way you would if you were using a regular PSTN phone. Bear in mind that this howto is geared specifically towards IdeaSIP and some details will have to be changed if you're using a different VoIP provider.

You'll need two pieces of information in order to get this working.

  1. First of all, you'll need your IdeaSIP account number (an 11-digit number of the form 1101xxxxxxx) and password.
  2. Secondly, you'll need a domain name pointing to the IP address of the machine on which you're running Asterisk. If your ISP provides you with a static IP address then you're probably already home and dry because the chances are you asked for it specifically so that you could point a domain to it. If you don't have a static IP address or if you don't know what this means, then you'll want to use the services of someone like DynDNS that provide an account that's easy to update whenever your IP address changes. Many broadband routers even have a DynDNS update feature built in, which allows them to update your DynDNS account automatically with no intervention on your part.

Note that the information given hereafter is for Asterisk version 1.4. It may also work in the current 1.6beta. Some details here will not work for version 1.2.

The first thing we're going to have to do is add a section to your /etc/asterisk/sip.conf file describing your VoIP account. This tells Asterisk how to negotiate with your VoIP provider when you want to place an outbound call. Add a section like this to your sip.conf file:

[ideasip-account]
type=friend
context=ideasip
username=11011234567
fromuser=11011234567
authuser=11011234567
secret=your_IdeaSIP_password_goes_here
host=proxy.ideasip.com
fromdomain=proxy.ideasip.com
insecure=very
nat=yes
canreinvite=no

type=friend means that we can use the connection with the IdeaSIP account both to pick up calls from the outside and to place outbound calls. context=ideasip refers to the "context" that we're going to define later in the /etc/asterisk/extensions.conf file. A context defines what Asterisk does in response to a call initiated by a feed claiming to be in that context. username=, fromuser= and authuser= should all have your IdeaSIP user ID in there instead of the bogus example. nat=yes and canreinvite=no are necessary if your computer running Asterisk accesses the Internet through a router.

Next, we have to add a line to the [general] section of your sip.conf file that instructs your Asterisk setup to register with youtr VoIP provider in the same way a hard SIP phone would do if you were connecting one directly to the Internet without going through your own PBX. This is necessary so that your provider knows "where" you are and therefore how to draw your attention when a call comes in for you. We also need to add a few things to help Asterisk authenticate to your VoIP provider. So, add lines like this to the [general] section of your sip.conf file:

externip=your.IP.address.or.domain.name
localnet=local.network.address/subnet.mask (eg: localnet=192.168.1.0/255.255.255.0)
nat=yes
register => 11011234567:password@proxy.ideasip.com/11011234567

Absent any externip= clause, Asterisk will find the IP address of the machine on which it's running and introduce itelf to your VoIP provider as that IP address. If the machine has an outward-facing network interface with a public IP address then there's no problem. But if the machine is in a LAN and only has a private IP address such as 192.168.1.6 then Asterisk won't be able to register with some VoIP providers because they don't like users registering with a private IP address. The answer, then, is to use the external IP address of your gateway device, which isn't a problem if you have a static allocation because your IP address never changes. Most users, however, have dynamic IP addresses. They are assigned a new IP address each time they connect to the Internet. That is why I went for a DynDNS account, which gets me a hostname that always points to my public IP address since the wireless router behind my cable modem updates DynDNS each time my public IP address changes.

The localnet= clause tells Asterisk what IP addresses are used by the LAN that the machine running Asterisk is in.

nat=yes is needed if your Asterisk machine connects to the Internet through a NAT/router device.

Replace "11011234567" with your real user ID and "password" with your password in the "register =>" line…

Finally, we need to describe the "ideasip" context so that your Asterisk setup knows what to do when a call comes in for you through your VoIP provider. For the purposes of this howto we're going to assume that you have two SIP phones connected to your PBX and that they are at extensions 2000 and 2001. Adjust according to your setup.

Add this section to your /etc/asterisk/extensions.conf file:

[ideasip]
exten => _X.,1,Dial(SIP/2000,20)
exten => _X.,n,Voicemail(2000,u)

Each line in the "dialplan" for this context is of the form:

exten => pattern,priority,action

See here for details on the syntax of pattern matching in Asterisk, and this page gives information about the priority and the action.

Basically, what the above context does is to match the caller ID of the person initiating the call with anything that starts with a digit, make extension 2000 ring for 20 seconds, and then hand the call off to the voicemail system if nobody answers.

You can also make several extensions ring at the same time when a call comes in and shunt the call over to the first extension to pick up. You'd use something like this, separating each of the extensions with an ampersand '&' sign:

[ideasip]
exten => _X.,1,Dial(SIP/2000&SIP/2001,20)
exten => _X.,n,Voicemail(2000,u)

Note that we still only shunt the call off to voicemail for one user (extension 2000) if nobody picks up. I'm not even sure if a call can be sent voicemail for more than one user anyway.

Now go to your Asterisk console and run this command at the "*CLI>" prompt in order to get Asterisk to read your new configuration:

module reload

You are now ready to start accepting calls from the outside. Whenever someone calls your VoIP account, the SIP phone at extension 2000 (or both phones if you used the latter syntax) will ring.

The next logical step is to devise a dialplan that lets you use your SIP phone just like you would a regular PSTN phone. We're going to have to map numbers of varying lengths punched into the phone to numbers as the VoIP provider needs them. For example, in order to dial another IdeaSIP user you simply dial that user's 11-digit ID. To dial a land line or cellphone in Northern America, you can either dial the last 7 digits of the number if you're in the same area code or you can dial the full number prefixed with "1" and the area code. IdeaSIP expects the full 11 digits regardless of whether or not you're in the same area code, so we need to map numbers of the form:

abc-defg

to numbers of the form:

1-xxx-abc-defg

where "xxx" is your area code, "a" is a digit from 2 through 9, and the other digits are in the range 0 through 9.

Dialling long distance numbers within Northern America requires no mapping since you already dial 11 digits as IdeaSIP requires it. International calls, however, do require mapping from this form:

01-country_code-area_code-number

to something of this form:

011*country_code-area_code-number

There are actually 3 different international prefixes that you can use with IdeaSIP: 011*, 00* and 001*. I have no idea if it makes any difference to them which one you use so I just use the first one in the list. See here.

Let's deal with long distance calls first of all, since that requires no mapping. Add these 2 lines to the context in which you put your SIP phones in /etc/asterisk/extensions.conf:

exten => _1ZXXNXXXXXX,1,Set(CALLERID(ALL)=Your Name <11011234567>)
exten => _1ZXXNXXXXXX,n,Dial(SIP/${EXTEN}@ideasip-account)

Refresh Asterisk's configuration by issuing the usual "module reload" command on the Asterisk console, and from now on, this is what will happen whenever you dial a long distance number using your SIP phone:

  1. For each number of the form '1ZXXNXXXXXX' (11 digits, starting with a '1', followed by a digit in the 1-9 range, then two in the 0-9 range, then one in the 2-9 range, then six in the 0-9 range), Asterisk starts by setting its caller ID to your name and your IdeaSIP account number. Obviously replace the bogus 11011234567 in the above example with your real account number.
  2. Next, we meet this thing: "${EXTEN}". This is a variable. This one's value in a dialplan line such as this is the number dialled. The "@ideasip-account" bit refers to the section in your sip.conf in which you described the VoIP account for the purposes of outbound calls. Therefore, the "SIP/${EXTEN}@ideasip-account" syntax means basically, use SIP to place a call to the number that was dialled using the ideasip-account.

Next, we'll deal with local calls where you simply dial the 7 digits of the number after the area code. This example assumes you're in area code 212 (New York City) so adjust to your requirements.

The first digit of the local part of a phone number cannot be '0' (so it doesn't get mixed up with the international dial prefix in case the sencond digit is a '1'), and it can't be a '1' either (so it doesn't get mixed up with the long distance prefix). It has to be in the 2-9 range. Add these 2 lines to your SIP phone's context in extensions.conf:

exten => _NXXXXXX,1,Set(CALLERID(ALL),Your Name <11011234567>)
exten => _NXXXXXX,n,Dial(SIP/1212${EXTEN}@ideasip-account)

The explanation for the first line is as above. The second is a little different. For each matching 7-digit number dialled, we pass the call to IdeaSIP having prepended 1212 to the number. In effect, we're transforming the NYC xxx-xxxx local number that was dialled into 1-212-xxx-xxxx, a full 11-digit number as required by IdeaSIP.

Finally, let's deal with international calls. We said earlier that we were going to have to map the 01xxxxx… form dialled on the keypad to 011*xxxxx… In essence, we need to rip off the initial '01' and tag '011*' on instead. We're going to use a special syntax of variables here.

${EXTEN} gives us the whole number keyed into the phone's keypad. But we don't need to use all of the number. We can in fact use just a portion of it. Each digit in the phone number is numbered, starting from zero. Therefore, the first digit is digit number zero, the second one is digit number one, the third one is digit number two, etc. We can get at the area code of a long distance number, for example, by extracting three digits as of the second digit (numbered #1). Therefore, if ${EXTEN} is the full number, ${EXTEN:1:3} is the area code.

This is what's called a substring in computer jargon. ${VARIABLE:start:count} extracts the substring starting at character number 'start' of ${VARIABLE} and continuing for 'count' characters.

If 'count' is omitted then the substring obtained continues to the end of the original variable. In effect, ${VARIABLE:start} returns ${VARIABLE} with the first 'start' characters chopped off the left, which is exactly what we want for managing international calls. We have to chop the first two characters off the head of the number dialled, i.e. the international prefix '01', and prepend '011*' in its place. Hence these 2 lines in your local phones' context:

exten => _01.,1,Set(CALLERID(ALL),Your Name <11011234567>)
exten => _01.,n,Dial(SIP/011*${EXTEN:2}@ideasip-account)

As an added bonus, we can use the diaplan to create speed-dials. Say, for example, you know someone called Howard. Let's take the first two letters of that: H-O. 'H' is on the '4' key of your phone and 'O' on the '6' key. So, let's make '46' a speed dial for Howard, whose number is in fact (123) 123-4567 (this is obviously a made-up number used for the purposes of illustrating the idea). In order to call that number you have to dial 1-123-123-4567. So, let's make it so that 11-digit number is dialled when someone dials just '46' on a SIP phone:

exten => 46,1,Set(CALLERID(ALL),Your Name <11011234567>)
exten => 46,n,Dial(SIP/11231234567@ideasip-account)

That just about wraps up this tutorial. Feel free to contact me if you see glaring mistakes on this page. Also bear in mind that I am no expert on using Asterisk or on IdeaSIP's services, so a query directly related to these subjects would best be directed to Asterisk user forums or IdeaSIP's support desk respectively.

NAVIGATION:
>> /

Last update: 09-FEB-2008
This page has been served 29519 times since 23-JAN-2008