Discussion:
[SR-Users] routing SIP REGISTER with app_perl
Vik Killa
2014-09-16 01:37:06 UTC
Permalink
Hello,
I'm learning Kamailio. My ultimate goal is to be able to route SIP messages
from User Agents to specific FS boxes.

I'm starting with routing REGISTER messages.
I used app_perl to randomly lookup one of the FS box's IP Address from
postgres database, then I stored the IP in avp and then in hash. It seemed
like overkill but it was the only way I could keep the FS IP address in
memory between the first REGISTER and REGISTER with Auth.

my app_perl script sets the IP as follows:

my $sth = $dbh->prepare("SELECT ip_address FROM host_machine WHERE
is_online = true ORDER BY random() LIMIT 1");
$sth->execute();
my $host_ip_address = $sth->fetchrow();
Kamailio::AVP::add(10, "sip:$host_ip_address:5060");

And here is my kamailio.cfg for REGISTERs

route[REGISTRAR]
{

if (!is_method("REGISTER|PUBLISH"))
return;

if (is_present_hf("Authorization")) {
add_path_received();
$avp(i:10) = $sht(a=>$ci::dest_server);
avp_pushto("$du", "$avp(i:10)");
} else {
perl_exec("random_server");
avp_pushto("$du", "$avp(i:10)");
$sht(a=>$ci::dest_server) = $avp(i:10);

}
t_relay();

exit;
}


"random_server" is the perl script to get the FS IP address...
Seems crude but works...
I'm sure there is probably a more elegant way to do what im trying so any
advice and criticism is appreciated, thank you!

/V
Daniel-Constantin Mierla
2014-09-16 15:52:23 UTC
Permalink
Hello,

if you want to rely on kamailio to memorize where to send the following
register, the hash table is the right tool.

Cheers,
Daniel
Post by Vik Killa
Hello,
I'm learning Kamailio. My ultimate goal is to be able to route SIP
messages from User Agents to specific FS boxes.
I'm starting with routing REGISTER messages.
I used app_perl to randomly lookup one of the FS box's IP Address from
postgres database, then I stored the IP in avp and then in hash. It
seemed like overkill but it was the only way I could keep the FS IP
address in memory between the first REGISTER and REGISTER with Auth.
my $sth = $dbh->prepare("SELECT ip_address FROM host_machine WHERE
is_online = true ORDER BY random() LIMIT 1");
$sth->execute();
my $host_ip_address = $sth->fetchrow();
Kamailio::AVP::add(10, "sip:$host_ip_address:5060");
And here is my kamailio.cfg for REGISTERs
route[REGISTRAR]
{
if (!is_method("REGISTER|PUBLISH"))
return;
if (is_present_hf("Authorization")) {
add_path_received();
$avp(i:10) = $sht(a=>$ci::dest_server);
avp_pushto("$du", "$avp(i:10)");
} else {
perl_exec("random_server");
avp_pushto("$du", "$avp(i:10)");
$sht(a=>$ci::dest_server) = $avp(i:10);
}
t_relay();
exit;
}
"random_server" is the perl script to get the FS IP address...
Seems crude but works...
I'm sure there is probably a more elegant way to do what im trying so
any advice and criticism is appreciated, thank you!
/V
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
--
Daniel-Constantin Mierla
http://twitter.com/#!/miconda - http://www.linkedin.com/in/miconda
Next Kamailio Advanced Trainings 2014 - http://www.asipto.com
Sep 22-25, Berlin, Germany
Vik Killa
2014-09-16 16:37:11 UTC
Permalink
Thanks Daniel,
I want to remove the htable entry after the register goes through, and I
also want to check if the htable entry exists later.

I see

sht_rm_value_re("ha=>.*");

to remove the htable entry but how do i later check if the key exists?

Thanks
/V

On Tue, Sep 16, 2014 at 11:52 AM, Daniel-Constantin Mierla <
Post by Vik Killa
Hello,
if you want to rely on kamailio to memorize where to send the following
register, the hash table is the right tool.
Cheers,
Daniel
Hello,
I'm learning Kamailio. My ultimate goal is to be able to route SIP
messages from User Agents to specific FS boxes.
I'm starting with routing REGISTER messages.
I used app_perl to randomly lookup one of the FS box's IP Address from
postgres database, then I stored the IP in avp and then in hash. It seemed
like overkill but it was the only way I could keep the FS IP address in
memory between the first REGISTER and REGISTER with Auth.
my $sth = $dbh->prepare("SELECT ip_address FROM host_machine WHERE
is_online = true ORDER BY random() LIMIT 1");
$sth->execute();
my $host_ip_address = $sth->fetchrow();
Kamailio::AVP::add(10, "sip:$host_ip_address:5060");
And here is my kamailio.cfg for REGISTERs
route[REGISTRAR]
{
if (!is_method("REGISTER|PUBLISH"))
return;
if (is_present_hf("Authorization")) {
add_path_received();
$avp(i:10) = $sht(a=>$ci::dest_server);
avp_pushto("$du", "$avp(i:10)");
} else {
perl_exec("random_server");
avp_pushto("$du", "$avp(i:10)");
$sht(a=>$ci::dest_server) = $avp(i:10);
}
t_relay();
exit;
}
"random_server" is the perl script to get the FS IP address...
Seems crude but works...
I'm sure there is probably a more elegant way to do what im trying so any
advice and criticism is appreciated, thank you!
/V
_______________________________________________
--
Daniel-Constantin Mierlahttp://twitter.com/#!/miconda - http://www.linkedin.com/in/miconda
Next Kamailio Advanced Trainings 2014 - http://www.asipto.com
Sep 22-25, Berlin, Germany
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
Daniel-Constantin Mierla
2014-09-16 17:01:18 UTC
Permalink
Hello,
Post by Vik Killa
Thanks Daniel,
I want to remove the htable entry after the register goes through, and
I also want to check if the htable entry exists later.
I see
sht_rm_value_re("ha=>.*");
this is going to remove all the items from the hash table -- perhaps you
don't want to do that.

To remove the entry you added for register once the registration was
successful, add reply_route where you check if it is REGISTER and the
code is 200, then remove the item using the call id (same key as you
used to add the item).

$sht(a=>$ci::dest_server) = $null;

Assigning $null removes the respective item from hash table.
Post by Vik Killa
to remove the htable entry but how do i later check if the key exists?
$null can be used to test if key exists or not, like if($sht(a=>x)==$null)

Cheers,
Daniel
Post by Vik Killa
Thanks
/V
On Tue, Sep 16, 2014 at 11:52 AM, Daniel-Constantin Mierla
Hello,
if you want to rely on kamailio to memorize where to send the
following register, the hash table is the right tool.
Cheers,
Daniel
Post by Vik Killa
Hello,
I'm learning Kamailio. My ultimate goal is to be able to route
SIP messages from User Agents to specific FS boxes.
I'm starting with routing REGISTER messages.
I used app_perl to randomly lookup one of the FS box's IP Address
from postgres database, then I stored the IP in avp and then in
hash. It seemed like overkill but it was the only way I could
keep the FS IP address in memory between the first REGISTER and
REGISTER with Auth.
my $sth = $dbh->prepare("SELECT ip_address FROM host_machine
WHERE is_online = true ORDER BY random() LIMIT 1");
$sth->execute();
my $host_ip_address = $sth->fetchrow();
Kamailio::AVP::add(10, "sip:$host_ip_address:5060");
And here is my kamailio.cfg for REGISTERs
route[REGISTRAR]
{
if (!is_method("REGISTER|PUBLISH"))
return;
if (is_present_hf("Authorization")) {
add_path_received();
$avp(i:10) = $sht(a=>$ci::dest_server);
avp_pushto("$du", "$avp(i:10)");
} else {
perl_exec("random_server");
avp_pushto("$du", "$avp(i:10)");
$sht(a=>$ci::dest_server) = $avp(i:10);
}
t_relay();
exit;
}
"random_server" is the perl script to get the FS IP address...
Seems crude but works...
I'm sure there is probably a more elegant way to do what im
trying so any advice and criticism is appreciated, thank you!
/V
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
--
Daniel-Constantin Mierla
http://twitter.com/#!/miconda <http://twitter.com/#%21/miconda> -http://www.linkedin.com/in/miconda
Next Kamailio Advanced Trainings 2014 -http://www.asipto.com
Sep 22-25, Berlin, Germany
_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users
--
Daniel-Constantin Mierla
http://twitter.com/#!/miconda - http://www.linkedin.com/in/miconda
Next Kamailio Advanced Trainings 2014 - http://www.asipto.com
Sep 22-25, Berlin, Germany
Loading...