-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtraceroute_book.c
253 lines (219 loc) · 7.19 KB
/
traceroute_book.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <signal.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/udp.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
static pid_t pid;
static int maxttl = 30;
unsigned short dport = 32768 + 666;
unsigned short sport;
int sendfd, recvfd;
int nsent;
char sendbuf[1500];
char recvbuf[1500];
int nprobes = 3;
struct udpdata {
unsigned short seq;
unsigned short ttl;
struct timeval tv;
};
int datalen = sizeof(struct udpdata);
static int goalarm;
char *host;
struct timeval tvrecv, tvsend;
static struct sockaddr_in sendaddr;
static struct sockaddr_in recvaddr;
static struct sockaddr_in lastaddr;
static struct sockaddr_in bindaddr;
socklen_t addrlen;
typedef void(*sig_handler)(int);
sig_handler fuck(int signo, sig_handler newhandler)
{
struct sigaction newact, oldact;
bzero(&newact, sizeof(newact));
newact.sa_handler = newhandler;
if (signo == SIGALRM) {
#ifdef SA_INTERRUPT
newact.sa_flags |= SA_INTERRUPT;
#endif
} else {
#ifdef SA_RESTART
newact.sa_flags |= SA_RESTART;
#endif
}
if (sigaction(signo, &newact, &oldact) < 0)
return SIG_ERR;
return oldact.sa_handler;
}
void sig_alrm(int signo)
{
goalarm = 1;
}
int sockaddr_same(struct sockaddr_in *left, struct sockaddr_in *right)
{
return left->sin_port == right->sin_port && left->sin_addr.s_addr == right->sin_addr.s_addr && left->sin_family == right->sin_family;
}
int process_icmpv4_response(int seq, struct timeval *tv)
{
alarm(3);
socklen_t len = sizeof(recvaddr);
ssize_t n = 0;
struct ip *ip_ptr = NULL;
struct icmp *icmp_ptr = NULL;
struct udphdr *udp_ptr = NULL;
struct ip *ip_ptr_2 = NULL;
int hlen1 = 0, hlen2 = 0;
int icmplen = 0;
goalarm = 0;
char *ptr = recvbuf;
int retcode = 0;
for (;;) {
if (goalarm)
return -3;
len = sizeof(recvaddr);
if ((n = recvfrom(recvfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&recvaddr, &len)) < 0) {
if (errno == EINTR)
continue;
perror("recvfrom error!");
exit(1);
}
ip_ptr = (struct ip *)recvbuf;
hlen1 = ip_ptr->ip_hl << 2;
if ((icmplen = n - hlen1) < 8)
continue;
icmp_ptr = (struct icmp *)(ptr + hlen1);
if (icmp_ptr->icmp_type == ICMP_TIMXCEED && icmp_ptr->icmp_code == ICMP_TIMXCEED_INTRANS) {
if (icmplen < 8 + sizeof(struct ip)) {
continue;
}
ip_ptr_2 = (struct ip *)(ptr + hlen1 + 8);
hlen2 = ip_ptr_2->ip_hl << 2;
if (icmplen < 8 + hlen2 + 8) {
continue;
}
udp_ptr = (struct udphdr *)(ptr + hlen1 + 8 + hlen2);
if (ip_ptr_2->ip_p == IPPROTO_UDP && udp_ptr->uh_sport == htons(sport) && udp_ptr->uh_dport == htons(dport + seq)) {
retcode = -2;
break;
} else {
printf("%d %d %d sport = %d dport = %d\n", ip_ptr_2->ip_p,(udp_ptr->uh_sport),(udp_ptr->uh_dport), sport, dport);
}
} else if (icmp_ptr->icmp_type == ICMP_UNREACH) {
if (icmplen < 8 + sizeof(struct ip))
continue;
ip_ptr_2 = (struct ip *)(icmp_ptr + 1);
hlen2 = ip_ptr_2->ip_hl << 2;
if (icmplen < 8 + hlen2 + 8)
continue;
udp_ptr = (struct udphdr *)(ptr + hlen1 + 8 + hlen2);
if (ip_ptr_2->ip_p == IPPROTO_UDP && udp_ptr->uh_sport == htons(sport) && udp_ptr->uh_dport == htons(dport + seq)) {
if (icmp_ptr->icmp_code == ICMP_UNREACH_PORT)
retcode = -1;
else
retcode = icmp_ptr->icmp_code;
break;
}
}
}
alarm(0);
gettimeofday(tv, NULL);
return retcode;
}
void traceloop()
{
if ((recvfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
perror("icmp socket fd create fail!");
exit(1);
}
setuid(getuid());
if ((sendfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("udp socket fd create fail!");
exit(1);
}
bindaddr.sin_family = AF_INET;
sport = (getpid() & 0xFFFF) | 0x8000;
bindaddr.sin_port = htons(sport);
if (bind(sendfd, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) < 0) {
perror("bind error!");
exit(1);
}
int done = 0;
int seq = 0;
for (int ttl = 1; ttl < maxttl; ttl++) {
if (setsockopt(sendfd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
perror("setsockopt error!");
exit(1);
}
bzero(&lastaddr, sizeof(lastaddr));
printf("%2d\t", ttl);
for (int probe = 0; probe < nprobes; probe++) {
struct udpdata *ud = (struct udpdata *)sendbuf;
ud->seq = ++seq;
ud->ttl = ttl;
gettimeofday(&tvsend, NULL);
sendaddr.sin_port = htons(dport + seq);
if ((sendto(sendfd, sendbuf, datalen, 0, (struct sockaddr *)&sendaddr, sizeof(sendaddr))) != datalen) {
perror("write error!");
exit(1);
}
int retcode = 0;
if ((retcode = process_icmpv4_response(seq, &tvrecv)) == -3) {
printf(" *");
} else {
if (!sockaddr_same(&lastaddr, &recvaddr)) {
printf("%s\t", inet_ntoa(recvaddr.sin_addr));
memcpy(&lastaddr, &recvaddr, sizeof(recvaddr));
}
if ((tvrecv.tv_usec -= tvsend.tv_usec) < 0) {
tvrecv.tv_sec--;
tvrecv.tv_usec += 1000000;
}
tvrecv.tv_sec -= tvsend.tv_sec;
double costtime = tvrecv.tv_sec * 1000.0 + tvrecv.tv_usec / 1000.0;
printf("%.3f ms ", costtime);
if (retcode == -1)
done = 1;
else if (retcode >= 0)
printf(" (ICMP %d) ", retcode);
}
}
printf("\n");
}
}
int main(int argc, char *argv[])
{
if (argc != 2) {
printf("usage : %s <hostname/ip>\n", argv[0]);
exit(1);
}
host = argv[1];
pid = getpid();
setbuf(stdout, NULL);
if (fuck(SIGALRM, sig_alrm) == SIG_ERR) {
perror("fuck error!");
exit(1);
}
struct addrinfo hints, *res = NULL;
bzero(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_flags = AI_CANONNAME;
int error = 0;
if ((error = getaddrinfo(host, NULL, &hints, &res)) != 0) {
printf("getaddrinfo error! %s : %s\n", host, gai_strerror(error));
exit(1);
}
char *ip = inet_ntoa(((struct sockaddr_in *)res->ai_addr)->sin_addr);
printf("traceroute to %s (%s) %d hops max, %d data bytes\n", res->ai_canonname ? res->ai_canonname : ip, ip, maxttl, datalen);
memcpy(&sendaddr, res->ai_addr, sizeof(struct sockaddr_in));
sendaddr.sin_port = htons(dport);
traceloop();
}