4 min read

Using Asterisk as a PSTN Gateway

Step 1: Add the gateway address in the trusted table using SerMyAdmin:

Using Asterisk as a PSTN Gateway for OpenSER

If desired or convenient, you can instead use the MySQL command line interface to achieve the same result.

#mysql –u openser –p
-- enter your mysql password --
mysql> use openser;
mysql> INSERT INTO trusted ( src_ip, proto, from_pattern )
VALUES ( '', 'any', '^sip:.*$');

The records above tell the OpenSER script to allow requests coming from the IP address with any transport protocol, matching the regular expression ^sip:.*$. You can use the following command if you don’t want to reload OpenSER.

#openserctl fifo trusted_reload

Step 2: Include your served domains in the domain table (if you have not done before).

openserctl domain add sermyadmin.org

You can also use SerMyAdmin to do this.

Using Asterisk as a PSTN Gateway for OpenSER

Step 3: Include the user into the groups (local, ld, and int):

#openserctl acl grant 1000@sermyadmin.org local
#openserctl acl grant 1000@sermyadmin.org ld
#openserctl acl grant 1000@sermyadmin.org int
#openserctl acl grant 1001@sermyadmin.org local

To use SerMyAdmin, just go to the screen below:

Using Asterisk as a PSTN Gateway for OpenSER

Step 4: Configuring Asterisk as a gateway.

Two very popular gateways for OpenSER are Asterisk and Cisco AS5300. Gateways from other manufacturers can be used too; check their documentation for instructions. Let’s see how to configure a Cisco 2601 with two FXO interfaces and an Asterisk with an E1 PSTN card.

It is important to prevent the direct sending of SIP packets to gateways. The SIP proxy should be in front of the gateway and a firewall should prevent users from sending SIP requests directly to the gateway.

Step 5: Setting up the Asterisk Server or the Cisco Gateway.

We will assume that the PSTN side of the Asterisk gateway is already configured. Now let’s change the SIP configuration (sip.conf) of our gateway and its dial plan (extensions.conf). We will configure Asterisk to send to the proxy each call coming from the PSTN and vice versa. We are using the guest feature of the SIP channel on the Asterisk Server. Prior knowledge of Asterisk is required here. Below is the simplest configuration allowing Asterisk to communicate with OpenSER. Please, adapt this script to your topology.

Allow SIP packets to your asterisk server, coming only from your SIP server. Do not allow SIP packets coming from other destinations. You can use IP Tables to do this, consult a Linux security specialist, if you are
in doubt.

Asterisk Gateway (sip.conf)

#calls incoming from the SIP proxy to be terminated in the PSTN lines
[sipproxy]#calls incoming from the PSTN to be forwarded to clients behind the SIP
Asterisk (extensions.conf)
[sipoutgoing]# If you have a digital interface use the lines below
#If you have analog FXO interfaces use the lines below.

Cisco 2601 Gateway

The following explanation could help, but prior knowledge of Cisco gateways is required to complete this configuration. The call routing on Cisco gateways is done by the instruction dial peer. Any call with the number called starting with 9 followed by any number (9T) is forwarded to the PSTN on the ports 1/0 or 1/1 as instructed by the dial peer voice 1 and 2 POTS lines (plain old telephone system). Called numbers starting from 1 to 9 with any number of digits following will be directed to the SIP proxy in the IP address as instructed in the ‘dial-peer voice 123 voip’ line.

voice class codec 1
codec preference 2 g711ulaw
interface Ethernet0/0
ip address
ip classless
ip route
no ip http server
ip pim bidir-enable
voice-port 1/0
voice-port 1/1
mgcp profile default
! The dial-peer pots commands will handle the calls coming from SIP
!dial-peers. Any call matching 9 followed by any number of digits will
be !forwarded to the PSTN with the 9 striped.
dial-peer voice 1 pots
destination-pattern 9T
port 1/0
dial-peer voice 2 pots
destination-pattern 9T
!The dial-peer voip commands will handle the calls coming from the
pots !dial peers (PSTN). You can prefix a number (80 in this example)
and send the DID number ahead.
dial-peer voice 123 voip
destination-pattern ....T
forward all
session protocol sipv2
session target ipv4:
dtmf-relay sip-notify

Step 6: Test the configuration making and receiving calls.


In this article, we have seen how to configure and use the Cisco 2601 gateway and the Asterisk gateway for OpenSER to send calls to the PSTN.

Subscribe to the weekly Packt Hub newsletter. We'll send you this year's Skill Up Developer Skills Report.

* indicates required


Please enter your comment!
Please enter your name here