]> git.ozlabs.org Git - ppp.git/blobdiff - pppdump/pppdump.c
pppoe: Custom host-uniq tag
[ppp.git] / pppdump / pppdump.c
index 25efafb28514dfe0ff948ec46fff93e8a6a10cae..87c2e8f1ac69e95ca080058ad403975ea10a76ba 100644 (file)
@@ -2,15 +2,41 @@
  * pppdump - print out the contents of a record file generated by
  * pppd in readable form.
  *
- * Copyright (C) 1999  Paul Mackerras.  All rights reserved.
+ * Copyright (c) 1999 Paul Mackerras. All rights reserved.
  *
- *  This program is free software; you can redistribute it and/or
- *  modify it under the terms of the GNU General Public License
- *  as published by the Free Software Foundation; either version
- *  2 of the License, or (at your option) any later version.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The name(s) of the authors of this software must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission.
+ *
+ * 4. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by Paul Mackerras
+ *     <paulus@samba.org>".
+ *
+ * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
+ * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
+ * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 #include <stdio.h>
 #include <unistd.h>
+#include <stdlib.h>
+#include <time.h>
 #include <sys/types.h>
 #include "ppp_defs.h"
 #include "ppp-comp.h"
@@ -20,9 +46,20 @@ int pppmode;
 int reverse;
 int decompress;
 int mru = 1500;
+int abs_times;
+time_t start_time;
+int start_time_tenths;
+int tot_sent, tot_rcvd;
 
 extern int optind;
+extern char *optarg;
+
+void dumplog();
+void dumpppp();
+void show_time();
+void handle_ccp();
 
+int
 main(ac, av)
     int ac;
     char **av;
@@ -31,7 +68,7 @@ main(ac, av)
     char *p;
     FILE *f;
 
-    while ((i = getopt(ac, av, "hprdm:")) != -1) {
+    while ((i = getopt(ac, av, "hprdm:a")) != -1) {
        switch (i) {
        case 'h':
            hexmode = 1;
@@ -48,8 +85,11 @@ main(ac, av)
        case 'm':
            mru = atoi(optarg);
            break;
+       case 'a':
+           abs_times = 1;
+           break;
        default:
-           fprintf(stderr, "Usage: %s [-h | -p[d]] [-r] [-m mru] [file ...]\n", av[0]);
+           fprintf(stderr, "Usage: %s [-h | -p[d]] [-r] [-m mru] [-a] [file ...]\n", av[0]);
            exit(1);
        }
     }
@@ -62,14 +102,17 @@ main(ac, av)
                perror(p);
                exit(1);
            }
-           dumplog(f);
+           if (pppmode)
+               dumpppp(f);
+           else
+               dumplog(f);
            fclose(f);
        }
     }
     exit(0);
 }
 
-
+void
 dumplog(f)
     FILE *f;
 {
@@ -77,18 +120,17 @@ dumplog(f)
     int nb, c2;
     unsigned char buf[16];
 
-    if (pppmode) {
-       dumpppp(f);
-       return;
-    }
     while ((c = getc(f)) != EOF) {
-       if (c == 1 || c == 2) {
+       switch (c) {
+       case 1:
+       case 2:
            if (reverse)
                c = 3 - c;
            printf("%s %c", c==1? "sent": "rcvd", hexmode? ' ': '"');
            col = 6;
            n = getc(f);
            n = (n << 8) + getc(f);
+           *(c==1? &tot_sent: &tot_rcvd) += n;
            nb = 0;
            for (; n > 0; --n) {
                c = getc(f);
@@ -138,20 +180,20 @@ dumplog(f)
            } else
                putchar('"');
            printf("\n");
-       } else if (c == 3 || c == 4) {
+           break;
+       case 3:
+       case 4:
            printf("end %s\n", c==3? "send": "recv");
-       } else if (c == 5 || c == 6) {
-           n = getc(f);
-           if (c == 5) {
-               for (c = 3; c > 0; --c)
-                   n = (n << 8) + getc(f);
-           }
-           printf("time %.1fs\n", (double) n / 10);
-       } else {
-           printf("?%.2x\n");
+           break;
+       case 5:
+       case 6:
+       case 7:
+           show_time(f, c);
+           break;
+       default:
+           printf("?%.2x\n", c);
        }
     }
-    exit(0);
 }
 
 /*
@@ -210,6 +252,7 @@ struct pkt {
 
 unsigned char dbuf[8192];
 
+void
 dumpppp(f)
     FILE *f;
 {
@@ -224,13 +267,16 @@ dumpppp(f)
     spkt.cnt = rpkt.cnt = 0;
     spkt.esc = rpkt.esc = 0;
     while ((c = getc(f)) != EOF) {
-       if (c == 1 || c == 2) {
+       switch (c) {
+       case 1:
+       case 2:
            if (reverse)
                c = 3 - c;
            dir = c==1? "sent": "rcvd";
            pkt = c==1? &spkt: &rpkt;
            n = getc(f);
            n = (n << 8) + getc(f);
+           *(c==1? &tot_sent: &tot_rcvd) += n;
            for (; n > 0; --n) {
                c = getc(f);
                switch (c) {
@@ -357,7 +403,9 @@ dumpppp(f)
                    break;
                }
            }
-       } else if (c == 3 || c == 4) {
+           break;
+       case 3:
+       case 4:
            if (reverse)
                c = 7 - c;
            dir = c==3? "send": "recv";
@@ -366,15 +414,14 @@ dumpppp(f)
            if (pkt->cnt > 0)
                printf("  [%d bytes in incomplete packet]", pkt->cnt);
            printf("\n");
-       } else if (c == 5 || c == 6) {
-           n = getc(f);
-           if (c == 5) {
-               for (c = 3; c > 0; --c)
-                   n = (n << 8) + getc(f);
-           }
-           printf("time %.1fs\n", (double) n / 10);
-       } else {
-           printf("?%.2x\n");
+           break;
+       case 5:
+       case 6:
+       case 7:
+           show_time(f, c);
+           break;
+       default:
+           printf("?%.2x\n", c);
        }
     }
 }
@@ -391,6 +438,7 @@ struct compressor *compressors[] = {
     NULL
 };
 
+void
 handle_ccp(cp, dp, len)
     struct pkt *cp;
     u_char *dp;
@@ -446,3 +494,40 @@ handle_ccp(cp, dp, len)
        break;
     }
 }
+
+void
+show_time(f, c)
+    FILE *f;
+    int c;
+{
+    time_t t;
+    int n;
+    struct tm *tm;
+
+    if (c == 7) {
+       t = getc(f);
+       t = (t << 8) + getc(f);
+       t = (t << 8) + getc(f);
+       t = (t << 8) + getc(f);
+       printf("start %s", ctime(&t));
+       start_time = t;
+       start_time_tenths = 0;
+       tot_sent = tot_rcvd = 0;
+    } else {
+       n = getc(f);
+       if (c == 5) {
+           for (c = 3; c > 0; --c)
+               n = (n << 8) + getc(f);
+       }
+       if (abs_times) {
+           n += start_time_tenths;
+           start_time += n / 10;
+           start_time_tenths = n % 10;
+           tm = localtime(&start_time);
+           printf("time  %.2d:%.2d:%.2d.%d", tm->tm_hour, tm->tm_min,
+                  tm->tm_sec, start_time_tenths);
+           printf("  (sent %d, rcvd %d)\n", tot_sent, tot_rcvd);
+       } else
+           printf("time  %.1fs\n", (double) n / 10);
+    }
+}