-
Notifications
You must be signed in to change notification settings - Fork 0
/
traceroute_tcp_icmp.c
306 lines (266 loc) · 9.08 KB
/
traceroute_tcp_icmp.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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/ip_icmp.h>
#include <stdbool.h>
#include <getopt.h>
#include <time.h>
#define PORT_NUMBER 12564
#define TIMEOUT 10 // seconds
#define MAX_TTL 30
#define RECV_BUF_LEN 10000
#define DEF_TRIES 1
char *dest;
bool check_dst = false;
int timeout_ = TIMEOUT;
int max_ttl = MAX_TTL;
int start_ttl = 1;
// int packet_size
int port = PORT_NUMBER;
int tries = DEF_TRIES;
void usage(char *app);
bool parse_argv(int argc, char *argv[]);
const static struct option long_options[] = {
{"help", no_argument, 0, 0x1},
{"destination-host", required_argument, 0, 'd'},
{"timeout", required_argument, 0, 't'},
{"max-ttl", required_argument, 0, 'm'},
{"start-ttl", required_argument, 0, 's'},
{"port-number", required_argument, 0, 'p'},
{"tries-number", required_argument, 0, 'a'},
{0, 0, 0, 0}};
int main(int argc, char *argv[])
{
if (!parse_argv(argc, argv))
{
usage(argv[0]);
return 1;
}
// Get current IP
struct sockaddr_in *src_addr;
struct ifaddrs *id, *id_tmp;
int getAddress = getifaddrs(&id);
if (getAddress == -1)
{
perror("Unable to retrieve IP address of interface");
exit(-1);
}
id_tmp = id;
while (id_tmp)
{
if ((id_tmp->ifa_addr) && (id_tmp->ifa_addr->sa_family == AF_INET))
{
src_addr = (struct sockaddr_in *)id_tmp->ifa_addr;
}
id_tmp = id_tmp->ifa_next;
}
printf("GATEWAY<%s>\n", inet_ntoa(src_addr->sin_addr));
// Resolve destination IP
struct sockaddr_in dest_addr;
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(port);
int is_ip_addr = inet_pton(AF_INET, dest, &(dest_addr.sin_addr));
if (!is_ip_addr)
{
struct hostent *host;
host = gethostbyname(dest);
if (host == NULL)
{
perror("failed DNS resolution!");
exit(-1);
}
dest_addr.sin_addr = *((struct in_addr *)host->h_addr);
// printf("DESTINATION<%s> : ip<%s>\n", trace_dest, inet_ntoa(dest_addr.sin_addr));
}
// create socket to send tcp messages
int sendSocket = socket(PF_INET, SOCK_STREAM, 0);
if (sendSocket < 0)
{
perror("failed to create tcp socket");
exit(-1);
}
// create socket to receive icmp messages
int recvSocket = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
if (recvSocket < 0)
{
perror("failed to create icmp socket");
exit(-1);
}
// timeout
struct timeval timeout;
timeout.tv_sec = timeout_;
int setTimeoutOptTcp = setsockopt(sendSocket, SOL_SOCKET, SO_SNDTIMEO, (struct timeval *)&timeout, sizeof(struct timeval));
if (setTimeoutOptTcp < 0)
{
perror("failed to set socket timeout (tcp)");
exit(-1);
}
int setTimeoutOptIcmp = setsockopt(recvSocket, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&timeout, sizeof(struct timeval));
if (setTimeoutOptIcmp < 0)
{
perror("failed to set socket timeout (icmp)");
exit(-1);
}
// receive buffer
char recvBuffer[RECV_BUF_LEN];
struct sockaddr_in cli_addr;
socklen_t cli_len = sizeof(struct sockaddr_in);
long numBytesReceived;
printf("******************************************************\n");
// printf("0: %s <gateway>\n", inet_ntoa(src_addr->sin_addr));
struct timespec time_start, time_end;
long double rtt = 0;
int i = 1;
while (i < max_ttl)
{
for (int k = 0; k < tries; k++) {
int icmpErrorReceived = 0;
// set TTL in IP header
setsockopt(sendSocket, IPPROTO_IP, IP_TTL, &i, sizeof(i));
clock_gettime(CLOCK_MONOTONIC, &time_start);
// send SYN packet (start 3-way handshake)
errno = 0;
connect(sendSocket, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
// calculate RTT
clock_gettime(CLOCK_MONOTONIC, &time_end);
double timeElapsed = ((double)(time_end.tv_nsec - time_start.tv_nsec)) / 1000000.0;
rtt = (time_end.tv_sec - time_start.tv_sec) * 1000.0 + timeElapsed;
// TTL expired
if (errno == EHOSTUNREACH)
{
while (!icmpErrorReceived)
{
numBytesReceived = recvfrom(recvSocket, recvBuffer, RECV_BUF_LEN, 0, (struct sockaddr *)&cli_addr, &cli_len);
// extract IP header
struct ip *ip_hdr = (struct ip *)recvBuffer;
// extract ICMP header
int ipHeaderLength = 4 * ip_hdr->ip_hl;
struct icmp *icmp_hdr = (struct icmp *)((char *)ip_hdr + ipHeaderLength);
int icmpMessageType = icmp_hdr->icmp_type;
int icmpMessageCode = icmp_hdr->icmp_code;
// TTL exceeded
if (icmpMessageType == ICMP_TIME_EXCEEDED && icmpMessageCode == ICMP_NET_UNREACH)
{
// check if ICMP messages are related to TCP SYN packets
struct ip *inner_ip_hdr = (struct ip *)((char *)icmp_hdr + ICMP_MINLEN);
if (inner_ip_hdr->ip_p == IPPROTO_TCP)
{
icmpErrorReceived = 1;
}
}
// port unreachable
else if (icmpMessageType == ICMP_DEST_UNREACH && icmpMessageCode == ICMP_PORT_UNREACH)
{
printf("%d: %s in %Lfms after %d tries [complete]\n", i, inet_ntoa(dest_addr.sin_addr), rtt, k + 1);
printf("******************************************************\n");
exit(0);
}
}
if (i >= start_ttl){
printf("%d: %s in %Lfms after %d tries \n", i, inet_ntoa(cli_addr.sin_addr), rtt, k + 1);
break;
}
// timeout
}
else if (
errno == ETIMEDOUT // socket timeout
|| errno == EINPROGRESS // operation in progress
|| errno == EALREADY // consecutive timeouts
)
{
if (i >= start_ttl && k == tries - 1)
printf("%d: * * * * * [timeout]\n", i);
}
// destination reached
else if (errno == ECONNRESET || errno == ECONNREFUSED)
{
printf("%d: %s in %Lfms after %d tries [complete]\n", i, inet_ntoa(dest_addr.sin_addr), rtt, k + 1);
printf("******************************************************\n");
exit(0);
}
else
{
printf("Unknown error: %d sending SYN packet\n", errno);
exit(-1);
}
}
i++;
}
printf("Unable to reach host within TTL of %d\n", max_ttl);
printf("******************************************************\n");
return -1;
}
bool parse_argv(int argc, char *argv[])
{
if (argc == 1)
return 0;
int c;
int opt_index = 0;
while (c != -1)
{
c = getopt_long(argc, argv, "s:t:m:p:d:a:", long_options, &opt_index);
if (c == -1)
break;
switch (c)
{
case 0x1:
return false;
break;
// destination host
case 'd':
dest = optarg;
printf("destination : %s\n", dest);
check_dst = true;
break;
// max ttl
case 'm':
max_ttl = atoi(optarg);
printf("max ttl : %d\n", max_ttl);
break;
// timeout
case 't':
timeout_ = atoi(optarg);
printf("timeout : %d\n", timeout_);
break;
// start ttl
case 's' :
start_ttl = atoi(optarg);
printf("start ttl : %d\n", start_ttl);
break;
// port number
case 'p' :
port = atoi(optarg);
printf("port: %d\n", port);
break;
// tries number
case 'a' :
tries = atoi(optarg);
printf("tries number : %d\n", tries);
break;
default:
printf("\n");
return false;
break;
}
}
return check_dst;
}
void usage(char *app)
{
printf("Usage: sudo %s -d <host> [options]\n", app);
printf(" Options:\n");
printf(" -m|--max-ttl <num>|Max ttl (default: %d))\n", MAX_TTL);
printf(" -t|--timeout <num>|Timeout is sec (default: %d)\n", TIMEOUT);
printf(" -s|--start-ttl <num>|Start ttl (default: 1)\n");
printf(" -p|--port-number <num>|Port number (default: %d)\n", PORT_NUMBER);
printf(" -a|--tries-number <num>|Tries number (default: %d)\n", DEF_TRIES);
}