Ditch the sysvinit stuff
[profile/ivi/iputils.git] / tracepath.c
1 /*
2  * tracepath.c
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <sys/socket.h>
16 #include <linux/types.h>
17 #include <linux/errqueue.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <netdb.h>
21 #include <netinet/in.h>
22 #include <resolv.h>
23 #include <sys/time.h>
24 #include <sys/uio.h>
25 #include <arpa/inet.h>
26
27 #ifndef IP_PMTUDISC_PROBE
28 #define IP_PMTUDISC_PROBE       3
29 #endif
30
31 struct hhistory
32 {
33         int     hops;
34         struct timeval sendtime;
35 };
36
37 struct hhistory his[64];
38 int hisptr;
39
40 struct sockaddr_in target;
41 __u16 base_port;
42
43 const int overhead = 28;
44 int mtu = 65535;
45 int hops_to = -1;
46 int hops_from = -1;
47 int no_resolve = 0;
48 int show_both = 0;
49
50 #define HOST_COLUMN_SIZE        52
51
52 struct probehdr
53 {
54         __u32 ttl;
55         struct timeval tv;
56 };
57
58 void data_wait(int fd)
59 {
60         fd_set fds;
61         struct timeval tv;
62         FD_ZERO(&fds);
63         FD_SET(fd, &fds);
64         tv.tv_sec = 1;
65         tv.tv_usec = 0;
66         select(fd+1, &fds, NULL, NULL, &tv);
67 }
68
69 void print_host(const char *a, const char *b, int both)
70 {
71         size_t plen = 0;
72         printf("%s", a);
73         plen = strlen(a);
74         if (both) {
75                 printf(" (%s)", b);
76                 plen += strlen(b) + 3;
77         }
78         if (plen >= HOST_COLUMN_SIZE)
79                 plen = HOST_COLUMN_SIZE - 1;
80         printf("%*s", HOST_COLUMN_SIZE - plen, "");
81 }
82
83 int recverr(int fd, int ttl)
84 {
85         int res;
86         struct probehdr rcvbuf;
87         char cbuf[512];
88         struct iovec  iov;
89         struct msghdr msg;
90         struct cmsghdr *cmsg;
91         struct sock_extended_err *e;
92         struct sockaddr_in addr;
93         struct timeval tv;
94         struct timeval *rettv;
95         int slot;
96         int rethops;
97         int sndhops;
98         int progress = -1;
99         int broken_router;
100
101 restart:
102         memset(&rcvbuf, -1, sizeof(rcvbuf));
103         iov.iov_base = &rcvbuf;
104         iov.iov_len = sizeof(rcvbuf);
105         msg.msg_name = (__u8*)&addr;
106         msg.msg_namelen = sizeof(addr);
107         msg.msg_iov = &iov;
108         msg.msg_iovlen = 1;
109         msg.msg_flags = 0;
110         msg.msg_control = cbuf;
111         msg.msg_controllen = sizeof(cbuf);
112
113         gettimeofday(&tv, NULL);
114         res = recvmsg(fd, &msg, MSG_ERRQUEUE);
115         if (res < 0) {
116                 if (errno == EAGAIN)
117                         return progress;
118                 goto restart;
119         }
120
121         progress = mtu;
122
123         rethops = -1;
124         sndhops = -1;
125         e = NULL;
126         rettv = NULL;
127         slot = ntohs(addr.sin_port) - base_port;
128         if (slot>=0 && slot < 63 && his[slot].hops) {
129                 sndhops = his[slot].hops;
130                 rettv = &his[slot].sendtime;
131                 his[slot].hops = 0;
132         }
133         broken_router = 0;
134         if (res == sizeof(rcvbuf)) {
135                 if (rcvbuf.ttl == 0 || rcvbuf.tv.tv_sec == 0) {
136                         broken_router = 1;
137                 } else {
138                         sndhops = rcvbuf.ttl;
139                         rettv = &rcvbuf.tv;
140                 }
141         }
142
143         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
144                 if (cmsg->cmsg_level == SOL_IP) {
145                         if (cmsg->cmsg_type == IP_RECVERR) {
146                                 e = (struct sock_extended_err *) CMSG_DATA(cmsg);
147                         } else if (cmsg->cmsg_type == IP_TTL) {
148                                 rethops = *(int*)CMSG_DATA(cmsg);
149                         } else {
150                                 printf("cmsg:%d\n ", cmsg->cmsg_type);
151                         }
152                 }
153         }
154         if (e == NULL) {
155                 printf("no info\n");
156                 return 0;
157         }
158         if (e->ee_origin == SO_EE_ORIGIN_LOCAL) {
159                 printf("%2d?: %*s ", ttl, -(HOST_COLUMN_SIZE - 1), "[LOCALHOST]");
160         } else if (e->ee_origin == SO_EE_ORIGIN_ICMP) {
161                 char abuf[128];
162                 struct sockaddr_in *sin = (struct sockaddr_in*)(e+1);
163                 struct hostent *h = NULL;
164
165                 inet_ntop(AF_INET, &sin->sin_addr, abuf, sizeof(abuf));
166
167                 if (sndhops>0)
168                         printf("%2d:  ", sndhops);
169                 else
170                         printf("%2d?: ", ttl);
171
172                 if (!no_resolve || show_both) {
173                         fflush(stdout);
174                         h = gethostbyaddr((char *) &sin->sin_addr, sizeof(sin->sin_addr), AF_INET);
175                 }
176
177                 if (no_resolve)
178                         print_host(abuf, h ? h->h_name : abuf, show_both);
179                 else
180                         print_host(h ? h->h_name : abuf, abuf, show_both);
181         }
182
183         if (rettv) {
184                 int diff = (tv.tv_sec-rettv->tv_sec)*1000000+(tv.tv_usec-rettv->tv_usec);
185                 printf("%3d.%03dms ", diff/1000, diff%1000);
186                 if (broken_router)
187                         printf("(This broken router returned corrupted payload) ");
188         }
189
190         switch (e->ee_errno) {
191         case ETIMEDOUT:
192                 printf("\n");
193                 break;
194         case EMSGSIZE:
195                 printf("pmtu %d\n", e->ee_info);
196                 mtu = e->ee_info;
197                 progress = mtu;
198                 break;
199         case ECONNREFUSED:
200                 printf("reached\n");
201                 hops_to = sndhops<0 ? ttl : sndhops;
202                 hops_from = rethops;
203                 return 0;
204         case EPROTO:
205                 printf("!P\n");
206                 return 0;
207         case EHOSTUNREACH:
208                 if (e->ee_origin == SO_EE_ORIGIN_ICMP &&
209                     e->ee_type == 11 &&
210                     e->ee_code == 0) {
211                         if (rethops>=0) {
212                                 if (rethops<=64)
213                                         rethops = 65-rethops;
214                                 else if (rethops<=128)
215                                         rethops = 129-rethops;
216                                 else
217                                         rethops = 256-rethops;
218                                 if (sndhops>=0 && rethops != sndhops)
219                                         printf("asymm %2d ", rethops);
220                                 else if (sndhops<0 && rethops != ttl)
221                                         printf("asymm %2d ", rethops);
222                         }
223                         printf("\n");
224                         break;
225                 }
226                 printf("!H\n");
227                 return 0;
228         case ENETUNREACH:
229                 printf("!N\n");
230                 return 0;
231         case EACCES:
232                 printf("!A\n");
233                 return 0;
234         default:
235                 printf("\n");
236                 errno = e->ee_errno;
237                 perror("NET ERROR");
238                 return 0;
239         }
240         goto restart;
241 }
242
243 int probe_ttl(int fd, int ttl)
244 {
245         int i;
246         char sndbuf[mtu];
247         struct probehdr *hdr = (struct probehdr*)sndbuf;
248
249         memset(sndbuf,0,mtu);
250
251 restart:
252         for (i=0; i<10; i++) {
253                 int res;
254
255                 hdr->ttl = ttl;
256                 target.sin_port = htons(base_port + hisptr);
257                 gettimeofday(&hdr->tv, NULL);
258                 his[hisptr].hops = ttl;
259                 his[hisptr].sendtime = hdr->tv;
260                 if (sendto(fd, sndbuf, mtu-overhead, 0, (struct sockaddr*)&target, sizeof(target)) > 0)
261                         break;
262                 res = recverr(fd, ttl);
263                 his[hisptr].hops = 0;
264                 if (res==0)
265                         return 0;
266                 if (res > 0)
267                         goto restart;
268         }
269         hisptr = (hisptr + 1)&63;
270
271         if (i<10) {
272                 data_wait(fd);
273                 if (recv(fd, sndbuf, sizeof(sndbuf), MSG_DONTWAIT) > 0) {
274                         printf("%2d?: reply received 8)\n", ttl);
275                         return 0;
276                 }
277                 return recverr(fd, ttl);
278         }
279
280         printf("%2d:  send failed\n", ttl);
281         return 0;
282 }
283
284 static void usage(void) __attribute((noreturn));
285
286 static void usage(void)
287 {
288         fprintf(stderr, "Usage: tracepath [-n] [-b] [-l <len>] <destination>[/<port>]\n");
289         exit(-1);
290 }
291
292 int
293 main(int argc, char **argv)
294 {
295         struct hostent *he;
296         int fd;
297         int on;
298         int ttl;
299         char *p;
300         int ch;
301
302         while ((ch = getopt(argc, argv, "nbh?l:")) != EOF) {
303                 switch(ch) {
304                 case 'n':
305                         no_resolve = 1;
306                         break;
307                 case 'b':
308                         show_both = 1;
309                         break;
310                 case 'l':
311                         if ((mtu = atoi(optarg)) <= overhead) {
312                                 fprintf(stderr, "Error: length must be >= %d\n", overhead);
313                                 exit(1);
314                         }
315                         break;
316                 default:
317                         usage();
318                 }
319         }
320
321         argc -= optind;
322         argv += optind;
323
324         if (argc != 1)
325                 usage();
326
327
328         fd = socket(AF_INET, SOCK_DGRAM, 0);
329         if (fd < 0) {
330                 perror("socket");
331                 exit(1);
332         }
333         target.sin_family = AF_INET;
334
335         p = strchr(argv[0], '/');
336         if (p) {
337                 *p = 0;
338                 base_port = atoi(p+1);
339         } else
340                 base_port = 44444;
341         he = gethostbyname(argv[0]);
342         if (he == NULL) {
343                 herror("gethostbyname");
344                 exit(1);
345         }
346         memcpy(&target.sin_addr, he->h_addr, 4);
347
348         on = IP_PMTUDISC_PROBE;
349         if (setsockopt(fd, SOL_IP, IP_MTU_DISCOVER, &on, sizeof(on)) &&
350             (on = IP_PMTUDISC_DO,
351              setsockopt(fd, SOL_IP, IP_MTU_DISCOVER, &on, sizeof(on)))) {
352                 perror("IP_MTU_DISCOVER");
353                 exit(1);
354         }
355         on = 1;
356         if (setsockopt(fd, SOL_IP, IP_RECVERR, &on, sizeof(on))) {
357                 perror("IP_RECVERR");
358                 exit(1);
359         }
360         if (setsockopt(fd, SOL_IP, IP_RECVTTL, &on, sizeof(on))) {
361                 perror("IP_RECVTTL");
362                 exit(1);
363         }
364
365         for (ttl=1; ttl<32; ttl++) {
366                 int res;
367                 int i;
368
369                 on = ttl;
370                 if (setsockopt(fd, SOL_IP, IP_TTL, &on, sizeof(on))) {
371                         perror("IP_TTL");
372                         exit(1);
373                 }
374
375 restart:
376                 for (i=0; i<3; i++) {
377                         int old_mtu;
378
379                         old_mtu = mtu;
380                         res = probe_ttl(fd, ttl);
381                         if (mtu != old_mtu)
382                                 goto restart;
383                         if (res == 0)
384                                 goto done;
385                         if (res > 0)
386                                 break;
387                 }
388
389                 if (res < 0)
390                         printf("%2d:  no reply\n", ttl);
391         }
392         printf("     Too many hops: pmtu %d\n", mtu);
393 done:
394         printf("     Resume: pmtu %d ", mtu);
395         if (hops_to>=0)
396                 printf("hops %d ", hops_to);
397         if (hops_from>=0)
398                 printf("back %d ", hops_from);
399         printf("\n");
400         exit(0);
401 }