Friday, 2 June 2017

OpenSIPS server installation

OpenSIPS is an open source SIP proxy server. This post will go through bringing up a working OpenSIPS istallation on Ubuntu 16.04 LTS. First we will install OpenSIPS and then verify that the installation by performing a SIP Registration using a CLI based SIP phone called linphone (specifically linphonec, the CLI version of the SIP phone)

Installing OpenSIPS


1. Adding keys: First fire the following commands in succession on the terminal to setup the keys and repositories for installation:

  apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 049AD65B
  echo "deb http://apt.opensips.org jessie 2.3-releases" >> /etc/apt/sources.list
  apt-get update

The above commands are explained below: 
a. The first command adds the keys from the keyserver.
b. Then we add the opensips repository to the sources.list file so that apt-get can locate the OpenSIPS server.
c. Then we update the package list available at the newly added repository by firing "apt-get update".
   
root@ubuntu:~# apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 049AD65B
Executing: /tmp/tmp.bZeJxENy5e/gpg.1.sh --keyserver
keyserver.ubuntu.com
--recv-keys
049AD65B
gpg: requesting key 049AD65B from hkp server keyserver.ubuntu.com
gpg: key 049AD65B: public key "OpenSIPS Project <info@opensips.org>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
root@ubuntu:~#
root@ubuntu:~# echo "deb http://apt.opensips.org jessie 2.3-releases" >> /etc/apt/sources.list
root@ubuntu:~#
root@ubuntu:~# apt-get update
.
.
root@ubuntu:~#

2. Installing OpenSIPS:  Now install opensips with the apt-get command.

apt-get install opensips

root@ubuntu:~# apt-get install opensips
.
.
root@ubuntu:~#

3. Editing the OpenSIPS Config file: The OpenSIPS config file is located at "/etc/opensips/opensips.cfg". In the file we need to replace the IP at which we want OpenSIPS to listen. This is done by replacing the IP in the line mentioning "listen" with the IP address at which you want OpenSIPS to listen(this will be an IP address at an interface in the machine). As we can see below the line is conveniently marked with a comment "CUSTOMIZE ME" to allow it to be easily searchable.

 41
 42 listen=udp:127.0.0.1:5060   # CUSTOMIZE ME
 43

So, replace line 42 to the following replacing the IP address to your IP address.

listen=udp:172.16.76.132:5060

The relevant portion of the config file now looks like this:

 41
 42 listen=udp:172.16.76.132:5060   # CUSTOMIZE ME
 43


4. Start OpenSIPS: Start OpenSIPS server with the command "service opensips start"

root@ubuntu:~# service opensips start
root@ubuntu:~#

5. Verify OpenSIPS is running: Verify that OpenSIPS is running with the "netstat -tulpn" command. The following output shows that OpenSIPS is running on port 5060 for SIP over UDP.


REGISTER a client


Now to verify that OpenSIPS is indeed running properly we will use a SIP phone called linphone to register to the OpenSIPS server. To follow this we first install linphonec, the CLI version of the linphone client, by following the steps given below.  (Note: I installed linphonec on a different machine but it can be done on the same machine that is running OpenSIPS as well)

1. Install linphonec: Fire command "apt-get install linphone-nogtk"

root@ubuntu:~# apt-get install linphone-nogtk
Reading package lists... Done
Building dependency tree     
.
.
.
root@ubuntu:~#


2. Register to OpenSIPS server using linphone client:

a. Start linphonec by firing command "linphonec"
b. Send a REGISTER message to OpenSIPS server by firing "register sip:user1@172.16.76.132 sip:172.16.76.132:5060". The IP address used here is the IP address of the OpenSIPS server. The username can be set to whatever is desired. The Registration successful messages conveys that the client successfully registered

root@ubuntu:~# linphonec
ALSA lib conf.c:4738:(snd_config_expand) Unknown parameters 0
ALSA lib control.c:954:(snd_ctl_open_noupdate) Invalid CTL default:0
XDG_RUNTIME_DIR (/run/user/1000) is not owned by us (uid 0), but by uid 1000! (This could e g happen if you try to connect to a non-root PulseAudio as a root user, over the native protocol. Don't do that.)
Ready
Warning: video is disabled in linphonec, use -V or -C or -D to enable.
linphonec> register sip:user1@172.16.76.132 sip:172.16.76.132:5060
linphonec> Registration on <sip:172.16.76.132:5060> successful.
linphonec>
root@ubuntu:~#

3. Crossverify client is registered at the OpenSIPS server: Now to crossverify that the client is registered, lets check the list of users currently registered to OpenSIPS.

On a terminal on the OpenSIPS machine fire the command "./opensipsctl ul show". It will show the currently registered users. As we can see below the user "user1" is registered with the OpenSIPS server. The Contact header specifies the contact of the user as "sip:user1@172.16.76.131"

root@ubuntu:/usr/sbin# ./opensipsctl ul show
Domain:: location table=512 records=1
    AOR:: user1
        Contact:: sip:user1@172.16.76.131;line=34937d1b1e5e27a Q=
            ContactID:: 1829798454851812362
            Expires:: 3510
            Callid:: 1216646129
            Cseq:: 1
            User-agent:: Linphone/3.6.1 (eXosip2/4.1.0)
            State:: CS_NEW
            Flags:: 0
            Cflags::
            Socket:: udp:172.16.76.132:5060
            Methods:: 4294967295
root@ubuntu:/usr/sbin#


A tcpdump trace at the OpenSIPS server shows that the following were the SIP messages that were exchanged during REGISTRATION :

23:44:09.743014 IP (tos 0x68, ttl 64, id 37937, offset 0, flags [DF], proto UDP (17), length 404)
    172.16.76.131.sip > 172.16.76.132.sip: [udp sum ok] SIP, length: 376
    REGISTER sip:172.16.76.132 SIP/2.0
    Via: SIP/2.0/UDP 172.16.76.131:5060;rport;branch=z9hG4bK1833702289
    From: <sip:user1@172.16.76.132>;tag=674536071
    To: <sip:user1@172.16.76.132>
    Call-ID: 1216646129
    CSeq: 1 REGISTER
    Contact: <sip:user1@172.16.76.131;line=34937d1b1e5e27a>
    Max-Forwards: 70
    User-Agent: Linphone/3.6.1 (eXosip2/4.1.0)
    Expires: 3600
    Content-Length: 0
   
23:44:09.743656 IP (tos 0x10, ttl 64, id 31501, offset 0, flags [DF], proto UDP (17), length 431)
    172.16.76.132.sip > 172.16.76.131.sip: [bad udp cksum 0xf2d4 -> 0x7ff8!] SIP, length: 403
    SIP/2.0 200 OK
    Via: SIP/2.0/UDP 172.16.76.131:5060;received=172.16.76.131;rport=5060;branch=z9hG4bK1833702289
    From: <sip:user1@172.16.76.132>;tag=674536071
    To: <sip:user1@172.16.76.132>;tag=bfa0967ee99eeb3ff1f10d7eea5636c7.3367
    Call-ID: 1216646129
    CSeq: 1 REGISTER
    Contact: <sip:user1@172.16.76.131;line=34937d1b1e5e27a>;expires=3600
    Server: OpenSIPS (2.3.0 (x86_64/linux))
    Content-Length: 0
   
Thanks for reading!

Saturday, 25 October 2014

SIPp Installation

      To install SIPp on a Fedora System follow the steps given below :

1. Checkout the sipp code from the SIPp svn. On the sourceforge download page click on the Code Tab and copy the http access svn link :

[root@localhost rishabh]$ svn checkout http://svn.code.sf.net/p/sipp/code/ sipp-code
A    sipp-code/doc
A    sipp-code/doc/trunk
.
.

This will create a folder called sipp-code in the current directory.


2. Now navigate to the directory "sipp-code/sipp/trunk"

[root@localhost rishabh]# cd sipp-code/sipp/trunk
[root@localhost trunk]#


3. Compile SIPp. Sipp can be compiled with the following options :
    a. OPEN-SSL support : required if SIPp need to be run over TLS
    b. PCAP-play support
    c. SCTP support

Here I will compile SIPp with all the above options. To do this run the command

     "autoreconf -ivf; ./configure --with-pcap --with-sctp --with-openssl; make".
  
I compiled SIPp on a freshly installed Fedora machine and encountered the following errors :

        a. Error : bash: autoreconf: command not found...
            Solution : Install autoconf with the command : "yum install autoconf"
      
        b. Error : Can't exec "aclocal": No such file or directory at /usr/share/autoconf/Autom4te/FileUtils.pm line 326.
            Solution :  Install automake with the command "yum install automake"
      
        c. Error : configure: error: ncurses library missing
            Solution : Install ncurses library with the command "yum install ncurses-devel.i686"
      
        d. Error : configure: error: <openssl/md5.h> header missing
            Solution : Install openssl and openssl-devel with the commands "yum install openssl" and "yum install openssl-devel.i686"

        e. Error : configure: WARNING: <netinet/sctp.h> header missing, but this is acceptable on Mac OS X Lion
            Solution : Install lksctp libraries with the command "yum install lksctp-tools-devel.i686".

        f.  Error : configure: error: <pcap.h> header missing  
            Solution : Install pcap libraries with command "yum install libpcap-devel.i686"

        g. Error : ./depcomp: line 772: exec: g++: not found  
            Solution : Install g++ with the command "yum install gcc-c++"


4. Next run "make install"

[root@localhost trunk]# make install
make[1]: Entering directory `/home/rishabh/sipp-code/sipp/trunk'
 /usr/bin/mkdir -p '/usr/local/bin'
  /usr/bin/install -c sipp '/usr/local/bin'
make[1]: Nothing to be done for `install-data-am'.
make[1]: Leaving directory `/home/rishabh/sipp-code/sipp/trunk'
[root@localhost trunk]#


5. To check if SIPp was installed correctly give the command "sipp -v". It will show the version of SIPp installed

[root@localhost trunk]# sipp -v

 SIPp v3.4-early-TLS-SCTP-PCAP, version unknown, built Oct 24 2014, 22:29:11.

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as
 published by the Free Software Foundation; either version 2 of
 the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public
 License along with this program; if not, write to the
 Free Software Foundation, Inc.,
 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

 Author: see source files.

[root@localhost trunk]#

SIPp

      SIPp is a command line, linux based, SIP traffic genarator. It has the capability of generating SIP traffic customized to user needs in order to test out SIP Proxies, B2BUAs, SIP media servers, SIP/x gateways etc. It features :

    1. TCP/TLS and UDP support over multiple sockets or multiplexed with retransmission management
    2. Dynamic display of statistics about running tests (call rate, round trip delay, and message statistics)
    3. Periodic CSV statistics dumps
    4. Dynamically adjustable call rates.

      SIPp uses XML files that provide the exact flow of SIP messages to be sent/received at the User Agents. these XML files are highly configurable to allow the user to simulate obscure scenarios effortlessly. The tool is especially useful for load testing as well as RFC compliance testing. 

Monday, 15 September 2014

Power over Ehternet (PoE) - Powering UP VOIP phones

What it is ?     


      Power over Ethernet or POE, is a system capable of transmitting electrical power + data over a single ethernet cable. In this sense it is similar to a USB cable which also does the same thing by supplying both a data as well as power connection to low power devices like keyboards/mice etc. However, the maximum length of USB cables is about 5 meters and they provide 2.5 W of power. POE, on the other hand allows longer cable lengths and more power(IEEE 802.3at-2009 POE standard, provides up to 25.5 W of power). POE operation can be obtained with a Category-3 cable for less power, however the IEEE standard requires CAT-5 cables or higher for high power levels.

      POE is nowadays widely used to power VOIP Phones, IP Cameras and Wireless access points.

Usage with VOIP Phones - Avaya 4621SW/9641G

 

       With VOIP phones like Avaya 4621SW or 9641G, PSE (Power Sourcing Equipment) capable switches are required for the phones to pull power from. Without a POE capable switch the phone will not be able to boot up. Such a switch is called an endspan. However such switches may not be readily available. In that case an intermediary device is required between a non POE capable switch and a POE powered device(PD). Such a device is called a midspan. An example of a midspan device is a POE injector.

A POE injector typically takes two inputs :

1. Power (48V DC generally)
2. Ethernet Cable (CAT-5 generally, from a switch)

      It is able to provide a single output in the form of a CAT-3/4/5 cable that is capable of transmitting both power and data. Such a POE injector can be easily used to power up the Avaya phones listed above. The POE injector I used, was capable of supplying 15.4 W power over a distance of 100 meters over a CAT-5 cable and came at a cheap cost of about $20.

Thursday, 13 March 2014

Monte Carlo Simulation - Estimation of Pi

What it is?


      Monte Carlo simulation is a problem solving technique which is used to approximate the solution for those problems which are infeasible or impossible to compute deterministically. It involves doing multiple trial runs or simulations for the problem at hand with suitable random values and observing the fraction of the values obeying some property or properties.


      As a simple example we try to calculate the value of Ï€ though a Monte Carlo Simulation. Consider the following figure where a circle is inscribed in a square. The side of the square is 2r units, and the radius of the circle is r units.
 
                                                     


        Now consider a single quadrant of the figure.

                                             

      Here,
 
                     

      So,
         


The Procedure


       Using Monte Carlo simulation we would calculate the value of Ï€ as follwos :


    1.  Consider the figure to be on a Cartesian plane.
    2.  Generate a large number of points.
    3.  Count the number of points lying inside the quadrant (n in the program below, this will represent the area of the shaded region). And count the total number of points (representing the area of the square)
    4.  Compute the value of Ï€ using the above formula. (4 * n / a)
    5.  To get better accuracy repeat steps 1-4 multiple times and take the average.

      Here is a perl program for this implementation :


$avg=0;
$no_of_times=10;
for($i=0;$i<$no_of_times;$i++)
{
        $a=int rand(1000000);
        $n=0;
        for($j=0;$j<$a;$j++)
        {
                $x=rand(1);
                $y=rand(1);
                if($x*$x + $y*$y <=1)
                {
                        $n++;
                }
        }
        $pi= 4.0*$n/$a;
        print "pi=$pi for $a tries\n";
        $avg+=$pi;
}
$avg/=$no_of_times;
print "pi=$pi\n";



The output is :


rishabh@ubuntu:~$ perl pi.pl 
pi=3.14276506439668 for 772633 tries
pi=3.14294672233944 for 542216 tries
pi=3.14328773838267 for 575283 tries
pi=3.14186626844556 for 203572 tries
pi=3.13502251907228 for 32639 tries
pi=3.13983316710037 for 800082 tries
pi=3.14055617007238 for 309186 tries
pi=3.13979483067174 for 818056 tries
pi=3.13903009074211 for 819355 tries
pi=3.1448481668525 for 365928 tries
Final value of pi=3.1448481668525
rishabh@ubuntu:~$ 


      The above program is just an example for demonstrating Monte Carlo Simulation. It is generally used in cases which are too complicated to solve analytically. It gives us the ability to examine more complex systems than we otherwise can. With Monte Carlo methods, a large system can be sampled in a number of random configurations, and that data can be used to describe the system as a whole.


Percolation Threshold



       Consider the problem of calculating the percolation threshold. 

Definitions :
 
Percolation : Consider a system as a 2-d grid composed of empty and filled sites. We say that the system percolates if there is a path from an empty site in the top row to one on the  bottom row through a set of empty sites in the grid.

For example consider the following systems :
System 1 does not percolate.
System 2 percolates.

   

    
      The Problem :  We are interested in the value of the site vacancy percentage(i.e. percolation threshold) for a system. If the site vacancy percentage is greater than this value then the system almost certainly percolates while if the site vacancy percentage is lesser then the system almost certainly does not percolate

      To determine this we can use Monte Carlo Simulation :

1.    Initialize all sites to be blocked.
2.    Repeat the following until the system percolates(Use union find algorithm):
       a.    Choose a site (row i, column j) uniformly at random among all blocked sites.
       b.    Open the site (row i, column j).
3.    The fraction of sites that are opened when the system percolates provides an estimate of the percolation threshold.

Repeat the above multiple times and take an average to get a more accurate result. It is generally around 0.593.

                                  
                  

Application of Percolation threshold :


1.  Given a composite systems comprised of randomly distributed insulating and metallic materials: what fraction of the materials need to be metallic so that the composite system is an electrical conductor?

2.  Given a porous landscape with water on the surface (or oil below), under what conditions will the water be able to drain through to the bottom (or the oil to gush through to the surface)?

3.  Given a network, how many nodes can be removed till the network looses connectivity? (Determination of network robustness and fragility).

 

Other applications of Monte Carlo Simulation :


1.  Finance : Monte Carlo methods are used in finance to value and analyze (complex) instruments, portfolios and investments by simulating the various sources of uncertainty affecting their value, and then determining their average value over the range of resultant outcomes.

2.  Searching for the best move in a game using a technique called Monte-Carlo tree search. Possible moves are organized in a search tree and a large number of random simulations are used to estimate the long-term potential of each move.

3.  In telecommunications, when planning a wireless network, design must be proved to work for a wide variety of scenarios that depend mainly on the number of users, their locations and the services they want to use. Monte Carlo methods are typically used to generate these users and their states. The network performance is then evaluated and, if results are not satisfactory, the network design goes through an optimization process.