forked from blitz/tcptrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetm.c
277 lines (230 loc) · 6.88 KB
/
netm.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
/*
* 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
* ostermann@cs.ohiou.edu
* http://www.tcptrace.org/
*/
#include "tcptrace.h"
static char const GCC_UNUSED copyright[] =
"@(#)Copyright (c) 2004 -- Ohio University.\n";
static char const GCC_UNUSED rcsid[] =
"@(#)$Header$";
/*
* netm.c - NetMetrix specific file reading stuff
*/
#ifdef GROK_NETM
#define NETM_DUMP_OFFSET 0x1000
/* Defining SYS_STDIN which is fp for Windows and stdin for all other systems */
#ifdef __WIN32
static FILE *fp;
#define SYS_STDIN fp
#else
#define SYS_STDIN stdin
#endif /* __WIN32 */
/* netm file header format */
struct netm_header {
int netm_key;
int version;
};
#define NETM_VERSION_OLD 3
#define NETM_VERSION_NEW 4
#define NETM_KEY 0x6476
/* netm packet header format */
struct netm_packet_header_old {
int unused1;
int unused2;
int tstamp_secs;
int tstamp_usecs;
int tlen;
int len;
};
struct netm_packet_header {
int unused1;
int tstamp_secs;
int tstamp_usecs;
int unused2;
int unused3;
int len;
int tlen; /* truncated length */
int unused5;
};
/* netm packet header format */
int netm_oldversion;
/* static buffers for reading */
static struct ether_header *pep;
static int *pip_buf;
/* currently only works for ETHERNET */
static int
pread_netm(
struct timeval *ptime,
int *plen,
int *ptlen,
void **pphys,
int *pphystype,
struct ip **ppip,
void **pplast)
{
int packlen;
int rlen;
struct netm_packet_header hdr;
int len;
int hlen;
while (1) {
hlen = netm_oldversion?
(sizeof(struct netm_packet_header_old)):
(sizeof(struct netm_packet_header));
/* read the netm packet header */
if ((rlen=fread(&hdr,1,hlen,SYS_STDIN)) != hlen) {
if (rlen != 0)
fprintf(stderr,"Bad netm header\n");
return(0);
}
packlen = ntohl(hdr.tlen);
/* round up to multiple of 4 bytes */
len = (packlen + 3) & ~0x3;
/* read the ethernet header */
rlen=fread(pep,1,sizeof(struct ether_header),SYS_STDIN);
if (rlen != sizeof(struct ether_header)) {
fprintf(stderr,"Couldn't read ether header\n");
return(0);
}
/* read the rest of the packet */
len -= sizeof(struct ether_header);
if (len >= IP_MAXPACKET) {
/* sanity check */
fprintf(stderr,
"pread_netm: invalid next packet, IP len is %d, return EOF\n", len);
return(0);
}
if ((rlen=fread(pip_buf,1,len,SYS_STDIN)) != len) {
if (rlen != 0)
if (debug)
fprintf(stderr,
"Couldn't read %d more bytes, skipping last packet\n",
len);
return(0);
}
if (netm_oldversion) {
void *ptr;
struct netm_packet_header_old *pho;
ptr=&hdr;
pho = (struct netm_packet_header_old *) ptr;
ptime->tv_sec = ntohl(pho->tstamp_secs);
ptime->tv_usec = ntohl(pho->tstamp_usecs);
*plen = ntohl(pho->len);
*ptlen = ntohl(pho->tlen);
} else {
ptime->tv_sec = ntohl(hdr.tstamp_secs);
ptime->tv_usec = ntohl(hdr.tstamp_usecs);
*plen = ntohl(hdr.len);
*ptlen = ntohl(hdr.tlen);
}
*ppip = (struct ip *) pip_buf;
*pplast = (char *)pip_buf+packlen-sizeof(struct ether_header)-1; /* last byte in the IP packet */
*pphys = pep;
*pphystype = PHYS_ETHER;
/* if it's not IP, then skip it */
if ((ntohs(pep->ether_type) != ETHERTYPE_IP) &&
(ntohs(pep->ether_type) != ETHERTYPE_IPV6)) {
if (debug > 2)
fprintf(stderr,"pread_netm: not an IP packet\n");
continue;
}
return(1);
}
}
/* is the input file a NetMetrix format file?? */
pread_f *is_netm(char *filename)
{
struct netm_header nhdr;
int rlen;
#ifdef __WIN32
if((fp = fopen(filename, "r")) == NULL) {
perror(filename);
exit(-1);
}
#endif /* __WIN32 */
/* read the netm file header */
if ((rlen=fread(&nhdr,1,sizeof(nhdr),SYS_STDIN)) != sizeof(nhdr)) {
rewind(SYS_STDIN);
return(NULL);
}
rewind(SYS_STDIN);
/* convert to local byte order */
nhdr.netm_key = ntohl(nhdr.netm_key);
nhdr.version = ntohl(nhdr.version);
/* check for NETM */
if (nhdr.netm_key != NETM_KEY) {
return(NULL);
}
/* check version */
if (nhdr.version == NETM_VERSION_OLD)
netm_oldversion = 1;
else if (nhdr.version == NETM_VERSION_NEW)
netm_oldversion = 0;
else {
fprintf(stderr,"Bad NETM file header version: %d\n",
nhdr.version);
return(NULL);
}
if (debug)
printf("NETM file version: %d\n", nhdr.version);
/* ignore the header at the top */
if (fseek(SYS_STDIN,NETM_DUMP_OFFSET,SEEK_SET) == -1) {
perror("NETM lseek");
exit(-1);
}
/* OK, it's mine. Init some stuff */
pep = MallocZ(sizeof(struct ether_header));
pip_buf = MallocZ(IP_MAXPACKET);
return(pread_netm);
}
#endif /* GROK_NETM */