1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * print PPP statistics:
3*7c478bd9Sstevel@tonic-gate  * 	pppstats [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  *   -a Show absolute values rather than deltas
6*7c478bd9Sstevel@tonic-gate  *   -d Show data rate (kB/s) rather than bytes
7*7c478bd9Sstevel@tonic-gate  *   -v Show more stats for VJ TCP header compression
8*7c478bd9Sstevel@tonic-gate  *   -r Show compression ratio
9*7c478bd9Sstevel@tonic-gate  *   -z Show compression statistics instead of default display
10*7c478bd9Sstevel@tonic-gate  *
11*7c478bd9Sstevel@tonic-gate  * History:
12*7c478bd9Sstevel@tonic-gate  *      perkins@cps.msu.edu: Added compression statistics and alternate
13*7c478bd9Sstevel@tonic-gate  *                display. 11/94
14*7c478bd9Sstevel@tonic-gate  *	Brad Parker (brad@cayman.com) 6/92
15*7c478bd9Sstevel@tonic-gate  *
16*7c478bd9Sstevel@tonic-gate  * from the original "slstats" by Van Jacobson
17*7c478bd9Sstevel@tonic-gate  *
18*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
19*7c478bd9Sstevel@tonic-gate  * All rights reserved.
20*7c478bd9Sstevel@tonic-gate  *
21*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1989 Regents of the University of California.
22*7c478bd9Sstevel@tonic-gate  * All rights reserved.
23*7c478bd9Sstevel@tonic-gate  *
24*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
25*7c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
26*7c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
27*7c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
28*7c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
29*7c478bd9Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
30*7c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
31*7c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
32*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
33*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
34*7c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #ifndef __STDC__
38*7c478bd9Sstevel@tonic-gate #define const
39*7c478bd9Sstevel@tonic-gate #endif
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #ifndef lint
44*7c478bd9Sstevel@tonic-gate static const char rcsid[] = "$Id: pppstats.c,v 1.27 1999/08/13 06:46:23 paulus Exp $";
45*7c478bd9Sstevel@tonic-gate #endif
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #include <stdio.h>
48*7c478bd9Sstevel@tonic-gate #include <stddef.h>
49*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
50*7c478bd9Sstevel@tonic-gate #include <string.h>
51*7c478bd9Sstevel@tonic-gate #include <ctype.h>
52*7c478bd9Sstevel@tonic-gate #include <errno.h>
53*7c478bd9Sstevel@tonic-gate #include <signal.h>
54*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
55*7c478bd9Sstevel@tonic-gate #include <unistd.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate #ifndef STREAMS
61*7c478bd9Sstevel@tonic-gate #if defined(_linux_) && defined(__powerpc__) \
62*7c478bd9Sstevel@tonic-gate     && (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)
63*7c478bd9Sstevel@tonic-gate /* kludge alert! */
64*7c478bd9Sstevel@tonic-gate #undef __GLIBC__
65*7c478bd9Sstevel@tonic-gate #endif
66*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>		/* *BSD, Linux, NeXT, Ultrix etc. */
67*7c478bd9Sstevel@tonic-gate #ifndef _linux_
68*7c478bd9Sstevel@tonic-gate #include <net/if.h>
69*7c478bd9Sstevel@tonic-gate #include <net/ppp_defs.h>
70*7c478bd9Sstevel@tonic-gate #include <net/if_ppp.h>
71*7c478bd9Sstevel@tonic-gate #else
72*7c478bd9Sstevel@tonic-gate /* Linux */
73*7c478bd9Sstevel@tonic-gate #if __GLIBC__ >= 2
74*7c478bd9Sstevel@tonic-gate #include <asm/types.h>		/* glibc 2 conflicts with linux/types.h */
75*7c478bd9Sstevel@tonic-gate #include <net/if.h>
76*7c478bd9Sstevel@tonic-gate #else
77*7c478bd9Sstevel@tonic-gate #include <linux/types.h>
78*7c478bd9Sstevel@tonic-gate #include <linux/if.h>
79*7c478bd9Sstevel@tonic-gate #endif
80*7c478bd9Sstevel@tonic-gate #include <linux/ppp_defs.h>
81*7c478bd9Sstevel@tonic-gate #include <linux/if_ppp.h>
82*7c478bd9Sstevel@tonic-gate #endif /* _linux_ */
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate #else	/* STREAMS */
85*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>	/* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */
86*7c478bd9Sstevel@tonic-gate #include <net/ppp_defs.h>
87*7c478bd9Sstevel@tonic-gate #include <net/pppio.h>
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate #ifdef PPPIO_GETSTAT64
90*7c478bd9Sstevel@tonic-gate #define	ppp_stats64	ppp_stats64
91*7c478bd9Sstevel@tonic-gate #endif
92*7c478bd9Sstevel@tonic-gate #endif	/* STREAMS */
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate #ifndef ppp_stats64
95*7c478bd9Sstevel@tonic-gate #define	ppp_stats64	ppp_stats
96*7c478bd9Sstevel@tonic-gate #endif
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate static int	vflag, rflag, zflag;	/* select type of display */
99*7c478bd9Sstevel@tonic-gate static int	aflag;			/* print absolute values, not deltas */
100*7c478bd9Sstevel@tonic-gate static int	dflag;			/* print data rates, not bytes */
101*7c478bd9Sstevel@tonic-gate static int	interval, count;
102*7c478bd9Sstevel@tonic-gate static int	infinite;
103*7c478bd9Sstevel@tonic-gate static int	unit;
104*7c478bd9Sstevel@tonic-gate static int	s;			/* socket or /dev/ppp file descriptor */
105*7c478bd9Sstevel@tonic-gate static int	signalled;		/* set if alarm goes off "early" */
106*7c478bd9Sstevel@tonic-gate static char	*progname;
107*7c478bd9Sstevel@tonic-gate static char	*interface;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate #if defined(SUNOS4) || defined(ULTRIX) || defined(NeXT)
110*7c478bd9Sstevel@tonic-gate extern int optind;
111*7c478bd9Sstevel@tonic-gate extern char *optarg;
112*7c478bd9Sstevel@tonic-gate #endif
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate /*
115*7c478bd9Sstevel@tonic-gate  * If PPP_DRV_NAME is not defined, use the legacy "ppp" as the
116*7c478bd9Sstevel@tonic-gate  * device name.
117*7c478bd9Sstevel@tonic-gate  */
118*7c478bd9Sstevel@tonic-gate #if !defined(PPP_DRV_NAME)
119*7c478bd9Sstevel@tonic-gate #define PPP_DRV_NAME    "ppp"
120*7c478bd9Sstevel@tonic-gate #endif /* !defined(PPP_DRV_NAME) */
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate static void usage __P((void));
123*7c478bd9Sstevel@tonic-gate static void catchalarm __P((int));
124*7c478bd9Sstevel@tonic-gate static void get_ppp_stats __P((struct ppp_stats64 *));
125*7c478bd9Sstevel@tonic-gate static void get_ppp_cstats __P((struct ppp_comp_stats *));
126*7c478bd9Sstevel@tonic-gate static void intpr __P((void));
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate int main __P((int, char *argv[]));
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate static void
131*7c478bd9Sstevel@tonic-gate usage()
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate     (void) fprintf(stderr,
134*7c478bd9Sstevel@tonic-gate 	"Usage: %s [-a|-d] [-v|-r|-z] [-c count] [-w wait] [interface]\n",
135*7c478bd9Sstevel@tonic-gate 	progname);
136*7c478bd9Sstevel@tonic-gate     exit(1);
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate /*
140*7c478bd9Sstevel@tonic-gate  * Called if an interval expires before intpr has completed a loop.
141*7c478bd9Sstevel@tonic-gate  * Sets a flag to not wait for the alarm.
142*7c478bd9Sstevel@tonic-gate  */
143*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
144*7c478bd9Sstevel@tonic-gate static void
145*7c478bd9Sstevel@tonic-gate catchalarm(arg)
146*7c478bd9Sstevel@tonic-gate     int arg;
147*7c478bd9Sstevel@tonic-gate {
148*7c478bd9Sstevel@tonic-gate     signalled = 1;
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate #ifndef STREAMS
153*7c478bd9Sstevel@tonic-gate static void
154*7c478bd9Sstevel@tonic-gate get_ppp_stats(curp)
155*7c478bd9Sstevel@tonic-gate     struct ppp_stats64 *curp;
156*7c478bd9Sstevel@tonic-gate {
157*7c478bd9Sstevel@tonic-gate     struct ifpppstatsreq req;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate     (void) memset (&req, 0, sizeof (req));
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate #ifdef _linux_
162*7c478bd9Sstevel@tonic-gate     req.stats_ptr = (caddr_t) &req.stats;
163*7c478bd9Sstevel@tonic-gate #undef ifr_name
164*7c478bd9Sstevel@tonic-gate #define ifr_name ifr__name
165*7c478bd9Sstevel@tonic-gate #endif
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate     strncpy(req.ifr_name, interface, sizeof(req.ifr_name));
168*7c478bd9Sstevel@tonic-gate     if (ioctl(s, SIOCGPPPSTATS, &req) < 0) {
169*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", progname);
170*7c478bd9Sstevel@tonic-gate 	if (errno == ENOTTY)
171*7c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr, "kernel support missing\n");
172*7c478bd9Sstevel@tonic-gate 	else
173*7c478bd9Sstevel@tonic-gate 	    perror("couldn't get PPP statistics");
174*7c478bd9Sstevel@tonic-gate 	exit(1);
175*7c478bd9Sstevel@tonic-gate     }
176*7c478bd9Sstevel@tonic-gate     *curp = req.stats;
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate static void
180*7c478bd9Sstevel@tonic-gate get_ppp_cstats(csp)
181*7c478bd9Sstevel@tonic-gate     struct ppp_comp_stats *csp;
182*7c478bd9Sstevel@tonic-gate {
183*7c478bd9Sstevel@tonic-gate     struct ifpppcstatsreq creq;
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate     (void) memset (&creq, 0, sizeof (creq));
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate #ifdef _linux_
188*7c478bd9Sstevel@tonic-gate     creq.stats_ptr = (caddr_t) &creq.stats;
189*7c478bd9Sstevel@tonic-gate #undef  ifr_name
190*7c478bd9Sstevel@tonic-gate #define ifr_name ifr__name
191*7c478bd9Sstevel@tonic-gate #endif
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate     strncpy(creq.ifr_name, interface, sizeof(creq.ifr_name));
194*7c478bd9Sstevel@tonic-gate     if (ioctl(s, SIOCGPPPCSTATS, &creq) < 0) {
195*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", progname);
196*7c478bd9Sstevel@tonic-gate 	if (errno == ENOTTY) {
197*7c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr, "no kernel compression support\n");
198*7c478bd9Sstevel@tonic-gate 	    if (zflag)
199*7c478bd9Sstevel@tonic-gate 		exit(1);
200*7c478bd9Sstevel@tonic-gate 	    rflag = 0;
201*7c478bd9Sstevel@tonic-gate 	} else {
202*7c478bd9Sstevel@tonic-gate 	    perror("couldn't get PPP compression stats");
203*7c478bd9Sstevel@tonic-gate 	    exit(1);
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate     }
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate #ifdef _linux_
208*7c478bd9Sstevel@tonic-gate     if (creq.stats.c.bytes_out == 0) {
209*7c478bd9Sstevel@tonic-gate 	creq.stats.c.bytes_out = creq.stats.c.comp_bytes + creq.stats.c.inc_bytes;
210*7c478bd9Sstevel@tonic-gate 	creq.stats.c.in_count = creq.stats.c.unc_bytes;
211*7c478bd9Sstevel@tonic-gate     }
212*7c478bd9Sstevel@tonic-gate     if (creq.stats.c.bytes_out == 0)
213*7c478bd9Sstevel@tonic-gate 	creq.stats.c.ratio = 0.0;
214*7c478bd9Sstevel@tonic-gate     else
215*7c478bd9Sstevel@tonic-gate 	creq.stats.c.ratio = 256.0 * creq.stats.c.in_count /
216*7c478bd9Sstevel@tonic-gate 			     creq.stats.c.bytes_out;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate     if (creq.stats.d.bytes_out == 0) {
219*7c478bd9Sstevel@tonic-gate 	creq.stats.d.bytes_out = creq.stats.d.comp_bytes + creq.stats.d.inc_bytes;
220*7c478bd9Sstevel@tonic-gate 	creq.stats.d.in_count = creq.stats.d.unc_bytes;
221*7c478bd9Sstevel@tonic-gate     }
222*7c478bd9Sstevel@tonic-gate     if (creq.stats.d.bytes_out == 0)
223*7c478bd9Sstevel@tonic-gate 	creq.stats.d.ratio = 0.0;
224*7c478bd9Sstevel@tonic-gate     else
225*7c478bd9Sstevel@tonic-gate 	creq.stats.d.ratio = 256.0 * creq.stats.d.in_count /
226*7c478bd9Sstevel@tonic-gate 			     creq.stats.d.bytes_out;
227*7c478bd9Sstevel@tonic-gate #endif
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate     *csp = creq.stats;
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate #else	/* STREAMS */
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate static int
235*7c478bd9Sstevel@tonic-gate strioctl(fd, cmd, ptr, ilen, olen)
236*7c478bd9Sstevel@tonic-gate     int fd, cmd, ilen, olen;
237*7c478bd9Sstevel@tonic-gate     char *ptr;
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate     struct strioctl str;
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate     str.ic_cmd = cmd;
242*7c478bd9Sstevel@tonic-gate     str.ic_timout = 0;
243*7c478bd9Sstevel@tonic-gate     str.ic_len = ilen;
244*7c478bd9Sstevel@tonic-gate     str.ic_dp = ptr;
245*7c478bd9Sstevel@tonic-gate     if (ioctl(fd, I_STR, &str) == -1)
246*7c478bd9Sstevel@tonic-gate 	return -1;
247*7c478bd9Sstevel@tonic-gate     if (str.ic_len != olen)
248*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
249*7c478bd9Sstevel@tonic-gate 	    "strioctl: expected %d bytes, got %d for cmd %x\n",
250*7c478bd9Sstevel@tonic-gate 	    olen, str.ic_len, cmd);
251*7c478bd9Sstevel@tonic-gate     return 0;
252*7c478bd9Sstevel@tonic-gate }
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate static void
255*7c478bd9Sstevel@tonic-gate get_ppp_stats(curp)
256*7c478bd9Sstevel@tonic-gate     struct ppp_stats64 *curp;
257*7c478bd9Sstevel@tonic-gate {
258*7c478bd9Sstevel@tonic-gate #ifdef PPPIO_GETSTAT64
259*7c478bd9Sstevel@tonic-gate     struct ppp_stats oldstat;
260*7c478bd9Sstevel@tonic-gate     if (strioctl(s, PPPIO_GETSTAT64, (char *)curp, 0, sizeof(*curp)) >= 0)
261*7c478bd9Sstevel@tonic-gate 	return;
262*7c478bd9Sstevel@tonic-gate     if (strioctl(s, PPPIO_GETSTAT, (char *)&oldstat, 0, sizeof(oldstat)) >= 0) {
263*7c478bd9Sstevel@tonic-gate 	curp->p.ppp_ibytes = oldstat.p.ppp_ibytes;
264*7c478bd9Sstevel@tonic-gate 	curp->p.ppp_ipackets = oldstat.p.ppp_ipackets;
265*7c478bd9Sstevel@tonic-gate 	curp->p.ppp_ierrors = oldstat.p.ppp_ierrors;
266*7c478bd9Sstevel@tonic-gate 	curp->p.ppp_obytes = oldstat.p.ppp_obytes;
267*7c478bd9Sstevel@tonic-gate 	curp->p.ppp_opackets = oldstat.p.ppp_opackets;
268*7c478bd9Sstevel@tonic-gate 	curp->p.ppp_oerrors = oldstat.p.ppp_oerrors;
269*7c478bd9Sstevel@tonic-gate 	curp->vj = oldstat.vj;
270*7c478bd9Sstevel@tonic-gate 	return;
271*7c478bd9Sstevel@tonic-gate     }
272*7c478bd9Sstevel@tonic-gate #else
273*7c478bd9Sstevel@tonic-gate     if (strioctl(s, PPPIO_GETSTAT, (char *)curp, 0, sizeof(*curp)) >= 0)
274*7c478bd9Sstevel@tonic-gate 	return;
275*7c478bd9Sstevel@tonic-gate #endif
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate     (void) fprintf(stderr, "%s: ", progname);
278*7c478bd9Sstevel@tonic-gate     if (errno == EINVAL)
279*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "kernel support missing\n");
280*7c478bd9Sstevel@tonic-gate     else
281*7c478bd9Sstevel@tonic-gate 	perror("couldn't get PPP statistics");
282*7c478bd9Sstevel@tonic-gate     exit(1);
283*7c478bd9Sstevel@tonic-gate }
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate static void
286*7c478bd9Sstevel@tonic-gate get_ppp_cstats(csp)
287*7c478bd9Sstevel@tonic-gate     struct ppp_comp_stats *csp;
288*7c478bd9Sstevel@tonic-gate {
289*7c478bd9Sstevel@tonic-gate     if (strioctl(s, PPPIO_GETCSTAT, (char *)csp, 0, sizeof(*csp)) < 0) {
290*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", progname);
291*7c478bd9Sstevel@tonic-gate 	if (errno == ENOTTY) {
292*7c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr, "no kernel compression support\n");
293*7c478bd9Sstevel@tonic-gate 	    if (zflag)
294*7c478bd9Sstevel@tonic-gate 		exit(1);
295*7c478bd9Sstevel@tonic-gate 	    rflag = 0;
296*7c478bd9Sstevel@tonic-gate 	} else {
297*7c478bd9Sstevel@tonic-gate 	    perror("couldn't get PPP compression statistics");
298*7c478bd9Sstevel@tonic-gate 	    exit(1);
299*7c478bd9Sstevel@tonic-gate 	}
300*7c478bd9Sstevel@tonic-gate     }
301*7c478bd9Sstevel@tonic-gate }
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate #endif /* STREAMS */
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate #define MAX0(a)		((int)(a) > 0? (a): 0)
306*7c478bd9Sstevel@tonic-gate #define V(offset)	MAX0(cur.offset - old.offset)
307*7c478bd9Sstevel@tonic-gate #define W(offset)	MAX0(ccs.offset - ocs.offset)
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate #define RATIO(c, i, u)	((c) == 0? 1.0: (u) / ((double)(c) + (i)))
310*7c478bd9Sstevel@tonic-gate #define CRATE(x)	RATIO(W(x.comp_bytes), W(x.inc_bytes), W(x.unc_bytes))
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate #define KBPS(n)		((n) / (interval * 1000.0))
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate /*
315*7c478bd9Sstevel@tonic-gate  * Print a running summary of interface statistics.
316*7c478bd9Sstevel@tonic-gate  * Repeat display every interval seconds, showing statistics
317*7c478bd9Sstevel@tonic-gate  * collected over that interval.  Assumes that interval is non-zero.
318*7c478bd9Sstevel@tonic-gate  * First line printed is cumulative.
319*7c478bd9Sstevel@tonic-gate  */
320*7c478bd9Sstevel@tonic-gate static void
321*7c478bd9Sstevel@tonic-gate intpr()
322*7c478bd9Sstevel@tonic-gate {
323*7c478bd9Sstevel@tonic-gate     register int line = 0;
324*7c478bd9Sstevel@tonic-gate     sigset_t oldmask, mask;
325*7c478bd9Sstevel@tonic-gate     char *bunit;
326*7c478bd9Sstevel@tonic-gate     int ratef = 0;
327*7c478bd9Sstevel@tonic-gate     struct ppp_stats64 cur, old;
328*7c478bd9Sstevel@tonic-gate     struct ppp_comp_stats ccs, ocs;
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate     (void) memset(&old, 0, sizeof(old));
331*7c478bd9Sstevel@tonic-gate     (void) memset(&ocs, 0, sizeof(ocs));
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate     for (;;) {
334*7c478bd9Sstevel@tonic-gate 	get_ppp_stats(&cur);
335*7c478bd9Sstevel@tonic-gate 	if (zflag || rflag)
336*7c478bd9Sstevel@tonic-gate 	    get_ppp_cstats(&ccs);
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	(void)signal(SIGALRM, catchalarm);
339*7c478bd9Sstevel@tonic-gate 	signalled = 0;
340*7c478bd9Sstevel@tonic-gate 	(void)alarm(interval);
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	if ((line % 20) == 0) {
343*7c478bd9Sstevel@tonic-gate 	    if (zflag) {
344*7c478bd9Sstevel@tonic-gate 		(void) printf("IN:  COMPRESSED  INCOMPRESSIBLE   COMP | ");
345*7c478bd9Sstevel@tonic-gate 		(void) printf("OUT: COMPRESSED  INCOMPRESSIBLE   COMP\n");
346*7c478bd9Sstevel@tonic-gate 		bunit = dflag? "KB/S": "BYTE";
347*7c478bd9Sstevel@tonic-gate 		(void) printf("    %s   PACK     %s   PACK  RATIO | ", bunit,
348*7c478bd9Sstevel@tonic-gate 		    bunit);
349*7c478bd9Sstevel@tonic-gate 		(void) printf("    %s   PACK     %s   PACK  RATIO", bunit,
350*7c478bd9Sstevel@tonic-gate 		    bunit);
351*7c478bd9Sstevel@tonic-gate 	    } else {
352*7c478bd9Sstevel@tonic-gate 		(void) printf("%8.8s %6.6s %6.6s",
353*7c478bd9Sstevel@tonic-gate 		       "IN", "PACK", "VJCOMP");
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 		if (!rflag)
356*7c478bd9Sstevel@tonic-gate 		    (void) printf(" %6.6s %6.6s", "VJUNC", "VJERR");
357*7c478bd9Sstevel@tonic-gate 		if (vflag)
358*7c478bd9Sstevel@tonic-gate 		    (void) printf(" %6.6s %6.6s", "VJTOSS", "NON-VJ");
359*7c478bd9Sstevel@tonic-gate 		if (rflag)
360*7c478bd9Sstevel@tonic-gate 		    (void) printf(" %6.6s %6.6s", "RATIO", "UBYTE");
361*7c478bd9Sstevel@tonic-gate 		(void) printf("  | %8.8s %6.6s %6.6s",
362*7c478bd9Sstevel@tonic-gate 		       "OUT", "PACK", "VJCOMP");
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 		if (!rflag)
365*7c478bd9Sstevel@tonic-gate 		    (void) printf(" %6.6s %6.6s", "VJUNC", "NON-VJ");
366*7c478bd9Sstevel@tonic-gate 		if (vflag)
367*7c478bd9Sstevel@tonic-gate 		    (void) printf(" %6.6s %6.6s", "VJSRCH", "VJMISS");
368*7c478bd9Sstevel@tonic-gate 		if (rflag)
369*7c478bd9Sstevel@tonic-gate 		    (void) printf(" %6.6s %6.6s", "RATIO", "UBYTE");
370*7c478bd9Sstevel@tonic-gate 	    }
371*7c478bd9Sstevel@tonic-gate 	    (void) putchar('\n');
372*7c478bd9Sstevel@tonic-gate 	}
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	if (zflag) {
375*7c478bd9Sstevel@tonic-gate 	    if (ratef) {
376*7c478bd9Sstevel@tonic-gate 		(void) printf("%8.3f %6u %8.3f %6u %6.2f",
377*7c478bd9Sstevel@tonic-gate 		       KBPS(W(d.comp_bytes)),
378*7c478bd9Sstevel@tonic-gate 		       W(d.comp_packets),
379*7c478bd9Sstevel@tonic-gate 		       KBPS(W(d.inc_bytes)),
380*7c478bd9Sstevel@tonic-gate 		       W(d.inc_packets),
381*7c478bd9Sstevel@tonic-gate 		       ccs.d.ratio / 256.0);
382*7c478bd9Sstevel@tonic-gate 		(void) printf(" | %8.3f %6u %8.3f %6u %6.2f",
383*7c478bd9Sstevel@tonic-gate 		       KBPS(W(c.comp_bytes)),
384*7c478bd9Sstevel@tonic-gate 		       W(c.comp_packets),
385*7c478bd9Sstevel@tonic-gate 		       KBPS(W(c.inc_bytes)),
386*7c478bd9Sstevel@tonic-gate 		       W(c.inc_packets),
387*7c478bd9Sstevel@tonic-gate 		       ccs.c.ratio / 256.0);
388*7c478bd9Sstevel@tonic-gate 	    } else {
389*7c478bd9Sstevel@tonic-gate 		(void) printf("%8u %6u %8u %6u %6.2f",
390*7c478bd9Sstevel@tonic-gate 		       W(d.comp_bytes),
391*7c478bd9Sstevel@tonic-gate 		       W(d.comp_packets),
392*7c478bd9Sstevel@tonic-gate 		       W(d.inc_bytes),
393*7c478bd9Sstevel@tonic-gate 		       W(d.inc_packets),
394*7c478bd9Sstevel@tonic-gate 		       ccs.d.ratio / 256.0);
395*7c478bd9Sstevel@tonic-gate 		(void) printf(" | %8u %6u %8u %6u %6.2f",
396*7c478bd9Sstevel@tonic-gate 		       W(c.comp_bytes),
397*7c478bd9Sstevel@tonic-gate 		       W(c.comp_packets),
398*7c478bd9Sstevel@tonic-gate 		       W(c.inc_bytes),
399*7c478bd9Sstevel@tonic-gate 		       W(c.inc_packets),
400*7c478bd9Sstevel@tonic-gate 		       ccs.c.ratio / 256.0);
401*7c478bd9Sstevel@tonic-gate 	    }
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 	} else {
404*7c478bd9Sstevel@tonic-gate 	    if (ratef)
405*7c478bd9Sstevel@tonic-gate 		(void) printf("%8.3f", KBPS(V(p.ppp_ibytes)));
406*7c478bd9Sstevel@tonic-gate 	    else
407*7c478bd9Sstevel@tonic-gate 		(void) printf("%8" PPP_COUNTER_F, V(p.ppp_ibytes));
408*7c478bd9Sstevel@tonic-gate 	    (void) printf(" %6" PPP_COUNTER_F " %6u",
409*7c478bd9Sstevel@tonic-gate 		   V(p.ppp_ipackets),
410*7c478bd9Sstevel@tonic-gate 		   V(vj.vjs_compressedin));
411*7c478bd9Sstevel@tonic-gate 	    if (!rflag)
412*7c478bd9Sstevel@tonic-gate 		(void) printf(" %6u %6u",
413*7c478bd9Sstevel@tonic-gate 		       V(vj.vjs_uncompressedin),
414*7c478bd9Sstevel@tonic-gate 		       V(vj.vjs_errorin));
415*7c478bd9Sstevel@tonic-gate 	    if (vflag)
416*7c478bd9Sstevel@tonic-gate 		(void) printf(" %6u %6" PPP_COUNTER_F,
417*7c478bd9Sstevel@tonic-gate 		       V(vj.vjs_tossed),
418*7c478bd9Sstevel@tonic-gate 		       V(p.ppp_ipackets) - V(vj.vjs_compressedin)
419*7c478bd9Sstevel@tonic-gate 		       - V(vj.vjs_uncompressedin) - V(vj.vjs_errorin));
420*7c478bd9Sstevel@tonic-gate 	    if (rflag) {
421*7c478bd9Sstevel@tonic-gate 		(void) printf(" %6.2f ", CRATE(d));
422*7c478bd9Sstevel@tonic-gate 		if (ratef)
423*7c478bd9Sstevel@tonic-gate 		    (void) printf("%6.2f", KBPS(W(d.unc_bytes)));
424*7c478bd9Sstevel@tonic-gate 		else
425*7c478bd9Sstevel@tonic-gate 		    (void) printf("%6u", W(d.unc_bytes));
426*7c478bd9Sstevel@tonic-gate 	    }
427*7c478bd9Sstevel@tonic-gate 	    if (ratef)
428*7c478bd9Sstevel@tonic-gate 		(void) printf("  | %8.3f", KBPS(V(p.ppp_obytes)));
429*7c478bd9Sstevel@tonic-gate 	    else
430*7c478bd9Sstevel@tonic-gate 		(void) printf("  | %8" PPP_COUNTER_F, V(p.ppp_obytes));
431*7c478bd9Sstevel@tonic-gate 	    (void) printf(" %6" PPP_COUNTER_F " %6u",
432*7c478bd9Sstevel@tonic-gate 		   V(p.ppp_opackets),
433*7c478bd9Sstevel@tonic-gate 		   V(vj.vjs_compressed));
434*7c478bd9Sstevel@tonic-gate 	    if (!rflag)
435*7c478bd9Sstevel@tonic-gate 		(void) printf(" %6u %6" PPP_COUNTER_F,
436*7c478bd9Sstevel@tonic-gate 		       V(vj.vjs_packets) - V(vj.vjs_compressed),
437*7c478bd9Sstevel@tonic-gate 		       V(p.ppp_opackets) - V(vj.vjs_packets));
438*7c478bd9Sstevel@tonic-gate 	    if (vflag)
439*7c478bd9Sstevel@tonic-gate 		(void) printf(" %6u %6u",
440*7c478bd9Sstevel@tonic-gate 		       V(vj.vjs_searches),
441*7c478bd9Sstevel@tonic-gate 		       V(vj.vjs_misses));
442*7c478bd9Sstevel@tonic-gate 	    if (rflag) {
443*7c478bd9Sstevel@tonic-gate 		(void) printf(" %6.2f ", CRATE(c));
444*7c478bd9Sstevel@tonic-gate 		if (ratef)
445*7c478bd9Sstevel@tonic-gate 		    (void) printf("%6.2f", KBPS(W(c.unc_bytes)));
446*7c478bd9Sstevel@tonic-gate 		else
447*7c478bd9Sstevel@tonic-gate 		    (void) printf("%6u", W(c.unc_bytes));
448*7c478bd9Sstevel@tonic-gate 	    }
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 	}
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
453*7c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
454*7c478bd9Sstevel@tonic-gate 	line++;
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 	count--;
457*7c478bd9Sstevel@tonic-gate 	if (!infinite && !count)
458*7c478bd9Sstevel@tonic-gate 	    break;
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&mask);
461*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&mask, SIGALRM);
462*7c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &mask, &oldmask);
463*7c478bd9Sstevel@tonic-gate 	if (!signalled) {
464*7c478bd9Sstevel@tonic-gate 	    (void) sigemptyset(&mask);
465*7c478bd9Sstevel@tonic-gate 	    (void) sigsuspend(&mask);
466*7c478bd9Sstevel@tonic-gate 	}
467*7c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oldmask, NULL);
468*7c478bd9Sstevel@tonic-gate 	signalled = 0;
469*7c478bd9Sstevel@tonic-gate 	(void)alarm(interval);
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 	if (!aflag) {
472*7c478bd9Sstevel@tonic-gate 	    old = cur;
473*7c478bd9Sstevel@tonic-gate 	    ocs = ccs;
474*7c478bd9Sstevel@tonic-gate 	    ratef = dflag;
475*7c478bd9Sstevel@tonic-gate 	}
476*7c478bd9Sstevel@tonic-gate     }
477*7c478bd9Sstevel@tonic-gate }
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate int
480*7c478bd9Sstevel@tonic-gate main(argc, argv)
481*7c478bd9Sstevel@tonic-gate     int argc;
482*7c478bd9Sstevel@tonic-gate     char *argv[];
483*7c478bd9Sstevel@tonic-gate {
484*7c478bd9Sstevel@tonic-gate     int c;
485*7c478bd9Sstevel@tonic-gate #ifdef STREAMS
486*7c478bd9Sstevel@tonic-gate     char *dev;
487*7c478bd9Sstevel@tonic-gate #endif
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate     interface = PPP_DRV_NAME "0";
490*7c478bd9Sstevel@tonic-gate     if ((progname = strrchr(argv[0], '/')) == NULL)
491*7c478bd9Sstevel@tonic-gate 	progname = argv[0];
492*7c478bd9Sstevel@tonic-gate     else
493*7c478bd9Sstevel@tonic-gate 	++progname;
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate     while ((c = getopt(argc, argv, "advrzc:w:")) != -1) {
496*7c478bd9Sstevel@tonic-gate 	switch (c) {
497*7c478bd9Sstevel@tonic-gate 	case 'a':
498*7c478bd9Sstevel@tonic-gate 	    ++aflag;
499*7c478bd9Sstevel@tonic-gate 	    break;
500*7c478bd9Sstevel@tonic-gate 	case 'd':
501*7c478bd9Sstevel@tonic-gate 	    ++dflag;
502*7c478bd9Sstevel@tonic-gate 	    break;
503*7c478bd9Sstevel@tonic-gate 	case 'v':
504*7c478bd9Sstevel@tonic-gate 	    ++vflag;
505*7c478bd9Sstevel@tonic-gate 	    break;
506*7c478bd9Sstevel@tonic-gate 	case 'r':
507*7c478bd9Sstevel@tonic-gate 	    ++rflag;
508*7c478bd9Sstevel@tonic-gate 	    break;
509*7c478bd9Sstevel@tonic-gate 	case 'z':
510*7c478bd9Sstevel@tonic-gate 	    ++zflag;
511*7c478bd9Sstevel@tonic-gate 	    break;
512*7c478bd9Sstevel@tonic-gate 	case 'c':
513*7c478bd9Sstevel@tonic-gate 	    count = atoi(optarg);
514*7c478bd9Sstevel@tonic-gate 	    if (count <= 0)
515*7c478bd9Sstevel@tonic-gate 		usage();
516*7c478bd9Sstevel@tonic-gate 	    break;
517*7c478bd9Sstevel@tonic-gate 	case 'w':
518*7c478bd9Sstevel@tonic-gate 	    interval = atoi(optarg);
519*7c478bd9Sstevel@tonic-gate 	    if (interval <= 0)
520*7c478bd9Sstevel@tonic-gate 		usage();
521*7c478bd9Sstevel@tonic-gate 	    break;
522*7c478bd9Sstevel@tonic-gate 	default:
523*7c478bd9Sstevel@tonic-gate 	    usage();
524*7c478bd9Sstevel@tonic-gate 	}
525*7c478bd9Sstevel@tonic-gate     }
526*7c478bd9Sstevel@tonic-gate     argc -= optind;
527*7c478bd9Sstevel@tonic-gate     argv += optind;
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate     if (!interval && count)
530*7c478bd9Sstevel@tonic-gate 	interval = 5;
531*7c478bd9Sstevel@tonic-gate     if (interval && !count)
532*7c478bd9Sstevel@tonic-gate 	infinite = 1;
533*7c478bd9Sstevel@tonic-gate     if (!interval && !count)
534*7c478bd9Sstevel@tonic-gate 	count = 1;
535*7c478bd9Sstevel@tonic-gate     if (aflag)
536*7c478bd9Sstevel@tonic-gate 	dflag = 0;
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate     if (argc > 1)
539*7c478bd9Sstevel@tonic-gate 	usage();
540*7c478bd9Sstevel@tonic-gate     if (argc > 0)
541*7c478bd9Sstevel@tonic-gate 	interface = argv[0];
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate     if (sscanf(interface, PPP_DRV_NAME "%d", &unit) != 1) {
544*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: invalid interface '%s' specified\n",
545*7c478bd9Sstevel@tonic-gate 		progname, interface);
546*7c478bd9Sstevel@tonic-gate     }
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate #ifndef STREAMS
549*7c478bd9Sstevel@tonic-gate     {
550*7c478bd9Sstevel@tonic-gate 	struct ifreq ifr;
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate 	s = socket(AF_INET, SOCK_DGRAM, 0);
553*7c478bd9Sstevel@tonic-gate 	if (s < 0) {
554*7c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr, "%s: ", progname);
555*7c478bd9Sstevel@tonic-gate 	    perror("couldn't create IP socket");
556*7c478bd9Sstevel@tonic-gate 	    exit(1);
557*7c478bd9Sstevel@tonic-gate 	}
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate #ifdef _linux_
560*7c478bd9Sstevel@tonic-gate #undef  ifr_name
561*7c478bd9Sstevel@tonic-gate #define ifr_name ifr_ifrn.ifrn_name
562*7c478bd9Sstevel@tonic-gate #endif
563*7c478bd9Sstevel@tonic-gate 	strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
564*7c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
565*7c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr, "%s: nonexistent interface '%s' specified\n",
566*7c478bd9Sstevel@tonic-gate 		    progname, interface);
567*7c478bd9Sstevel@tonic-gate 	    exit(1);
568*7c478bd9Sstevel@tonic-gate 	}
569*7c478bd9Sstevel@tonic-gate     }
570*7c478bd9Sstevel@tonic-gate 
571*7c478bd9Sstevel@tonic-gate #else	/* STREAMS */
572*7c478bd9Sstevel@tonic-gate #ifdef __osf__
573*7c478bd9Sstevel@tonic-gate     dev = "/dev/streams/ppp";
574*7c478bd9Sstevel@tonic-gate #else
575*7c478bd9Sstevel@tonic-gate     dev = "/dev/" PPP_DRV_NAME;
576*7c478bd9Sstevel@tonic-gate #endif
577*7c478bd9Sstevel@tonic-gate     if ((s = open(dev, O_RDONLY)) < 0) {
578*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: couldn't open ", progname);
579*7c478bd9Sstevel@tonic-gate 	perror(dev);
580*7c478bd9Sstevel@tonic-gate 	exit(1);
581*7c478bd9Sstevel@tonic-gate     }
582*7c478bd9Sstevel@tonic-gate     if (strioctl(s, PPPIO_ATTACH, (char *)&unit, sizeof(int), 0) < 0) {
583*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: " PPP_DRV_NAME "%d is not available\n",
584*7c478bd9Sstevel@tonic-gate 	    progname, unit);
585*7c478bd9Sstevel@tonic-gate 	exit(1);
586*7c478bd9Sstevel@tonic-gate     }
587*7c478bd9Sstevel@tonic-gate 
588*7c478bd9Sstevel@tonic-gate #endif	/* STREAMS */
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate     intpr();
591*7c478bd9Sstevel@tonic-gate     return (0);
592*7c478bd9Sstevel@tonic-gate }
593