-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_collie.c
411 lines (332 loc) · 9.49 KB
/
mod_collie.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Copyright (c) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
* 2002, 2003, 2004
* Ohio University.
*
* ---
*
* Starting with the release of tcptrace version 6 in 2001, tcptrace
* is licensed under the GNU General Public License (GPL). We believe
* that, among the available licenses, the GPL will do the best job of
* allowing tcptrace to continue to be a valuable, freely-available
* and well-maintained tool for the networking community.
*
* Previous versions of tcptrace were released under a license that
* was much less restrictive with respect to how tcptrace could be
* used in commercial products. Because of this, I am willing to
* consider alternate license arrangements as allowed in Section 10 of
* the GNU GPL. Before I would consider licensing tcptrace under an
* alternate agreement with a particular individual or company,
* however, I would have to be convinced that such an alternative
* would be to the greater benefit of the networking community.
*
* ---
*
* This file is part of Tcptrace.
*
* Tcptrace was originally written and continues to be maintained by
* Shawn Ostermann with the help of a group of devoted students and
* users (see the file 'THANKS'). The work on tcptrace has been made
* possible over the years through the generous support of NASA GRC,
* the National Science Foundation, and Sun Microsystems.
*
* Tcptrace is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Tcptrace 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tcptrace (in the file 'COPYING'); if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Author: Shawn Ostermann
* School of Electrical Engineering and Computer Science
* Ohio University
* Athens, OH
* http://www.tcptrace.org/
*/
#include "tcptrace.h"
static char const GCC_UNUSED rcsid[] =
"$Header: /usr/local/cvs/tcptrace/mod_collie.c,v 5.7 2003/11/19 14:38:02 sdo Exp $";
#ifdef LOAD_MODULE_COLLIE
#include "mod_collie.h"
/* this module was written as a favor for a friend, but serves as a */
/* useful little example of a quick hack... :-) */
/* additional info kept per connection */
struct conn_info {
tcp_pair *ptp;
struct conn_info *next;
};
static struct conn_info *connhead = NULL;
/* additional info kept per UDP pair */
struct uconn_info {
udp_pair *pup;
struct uconn_info *next;
};
static struct uconn_info *uconnhead = NULL;
/* locally-global info */
static char *collie_filename = NULL;
static Bool print_labels = TRUE;
/* local routines */
static struct conn_info *MakeConnRec(void);
static struct uconn_info *MakeUDPConnRec(void);
static char *collie_name(ipaddr ipaddress);
static char *collie_dots(ipaddr ipaddress);
static char *collie_time(struct timeval *ptime);
static char *collie_date(time_t timestamp);
static void ParseArgs(char *argstring);
/* Set it up */
int
collie_init(
int argc,
char *argv[])
{
int i;
int enable=0;
char *args = NULL;
for (i=1; i < argc; ++i) {
if (!argv[i])
continue; /* argument already taken by another module... */
if (strncmp(argv[i],"-x",2) == 0) {
if (strncasecmp(argv[i]+2,"collie",sizeof("collie")-1) == 0) {
/* I want to be called */
args = argv[i]+(sizeof("-xcollie")-1);
enable = 1;
argv[i] = NULL;
}
}
}
if (!enable)
return(0); /* don't call me again */
/* parse any arguments for ME */
ParseArgs(args);
/* we don't want the normal output */
printsuppress = TRUE;
/* please also include UDP packets */
do_udp = TRUE;
return(1); /* TRUE means call collie_read and collie_done later */
}
static struct conn_info *
MakeConnRec(void)
{
struct conn_info *pci;
pci = MallocZ(sizeof(struct conn_info));
/* chain it in (at head of list) */
pci->next = connhead;
connhead = pci;
return(pci);
}
static struct uconn_info *
MakeUDPConnRec(void)
{
struct uconn_info *puci;
puci = MallocZ(sizeof(struct uconn_info));
/* chain it in (at head of list) */
puci->next = uconnhead;
uconnhead = puci;
return(puci);
}
#define LABEL(str)(print_labels?str:"")
#define DESCR(ptr)\
printf("\n"); \
printf("%s%s \n",\
LABEL("Session Start: "),\
collie_time(&(ptr)->first_time));\
printf("%s%s\n",\
LABEL("Session End: "),\
collie_time(&(ptr)->last_time));\
printf("%s%s\n",\
LABEL("Source IP address: "),\
collie_dots((ptr)->addr_pair.a_address));\
printf("%s%u\n",\
LABEL("Source Port: "),\
(unsigned)(ptr)->addr_pair.a_port);\
printf("%s%s\n",\
LABEL("Source Fully Qualified domain name: "),\
collie_name((ptr)->addr_pair.a_address));\
printf("%s%s\n",\
LABEL("Destination IP address: "),\
collie_dots((ptr)->addr_pair.b_address));\
printf("%s%u\n",\
LABEL("Destination Port: "),\
(unsigned)(ptr)->addr_pair.b_port);\
printf("%s%s\n",\
LABEL("Destination Fully Qualified domain name: "),\
collie_name((ptr)->addr_pair.b_address));\
printf("%s%" FS_ULL "\n",\
LABEL("Bytes Transferred Source to Destination: "),\
(ptr)->a2b.data_bytes);\
printf("%s%" FS_ULL "\n",\
LABEL("Bytes Transferred Destination to Source: "),\
(ptr)->b2a.data_bytes);\
printf("%s%" FS_ULL "\n",\
LABEL("Packets Transferred Source to Destination: "),\
(ptr)->a2b.packets);\
printf("%s%" FS_ULL "\n",\
LABEL("Packets Transferred Destination to Source: "),\
(ptr)->b2a.packets);
void
collie_done(void)
{
struct conn_info *pci;
struct uconn_info *upci;
struct stat statbuf;
/* check the input file timestamp */
if (stat(collie_filename,&statbuf) != 0) {
perror(collie_filename);
exit(-1);
}
/* print meta information */
printf("\n");
printf("%s%s\n",
LABEL("Source file: "),
collie_filename);
printf("%s%s\n",
LABEL("File modification timestamp: "),
collie_date(statbuf.st_mtime));
printf("%s%s\n",
LABEL("First packet: "),
collie_time(&first_packet));
printf("%s%s\n",
LABEL("Last packet: "),
collie_time(&last_packet));
/* print out the TCP connections */
if (print_labels)
printf("\nTCP Connections\n");
for (pci=connhead; pci; pci=pci->next) {
DESCR(pci->ptp)
}
/* print out the UDP connections */
if (print_labels)
printf("\nUDP Connections\n");
for (upci=uconnhead; upci; upci=upci->next) {
DESCR(upci->pup)
}
}
/* called for each new file */
void
collie_newfile(
char *newfile,
u_long filesize,
Bool fcompressed)
{
if (collie_filename == NULL)
collie_filename = strdup(newfile);
else {
fprintf(stderr,"\n\n\
Sorry, as the problem was defined, only a single file can be\n\
processed at a time\n");
exit(-1);
}
}
void
collie_usage(void)
{
printf("\t-xcollie\"[-ln]\tprovide connection summary\n");
printf("\t -l attach labels\n");
printf("\t -n no labels please\n");
}
void *
collie_newconn(
tcp_pair *ptp)
{
struct conn_info *pci;
pci = MakeConnRec();
pci->ptp = ptp;
return(pci);
}
void *
collie_newudpconn(
udp_pair *pup)
{
struct uconn_info *puci;
puci = MakeUDPConnRec();
puci->pup = pup;
return(puci);
}
/* return the IP address in IPv4 or IPv6 dotted representation */
static char *collie_dots(
ipaddr ipaddress)
{
char *pch;
int map = resolve_ipaddresses;
resolve_ipaddresses = 0;
pch = HostName(ipaddress);
resolve_ipaddresses = map;
return(pch);
}
/* convert the IP address to a name */
static char *collie_name(
ipaddr ipaddress)
{
char *pch;
int map = resolve_ipaddresses;
resolve_ipaddresses = 1;
pch = HostName(ipaddress);
resolve_ipaddresses = map;
return(pch);
}
/* return a date stamp that we like */
/* Unix format: "Fri Sep 13 00:00:00 1986\n" */
/* 1 2 */
/* 012345678901234567890123456 */
static char *
collie_date(
time_t timestamp)
{
struct tm *ptm;
char *now;
ptm = localtime(×tamp);
now = asctime(ptm);
/* nuke the newline */
now[24] = '\00';
return(now);
}
/* return a time stamp that we like */
/* Unix format: "Fri Sep 13 00:00:00 1986\n" */
/* 1 2 */
/* 012345678901234567890123456 */
static char *
collie_time(
struct timeval *ptime)
{
char *now;
now = ts2ascii(ptime);
return(now);
}
static void
ParseArgs(char *argstring)
{
int argc;
char **argv;
int i;
/* make sure there ARE arguments */
if (!(argstring && *argstring))
return;
/* break the string into normal arguments */
StringToArgv(argstring,&argc,&argv);
/* check the module args */
for (i=1; i < argc; ++i) {
if (debug > 1)
printf("Checking argv[%d]:%s\n", i, argv[i]);
if (strcmp(argv[i],"-d") == 0) {
debug = 1;
} else if (strcmp(argv[i],"-l") == 0) {
print_labels = TRUE;
} else if (strcmp(argv[i],"-n") == 0) {
print_labels = FALSE;
} else {
fprintf(stderr,"Collie module: bad argument '%s'\n",
argv[i]);
exit(-1);
}
}
}
#endif /* LOAD_MODULE_COLLIE */