Category: VoIP

Home / Category: VoIP

Asterisk Gateway Interface

1. What is Asterisk Gateway Interface? 

 
In simple word AGI is Language Independent API to programmers to control the call flow on their Asterisk PBXs.
 
Asterisk provides more than its own dial-plan, to control to the call flow or lets say call logics. So which means you may use either one of
 
  1. Dialplan
  2. Asterisk Manager Interface (AMI)
  3. Asterisk Gateway Interface (AGI)
to manipulate your call logics.
 
Before we move on to AGI lets briefly discuss about each one of above,
 


Dialplan

Dial plan is Asterisk native call logics performer, it’s fast, easy to learn and efficient. But this configuration script is more closer to assembly program (If you have any previous experience on assembly), the main drawback of the Asterisk Dialplan in it’s lack of support on standard procedural  language as an example when you want create a loop. 
 
Any way in the following tutorials we will only discuss about the AGI, But we can’t avoid the fact that we need Dialplan to call out AGI script.
 


Asterisk Manager Interface (AMI)

What if you have a remote controller to your Asterisk PBX, it is AMI. It is more sophisticate with regards to Dialplan. So in essence you can control your PBX by using TCP socket. 
 
You can use a remote control tool to control your PBX but beware there are more security concerns would be followed up with this.


Asterisk Gateway Interface (AGI)

If you consider AMI and Dialplan regards to AGI, AGI should lay between them. AGI can’t be completely independent, we have to get the support of the Dialplan, also you may use your desired language on AGI script. 
 
In this tutorial I’ll use Perl on our AGI script…
 
 
But please note that if your PBX system is pure outbound AGI is not for you, AGI is only for inbound call  handling.
 
There are four type of AGI’s you can find,
  • Standard AGI
It is the Simplest of all, and use standard inputs(STDIN) and outputs(STDOUT) to communicate with the PBX. In this tutorial are about to use this one. 
  • Dead AGI
Dead AGI is to handle the call logics after call hangup. Some AGI commands will not be able to use under this. 
  • Fast AGI
Using Fast AGI you can transfer your AGI processing burden to another server. You may use TCP sockets for the communications. It’s Provide all the features in Standard AGI. 
  • EAGI
If Developers need to access/communicate beyond the STDIN and STDOUT which if they need to access the media channel, they may use EAGI.
 


2. Okay Now it’s time to discuss our scenario,

I have two SIP peers call, peter and bob. We need them to able call with each other, and if one of them is not available, calling party should be able to drop a voice mail.
Later the other party may listen to that voice mail by accessing their voice mail.
By Dialing an extension with ‘*’ (eg: *101) can listen their Voice Mail Boxes 
 

Create two sip peers for bob and peter

  • sip.conf
          [peter]             ;Peer name
          type=friend         ;Allow both incoming and outgoing 
          secret=123          ;Password for SIP peer
          host=dynamic        ;IP address of the SIP peer 
          allow=alaw,ulaw     ;Allowed coddecs
          context=users       ;Dialplan context for the SIP peer

          [bob]
          type=friend
          secret=123
          host=dynamic
          allow=alaw,ulaw
          context=users
         
Configure Dialplan to use our AGI script 

  • extensions.conf
   [users]
          exten => _[*0-9]X.,1,NoOp("Dialiing AGI") ; This will accept dial pattern which are starting from * or from a number
          same => n,AGI(dialplan.pl,${EXTEN})            ; run Our AGI script which resides in agi-bin, and also we are providing the dialled extension number as a command line input.

Configure voice mail boxes

  • voicemail.conf
          [sales]                                       ;VM Context
          101 => 123,Bob's Mailbox,bob@example.com ;vmbox => [password],[Description], [Mail address to use]
          102 => 321,Peter's Mailbox,peter@example.com
         
Now please reload your configuration through asterisk CLI and Verify them
                   #rasterisk
                     > sip reload
                     > sip show peers
                     > dialplan reload
                     > voicemail reload
                     > voicemail show users
              
 
Now lets look at our Perl script
#!/usr/bin/perl
use warnings;
use strict;

#apt-get install build-essential

use Asterisk::AGI;

Asterisk::AGI for perl : 
First you may install cpan which is use to manage perl module by using 
#apt-get install build-essential
Then install Asterisk:AGI module using cpan
#cpan
>install Asterisk::AGI

Now in here I’ve define a global variable and assign the extension number which has passed as a command line argument from the Dialplan.

our $DPLAN_EXTEN=$ARGV[0];



Now depends on the number that dialed, if number start with ‘*’ it should dialed to relevant voice mail box or else if it’s a number it should directly dial the relevant sip peer.

if ($DPLAN_EXTEN =~ m/^*/){
       vm_box();
} else {
       main();
}
####Call Routes between two peers, Bob and Peter 
sub main{

#-->In here I have created a hash(a nested hash) to store relevant information to use
 my %EXTEN_CONF = (
                '101' => {
                        'CHAN'          =>'SIP',
                        'PEER'          =>'bob',
                        'MAXWAIT'       =>5,
                        'VM_CONTEXT'    =>'sales',
                },
                '102' => {
                        'CHAN'          =>'SIP',
                        'PEER'          =>'peter',
                        'MAXWAIT'       =>5,
                        'VM_CONTEXT'    =>'sales',
                },
 );

#-->Now here we create our AGI object
my $AGI = new Asterisk::AGI;


#-->Use exec to use any Dialplan Application in your AGI script. Using exec you can execute the relevant application and pass the argument to the relevant application.
#-->$AGI->exec($app, $options)
#--> As per the following example, execute dial function and pass relevant Chanel , Peer and wait time. If you execute the following on the Dialplan it would be "exten => 101,1,Dial(SIP/bob,5)"
$AGI->exec('Dial',"$EXTEN_CONF{$DPLAN_EXTEN}{'CHAN'}/$EXTEN_CONF{$DPLAN_EXTEN}{'PEER'},$EXTEN_CONF{$DPLAN_EXTEN}{'MAXWAIT'}");

#-->Now in order to put a voice mail use voice mail function
$AGI->exec('VoiceMail',"$DPLAN_EXTEN@$EXTEN_CONF{$DPLAN_EXTEN}{'VM_CONTEXT'}");


#-->To Hangup
$AGI->hangup();
}
####Listen to the Voice Mails
sub vm_box{
#-->In order to store the informations regarding to the voice mail boxes here I've use a hash again
my %VM_CONF = (
                '*101' => {
                        'VM_BOX'        =>'101',
                        'VM_CONTEXT'    =>'sales',
                },
                '*102' => {
                        'VM_BOX'        =>'102',
                        'VM_CONTEXT'    =>'sales',
                },
);
my $AGI = new Asterisk::AGI;
$AGI->exec('VoiceMailMain',"$VM_CONF{$DPLAN_EXTEN}{VM_BOX}@$VM_CONF{$DPLAN_EXTEN}{VM_CONTEXT}");
$AGI->hangup();
You can put your AGI file in asterisk agi-bin. in my Debian system its on 
#cd /var/lib/asterisk/agi-bin


In order to properly run the script change the ownership to your Asterisk user and grant the execute permission.

#chown asterisk:asterisk dialplan.pl
#chmod u+x dialplan.pl

Click here to download configuration file.

 
 
To get the asterisk Dialplan application information and how to use them you may refer asterisk CLI or you can search them on https://www.voip-info.org.
Installing Perl modules,

Thats it…. Enjoy….

Asterisk on Docker

August 25, 2017 | Linux Containers, Linux Tools, VoIP | No Comments

This Document covers asterisk basic installation on docker. Since till now there is no official asterisk image on Docker hub we will use Debian core to install the asterisk.

Prerequisites Linux host to install docker, Internet connectivity and docker account to download docker images.

1. Installing Docker (Ensure your Internet connectivity)

  • Centos 7

          #wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
#rpm -ivh epel-release-latest-7.noarch.rpm
#yum install docker

  •  Install Docker on RHEL and CentOS 6
         # yum install epel-release
# yum install docker-io

2. Start Docker

  • Centos 7

         # systemctl start docker
# systemctl status docker
# systemctl enable docker

  •  On RHEL/CentOS 6

         # service docker start
# service docker status
# chkconfig docker on


Our Asterisk PBX will reside on Debain, So first we must set our Debian container to install asterisk. (Please ensure that you docker is up running without any issue, If you wish to verify you docker engine please use hello world application “# docker run hello-world” )
If everything went well as we expected, you will see something like this.

 3. Pull Debian Image to Docker

         # docker pull debian


This might take some time.

4. Now Lets run our New Debian container using pulled image

First we shall get the Debian image ID by using

            # docker images
You will get someting like this (This will show your all docker images),

Now we shall Start our container using Our Debian image (My Debian image ID appears to be a20fd0d59cf1 )

       # docker run -it –name=<Container Name> –network=host <Image ID>

I’ve used -i and -t option to Keep STDIN open even when container is detached :i and to allocate  pseudo-TTY :t. Plus I’ve used –network option to connect my container with my host network.

After creating your Debian container you will be inside the Container.

5. Install Asterisk

Update your Debain distribution

       # apt-get update
Install asterisk

        # apt-get -y install asterisk
This will take some time to install the packages.

Now After completion of the installation, we might configure the asterisk.

6. Asterisk Configuration

Configure the asterisk RTP Port Limitation.

       #vim /etc/asterisk/rtp.conf

(Use #apt-get install vim-nox To Install VIM)

Change rtpstart and rtpend parameters.

Save and Exit
This will help us wo bind the ports when we running our Asterisk container.
Now We shall start the asterisk.
 

       # /etc/init.d/asterisk start
Use # rasterisk to check if you can  access the Asterisk CLI.

Type exit and Hit Enter to exit Asterisk CLI

Now we shall commit our changes to keep our container configuration.

7. Committing the Container configuration

use Ctrl + p + q to detach from the container
Now issue docker ps  command to see running containers.

        # docker commit -m “<Commit Message>” -a “<author’s name>” <ContainerID or Name to commit> <Repository>:<tag>

Now you can check your committed Docker images by using # docker images

8. Run our Committed Image

        docker run -itd –name=PBX-Asterisk –network=host -p 5060:5060/tcp -p 5060:5060/udp -p 10010:10010/udp -p 10011:10011/udp -p 10012:10012/udp -p 10013:10013/udp -p 10014:10014/udp -p 10015:10015/udp -p 10016:10016/udp -p 10017:10017/udp -p 10018:10018/udp -p 10019:10019/udp -p 10020:10020/udp <Commited Docker ImageID to run>


 I’ve used the –name option to assign a name to my container, and use -p to map the container port with host port. 

9. Configure the sip peers

Lets jump in to our asterisk container
You can use

        # docker exec -it <Your Container ID> bash

or

         # docker attach <Your Container ID>

 Let’s configure our sip peers in # vim /etc/asterisk/sip.conf

       [100]
       secrete=abc123
       context=home
       type=friend
       allow=ulaw,alaw
       host=dynamic

10. Dialplan configuration

11. Complete Asterisk configuration

Now we shall start the asterisk if we already haven’t,

        # service asterisk start


Jump to Asterisk CLI

         # rasterisk
Reload the sip and dialplans

          localhost*CLI> sip reload
localhost*CLI> dialplan reload

You can connect youe sip phones now…..

Note that, If you have iptables service aka firewalls up on your host machine you may need to do some changes accordance.

FreePBX-Installation

March 26, 2017 | Linux Administration, Linux Tools, VoIP | No Comments

FreePBX-Installation

 

1. Pre-installation

In order to setup call center server first we have to confirm that our system is full filled the minimum requirements. This asterisk deployment is based on RedHat distribution aka CentOS.  To full-fill the above requirement we are going to setup asterisk 11 on CentOS 6.5 (x64).

1.2 CentOS 6.5×64 installation 

It is recommended to install CentOS 6.5×64 minimum version and manually install all the other package as our requirement. 
 
At the beginning it recommended to configure the logical disk drives aka Raid. 
 
Note that some of the server-rigs will not compatible to centos 6.5×64, most of the time it’s because the particular server’s Raid drivers might not be found in centos 6.5×64 disk. In such scenario please follow the below instruction.
First we have to download the Raid driver from relevant vendor. (If it’s HP you will find somewhat like this hpvsa-X.X.X-X.rhel6u.5x86_64.dd).
 
Note that if the driver has compressed with gzip(.gz) it is recommended to extract by using “tar” in a Linux platform.
Use your Fat32 formatted usb stick and copy those extracted driver (.dd) files.
Now boot your server with centos 6.5×64 and plug your USB stick during the boot time.  At the installation menu, select the installation method and hit “ESC” key to receive the “boot:” prompt. In the boot prompt enter following command 
 

                linux dd blacklist=ahci

 
and then let your system to boot (Don’t reboot the system).
 
And during the installation you will be prompted a window to select the Raid driver and select the relevant driver. On successful driver installation you will be able to see your Logical (Raid driver).
 
And install the centos 6.5×64 without customize any package.
 
Note that it is recommended to customize your Logical disk in order to accommodate root user to enough space.  

2 Asterisks Installation

2.2 Disable SELinux

First You have to disable SELinux
 

             # sed -i ‘s/(^SELINUX=).*/SELINUX=disabled/’ /etc/sysconfig/selinux

Afterwards to affect the system changes restart the PC.
 

           # init 6

 
Now check selinux stats by using
 

          # sestatus

 
 
 
Or you can confirm it on “/etc/sysconfig/selinux” file
 
 

2.3 Network Configuration 

          # vim /etc/sysconfig/network-scripts/ifcfg-<netowrk card>`

   Note that network card name can be found using

         # ifconfig

 
This an example network scripts for network card called “eth0” assign with the IP address “192.168.1.250”.
Make sure that your changes only limited to following parameters,

ONBOOT 
BOOTPROTO 
IPADDR 
NETMASK 
GATEWAY 
DNS1 
DNS2

 
You are always welcome to use `[root@localhost ~]# setup` in order to perform following configurations.

2.4 Update the System

Then you have to update your system.
 
Make sure that internet connectivity of the server is alive.
 
Now using yum application manger lets update the server,
 

            # yum -y update

These Updating processes will take a while.

         # yum groupinstall core
         # yum groupinstall base

 

2.5 Install essential dependencies 

In order to deploy the system, we have to install some additional packages aka dependencies.  
 

           # yum install gcc gcc-c++ lynx bison mysql-devel mysql-server php php-mysql php-pear php-mbstring tftp-server httpd make ncurses-devel libtermcap-devel sendmail sendmail-cf caching-nameserver sox newt-devel libxml2-devel libtiff-devel audiofile-devel gtk2-devel subversion kernel-devel git subversion kernel-devel php-process crontabs cronie cronie-anacron wget vim php-xml uuid-devel libtool sqlite-devel unixODBC mysql-connector-odbc

2.6 Disabling Default IP tables 

Then you have to disable default IP tables and you may enable them again after the installation.
 

 

            chkconfig –level 0123456 iptables off`

 

2.7 Configurations on MySQL and Apache

It is required MySQL and Apache servers to perform Both asterisk and FreePBX operations and it’s recommended if MySQL and Apache services start at the server boot time.
 
          chkconfig –level 345 mysqld on

 

          chkconfig –level 345 Apache on

 

 
Let’s bring up the Both Apache and MySQL server
 
          service mysqld start

 

          service httpd start

 

2.8 PearDB Installation

PearDB is a database abstraction library which allows to connect to different kinds of databases such as    PostgreSQL, MySQL using a consistent API.
 
             # pear channel-update pear.php.net
   # pear channel-update pear.php.net
You may found some warnings saying “WARNING: “pear/DB” is deprecated in favor of “pear/MDB2″”, You can just ignore them.
 
It is preferred to reboot the system before continue further.
 

         # init 6

2.9 Setup the Asterisk user

Then it is required add user to the system  with relevant permission to perform asterisk activities.
 
          # adduser asterisk -M -c “Asterisk User”
 

2.10 Download Asterisk Source Files

 
Use following links to download asterisk sources files. 
 
Note that it is preferred  to download the following files to “/usr/src/”.

       # cd /usr/src
       # wgethttp://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/dahdi-linux-complete-current.tar.gz
       # wget http://downloads.asterisk.org/pub/telephony/libpri/libpri-current.tar.gz
       # wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-11-current.tar.gz
       # git clone https://github.com/akheron/jansson.git
       # wget http://www.pjsip.org/release/2.2.1/pjproject-2.2.1.tar.bz2

2.11 LibPRI and DAHDI

        # cd /usr/src
        # tar xvfz dahdi-linux-complete-current.tar.gz
        # tar xvfz libpri-current.tar.gz
        # rm -f dahdi-linux-complete-current.tar.gz libpri-current.tar.gz
        # cd dahdi-linux-complete-…… Folder……
        # make all
        # make install
        # make config
        # cd /usr/src/libpri-……Folder……
        # make
        # make install

 
These modules are recommended to install even those modules required only if you are using physical devices.
 

2.12 Pjproject Setup

 
pjproject is a collection of utilities, libraries for building and testing SIP based applications.
 

         # cd /usr/src`
         # tar -xjvf pjproject-2.2.1.tar.bz2`
         # cd pjproject-2.2.1`
         # CFLAGS=’-DPJ_HAS_IPV6=1′ ./configure –prefix=/usr –enable-shared –disable-sound –disable-resample –disable-video –disable-opencore-amr –libdir=/usr/lib64`
         # make dep`
         # make`
         # make install`

 

2.13 Jansson Setup 

Jansson is a C library for encoding, decoding and manipulating JSON (JavaScript Object Notation is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming) data. 
 

         # cd /usr/src/jansson
         # autoreconf -i
         #./configure –libdir=/usr/lib64
         # make
         # make install

2.14 Asterisk Setup

        # cd /usr/src
        # tar xvfz asterisk-13-current.tar.gz
        # rm -f asterisk-13-current.tar.gz
        # cd asterisk-…..Folder….
        # contrib/scripts/install_prereq install
        #./configure –libdir=/usr/lib64
        # contrib/scripts/get_mp3_source.sh
        #make menuselect

 
Now you must be prompted with “Asterisk Module and Build Option Selection”.
 
 
 
Add mp3 format support if you’re required too compatible mp3 formats on your asterisk server and then press “Save & Exit”.
Finally let’s complete the asterisk installation process.
 

        # make
        # make install
        # make config
        # ldconfig

 
You may need to install Asterisk-Extra-Sound
 

           # mkdir -p /var/lib/asterisk/sounds
           # cd /var/lib/asterisk/sounds
           # wget http://downloads.asterisk.org/pub/telephony/sounds/asterisk-extra-sounds-en-wav-current.tar.gz
          # tar xfz asterisk-extra-sounds-en-wav-current.tar.gz
          # rm -f asterisk-extra-sounds-en-wav-current.tar.gz
          # wget http://downloads.asterisk.org/pub/telephony/sounds/asterisk-extra-sounds-en-g722-current.tar.gz
         # tar xfz asterisk-extra-sounds-en-g722-current.tar.gz
         # rm -f asterisk-extra-sounds-en-g722-current.tar.gz

3 FreePBX Installation

 
Note that asterisk is our actual PBX aka core switch, while FreePDB can defined as a web-based GUI and configuration file writer which will write asterisk dial planes and configuration for you.
 

3.1 Download FreePBX Source Files

        # cd /usr/src

        # wget http://mirror.freepbx.org/modules/packages/freepbx/freepbx-12.0-latest.tgz
        #tar vxfz freepbx-12-latest.tgz

 

3.2 Setup Relevant permission on Asterisk

It is required to setup couple of ownership permissions as follows.

 

         #chown asterisk. /var/run/asterisk
         #chown -R asterisk. /etc/asterisk
         #chown -R asterisk. /var/{lib,log,spool}/asterisk
         #chown -R asterisk. /usr/lib/asterisk
         #chown -R asterisk. /usr/lib64/asterisk
         #mkdir /var/www/html
         #chown -R asterisk. /var/www/

3.3 Configuration on Apache Server

FreePBX use Apache web server to provide web-based GUI, Following modifications are essential on Apache server.

 

            #sed -i ‘s/(^upload_max_filesize = ).*/120M/’ /etc/php.ini
            #cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf_orig
           #1 asterisk/’ /etc/httpd/conf/httpd.conf
           #service httpd restart

3.4 Configuration on MySQL Server

 
Asterisk uses MySQL database engine to perform its database activities, These Following modifications are essential on MySQL database.
 

          #cd /usr/src/freepbx
          #export ASTERISK_DB_PW=amp109
          #mysqladmin -u root create asterisk
          #mysqladmin -u root create asteriskcdrdb

 
Now It’s required Grant relevant permissions on asterisk database.

 

         #mysql -u root -e “GRANT ALL PRIVILEGES ON asterisk.* TO asteriskuser@localhost IDENTIFIED BY ‘${ASTERISK_DB_PW}’;”
        #mysql -u root -e “GRANT ALL PRIVILEGES ON asteriskcdrdb.* TO asteriskuser@localhost IDENTIFIED BY ‘${ASTERISK_DB_PW}’;”
        #mysql -u root -e “flush privileges;”

3.5 Finalize the FreePBX installation

        #cd /usr/src/freepbx
         #./start_asterisk start
         #./install_amp –installdb –username=asteriskuser –password=${ASTERISK_DB_PW}
         #amportal chown
         #amportal a ma installall
         #amportal a reload
         #amportal a ma refreshsignatures
         #amportal chown

Let’s Start the FreePBX
 

          #ln -s /var/lib/asterisk/moh /var/lib/asterisk/mohmp3
          #amportal restart

Note that amportal is the Linux command which is use to controls FreePBX by using Linux command prompt.
 
 
 
 
 
 
 
 
 
 
 

Reference

Nagy, A. (2014). Version 12.0 Installation – FreePBX OpenSource Project – Documentation. [online] Wiki.freepbx.org. Available at: https://wiki.freepbx.org/display/FOP/Version+12.0+Installation [Accessed 7 Mar. 2016].

Jordan, M. (2019). Asterisk 11 Documentation – Asterisk Project – Asterisk Project Wiki. [online] Wiki.asterisk.org. Available at: https://wiki.asterisk.org/wiki/display/AST/Asterisk+11+Documentation [Accessed 3 Mar. 2014].

SIP vs BRI/PRI

February 26, 2017 | Discussions, VoIP | No Comments

SIP

SIP standards for Session Initiation Protocol and It’s Purely IP based.

BRI/PRI

PRI stands for Primary Rate Interface and It contains One 64Kbps T1 or E1 Chanel for Signaling AKA Channel D and 23 T1 or 30 E1 Channels as Bearing Chanel aka Channel B.
BRI standards for Basic Rate Interface and Contain Two Barer channel and One Signaling Chnnel AKA 2B+D.
Further Both Both PRI and BRI are ISDN services and also data rate of PRI is 2.048Mbps while 128-144Kbps.
ISDN : Integrated Service Digital Network / It’s Some Dumb Network

SIP vs BRI/PRI

SIP does Best effort Delivery as same as IP traffic do, while BRI/PRI Provide QoS. If someone requires to attain QoS through SIP something like MPLS will do with a considerable amount cost.
SIP is more flexible than BRI/PRI because it can be accommodated by company existing data network while BRI/PRI reuires to have a dedicated link for it self.