A primitive SIP server in Perl

 

Emin Gabrielyan

Switzernet

2007-05-28

 

This web site presents a very primitive SIP server written in Perl. This server handles the registration messages by saving the locations and granting registration to everybody. It handles the INVITE transaction. The rest of the messages of the dialog are exchanged directly between SIP phones (e.g. BYE messages). The pieces of the script below are examples how the messages are handled in the script.

 

Handling of the registration:

  if($method eq "REGISTER")

  {

    print "$method\n";

    $ip=sender_ip($headers);

    save($headers);

    send_msg($ip,"SIP/2.0 200 OK",$headers,"");

  }

[cfg]

 

Handling of replies (where we remove the top-most Via header field and send the reply according to the next Via header field):

  if($method eq "SIP/2.0")

  {

    #handling replies of the transaction

    $msg=$headers;

    $msg=~s/(^|\n)Via:[^\n]*\n/$1/s;

    $ip=$msg;

    $ip=~s/.*(^|\n)(Via: +[^ ]+ +)(\d+(\.\d+){3}).*$/$3/s;

    send_msg($ip,$infoline,$msg,$body);

  }

[cfg]

 

In the Perl script of the PrimiSIP file write your IP address at the top of the file:

my $me = "192.168.1.15";

 

The script works in Unix or in Cygwin (Windows):

[png]

 

[png]

 

Below we show flow graphs of SIP messages passing through this SIP server.

 

Flow graph of registration of two phones:

[png], [pdf], [txt], [ps]

 

 

For this demo we used two Grandstream Budge Tone 100 phones with phone numbers +41215501111 and +41215509999.

 

Flow graph of INVITE transactions:

From +41215509999 to +41215501111:

[png], [pdf], [txt], [ps]

 

From +41215501111 to +41215509999:

[png], [pdf], [txt], [ps]

 

*   *   *