File: PPPStats.cpp

package info (click to toggle)
kpppload 1.01-5
  • links: PTS
  • area: contrib
  • in suites: hamm
  • size: 152 kB
  • ctags: 125
  • sloc: cpp: 950; makefile: 34
file content (105 lines) | stat: -rw-r--r-- 3,270 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// KPPPLoad - a PPP load monitor
// Copyright (C) 1998  Sean Vyain, svyain@mail.tds.net
//
// 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 of the License, 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <asm/types.h>
#include <linux/ppp_defs.h>

#include "PPPStats.h"
#include "Options.h"

typedef struct
{
    struct ppp_stats  stats;
} DclPPPInfo;

PPPStats::PPPStats()
        : _rxTotal( 0 ),
          _txTotal( 0 ),
          _isUp( FALSE ),
          _retryId( -1 ),
          _periodicId( -1 )
{
    if ((_s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("couldn't create IP socket");
        exit(1);
    }

    timerEvent( 0 );
    
    startTimer( options->getStatsInterval() * 1000 );
    if ( options->getPeriodicInterval() ) {
        _periodicId = startTimer( options->getPeriodicInterval() * 1000 );
    }

    if ( options->getPPPCommand() ) {
        _retryId = startTimer( options->getRetryInterval() * 1000 );
    }
}

void PPPStats::timerEvent( QTimerEvent* e )
{
    if ( ( !e ) || ( e->timerId() == _retryId ) )
    {
        system( options->getPPPCommand() );
    } else if ( ( e->timerId() == _periodicId ) && ( _isUp ) ) {
        system( options->getPeriodicCommand() );
    }
    
    DclPPPInfo        PPPInfo[1];
    struct ifreq      ifreq;
    struct ppp_stats* PPPStat;
    struct ppp_stats  LastPPPStat[1];
	
    memset (& ifreq, 0, sizeof (ifreq));
    sprintf ( ifreq.ifr_ifrn.ifrn_name, "ppp%d", options->getLinkNumber() );
	
    ifreq.ifr_ifru.ifru_data = (caddr_t) PPPInfo;
    PPPStat = & PPPInfo->stats;
    memset (LastPPPStat, 0, sizeof (LastPPPStat));
	
    if ( (ioctl (_s, SIOCDEVPRIVATE, (caddr_t) & ifreq) < 0) || ( !PPPStat->p.ppp_ibytes ) ) {
        if ( _isUp ) {
            _isUp = FALSE;
            emit linkDown();
            if ( options->getPPPCommand() ) {
                _retryId = startTimer( options->getRetryInterval() * 1000 );
                system( options->getPPPCommand() );
            }
        }
        PPPStat->p.ppp_ibytes = 0;
        PPPStat->p.ppp_obytes = 0;
    } else if ( !_isUp ) {
        _isUp = TRUE;
        emit linkUp();
        killTimer( _retryId );
        _retryId = -1;
    }

    unsigned int rxDelta = PPPStat->p.ppp_ibytes - _rxTotal;
    unsigned int txDelta = PPPStat->p.ppp_obytes - _txTotal;
    _rxTotal = PPPStat->p.ppp_ibytes;
    _txTotal = PPPStat->p.ppp_obytes;
    if ( rxDelta == _rxTotal ) rxDelta = 0;
    if ( txDelta == _txTotal ) txDelta = 0;
    emit changeStats( rxDelta, txDelta, _rxTotal, _txTotal );
}