tcpsnooper/debug.c

354 lines
8.3 KiB
C
Raw Permalink Normal View History

2019-01-15 04:06:45 +00:00
/*=================================================
*
* debug.c - Code to dump Marinetti internals for TCP Snooper CDA
*
* Copyright (C) 2004-2006 Kelvin Sherlock
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*=================================================
*
* 2006.12.02 KS - Initial release as open source
*
*=================================================*/
#pragma noroot
#pragma lint -1
#pragma optimize -1
#pragma debug 0x8000
#include <gsos.h>
#include <tcpip.h>
#include <tcpipx.h>
#include <memory.h>
#include <intmath.h>
#include <loader.h>
#include <misctool.h>
#include <stdio.h>
2019-01-15 04:06:45 +00:00
char *errStrings[] =
{
"tcpDGMSTBLEN",
"tcpDGMSTOTAL",
"tcpDGMSFRAGSIN",
"tcpDGMSFRAGSLOST",
"tcpDGMSBUILT",
"tcpDGMSOK",
"tcpDGMSBADCHK",
"tcpDGMSBADHEADLEN",
"tcpDGMSBADPROTO",
"tcpDGMSBADIP",
"tcpDGMSICMP",
"tcpDGMSICMPUSER",
"tcpDGMSICMPKERNEL",
"tcpDGMSICMPBAD",
"tcpDGMSICMPBADTYPE",
"tcpDGMSICMPBADCODE",
"tcpDGMSICMPECHORQ",
"tcpDGMSICMPECHORQOUT",
"tcpDGMSICMPECHORP",
"tcpDGMSICMPECHORPBADID",
"tcpDGMSUDP",
"tcpDGMSUDPBAD",
"tcpDGMSUDPNOPORT",
"tcpDGMSTCP",
"tcpDGMSTCPBAD",
"tcpDGMSTCPNOPORT",
"tcpDGMSTCPQUEUED",
"tcpDGMSTCPOLD",
"tcpDGMSOFRAGMENTS"
"tcpDGMSFRAGMENTED"
};
char *tuneStrings[] =
{
"tcpTUNECOUNT",
"tcpTUNEIPUSERPOLLCT",
"tcpTUNEIPRUNQFREQ",
"tcpTUNEIPRUNQCT",
"tcpTUNETCPUSERPOLL"
};
// creates a unique file and opens it.
FILE *createFile(void)
2019-01-15 04:06:45 +00:00
{
static GSString32 template = {28, "*:system:tcpip:debugxxxx.txt"};
static CreateRecGS createDCB = {4, (GSString255Ptr)&template, 0xe3, 4, 0};
FILE *fp;
2019-01-15 04:06:45 +00:00
word i;
for (i = 0; i < 9999; i++)
{
Int2Dec(i, &template.text[20], 4, 0);
template.text[20] |= 0x10; // convert ' ' (0x20) to '0' (0x30)
template.text[21] |= 0x10;
template.text[22] |= 0x10;
template.text[23] |= 0x10;
CreateGS(&createDCB);
if (_toolErr == dupPathname) continue;
break;
}
if (_toolErr) return NULL;
2019-01-15 04:06:45 +00:00
fp = fopen(template.text, "w");
return fp;
2019-01-15 04:06:45 +00:00
}
void dump(FILE *fp, void *data, Word length)
2019-01-15 04:06:45 +00:00
{
Word i;
Word j;
Word k;
static char buffer[80];
for (i = 0, j = 0; i < length; i++)
{
k = ((char *)data)[i];
buffer[j++] = "0123456789abcdef"[k >> 4];
buffer[j++] = "0123456789abcdef"[k & 0x0f];
buffer[j++] = ' ';
if ((i & 0x0f) == 0x07) buffer[j++] = ' ';
if ((i & 0x0f) == 0x0f)
{
buffer[j++] = 0;
fprintf(fp, "%04x: %s\r", i - 15, buffer);
2019-01-15 04:06:45 +00:00
j = 0;
}
}
if (i & 0x0f)
{
buffer[j++] = 0;
fprintf(fp, "%04x: %s\r", i - 15, buffer);
2019-01-15 04:06:45 +00:00
}
}
void debug(void)
{
Word i;
Long l;
LongWord *lw;
variablesPtr lv;
static Word tune[5];
static linkInfoBlk link;
static char buffer[80];
static DNSRec dns;
static udpVars udp;
static srBuff tcp;
Word count;
Handle h;
Word size;
FILE *fp;
2019-01-15 04:06:45 +00:00
fp = createFile();
if (!fp) return;
2019-01-15 04:06:45 +00:00
// version
VersionString(0, TCPIPLongVersion(), buffer);
fprintf(fp, "Version: %b\r", buffer);
2019-01-15 04:06:45 +00:00
// link layer
TCPIPGetLinkLayer(&link);
fprintf(fp, "Link Layer:\r");
fprintf(fp, " MethodID: $%04x\r", link.liMethodID);
fprintf(fp, " Name: %b\r", link.liName);
2019-01-15 04:06:45 +00:00
VersionString(0, link.liVersion, buffer);
fprintf(fp, " Version: %b\r", buffer);
fprintf(fp, " Flags: $%04x\r", link.liFlags);
2019-01-15 04:06:45 +00:00
lv = TCPIPGetLinkVariables();
fprintf(fp, "Link Variables\r");
fprintf(fp, " Version: %d\r", lv->lvVersion);
fprintf(fp, " Connected: $%04x\r", lv->lvConnected);
2019-01-15 04:06:45 +00:00
TCPIPConvertIPToASCII(lv->lvIPaddress, buffer, 0);
fprintf(fp, " IP Address: %b\r", buffer);
fprintf(fp, " RefCon: $%08lx\r", lv->lvRefCon);
fprintf(fp, " Errors: $%08lx\r", lv->lvErrors);
fprintf(fp, " MTU: %d\r", lv->lvMTU);
2019-01-15 04:06:45 +00:00
// connect status
fprintf(fp, "Connect Status: $%04x\r", TCPIPGetConnectStatus());
2019-01-15 04:06:45 +00:00
// ip address
l = TCPIPGetMyIPAddress();
TCPIPConvertIPToASCII(l, buffer, 0);
fprintf(fp, "IP Address: %b\r", buffer);
2019-01-15 04:06:45 +00:00
// dns
TCPIPGetDNS(&dns);
TCPIPConvertIPToASCII(dns.DNSMain, buffer, 0);
fprintf(fp, "DNS 1: %b\r", buffer);
2019-01-15 04:06:45 +00:00
TCPIPConvertIPToASCII(dns.DNSAux, buffer, 0);
fprintf(fp, "DNS 2: %b\r", buffer);
2019-01-15 04:06:45 +00:00
// hostname
TCPIPGetHostName((hnBuffPtr)buffer);
fprintf(fp, "Hostname: %b\r", buffer);
2019-01-15 04:06:45 +00:00
//mtu
fprintf(fp, "MTU: %d\r", TCPIPGetMTU());
fprintf(fp, "Alive Flag: $%04x\r", TCPIPGetAliveFlag());
fprintf(fp, "Alive Minutes: %d\r", TCPIPGetAliveMinutes());
fprintf(fp, "Login Count: %d\r", TCPIPGetLoginCount());
2019-01-15 04:06:45 +00:00
// error table
fprintf(fp, "\rError Table\r");
2019-01-15 04:06:45 +00:00
lw = (LongWord *)TCPIPGetErrorTable();
count = lw[0] >> 2;
if (count > sizeof(errStrings) / 4) count = sizeof(errStrings) / 4;
for (i = 0; i < count; i++);
{
fprintf(fp, " %s -- $%08lx\r", errStrings[i], lw[i]);
2019-01-15 04:06:45 +00:00
}
// tuning table
tune[0] = 10;
fprintf(fp, "\rTuning Table\r");
2019-01-15 04:06:45 +00:00
TCPIPGetTuningTable((tunePtr)tune);
count = tune[0] >> 1;
if (count > sizeof(tuneStrings) / 4) count = sizeof(tuneStrings) / 4;
for (i = 0; i < count; i++)
{
fprintf(fp, " %s -- $%04x\r", tuneStrings[i], tune[i]);
2019-01-15 04:06:45 +00:00
}
fprintf(fp, "\rDirect Page\r");
dump(fp, (void *)TCPIPGetDP(), 0x0100);
2019-01-15 04:06:45 +00:00
// dump all user records. ipid will be even numbers in the range 0..98
for (i = 0; i < 100; i += 2)
{
h = (Handle)TCPIPGetUserRecord(i);
if (h == NULL) continue;
size = (Word)GetHandleSize(h);
if (_toolErr) continue;
fprintf(fp, "\rIpid %d\r", i);
2019-01-15 04:06:45 +00:00
fprintf(fp, "Datagram count (all): %d\r",
2019-01-15 04:06:45 +00:00
TCPIPGetDatagramCount(i, protocolAll));
fprintf(fp, "Datagram count (icmp): %d\r",
2019-01-15 04:06:45 +00:00
TCPIPGetDatagramCount(i, protocolICMP));
fprintf(fp, "Datagram count (tcp): %d\r",
2019-01-15 04:06:45 +00:00
TCPIPGetDatagramCount(i, protocolTCP));
fprintf(fp, "Datagram count (udp): %d\r",
2019-01-15 04:06:45 +00:00
TCPIPGetDatagramCount(i, protocolUDP));
fprintf(fp, "User statistic 1: $%08lx\r",
2019-01-15 04:06:45 +00:00
TCPIPGetUserStatistic(i, 1));
fprintf(fp, "User statistic 2: $%08lx\r",
2019-01-15 04:06:45 +00:00
TCPIPGetUserStatistic(i, 2));
#if 0 // tcpipStatusUDP has a stack imbalance bug.
TCPIPStatusUDP(i, &udp);
if (_toolErr == 0)
{
fprintf(fp, "\rStatus UDP\r");
fprintf(fp, " Queue Size: %d\r", udp.uvQueueSize);
fprintf(fp, " Error: %d\r", udp.uvError);
fprintf(fp, " Error Tick: %ld\r", udp.uvErrorTick);
fprintf(fp, " Count: %ld\r", udp.uvCount);
fprintf(fp, " Total Count: %ld\r", udp.uvTotalCount);
fprintf(fp, " Dispatch Flag: %d\r", udp.uvDispatchFlag);
2019-01-15 04:06:45 +00:00
}
#endif
TCPIPStatusTCP(i, &tcp);
if (_toolErr == 0)
{
fprintf(fp, "\rStatus TCP\r");
fprintf(fp, " State: %d\r", tcp.srState);
fprintf(fp, " Network Error: %d\r", tcp.srNetworkError);
fprintf(fp, " Send Queued: %ld\r", tcp.srSndQueued);
fprintf(fp, " Recv Queued: %ld\r", tcp.srRcvQueued);
2019-01-15 04:06:45 +00:00
TCPIPConvertIPToASCII(tcp.srDestIP, buffer, 0);
fprintf(fp, " Dest IP: %b\r", buffer);
fprintf(fp, " Dest Port: %d\r", tcp.srDestPort);
fprintf(fp, " Connect Type: %d\r", tcp.srConnectType);
fprintf(fp, " Accept Count: %d\r", tcp.srAcceptCount);
2019-01-15 04:06:45 +00:00
}
HLock(h);
if (1) // (size == sizeof(userRecord))
{
GSString255Ptr appName;
userRecord *rec = (userRecord *)*h;
fprintf(fp, "\rUser Record\r");
2019-01-15 04:06:45 +00:00
appName = (GSString255Ptr)LGetPathname2(rec->uwUserID, 1);
if (_toolErr == 0)
{
fprintf(fp, "\rApplication %.*s\r", appName->length, appName->text);
2019-01-15 04:06:45 +00:00
}
#include "ur.c"
}
else dump(fp, *h, size);
2019-01-15 04:06:45 +00:00
HUnlock(h);
}
fclose(fp);
2019-01-15 04:06:45 +00:00
}
#if 0
void _beginStackCheck (void);
int _endStackCheck (void);
void main (void)
{
_beginStackCheck();
debug();
fprintf(fp "%d bytes of stack used\r", _endStackCheck());
2019-01-15 04:06:45 +00:00
}
#endif