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
|
use Kamailio qw ( log );
use Kamailio::Constants;
use IPC::Shareable;
my $foo = 0;
my %lastcalltimes;
my %lastcallids;
# This function shows that normal persistent variables are _not_ valid between multiple instances of the Kamailio.
# With the default setup of 4 children, the value logged is only incremented every 4th time.
sub limited {
use vars qw ($foo);
log(L_INFO, "My persistent variable is $foo\n");
$foo++;
return 1;
}
# Only one call per hour to each recipient
# By using the IPC::Shareable perl module, real persistence between multiple instances can be achieved
# Consider using the locking mechanisms available in IPC::Shareable when using in real world environments.
sub onceperhour {
my $m = shift;
tie %lastcalltimes, IPC::Shareable, { key => "lstt", create => 1, destroy => 1 } or die "Error with IPC::Shareable";
tie %lastcallids, IPC::Shareable, { key => "lsti", create => 1, destroy => 1 } or die "Error with IPC::Shareable";
if ($m->getMethod() eq "INVITE") {
my $dst = $m->getRURI();
my $currentid = $m->getHeader("Call-ID");
log(L_INFO, "invite to $dst");
my $lasttime = %lastcalltimes->{$dst};
log(L_INFO, "lasttime is $lasttime");
if ($lasttime) { # Value set.
log(L_INFO, "I was already called at $lasttime");
if ((time() - $lasttime) < 3600) {
if ($currentid eq %lastcallids->{$dst}) {
log(L_INFO, "I know this call already. Doing nothing.");
} else {
log(L_INFO, "replying, return 1");
$m->sl_send_reply("488", "You already called this hour");
return 1;
}
}
}
%lastcalltimes->{$dst} = time();
%lastcallids->{$dst} = $currentid;
}
return -1;
}
|