17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * pppdump - print out the contents of a record file generated by
37c478bd9Sstevel@tonic-gate  * pppd in readable form.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Copyright (C) 1999  Paul Mackerras.  All rights reserved.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  *  This program is free software; you can redistribute it and/or
87c478bd9Sstevel@tonic-gate  *  modify it under the terms of the GNU General Public License
97c478bd9Sstevel@tonic-gate  *  as published by the Free Software Foundation; either version
107c478bd9Sstevel@tonic-gate  *  2 of the License, or (at your option) any later version.
117c478bd9Sstevel@tonic-gate  */
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate #include <stdio.h>
147c478bd9Sstevel@tonic-gate #include <unistd.h>
15*aee32e3dScarlsonj #include <stdlib.h>
167c478bd9Sstevel@tonic-gate #include <time.h>
177c478bd9Sstevel@tonic-gate #include <sys/types.h>
187c478bd9Sstevel@tonic-gate #ifdef PPP_DEFS_IN_NET
197c478bd9Sstevel@tonic-gate #include <net/ppp_defs.h>
207c478bd9Sstevel@tonic-gate #else
217c478bd9Sstevel@tonic-gate #include "ppp_defs.h"
227c478bd9Sstevel@tonic-gate #endif
237c478bd9Sstevel@tonic-gate #include "ppp-comp.h"
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate int hexmode;
267c478bd9Sstevel@tonic-gate int pppmode;
277c478bd9Sstevel@tonic-gate int reverse;
287c478bd9Sstevel@tonic-gate int decompress;
297c478bd9Sstevel@tonic-gate int mru = 1500;
307c478bd9Sstevel@tonic-gate int abs_times;
317c478bd9Sstevel@tonic-gate time_t start_time;
327c478bd9Sstevel@tonic-gate int start_time_tenths;
337c478bd9Sstevel@tonic-gate int tot_sent, tot_rcvd;
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate extern int optind;
367c478bd9Sstevel@tonic-gate extern char *optarg;
377c478bd9Sstevel@tonic-gate 
38*aee32e3dScarlsonj void dumplog();
39*aee32e3dScarlsonj void dumpppp();
40*aee32e3dScarlsonj void show_time();
41*aee32e3dScarlsonj void handle_ccp();
42*aee32e3dScarlsonj 
43*aee32e3dScarlsonj int
main(ac,av)447c478bd9Sstevel@tonic-gate main(ac, av)
457c478bd9Sstevel@tonic-gate     int ac;
467c478bd9Sstevel@tonic-gate     char **av;
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate     int i;
497c478bd9Sstevel@tonic-gate     char *p;
507c478bd9Sstevel@tonic-gate     FILE *f;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate     while ((i = getopt(ac, av, "hprdm:a")) != -1) {
537c478bd9Sstevel@tonic-gate 	switch (i) {
547c478bd9Sstevel@tonic-gate 	case 'h':
557c478bd9Sstevel@tonic-gate 	    hexmode = 1;
567c478bd9Sstevel@tonic-gate 	    break;
577c478bd9Sstevel@tonic-gate 	case 'p':
587c478bd9Sstevel@tonic-gate 	    pppmode = 1;
597c478bd9Sstevel@tonic-gate 	    break;
607c478bd9Sstevel@tonic-gate 	case 'r':
617c478bd9Sstevel@tonic-gate 	    reverse = 1;
627c478bd9Sstevel@tonic-gate 	    break;
637c478bd9Sstevel@tonic-gate 	case 'd':
647c478bd9Sstevel@tonic-gate 	    decompress = 1;
657c478bd9Sstevel@tonic-gate 	    break;
667c478bd9Sstevel@tonic-gate 	case 'm':
677c478bd9Sstevel@tonic-gate 	    mru = atoi(optarg);
687c478bd9Sstevel@tonic-gate 	    break;
697c478bd9Sstevel@tonic-gate 	case 'a':
707c478bd9Sstevel@tonic-gate 	    abs_times = 1;
717c478bd9Sstevel@tonic-gate 	    break;
727c478bd9Sstevel@tonic-gate 	default:
737c478bd9Sstevel@tonic-gate 	    fprintf(stderr, "Usage: %s [-h | -p[d]] [-r] [-m mru] [-a] [file ...]\n", av[0]);
747c478bd9Sstevel@tonic-gate 	    exit(1);
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate     }
777c478bd9Sstevel@tonic-gate     if (optind >= ac)
787c478bd9Sstevel@tonic-gate 	dumplog(stdin);
797c478bd9Sstevel@tonic-gate     else {
807c478bd9Sstevel@tonic-gate 	for (i = optind; i < ac; ++i) {
817c478bd9Sstevel@tonic-gate 	    p = av[i];
827c478bd9Sstevel@tonic-gate 	    if ((f = fopen(p, "r")) == NULL) {
837c478bd9Sstevel@tonic-gate 		perror(p);
847c478bd9Sstevel@tonic-gate 		exit(1);
857c478bd9Sstevel@tonic-gate 	    }
867c478bd9Sstevel@tonic-gate 	    if (pppmode)
877c478bd9Sstevel@tonic-gate 		dumpppp(f);
887c478bd9Sstevel@tonic-gate 	    else
897c478bd9Sstevel@tonic-gate 		dumplog(f);
907c478bd9Sstevel@tonic-gate 	    fclose(f);
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate     }
937c478bd9Sstevel@tonic-gate     exit(0);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
96*aee32e3dScarlsonj void
dumplog(f)977c478bd9Sstevel@tonic-gate dumplog(f)
987c478bd9Sstevel@tonic-gate     FILE *f;
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate     int c, n, k, col;
1017c478bd9Sstevel@tonic-gate     int nb, c2;
1027c478bd9Sstevel@tonic-gate     unsigned char buf[16];
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate     while ((c = getc(f)) != EOF) {
1057c478bd9Sstevel@tonic-gate 	switch (c) {
1067c478bd9Sstevel@tonic-gate 	case RECMARK_STARTSEND:
1077c478bd9Sstevel@tonic-gate 	case RECMARK_STARTRECV:
1087c478bd9Sstevel@tonic-gate 	    if (reverse)
1097c478bd9Sstevel@tonic-gate 		c = c==RECMARK_STARTSEND ? RECMARK_STARTRECV :
1107c478bd9Sstevel@tonic-gate 		    RECMARK_STARTSEND;
1117c478bd9Sstevel@tonic-gate 	    printf("%s %c", c==RECMARK_STARTSEND? "sent": "rcvd",
1127c478bd9Sstevel@tonic-gate 		hexmode? ' ': '"');
1137c478bd9Sstevel@tonic-gate 	    col = 6;
1147c478bd9Sstevel@tonic-gate 	    n = getc(f);
1157c478bd9Sstevel@tonic-gate 	    n = (n << 8) + getc(f);
1167c478bd9Sstevel@tonic-gate 	    *(c==1? &tot_sent: &tot_rcvd) += n;
1177c478bd9Sstevel@tonic-gate 	    nb = 0;
1187c478bd9Sstevel@tonic-gate 	    for (; n > 0; --n) {
1197c478bd9Sstevel@tonic-gate 		c = getc(f);
1207c478bd9Sstevel@tonic-gate 		if (c == EOF) {
1217c478bd9Sstevel@tonic-gate 		    printf("\nEOF\n");
1227c478bd9Sstevel@tonic-gate 		    exit(0);
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 		if (hexmode) {
1257c478bd9Sstevel@tonic-gate 		    if (nb >= 16) {
1267c478bd9Sstevel@tonic-gate 			printf("  ");
1277c478bd9Sstevel@tonic-gate 			for (k = 0; k < nb; ++k) {
1287c478bd9Sstevel@tonic-gate 			    c2 = buf[k];
1297c478bd9Sstevel@tonic-gate 			    putchar((' ' <= c2 && c2 <= '~')? c2: '.');
1307c478bd9Sstevel@tonic-gate 			}
1317c478bd9Sstevel@tonic-gate 			printf("\n      ");
1327c478bd9Sstevel@tonic-gate 			nb = 0;
1337c478bd9Sstevel@tonic-gate 		    }
1347c478bd9Sstevel@tonic-gate 		    buf[nb++] = c;
1357c478bd9Sstevel@tonic-gate 		    printf(" %.2x", c);
1367c478bd9Sstevel@tonic-gate 		} else {
1377c478bd9Sstevel@tonic-gate 		    k = (' ' <= c && c <= '~')? (c != '\\' && c != '"')? 1: 2: 3;
1387c478bd9Sstevel@tonic-gate 		    if ((col += k) >= 78) {
1397c478bd9Sstevel@tonic-gate 			printf("\n      ");
1407c478bd9Sstevel@tonic-gate 			col = 6 + k;
1417c478bd9Sstevel@tonic-gate 		    }
1427c478bd9Sstevel@tonic-gate 		    switch (k) {
1437c478bd9Sstevel@tonic-gate 		    case 1:
1447c478bd9Sstevel@tonic-gate 			putchar(c);
1457c478bd9Sstevel@tonic-gate 			break;
1467c478bd9Sstevel@tonic-gate 		    case 2:
1477c478bd9Sstevel@tonic-gate 			printf("\\%c", c);
1487c478bd9Sstevel@tonic-gate 			break;
1497c478bd9Sstevel@tonic-gate 		    case 3:
1507c478bd9Sstevel@tonic-gate 			printf("\\%.2x", c);
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 		    }
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 	    }
1557c478bd9Sstevel@tonic-gate 	    if (hexmode) {
1567c478bd9Sstevel@tonic-gate 		for (k = nb; k < 16; ++k)
1577c478bd9Sstevel@tonic-gate 		    printf("   ");
1587c478bd9Sstevel@tonic-gate 		printf("  ");
1597c478bd9Sstevel@tonic-gate 		for (k = 0; k < nb; ++k) {
1607c478bd9Sstevel@tonic-gate 		    c2 = buf[k];
1617c478bd9Sstevel@tonic-gate 		    putchar((' ' <= c2 && c2 <= '~')? c2: '.');
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 	    } else
1647c478bd9Sstevel@tonic-gate 		putchar('"');
1657c478bd9Sstevel@tonic-gate 	    printf("\n");
1667c478bd9Sstevel@tonic-gate 	    break;
1677c478bd9Sstevel@tonic-gate 	case RECMARK_ENDSEND:
1687c478bd9Sstevel@tonic-gate 	case RECMARK_ENDRECV:
1697c478bd9Sstevel@tonic-gate 	    if (reverse)
1707c478bd9Sstevel@tonic-gate 		c = c==RECMARK_ENDSEND ? RECMARK_ENDRECV : RECMARK_ENDSEND;
1717c478bd9Sstevel@tonic-gate 	    printf("end %s\n", c==RECMARK_ENDSEND? "send": "recv");
1727c478bd9Sstevel@tonic-gate 	    break;
1737c478bd9Sstevel@tonic-gate 	case RECMARK_TIMEDELTA32:
1747c478bd9Sstevel@tonic-gate 	case RECMARK_TIMEDELTA8:
1757c478bd9Sstevel@tonic-gate 	case RECMARK_TIMESTART:
1767c478bd9Sstevel@tonic-gate 	    show_time(f, c);
1777c478bd9Sstevel@tonic-gate 	    break;
1787c478bd9Sstevel@tonic-gate 	default:
1797c478bd9Sstevel@tonic-gate 	    printf("?%.2x\n");
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate     }
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * FCS lookup table as calculated by genfcstab.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate static u_short fcstab[256] = {
1887c478bd9Sstevel@tonic-gate 	0x0000,	0x1189,	0x2312,	0x329b,	0x4624,	0x57ad,	0x6536,	0x74bf,
1897c478bd9Sstevel@tonic-gate 	0x8c48,	0x9dc1,	0xaf5a,	0xbed3,	0xca6c,	0xdbe5,	0xe97e,	0xf8f7,
1907c478bd9Sstevel@tonic-gate 	0x1081,	0x0108,	0x3393,	0x221a,	0x56a5,	0x472c,	0x75b7,	0x643e,
1917c478bd9Sstevel@tonic-gate 	0x9cc9,	0x8d40,	0xbfdb,	0xae52,	0xdaed,	0xcb64,	0xf9ff,	0xe876,
1927c478bd9Sstevel@tonic-gate 	0x2102,	0x308b,	0x0210,	0x1399,	0x6726,	0x76af,	0x4434,	0x55bd,
1937c478bd9Sstevel@tonic-gate 	0xad4a,	0xbcc3,	0x8e58,	0x9fd1,	0xeb6e,	0xfae7,	0xc87c,	0xd9f5,
1947c478bd9Sstevel@tonic-gate 	0x3183,	0x200a,	0x1291,	0x0318,	0x77a7,	0x662e,	0x54b5,	0x453c,
1957c478bd9Sstevel@tonic-gate 	0xbdcb,	0xac42,	0x9ed9,	0x8f50,	0xfbef,	0xea66,	0xd8fd,	0xc974,
1967c478bd9Sstevel@tonic-gate 	0x4204,	0x538d,	0x6116,	0x709f,	0x0420,	0x15a9,	0x2732,	0x36bb,
1977c478bd9Sstevel@tonic-gate 	0xce4c,	0xdfc5,	0xed5e,	0xfcd7,	0x8868,	0x99e1,	0xab7a,	0xbaf3,
1987c478bd9Sstevel@tonic-gate 	0x5285,	0x430c,	0x7197,	0x601e,	0x14a1,	0x0528,	0x37b3,	0x263a,
1997c478bd9Sstevel@tonic-gate 	0xdecd,	0xcf44,	0xfddf,	0xec56,	0x98e9,	0x8960,	0xbbfb,	0xaa72,
2007c478bd9Sstevel@tonic-gate 	0x6306,	0x728f,	0x4014,	0x519d,	0x2522,	0x34ab,	0x0630,	0x17b9,
2017c478bd9Sstevel@tonic-gate 	0xef4e,	0xfec7,	0xcc5c,	0xddd5,	0xa96a,	0xb8e3,	0x8a78,	0x9bf1,
2027c478bd9Sstevel@tonic-gate 	0x7387,	0x620e,	0x5095,	0x411c,	0x35a3,	0x242a,	0x16b1,	0x0738,
2037c478bd9Sstevel@tonic-gate 	0xffcf,	0xee46,	0xdcdd,	0xcd54,	0xb9eb,	0xa862,	0x9af9,	0x8b70,
2047c478bd9Sstevel@tonic-gate 	0x8408,	0x9581,	0xa71a,	0xb693,	0xc22c,	0xd3a5,	0xe13e,	0xf0b7,
2057c478bd9Sstevel@tonic-gate 	0x0840,	0x19c9,	0x2b52,	0x3adb,	0x4e64,	0x5fed,	0x6d76,	0x7cff,
2067c478bd9Sstevel@tonic-gate 	0x9489,	0x8500,	0xb79b,	0xa612,	0xd2ad,	0xc324,	0xf1bf,	0xe036,
2077c478bd9Sstevel@tonic-gate 	0x18c1,	0x0948,	0x3bd3,	0x2a5a,	0x5ee5,	0x4f6c,	0x7df7,	0x6c7e,
2087c478bd9Sstevel@tonic-gate 	0xa50a,	0xb483,	0x8618,	0x9791,	0xe32e,	0xf2a7,	0xc03c,	0xd1b5,
2097c478bd9Sstevel@tonic-gate 	0x2942,	0x38cb,	0x0a50,	0x1bd9,	0x6f66,	0x7eef,	0x4c74,	0x5dfd,
2107c478bd9Sstevel@tonic-gate 	0xb58b,	0xa402,	0x9699,	0x8710,	0xf3af,	0xe226,	0xd0bd,	0xc134,
2117c478bd9Sstevel@tonic-gate 	0x39c3,	0x284a,	0x1ad1,	0x0b58,	0x7fe7,	0x6e6e,	0x5cf5,	0x4d7c,
2127c478bd9Sstevel@tonic-gate 	0xc60c,	0xd785,	0xe51e,	0xf497,	0x8028,	0x91a1,	0xa33a,	0xb2b3,
2137c478bd9Sstevel@tonic-gate 	0x4a44,	0x5bcd,	0x6956,	0x78df,	0x0c60,	0x1de9,	0x2f72,	0x3efb,
2147c478bd9Sstevel@tonic-gate 	0xd68d,	0xc704,	0xf59f,	0xe416,	0x90a9,	0x8120,	0xb3bb,	0xa232,
2157c478bd9Sstevel@tonic-gate 	0x5ac5,	0x4b4c,	0x79d7,	0x685e,	0x1ce1,	0x0d68,	0x3ff3,	0x2e7a,
2167c478bd9Sstevel@tonic-gate 	0xe70e,	0xf687,	0xc41c,	0xd595,	0xa12a,	0xb0a3,	0x8238,	0x93b1,
2177c478bd9Sstevel@tonic-gate 	0x6b46,	0x7acf,	0x4854,	0x59dd,	0x2d62,	0x3ceb,	0x0e70,	0x1ff9,
2187c478bd9Sstevel@tonic-gate 	0xf78f,	0xe606,	0xd49d,	0xc514,	0xb1ab,	0xa022,	0x92b9,	0x8330,
2197c478bd9Sstevel@tonic-gate 	0x7bc7,	0x6a4e,	0x58d5,	0x495c,	0x3de3,	0x2c6a,	0x1ef1,	0x0f78
2207c478bd9Sstevel@tonic-gate };
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate struct pkt {
2237c478bd9Sstevel@tonic-gate     int	cnt;
2247c478bd9Sstevel@tonic-gate     int	esc;
2257c478bd9Sstevel@tonic-gate     int	flags;
2267c478bd9Sstevel@tonic-gate     struct compressor *comp;
2277c478bd9Sstevel@tonic-gate     void *state;
2287c478bd9Sstevel@tonic-gate     unsigned char buf[8192];
2297c478bd9Sstevel@tonic-gate } spkt, rpkt;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /* Values for flags */
2327c478bd9Sstevel@tonic-gate #define CCP_ISUP	1
2337c478bd9Sstevel@tonic-gate #define CCP_ERROR	2
2347c478bd9Sstevel@tonic-gate #define CCP_FATALERROR	4
2357c478bd9Sstevel@tonic-gate #define CCP_ERR		(CCP_ERROR | CCP_FATALERROR)
2367c478bd9Sstevel@tonic-gate #define CCP_DECOMP_RUN	8
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate unsigned char dbuf[8192];
2397c478bd9Sstevel@tonic-gate 
240*aee32e3dScarlsonj void
dumpppp(f)2417c478bd9Sstevel@tonic-gate dumpppp(f)
2427c478bd9Sstevel@tonic-gate     FILE *f;
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate     int c, n, k;
2457c478bd9Sstevel@tonic-gate     int nb, nl, dn, proto, rv;
2467c478bd9Sstevel@tonic-gate     char *dir, *q;
2477c478bd9Sstevel@tonic-gate     unsigned char *p, *r, *endp;
2487c478bd9Sstevel@tonic-gate     unsigned char *d;
2497c478bd9Sstevel@tonic-gate     unsigned short fcs;
2507c478bd9Sstevel@tonic-gate     struct pkt *pkt;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate     spkt.cnt = rpkt.cnt = 0;
2537c478bd9Sstevel@tonic-gate     spkt.esc = rpkt.esc = 0;
2547c478bd9Sstevel@tonic-gate     while ((c = getc(f)) != EOF) {
2557c478bd9Sstevel@tonic-gate 	switch (c) {
2567c478bd9Sstevel@tonic-gate 	case RECMARK_STARTSEND:
2577c478bd9Sstevel@tonic-gate 	case RECMARK_STARTRECV:
2587c478bd9Sstevel@tonic-gate 	    if (reverse)
2597c478bd9Sstevel@tonic-gate 		c = c==RECMARK_STARTSEND ? RECMARK_STARTRECV :
2607c478bd9Sstevel@tonic-gate 		    RECMARK_STARTSEND;
2617c478bd9Sstevel@tonic-gate 	    dir = c==RECMARK_STARTSEND? "sent": "rcvd";
2627c478bd9Sstevel@tonic-gate 	    pkt = c==RECMARK_STARTSEND? &spkt: &rpkt;
2637c478bd9Sstevel@tonic-gate 	    n = getc(f);
2647c478bd9Sstevel@tonic-gate 	    n = (n << 8) + getc(f);
2657c478bd9Sstevel@tonic-gate 	    *(c==1? &tot_sent: &tot_rcvd) += n;
2667c478bd9Sstevel@tonic-gate 	    for (; n > 0; --n) {
2677c478bd9Sstevel@tonic-gate 		c = getc(f);
2687c478bd9Sstevel@tonic-gate 		switch (c) {
2697c478bd9Sstevel@tonic-gate 		case EOF:
2707c478bd9Sstevel@tonic-gate 		    printf("\nEOF\n");
2717c478bd9Sstevel@tonic-gate 		    if (spkt.cnt > 0)
2727c478bd9Sstevel@tonic-gate 			printf("[%d bytes in incomplete send packet]\n",
2737c478bd9Sstevel@tonic-gate 			       spkt.cnt);
2747c478bd9Sstevel@tonic-gate 		    if (rpkt.cnt > 0)
2757c478bd9Sstevel@tonic-gate 			printf("[%d bytes in incomplete recv packet]\n",
2767c478bd9Sstevel@tonic-gate 			       rpkt.cnt);
2777c478bd9Sstevel@tonic-gate 		    exit(0);
2787c478bd9Sstevel@tonic-gate 		case '~':
2797c478bd9Sstevel@tonic-gate 		    if (pkt->cnt > 0) {
2807c478bd9Sstevel@tonic-gate 			q = dir;
2817c478bd9Sstevel@tonic-gate 			if (pkt->esc) {
2827c478bd9Sstevel@tonic-gate 			    printf("%s aborted packet:\n     ", dir);
2837c478bd9Sstevel@tonic-gate 			    q = "    ";
2847c478bd9Sstevel@tonic-gate 			}
2857c478bd9Sstevel@tonic-gate 			nb = pkt->cnt;
2867c478bd9Sstevel@tonic-gate 			p = pkt->buf;
2877c478bd9Sstevel@tonic-gate 			pkt->cnt = 0;
2887c478bd9Sstevel@tonic-gate 			pkt->esc = 0;
2897c478bd9Sstevel@tonic-gate 			if (nb <= 2) {
2907c478bd9Sstevel@tonic-gate 			    printf("%s short packet [%d bytes]:", q, nb);
2917c478bd9Sstevel@tonic-gate 			    for (k = 0; k < nb; ++k)
2927c478bd9Sstevel@tonic-gate 				printf(" %.2x", p[k]);
2937c478bd9Sstevel@tonic-gate 			    printf("\n");
2947c478bd9Sstevel@tonic-gate 			    break;
2957c478bd9Sstevel@tonic-gate 			}
2967c478bd9Sstevel@tonic-gate 			fcs = PPP_INITFCS;
2977c478bd9Sstevel@tonic-gate 			for (k = 0; k < nb; ++k)
2987c478bd9Sstevel@tonic-gate 			    fcs = PPP_FCS(fcs, p[k]);
2997c478bd9Sstevel@tonic-gate 			fcs &= 0xFFFF;
3007c478bd9Sstevel@tonic-gate 			nb -= 2;
3017c478bd9Sstevel@tonic-gate 			endp = p + nb;
3027c478bd9Sstevel@tonic-gate 			r = p;
3037c478bd9Sstevel@tonic-gate 			if (r[0] == 0xff && r[1] == 3)
3047c478bd9Sstevel@tonic-gate 			    r += 2;
3057c478bd9Sstevel@tonic-gate 			if ((r[0] & 1) == 0)
3067c478bd9Sstevel@tonic-gate 			    ++r;
3077c478bd9Sstevel@tonic-gate 			++r;
3087c478bd9Sstevel@tonic-gate 			if (endp - r > mru)
3097c478bd9Sstevel@tonic-gate 			    printf("     ERROR: length (%d) > MRU (%d)\n",
3107c478bd9Sstevel@tonic-gate 				   endp - r, mru);
3117c478bd9Sstevel@tonic-gate 			if (decompress && fcs == PPP_GOODFCS) {
3127c478bd9Sstevel@tonic-gate 			    /* See if this is a CCP or compressed packet */
3137c478bd9Sstevel@tonic-gate 			    d = dbuf;
3147c478bd9Sstevel@tonic-gate 			    r = p;
3157c478bd9Sstevel@tonic-gate 			    if (r[0] == 0xff && r[1] == 3) {
3167c478bd9Sstevel@tonic-gate 				*d++ = *r++;
3177c478bd9Sstevel@tonic-gate 				*d++ = *r++;
3187c478bd9Sstevel@tonic-gate 			    }
3197c478bd9Sstevel@tonic-gate 			    proto = r[0];
3207c478bd9Sstevel@tonic-gate 			    if ((proto & 1) == 0)
3217c478bd9Sstevel@tonic-gate 				proto = (proto << 8) + r[1];
3227c478bd9Sstevel@tonic-gate 			    if (proto == PPP_CCP) {
3237c478bd9Sstevel@tonic-gate 				handle_ccp(pkt, r + 2, endp - r - 2);
3247c478bd9Sstevel@tonic-gate 			    } else if (proto == PPP_COMP) {
3257c478bd9Sstevel@tonic-gate 				if ((pkt->flags & CCP_ISUP)
3267c478bd9Sstevel@tonic-gate 				    && (pkt->flags & CCP_DECOMP_RUN)
3277c478bd9Sstevel@tonic-gate 				    && pkt->state
3287c478bd9Sstevel@tonic-gate 				    && (pkt->flags & CCP_ERR) == 0) {
3297c478bd9Sstevel@tonic-gate 				    rv = pkt->comp->decompress(pkt->state, r,
3307c478bd9Sstevel@tonic-gate 							endp - r, d, &dn);
3317c478bd9Sstevel@tonic-gate 				    switch (rv) {
3327c478bd9Sstevel@tonic-gate 				    case DECOMP_OK:
3337c478bd9Sstevel@tonic-gate 					p = dbuf;
3347c478bd9Sstevel@tonic-gate 					nb = d + dn - p;
3357c478bd9Sstevel@tonic-gate 					if ((d[0] & 1) == 0)
3367c478bd9Sstevel@tonic-gate 					    --dn;
3377c478bd9Sstevel@tonic-gate 					--dn;
3387c478bd9Sstevel@tonic-gate 					if (dn > mru)
3397c478bd9Sstevel@tonic-gate 					    printf("     ERROR: decompressed length (%d) > MRU (%d)\n", dn, mru);
3407c478bd9Sstevel@tonic-gate 					break;
3417c478bd9Sstevel@tonic-gate 				    case DECOMP_ERROR:
3427c478bd9Sstevel@tonic-gate 					printf("     DECOMPRESSION ERROR\n");
3437c478bd9Sstevel@tonic-gate 					pkt->flags |= CCP_ERROR;
3447c478bd9Sstevel@tonic-gate 					break;
3457c478bd9Sstevel@tonic-gate 				    case DECOMP_FATALERROR:
3467c478bd9Sstevel@tonic-gate 					printf("     FATAL DECOMPRESSION ERROR\n");
3477c478bd9Sstevel@tonic-gate 					pkt->flags |= CCP_FATALERROR;
3487c478bd9Sstevel@tonic-gate 					break;
3497c478bd9Sstevel@tonic-gate 				    }
3507c478bd9Sstevel@tonic-gate 				}
3517c478bd9Sstevel@tonic-gate 			    } else if (pkt->state
3527c478bd9Sstevel@tonic-gate 				       && (pkt->flags & CCP_DECOMP_RUN)) {
3537c478bd9Sstevel@tonic-gate 				pkt->comp->incomp(pkt->state, r, endp - r);
3547c478bd9Sstevel@tonic-gate 			    }
3557c478bd9Sstevel@tonic-gate 			}
3567c478bd9Sstevel@tonic-gate 			do {
3577c478bd9Sstevel@tonic-gate 			    nl = nb < 16? nb: 16;
3587c478bd9Sstevel@tonic-gate 			    printf("%s ", q);
3597c478bd9Sstevel@tonic-gate 			    for (k = 0; k < nl; ++k)
3607c478bd9Sstevel@tonic-gate 				printf(" %.2x", p[k]);
3617c478bd9Sstevel@tonic-gate 			    for (; k < 16; ++k)
3627c478bd9Sstevel@tonic-gate 				printf("   ");
3637c478bd9Sstevel@tonic-gate 			    printf("  ");
3647c478bd9Sstevel@tonic-gate 			    for (k = 0; k < nl; ++k) {
3657c478bd9Sstevel@tonic-gate 				c = p[k];
3667c478bd9Sstevel@tonic-gate 				putchar((' ' <= c && c <= '~')? c: '.');
3677c478bd9Sstevel@tonic-gate 			    }
3687c478bd9Sstevel@tonic-gate 			    printf("\n");
3697c478bd9Sstevel@tonic-gate 			    q = "    ";
3707c478bd9Sstevel@tonic-gate 			    p += nl;
3717c478bd9Sstevel@tonic-gate 			    nb -= nl;
3727c478bd9Sstevel@tonic-gate 			} while (nb > 0);
3737c478bd9Sstevel@tonic-gate 			if (fcs != PPP_GOODFCS)
3747c478bd9Sstevel@tonic-gate 			    printf("     BAD FCS: (residue = %x)\n", fcs);
3757c478bd9Sstevel@tonic-gate 		    }
3767c478bd9Sstevel@tonic-gate 		    break;
3777c478bd9Sstevel@tonic-gate 		case '}':
3787c478bd9Sstevel@tonic-gate 		    if (!pkt->esc) {
3797c478bd9Sstevel@tonic-gate 			pkt->esc = 1;
3807c478bd9Sstevel@tonic-gate 			break;
3817c478bd9Sstevel@tonic-gate 		    }
3827c478bd9Sstevel@tonic-gate 		    /* else fall through */
3837c478bd9Sstevel@tonic-gate 		default:
3847c478bd9Sstevel@tonic-gate 		    if (pkt->esc) {
3857c478bd9Sstevel@tonic-gate 			c ^= 0x20;
3867c478bd9Sstevel@tonic-gate 			pkt->esc = 0;
3877c478bd9Sstevel@tonic-gate 		    }
3887c478bd9Sstevel@tonic-gate 		    pkt->buf[pkt->cnt++] = c;
3897c478bd9Sstevel@tonic-gate 		    break;
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 	    }
3927c478bd9Sstevel@tonic-gate 	    break;
3937c478bd9Sstevel@tonic-gate 	case RECMARK_ENDSEND:
3947c478bd9Sstevel@tonic-gate 	case RECMARK_ENDRECV:
3957c478bd9Sstevel@tonic-gate 	    if (reverse)
3967c478bd9Sstevel@tonic-gate 		c = c==RECMARK_ENDSEND ? RECMARK_ENDRECV : RECMARK_ENDSEND;
3977c478bd9Sstevel@tonic-gate 	    dir = c==RECMARK_ENDSEND ? "send": "recv";
3987c478bd9Sstevel@tonic-gate 	    pkt = c==RECMARK_ENDSEND ? &spkt: &rpkt;
3997c478bd9Sstevel@tonic-gate 	    printf("end %s", dir);
4007c478bd9Sstevel@tonic-gate 	    if (pkt->cnt > 0)
4017c478bd9Sstevel@tonic-gate 		printf("  [%d bytes in incomplete packet]", pkt->cnt);
4027c478bd9Sstevel@tonic-gate 	    printf("\n");
4037c478bd9Sstevel@tonic-gate 	    break;
4047c478bd9Sstevel@tonic-gate 	case RECMARK_TIMEDELTA32:
4057c478bd9Sstevel@tonic-gate 	case RECMARK_TIMEDELTA8:
4067c478bd9Sstevel@tonic-gate 	case RECMARK_TIMESTART:
4077c478bd9Sstevel@tonic-gate 	    show_time(f, c);
4087c478bd9Sstevel@tonic-gate 	    break;
4097c478bd9Sstevel@tonic-gate 	default:
4107c478bd9Sstevel@tonic-gate 	    printf("?%.2x\n");
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate     }
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate extern struct compressor ppp_bsd_compress, ppp_deflate;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate struct compressor *compressors[] = {
4187c478bd9Sstevel@tonic-gate #if DO_BSD_COMPRESS
4197c478bd9Sstevel@tonic-gate     &ppp_bsd_compress,
4207c478bd9Sstevel@tonic-gate #endif
4217c478bd9Sstevel@tonic-gate #if DO_DEFLATE
4227c478bd9Sstevel@tonic-gate     &ppp_deflate,
4237c478bd9Sstevel@tonic-gate #endif
4247c478bd9Sstevel@tonic-gate     NULL
4257c478bd9Sstevel@tonic-gate };
4267c478bd9Sstevel@tonic-gate 
427*aee32e3dScarlsonj void
handle_ccp(cp,dp,len)4287c478bd9Sstevel@tonic-gate handle_ccp(cp, dp, len)
4297c478bd9Sstevel@tonic-gate     struct pkt *cp;
4307c478bd9Sstevel@tonic-gate     u_char *dp;
4317c478bd9Sstevel@tonic-gate     int len;
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate     int clen;
4347c478bd9Sstevel@tonic-gate     struct compressor **comp;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate     if (len < CCP_HDRLEN)
4377c478bd9Sstevel@tonic-gate 	return;
4387c478bd9Sstevel@tonic-gate     clen = CCP_LENGTH(dp);
4397c478bd9Sstevel@tonic-gate     if (clen > len)
4407c478bd9Sstevel@tonic-gate 	return;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate     switch (CCP_CODE(dp)) {
4437c478bd9Sstevel@tonic-gate     case CCP_CONFACK:
4447c478bd9Sstevel@tonic-gate 	cp->flags &= ~(CCP_DECOMP_RUN | CCP_ISUP);
4457c478bd9Sstevel@tonic-gate 	if (clen < CCP_HDRLEN + CCP_OPT_MINLEN
4467c478bd9Sstevel@tonic-gate 	    || clen < CCP_HDRLEN + CCP_OPT_LENGTH(dp + CCP_HDRLEN))
4477c478bd9Sstevel@tonic-gate 	    break;
4487c478bd9Sstevel@tonic-gate 	dp += CCP_HDRLEN;
4497c478bd9Sstevel@tonic-gate 	clen -= CCP_HDRLEN;
4507c478bd9Sstevel@tonic-gate 	for (comp = compressors; *comp != NULL; ++comp) {
4517c478bd9Sstevel@tonic-gate 	    if ((*comp)->compress_proto == dp[0]) {
4527c478bd9Sstevel@tonic-gate 		if (cp->state != NULL) {
4537c478bd9Sstevel@tonic-gate 		    (*cp->comp->decomp_free)(cp->state);
4547c478bd9Sstevel@tonic-gate 		    cp->state = NULL;
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 		cp->comp = *comp;
4577c478bd9Sstevel@tonic-gate 		cp->state = (*comp)->decomp_alloc(dp, CCP_OPT_LENGTH(dp));
4587c478bd9Sstevel@tonic-gate 		cp->flags |= CCP_ISUP;
4597c478bd9Sstevel@tonic-gate 		if (cp->state != NULL
4607c478bd9Sstevel@tonic-gate 		    && (*cp->comp->decomp_init)
4617c478bd9Sstevel@tonic-gate 		        (cp->state, dp, clen, 0, 0, 8192, 1))
4627c478bd9Sstevel@tonic-gate 		    cp->flags = (cp->flags & ~CCP_ERR) | CCP_DECOMP_RUN;
4637c478bd9Sstevel@tonic-gate 		break;
4647c478bd9Sstevel@tonic-gate 	    }
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 	break;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate     case CCP_CONFNAK:
4697c478bd9Sstevel@tonic-gate     case CCP_CONFREJ:
4707c478bd9Sstevel@tonic-gate 	cp->flags &= ~(CCP_DECOMP_RUN | CCP_ISUP);
4717c478bd9Sstevel@tonic-gate 	break;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate     case CCP_RESETACK:
4747c478bd9Sstevel@tonic-gate 	if (cp->flags & CCP_ISUP) {
4757c478bd9Sstevel@tonic-gate 	    if (cp->state && (cp->flags & CCP_DECOMP_RUN)) {
4767c478bd9Sstevel@tonic-gate 		(*cp->comp->decomp_reset)(cp->state);
4777c478bd9Sstevel@tonic-gate 		cp->flags &= ~CCP_ERROR;
4787c478bd9Sstevel@tonic-gate 	    }
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 	break;
4817c478bd9Sstevel@tonic-gate     }
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
484*aee32e3dScarlsonj void
show_time(f,c)4857c478bd9Sstevel@tonic-gate show_time(f, c)
4867c478bd9Sstevel@tonic-gate     FILE *f;
4877c478bd9Sstevel@tonic-gate     int c;
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate     time_t t;
4907c478bd9Sstevel@tonic-gate     int n;
4917c478bd9Sstevel@tonic-gate     struct tm *tm;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate     if (c == RECMARK_TIMESTART) {
4947c478bd9Sstevel@tonic-gate 	t = getc(f);
4957c478bd9Sstevel@tonic-gate 	t = (t << 8) + getc(f);
4967c478bd9Sstevel@tonic-gate 	t = (t << 8) + getc(f);
4977c478bd9Sstevel@tonic-gate 	t = (t << 8) + getc(f);
4987c478bd9Sstevel@tonic-gate 	printf("start %s", ctime(&t));
4997c478bd9Sstevel@tonic-gate 	start_time = t;
5007c478bd9Sstevel@tonic-gate 	start_time_tenths = 0;
5017c478bd9Sstevel@tonic-gate 	tot_sent = tot_rcvd = 0;
5027c478bd9Sstevel@tonic-gate     } else {
5037c478bd9Sstevel@tonic-gate 	n = getc(f);
5047c478bd9Sstevel@tonic-gate 	if (c == RECMARK_TIMEDELTA32) {
5057c478bd9Sstevel@tonic-gate 	    for (c = 3; c > 0; --c)
5067c478bd9Sstevel@tonic-gate 		n = (n << 8) + getc(f);
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 	if (abs_times) {
5097c478bd9Sstevel@tonic-gate 	    n += start_time_tenths;
5107c478bd9Sstevel@tonic-gate 	    start_time += n / 10;
5117c478bd9Sstevel@tonic-gate 	    start_time_tenths = n % 10;
5127c478bd9Sstevel@tonic-gate 	    tm = localtime(&start_time);
5137c478bd9Sstevel@tonic-gate 	    printf("time  %.2d:%.2d:%.2d.%d", tm->tm_hour, tm->tm_min,
5147c478bd9Sstevel@tonic-gate 		   tm->tm_sec, start_time_tenths);
5157c478bd9Sstevel@tonic-gate 	    printf("  (sent %d, rcvd %d)\n", tot_sent, tot_rcvd);
5167c478bd9Sstevel@tonic-gate 	} else
5177c478bd9Sstevel@tonic-gate 	    printf("time  %.1fs\n", (double) n / 10);
5187c478bd9Sstevel@tonic-gate     }
5197c478bd9Sstevel@tonic-gate }
520