-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_trace.c
More file actions
325 lines (270 loc) · 8.34 KB
/
process_trace.c
File metadata and controls
325 lines (270 loc) · 8.34 KB
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
#include "traceroute.h"
#include <unistd.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <stdbool.h>
#include <asm-generic/errno.h>
#include <errno.h>
#include <netinet/ip_icmp.h>
#include <sys/time.h>
#include "traceroute.h"
#include <arpa/inet.h>
#include <linux/errqueue.h>
#include <linux/udp.h>
/*
* Prototypes
*
*/
static void trcrt_send();
static void initialize_socket();
static void destroy_socket();
static void trcrt_receive();
static void trcrt_handle_icmp();
static void trcrt_handle_udp();
static void trcrt_print_result();
static __suseconds_t time_diff(struct timeval* begin, struct timeval *end);
int send_icmp_msg_v4(int sock, uint8_t tos, uint16_t id, uint8_t ttl, uint8_t icmp_type,
uint16_t icmp_seq_num, size_t payload_size, in_addr_t source_ip,
in_addr_t dest_ip);
int send_udp_trcrt_msg(int udp_sock, uint8_t tos, uint8_t ttl, size_t payload_size,
in_addr_t dest_ip, in_port_t dest_port);
int get_name_by_ipaddr(in_addr_t ip, char *host,
size_t host_len, bool *in_cache);
/*
* Main logic
*
*/
void process_trace() {
if (g_tcrt_ctx.current_ttl > g_tcrt_ctx.max_ttl) {
fprintf(stderr, "first hop out of range\n");
exit(EXIT_FAILURE);
}
// Go, go, go !!!
printf("traceroute to %s (%s), %d hops max, %d byte packets\n",
g_tcrt_ctx.dest_name,
inet_ntoa((struct in_addr){ g_tcrt_ctx.dest_ip }),
g_tcrt_ctx.max_ttl, g_tcrt_ctx.pack_len);
for (
int i = 1;
g_tcrt_ctx.current_ttl <= g_tcrt_ctx.max_ttl;
g_tcrt_ctx.current_ttl++, i++) {
printf("%2d ", i);
for (uint8_t j = 0; j < g_tcrt_ctx.query_count; j++, g_tcrt_ctx.dest_port++) {
// Pipeline
initialize_socket();
trcrt_send();
trcrt_receive();
trcrt_print_result();
destroy_socket();
sleep(g_tcrt_ctx.send_wait);
}
printf("\n");
if (g_tcrt_ctx.end_tracing) break;
}
}
static void trcrt_send() {
int ret;
if (g_tcrt_ctx.flags & TRCRT_ICMP) {
ret = send_icmp_msg_v4(
g_tcrt_ctx.sock,
g_tcrt_ctx.tos,
getpid(),
g_tcrt_ctx.current_ttl,
ICMP_ECHO,
g_tcrt_ctx.dest_port /* according to man */,
max(g_tcrt_ctx.pack_len - (int)sizeof (struct iphdr) - (int)sizeof (struct icmphdr), 0),
g_tcrt_ctx.source_ip,
g_tcrt_ctx.dest_ip);
}
else {
ret = send_udp_trcrt_msg(
g_tcrt_ctx.sock,
g_tcrt_ctx.tos,
g_tcrt_ctx.current_ttl,
max(g_tcrt_ctx.pack_len - (int)sizeof (struct iphdr) - (int)sizeof (struct udphdr), 0),
g_tcrt_ctx.dest_ip,
g_tcrt_ctx.dest_port);
}
if (ret != 0) {
perror("cannot send msg");
exit(EXIT_FAILURE);
}
ret = gettimeofday(&g_tcrt_ctx.time_sent, NULL);
if (ret != 0) {
perror("cannot set time");
exit(EXIT_FAILURE);
}
}
static void initialize_socket() {
int sock, setsock;
if (g_tcrt_ctx.flags & TRCRT_ICMP) {
sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
}
else {
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
}
if (sock < 0) {
perror("cannot create socket");
exit(EXIT_FAILURE);
}
if (g_tcrt_ctx.flags & TRCRT_ICMP)
setsock = setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (int[1]){ 1 }, sizeof(int));
else {
setsock = setsockopt(sock, SOL_IP, IP_RECVERR, (int[1]){ 1 }, sizeof(int));
}
if (setsock < 0) {
perror("cannot set sock option");
close(sock);
exit(EXIT_FAILURE);
}
// Set socket to context
g_tcrt_ctx.sock = sock;
}
static void destroy_socket() {
if (close(g_tcrt_ctx.sock) != 0) {
perror("cannot close socket");
exit(EXIT_FAILURE);
}
}
static void trcrt_receive() {
alarm(g_tcrt_ctx.wait_max);
if (g_tcrt_ctx.flags & TRCRT_ICMP) {
trcrt_handle_icmp();
}
else {
trcrt_handle_udp();
}
}
static void trcrt_handle_icmp() {
struct icmphdr* icmp;
struct iphdr* ip;
char buffer[2048];
ssize_t ret;
ret = recvfrom(g_tcrt_ctx.sock, buffer, sizeof buffer, 0, NULL, 0);
// Answer came
if (ret > 0) {
ip = (struct iphdr *)buffer;
icmp = (struct icmphdr*)(buffer + sizeof (struct iphdr));
g_tcrt_ctx.answer_ip = ip->saddr;
g_tcrt_ctx.icmp_rpl_type = icmp->type;
g_tcrt_ctx.icmp_rpl_code = icmp->code;
if (icmp->type == ICMP_ECHOREPLY || icmp->type == ICMP_DEST_UNREACH) {
g_tcrt_ctx.end_tracing = 1;
}
return;
}
// Some unexpected error
if (ret == 0) {
fprintf(stderr, "Connection closed\n");
exit(EXIT_FAILURE);
}
// Reading interrupted by signal, equivalent to (errno==EINTR)
g_tcrt_ctx.icmp_rpl_type = -1;
}
static void trcrt_handle_udp() {
char buffer[2048];
struct iovec iov[1];
struct msghdr msg;
ssize_t ret;
// Init message struct
memset(&msg, 0x0, sizeof(msg));
memset(iov, 0x0, sizeof(iov));
iov[0].iov_base = buffer;
iov[0].iov_len = sizeof buffer;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
char buffer2[1024];
struct cmsghdr *cmsg;
struct sock_extended_err *sock_err;
msg.msg_flags = 0;
msg.msg_control = buffer2;
msg.msg_controllen = sizeof(buffer2);
g_tcrt_ctx.try_read = 1;
while (g_tcrt_ctx.try_read) {
ret = recvmsg(g_tcrt_ctx.sock, &msg, MSG_ERRQUEUE);
if (ret < 0) continue; // Resp have not come yet, try again...
cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR) {
sock_err = (struct sock_extended_err *)CMSG_DATA(cmsg);
g_tcrt_ctx.icmp_rpl_type = sock_err->ee_type;
g_tcrt_ctx.icmp_rpl_code = sock_err->ee_code;
if (sock_err->ee_type == ICMP_ECHOREPLY || sock_err->ee_code == ICMP_DEST_UNREACH) {
g_tcrt_ctx.end_tracing = 1;
}
// Get sender's IP
g_tcrt_ctx.answer_ip = ((struct sockaddr_in*)SO_EE_OFFENDER(sock_err))->sin_addr.s_addr;
}
else {
printf("unknown\n");
}
return;
}
g_tcrt_ctx.icmp_rpl_type = -1;
}
static void trcrt_print_result() {
char hostname[NI_MAXHOST] = { 0 };
struct timeval msg_rcv_time;
__suseconds_t rtt;
int ret;
if (gettimeofday(&msg_rcv_time, NULL) != 0) {
perror("cannot set time of receiving");
exit(EXIT_FAILURE);
}
ret = get_name_by_ipaddr(g_tcrt_ctx.answer_ip, hostname, sizeof hostname, NULL);
rtt = time_diff(&g_tcrt_ctx.time_sent, &msg_rcv_time);
if (g_tcrt_ctx.answer_ip != g_tcrt_ctx.last_printed_ip) {
if (ret == 0) {
// managed to resolve hostname
printf("%s ", hostname);
}
else {
// Did not manage to resolve hostname
printf("%s ", inet_ntoa((struct in_addr){ g_tcrt_ctx.answer_ip }));
}
printf("(%s) ", inet_ntoa((struct in_addr){ g_tcrt_ctx.answer_ip }));
g_tcrt_ctx.last_printed_ip = g_tcrt_ctx.answer_ip;
}
switch (g_tcrt_ctx.icmp_rpl_type)
{
case ICMP_ECHOREPLY:
case ICMP_DEST_UNREACH:
case ICMP_TIME_EXCEEDED:
printf("%ld.%03ld ms ", rtt / 1000, rtt % 1000);
break;
case -1:
printf("* ");
break;
default:
printf("# ");
}
if (g_tcrt_ctx.icmp_rpl_type == ICMP_DEST_UNREACH)
switch (g_tcrt_ctx.icmp_rpl_code)
{
case ICMP_NET_UNREACH:
printf("!N ");
break;
case ICMP_HOST_UNREACH:
printf("!H ");
break;
case ICMP_PROT_UNREACH:
printf("!P ");
break;
case ICMP_PORT_UNREACH:
break; // Default case, no reaction
case ICMP_NET_ANO:
default:
printf("!X ");
break;
}
fflush(stdout);
}
static __suseconds_t time_diff(struct timeval* begin, struct timeval *end) {
__suseconds_t ret;
ret = end->tv_sec - begin->tv_sec;
ret *= MICROSECONDS_IN_SECOND;
ret += (end->tv_usec - begin->tv_usec);
return ret;
}