Setup a SIP trunk using FreePBX

Asterisk configuration is often confusing and frustrating. The FreePBX GUI simplifies the many tedious configuration tasks in Asterisk. The following guide will walk through the steps to set up a SIP trunk using FreePBX.

Prerequisites

  1. A functioning Asterisk server with FreePBX.
  2. SIP trunk info from a SIP provider. We'll be using Broadvoice.

Setup the SIP Trunk

Open up a web browser and go to your Asterisk server web interface:

http://[Server Name or IP]/admin/
Login as "admin" or "maint" (depending on your system).

Now, on the left, under Basic click Trunks, you should see a selection of trunk types, like this:

Add a Trunk

Add SIP Trunk

Add DAHDi Trunk

Add Zap Trunk (DAHDi compatibility mode)

Add IAX2 Trunk

Click Add SIP Trunk. We are now presented with a page that we must fill in with our trunk info.

General Settings

In the Outbound Caller ID field, you can enter a caller ID, but it may not do anything. So, we'll skip this field. We'll also leave the Never Override CallerID unchecked.

For the Maximum Channels field, we'll put in 1. This is because the plan we are using in this guide only allows 1 incoming call at a time.

Leave the Disable Trunk and Monitor Trunk Failures at their defaults.

Dialed Number Manipulation Rules (Outgoing Dial Rules)

Dial rules are powerful, yet quite simple to learn. These rules can manipulate the dialed number before sending it out this trunk. If no rule applies, the number is not changed. The original dialed number is passed down from the route where some manipulation may have already occurred. This trunk has the option to further manipulate the number.

Dial rules follow the following basic format:

("prepend") + "prefix" | "match pattern"

If the number matches the combined values in the prefix plus the match pattern boxes, the rule will be applied and all subsequent rules ignored. Upon a match, the prefix, if defined, will be stripped. Next the prepend will be inserted in front of the match pattern and the resulting number will be sent to the trunk. All fields are optional.

Rules:

  • X    matches any digit from 0-9
  • Z    matches any digit from 1-9
  • N    matches any digit from 2-9
  • [1237-9]  matches any digit in the brackets (example: 1,2,3,7,8,9)
  • .    wildcard, matches one or more dialed digits
  • +    Adds a number onto the dialed digits.
  • |    Subtracts a number from the dialed digits.
  • prepend:    Digits to prepend upon a successful match. If the dialed number matches the patterns in the prefix and match pattern boxes, this will be prepended before sending to the trunk.
  • prefix:    Prefix to remove upon a successful match. If the dialed number matches this plus the match pattern box, this prefix is removed before adding the optional prepend box and sending the results to the trunk.
  • match pattern:    The dialed number will be compared against the prefix plus this pattern. Upon a match, this portion of the number will be sent to the trunks after removing the prefix and appending the prepend digits. You can completely replace a number by matching on the prefix only, replacing it with a prepend and leaving the match pattern blank.

In the following examples, we'll use dialing rules to modify numbers for US 10-digit dialing.

1+NXXNXXXXXX
1416+NXXXXXX
9|.

Let's examine what these mean:
We'll start with the first one. (1+NXXNXXXXXX)

1+ means prepends "1" to the number.
N means match any number between 2 and 9.
X means match any number between 0 and 9.
This would match a number like, say 416-515-1234 and turn it into 1-416-555-1234 before sending it to the SIP servers.

So, the next one (1416+NXXXXXX) goes like this:
1416+ prepends "1416" to the number.
N matches any number between 2 and 9.
X matches any number between 0 and 9.
So then this one would match a number like, 555-1234 and turn it into 1-416-555-1234 before sending it to the SIP servers.

In the third example (9|.)
9| prefix or remove "9" from the number. This is normally added to route calls to a trunk. So the user would dial "9" to dial-out from this trunk.
. The period or dot "." is a wildcard that matches one or more digits so this will allow any type of call to use this trunk.
So then this one would match any number with a prefix 9, strip the 9 from the number and send the rest of the number to the SIP servers.

The Outbound Dial Prefix field prefixes a number to all numbers dialed through this trunk. For most cases including this example, we will leave it blank. However, if this is a trunk to another Asterisk server or a Centrex line, you many need to put "9" in this box to access an outside line.

Outgoing Settings

Under Outgoing Settings, we see the field Trunk Name. We'll put "Broadvoice" in this box.

Now, here comes one of the most complicated parts of setting up a SIP trunk, the PEER Details. These settings tell Asterisk how to connect to the SIP provider.

Here is a list of the most common settings with descriptions of each one:

disallow=all
This should always come before any allow directives
allow=ulaw
This allows use of the G.711 u-law codec. Most SIP providers support this codec.
context=from-trunk
This is the context that Asterisk will dump calls coming from the trunk into this dialplan context. Without this set to a proper context, incoming calls will not work.
dtmfmode=auto
This tells Asterisk how to interpert DTMF tones. It can be auto (auto-negotiates what mode to use - recommended), inband (sends DTMF as sounds in the audio stream), rfc2833 (recommended if your SIP provider supports it), or info (DTMF info is sent inside the SIP header)
fromdomain=sip.broadvoice.com
This tells the SIP provider what domain the call comes from. Some SIP providers require this for authentication.
fromuser=<Username>
This is the username to authenticate to the SIP provider with.
host=sip.broadvoice.com
This is the host to connect with to send calls
insecure=very
This determines if Asterisk should authenticate calls coming in. Your SIP provider should tell what to set this to. Common settings are "invite", "invite,port", or "very".
qualify=yes
This tells Asterisk whether or not to send SIP NOTIFY messages to the peer to check if it's still avalible the latency between it and Asterisk.
secret=<Password>
This is the password to authenticate to the SIP provider
type=peer
This sets the type to peer. This is required.
username=<Username>
This is the username to connect to the SIP provider with.
authname<Username>
This is the authentication username to connect to the SIP provider with. This isn't normally required, but some providers like Broadvoice require it to register.
canreinvite=no
This tells Asterisk if it should try to set up a call between the SIP provider and the destination phone directly. This is typicly set to no. If you're behind a NAT, this should be set to "no".

Incoming Settings

We do not need anything under Incoming Settings, so just leave it blank.

Registration

One of the most important settings in a SIP trunk, is the register string. You will find the field under Registration.
Some SIP providers use a slightly different register string format than others. The formats go as below:

Some, like Broadvoice, use this format:

<Username>@<SIP proxy>:<Password>:<AuthID>@<SIP proxy>/<DID>

Which translates into:
<Phone number>@<SIP proxy>:<Password>:<Phone number>@<SIP proxy>/<DID>

While others use this format:
<Username>:<Password>@<SIP proxy>/<DID>

Which translates into:
<Phone number>:<Password>@<SIP proxy>/<DID>

The /<DID> is important because it tells Asterisk how to route incoming calls from this trunk. It is a good idea to set it to your phone number/username.
So, for this guide, we'll use a register string like this:
4165555555@sip.broadvoice.com:ATenCharPw:4165555555@sip.broadvoice.com/4165555555

Finally we can click the Submit Changes button.
Now we can move on to setting up the inbound route.

No comments:

Post a Comment