]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/pppoe/debug.c
Merge pull request #297 from mjeveritt/patch-11-test-pr
[ppp.git] / pppd / plugins / pppoe / debug.c
1 /***********************************************************************
2 *
3 * debug.c
4 *
5 * Implementation of user-space PPPoE redirector for Linux.
6 *
7 * Functions for printing debugging information
8 *
9 * Copyright (C) 2000 by Roaring Penguin Software Inc.
10 *
11 * This program may be distributed according to the terms of the GNU
12 * General Public License, version 2 or (at your option) any later version.
13 *
14 ***********************************************************************/
15
16 static char const RCSID[] =
17 "$Id: debug.c,v 1.2 2008/06/09 08:34:23 paulus Exp $";
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include "pppoe.h"
24 #include <sys/time.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <ctype.h>
28
29 /**********************************************************************
30 *%FUNCTION: dumpHex
31 *%ARGUMENTS:
32 * fp -- file to dump to
33 * buf -- buffer to dump
34 * len -- length of data
35 *%RETURNS:
36 * Nothing
37 *%DESCRIPTION:
38 * Dumps buffer to fp in an easy-to-read format
39 ***********************************************************************/
40 void
41 dumpHex(FILE *fp, unsigned char const *buf, int len)
42 {
43     int i;
44     int base;
45
46     if (!fp) return;
47
48     /* do NOT dump PAP packets */
49     if (len >= 2 && buf[0] == 0xC0 && buf[1] == 0x23) {
50         fprintf(fp, "(PAP Authentication Frame -- Contents not dumped)\n");
51         return;
52     }
53
54     for (base=0; base<len; base += 16) {
55         for (i=base; i<base+16; i++) {
56             if (i < len) {
57                 fprintf(fp, "%02x ", (unsigned) buf[i]);
58             } else {
59                 fprintf(fp, "   ");
60             }
61         }
62         fprintf(fp, "  ");
63         for (i=base; i<base+16; i++) {
64             if (i < len) {
65                 if (isprint(buf[i])) {
66                     fprintf(fp, "%c", buf[i]);
67                 } else {
68                     fprintf(fp, ".");
69                 }
70             } else {
71                 break;
72             }
73         }
74         fprintf(fp, "\n");
75     }
76 }
77
78 /**********************************************************************
79 *%FUNCTION: dumpPacket
80 *%ARGUMENTS:
81 * fp -- file to dump to
82 * packet -- a PPPoE packet
83 * dir -- either SENT or RCVD
84 *%RETURNS:
85 * Nothing
86 *%DESCRIPTION:
87 * Dumps the PPPoE packet to fp in an easy-to-read format
88 ***********************************************************************/
89 void
90 dumpPacket(FILE *fp, PPPoEPacket *packet, char const *dir)
91 {
92     int len = ntohs(packet->length);
93
94     /* Sheesh... printing times is a pain... */
95     struct timeval tv;
96     time_t now;
97     int millisec;
98     struct tm *lt;
99     char timebuf[256];
100
101     UINT16_t type = etherType(packet);
102     if (!fp) return;
103     gettimeofday(&tv, NULL);
104     now = (time_t) tv.tv_sec;
105     millisec = tv.tv_usec / 1000;
106     lt = localtime(&now);
107     strftime(timebuf, 256, "%H:%M:%S", lt);
108     fprintf(fp, "%s.%03d %s PPPoE ", timebuf, millisec, dir);
109     if (type == Eth_PPPOE_Discovery) {
110         fprintf(fp, "Discovery (%x) ", (unsigned) type);
111     } else if (type == Eth_PPPOE_Session) {
112         fprintf(fp, "Session (%x) ", (unsigned) type);
113     } else {
114         fprintf(fp, "Unknown (%x) ", (unsigned) type);
115     }
116
117     switch(packet->code) {
118     case CODE_PADI: fprintf(fp, "PADI "); break;
119     case CODE_PADO: fprintf(fp, "PADO "); break;
120     case CODE_PADR: fprintf(fp, "PADR "); break;
121     case CODE_PADS: fprintf(fp, "PADS "); break;
122     case CODE_PADT: fprintf(fp, "PADT "); break;
123     case CODE_PADM: fprintf(fp, "PADM "); break;
124     case CODE_PADN: fprintf(fp, "PADN "); break;
125     case CODE_SESS: fprintf(fp, "SESS "); break;
126     }
127
128     fprintf(fp, "sess-id %d length %d\n",
129             (int) ntohs(packet->session),
130             len);
131
132     /* Ugly... I apologize... */
133     fprintf(fp,
134             "SourceAddr %02x:%02x:%02x:%02x:%02x:%02x "
135             "DestAddr %02x:%02x:%02x:%02x:%02x:%02x\n",
136             (unsigned) packet->ethHdr.h_source[0],
137             (unsigned) packet->ethHdr.h_source[1],
138             (unsigned) packet->ethHdr.h_source[2],
139             (unsigned) packet->ethHdr.h_source[3],
140             (unsigned) packet->ethHdr.h_source[4],
141             (unsigned) packet->ethHdr.h_source[5],
142             (unsigned) packet->ethHdr.h_dest[0],
143             (unsigned) packet->ethHdr.h_dest[1],
144             (unsigned) packet->ethHdr.h_dest[2],
145             (unsigned) packet->ethHdr.h_dest[3],
146             (unsigned) packet->ethHdr.h_dest[4],
147             (unsigned) packet->ethHdr.h_dest[5]);
148     dumpHex(fp, packet->payload, ntohs(packet->length));
149 }