-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlanr.c
239 lines (200 loc) · 7.63 KB
/
nlanr.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
/*
* 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 copyright[] =
"@(#)Copyright (c) 2004 -- Ohio University.\n";
static char const GCC_UNUSED rcsid[] =
"@(#)$Header: /usr/local/cvs/tcptrace/nlanr.c,v 1.7 2003/11/19 14:38:04 sdo Exp $";
/*
* nlanr - TSH specific file reading stuff
*/
/* TSH header format:
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* 0 | timestamp (seconds) | Time
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* 1 | interface # | timestamp (microseconds) |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* 2 |Version| IHL |Type of Service| Total Length | IP
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* 3 | Identification |Flags| Fragment Offset |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* 4 | Time to Live | Protocol | Header Checksum |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* 5 | Source Address |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* 6 | Destination Address |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* 7 | Source Port | Destination Port | TCP
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* 8 | Sequence Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* 9 | Acknowledgment Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Data | |U|A|P|R|S|F| |
* 10 | Offset| Reserved |R|C|S|S|Y|I| Window |
* | | |G|K|H|T|N|N| |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
#ifdef GROK_NLANR
/* 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 */
/* information necessary to understand NLANL Tsh output */
#define TSH_DUMP_OFFSET 16
struct tsh_packet_header {
unsigned int ts_secs;
#ifdef _BIT_FIELDS_LTOH
unsigned int interface_id:8;
unsigned int ts_usecs:24;
#else
unsigned int ts_usecs:24;
unsigned int interface_id:8;
#endif
};
struct tsh_frame {
struct tsh_packet_header tph;
struct ip ip_header;
struct tcphdr tcp_header; /* just the first 16 bytes present */
};
/* static buffers for reading */
static struct ether_header *pep;
/* return the next packet header */
/* currently only works for ETHERNET */
static int
pread_nlanr(
struct timeval *ptime,
int *plen,
int *ptlen,
void **pphys,
int *pphystype,
struct ip **ppip,
void **pplast)
{
int rlen;
static struct tsh_frame hdr;
int packlen = sizeof(struct ip) + sizeof(struct tcphdr);
int hlen = 44;
/* read the next frames */
if ((rlen=fread(&hdr,1,hlen,SYS_STDIN)) != hlen) {
if (debug && (rlen != 0))
fprintf(stderr,"Bad tsh packet header (len:%d)\n", rlen);
return(0);
}
/* grab the time */
ptime->tv_sec = hdr.tph.ts_secs;
ptime->tv_usec = hdr.tph.ts_usecs;
/* truncated length is just an IP header and a TCP header */
*ptlen = packlen;
/* original length is from the IP header */
*plen = hdr.ip_header.ip_len;
/* Here's the IP/TCP stuff */
*ppip = &hdr.ip_header;
/* Here's the last byte of the packet */
*pplast = (char *)(*ppip)+packlen-1;
/* here's the (pseudo) ethernet header */
*pphys = pep;
*pphystype = PHYS_ETHER;
return(1);
}
/*
* is_nlanr() is the input file in tsh format??
*/
pread_f *is_nlanr(char *filename)
{
struct tsh_frame tf;
int rlen;
#ifdef __WIN32
if((fp = fopen(filename, "r")) == NULL) {
perror(filename);
exit(-1);
}
#endif /* __WIN32 */
/* tsh is a little hard because there's no magic number */
/* read the tsh file header */
if ((rlen=fread(&tf,1,sizeof(tf),SYS_STDIN)) != sizeof(tf)) {
/* not even a full frame */
rewind(SYS_STDIN);
return(NULL);
}
rewind(SYS_STDIN);
if (debug) {
printf("nlanr tsh ts_secs: %d\n", tf.tph.ts_secs);
printf("nlanr tsh ts_usecs: %d\n", tf.tph.ts_usecs);
printf("nlanr tsh interface: %d\n", tf.tph.interface_id);
printf("nlanr sizeof(tf): %d\n", sizeof(tf));
printf("nlanr sizeof(tph): %d\n", sizeof(tf.tph));
if (debug > 1)
PrintRawDataHex("NLANR TSH header",&tf,(char *)&tf+39);
}
/* quick heuristics */
if (((tf.ip_header.ip_v != 4) && (tf.ip_header.ip_v != 6))
) {
return(NULL);
}
/* OK, let's hope it's a tsh file */
/* there's no physical header present, so make up one */
pep = MallocZ(sizeof(struct ether_header));
pep->ether_type = htons(ETHERTYPE_IP);
if (debug)
fprintf(stderr,"TSH format, interface ID %d\n", tf.tph.interface_id);
return(pread_nlanr);
}
#endif /* GROK_NLANR */