-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpcap-tcpdump.c
More file actions
167 lines (150 loc) · 3.88 KB
/
pcap-tcpdump.c
File metadata and controls
167 lines (150 loc) · 3.88 KB
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
/*
* Only supported under Linux at this point!
*/
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include OSHEADER
#include "sniff.h"
#include "if.h"
#include "pcap-tcpdump.h"
/*
* Local variables.
*/
static int byteswapped = 0;
static int iface;
static int llinkhdr_len = sizeof (ETHhdr);
/*
* Misc. local macros.
*/
#define FIXSHORT(X) (byteswapped ? ntohs (X) : (X))
#define FIXLONG(X) (byteswapped ? ntohl (X) : (X))
/*
* Invisible global shit.
*/
extern char if_name[];
/*
* Local function prototypes.
*/
static void if_read_ip_pcap_pipe (void (*filter) (UCHAR *, int));
/*
* Signal handler.
*/
void
if_close_pcap (int sig)
{
close (iface);
fprintf (stderr, "\nClosed input file \"%s\"\n", if_name);
if (sig) {
exit (0);
}
}
/*
* For reading from a 'tcpdump -w' file.
*/
void
if_open_pcap (int nolocal) /* "nolocal" is meaningless here. */
{
struct pcap_file_header hdr;
if (!strcmp (if_name, "-")) {
iface = 0; /* stdin */
} else if ((iface = open (if_name, O_RDONLY)) < 0) {
perror ("open");
exit (errno);
}
if ((read (iface, &hdr, sizeof (struct pcap_file_header))) !=
sizeof (struct pcap_file_header)) {
int err = errno;
perror ("read (pcap_file_header)");
if_close_pcap (0);
exit (err);
}
if (hdr.magic != TCPDUMP_MAGIC) {
if (ntohl (hdr.magic) != TCPDUMP_MAGIC) {
fputs ("Unknown/bad dump file format.\n", stderr);
if_close_pcap (0);
exit (-1);
}
fputs ("Note: dump file is byteswapped.\n", stderr);
byteswapped = 1;
}
if ((FIXSHORT (hdr.version_major) != PCAP_VERSION_MAJOR) ||
(FIXSHORT (hdr.version_minor) != PCAP_VERSION_MINOR)) {
fprintf (stderr, "Wrong dump file version format: %d.%d\n",
FIXSHORT (hdr.version_major), FIXSHORT (hdr.version_minor));
fprintf (stderr, "(Only version %d.%d is currently supported.)\n",
PCAP_VERSION_MAJOR, PCAP_VERSION_MINOR);
if_close_pcap (0);
exit (-1);
}
/* Should make IF_BUFSIZ dynamic.... */
if (FIXLONG (hdr.snaplen) > IF_BUFSIZ) {
fprintf (stderr, "File snapshot length %ld too long for IF_BUFSIZ of %d.\n",
FIXLONG (hdr.snaplen), IF_BUFSIZ);
if_close_pcap (0);
exit (-1);
}
fprintf (stderr, "Version %s reading from file \"%s\"\n\n", IS_VERSION,
if_name);
}
/*
* Read from a 'tcpdump -w' file. Still needs some work.
* (Reading from stdin is a mess, for instance!)
*/
void
if_read_ip_pcap (void (*filter) (UCHAR *, int))
{
int bytes;
UCHAR buf[IF_BUFSIZ];
struct pcap_pkthdr hdr;
/* Should set if_read to point to this.... */
if (!iface) {
if_read_ip_pcap_pipe (filter);
}
for (;;) {
if ((bytes = read (iface, &hdr, sizeof (struct pcap_pkthdr))) !=
sizeof (struct pcap_pkthdr)) {
if (bytes == 0) {
fprintf (stderr,
"\n** End of file...dumping what's left in memory.\n\n");
raise (SIGINT); /* FIX ME! */
}
perror ("read (pcap_pkthdr)");
return;
}
if ((bytes = read (iface, &buf, FIXLONG (hdr.caplen))) !=
FIXLONG (hdr.caplen)) {
fprintf (stderr, "%d/%ld--", bytes, FIXLONG (hdr.caplen));
perror ("read (packet)");
return;
}
if (ETHTYPE ((ETHhdr *)buf) == IPTYPE) {
filter (&buf[llinkhdr_len], bytes - llinkhdr_len);
}
}
}
/*
* Read from 'tcpdump -w -' (stdin).
*/
void
if_read_ip_pcap_pipe (void (*filter) (UCHAR *, int))
{
int bytes;
UCHAR buf[IF_BUFSIZ];
struct pcap_pkthdr hdr;
for (;;) {
for (bytes = 0; bytes < sizeof (struct pcap_pkthdr);
bytes += read (iface, &buf[bytes],
sizeof (struct pcap_pkthdr) - bytes));
memcpy (&hdr, buf, sizeof (struct pcap_pkthdr));
for (bytes = 0; bytes < FIXLONG (hdr.caplen);
bytes += read (iface, &buf[bytes], FIXLONG (hdr.caplen) - bytes));
if (ETHTYPE ((ETHhdr *)buf) == IPTYPE) {
filter (&buf[llinkhdr_len], bytes - llinkhdr_len);
}
}
}