File: ioctl_stat.c

package info (click to toggle)
pload 0.9.5-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 208 kB
  • ctags: 96
  • sloc: ansic: 897; makefile: 88; sh: 63
file content (144 lines) | stat: -rw-r--r-- 3,810 bytes parent folder | download | duplicates (4)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
    This file is part of pload - a program to monitor ppp activity for X
    Copyright (C) 1999-2000  Matt Smith <mdsmith@engr.utk.edu>

    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., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/* Thanks to David Brownlee <abs@netbsd.org>for catching the bug concerning
 * non-reseting stats after a disconnect for the non-streams section */

/* ioctl_stat.c */

#include <string.h>		/* memset, strncpy */
#include <unistd.h>
#include <sys/types.h>		/* socket */
#include <sys/stat.h>		/* open */
#include <fcntl.h>		/* open */
#include <sys/ioctl.h>		/* ioctl */
#include <errno.h>

#ifndef STREAMS				/* Linux, FreeBSD, NetBSD, Ultrix */
#	include <sys/socket.h>		/* socket */
#	if ( defined linux ) && ( __GLIBC__ < 2 ) /* old libc5 linux */
#		include <linux/if.h>
#		include <linux/ppp_defs.h>
#		include <linux/if_ppp.h>
#	else				/* most everything else */
#		include <net/if.h>
#		include <net/ppp_defs.h>
#		include <net/if_ppp.h>
#	endif	/* linux && __GLIBC__ < 2 */
#else	/* STREAMS */			/* Solaris, SunOS, OSF/1, SVR4 */
#	include <net/ppp_defs.h>
#	include <net/pppio.h>
#endif /* STREAMS */

#include "pload.h"

#ifndef STREAMS /***************************************/

void getsocket(if_data *ifd)
{
	if ((ifd->s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
		die("socket");
	return;
}

void ioctl_stat(if_data *ifd)
{
	struct ifreq ifr;
	struct ifpppstatsreq req;
	
	if (!ifd->s) getsocket(ifd);
	
	memset(&ifr, 0, sizeof(ifr));
	strncpy(ifr.ifr_name, ifd->device, sizeof(ifr.ifr_name));
	if ( (ioctl(ifd->s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) ||
		!(ifr.ifr_flags&IFF_UP) )
	{
		/* invalid interface, or interface down */
		ifd->in_bytes = 0UL;
		ifd->out_bytes = 0UL;
		return;
	}
	
	memset(&req, 0, sizeof(req));

#ifdef linux
	req.stats_ptr = (caddr_t) &req.stats;
#undef ifr_name
#define ifr_name ifr__name
#endif	

	strncpy(req.ifr_name, ifd->device, sizeof(req.ifr_name));
	if (ioctl(ifd->s, SIOCGPPPSTATS, &req) != 0)
	{
		/* shouldn't fail if SIOCGIFFLAGS worked... */
		ifd->in_bytes = 0UL;
		ifd->out_bytes = 0UL;
		return;
	}
	
	ifd->in_bytes = (unsigned long)req.stats.p.ppp_ibytes;
	ifd->out_bytes = (unsigned long)req.stats.p.ppp_obytes;
	
	return;
}

#else /*STREAMS */ /******************************************/

void getsocket(if_data *ifd)	/* not really a socket... */
{
#ifdef osf
	if ((ifd->s = open("/dev/streams/ppp", O_RDONLY)) == -1)
		die("couldn't open /dev/streams/ppp");
#else
	if ((ifd->s = open("/dev/ppp", O_RDONLY)) == -1)
		die("couldn't open /dev/ppp");
#endif
	return;
}

void ioctl_stat(if_data *ifd)
{
	struct ppp_stats req;
	
	if (!ifd->s)
		getsocket(ifd);
	
	memset(&req, 0, sizeof(req));
	if (strioctl(ifd->s, PPPIO_GETSTAT, &req, 0, sizeof(req)) == -1)
	{
		if (errno == EINVAL) /* invalid interface, or interface down */
		{
			/* try to attach */
			(void)strioctl(ifd->s, PPPIO_ATTACH, 
			&ifd->dev_n, sizeof(ifd->dev_n), 0);
		}
		/* no connection */
		ifd->in_bytes = 0UL;
		ifd->out_bytes = 0UL;
		return;
	}
	
	ifd->in_bytes = (unsigned long)req.p.ppp_ibytes;
	ifd->out_bytes = (unsigned long)req.p.ppp_obytes;
	
	return;
}

#endif /*STREAMS */ /******************************************/