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
16static char const RCSID[] =
17"$Id: debug.c,v 1.1.1.1 2008/10/15 03:31:00 james26_jang Exp $";
18
19#include "pppoe.h"
20
21/**********************************************************************
22*%FUNCTION: dumpHex
23*%ARGUMENTS:
24* fp -- file to dump to
25* buf -- buffer to dump
26* len -- length of data
27*%RETURNS:
28* Nothing
29*%DESCRIPTION:
30* Dumps buffer to fp in an easy-to-read format
31***********************************************************************/
32void
33dumpHex(FILE *fp, unsigned char const *buf, int len)
34{
35    int i;
36
37    if (!fp) return;
38    for (i=0; i<len; i++) {
39	if (i == len-1 || !((i+1)%16)) {
40	    fprintf(fp, "%02x\n", (unsigned) buf[i]);
41	} else if (!((i+1)%8)) {
42	    fprintf(fp, "%02x-", (unsigned) buf[i]);
43	} else {
44	    fprintf(fp, "%02x ", (unsigned) buf[i]);
45	}
46    }
47}
48
49/**********************************************************************
50*%FUNCTION: dumpPacket
51*%ARGUMENTS:
52* fp -- file to dump to
53* packet -- a PPPoE packet
54*%RETURNS:
55* Nothing
56*%DESCRIPTION:
57* Dumps the PPPoE packet to fp in an easy-to-read format
58***********************************************************************/
59void
60dumpPacket(FILE *fp, struct PPPoEPacket *packet)
61{
62    int len = ntohs(packet->length);
63    UINT16_t type = etherType(packet);
64    if (!fp) return;
65    fprintf(fp, "PPPOE ");
66    if (type == Eth_PPPOE_Discovery) {
67	fprintf(fp, "Discovery (%x) ", (unsigned) type);
68    } else if (type == Eth_PPPOE_Session) {
69	fprintf(fp, "Session (%x) ", (unsigned) type);
70    } else {
71	fprintf(fp, "Unknown (%x) ", (unsigned) type);
72    }
73
74    switch(packet->code) {
75    case CODE_PADI: fprintf(fp, "PADI "); break;
76    case CODE_PADO: fprintf(fp, "PADO "); break;
77    case CODE_PADR: fprintf(fp, "PADR "); break;
78    case CODE_PADS: fprintf(fp, "PADS "); break;
79    case CODE_PADT: fprintf(fp, "PADT "); break;
80    case CODE_SESS: fprintf(fp, "SESS "); break;
81    }
82
83    fprintf(fp, "sess-id %d length %d\n",
84	    (int) ntohs(packet->session),
85	    len);
86
87    /* Ugly... I apologize... */
88    fprintf(fp,
89	    "SourceAddr %02x:%02x:%02x:%02x:%02x:%02x "
90	    "DestAddr %02x:%02x:%02x:%02x:%02x:%02x\n",
91	    (unsigned) packet->ethHdr.h_source[0],
92	    (unsigned) packet->ethHdr.h_source[1],
93	    (unsigned) packet->ethHdr.h_source[2],
94	    (unsigned) packet->ethHdr.h_source[3],
95	    (unsigned) packet->ethHdr.h_source[4],
96	    (unsigned) packet->ethHdr.h_source[5],
97	    (unsigned) packet->ethHdr.h_dest[0],
98	    (unsigned) packet->ethHdr.h_dest[1],
99	    (unsigned) packet->ethHdr.h_dest[2],
100	    (unsigned) packet->ethHdr.h_dest[3],
101	    (unsigned) packet->ethHdr.h_dest[4],
102	    (unsigned) packet->ethHdr.h_dest[5]);
103    dumpHex(fp, packet->payload, ntohs(packet->length));
104}
105