File: dump.c

package info (click to toggle)
tcpstat 1.5-8
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 628 kB
  • ctags: 372
  • sloc: ansic: 2,281; sh: 439; makefile: 30
file content (131 lines) | stat: -rw-r--r-- 3,924 bytes parent folder | download | duplicates (5)
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
/*
 * Copyright (c) 2000 Paul Herman
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: dump.c,v 1.14 2002/09/18 21:00:29 pherman Exp $
 */

/**********************************************************
 * Example of how to use the library included
 * with this program
 **********************************************************/
#include "packetdump.h"

/* GLOBALS */
char filterexpr[BUF_SIZ]	= "";
int	get_tcp_flags		= 0;
int	what_to_show		= 0;
int	packet_number		= 0;

void my_hook(packet_data *pd, void **args) {
	printf("PACKET NUMBER: %d\n", ++packet_number);
	print_packet(pd, what_to_show);
	printf("-------------------\n");
}

/*
 * process_file() takes the output of tcpdump, saves packets, and displays
 * statistics
 */
void process_file(char *fname, u_int unused) {
        get_dump_data(fname, filterexpr, get_tcp_flags,
		-1.0, my_hook, NULL);
}

int parse_show_types(char *in) {
	int ret = 0;
	while (*in) {
		if (*in == 'b' || *in == 'a' ) ret |= PP_SHOW_BASICINFO;
		if (*in == 'l' || *in == 'a' ) ret |= PP_SHOW_LINKLAYER;
		if (*in == 'i' || *in == 'a' ) ret |= PP_SHOW_IPHEADER;
		if (*in == 'c' || *in == 'a' ) ret |= PP_SHOW_PACKETCONTENT; 
		if (*in == 't' || *in == 'a' )
			ret |=    PP_SHOW_TCPHEADER
				| PP_SHOW_UDPHEADER
				| PP_SHOW_ICMPHEADER;
		in++;
	}
	return ret;
}

#define USAGE 							\
"Usage: %s [-?hls] [-f filter expr] [-r file] [-s letters]		\
\n	-?, -h 		- display this help				\
\n	-f filter expr 	- filter expression to pass to tcpdump		\
\n	-l		- include link layer in packet size calc	\
\n	-r file 	- file to read data from			\
\n	-s letters	- what to show from packet, where letters is any of: \
\n		a		- everything				\
\n		b		- basic info				\
\n		l		- link info				\
\n		i		- IP info				\
\n		t		- TCP/UDP/ICMP info			\
\n		c		- packet content			\
\n"

int Usage(int r, char *prog) {
	fprintf(stderr, USAGE, my_basename(prog)); return r;
}

void error(char *s) {
	fprintf(stderr, "error: %s\n", s);
	Usage(-1, "packetdump"); exit(-1);
}

int main(int argc, char **argv) {
	char filename[BUF_SIZ];
	int ch;

	*filename = 0;
	while ( (ch = getopt(argc, argv, "h?lf:r:s:")) != -1) {
		switch(ch) {
			case 'h':
			case '?':
				return Usage(1, argv[0]);
				break;
			case 'l':
				get_tcp_flags |= GET_TCPD_COUNT_LINKSIZE;
				break;
			case 'f':
				strncpy(filterexpr, optarg, BUF_SIZ);
				break;
			case 'r':
				strncpy(filename, optarg, BUF_SIZ);
				break;
			case 's':
				what_to_show |= parse_show_types(optarg);
				break;
			default:
				return Usage(1, argv[0]);
				break;
			}				
		}

	if (*filename == 0) error("must specify filename");

	process_file(filename, 0);

	return 0;
}