forked from mct/tcptraceroute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
311 lines (252 loc) · 6.27 KB
/
util.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
/* -*- 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 <mct@toren.net>
*
* 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"
#include <stdarg.h>
/*
* fatal() and pfatal() are useful stdarg functions from
* namp. debug() and warn() are based on them.
*/
void fatal(char *fmt, ...)
{
va_list ap;
fflush(stdout);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1);
}
void debug(char *fmt, ...)
{
va_list ap;
if (! o_debug) return;
fflush(stdout);
fprintf(stderr, "debug: ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fflush(stderr);
}
void warn(char *fmt, ...)
{
va_list ap;
fflush(stdout);
fprintf(stderr, "Warning: ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fflush(stderr);
}
void pfatal(char *err)
{
debug("errno == %d\n", errno);
fflush(stdout);
perror(err);
exit(1);
}
void usage(void)
{
printf("\n%s %s\n%s\n", PACKAGE, VERSION, BANNER);
fatal("\
Usage: %s [-nNFSAE] [-i <interface>] [-f <first ttl>]\n\
[-l <packet length>] [-q <number of queries>] [-t <tos>]\n\
[-m <max ttl>] [-pP] <source port>] [-s <source address>]\n\
[-w <wait time>] <host> [destination port] [packet length]\n\n", name);
}
void about(void)
{
printf("\n%s %s\n%s\n", PACKAGE, VERSION, BANNER);
exit(0);
}
/*
* realloc(3) or bust!
*/
void *xrealloc(void *oldp, int size)
{
void *p;
if (!oldp)
p = malloc(size);
else
p = realloc(oldp, size);
if (!p)
fatal("Out of memory! Could not reallocate %d bytes!\n", size);
memset(p, 0, size);
return p;
}
/*
* Same as strncpy and snprintf, but always be sure the result is terminated.
*/
char *safe_strncpy(char *dst, char *src, int size)
{
dst[size-1] = '\0';
return strncpy(dst, src, size-1);
}
int safe_snprintf(char *s, int size, char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = vsnprintf(s, size, fmt, ap);
s[size-1] = '\0';
va_end(ap);
return ret;
}
/*
* return a pointer to a string containing only the
* printable characters of the string passed to it.
*/
char *sprintable(char *s)
{
static char buf[TEXTSIZE];
int i;
if (s && s[0])
safe_strncpy(buf, s, TEXTSIZE);
else
safe_strncpy(buf, "(empty)", TEXTSIZE);
for (i = 0; buf[i]; i++)
if (! isprint((u_char) buf[i]))
buf[i] = '?';
return buf;
}
/*
* isdigit() across an entire string.
*/
int isnumeric(char *s)
{
int i;
if (!s || !s[0])
return 0;
for (i = 0; s[i]; i++)
if (! isdigit((u_char) s[i]))
return 0;
return 1;
}
/*
* Compute the difference between two timeval structures.
*/
struct timeval tvdiff(struct timeval *tv1, struct timeval *tv2)
{
struct timeval tvdiff;
tvdiff.tv_sec = tv1->tv_sec - tv2->tv_sec;
tvdiff.tv_usec = tv1->tv_usec - tv2->tv_usec;
if ((tvdiff.tv_sec > 0) && (tvdiff.tv_usec < 0))
{
tvdiff.tv_usec += 1000000L;
tvdiff.tv_sec--;
}
else if ((tvdiff.tv_sec < 0) && (tvdiff.tv_usec > 0))
{
tvdiff.tv_usec -= 1000000L;
tvdiff.tv_sec++;
}
return tvdiff;
}
/*
* Is the timeval less than, equal to, or greater than zero?
*/
int tvsign(struct timeval *tv)
{
if (tv->tv_sec < 0) return -1;
if (tv->tv_sec == 0)
{
if (tv->tv_usec < 0) return -1;
if (tv->tv_usec == 0) return 0;
if (tv->tv_usec > 0) return 1;
}
if (tv->tv_sec > 0) return 1;
return -1;
}
/*
* Inspired by libnet_host_lookup(), but I needed more than 2 buffers while
* I was debugging. I really could get by with only 2 now, but *shrug*.
*/
char *iptos(u_long in)
{
static char output[IPTOSBUFFERS][IPTOSBUFSIZ];
static short which;
u_char *p;
p = (u_char *)∈
which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
safe_snprintf(output[which], IPTOSBUFSIZ, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
return output[which];
}
/*
* A wrapper for libnet_addr2name4(), with the option not to resolve
* RFC1918 space.
*/
char *iptohost(u_long in)
{
u_char *p = (u_char *)∈
if ((o_numeric > -1) &&
((p[0] == 10) ||
(p[0] == 192 && p[1] == 168) ||
(p[0] == 172 && p[1] >= 16 && p[1] <= 31)))
{
debug("Not attempting to resolve RFC1918 address %s\n", iptos(in));
return iptos(in);
}
return (char *)libnet_addr2name4(in,
o_numeric > 0 ? LIBNET_DONT_RESOLVE : LIBNET_RESOLVE);
}
/*
* Useful for debugging; dump #define's and command line options.
*/
void debugoptions(void)
{
if (! o_debug)
return;
debug("debugoptions():\n");
debug("%16s: %-2d %14s: %-2d %16s: %-2d\n",
"TEXTSIZE", TEXTSIZE,
"SNAPLEN", SNAPLEN,
"IPTOSBUFFERS", IPTOSBUFFERS);
debug("%16s: %-3d %15s: %-2d %16s: %-2d\n",
"ALLOCATEID_CACHE", ALLOCATEID_CACHE_SIZE,
"datalink", datalink,
"datalinkoffset", datalinkoffset(datalink));
debug("%16s: %-2d %16s: %-2d %16s: %-2d\n",
"o_minttl", o_minttl,
"o_maxttl", o_maxttl,
"o_timeout", o_timeout);
debug("%16s: %-2d %16s: %-2d %16s: %-2d\n",
"o_debug", o_debug,
"o_numeric", o_numeric,
"o_pktlen", o_pktlen);
debug("%16s: %-2d %16s: %-2d %16s: %-2d\n",
"o_nqueries", o_nqueries,
"o_dontfrag", o_dontfrag,
"o_tos", o_tos);
debug("%16s: %-2d %16s: %-2d %16s: %-2d\n",
"o_forceport", o_forceport,
"o_syn", o_syn,
"o_ack", o_ack);
debug("%16s: %-2d %16s: %d %16s: %-2d\n",
"o_ecn", o_ecn,
"o_nofilter", o_nofilter,
"o_nogetinterfaces", o_nogetinterfaces);
debug("%16s: %-2d %16s: %-12s %s: %s\n",
"o_trackport", o_trackport,
"datalinkname", datalinkname(datalink),
"device", device);
debug("%16s: %-2d %16s: %-2d %16s: %-10ld\n",
"o_noselect", o_noselect,
"o_dnat", o_dnat,
"isn", isn);
}