pktdump.c 7.12 KB
Newer Older
1
2
3
4
5
/* pktdump.c */

/* $Id$ */

#include "fidodefs.h"
deuce's avatar
deuce committed
6
#include "xpendian.h"	/* swap */
7
8
#include "dirwrap.h"	/* _PATH_DEVNULL */
#include <stdio.h>
9
#include <stdbool.h>
10
#include <string.h>
11
12
13
14
15
16
17

FILE* nulfp;
FILE* bodyfp;

/****************************************************************************/
/* Returns an ASCII string for FidoNet address 'addr'                       */
/****************************************************************************/
18
char *faddrtoa(struct fidoaddr* addr, char* outstr)
19
20
21
22
23
24
{
	static char str[64];

	if(addr==NULL)
		return("0:0/0");
	sprintf(str,"%hu:%hu/%hu",addr->zone,addr->net,addr->node);
25
26
27
28
	if(addr->point)
		sprintf(str + strlen(str), ".%hu", addr->point);
	if(addr->domain[0])
		sprintf(str + strlen(str), "@%s", addr->domain);
29
30
31
32
33
34
	if(outstr==NULL)
		return(str);
	strcpy(outstr,str);
	return(outstr);
}

35
bool freadstr(FILE* fp, char* str, size_t maxlen)
36
37
38
39
{
	int		ch;
	size_t	len=0;

40
	memset(str, 0, maxlen);
41
42
43
44
45
46
	while((ch=fgetc(fp))!=EOF && len<maxlen) {
		str[len++]=ch;
		if(ch==0)
			break;
	}

47
	return str[maxlen-1] == 0;
48
49
}

50
int pktdump(FILE* fp, const char* fname, FILE* out)
51
{
52
	int			ch,lastch=0;
53
54
55
56
57
	char		buf[128];
	char		to[FIDO_NAME_LEN];
	char		from[FIDO_NAME_LEN];
	char		subj[FIDO_SUBJ_LEN];
	long		offset;
58
59
	struct fidoaddr	orig = {0};
	struct fidoaddr	dest = {0};
60
61
62
	fpkthdr_t	pkthdr;
	fpkdmsg_t	pkdmsg;

63
	if(fread(&pkthdr,sizeof(pkthdr),1,fp) != 1) {
64
		fprintf(stderr,"%s !Error reading pkthdr (%" XP_PRIsize_t "u bytes)\n"
65
66
67
68
69
70
71
72
73
74
75
76
			,fname,sizeof(pkthdr));
		return(-1);
	}

	fseek(fp,-2L,SEEK_END);
	fread(buf,sizeof(BYTE),sizeof(buf),fp);
	if(memcmp(buf,"\x00\x00",2)) {
		fprintf(stderr,"%s !Packet missing terminating nulls: %02X %02X\n"
			,fname,buf[0],buf[1]);
//		return(-2);
	}

77
78
79
80
81
82
83
84
85
86
	printf("%s Packet Type ", fname);

	if(pkthdr.type2.pkttype != 2) {
		fprintf(stderr, "%u (unsupported packet type)\n", pkthdr.type2.pkttype);
		return -3;
	}

	orig.zone=pkthdr.type2.origzone;
	orig.net=pkthdr.type2.orignet;
	orig.node=pkthdr.type2.orignode;
87
88
	orig.point=0;

89
90
91
	dest.zone=pkthdr.type2.destzone;
	dest.net=pkthdr.type2.destnet;
	dest.node=pkthdr.type2.destnode;
92
93
	dest.point=0;				/* No point info in the 2.0 hdr! */

94
95
	if(pkthdr.type2plus.cword==BYTE_SWAP_16(pkthdr.type2plus.cwcopy)  /* 2+ Packet Header */
		&& pkthdr.type2plus.cword&1) {
96
97
98
		fprintf(stdout,"2+ (prod: %02X%02X, rev: %u.%u)"
			,pkthdr.type2plus.prodcodeHi	,pkthdr.type2plus.prodcodeLo
			,pkthdr.type2plus.prodrevMajor	,pkthdr.type2plus.prodrevMinor);
99
100
101
102
		dest.point=pkthdr.type2plus.destpoint;
		if(pkthdr.type2plus.origpoint!=0 && orig.net==0xffff) {	/* see FSC-0048 for details */
			orig.net=pkthdr.type2plus.auxnet;
			orig.point=pkthdr.type2plus.origpoint;
103
		}
104
		if(pkthdr.type2plus.origzone != orig.zone)
105
			printf("!Warning: origination zone mismatch in type 2+ packet header (%u != %u)\n"
106
107
				,pkthdr.type2plus.origzone, orig.zone);
		if(pkthdr.type2plus.destzone != dest.zone)
108
			printf("!Warning: destination zone mismatch in type 2+ packet header (%u != %u)\n"
109
				,pkthdr.type2plus.destzone, dest.zone);
110
	} else if(pkthdr.type2_2.subversion==2) {					/* Type 2.2 Packet Header (FSC-45) */
111
		fprintf(stdout,"2.2 (prod: %02X, rev: %u)", pkthdr.type2_2.prodcode, pkthdr.type2_2.prodrev);
112
		dest.point=pkthdr.type2_2.destpoint; 
113
114
		memcpy(orig.domain, pkthdr.type2_2.origdomn, sizeof(pkthdr.type2_2.origdomn));
		memcpy(dest.domain, pkthdr.type2_2.destdomn, sizeof(pkthdr.type2_2.destdomn));
115
	} else
116
		fprintf(stdout,"2.0 (prod: %02X, serial: %u)", pkthdr.type2.prodcode, pkthdr.type2.sernum);
117

118
119
	printf(" from %s", faddrtoa(&orig,NULL));
	printf(" to %s\n", faddrtoa(&dest,NULL));
120

121
	if(pkthdr.type2.password[0])
122
		fprintf(stdout,"Password: '%.*s'\n",(int)sizeof(pkthdr.type2.password),pkthdr.type2.password);
123

124
125
126
	if(out != NULL)
		fwrite(&pkthdr, sizeof(pkthdr), 1, out);

127
128
129
130
131
	fseek(fp,sizeof(pkthdr),SEEK_SET);

	/* Read/Display packed messages */
	while(!feof(fp)) {

132
		offset=ftell(fp);
133

134
		/* Read fixed-length header fields (or final NULL byte) */
135
136
		if(fread(&pkdmsg,sizeof(BYTE),sizeof(pkdmsg),fp)!=sizeof(pkdmsg))
			break;
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

		/* Read variable-length header fields */
		freadstr(fp,to,sizeof(to));
		freadstr(fp,from,sizeof(from));
		freadstr(fp,subj,sizeof(subj));
		if(pkdmsg.type != 2
			|| to[sizeof(to) - 1] != '\0'
			|| from[sizeof(from) - 1] != '\0'
			|| subj[sizeof(subj) - 1] != '\0'
			|| from[0] == '\0'
			|| pkdmsg.time[0] == '\0'
			|| pkdmsg.time[sizeof(pkdmsg.time) - 1] != '\0'
			) {
			printf("%s %06lX Corrupted Message Header\n"
				,fname
				,offset);
			while((ch = fgetc(fp)) != EOF && ch != 0)
				;
			continue;
		}

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
		/* Display fixed-length fields */
		printf("%s %06lX Packed Message Type: %d from %u/%u to %u/%u\n"
			,fname
			,offset
			,pkdmsg.type
			,pkdmsg.orignet, pkdmsg.orignode
			,pkdmsg.destnet, pkdmsg.destnode);
		printf("Attribute: %04X\n",pkdmsg.attr);
		printf("Date/Time: %s\n",pkdmsg.time);
	
		/* Display variable-length fields */
		printf("%-4s : %s\n","To",to);
		printf("%-4s : %s\n","From",from);
		printf("%-4s : %s\n","Subj",subj);

173
174
175
176
177
178
179
		if(out != NULL) {
			fwrite(&pkdmsg, sizeof(pkdmsg), 1, out);
			fwrite(to, strlen(to) + 1, 1, out);
			fwrite(from, strlen(from) + 1, 1, out);
			fwrite(subj, strlen(subj) + 1, 1, out);
		}

180
181
182
183
184
185
		fprintf(bodyfp,"\n-start of message text-\n");

		while((ch=fgetc(fp))!=EOF && ch!=0) {
			if(lastch=='\r' && ch!='\n')
				fputc('\n',bodyfp);
			fputc(lastch=ch,bodyfp);
186
187
			if(out != NULL)
				fputc(ch, out);
188
		}
189
190
		if(out != NULL)
			fputc('\0', out);
191
192
193

		fprintf(bodyfp,"\n-end of message text-\n");
	}
194
195
196
197
	if(out != NULL) { // Final terminating NULL bytes
		fputc('\0', out);
		fputc('\0', out);
	}
198
199
200
201

	return(0);
}

202
char* usage = "usage: pktdump [-body] [-recover] <file1.pkt> [file2.pkt] [...]\n";
203

204
205
206
int main(int argc, char** argv)
{
	FILE*	fp;
207
	bool	recover = false;
208
	int		i;
209
210
211
212
213
214
215
216
217
218
219
220
	char	revision[16];

	sscanf("$Revision$", "%*s %s", revision);

	fprintf(stderr,"pktdump rev %s - Dump FidoNet Packets\n\n"
		,revision
		);

	if(argc<2) {
		fprintf(stderr,"%s",usage);
		return -1;
	}
221
222
223
224
225
226
227
228

	if((nulfp=fopen(_PATH_DEVNULL,"w+"))==NULL) {
		perror(_PATH_DEVNULL);
		return -1;
	}
	bodyfp=nulfp;

	if(sizeof(fpkthdr_t)!=FIDO_PACKET_HDR_LEN) {
229
		printf("sizeof(fpkthdr_t)=%" XP_PRIsize_t "u, expected: %d\n",sizeof(fpkthdr_t),FIDO_PACKET_HDR_LEN);
230
231
232
		return(-1);
	}
	if(sizeof(fpkdmsg_t)!=FIDO_PACKED_MSG_HDR_LEN) {
233
		printf("sizeof(fpkdmsg_t)=%" XP_PRIsize_t "u, expected: %d\n",sizeof(fpkdmsg_t),FIDO_PACKED_MSG_HDR_LEN);
234
235
236
		return(-1);
	}
	if(sizeof(fmsghdr_t)!=FIDO_STORED_MSG_HDR_LEN) {
237
		printf("sizeof(fmsghdr_t)=%" XP_PRIsize_t "u, expected: %d\n",sizeof(fmsghdr_t),FIDO_STORED_MSG_HDR_LEN);
238
239
240
241
242
243
244
		return(-1);
	}

	for(i=1;i<argc;i++) {
		if(argv[i][0]=='-') {
			switch(tolower(argv[i][1])) {
				case 'b':
245
246
247
248
					bodyfp=stdout;
					break;
				case 'r':
					recover=true;
249
250
					break;
				default:
251
					printf("%s",usage);
252
253
254
255
256
257
258
259
260
					return(0);
			}
			continue;
		}
		fprintf(stdout,"Opening %s\n",argv[i]);
		if((fp=fopen(argv[i],"rb"))==NULL) {
			perror(argv[i]);
			continue;
		}
261
262
263
264
265
266
267
268
269
270
		FILE* out = NULL;
		if(recover) {
			char fname[MAX_PATH + 1];
			SAFEPRINTF(fname, "%s.recovered", argv[i]);
			if((out = fopen(fname, "wb")) == NULL) {
				perror(argv[i]);
				return EXIT_FAILURE;
			}
		}
		pktdump(fp, argv[i], out);
271
		fclose(fp);
272
273
274
275
		if(out != NULL) {
			fclose(out);
			out = NULL;
		}
276
277
278
	}

	return(0);
deuce's avatar
deuce committed
279
}