Mercurial > hg > toybox
annotate toys/pending/traceroute.c @ 1396:e0c9c5424864 draft
Isaac Dunham spotted that dhcp was also reimplementing daemon().
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 20 Jul 2014 21:34:49 -0500 |
parents | a2a7eb0dd933 |
children | 5fac2769a159 |
rev | line source |
---|---|
1075 | 1 /* traceroute - trace the route to "host". |
2 * | |
3 * Copyright 2012 Madhur Verma <mad.flexi@gmail.com> | |
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com> | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
5 * Copyright 2013 Bilal Qureshi <bilal.jmi@gmail.com> |
1160
a2a7eb0dd933
Updating the email id in Copyright.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1158
diff
changeset
|
6 * Copyright 2013 Ashwini Kumar <ak.ashwini1981@gmail.com> |
1075 | 7 * |
8 * No Standard | |
9 | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
10 USE_TRACEROUTE(NEWTOY(traceroute, "<1>2i:f#<1>255=1z#<0>86400=0g*w#<0>86400=5t#<0>255=0s:q#<1>255=3p#<1>65535=33434m#<1>255=30rvndlIUF64", TOYFLAG_STAYROOT|TOYFLAG_USR|TOYFLAG_BIN)) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
11 USE_TRACEROUTE(OLDTOY(traceroute6,traceroute, OPTSTR_traceroute, TOYFLAG_STAYROOT|TOYFLAG_USR|TOYFLAG_BIN)) |
1075 | 12 config TRACEROUTE |
13 bool "traceroute" | |
1158 | 14 default n |
1075 | 15 help |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
16 usage: traceroute [-46FUIldnvr] [-f 1ST_TTL] [-m MAXTTL] [-p PORT] [-q PROBES] |
1075 | 17 [-s SRC_IP] [-t TOS] [-w WAIT_SEC] [-g GATEWAY] [-i IFACE] [-z PAUSE_MSEC] HOST [BYTES] |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
18 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
19 traceroute6 [-dnrv] [-m MAXTTL] [-p PORT] [-q PROBES][-s SRC_IP] [-t TOS] [-w WAIT_SEC] |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
20 [-i IFACE] HOST [BYTES] |
1075 | 21 |
22 Trace the route to HOST | |
23 | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
24 -4,-6 Force IP or IPv6 name resolution |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
25 -F Set the don't fragment bit (supports IPV4 only) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
26 -U Use UDP datagrams instead of ICMP ECHO (supports IPV4 only) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
27 -I Use ICMP ECHO instead of UDP datagrams (supports IPV4 only) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
28 -l Display the TTL value of the returned packet (supports IPV4 only) |
1075 | 29 -d Set SO_DEBUG options to socket |
30 -n Print numeric addresses | |
31 -v verbose | |
32 -r Bypass routing tables, send directly to HOST | |
33 -m Max time-to-live (max number of hops)(RANGE 1 to 255) | |
34 -p Base UDP port number used in probes(default 33434)(RANGE 1 to 65535) | |
35 -q Number of probes per TTL (default 3)(RANGE 1 to 255) | |
36 -s IP address to use as the source address | |
37 -t Type-of-service in probe packets (default 0)(RANGE 0 to 255) | |
38 -w Time in seconds to wait for a response (default 3)(RANGE 0 to 86400) | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
39 -g Loose source route gateway (8 max) (supports IPV4 only) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
40 -z Pause Time in milisec (default 0)(RANGE 0 to 86400) (supports IPV4 only) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
41 -f Start from the 1ST_TTL hop (instead from 1)(RANGE 1 to 255) (supports IPV4 only) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
42 -i Specify a network interface to operate with |
1075 | 43 */ |
44 #define FOR_traceroute | |
45 #include "toys.h" | |
46 #include <netinet/udp.h> | |
47 #include <netinet/ip_icmp.h> | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
48 #include <netinet/ip6.h> |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
49 #include <netinet/icmp6.h> |
1075 | 50 |
51 GLOBALS( | |
52 long max_ttl; | |
53 long port; | |
54 long ttl_probes; | |
55 char *src_ip; | |
56 long tos; | |
57 long wait_time; | |
58 struct arg_list *loose_source; | |
59 long pause_time; | |
60 long first_ttl; | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
61 char *iface; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
62 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
63 uint32_t gw_list[9]; |
1075 | 64 int recv_sock; |
65 int snd_sock; | |
66 unsigned msg_len; | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
67 char *packet; |
1075 | 68 uint32_t ident; |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
69 int istraceroute6; |
1075 | 70 ) |
71 | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
72 #ifndef SOL_IPV6 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
73 # define SOL_IPV6 IPPROTO_IPV6 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
74 #endif |
1075 | 75 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
76 #define ICMP_HD_SIZE4 8 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
77 #define USEC 1000000ULL |
1075 | 78 |
79 struct payload_s { | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
80 uint32_t seq; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
81 uint32_t ident; |
1075 | 82 }; |
83 | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
84 char addr_str[INET6_ADDRSTRLEN]; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
85 struct sockaddr_storage dest; |
1075 | 86 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
87 //Compute checksum SUM of buffer P of length LEN |
1075 | 88 static u_int16_t in_cksum(u_int16_t *p, u_int len) |
89 { | |
90 u_int32_t sum = 0; | |
91 int nwords = len >> 1; | |
92 | |
93 while (nwords-- != 0) sum += *p++; | |
94 if (len & 1) { | |
95 union { | |
96 u_int16_t w; | |
97 u_int8_t c[2]; | |
98 } u; | |
99 u.c[0] = *(u_char *) p; | |
100 u.c[1] = 0; | |
101 sum += u.w; | |
102 } | |
103 // end-around-carry | |
104 sum = (sum >> 16) + (sum & 0xffff); | |
105 sum += (sum >> 16); | |
106 return (~sum); | |
107 } | |
108 | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
109 //sends a single probe packet with sequence(SEQ) and time-to-live(TTL) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
110 static void send_probe4(int seq, int ttl) |
1075 | 111 { |
112 int res, len; | |
113 void *out; | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
114 struct payload_s *send_data4 = (struct payload_s *)(TT.packet); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
115 struct icmp *send_icmp4 = (struct icmp *)(TT.packet); |
1075 | 116 |
117 if (toys.optflags & FLAG_U) { | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
118 send_data4->seq = seq; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
119 send_data4->ident = TT.ident; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
120 ((struct sockaddr_in *)&dest)->sin_port = TT.port + seq; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
121 out = send_data4; |
1075 | 122 } else { |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
123 send_icmp4->icmp_type = ICMP_ECHO; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
124 send_icmp4->icmp_id = htons(TT.ident); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
125 send_icmp4->icmp_seq = htons(seq); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
126 send_icmp4->icmp_cksum = 0; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
127 send_icmp4->icmp_cksum = in_cksum((uint16_t *) send_icmp4, TT.msg_len); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
128 if (send_icmp4->icmp_cksum == 0) send_icmp4->icmp_cksum = 0xffff; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
129 out = send_icmp4; |
1075 | 130 } |
131 | |
132 res = setsockopt(TT.snd_sock, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); | |
133 if (res < 0) perror_exit("setsockopt ttl %d", ttl); | |
134 | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
135 len = TT.msg_len; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
136 res = sendto(TT.snd_sock, out, len, 0, (struct sockaddr *) &dest, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
137 sizeof(struct sockaddr_in)); |
1075 | 138 if (res != len) perror_exit(" sendto"); |
139 } | |
140 | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
141 //sends a single probe packet with sequence(SEQ) and time-to-live(TTL) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
142 static void send_probe6(int seq, int ttl) |
1075 | 143 { |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
144 void *out; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
145 struct payload_s *send_data6 = (struct payload_s *) (TT.packet); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
146 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
147 send_data6->seq = seq; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
148 send_data6->ident = TT.ident; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
149 ((struct sockaddr_in6 *)&dest)->sin6_port = TT.port; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
150 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
151 if (setsockopt(TT.snd_sock, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
152 sizeof(ttl)) < 0) error_exit("setsockopt ttl %d", ttl); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
153 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
154 out = send_data6; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
155 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
156 if (sendto(TT.snd_sock, out, TT.msg_len, 0, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
157 (struct sockaddr *) &dest, sizeof(struct sockaddr_in6)) < 0) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
158 perror_exit("sendto"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
159 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
160 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
161 static void set_flag_dr(int sock) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
162 { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
163 int set = 1; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
164 if ((toys.optflags & FLAG_d) && (setsockopt(sock,SOL_SOCKET, SO_DEBUG, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
165 &set, sizeof(set)) < 0)) perror_exit("SO_DEBUG failed "); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
166 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
167 if ((toys.optflags & FLAG_r) && (setsockopt(sock, SOL_SOCKET, SO_DONTROUTE, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
168 &set, sizeof(set)) < 0)) perror_exit("SO_DONTROUTE failed "); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
169 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
170 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
171 static void bind_to_interface(int sock) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
172 { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
173 struct ifreq ifr; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
174 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
175 snprintf(ifr.ifr_name, IFNAMSIZ, "%s", TT.iface); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
176 if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr))) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
177 perror_msg("can't bind to interface %s", TT.iface); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
178 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
179 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
180 static void resolve_addr(char *host, int family, int type, int proto, void *sock) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
181 { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
182 struct addrinfo *info, hint; |
1075 | 183 int ret; |
184 | |
185 memset(&hint, 0, sizeof(hint)); | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
186 hint.ai_family = family; |
1075 | 187 hint.ai_socktype = type; |
188 hint.ai_protocol = proto; | |
189 | |
190 ret = getaddrinfo(host, NULL, &hint, &info); | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
191 if (ret || !info) error_exit("bad address: %s ", host); |
1075 | 192 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
193 memcpy(sock, info->ai_addr, info->ai_addrlen); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
194 freeaddrinfo(info); |
1075 | 195 } |
196 | |
197 static void do_trace() | |
198 { | |
199 int seq, fexit, ttl, tv = TT.wait_time * 1000; | |
200 struct pollfd pfd[1]; | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
201 struct sockaddr_storage from; |
1075 | 202 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
203 memset(&from, 0, sizeof(from)); |
1075 | 204 pfd[0].fd = TT.recv_sock; |
205 pfd[0].events = POLLIN; | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
206 |
1075 | 207 for (ttl = TT.first_ttl; ttl <= TT.max_ttl; ++ttl) { |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
208 int probe, dest_reach = 0, print_verbose = 1; |
1075 | 209 struct timeval t1, t2; |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
210 struct sockaddr_storage last_addr; |
1075 | 211 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
212 memset(&last_addr, 0, sizeof(last_addr)); |
1075 | 213 fexit = 0; |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
214 xprintf("%2d", ttl); |
1075 | 215 |
216 for (probe = 0, seq = 0; probe < TT.ttl_probes; ++probe) { | |
217 int res = 0, tleft; | |
218 | |
219 fflush(NULL); | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
220 if (!TT.istraceroute6) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
221 if (probe && (toys.optflags & FLAG_z)) usleep(TT.pause_time * 1000); |
1075 | 222 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
223 if (!TT.istraceroute6) send_probe4(++seq, ttl); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
224 else send_probe6(++seq, ttl); |
1075 | 225 gettimeofday(&t1, NULL); |
226 t2 = t1; | |
227 | |
228 while ((tleft = (int)(tv - ((unsigned long long)(t2.tv_sec * 1000ULL | |
229 + t2.tv_usec/1000) - (unsigned long long)(t1.tv_sec * 1000ULL | |
230 + t1.tv_usec/1000)))) >= 0) { | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
231 unsigned delta = 0; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
232 if (!(res = poll(pfd, 1, tleft))) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
233 xprintf(" *"); |
1075 | 234 break; |
235 } | |
236 gettimeofday(&t2, NULL); | |
237 if (res < 0) { | |
238 if (errno != EINTR) perror_exit("poll"); | |
239 continue; | |
240 } | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
241 delta = (t2.tv_sec * USEC + t2.tv_usec) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
242 - (t1.tv_sec * USEC + t1.tv_usec); |
1075 | 243 |
244 if (pfd[0].revents) { | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
245 unsigned addrlen = sizeof(struct sockaddr_storage); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
246 int rcv_len, icmp_res = 0; |
1075 | 247 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
248 rcv_len = recvfrom(TT.recv_sock, toybuf, sizeof(toybuf), |
1075 | 249 MSG_DONTWAIT, (struct sockaddr *) &from, &addrlen); |
250 if (rcv_len <= 0) continue; | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
251 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
252 if (!TT.istraceroute6) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
253 int pmtu = 0; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
254 struct ip *rcv_pkt = (struct ip*) toybuf; |
1075 | 255 struct icmp *ricmp; |
256 | |
257 ricmp = (struct icmp *) ((void*)rcv_pkt + (rcv_pkt->ip_hl << 2)); | |
258 if (ricmp->icmp_code == ICMP_UNREACH_NEEDFRAG) | |
259 pmtu = ntohs(ricmp->icmp_nextmtu); | |
260 | |
261 if ((ricmp->icmp_type == ICMP_TIMXCEED | |
262 && ricmp->icmp_code == ICMP_TIMXCEED_INTRANS) | |
263 || ricmp->icmp_type == ICMP_UNREACH | |
264 || ricmp->icmp_type == ICMP_ECHOREPLY) { | |
265 | |
266 struct udphdr *hudp; | |
267 struct icmp *hicmp; | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
268 struct ip *hip = &ricmp->icmp_ip; |
1075 | 269 |
270 if (toys.optflags & FLAG_U) { | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
271 hudp = (struct udphdr*) ((char*)hip + (hip->ip_hl << 2)); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
272 if ((hip->ip_hl << 2) + 12 <=(rcv_len - (rcv_pkt->ip_hl << 2)) |
1075 | 273 && hip->ip_p == IPPROTO_UDP |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
274 && hudp->dest == (TT.port + seq)) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
275 icmp_res = (ricmp->icmp_type == ICMP_TIMXCEED ? -1 : |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
276 ricmp->icmp_code); |
1075 | 277 } else { |
278 hicmp = (struct icmp *) ((void*)hip + (hip->ip_hl << 2)); | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
279 if (ricmp->icmp_type == ICMP_ECHOREPLY |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
280 && ricmp->icmp_id == ntohs(TT.ident) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
281 && ricmp->icmp_seq == ntohs(seq)) |
1075 | 282 icmp_res = ICMP_UNREACH_PORT; |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
283 else if ((hip->ip_hl << 2) + ICMP_HD_SIZE4 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
284 <= (rcv_len - (rcv_pkt->ip_hl << 2)) |
1075 | 285 && hip->ip_p == IPPROTO_ICMP |
286 && hicmp->icmp_id == htons(TT.ident) | |
287 && hicmp->icmp_seq == htons(seq)) | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
288 icmp_res = (ricmp->icmp_type == ICMP_TIMXCEED ? -1 : |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
289 ricmp->icmp_code); |
1075 | 290 } |
291 } | |
292 if (!icmp_res) continue; | |
293 | |
294 if (addrlen > 0) { | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
295 if (memcmp(&((struct sockaddr_in *)&last_addr)->sin_addr, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
296 &((struct sockaddr_in *)&from)->sin_addr, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
297 sizeof(struct in_addr))) { |
1075 | 298 if (!(toys.optflags & FLAG_n)) { |
299 char host[NI_MAXHOST]; | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
300 if (!getnameinfo((struct sockaddr *) &from, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
301 sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, 0)) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
302 xprintf(" %s (", host); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
303 else xprintf(" %s (", inet_ntoa( |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
304 ((struct sockaddr_in *)&from)->sin_addr)); |
1075 | 305 } |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
306 xprintf(" %s", inet_ntoa( |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
307 ((struct sockaddr_in *)&from)->sin_addr)); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
308 if (!(toys.optflags & FLAG_n)) xprintf(")"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
309 memcpy(&last_addr, &from, sizeof(from)); |
1075 | 310 } |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
311 xprintf(" %u.%03u ms", delta / 1000, delta % 1000); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
312 if (toys.optflags & FLAG_l) xprintf(" (%d)", rcv_pkt->ip_ttl); |
1075 | 313 if (toys.optflags & FLAG_v) { |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
314 xprintf(" %d bytes from %s : icmp type %d code %d\t", |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
315 rcv_len, inet_ntoa(((struct sockaddr_in *)&from)->sin_addr), |
1075 | 316 ricmp->icmp_type, ricmp->icmp_code); |
317 } | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
318 } else xprintf("\t!H"); |
1075 | 319 |
320 switch (icmp_res) { | |
321 case ICMP_UNREACH_PORT: | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
322 if (rcv_pkt->ip_ttl <= 1) xprintf(" !"); |
1075 | 323 dest_reach = 1; |
324 break; | |
325 case ICMP_UNREACH_NET: | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
326 xprintf(" !N"); |
1075 | 327 ++fexit; |
328 break; | |
329 case ICMP_UNREACH_HOST: | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
330 xprintf(" !H"); |
1075 | 331 ++fexit; |
332 break; | |
333 case ICMP_UNREACH_PROTOCOL: | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
334 xprintf(" !P"); |
1075 | 335 dest_reach = 1; |
336 break; | |
337 case ICMP_UNREACH_NEEDFRAG: | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
338 xprintf(" !F-%d", pmtu); |
1075 | 339 ++fexit; |
340 break; | |
341 case ICMP_UNREACH_SRCFAIL: | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
342 xprintf(" !S"); |
1075 | 343 ++fexit; |
344 break; | |
345 case ICMP_UNREACH_FILTER_PROHIB: | |
346 case ICMP_UNREACH_NET_PROHIB: | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
347 xprintf(" !A"); |
1075 | 348 ++fexit; |
349 break; | |
350 case ICMP_UNREACH_HOST_PROHIB: | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
351 xprintf(" !C"); |
1075 | 352 ++fexit; |
353 break; | |
354 case ICMP_UNREACH_HOST_PRECEDENCE: | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
355 xprintf(" !V"); |
1075 | 356 ++fexit; |
357 break; | |
358 case ICMP_UNREACH_PRECEDENCE_CUTOFF: | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
359 xprintf(" !C"); |
1075 | 360 ++fexit; |
361 break; | |
362 case ICMP_UNREACH_NET_UNKNOWN: | |
363 case ICMP_UNREACH_HOST_UNKNOWN: | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
364 xprintf(" !U"); |
1075 | 365 ++fexit; |
366 break; | |
367 case ICMP_UNREACH_ISOLATED: | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
368 xprintf(" !I"); |
1075 | 369 ++fexit; |
370 break; | |
371 case ICMP_UNREACH_TOSNET: | |
372 case ICMP_UNREACH_TOSHOST: | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
373 xprintf(" !T"); |
1075 | 374 ++fexit; |
375 break; | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
376 default: |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
377 break; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
378 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
379 break; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
380 } else { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
381 struct icmp6_hdr *ricmp = (struct icmp6_hdr *) toybuf; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
382 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
383 if ((ricmp->icmp6_type == ICMP6_TIME_EXCEEDED |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
384 && ricmp->icmp6_code == ICMP6_TIME_EXCEED_TRANSIT) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
385 || ricmp->icmp6_type == ICMP6_DST_UNREACH |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
386 || ricmp->icmp6_type == ICMP6_ECHO_REPLY) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
387 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
388 struct ip6_hdr *hip; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
389 struct udphdr *hudp; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
390 int hdr_next; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
391 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
392 hip = (struct ip6_hdr *)(ricmp + 1); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
393 hudp = (struct udphdr*) (hip + 1); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
394 hdr_next = hip->ip6_nxt; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
395 if (hdr_next == IPPROTO_FRAGMENT) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
396 hdr_next = *(unsigned char*)hudp; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
397 hudp++; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
398 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
399 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
400 if (hdr_next == IPPROTO_UDP) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
401 struct payload_s *pkt = (struct payload_s*)(hudp + 1); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
402 if ((pkt->ident == TT.ident) && (pkt->seq == seq)) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
403 icmp_res = (ricmp->icmp6_type == ICMP6_TIME_EXCEEDED) ? -1 : |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
404 ricmp->icmp6_code; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
405 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
406 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
407 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
408 if (!icmp_res) continue; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
409 if (addrlen > 0) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
410 if (memcmp(&((struct sockaddr_in6 *)&last_addr)->sin6_addr, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
411 &((struct sockaddr_in6 *)&from)->sin6_addr, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
412 sizeof(struct in6_addr))) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
413 if (!(toys.optflags & FLAG_n)) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
414 char host[NI_MAXHOST]; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
415 if (!getnameinfo((struct sockaddr *) &from, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
416 sizeof(from), host, sizeof(host), NULL, 0, 0)) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
417 xprintf(" %s (", host); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
418 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
419 memset(addr_str, '\0', INET6_ADDRSTRLEN); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
420 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&from)->sin6_addr, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
421 addr_str, INET6_ADDRSTRLEN); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
422 xprintf(" %s", addr_str); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
423 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
424 if (!(toys.optflags & FLAG_n)) xprintf(")"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
425 memcpy(&last_addr,&from,sizeof(from)); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
426 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
427 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
428 if (toys.optflags & FLAG_v) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
429 if(print_verbose){ |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
430 memset(addr_str, '\0', INET6_ADDRSTRLEN); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
431 inet_ntop(AF_INET6, &((struct sockaddr_in6 *) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
432 &from)->sin6_addr, addr_str, INET6_ADDRSTRLEN); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
433 rcv_len -= sizeof(struct ip6_hdr); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
434 xprintf(" %d bytes to %s ", rcv_len, addr_str); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
435 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
436 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
437 xprintf(" %u.%03u ms", delta / 1000, delta % 1000); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
438 delta = 0; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
439 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
440 } else xprintf("\t!H"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
441 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
442 switch (icmp_res) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
443 case ICMP6_DST_UNREACH_NOPORT: |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
444 ++fexit; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
445 dest_reach = 1; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
446 break; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
447 case ICMP6_DST_UNREACH_NOROUTE: |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
448 xprintf(" !N"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
449 ++fexit; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
450 break; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
451 case ICMP6_DST_UNREACH_ADDR: |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
452 xprintf(" !H"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
453 ++fexit; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
454 break; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
455 case ICMP6_DST_UNREACH_ADMIN: |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
456 xprintf(" !S"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
457 ++fexit; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
458 break; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
459 default: |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
460 break; |
1075 | 461 } |
462 break; | |
463 } | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
464 } //revents |
1075 | 465 } |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
466 print_verbose = 0; |
1075 | 467 } |
468 xputc('\n'); | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
469 if(!TT.istraceroute6) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
470 if (!memcmp(&((struct sockaddr_in *)&from)->sin_addr, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
471 &((struct sockaddr_in *)&dest)->sin_addr, sizeof(struct in_addr)) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
472 || dest_reach || (fexit && fexit >= TT.ttl_probes -1)) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
473 break; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
474 } else if (dest_reach || (fexit > 0 && fexit >= TT.ttl_probes -1)) break; |
1075 | 475 } |
476 } | |
477 | |
478 void traceroute_main(void) | |
479 { | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
480 unsigned opt_len = 0, pack_size = 0, tyser = 0; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
481 int lsrr = 0, set = 1; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
482 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
483 if(!(toys.optflags & FLAG_4) && |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
484 (inet_pton(AF_INET6, toys.optargs[0], (void*)&dest))) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
485 toys.optflags |= FLAG_6; |
1075 | 486 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
487 memset(&dest, 0, sizeof(dest)); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
488 if (toys.optflags & FLAG_6) TT.istraceroute6 = 1; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
489 else TT.istraceroute6 = toys.which->name[10] == '6'; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
490 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
491 if(!TT.istraceroute6 && (toys.optflags & FLAG_g)) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
492 struct arg_list *node; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
493 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
494 for (node = TT.loose_source; node; node = node->next, lsrr++) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
495 struct sockaddr_in sin; |
1075 | 496 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
497 memset( &sin, 0, sizeof(sin)); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
498 if (lsrr >= 8) error_exit("no more than 8 gateways"); // NGATEWAYS |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
499 resolve_addr(node->arg, AF_INET, SOCK_STREAM, 0, &sin); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
500 TT.gw_list[lsrr] = sin.sin_addr.s_addr; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
501 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
502 opt_len = (lsrr + 1) * sizeof(TT.gw_list[0]); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
503 } else TT.first_ttl = 1; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
504 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
505 TT.msg_len = pack_size = ICMP_HD_SIZE4; //udp payload is also 8bytes |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
506 if (toys.optargs[1]) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
507 TT.msg_len = atolx_range(toys.optargs[1], pack_size, 32768);//max packet size |
1075 | 508 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
509 TT.recv_sock = xsocket((TT.istraceroute6 ? AF_INET6 : AF_INET), SOCK_RAW, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
510 (TT.istraceroute6 ? IPPROTO_ICMPV6 : IPPROTO_ICMP)); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
511 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
512 if (TT.istraceroute6) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
513 int two = 2; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
514 #ifdef IPV6_RECVPKTINFO |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
515 setsockopt(TT.recv_sock, SOL_IPV6, IPV6_RECVPKTINFO, &set, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
516 sizeof(set)); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
517 setsockopt(TT.recv_sock, SOL_IPV6, IPV6_2292PKTINFO, &set, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
518 sizeof(set)); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
519 #else |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
520 setsockopt(TT.recv_sock, SOL_IPV6, IPV6_PKTINFO, &set, sizeof(set)); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
521 #endif |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
522 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
523 if (setsockopt(TT.recv_sock, SOL_RAW, IPV6_CHECKSUM, &two, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
524 sizeof(two)) < 0) perror_exit("setsockopt RAW_CHECKSUM"); |
1075 | 525 } |
526 | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
527 set_flag_dr(TT.recv_sock); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
528 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
529 if (!TT.istraceroute6) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
530 if (toys.optflags & FLAG_U) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
531 TT.snd_sock = xsocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
532 else TT.snd_sock = xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
533 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
534 if (toys.optflags & FLAG_i) bind_to_interface(TT.snd_sock); |
1075 | 535 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
536 resolve_addr(toys.optargs[0], AF_INET, ((toys.optflags & FLAG_U) ? |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
537 SOCK_DGRAM : SOCK_RAW), ((toys.optflags & FLAG_U) ? IPPROTO_UDP : |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
538 IPPROTO_ICMP), &dest); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
539 if (lsrr > 0) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
540 unsigned char optlist[MAX_IPOPTLEN]; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
541 unsigned size; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
542 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
543 TT.gw_list[lsrr] = ((struct sockaddr_in *)&dest)->sin_addr.s_addr; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
544 ++lsrr; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
545 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
546 optlist[0] = IPOPT_NOP; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
547 optlist[1] = IPOPT_LSRR;// loose source route option |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
548 size = lsrr * sizeof(TT.gw_list[0]); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
549 optlist[2] = size + 3; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
550 optlist[3] = IPOPT_MINOFF; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
551 memcpy(optlist + 4, TT.gw_list, size); |
1075 | 552 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
553 if (setsockopt(TT.snd_sock, IPPROTO_IP, IP_OPTIONS, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
554 (char *)optlist, size + sizeof(TT.gw_list[0])) < 0) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
555 perror_exit("LSRR IP_OPTIONS"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
556 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
557 } else TT.snd_sock = xsocket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
558 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
559 if (setsockopt(TT.snd_sock, SOL_SOCKET, SO_SNDBUF, &TT.msg_len, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
560 sizeof(TT.msg_len)) < 0) perror_exit("SO_SNDBUF failed "); |
1075 | 561 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
562 if (!TT.istraceroute6) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
563 if ((toys.optflags & FLAG_t) && |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
564 setsockopt(TT.snd_sock, IPPROTO_IP, IP_TOS, &tyser, sizeof(tyser)) < 0) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
565 perror_exit("IP_TOS %d failed ", TT.tos); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
566 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
567 #ifdef IP_DONTFRAG |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
568 if ((toys.optflags & FLAG_F) && |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
569 (setsockopt(TT.snd_sock, IPPROTO_IP, IP_DONTFRAG, &set, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
570 sizeof(set)) < 0)) perror_exit("IP_DONTFRAG failed "); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
571 #endif |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
572 } else if (setsockopt(TT.snd_sock, IPPROTO_IPV6, IPV6_TCLASS, &TT.tos, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
573 sizeof(TT.tos)) < 0) perror_exit("IPV6_TCLASS %d failed ", TT.tos); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
574 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
575 set_flag_dr(TT.snd_sock); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
576 TT.packet = xzalloc(TT.msg_len); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
577 TT.ident = getpid(); |
1075 | 578 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
579 if (!TT.istraceroute6) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
580 if (!(toys.optflags & FLAG_U)) TT.ident |= 0x8000; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
581 if (toys.optflags & FLAG_s) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
582 struct sockaddr_in source; |
1075 | 583 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
584 memset(&source, 0, sizeof(source)); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
585 if (!inet_aton(TT.src_ip, &(source.sin_addr))) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
586 error_exit("bad address: %s", TT.src_ip); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
587 if (setsockopt(TT.snd_sock, IPPROTO_IP, IP_MULTICAST_IF, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
588 (struct sockaddr*)&source, sizeof(struct sockaddr_in))) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
589 perror_exit("can't set multicast source interface"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
590 if (bind(TT.snd_sock,(struct sockaddr*)&source, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
591 sizeof(struct sockaddr_in)) < 0) perror_exit("bind"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
592 } |
1075 | 593 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
594 if(TT.first_ttl > TT.max_ttl) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
595 error_exit("ERROR :Range for -f is 1 to %d (max ttl)", TT.max_ttl); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
596 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
597 xprintf("traceroute to %s(%s)", toys.optargs[0], |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
598 inet_ntoa(((struct sockaddr_in *)&dest)->sin_addr)); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
599 } else { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
600 if (toys.optflags & FLAG_i) bind_to_interface(TT.snd_sock); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
601 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
602 resolve_addr(toys.optargs[0], AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &dest); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
603 if (toys.optflags & FLAG_s) { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
604 struct sockaddr_in6 source; |
1075 | 605 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
606 memset(&source, 0, sizeof(source)); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
607 if(inet_pton(AF_INET6, TT.src_ip, &(source.sin6_addr)) <= 0) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
608 error_exit("bad address: %s", TT.src_ip); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
609 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
610 if (bind(TT.snd_sock,(struct sockaddr*)&source, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
611 sizeof(struct sockaddr_in6)) < 0) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
612 error_exit("bind: Cannot assign requested address"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
613 } else { |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
614 struct sockaddr_in6 prb; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
615 socklen_t len = sizeof(prb); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
616 int p_fd = xsocket(AF_INET6, SOCK_DGRAM, 0); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
617 if (toys.optflags & FLAG_i) bind_to_interface(p_fd); |
1075 | 618 |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
619 ((struct sockaddr_in6 *)&dest)->sin6_port = htons(1025); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
620 if (connect(p_fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_in6)) < 0) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
621 perror_exit("can't connect to remote host"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
622 if(getsockname(p_fd, (struct sockaddr *)&prb, &len)) |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
623 error_exit("probe addr failed"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
624 close(p_fd); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
625 prb.sin6_port = 0; |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
626 if (bind(TT.snd_sock, (struct sockaddr*)&prb, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
627 sizeof(struct sockaddr_in6))) perror_exit("bind"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
628 if (bind(TT.recv_sock, (struct sockaddr*)&prb, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
629 sizeof(struct sockaddr_in6))) perror_exit("bind"); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
630 } |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
631 |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
632 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&dest)->sin6_addr, |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
633 addr_str, INET6_ADDRSTRLEN); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
634 xprintf("traceroute6 to %s(%s)", toys.optargs[0], addr_str); |
1075 | 635 } |
636 | |
1157
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
637 if (toys.optflags & FLAG_s) xprintf(" from %s",TT.src_ip); |
f10898e637ea
IPv6 support is added into traceroute command.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
1104
diff
changeset
|
638 xprintf(", %ld hops max, %u byte packets\n", TT.max_ttl, TT.msg_len); |
1075 | 639 |
640 do_trace(); | |
641 } |