Location>code7788 >text

Extension registration and mutual dialing with kamailio

Popularity:711 ℃/2024-09-02 22:35:11

OS version: Debian 12.5_x64

kamailio version: 5.8.2

As a professional SIP server, kamailio can take on the role of a registration server. Today, we record the process of kamailio as a registration server to take over the registration of extensions and realize dialing each other.

I will expand on the following:

  • Module Configuration
  • Extension Account Addition
  • Extension interdialing without rtp proxy
  • Extension interdialing with rtp proxy
  • Supporting Resources Download

I. Module loading

Module Documentation Address:

/docs/modules/5./modules/

Module loading and configuration (just use kamailio's default configuration):

loadmodule "" 

# ----- registrar params -----
modparam("registrar", "method_filtering", 1)
/* uncomment the next line to disable parallel forking via location */
# modparam("registrar", "append_branches", 0)
/* uncomment the next line not to allow more than 10 contacts per AOR */
# modparam("registrar", "max_contacts", 10)
/* max value for expires of registrations */
modparam("registrar", "max_expires", 3600)
/* set it to 1 to enable GRUU */
modparam("registrar", "gruu_enabled", 0)

Installation of kamailio can be found in this article:

kamailio5.8 installation and configuration

II. Adding extensions

The command format is as follows:

kamctl add  <username> <password>

Example:

kamctl add 2001 123456

If the following error is prompted:

ERROR: domain unknown: use usernames with domain or set default domain in SIP_DOMAIN

Then you need to edit the kamctlrc file to configure the SIP_DOMAIN variable.

The account information is stored in the subscriber table with the following effect:

Verification can be registered using a softphone with the following results:

Multiple users can be added:

kamctl add 2001 123456
kamctl add 2002 123456

The data table effect is as follows:

III. Extension dialing

The intranet scenario is modeled here.

Extension 2001 and Extension 2002 registered address: 192.168.137.1

kamailio address: 192.168.137.71

The call model is as follows:

1、Store registration information

Configure the relevant modules, as exemplified below:

loadmodule "db_mysql.so"
loadmodule ""

modparam("usrloc", "db_url", "mysql://root:[email protected]/kamailio71")
modparam("usrloc", "db_mode", 1)
modparam("usrloc", "use_domain", MULTIDOMAIN)

Here db_mode is used with 1, which writes directly to the database, or with 2, which refreshes to the database at regular intervals.

2. Add extension routing

You can use the default LOCATION route, or you can customize the route, here is an example of a custom route for demonstration.

Configure the relevant modules, as exemplified below:

loadmodule ""

modparam("sqlops","sqlcon","db=>mysql://root:[email protected]/kamailio71")

sqlops is used to query the database "db=>" is the definition of the identity of the subsequent query with the db tag.

Add a route to query the database using the sql_xquery function;

route(EXTEN);

The routing is as follows:

route[EXTEN] {
    if(!is_method("INVITE"))
        return;
    xlog("L_INFO","rU is $rU \n");

    if (!registered("location")) {
        xlog("L_INFO","$rU not registered\n");
        exit;
    }
    sql_xquery("db","SELECT contact FROM location WHERE username='$rU';","rb");
    if ( $xavp(rb=>contact) == $null) {
        xlog("L_INFO","contact is  null  \n");
    } else {
        xlog("L_INFO","contact is   $xavp(rb=>contact)  \n");
        $du =  $xavp(rb=>contact);
    }
    xlog("L_INFO","rU is $rU , du is $du\n");

    route(RELAY);
    exit;
}

A screenshot of the configuration is shown below:

3. Call testing

Here, extension 2002 is used to call extension 2001 and the routing is successful with the following results:

The companion configuration file is available from the following sources:

Get it by replying 20240902 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

There is no rtp proxy here, the ip address of the phone is written inside the sdp:

The accompanying grab bag file is available from the following sources:

Get it by replying 20240902 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

Four, add rtp proxy

In the above steps, sip signaling proxy is implemented, if you want to proxy rtp data, you can use rtpproxy or rtpengine to realize it, here take rtpengine as an example for illustration.

The call model is as follows:

1. Install rtpengine

Can be installed via apt:

apt install rtpengine

It can also be installed via source code, for specific reference:

debian10 environment install rtpengine

Default profile:

/etc/rtpengine/

Start rtpengine:

systemctl start rtpengine

The effect is as follows:

2. Configure kamailio

Documentation:

Load the module and configure the parameters:

loadmodule ""

modparam("rtpengine", "rtpengine_sock", "udp:127.0.0.1:2223")

If you use the default profile's rules (), you need to define the variables first:

#!define WITH_NAT                                                                            
#!define WITH_RTPENGINE

The configuration effect is as follows:

Configure routing (in NATMANAGE, the default configuration):

 if(nat_uac_test("8")) {
        rtpengine_manage("SIP-source-address replace-origin replace-session-connection");
    } else {
        rtpengine_manage("replace-origin replace-session-connection");
    }

Custom routes can also be used (in NATMANAGE):

if (has_body("application/sdp")) {
    rtpengine_manage("trust-address replace-origin replace-session-connection");
}

Here is a demonstration of the default configuration, which is configured as follows:

Restart the kamailio service to take effect:

systemctl restart kamailio

The companion configuration file is available from the following sources:

Get it by replying 20240902 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

3. Proxy using rtpengine

Use 2002 to call 2001 and do a packet capture on kamailio.

The call effect is as follows:

After using rtpengine for rtp proxy, the ip address of kamailio is written inside the sdp:

Can play rtp audio streams using wireshark:

The accompanying grab bag file is available from the sources provided at the end of the article.

V. Resource downloads

This article covers the source code and related documentation, which can be obtained from the following sources:

Get it by replying 20240902 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

Okay, that's it, don't forget to like it haha!