Module: sip-router
Branch: ser_modules
Commit: c13d686fb4b289968c707a1f45fc648f9773eaee
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=c13d686…
Author: Tomas Mandys <tomas.mandys(a)iptel.org>
Committer: Tomas Mandys <tomas.mandys(a)iptel.org>
Date: Tue Jul 14 16:56:23 2009 +0000
- minor ERR message improvment
- new clean_rtpproxy_attrs script
---
.../iptrtpproxy/examples/clean_rtpproxy_attrs | 217 ++++++++++++++++++++
modules_s/iptrtpproxy/iptrtpproxy.c | 19 +-
2 files changed, 226 insertions(+), 10 deletions(-)
diff --git a/modules_s/iptrtpproxy/examples/clean_rtpproxy_attrs
b/modules_s/iptrtpproxy/examples/clean_rtpproxy_attrs
new file mode 100755
index 0000000..5c93078
--- /dev/null
+++ b/modules_s/iptrtpproxy/examples/clean_rtpproxy_attrs
@@ -0,0 +1,217 @@
+#!/usr/bin/env perl
+
+# $Id$
+
+# The script cleans up orphaned RTP proxy attributes from rtpproxy_attrs.
+# The orphaned attribute is the attribute having none of its sessions.
+# in iptrtpproxy session list.
+
+use warnings;
+#use strict;
+use DBI;
+
+my $version = "0.1";
+my $defConfigFile = "/etc/iptrtpproxy.cfg";
+my $configFile = $defConfigFile;
+my $verbose = 0;
+
+my $mysqlhost = 'localhost';
+my $mysqldb = 'ser';
+my $mysqluser = 'ser';
+my $mysqlpassword = 'heslo';
+
+use Config::IniFiles;
+
+sub dbg {
+ if ($verbose) {
+ print STDERR "DBG: $_[0]\n";
+ }
+}
+
+sub myexit ($$)
+{
+ my ($msg, $exitcode ) = @_;
+ print "# ERROR: $msg\n";
+ DBI->disconnect_all();
+ exit $exitcode;
+}
+
+
+sub do_exec($)
+{
+ my $cmd = $_[0];
+ dbg "Exec: $cmd";
+ my @res = `$cmd`;
+ dbg "Exit code: $?";
+ @res;
+}
+
+
+# parse config file to $cfg
+sub parseConfigFile($)
+{
+ my $fileName = $_[0];
+ dbg "Parsing config file '$fileName'\n" if $verbose;
+ my %switchboards = ();
+ goto ret unless -e $configFile;
+ my $cfg = Config::IniFiles->new( -file => $configFile, -default =>
"<default>" );
+
+ for my $section ($cfg->Sections) {
+ if ($section =~ /^[a-zA-Z_]+$/) {
+ print STDERR "Parsing section '$section'\n" if $verbose;
+ for my $param ('addr-a', 'addr-b', 'port-a', 'port-b')
{
+ my $val = $cfg->val($section, $param);
+ if ($val) {
+ $switchboards{$section}{$param} = $val;
+ }
+ }
+ }
+
+ }
+ret:
+ return %switchboards;
+}
+
+my $scriptName = $0;
+$scriptName =~ s!^.*/!!;
+
+sub printUsage {
+ print "$scriptName, Version: $version\n";
+ print "usage: $scriptName [-f <config>] [-v] [-h] [-H <db_host>] [-D
<database_name>] [-u <user>] [-p <password>]\n";
+
+}
+
+my $command = '';
+my $arg;
+
+while ($#ARGV >= 0) {
+ $arg = shift(@ARGV);
+ if ($arg eq '-f') {
+ if ($#ARGV < 0) {
+ print STDERR "ERROR: config file name required\n";
+ &printUsage();
+ exit(1);
+ } else {
+ $configFile = shift(@ARGV);
+ }
+ } elsif ($arg eq '-D') {
+ if ($#ARGV < 0) {
+ print STDERR "ERROR: database name required\n";
+ &printUsage();
+ exit(1);
+ } else {
+ $mysqldb = shift(@ARGV);
+ }
+ } elsif ($arg eq '-u') {
+ if ($#ARGV < 0) {
+ print STDERR "ERROR: user name required\n";
+ &printUsage();
+ exit(1);
+ } else {
+ $mysqluser = shift(@ARGV);
+ }
+ } elsif ($arg eq '-p') {
+ if ($#ARGV < 0) {
+ print STDERR "ERROR: password required\n";
+ &printUsage();
+ exit(1);
+ } else {
+ $mysqlpassword = shift(@ARGV);
+ }
+ } elsif ($arg eq '-H') {
+ if ($#ARGV < 0) {
+ print STDERR "ERROR: hostname required\n";
+ &printUsage();
+ exit(1);
+ } else {
+ $mysqlhost = shift(@ARGV);
+ }
+ } elsif ($arg eq '-v') {
+ $verbose++;
+ } elsif ($arg eq '-h') {
+ &printUsage();
+ exit(0);
+ } else {
+ print STDERR "ERROR: unknown argument '$arg'\n";
+ &printUsage();
+ exit(1);
+ }
+}
+
+dbg "Verbose: $verbose";
+
+dbg "Connecting: mysql://$mysqluser\@$mysqlhost/$mysqldb";
+myexit("Cannot connect database", 2) unless my $dbh =
DBI->connect("dbi:mysql:database=$mysqldb;host=$mysqlhost", $mysqluser,
$mysqlpassword);
+
+dbg "Reading rtpproxy_attrs";
+my %attrs = ();
+# copy rtpproxy_attrs to memory, we need snapshot of table
+$sth = $dbh->prepare("SELECT * FROM rtpproxy_attrs");
+$sth->execute();
+my $i = 0;
+while (my $href = $sth->fetchrow_hashref()) {
+
+ if ($href->{'name'} eq 'dlg_sess_ids') {
+ $attrs{$href->{'id'}} = $href->{'value'};
+ $i++;
+ }
+}
+$sth->finish();
+dbg "rtpproxy_attrs records: $i";
+
+my %cfgSwitchboards = &parseConfigFile($configFile);
+
+my %switchboards = ();
+for my $id (keys %cfgSwitchboards) {
+ dbg "Switchboard: $id";
+ $cmd = "iptrtpproxy list --addr-a $cfgSwitchboards{$id}{'addr-a'} --port-a
$cfgSwitchboards{$id}{'port-a'} --addr-b $cfgSwitchboards{$id}{'addr-b'}
--port-b $cfgSwitchboards{$id}{'port-b'} --force-switchboard-audit --format
tab";
+ my @lines = &do_exec($cmd);
+ my @l = split(/\t/, $lines[0]);
+ my $sess_id = -1;
+ my $tick = -1;
+ for my $i (0..$#l) {
+ if ($l[$i] eq 'sess-id') {
+ $sess_id = $i;
+ } elsif ($l[$i] eq 'created/tick') {
+ $tick = $i;
+ }
+ }
+ dbg "Lines: $#lines, Columns: sess-id: $sess_id, tick: $tick";
+ for my $i (1..$#lines) {
+ my @l = split(/\t/, $lines[$i]);
+ $switchboards{$id}{$l[$sess_id]} = $l[$tick];
+ }
+}
+
+dbg "Clean rtpproxy_attrs";
+# get iptrtpproxy switchboards
+my $delCnt = 0;
+$sth = $dbh->prepare("DELETE FROM rtpproxy_attrs WHERE id=?");
+loop: for my $id (keys %attrs) {
+ # format name:session_id/stamp (, session_id/stamp)*]
+ my $found = 0;
+ my ($name, $s) = split( /:/, $attrs{$id});
+ if (!defined $s) {
+ goto del;
+ }
+ my @ss = split( /,/, $s);
+ for $i (0..$#ss) {
+ my ($sess_id, $tick) = split ( /\//, $ss[$i]);
+ if ($sess_id eq "" || !defined $tick || $tick eq "") {
+ next;
+ }
+ if ($switchboards{$name}{$sess_id} && $switchboards{$name}{$sess_id} eq $tick)
{
+ dbg "Leave: $id=$attrs{$id}";
+ next loop;
+ }
+ }
+del:
+ dbg "Delete: $id=$attrs{$id}";
+ $sth->execute($id);
+ $delCnt++;
+}
+dbg "Deleted: $delCnt records";
+
+exit(0);
+
+
diff --git a/modules_s/iptrtpproxy/iptrtpproxy.c b/modules_s/iptrtpproxy/iptrtpproxy.c
index d8c2bc2..32cfdf6 100644
--- a/modules_s/iptrtpproxy/iptrtpproxy.c
+++ b/modules_s/iptrtpproxy/iptrtpproxy.c
@@ -329,10 +329,10 @@ static int parse_sdp_content(struct sip_msg* msg, struct sdp_session
*sess) {
}
break;
invalidate:
- if (sess_fl == 2) {
- sess->media[sess->media_count-1].active = 0;
- }
- break;
+ if (sess_fl == 2) {
+ sess->media[sess->media_count-1].active = 0;
+ }
+ break;
case 'm':
/* Media Announcements: m=<media> <port>[/<number of ports>]
<transport> <fmt list>, eg. m=audio 49170 RTP/AVP 0 */
switch (sess_fl) {
@@ -492,17 +492,17 @@ static int unserialize_ipt_session(str* session_ids, struct
ipt_session* sess) {
s.len = p-s.s;
sess->switchboard = find_switchboard(&s);
if (!sess->switchboard) {
- ERR(MODULE_NAME": unserialize_ipt_session: switchboard '%.*s' not
found\n", s.len, s.s);
+ ERR(MODULE_NAME": unserialize_ipt_session: '%.*s', switchboard
'%.*s' not found\n", session_ids->len, session_ids->s, s.len, s.s);
return -1;
}
if (p == pend) return 0;
if (*p != ':') {
- ERR(MODULE_NAME": unserialize_ipt_session: colon expected near
'%.*s'\n", pend-p, p);
+ ERR(MODULE_NAME": unserialize_ipt_session: '%.*s', colon expected near
'%.*s'\n", session_ids->len, session_ids->s, pend-p, p);
return -1;
}
do {
if (sess->stream_count >= MAX_MEDIA_NUMBER) {
- ERR(MODULE_NAME": unserialize_ipt_session: max.media number (%d) exceeded\n",
MAX_MEDIA_NUMBER);
+ ERR(MODULE_NAME": unserialize_ipt_session: '%.*s', max.media number (%d)
exceeded\n", session_ids->len, session_ids->s, MAX_MEDIA_NUMBER);
return -1;
}
p++;
@@ -512,7 +512,7 @@ static int unserialize_ipt_session(str* session_ids, struct
ipt_session* sess) {
s.s = p;
while (p < pend && (*p >= '0' && *p <= '9'))
p++;
if (p != pend && *p != '/') {
- ERR(MODULE_NAME": unserialize_ipt_session: '/' expected near
'%.*s'\n", pend-p, p);
+ ERR(MODULE_NAME": unserialize_ipt_session: '%.*s', '/' expected
near '%.*s'\n", session_ids->len, session_ids->s, pend-p, p);
return -1;
}
s.len = p-s.s;
@@ -527,7 +527,7 @@ static int unserialize_ipt_session(str* session_ids, struct
ipt_session* sess) {
while (p < pend && (*p >= '0' && *p <= '9'))
p++;
if (p != pend && *p != ',') {
sess->streams[sess->stream_count-1].sess_id = -1;
- ERR(MODULE_NAME": unserialize_ipt_session: comma expected near
'%.*s'\n", pend-p, p);
+ ERR(MODULE_NAME": unserialize_ipt_session: '%.*s', comma expected near
'%.*s'\n", session_ids->len, session_ids->s, pend-p, p);
return -1;
}
s.len = p-s.s;
@@ -732,7 +732,6 @@ static int rtpproxy_update(struct sip_msg* msg, char* _flags, char*
_session_ids
}
}
}
-serialize_ipt_session(&ipt_sess, &session_ids);
if (update_sdp_content(msg, GATE_A_TO_B(flags), &sdp_sess, &ipt_sess) < 0) {
/* delete all sessions ? */
return -1;