我正在为学校写一个traceroute程序。 它适用于某些网站(如www.google.fr),程序无法到达目的地:它在第9跳后停止。
Domain: www.google.fr
Address: 173.194.40.159
1 172.29.255.253 172.29.255.253
2 130.79.209.197 routeur-espla-rc1-130-79-209-197.u-strasbg.fr
3 * * *
4 193.51.189.85 te1-2-nancy-rtr-021.noc.renater.fr
5 193.51.189.161 te0-0-0-2-paris1-rtr-001.noc.renater.fr
6 193.51.189.6 te0-0-0-0-paris2-rtr-001.noc.renater.fr
7 * * *
8 193.51.182.197 193.51.182.197
9 72.14.238.228 72.14.238.228
10 * * *
11 * * *
...
30 * * *
真正的traceroute没有问题:
traceroute to www.google.fr (173.194.40.223), 30 hops max, 60 byte packets
1 172.29.255.253 (172.29.255.253) 3.575 ms 3.588 ms 4.119 ms
2 routeur-espla-rc1-130-79-209-197.u-strasbg.fr (130.79.209.197) 4.321 ms 4.273 ms 5.032 ms
3 * * *
4 te1-2-nancy-rtr-021.noc.renater.fr (193.51.189.85) 11.216 ms 11.662 ms 12.960 ms
5 te0-0-0-2-paris1-rtr-001.noc.renater.fr (193.51.189.161) 14.655 ms 14.637 ms 14.615 ms
6 te0-0-0-0-paris2-rtr-001.noc.renater.fr (193.51.189.6) 14.588 ms 213.011 ms 18.148 ms
7 * * *
8 193.51.182.197 (193.51.182.197) 11.062 ms 11.084 ms 11.243 ms
9 72.14.238.228 (72.14.238.228) 12.217 ms 12.765 ms 17.578 ms
10 209.85.243.51 (209.85.243.51) 13.511 ms 17.515 ms 17.493 ms
11 par10s12-in-f31.1e100.net (173.194.40.223) 13.183 ms 14.091 ms 14.054 ms
以下是循环和更改TTL值的函数的代码:
void LoopUDP(int rcvt, int sndt, int ttl_t[3],
struct sockaddr_in server, struct sockaddr_in my_addr)
{
struct timeval r_timeout = { rcvt, 0 };
struct timeval s_timeout = { sndt, 0 };
Socket send_socket, receive_socket;
socklen_t addrlen = sizeof (struct sockaddr_in);
struct sockaddr_in recept = { 0 };
char recvbuf[MAX_PACKET];
char *host = NULL, *rsaddr = NULL;
char dest[MAX_ADDRESS];
strcpy(dest, inet_ntoa(server.sin_addr));
int reach_dest = 0;
int tent, tentative = 3;
int ttl, min_ttl = ttl_t[0], max_ttl = ttl_t[1], hops = ttl_t[2];
for (ttl = min_ttl; ttl <= max_ttl; ttl += hops)
{
send_socket = OpenDgramSocket('U');
receive_socket = OpenRawSocket('I');
if (bind(receive_socket, (struct sockaddr*)&my_addr, addrlen) == -1)
{
perror("bind receive socket");
exit(-1);
}
if ( ! SetTTL(send_socket, ttl)) exit(-1);
if ( ! SetSNDTimeOut(send_socket, s_timeout)) exit(-1);
if ( ! SetRCVTimeOut(receive_socket, r_timeout)) exit(-1);
if (sendto(send_socket, "hello", 4, 0, (struct sockaddr*) &server, addrlen) == -1)
{
perror("sendto()");
}
else
{
printf(" %-2d ", ttl);
for (tent = 0; tent < tentative; tent++)
{
if (recvfrom(receive_socket, recvbuf, MAX_PACKET, 0, (struct sockaddr*)&recept, &addrlen) == -1)
{
printf("* ");
}
else
{
rsaddr = inet_ntoa(recept.sin_addr);
host = GetHostNameFromIP(rsaddr);
printf("%-15s %s", rsaddr, host);
if (strcmp(dest, rsaddr)==0)
{
reach_dest = 1;
}
break;
}
}
printf("\n");
}
close(send_socket);
close(receive_socket);
if (reach_dest) return;
}
}
你知道为什么它没有到达目的地吗? 国旗问题?