-
Notifications
You must be signed in to change notification settings - Fork 24
/
probe.c
409 lines (333 loc) · 10.5 KB
/
probe.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
/* -*- Mode: c; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4; -*- */
/* vim:set ts=4 sw=4 ai nobackup nocindent sm: */
/*
* tcptraceroute -- A traceroute implementation using TCP packets
* Copyright (c) 2001-2015 Michael C. Toren <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, version 2, as published
* by the Free Software Foundation.
*
* This program 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.
*
* A copy of the GNU GPL is available as /usr/doc/copyright/GPL on Debian
* systems, or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html
* You can also obtain it by writing to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include "tcptraceroute.h"
#if (LIBNET_API_VERSION < 110)
int sockfd;
#else
libnet_t *libnet_context;
#endif
/*
* Initialize the libnet library context.
*/
void initlibnet(void)
{
#if (LIBNET_API_VERSION >= 110)
libnet_context = libnet_init(LIBNET_RAW4, NULL, errbuf);
if (libnet_context == NULL)
fatal("libnet_init() failed: %s\n", errbuf);
#else
/* Nothing to do for libnet-1.0 */
#endif
return;
}
/*
* Allocates memory for a new proberecord structure.
*/
proberecord *newproberecord(void)
{
proberecord *record;
record = xrealloc(NULL, sizeof(proberecord));
record->state = xrealloc(NULL, TEXTSIZE);
record->string = xrealloc(NULL, TEXTSIZE);
return record;
}
/*
* Destroys a proberecord structure, carefully, as not to leak memory.
*/
void freeproberecord(proberecord *record)
{
if (record->string)
free(record->string);
if (record->state)
free(record->state);
free(record);
}
/*
* Request a local unused TCP port from the kernel using bind(2)
*/
u_short allocateport(u_short requested)
{
struct sockaddr_in in;
int s;
unsigned int insize;
if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
pfatal("socket error");
insize = sizeof(in);
memset(&in, 0, insize);
in.sin_family = AF_INET;
in.sin_port = htons(requested);
if ((bind(s, (struct sockaddr *)&in, insize)) < 0)
return 0;
if ((getsockname(s, (struct sockaddr *)&in, &insize)) < 0)
pfatal("getsockname");
close(s);
return ntohs(in.sin_port);
}
/*
* Allocate an IP ID from our pool of unallocated ID's. A cache is kept of
* the last ALLOCATEID_CACHE_SIZE allocations, so we can check for duplicates.
*/
u_short allocateid(void)
{
static u_short ids[ALLOCATEID_CACHE_SIZE];
static int n;
int i, j;
if ((n = n % ALLOCATEID_CACHE_SIZE) == 0)
{
debug("Generating a new batch of %d IP ID's\n", ALLOCATEID_CACHE_SIZE);
for(i = 0; i < ALLOCATEID_CACHE_SIZE; i++)
{
for(ids[i] = libnet_get_prand(LIBNET_PRu16), j = i + 1; j < ALLOCATEID_CACHE_SIZE + i; j++)
if (ids[i] == ids[j % ALLOCATEID_CACHE_SIZE])
ids[i] = libnet_get_prand(LIBNET_PRu16), j = i + 1;
}
}
return ids[n++];
}
/*
* Sends out a TCP SYN packet with the specified TTL, and returns a
* proberecord structure describing the packet sent, so we know what
* to listen for later. A new IP ID is generated for each probe, and
* a new source port if o_trackport is specified.
*/
void probe(proberecord *record, int ttl, int q)
{
static u_char *payload;
int i, size, ret;
#if (LIBNET_API_VERSION < 110)
static u_char *buf;
#else
static libnet_ptag_t ip_tag, tcp_tag, data_tag;
#endif
size = LIBNET_IPV4_H + LIBNET_TCP_H + o_pktlen;
#if (LIBNET_API_VERSION < 110)
if (!buf)
{
debug("Initializing packet buffer of %d bytes\n", size);
buf = xrealloc(buf, size);
}
else
memset(buf, 0, size);
#endif
/* Initialize the packet payload */
if (o_pktlen && !payload)
{
debug("Initializing payload of %d bytes\n", o_pktlen);
payload = xrealloc(payload, o_pktlen);
for(i = 0; i < o_pktlen; i++)
payload[i] = i % ('~' - '!') + '!';
debug("Payload: %s\n", sprintable((char *)payload));
}
/* Set some values of the probe record */
record->q = q;
record->ttl = ttl;
record->addr = INADDR_ANY;
record->dnat_ip = INADDR_ANY;
record->src_prt = src_prt;
record->id = allocateid();
record->delta = 0;
if (o_trackport)
{
record->src_prt = allocateport(0);
if (record->src_prt == 0)
pfatal("Could not allocate local port: bind");
}
if (gettimeofday(&(record->timestamp), NULL) < 0)
pfatal("gettimeofday");
/* Build the packet, and send it off into the cold, cruel world */
#if (LIBNET_API_VERSION < 110)
libnet_build_ip(
LIBNET_TCP_H+o_pktlen, /* len */
o_tos, /* tos */
record->id, /* id */
o_dontfrag ? IP_DF : 0, /* frag */
ttl, /* ttl */
IPPROTO_TCP, /* proto */
src_ip, /* saddr */
dst_ip, /* daddr */
NULL, /* data */
0, /* datasize? */
buf); /* buffer */
libnet_build_tcp(
record->src_prt, /* source port */
dst_prt, /* dest port */
isn, /* seq number */
0, /* ack number */
(o_syn ? TH_SYN : 0) |
(o_ack ? TH_ACK : 0) |
(o_ecn ? TH_CWR|TH_ECN : 0), /* control */
0, /* window */
0, /* urgent? */
payload, /* data */
o_pktlen, /* datasize */
buf + LIBNET_IPV4_H); /* buffer */
libnet_do_checksum(buf, IPPROTO_TCP, LIBNET_TCP_H + o_pktlen);
/* Write */
if ((ret = libnet_write_ip(sockfd, buf, size)) < size)
fatal("libnet_write_ip failed? Attempted to write %d bytes, only wrote %d\n",
size, ret);
#else
/* Add the payload */
data_tag = libnet_build_data(payload, o_pktlen, libnet_context, data_tag);
if (data_tag < 0)
fatal("Can't add payload: %s\n", libnet_geterror(libnet_context));
/* Add the TCP header */
tcp_tag = libnet_build_tcp(
record->src_prt, /* source port */
dst_prt, /* dest port */
isn, /* seq number */
0, /* ack number */
(o_syn ? TH_SYN : 0) |
(o_ack ? TH_ACK : 0) |
(o_ecn ? TH_CWR|TH_ECN : 0), /* control */
0, /* window */
0, /* checksum TBD */
0, /* urgent? */
LIBNET_TCP_H + o_pktlen, /* TCP PDU size */
NULL, /* data */
0, /* datasize */
libnet_context, /* libnet context */
tcp_tag); /* libnet protocol tag */
if (tcp_tag < 0)
fatal("Can't build TCP header: %s\n", libnet_geterror(libnet_context));
/* Add the IP header */
ip_tag = libnet_build_ipv4(
size, /* total packet len */
o_tos, /* tos */
record->id, /* id */
o_dontfrag ? IP_DF : 0, /* frag */
ttl, /* ttl */
IPPROTO_TCP, /* proto */
0, /* checksum TBD */
src_ip, /* saddr */
dst_ip, /* daddr */
NULL, /* data */
0, /* datasize? */
libnet_context, /* libnet context */
ip_tag); /* libnet protocol tag */
if (ip_tag < 0)
fatal("Can't build IP header: %s\n", libnet_geterror(libnet_context));
/* Write */
if ((ret = libnet_write(libnet_context)) < size)
fatal("libnet_write failed? Attempted to write %d bytes, only wrote %d\n",
size, ret);
#endif
}
/*
* A mess of a function, but it works. The aim is to be as compatible as
* possible with traceroute(8), with the one exception that if for the same hop
* we receive a response from two different hosts, display the second host on a
* new line, as Cisco does. This drastically improves readability when tracing
* through links which have per-packet, round-robin load balancing.
*/
void showprobe(proberecord *record)
{
/* Variables to keep state between calls */
static char laststate[TEXTSIZE];
static int lastttl;
static u_long lastaddr, lastdnat_ip;
static u_short lastdnat_dport;
static int everprinthost; // have we ever printed the hostname?
int printhost = 0; // should we print the hostname this time?
/* kludge to make debug mode usable */
if (o_debug)
{
fflush(stdout);
fprintf(stderr, "debug: displayed hop\n");
fflush(stderr);
}
/* print the DNAT line */
if ((lastdnat_ip != record->dnat_ip && record->dnat_ip != INADDR_ANY)
|| (lastdnat_dport != record->dnat_dport && record->dnat_dport != 0))
{
/* If lastttl != record->ttl, we're already on a newline */
if (lastttl == record->ttl)
printf("\n");
printf(" Detected DNAT to %s", iptos(record->dnat_ip));
if (record->dnat_dport)
printf(":%d", ntohs(record->dnat_dport));
printf("\n");
/* Only print the leading four spaces if this is not the start of a new hop */
if (lastttl == record->ttl)
printf(" ");
lastdnat_ip = record->dnat_ip;
lastdnat_dport = record->dnat_dport;
printhost = 1;
}
/* ttl */
if (lastttl != record->ttl)
{
printf("%2d ", record->ttl);
printhost = 1;
everprinthost = 0;
safe_strncpy(laststate, "", TEXTSIZE);
}
else if (lastaddr != record->addr && record->addr != INADDR_ANY && lastaddr != INADDR_ANY)
{
printf("\n ");
printhost = 1;
}
/* host */
if ((printhost || !everprinthost) && record->addr != INADDR_ANY)
{
char buf[TEXTSIZE];
if (record->q > 1 && lastaddr == INADDR_ANY)
printf(" ");
printf("%s", iptohost(record->addr));
safe_strncpy(buf, iptohost(record->addr), TEXTSIZE);
if (strncmp(buf, iptos(record->addr), IPTOSBUFSIZ) != 0)
printf(" (%s)", iptos(record->addr));
everprinthost = 1;
}
/* tcp state */
if ( ((record->ttl != lastttl) && *(record->state)) ||
((record->ttl == lastttl) && *(record->state) && (strncmp(laststate, record->state, TEXTSIZE) != 0)))
{
printf(" [%s]", record->state);
}
/* space before ms */
if (! (record->addr == INADDR_ANY && record->q == 1))
{
/* if timeout, only print one space. otherwise, two */
if ((record->addr == INADDR_ANY) || (lastaddr == INADDR_ANY && record->q > 1))
printf(" ");
else
printf(" ");
}
if (record->addr == INADDR_ANY)
safe_strncpy(record->string, "*", TEXTSIZE);
if (! record->string)
fatal("something bad happened\n");
printf(record->string, record->delta);
/* If this will be the last probe, print the newline */
if (record->q == o_nqueries)
printf("\n");
lastttl = record->ttl;
lastaddr = record->addr;
if (*(record->state))
safe_strncpy(laststate, record->state, TEXTSIZE);
/* kludge to make debug mode usable */
if (o_debug)
fprintf(stdout, "\n");
if (o_debug && record->q != o_nqueries)
fprintf(stdout, "\n");
fflush(stdout);
}