18ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project/***********************************************************************
28ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*
38ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* debug.c
48ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*
58ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* Implementation of user-space PPPoE redirector for Linux.
68ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*
78ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* Functions for printing debugging information
88ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*
98ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* Copyright (C) 2000 by Roaring Penguin Software Inc.
108ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*
118ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* This program may be distributed according to the terms of the GNU
128ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* General Public License, version 2 or (at your option) any later version.
138ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*
148ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project***********************************************************************/
158ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project
168ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Projectstatic char const RCSID[] =
178ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project"$Id: debug.c,v 1.1 2001/12/14 02:55:20 mostrows Exp $";
188ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project
198ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project#include "pppoe.h"
208ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project#include <sys/time.h>
218ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project#include <time.h>
228ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project#include <unistd.h>
238ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project#include <ctype.h>
248ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project
258ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project/**********************************************************************
268ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*%FUNCTION: dumpHex
278ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*%ARGUMENTS:
288ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* fp -- file to dump to
298ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* buf -- buffer to dump
308ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* len -- length of data
318ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*%RETURNS:
328ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* Nothing
338ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*%DESCRIPTION:
348ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* Dumps buffer to fp in an easy-to-read format
358ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project***********************************************************************/
368ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Projectvoid
378ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source ProjectdumpHex(FILE *fp, unsigned char const *buf, int len)
388ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project{
398ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    int i;
408ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    int base;
418ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project
428ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    if (!fp) return;
438ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project
448ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    /* do NOT dump PAP packets */
458ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    if (len >= 2 && buf[0] == 0xC0 && buf[1] == 0x23) {
468ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	fprintf(fp, "(PAP Authentication Frame -- Contents not dumped)\n");
478ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	return;
488ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    }
498ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project
508ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    for (base=0; base<len; base += 16) {
518ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	for (i=base; i<base+16; i++) {
528ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    if (i < len) {
538ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project		fprintf(fp, "%02x ", (unsigned) buf[i]);
548ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    } else {
558ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project		fprintf(fp, "   ");
568ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    }
578ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	}
588ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	fprintf(fp, "  ");
598ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	for (i=base; i<base+16; i++) {
608ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    if (i < len) {
618ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project		if (isprint(buf[i])) {
628ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project		    fprintf(fp, "%c", buf[i]);
638ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project		} else {
648ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project		    fprintf(fp, ".");
658ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project		}
668ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    } else {
678ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project		break;
688ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    }
698ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	}
708ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	fprintf(fp, "\n");
718ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    }
728ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project}
738ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project
748ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project/**********************************************************************
758ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*%FUNCTION: dumpPacket
768ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*%ARGUMENTS:
778ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* fp -- file to dump to
788ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* packet -- a PPPoE packet
798ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* dir -- either SENT or RCVD
808ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*%RETURNS:
818ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* Nothing
828ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project*%DESCRIPTION:
838ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project* Dumps the PPPoE packet to fp in an easy-to-read format
848ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project***********************************************************************/
858ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Projectvoid
868ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source ProjectdumpPacket(FILE *fp, PPPoEPacket *packet, char const *dir)
878ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project{
888ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    int len = ntohs(packet->length);
898ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project
908ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    /* Sheesh... printing times is a pain... */
918ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    struct timeval tv;
928ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    time_t now;
938ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    int millisec;
948ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    struct tm *lt;
958ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    char timebuf[256];
968ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project
978ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    UINT16_t type = etherType(packet);
988ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    if (!fp) return;
998ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    gettimeofday(&tv, NULL);
1008ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    now = (time_t) tv.tv_sec;
1018ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    millisec = tv.tv_usec / 1000;
1028ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    lt = localtime(&now);
1038ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    strftime(timebuf, 256, "%H:%M:%S", lt);
1048ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    fprintf(fp, "%s.%03d %s PPPoE ", timebuf, millisec, dir);
1058ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    if (type == Eth_PPPOE_Discovery) {
1068ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	fprintf(fp, "Discovery (%x) ", (unsigned) type);
1078ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    } else if (type == Eth_PPPOE_Session) {
1088ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	fprintf(fp, "Session (%x) ", (unsigned) type);
1098ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    } else {
1108ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	fprintf(fp, "Unknown (%x) ", (unsigned) type);
1118ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    }
1128ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project
1138ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    switch(packet->code) {
1148ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    case CODE_PADI: fprintf(fp, "PADI "); break;
1158ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    case CODE_PADO: fprintf(fp, "PADO "); break;
1168ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    case CODE_PADR: fprintf(fp, "PADR "); break;
1178ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    case CODE_PADS: fprintf(fp, "PADS "); break;
1188ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    case CODE_PADT: fprintf(fp, "PADT "); break;
1198ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    case CODE_SESS: fprintf(fp, "SESS "); break;
1208ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    }
1218ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project
1228ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    fprintf(fp, "sess-id %d length %d\n",
1238ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    (int) ntohs(packet->session),
1248ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    len);
1258ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project
1268ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    /* Ugly... I apologize... */
1278ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    fprintf(fp,
1288ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    "SourceAddr %02x:%02x:%02x:%02x:%02x:%02x "
1298ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    "DestAddr %02x:%02x:%02x:%02x:%02x:%02x\n",
1308ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    (unsigned) packet->ethHdr.h_source[0],
1318ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    (unsigned) packet->ethHdr.h_source[1],
1328ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    (unsigned) packet->ethHdr.h_source[2],
1338ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    (unsigned) packet->ethHdr.h_source[3],
1348ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    (unsigned) packet->ethHdr.h_source[4],
1358ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    (unsigned) packet->ethHdr.h_source[5],
1368ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    (unsigned) packet->ethHdr.h_dest[0],
1378ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    (unsigned) packet->ethHdr.h_dest[1],
1388ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    (unsigned) packet->ethHdr.h_dest[2],
1398ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    (unsigned) packet->ethHdr.h_dest[3],
1408ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    (unsigned) packet->ethHdr.h_dest[4],
1418ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project	    (unsigned) packet->ethHdr.h_dest[5]);
1428ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project    dumpHex(fp, packet->payload, ntohs(packet->length));
1438ad0dd2a5c5f23cd210aedba72a43e48026e7436The Android Open Source Project}
144