| 1 |
/* |
| 2 |
* pppdump - print out the contents of a record file generated by |
| 3 |
* pppd in readable form. |
| 4 |
* |
| 5 |
* Copyright (c) 1999-2024 Paul Mackerras. All rights reserved. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions |
| 9 |
* are met: |
| 10 |
* |
| 11 |
* 1. Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* |
| 14 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
* notice, this list of conditions and the following disclaimer in |
| 16 |
* the documentation and/or other materials provided with the |
| 17 |
* distribution. |
| 18 |
* |
| 19 |
* THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO |
| 20 |
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 21 |
* AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 22 |
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 23 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN |
| 24 |
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
| 25 |
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 26 |
*/ |
| 27 |
#include <stdio.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <time.h> |
| 31 |
#include <sys/types.h> |
| 32 |
|
| 33 |
int hexmode; |
| 34 |
int pppmode; |
| 35 |
int reverse; |
| 36 |
int mru = 1500; |
| 37 |
int abs_times; |
| 38 |
time_t start_time; |
| 39 |
int start_time_tenths; |
| 40 |
int tot_sent, tot_rcvd; |
| 41 |
|
| 42 |
extern int optind; |
| 43 |
extern char *optarg; |
| 44 |
|
| •45 |
void dumplog(FILE *); |
| 46 |
void dumpppp(FILE *); |
| 47 |
void show_time(FILE *, int); |
| 48 |
|
| 49 |
int |
| •50 |
main(int ac, char **av) |
| 51 |
{ |
| 52 |
int i; |
| 53 |
char *p; |
| 54 |
FILE *f; |
| 55 |
|
| 56 |
while ((i = getopt(ac, av, "hprdm:a")) != -1) { |
| 57 |
switch (i) { |
| 58 |
case 'h': |
| 59 |
hexmode = 1; |
| 60 |
break; |
| 61 |
case 'p': |
| 62 |
pppmode = 1; |
| 63 |
break; |
| 64 |
case 'r': |
| 65 |
reverse = 1; |
| 66 |
break; |
| 67 |
case 'm': |
| 68 |
mru = atoi(optarg); |
| 69 |
break; |
| 70 |
case 'a': |
| 71 |
abs_times = 1; |
| 72 |
break; |
| 73 |
default: |
| 74 |
fprintf(stderr, "Usage: %s [-h | -p[d]] [-r] [-m mru] [-a] [file ...]\n", av[0]); |
| 75 |
exit(1); |
| 76 |
} |
| 77 |
} |
| 78 |
if (optind >= ac) |
| 79 |
dumplog(stdin); |
| 80 |
else { |
| 81 |
for (i = optind; i < ac; ++i) { |
| 82 |
p = av[i]; |
| 83 |
if ((f = fopen(p, "r")) == NULL) { |
| 84 |
perror(p); |
| 85 |
exit(1); |
| 86 |
} |
| 87 |
if (pppmode) |
| 88 |
dumpppp(f); |
| 89 |
else |
| 90 |
dumplog(f); |
| 91 |
fclose(f); |
| 92 |
} |
| 93 |
} |
| 94 |
exit(0); |
| 95 |
} |
| 96 |
|
| 97 |
void |
| •98 |
dumplog(FILE *f) |
| 99 |
{ |
| 100 |
int c, n, k, col; |
| 101 |
int nb, c2; |
| 102 |
unsigned char buf[16]; |
| 103 |
|
| 104 |
while ((c = getc(f)) != EOF) { |
| 105 |
switch (c) { |
| 106 |
case 1: |
| 107 |
case 2: |
| 108 |
if (reverse) |
| 109 |
c = 3 - c; |
| 110 |
printf("%s %c", c==1? "sent": "rcvd", hexmode? ' ': '"'); |
| 111 |
col = 6; |
| 112 |
n = getc(f); |
| 113 |
n = (n << 8) + getc(f); |
| 114 |
*(c==1? &tot_sent: &tot_rcvd) += n; |
| 115 |
nb = 0; |
| 116 |
for (; n > 0; --n) { |
| 117 |
c = getc(f); |
| 118 |
if (c == EOF) { |
| 119 |
printf("\nEOF\n"); |
| 120 |
exit(0); |
| 121 |
} |
| 122 |
if (hexmode) { |
| 123 |
if (nb >= 16) { |
| 124 |
printf(" "); |
| 125 |
for (k = 0; k < nb; ++k) { |
| 126 |
c2 = buf[k]; |
| 127 |
putchar((' ' <= c2 && c2 <= '~')? c2: '.'); |
| 128 |
} |
| 129 |
printf("\n "); |
| 130 |
nb = 0; |
| 131 |
} |
| 132 |
buf[nb++] = c; |
| 133 |
printf(" %.2x", c); |
| 134 |
} else { |
| 135 |
k = (' ' <= c && c <= '~')? (c != '\\' && c != '"')? 1: 2: 3; |
| 136 |
if ((col += k) >= 78) { |
| 137 |
printf("\n "); |
| 138 |
col = 6 + k; |
| 139 |
} |
| 140 |
switch (k) { |
| 141 |
case 1: |
| 142 |
putchar(c); |
| 143 |
break; |
| 144 |
case 2: |
| 145 |
printf("\\%c", c); |
| 146 |
break; |
| 147 |
case 3: |
| 148 |
printf("\\%.2x", c); |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
if (hexmode) { |
| 154 |
for (k = nb; k < 16; ++k) |
| 155 |
printf(" "); |
| 156 |
printf(" "); |
| 157 |
for (k = 0; k < nb; ++k) { |
| 158 |
c2 = buf[k]; |
| 159 |
putchar((' ' <= c2 && c2 <= '~')? c2: '.'); |
| 160 |
} |
| 161 |
} else |
| 162 |
putchar('"'); |
| 163 |
printf("\n"); |
| 164 |
break; |
| 165 |
case 3: |
| 166 |
case 4: |
| 167 |
printf("end %s\n", c==3? "send": "recv"); |
| 168 |
break; |
| 169 |
case 5: |
| 170 |
case 6: |
| 171 |
case 7: |
| 172 |
show_time(f, c); |
| 173 |
break; |
| 174 |
default: |
| 175 |
printf("?%.2x\n", c); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/* |
| 181 |
* FCS lookup table as calculated by genfcstab. |
| 182 |
*/ |
| 183 |
static u_short fcstab[256] = { |
| 184 |
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, |
| 185 |
0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, |
| 186 |
0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, |
| 187 |
0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, |
| 188 |
0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, |
| 189 |
0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, |
| 190 |
0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, |
| 191 |
0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, |
| 192 |
0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, |
| 193 |
0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, |
| 194 |
0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, |
| 195 |
0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, |
| 196 |
0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, |
| 197 |
0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, |
| 198 |
0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, |
| 199 |
0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, |
| 200 |
0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, |
| 201 |
0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, |
| 202 |
0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, |
| 203 |
0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, |
| 204 |
0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, |
| 205 |
0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, |
| 206 |
0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, |
| 207 |
0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, |
| 208 |
0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, |
| 209 |
0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, |
| 210 |
0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, |
| 211 |
0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, |
| 212 |
0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, |
| 213 |
0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, |
| 214 |
0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, |
| 215 |
0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 |
| 216 |
}; |
| 217 |
#define PPP_FCS(fcs, c) (((fcs) >> 8) ^ fcstab[((fcs) ^ (c)) & 0xff]) |
| 218 |
|
| 219 |
#define PPP_INITFCS 0xffff /* Initial FCS value */ |
| 220 |
#define PPP_GOODFCS 0xf0b8 /* Good final FCS value */ |
| 221 |
|
| 222 |
struct pkt { |
| 223 |
int cnt; |
| 224 |
int esc; |
| 225 |
int flags; |
| 226 |
struct compressor *comp; |
| 227 |
void *state; |
| 228 |
unsigned char buf[8192]; |
| 229 |
} spkt, rpkt; |
| 230 |
|
| 231 |
/* Values for flags */ |
| 232 |
#define CCP_ISUP 1 |
| 233 |
#define CCP_ERROR 2 |
| 234 |
#define CCP_FATALERROR 4 |
| 235 |
#define CCP_ERR (CCP_ERROR | CCP_FATALERROR) |
| 236 |
#define CCP_DECOMP_RUN 8 |
| 237 |
|
| 238 |
unsigned char dbuf[8192]; |
| 239 |
|
| 240 |
void |
| •241 |
dumpppp(FILE *f) |
| 242 |
{ |
| 243 |
int c, n, k; |
| 244 |
int nb, nl, dn, proto, rv; |
| 245 |
char *dir, *q; |
| 246 |
unsigned char *p, *r, *endp; |
| 247 |
unsigned char *d; |
| 248 |
unsigned short fcs; |
| 249 |
struct pkt *pkt; |
| 250 |
|
| 251 |
spkt.cnt = rpkt.cnt = 0; |
| 252 |
spkt.esc = rpkt.esc = 0; |
| 253 |
while ((c = getc(f)) != EOF) { |
| 254 |
switch (c) { |
| 255 |
case 1: |
| 256 |
case 2: |
| 257 |
if (reverse) |
| 258 |
c = 3 - c; |
| 259 |
dir = c==1? "sent": "rcvd"; |
| 260 |
pkt = c==1? &spkt: &rpkt; |
| 261 |
n = getc(f); |
| 262 |
n = (n << 8) + getc(f); |
| 263 |
*(c==1? &tot_sent: &tot_rcvd) += n; |
| 264 |
for (; n > 0; --n) { |
| 265 |
c = getc(f); |
| 266 |
switch (c) { |
| 267 |
case EOF: |
| 268 |
printf("\nEOF\n"); |
| 269 |
if (spkt.cnt > 0) |
| 270 |
printf("[%d bytes in incomplete send packet]\n", |
| 271 |
spkt.cnt); |
| 272 |
if (rpkt.cnt > 0) |
| 273 |
printf("[%d bytes in incomplete recv packet]\n", |
| 274 |
rpkt.cnt); |
| 275 |
exit(0); |
| 276 |
case '~': |
| 277 |
if (pkt->cnt > 0) { |
| 278 |
q = dir; |
| 279 |
if (pkt->esc) { |
| 280 |
printf("%s aborted packet:\n ", dir); |
| 281 |
q = " "; |
| 282 |
} |
| 283 |
if (pkt->cnt >= sizeof(pkt->buf)) { |
| 284 |
printf("%s over-long packet truncated:\n ", dir); |
| 285 |
q = " "; |
| 286 |
} |
| 287 |
nb = pkt->cnt; |
| 288 |
p = pkt->buf; |
| 289 |
pkt->cnt = 0; |
| 290 |
pkt->esc = 0; |
| 291 |
if (nb <= 2) { |
| 292 |
printf("%s short packet [%d bytes]:", q, nb); |
| 293 |
for (k = 0; k < nb; ++k) |
| 294 |
printf(" %.2x", p[k]); |
| 295 |
printf("\n"); |
| 296 |
break; |
| 297 |
} |
| 298 |
fcs = PPP_INITFCS; |
| 299 |
for (k = 0; k < nb; ++k) |
| 300 |
fcs = PPP_FCS(fcs, p[k]); |
| 301 |
fcs &= 0xFFFF; |
| 302 |
nb -= 2; |
| 303 |
endp = p + nb; |
| 304 |
r = p; |
| 305 |
if (r[0] == 0xff && r[1] == 3) |
| 306 |
r += 2; |
| 307 |
if ((r[0] & 1) == 0) |
| 308 |
++r; |
| 309 |
++r; |
| 310 |
if (endp - r > mru) |
| 311 |
printf(" ERROR: length (%zd) > MRU (%d)\n", |
| 312 |
endp - r, mru); |
| 313 |
do { |
| 314 |
nl = nb < 16? nb: 16; |
| 315 |
printf("%s ", q); |
| 316 |
for (k = 0; k < nl; ++k) |
| 317 |
printf(" %.2x", p[k]); |
| 318 |
for (; k < 16; ++k) |
| 319 |
printf(" "); |
| 320 |
printf(" "); |
| 321 |
for (k = 0; k < nl; ++k) { |
| 322 |
c = p[k]; |
| 323 |
putchar((' ' <= c && c <= '~')? c: '.'); |
| 324 |
} |
| 325 |
printf("\n"); |
| 326 |
q = " "; |
| 327 |
p += nl; |
| 328 |
nb -= nl; |
| 329 |
} while (nb > 0); |
| 330 |
if (fcs != PPP_GOODFCS) |
| 331 |
printf(" BAD FCS: (residue = %x)\n", fcs); |
| 332 |
} |
| 333 |
break; |
| 334 |
case '}': |
| 335 |
if (!pkt->esc) { |
| 336 |
pkt->esc = 1; |
| 337 |
break; |
| 338 |
} |
| 339 |
/* else fall through */ |
| 340 |
default: |
| 341 |
if (pkt->esc) { |
| 342 |
c ^= 0x20; |
| 343 |
pkt->esc = 0; |
| 344 |
} |
| 345 |
if (pkt->cnt < sizeof(pkt->buf)) |
| 346 |
pkt->buf[pkt->cnt++] = c; |
| 347 |
break; |
| 348 |
} |
| 349 |
} |
| 350 |
break; |
| 351 |
case 3: |
| 352 |
case 4: |
| 353 |
if (reverse) |
| 354 |
c = 7 - c; |
| 355 |
dir = c==3? "send": "recv"; |
| 356 |
pkt = c==3? &spkt: &rpkt; |
| 357 |
printf("end %s", dir); |
| 358 |
if (pkt->cnt > 0) |
| 359 |
printf(" [%d bytes in incomplete packet]", pkt->cnt); |
| 360 |
printf("\n"); |
| 361 |
break; |
| 362 |
case 5: |
| 363 |
case 6: |
| 364 |
case 7: |
| 365 |
show_time(f, c); |
| 366 |
break; |
| 367 |
default: |
| 368 |
printf("?%.2x\n", c); |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
void |
| •374 |
show_time(FILE *f, int c) |
| 375 |
{ |
| 376 |
time_t t; |
| 377 |
int n; |
| 378 |
struct tm *tm; |
| 379 |
|
| 380 |
if (c == 7) { |
| 381 |
t = getc(f); |
| 382 |
t = (t << 8) + getc(f); |
| 383 |
t = (t << 8) + getc(f); |
| 384 |
t = (t << 8) + getc(f); |
| 385 |
printf("start %s", ctime(&t)); |
| 386 |
start_time = t; |
| 387 |
start_time_tenths = 0; |
| 388 |
tot_sent = tot_rcvd = 0; |
| 389 |
} else { |
| 390 |
n = getc(f); |
| 391 |
if (c == 5) { |
| 392 |
for (c = 3; c > 0; --c) |
| 393 |
n = (n << 8) + getc(f); |
| 394 |
} |
| 395 |
if (abs_times) { |
| 396 |
n += start_time_tenths; |
| 397 |
start_time += n / 10; |
| 398 |
start_time_tenths = n % 10; |
| 399 |
tm = localtime(&start_time); |
| 400 |
printf("time %.2d:%.2d:%.2d.%d", tm->tm_hour, tm->tm_min, |
| 401 |
tm->tm_sec, start_time_tenths); |
| 402 |
printf(" (sent %d, rcvd %d)\n", tot_sent, tot_rcvd); |
| 403 |
} else |
| 404 |
printf("time %.1fs\n", (double) n / 10); |
| 405 |
} |
| 406 |
} |