Knowledge Updates

OpenSIPS Development: An important resource for joined communication

GVenture Technology takes on a pleasant ride to develop OpenSIPS applications. OpenSIPS (Open SIP Server) is a developed Open Source implementation of a SIP server that runs on Linux platforms and play in the infrastructure of an Internet Telephony Service Provider and it includes application-level functionality.

Why use OpenSIPS?

  • No vendor trap
  • Faster development cycle
  • Split work between parties
  • Easy synchronization with the mainstream via contributions ⇒ Unified effort for development
  • Performance and flexibility

Prerequisites:

  • Since OpenSIPS is known to work on most nix based systems, installing and running OpenSIPS requires some very basic Linux knowledge.
  • Also, since OpenSIPS is a SIP proxy, you will most likely need some basic SIP knowledge in order to deploy/debug OpenSIPS in some more advanced scenarios.
  • Last but not least, you will need some basic programming logic knowledge – The OpenSIPS configuration file is text-based, written in an OpenSIPS custom language, very similar to the C language.

Requirements:

gcc / suncc / icc : gcc >= 2.9x; 4.[012] recommended (it will work with older version but it might require some options tweaking for best performance)

  • bison or yacc (Berkley yacc)
  • flex
  • GNU make (on Linux this is the standard “make”, on FreeBSD and Solaris is called “gmake”) version >= 3.79.
  • sed and tr (used in the makefiles)
  • GNU tar (“gtar” on Solaris) and gzip if you want “make tar” to work
  • GNU install or BSD install (on Solaris “ginstall”) if you want “make install”, “make bin”, “make sunpkg” to work
  • openssl if you want to compile the TLS support
  • libsctp if you want to compile the SCTP support
  • libmysqlclient & libz (zlib) -libs and devel headers- if you want mysql DB support (the db_mysql module)
  • libpq / postgresql -libs and devel headers- if you want postgres DB support (the db_postgres module)
  • unixodbc -libs and devel headers- if you want unixodbc DB support (the db_unixodbc module)
  • libexpat if you want the jabber gateway support (the jabber module) or the XMPP gateway support
  • libxml2 if you want to use the cpl-c (Call Processing Language) or the presence modules (presence and pua*)
  • libradius-ng -libs and devel headers- if you want to use functionalities with radius support – authentication, accounting, group support, etc
  • unixodbc – libs and devel headers – if you want UNIXODBC support as DB underlayer
  • libxmlrpc-c3 – libs and devel headers – if you want to have XML-RPC support for the Management interface (MI)
  • libperl – libs and devel headers – if you want PERL connector to support perl scripting from you config file (perl module)
  • libsnmp9 – libs and devel headers – if you want SNMP client functionality (SNMP AgentX subagent) for opensips
  • libldap libs and devel headers v2.1 or greater – if you want LDAP support
  • libconfuse and devel headers – if you want to compile the carrier route module

Installation:

By following above 2 processes we have installed the OpenSIPS server on our system now we have to configure the server corresponding to our requirement.

OpenSIPS Installation:

1. System Update:  Update your system with latest updates and security patches using the following command.

1
# apt-get update

2. Installing Dependencies: After system update you need to install the missing packages using the ‘apt-get’ command if these are not already installed.

1
# apt-get install build-essential openssl bison flex

3. Installing MySQL Server: Let’s run the following commands to install MySQL server and it development libraries.

1
# apt-get install mysql-server libmysqlclient-dev

4. Download and extract OpenSIPS Package: We are going to download the package into the following directory using ‘wget’ command and the extract within th esame directory.

1
2
3
# cd /usr/src
# wget http://opensips.org/pub/opensips/latest/opensips-2.1.2.tar.gz
# tar -zxvf opensips-2.1.2.tar.gz

5. Compiling OpenSIPS Source: Moving step ahead let’s move into the opensips directory and run the command below to start the compilation process.

1
2
# cd opensips-2.1.2/
# make all

6. Installing OpenSIPS Source: Once the compilation process completes, you can start making its installation as shown below.

1
# make install

7. OpenSIPS Configuration: The OpenSIPS has been installed, now we are going to configure some of its basic parameters and startup script. Let’s first create a new directory for the OpenSIPS run files.

1
# mkdir /var/run/opensips

Now move to the following ‘debians’ directory and list the files in it.

1
# cd packaging/debian/

8. Starting OpenSIPS Service: All necessary configurations has been setup to proceed on starting OpenSIPS service. Simply run the following command to start OpenSIPS and the check its status, that should be active and running as shown in the image.

1
2
# /etc/init.d/opensips start
# systemctl status opensips

You can also check the state of OpenSIPS services and the port on which its running that is ‘5060’ by using the below commands.

1
2
# ps -ef | grep opensips
# netstat -alnp | grep opensips

CONFIGURATION

We have following directory where all files has be installed:

1
2
/usr/local/lib/opensips/ directory where all modules are installed
/usr/local/etc/opensips/ directory where all configuration you have make

FOR LOAD

Under Load-balancing format The below setting will create alternate call from one server and other alternately it will create same number of calls from each gateway.

1
2
3
4
5
6
log_stderror=yes
log_facility=LOG_LOCAL0
fork=yes
children=4
debug=2
listen=5060

MODULE PATH

1
mpath="/usr/local/lib/opensips/modules/"

Modules Section

DB_MYSQL

1
2
3
4
loadmodule "db_mysql.so"
modparam("db_mysql", "ping_interval", 300)
modparam("db_mysql", "timeout_interval", 10)
modparam("db_mysql", "auto_reconnect", 1)

SL

1
2
loadmodule "sl.so"
modparam("sl", "enable_stats", 1)

TM

1
2
3
4
5
6
7
8
9
10
11
12
loadmodule "tm.so"
modparam("tm", "fr_timer", 30)
modparam("tm", "fr_inv_timer", 120)
modparam("tm", "wt_timer", 5)
modparam("tm", "delete_timer", 2)
#modparam("tm", "noisy_ctimer", 0)
modparam("tm", "ruri_matching", 1)
modparam("tm", "via1_matching", 1)
modparam("tm", "unix_tx_timeout", 2)
modparam("tm", "restart_fr_on_each_reply", 1)
modparam("tm", "pass_provisional_replies", 0)
modparam("tm", "fr_inv_timer_avp", "$avp(s:callee_fr_inv_timer)")

RR

1
2
3
4
5
loadmodule "rr.so"
modparam("rr", "enable_full_lr", 0)
modparam("rr", "append_fromtag", 1)
modparam("rr", "enable_double_rr", 0)
modparam("rr", "add_username", 0)

MAXFWD

1
loadmodule "maxfwd.so"

USRLOC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
loadmodule "usrloc.so"
modparam("usrloc", "user_column", "username")
modparam("usrloc", "domain_column", "domain")
modparam("usrloc", "contact_column", "contact")
modparam("usrloc", "expires_column", "expires")
modparam("usrloc", "q_column", "q")
modparam("usrloc", "callid_column", "callid")
modparam("usrloc", "cseq_column", "cseq")
modparam("usrloc", "methods_column", "methods")
modparam("usrloc", "flags_column", "flags")
modparam("usrloc", "user_agent_column", "user_agent")
modparam("usrloc", "received_column", "received")
modparam("usrloc", "socket_column", "socket")
modparam("usrloc", "use_domain", 0)
modparam("usrloc", "desc_time_order", 0)
modparam("usrloc", "timer_interval", 60)
modparam("usrloc", "db_mode", 2)
modparam("usrloc", "db_url" , "mysql://opensips:opensips@database/opensips")
modparam("usrloc", "matching_mode", 0)
modparam("usrloc", "cseq_delay", 20)
modparam("usrloc", "nat_bflag", 6)

REGISTRAR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
loadmodule "registrar.so"
modparam("registrar", "method_filtering", 1)
modparam("registrar", "default_expires", 3600)
modparam("registrar", "min_expires", 60)
modparam("registrar", "max_expires", 0)
modparam("registrar", "default_q", 0)
modparam("registrar", "append_branches", 1)
modparam("registrar", "case_sensitive", 0)
modparam("registrar", "received_param", "received")
modparam("registrar", "max_contacts", 10)
modparam("registrar", "retry_after", 0)
modparam("registrar", "path_mode", 2)
modparam("registrar", "path_use_received", 0)
#modparam("registrar", "received_avp", "$avp(i:801)")
modparam("registrar", "aor_avp", "$avp(i:3223)")
modparam("registrar", "received_avp", "$avp(s:rcv)")

TEXTOPS

1
loadmodule "textops.so"

MI_FIFO

1
2
3
4
5
6
7
loadmodule "mi_fifo.so"
modparam("mi_fifo", "fifo_name", "/tmp/opensips_fifo")
#modparam("mi_fifo", "fifo_mode", 0660)
#modparam("mi_fifo", "fifo_group", "opensips")
#modparam("mi_fifo", "fifo_user", "opensips")
#modparam("mi_fifo", "reply_dir", "/tmp/")
#modparam("mi_fifo", "reply_indent", "\t")

URI

1
loadmodule "uri.so"

URI_DB

1
2
3
4
5
6
7
8
loadmodule "uri_db.so"
modparam("uri_db", "db_url", "mysql://opensips:opensips@database/opensips")
modparam("uri_db", "db_table", "uri")
modparam("uri_db", "use_uri_table", 1)
modparam("uri_db", "use_domain", 1)
modparam("uri_db", "user_column", "username")
modparam("uri_db", "domain_column", "domain")
modparam("uri_db", "uriuser_column", "uri_user")

xlog

1
2
3
loadmodule "xlog.so"
modparam("xlog", "force_color", 1)
modparam("xlog", "buf_size", 4096)

acc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
loadmodule "acc.so"
modparam("acc", "db_url", "mysql://opensips:opensips@database/opensips")
modparam("acc", "early_media", 1)
modparam("acc", "report_ack", 1)
modparam("acc", "report_cancels", 1)
modparam("acc", "detect_direction", 1)
modparam("acc", "failed_transaction_flag", 3)
modparam("acc", "log_flag", 1)
modparam("acc", "log_missed_flag", 2)
modparam("acc", "db_flag", 1)
modparam("acc", "db_missed_flag", 2)
modparam("acc", "db_extra" , "ua=$hdr(User-Agent); src_user=$avp(i:100);
src_domain=$fd; src_port=$sp; dst_user=$rU; dst_domain=$rd; dst_port=$rp;
message=$mf; unixtime=$Ts; avp_var=$avp(s:can_uri); callstate=$avp(s:callstate);
usercode=$avp(i:1000); recieved=$avp(s:rcv); aor_avp=$avp(i:3223)")

auth

1
2
3
4
loadmodule "auth.so"
modparam("auth", "nonce_expire", 300)
modparam("auth", "rpid_suffix", ";party=calling;id-type=subscriber;screen=yes")
modparam("auth", "rpid_avp", "$avp(s:rpid)")

AVPOPS

1
2
3
4
5
6
7
8
9
10
11
loadmodule "avpops.so"
modparam("avpops", "db_url", "mysql://opensips:opensips@database/opensips")
modparam("avpops", "avp_table", "usr_preferences")
modparam("avpops", "use_domain", 0)
modparam("avpops", "uuid_column", "uuid")
modparam("avpops", "username_column", "username")
modparam("avpops", "domain_column", "domain")
modparam("avpops", "attribute_column", "attribute")
modparam("avpops", "value_column", "value")
modparam("avpops", "type_column", "type")
#modparam("avpops", "avp_aliases", "c_uri" )

DIALOG

1
2
3
4
5
6
loadmodule "dialog.so"
modparam("dialog", "enable_stats", 1)
modparam("dialog", "default_timeout", 21600)
modparam("dialog", "db_url", "mysql://opensips:opensips@database/opensips")
modparam("dialog", "db_mode", 1)
modparam("dialog", "dlg_flag", 4)

SIPTRACE

1
2
3
4
loadmodule "siptrace.so"
modparam("siptrace","db_url","mysql://opensips:opensips@database/opensips")
modparam("siptrace", "trace_on", 1)
modparam("siptrace", "trace_flag", 22)

MI_DATAGRAM

1
2
3
loadmodule "mi_datagram.so"
modparam("mi_fifo", "fifo_name", "/tmp/opensips_fifo")
modparam("mi_datagram", "socket_name", "udp:10.10.2.144:8080")

Routing Logic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
 main request routing logic
 
route{
        sip_trace(); # Recording all sip signal for each calls connect through this server
        xlog("Recive call variable : $avp(s:rcv) $avp(i:3223)\n"); # prints the cli log
 
        if($fU==NULL){
                $avp(i:100)="0";
        }
        else
                $avp(i:100)=$fU;
 
        if (!mf_process_maxfwd_header("10")) {
                sl_send_reply("483","Too Many Hops");
                exit;
        }
 
        if (has_totag()) {
                if (loose_route()) {
                        if(is_method("BYE")){
		        #setting for call accounting
                                setflag(1);
                                setflag(3);
                        }
                        route(1);
                } else {
                        if ( is_method("ACK") ) {
                                if ( t_check_trans() ) {
                                        t_relay();
                                        exit;
                                } else {
                                        exit;
                                }
                        }
                        sl_send_reply("404","Not here");
                }
                exit;
        }
 
        if (is_method("CANCEL"))
        {
                if (t_check_trans())
                        t_relay();
                exit;
        }
 
       t_check_trans();
 
        # record routing
        if (!is_method("REGISTER|MESSAGE")){
                record_route();
        }
 
        # account only INVITEs
        if (is_method("INVITE")) {
	   if(from_uri=~".*@10.10.2.136" || from_uri=~".*@10.10.2.134"){
 
                }
                else{
                        avp_db_query("select id_skillgroup from subscriber u where username='$rU'", "$avp(i:2001)");
                        avp_db_query("select id from ast_skillgroup where id='$avp(i:2001)'", "$avp(i:2002)");
                        avp_db_query("select id, serverip, callerid from ast_servers where id_skillgroup='$avp(i:2002)'
                                      order by calls asc limit 1", "$avp(i:2003);$avp(i:2004);$avp(i:2005)");
                        avp_db_query("Update ast_servers SET calls=calls+1 WHERE id=$avp(i:2003)");
 
                        avp_pushto("$ru/domain","$avp(i:2004)");
 
                        if (!t_relay()) {
                                sl_reply_error();
                        };
                        exit;
                }
        }
 
         if (!uri==myself)
        {
                append_hf("P-hint: outbound\r\n");
                route(1);
        }
 
        if (is_method("PUBLISH"))
        {
                sl_send_reply("503", "Service Unavailable");
                exit;
        }
 
        if (is_method("REGISTER"))
        {
                # authenticate the REGISTER requests (uncomment to enable auth)
                xlog("User credentals : $avp(i:1000)\n");
                if (www_authorize("", "subscriber"))
                {
                        xlog("User credentals : $avp(i:1000)\n");
                        if (!save("location"))
                                sl_reply_error();
 
                        exit;
                }
                else{
                        www_challenge("", "0");
                        exit;
                }
 
                if (!check_to())
                {
                        sl_send_reply("403","Forbidden auth ID");
                        exit;
                }
        }
 
        if ($rU==NULL) {
                sl_send_reply("484","Address Incomplete");
                exit;
        }
 
        if (!lookup("location")) {
                switch ($retcode) {
                        case -1:
                        case -3:
                                t_newtran();
                                t_reply("404", "Not Found");
                                exit;
                        case -2:
                                sl_send_reply("405", "Method Not Allowed");
                                exit;
                }
        }
 
        setflag(2);
        xlog("Script is at the end\n");
        route(1);
   }
 
   route[1] {
        # for INVITEs enable some additional helper routes
        if (is_method("INVITE")) {
                avp_db_query("select uuid from subscriber where username = '$rU'", "$avp(s:callee_uuid)");
                $avp(s:can_uri) = $ru;
                xlog("User credentals : $avp(s:callee_uuid)\n");
                t_on_branch("2");
                t_on_reply("2");
                t_on_failure("1");
        }
 
        if (!t_relay()) {
                sl_reply_error();
        };
        exit;
   }
 
    branch_route[2] {
        xlog("new branch at $ru\n");
        $avp(s:can_uri) = $ru;
        $avp(s:callstate)="CALL REROUTED";
        add_rr_param(";foo=true;user=vivek");
        xlog("Call is forwared to asterisk\n");
   }
 
    onreply_route[2] {
        xlog("incoming reply\n");
   }
 
    failure_route[1] {
        if (t_was_cancelled()) {
                exit;
        }
   }

Above configuration is sampled corresponding to load balanced format where calls are forwarded to different gateway corresponding to our requirements and availability of gateway.

GVenture Technology will provide with vicinity to make yourself convenient and to have carefree talk to tell us about your project and then we can offer you all the bits and pieces essential for it.