1 #include "trace.h"
2
3 void
traceloop(void)4 traceloop(void)
5 {
6 int seq, code, done;
7 double rtt;
8 struct rec *rec;
9 struct timeval tvrecv;
10
11 recvfd = Socket(pr->sasend->sa_family, SOCK_RAW, pr->icmpproto);
12 setuid(getuid()); /* don't need special permissions any more */
13
14 sendfd = Socket(pr->sasend->sa_family, SOCK_DGRAM, 0);
15
16 pr->sabind->sa_family = pr->sasend->sa_family;
17 sport = (getpid() & 0xffff) | 0x8000; /* our source UDP port# */
18 sock_set_port(pr->sabind, pr->salen, htons(sport));
19 Bind(sendfd, pr->sabind, pr->salen);
20
21 sig_alrm(SIGALRM);
22
23 seq = 0;
24 done = 0;
25 for (ttl = 1; ttl <= max_ttl && done == 0; ttl++) {
26 Setsockopt(sendfd, pr->ttllevel, pr->ttloptname, &ttl, sizeof(int));
27 bzero(pr->salast, pr->salen);
28
29 printf("%2d ", ttl);
30 fflush(stdout);
31
32 for (probe = 0; probe < nprobes; probe++) {
33 rec = (struct rec *) sendbuf;
34 rec->rec_seq = ++seq;
35 rec->rec_ttl = ttl;
36 Gettimeofday(&rec->rec_tv, NULL);
37
38 sock_set_port(pr->sasend, pr->salen, htons(dport + seq));
39 Sendto(sendfd, sendbuf, datalen, 0, pr->sasend, pr->salen);
40
41 if ( (code = (*pr->recv)(seq, &tvrecv)) == -3)
42 printf(" *"); /* timeout, no reply */
43 else {
44 char str[NI_MAXHOST];
45
46 if (sock_cmp_addr(pr->sarecv, pr->salast, pr->salen) != 0) {
47 if (getnameinfo(pr->sarecv, pr->salen, str, sizeof(str),
48 NULL, 0, 0) == 0)
49 printf(" %s (%s)", str,
50 Sock_ntop_host(pr->sarecv, pr->salen));
51 else
52 printf(" %s",
53 Sock_ntop_host(pr->sarecv, pr->salen));
54 memcpy(pr->salast, pr->sarecv, pr->salen);
55 }
56 tv_sub(&tvrecv, &rec->rec_tv);
57 rtt = tvrecv.tv_sec * 1000.0 + tvrecv.tv_usec / 1000.0;
58 printf(" %.3f ms", rtt);
59
60 if (code == -1) /* port unreachable; at destination */
61 done++;
62 else if (code >= 0)
63 printf(" (ICMP %s)", (*pr->icmpcode)(code));
64 }
65 fflush(stdout);
66 }
67 printf("\n");
68 }
69 }
70