isdn4linux-web/workshop/linklevel/isdn_budget.c.shtml

225 lines
12 KiB
Plaintext

<HTML>
<!-- Generated by c2html-1.0, Copyright 1998 by Dave Whittington -->
<HEAD>
<TITLE>isdn_budget.c</TITLE>
<!--#include virtual="/ssi/js.shtml" -->
<!--#include virtual="/ssi/buttondefs.shtml" -->
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<TABLE WIDTH="100%">
<TR>
<TD ALIGN=LEFT WIDTH="90"><!--#include virtual="/ssi/b_home.shtml" --></TD>
<TD ALIGN=RIGHT WIDTH="90"><!--#include virtual="/ssi/b_index.shtml" --></TD>
</TR>
</TABLE>
<CENTER><H1>isdn_budget.c</H1></CENTER>
<HR>
<PRE>
<FONT COLOR=#0000FF>/* $Id$
*
* Linux ISDN subsystem, budget-accounting for network interfaces.
*
* Copyright 1997 by Christian Lademann &lt;cal@zls.de&gt;
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Log$
* Revision 1.2 1998/03/07 23:17:30 fritz
* Added RCS keywords
* Bugfix: Did not compile without isdn_dumppkt beeing enabled.
*
*/</FONT>
<FONT COLOR=#0000FF>/*
30.06.97:cal:angelegt
04.11.97:cal:budget.period: int --&gt; time_t
*/</FONT>
<FONT COLOR=#A521F7>#include</FONT> &lt;linux/config.h&gt;
<FONT COLOR=#A521F7>#define</FONT> <A HREF="isdn_audio.c.shtml#__NO_VERSION__">__NO_VERSION__</A>
<FONT COLOR=#A521F7>#include</FONT> &lt;linux/module.h&gt;
<FONT COLOR=#A521F7>#include</FONT> &lt;linux/isdn.h&gt;
<FONT COLOR=#A521F7>#include</FONT> <FONT COLOR="#FF0000">"isdn_common.h"</FONT>
<FONT COLOR=#A521F7>#include</FONT> <FONT COLOR="#FF0000">"isdn_net.h"</FONT>
<FONT COLOR=#A521F7>#ifdef</FONT> CONFIG_ISDN_BUDGET
<FONT COLOR=#A521F7>#define</FONT> <A HREF="#VERBOSE_PRINTK">VERBOSE_PRINTK</A>(v, l, p...) { \
<FONT COLOR="#298C52">if</FONT>(dev-&gt;net_verbose &gt;= (v)) { \
printk(l ## p); \
} <FONT COLOR="#298C52">else</FONT> { ; } \
}
<FONT COLOR="#298C52">int</FONT>
<A NAME="isdn_net_budget">isdn_net_budget</A>(<FONT COLOR="#298C52">int</FONT> type, <FONT COLOR="#298C52">struct</FONT> device *ndev) {
isdn_net_local *lp = (isdn_net_local *)ndev-&gt;priv;
<FONT COLOR="#298C52">int</FONT> i, ret = 0;
<FONT COLOR="#298C52">switch</FONT>(type) {
<FONT COLOR="#298C52">case</FONT> <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_INIT">ISDN_BUDGET_INIT</A>:
<FONT COLOR="#298C52">for</FONT>(i = 0; i &lt; <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_NUM_BUDGET">ISDN_BUDGET_NUM_BUDGET</A>; i++) {
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .amount = -1;
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .used = 0;
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .period = (time_t)0;
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .period_started = (time_t)0;
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .last_check = CURRENT_TIME;
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .notified = 0;
}
<FONT COLOR="#298C52">return</FONT>(0);
<FONT COLOR="#298C52">break</FONT>;
<FONT COLOR="#298C52">case</FONT> <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_CHECK_DIAL">ISDN_BUDGET_CHECK_DIAL</A>:
<FONT COLOR="#298C52">case</FONT> <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_CHECK_CHARGE">ISDN_BUDGET_CHECK_CHARGE</A>:
<FONT COLOR="#298C52">case</FONT> <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_CHECK_ONLINE">ISDN_BUDGET_CHECK_ONLINE</A>:
ret = 0;
<FONT COLOR="#298C52">for</FONT>(i = 0; i &lt; <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_NUM_BUDGET">ISDN_BUDGET_NUM_BUDGET</A>; i++) {
<FONT COLOR="#298C52">if</FONT>(lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .amount &lt; 0)
<FONT COLOR="#298C52">continue</FONT>;
<FONT COLOR="#298C52">if</FONT>(lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .period_started + lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .period &lt; CURRENT_TIME) {
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .used = 0;
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .period_started = CURRENT_TIME;
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .notified = 0;
}
<FONT COLOR="#298C52">if</FONT>(lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .used &gt;= lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .amount)
ret |= (1 &lt;&lt; i);
}
<FONT COLOR="#298C52">switch</FONT>(type) {
<FONT COLOR="#298C52">case</FONT> <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_CHECK_DIAL">ISDN_BUDGET_CHECK_DIAL</A>:
<FONT COLOR="#298C52">if</FONT>(! ret) {
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [<A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_DIAL">ISDN_BUDGET_DIAL</A>] .used++;
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [<A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_DIAL">ISDN_BUDGET_DIAL</A>] .last_check = CURRENT_TIME;
}
<FONT COLOR="#298C52">break</FONT>;
<FONT COLOR="#298C52">case</FONT> <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_CHECK_CHARGE">ISDN_BUDGET_CHECK_CHARGE</A>:
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [<A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_CHARGE">ISDN_BUDGET_CHARGE</A>] .used++;
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [<A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_CHARGE">ISDN_BUDGET_CHARGE</A>] .last_check = CURRENT_TIME;
<FONT COLOR="#298C52">break</FONT>;
<FONT COLOR="#298C52">case</FONT> <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_CHECK_ONLINE">ISDN_BUDGET_CHECK_ONLINE</A>:
<FONT COLOR="#298C52">if</FONT>(lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [<A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_ONLINE">ISDN_BUDGET_ONLINE</A>] .last_check) {
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [<A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_ONLINE">ISDN_BUDGET_ONLINE</A>] .used += (CURRENT_TIME - lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [<A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_ONLINE">ISDN_BUDGET_ONLINE</A>] .last_check);
}
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [<A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_ONLINE">ISDN_BUDGET_ONLINE</A>] .last_check = CURRENT_TIME;
<FONT COLOR="#298C52">break</FONT>;
}
<FONT COLOR=#0000FF>/*
if(ret)
lp-&gt;flags |= ISDN_NET_STOPPED;
*/</FONT>
<FONT COLOR="#298C52">for</FONT>(i = 0; i &lt; <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_NUM_BUDGET">ISDN_BUDGET_NUM_BUDGET</A>; i++) {
<FONT COLOR="#298C52">if</FONT>(ret &amp; (1 &lt;&lt; i) &amp;&amp; ! lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .notified) {
<FONT COLOR="#298C52">switch</FONT>(i) {
<FONT COLOR="#298C52">case</FONT> <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_DIAL">ISDN_BUDGET_DIAL</A>:
printk(KERN_WARNING <FONT COLOR="#FF0000">"isdn_budget: dial budget used up.\n"</FONT>);
<FONT COLOR="#298C52">break</FONT>;
<FONT COLOR="#298C52">case</FONT> <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_CHARGE">ISDN_BUDGET_CHARGE</A>:
printk(KERN_WARNING <FONT COLOR="#FF0000">"isdn_budget: charge budget used up.\n"</FONT>);
<FONT COLOR="#298C52">break</FONT>;
<FONT COLOR="#298C52">case</FONT> <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_ONLINE">ISDN_BUDGET_ONLINE</A>:
printk(KERN_WARNING <FONT COLOR="#FF0000">"isdn_budget: online budget used up.\n"</FONT>);
<FONT COLOR="#298C52">break</FONT>;
<FONT COLOR="#298C52">default</FONT>:
printk(KERN_WARNING <FONT COLOR="#FF0000">"isdn_budget: budget #%d used up.\n"</FONT>, i);
<FONT COLOR="#298C52">break</FONT>;
}
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [i] .notified = 1;
}
}
<FONT COLOR="#298C52">return</FONT>(ret);
<FONT COLOR="#298C52">break</FONT>;
<FONT COLOR="#298C52">case</FONT> <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_START_ONLINE">ISDN_BUDGET_START_ONLINE</A>:
lp-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [<A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_ONLINE">ISDN_BUDGET_ONLINE</A>] .last_check = CURRENT_TIME;
<FONT COLOR="#298C52">return</FONT>(0);
<FONT COLOR="#298C52">break</FONT>;
}
<FONT COLOR="#298C52">return</FONT>(-1);
}
<FONT COLOR="#298C52">int</FONT>
<A NAME="isdn_budget_ioctl">isdn_budget_ioctl</A>(<A HREF="../includes/isdn_budget.h.shtml#isdn_ioctl_budget">isdn_ioctl_budget</A> *iocmd) {
<A HREF="../includes/isdn.h.shtml#isdn_net_dev">isdn_net_dev</A> *p = <A HREF="isdn_net.c.shtml#isdn_net_findif">isdn_net_findif</A>(iocmd-&gt;<A HREF="isdn_common.c.shtml#name">name</A>);
<FONT COLOR="#298C52">if</FONT>(p) {
<FONT COLOR="#298C52">switch</FONT>(iocmd-&gt;command) {
<FONT COLOR="#298C52">case</FONT> <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_SET_BUDGET">ISDN_BUDGET_SET_BUDGET</A>:
<FONT COLOR="#298C52">if</FONT>(! suser())
<FONT COLOR="#298C52">return</FONT>(-EPERM);
<FONT COLOR="#298C52">if</FONT>(iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> &lt; 0 || iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> &gt; <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_NUM_BUDGET">ISDN_BUDGET_NUM_BUDGET</A>)
<FONT COLOR="#298C52">return</FONT>(-EINVAL);
<FONT COLOR="#298C52">if</FONT>(iocmd-&gt;amount &lt; 0)
iocmd-&gt;amount = -1;
p-&gt;local-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A>] .amount = iocmd-&gt;amount;
p-&gt;local-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A>] .period = iocmd-&gt;period;
<FONT COLOR="#298C52">if</FONT>(iocmd-&gt;used &lt;= 0)
p-&gt;local-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A>] .used = 0;
<FONT COLOR="#298C52">else</FONT>
p-&gt;local-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A>] .used = iocmd-&gt;used;
<FONT COLOR="#298C52">if</FONT>(iocmd-&gt;period_started == (time_t)0)
p-&gt;local-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A>] .period_started = CURRENT_TIME;
<FONT COLOR="#298C52">else</FONT>
p-&gt;local-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A>] .period_started = iocmd-&gt;period_started;
<FONT COLOR="#298C52">return</FONT>(0);
<FONT COLOR="#298C52">break</FONT>;
<FONT COLOR="#298C52">case</FONT> <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_GET_BUDGET">ISDN_BUDGET_GET_BUDGET</A>:
<FONT COLOR="#298C52">if</FONT>(iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> &lt; 0 || iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> &gt; <A HREF="../includes/isdn_budget.h.shtml#ISDN_BUDGET_NUM_BUDGET">ISDN_BUDGET_NUM_BUDGET</A>)
<FONT COLOR="#298C52">return</FONT>(-EINVAL);
iocmd-&gt;amount = p-&gt;local-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A>] .amount;
iocmd-&gt;used = p-&gt;local-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A>] .used;
iocmd-&gt;period = p-&gt;local-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A>] .period;
iocmd-&gt;period_started = p-&gt;local-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A> [iocmd-&gt;<A HREF="isdn_common.c.shtml#budget">budget</A>] .period_started;
<FONT COLOR="#298C52">return</FONT>(0);
<FONT COLOR="#298C52">break</FONT>;
<FONT COLOR="#298C52">default</FONT>:
<FONT COLOR="#298C52">return</FONT>(-EINVAL);
<FONT COLOR="#298C52">break</FONT>;
}
}
<FONT COLOR="#298C52">return</FONT>(-ENODEV);
}
<FONT COLOR=#A521F7>#endif</FONT>
</BODY>
</HTML>