(RADIATOR) patch for AuthSQL module to calculate real day time ACCTSESSIONTIME

Hugh Irvine hugh at open.com.au
Wed Dec 11 23:54:09 CST 2002


Hello Ganbold -

I would suggest that you use a hook (PreAuthHook or PostAuthHook  
probably), rather than alter the code in AuthSQL.pm, as you can then  
upgrade Radiator versions without having to migrate your changes. In  
fact, I would probably use a PreAuthHook, and calculate the time used  
and store it in the incoming request as a pseudo-attribute called  
Time-Used for example, then I would simply add the appropriate  
AcctColumnDef or AcctSQLStatement to deal with it.

You will find some example hooks in the file "goodies/hooks.txt".

regards

Hugh


On Thursday, Dec 12, 2002, at 16:10 Australia/Melbourne, Ganbold wrote:

> Hi,
>
> I just wrote some codes which calculates user's connected time. We  
> have some PREPAID users and every time
> when they use Internet during day time we have to subtract used time  
> from TIMELEFT column in MySQL table.
> But it is not always.
>
> if user is connected around 23:00:00 and used 3 hours we have to  
> subtract only one hour(3600) from TIMELEFT. If user is connected  
> around 06:00:00 and used 3 hours we have to subtract 2 hours(7200)  
> from TIMELEFT.
>
> If user is connected 01:00:00 and used 2 hours then we don't have to  
> subtract anything.
> If user is connected 10:00:00 and used 3 hours then we have to just  
> subtract ACCTSESSIONTIME.
>
> Day time means 	07:00:00 - 23:59:59	(this time is charged and we want  
> to subtract used time from TIMELEFT)
> Night time means 	00:00:00 - 07:00:00	(this time is free and we don't  
> want to subtract used time from TIMELEFT)
>
> Following is the code I want to add to AuthSQL.pm module.
> Can somebody look at the code and give me some recommendation on that?
> Can I add it to AuthSQL.pm module and use?
>
>
> TIA,
>
> Ganbold
>
> ----------------------------------------------------------------------- 
> --------------------------------------------------------
> # patch for AuthSQL.pm which calculates day only duration
> # Day means from 07:00:00 to 23:59:59
> # if connection continued over 00:00:00 or 07:00:00 we have to get  
> real duration
>
> use Time::Local;
>
> # in handle_request
> .
> .
> .
>         if ($p->getAttrByNum($Radius::Radius::ACCT_STATUS_TYPE) eq  
> 'Stop')
>         {
>             my $session_time =  
> $p->getAttrByNum($Radius::Radius::ACCT_SESSION_TIME) + 0;
>             my $user_name = $p->getUserName;
>             my $time_stamp = $p->{RecvTime} - int  
> $p->getAttrByNum($Radius::Radius::ACCT_DELAY_TIME);
>             my $duration = $session_time;
>
>             ########### This lines added to calculate real duration  
> during day time ###########################
>
>
>                 my ($rduration, $i);
>                 my ($start_stamp);
>
>                 my ($start_hour,$end_hour);
>                 my ($start_day, $end_day);
>
>                 my  
> ($s_tstamp_at_0,$s_tstamp_at_7,$e_tstamp_at_0,$e_tstamp_at_7);
>
>                 my ($diff, $day_diff, @numDays, $thismonth);
>                 my  
> ($sec,$min,$hour,$mday,$mon,$wday,$yday,$isdst,$year);
>
>                 # initialize first real duration
>                 $rduration = 0;
>
>                 $start_stamp = $time_stamp - $duration;
>
>                 # stamp at 0 and 7 o'clock of start day
>                 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)  
> = localtime($start_stamp);
>                 $s_tstamp_at_0 = timelocal(0,0,0,$mday,$mon,$year);
>                 $s_tstamp_at_7 = timelocal(0,0,7,$mday,$mon,$year);
>
>                 # calculating number of days in month
>                 for($i=0; $i<12; $i += 2){
>                         $numDays[$i] = 31;
>                 }
>                 for($i=1; $i<12; $i += 2){
>                         $numDays[$i] = 30;
>                 }
>
>                 # calculating number of days in February
>                 if(($year % 4) != 0){
>                         $numDays[1] = 28;
>                 }elsif(($year % 400) == 0){
>                         $numDays[1] = 29;
>                 }elsif(($year % 100) == 0){
>                         $numDays[1] = 28;
>                 }else{
>                         $numDays[1] = 29;
>                 }
>                 $thismonth = $mon;
>
>                 # stamp at 0 and 7 o'clock of stop day
>                 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)  
> = localtime($time_stamp);
>                 $e_tstamp_at_0 = timelocal(0,0,0,$mday,$mon,$year);
>                 $e_tstamp_at_7 = timelocal(0,0,7,$mday,$mon,$year);
>
>                 # start and stop hours and start and stop days
>                  
> ($sec,$min,$start_hour,$start_day,$mon,$year,$wday,$yday,$isdst) =  
> localtime($start_stamp);
>                  
> ($sec,$min,$end_hour,$end_day,$mon,$year,$wday,$yday,$isdst) =  
> localtime($time_stamp);
>
>
>                 $diff = int $duration/86400;    # how many day's  
> connection continued
>
>                 # day difference between start and stop
>
>                 # connecton continued until beginning of the next month
>                 if($end_day < $start_day){
>                         $day_diff = $numDays[$thismonth] - $start_day  
> + $end_day;
>                 }else{
>                         $day_diff = $end_day - $start_day;
>                 }
>                 if($start_hour >= 0 && $start_hour < 7){
>
>                         if($end_hour >= 0 && $end_hour < 7){
>
>                                 if($day_diff > 0){
>                 $rduration = $duration - ($s_tstamp_at_7 -  
> $start_stamp) - ($diff * 25200) - ($time_stamp - $e_tstamp_at_0);
>                                 }else{
>                                         $rduration = 0;
>                                 }
>                         }elsif($end_hour >= 7 && $end_hour <= 23){
>
>                                 $rduration = $duration -  
> ($s_tstamp_at_7 - $start_stamp) - ($diff * 25200);
>                         }
>                 }elsif($start_hour >= 7 && $start_hour <= 23){
>
>                         if($end_hour >= 7 && $end_hour <= 23){
>                                 $rduration = $duration - ($day_diff *  
> 25200);
>
>                         }elsif($end_hour >= 0 && $end_hour < 7){
>                                 $rduration = $duration - ($time_stamp  
> - $e_tstamp_at_0) - ($diff * 25200);
>                         }
>                 }
>
>
>             ########### Above lines added to calculate real duration  
> during day time ###########################
>
>             $q = "update SUBSCRIBERS set TIMELEFT = (TIMELEFT -  
> $rduration) where USERNAME = '$user_name'";
>             $self->log($main::LOG_DEBUG, "Query is: $q\n", $p);
>
>             $self->do($q);
>         }
> .
> .
> .
>
>
>
> ===
> Archive at http://www.open.com.au/archives/radiator/
> Announcements on radiator-announce at open.com.au
> To unsubscribe, email 'majordomo at open.com.au' with
> 'unsubscribe radiator' in the body of the message.
>
>

-- 
Radiator: the most portable, flexible and configurable RADIUS server
anywhere. Available on *NIX, *BSD, Windows 95/98/2000, NT, MacOS X.
-
Nets: internetwork inventory and management - graphical, extensible,
flexible with hardware, software, platform and database independence.

===
Archive at http://www.open.com.au/archives/radiator/
Announcements on radiator-announce at open.com.au
To unsubscribe, email 'majordomo at open.com.au' with
'unsubscribe radiator' in the body of the message.


More information about the radiator mailing list