41 udp = (struct udphdr*)(recvbuf + 8 + hlen2);
42 if (hip6->ip6_nxt == IPPROTO_UDP &&
43 udp->uh_sport == htons(sport) &&
44 udp->uh_dport == htons(dport + seq))
45 ret = -2; /* ответил промежуточный маршрутизатор */
46 break;
47 } else if (icmp6->icmp6_type == ICMP6_DST_UNREACH) {
48 if (icmp6len < 8 + sizeof(struct ip6_hdr) + 4)
49 continue; /* недостаточно для проверки внутреннего заголовка */
50 hip6 = (struct ip6_hdr*)(recvbuf + 8);
51 hlen2 = sizeof(struct ip6_hdr);
52 udp = (struct udphdr*)(recvbuf + 8 + hlen2);
53 if (hip6->ip6_nxt == IPPROTO_UDP &&
54 udp->uh_sport == htons(sport) &&
55 udp->uh_dport == htons(dport + seq)) {
56 if (icmp6->icmp6_code == ICMP6_DST_UNREACH_NOPORT)
57 ret = -1; /* цель достигнута */
58 else
59 ret = icmp6->icmp6_code; /* 0, 1, 2, ... */
60 break;
61 }
62 } else if (verbose) {
63 printf(" (from %s: type = %d, code = %d)\n",
64 Sock_ntop_host(pr->sarecv, pr->salen);
65 icmp6->icmp6_type, icmp6->icmp6_code);
66 }
67 /* другая ICMP-ошибка. нужно вызвать recvfrom() */
68 }
69 alarm(0); /* отключаем таймер */
70 Gettimeofday(tv, NULL); /* get time of packet arrival */
71 return(ret);
72 #endif
73 }
Листинг 28.19. Возвращение строки, соответствующей коду недоступности ICMPv6
//traceroute/icmpcode_v6.c
1 #include "trace.h"
2 const char *
3 icmpcode_v6(int code)
4 {
5 #ifdef IPV6
6 static char errbuf[100];
7 switch (code) {
8 case ICMP6_DST_UNREACH_NOROUTE:
9 return("no route to host");
10 case ICMP6_DST_UNREACH_ADMIN:
11 return("administratively prohibited");
12 case ICMP6_DST_UNREACH_NOTNEIGHBOR:
13 return("not a neighbor");
14 case ICMP6_DST_UNREACH_ADDR:
15 return("address unreachable");
15 case ICMP6_DST_UNREACH_NOPORT:
16 return("port unreachable");
17 default:
18 sprintf(errbuf, "[unknown code %d]",. code);
19 return errbuf;
20 }
21 #endif
22 }
Пример
Сначала приведем пример с Ipv4:
freebsd % <b>traceroute www.unpbook.com</b>
traceroute to www.unpbook.com (206.168.112.219): 30 hops max. 24 data bytes
1 12.106.32.1 (12.106.32.1) 0.799 ms 0.719 ms 0.540 ms
2 12.124.47.113 (12.124.47.113) 1.758 ms 1.760 ms 1.839 ms
3 gbr2-p27.sffca.ip.att.net (12.123.195.38) 2.744 ms 2.575 ms 2.648 ms
4 tbr2-p012701.sffca.ip.att.net (12.122.11.85) 3.770 ms 3.689 ms 3.848 ms
5 gbr3-p50.dvmco.ip.att.net (12.122.2.66) 26.202 ms 26.242 ms 26.102 ms
6 gbr2-p20.dvmco.ip.att.net (12.122.5.26) 26 255 ms 26.194 ms 26.470 ms
7 gar2-p370.dvmco.ip.att.net (12.123.36.141) 26.443 ms 26.310 ms 26.427 ms
8 att-46.den.internap.ip.att.net (12.124.158.58) 26.962 ms 27.130 ms 27.279 ms
9 border10 ge3-0-bbnet2.den.pnap.net (216.52.40.79) 27.285 ms 27 293 ms 26.860 ms
10 coop-2.border10.den.pnap.net (216 52.42.118) 28.721 ms 28.991 ms 30.077 ms
11 199.45.130.33 (199.45.130.33) 29.095 ms 29.055 ms 29 378 ms
12 border-to-141-netrack.boulder.со.coop.net (207.174.144.178) 30.875 ms 29.747 ms 30.142 ms
13 linux.unpbook.com (206.168.112.219) 31.713 ms 31.573 ms 33.952 ms
Ниже приведен пример с IPv6. Для лучшей читаемости длинные строки разбиты.
freebsd % <b>traceroute www.kame.net</b>
traceroute to orange.kame.net (2001:200:0:4819:203:47ff:fea5:3085): 30 hops max, 24 data bytes
1 3ffe:b80:3:9ad1::1 (3ffe:b80:3:9ad1::1) 107.437 ms 99.341 ms 103.477 ms
2 Viagenie-gw.int.ipv6.ascc.net (2001:288:3b0::55)
105.129 ms 89.418 ms 90.016 ms
3 gw-Viagenie.int.ipv6.ascc.net (2001:288:3b0::54)
302.300 ms 291.580 ms 289.839 ms
4 c7513-gw.int.ipv6.ascc.net (2001:288:3b0::c)
296.088 ms 298.600 ms 292.196 ms
5 m160-c7513.int.ipv6.ascc.net (2001:288:3b0::1e)
296.266 ms 314.878 ms 302.429 ms