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