forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recv_v4.lc
74 lines (65 loc) · 4.73 KB
/
recv_v4.lc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "trace.h"## 1 ##src/traceroute/recv_v4.c##
/*## 2 ##src/traceroute/recv_v4.c##
* Return: -3 on timeout## 3 ##src/traceroute/recv_v4.c##
* -2 on ICMP time exceeded in transit (caller keeps going)## 4 ##src/traceroute/recv_v4.c##
* -1 on ICMP port unreachable (caller is done)## 5 ##src/traceroute/recv_v4.c##
* >= 0 return value is some other ICMP unreachable code## 6 ##src/traceroute/recv_v4.c##
*/## 7 ##src/traceroute/recv_v4.c##
int## 8 ##src/traceroute/recv_v4.c##
recv_v4(int seq, struct timeval *tv)## 9 ##src/traceroute/recv_v4.c##
{## 10 ##src/traceroute/recv_v4.c##
int hlen1, hlen2, icmplen;## 11 ##src/traceroute/recv_v4.c##
socklen_t len;## 12 ##src/traceroute/recv_v4.c##
ssize_t n;## 13 ##src/traceroute/recv_v4.c##
struct ip *ip, *hip;## 14 ##src/traceroute/recv_v4.c##
struct icmp *icmp;## 15 ##src/traceroute/recv_v4.c##
struct udphdr *udp;## 16 ##src/traceroute/recv_v4.c##
alarm(3);## 17 ##src/traceroute/recv_v4.c##
for (;;) {## 18 ##src/traceroute/recv_v4.c##
len = pr->salen;## 19 ##src/traceroute/recv_v4.c##
n = recvfrom(recvfd, recvbuf, sizeof(recvbuf), 0, pr->sarecv, &len);## 20 ##src/traceroute/recv_v4.c##
if (n < 0) {## 21 ##src/traceroute/recv_v4.c##
if (errno == EINTR)## 22 ##src/traceroute/recv_v4.c##
return (-3); /* alarm expired */## 23 ##src/traceroute/recv_v4.c##
else## 24 ##src/traceroute/recv_v4.c##
err_sys("recvfrom error");## 25 ##src/traceroute/recv_v4.c##
}## 26 ##src/traceroute/recv_v4.c##
Gettimeofday(tv, NULL); /* get time of packet arrival */## 27 ##src/traceroute/recv_v4.c##
ip = (struct ip *) recvbuf; /* start of IP header */## 28 ##src/traceroute/recv_v4.c##
hlen1 = ip->ip_hl << 2; /* length of IP header */## 29 ##src/traceroute/recv_v4.c##
icmp = (struct icmp *) (recvbuf + hlen1); /* start of ICMP header */## 30 ##src/traceroute/recv_v4.c##
if ((icmplen = n - hlen1) < 8)## 31 ##src/traceroute/recv_v4.c##
err_quit("icmplen (%d) < 8", icmplen);## 32 ##src/traceroute/recv_v4.c##
if (icmp->icmp_type == ICMP_TIMXCEED &&## 33 ##src/traceroute/recv_v4.c##
icmp->icmp_code == ICMP_TIMXCEED_INTRANS) {## 34 ##src/traceroute/recv_v4.c##
if (icmplen < 8 + 20 + 8)## 35 ##src/traceroute/recv_v4.c##
err_quit("icmplen (%d) < 8 + 20 + 8", icmplen);## 36 ##src/traceroute/recv_v4.c##
hip = (struct ip *) (recvbuf + hlen1 + 8);## 37 ##src/traceroute/recv_v4.c##
hlen2 = hip->ip_hl << 2;## 38 ##src/traceroute/recv_v4.c##
udp = (struct udphdr *) (recvbuf + hlen1 + 8 + hlen2);## 39 ##src/traceroute/recv_v4.c##
if (hip->ip_p == IPPROTO_UDP &&## 40 ##src/traceroute/recv_v4.c##
udp->uh_sport == htons(sport) &&## 41 ##src/traceroute/recv_v4.c##
udp->uh_dport == htons(dport + seq))## 42 ##src/traceroute/recv_v4.c##
return (-2); /* we hit an intermediate router */## 43 ##src/traceroute/recv_v4.c##
} else if (icmp->icmp_type == ICMP_UNREACH) {## 44 ##src/traceroute/recv_v4.c##
if (icmplen < 8 + 20 + 8)## 45 ##src/traceroute/recv_v4.c##
err_quit("icmplen (%d) < 8 + 20 + 8", icmplen);## 46 ##src/traceroute/recv_v4.c##
hip = (struct ip *) (recvbuf + hlen1 + 8);## 47 ##src/traceroute/recv_v4.c##
hlen2 = hip->ip_hl << 2;## 48 ##src/traceroute/recv_v4.c##
udp = (struct udphdr *) (recvbuf + hlen1 + 8 + hlen2);## 49 ##src/traceroute/recv_v4.c##
if (hip->ip_p == IPPROTO_UDP &&## 50 ##src/traceroute/recv_v4.c##
udp->uh_sport == htons(sport) &&## 51 ##src/traceroute/recv_v4.c##
udp->uh_dport == htons(dport + seq)) {## 52 ##src/traceroute/recv_v4.c##
if (icmp->icmp_code == ICMP_UNREACH_PORT)## 53 ##src/traceroute/recv_v4.c##
return (-1); /* have reached destination */## 54 ##src/traceroute/recv_v4.c##
else## 55 ##src/traceroute/recv_v4.c##
return (icmp->icmp_code); /* 0, 1, 2, ... */## 56 ##src/traceroute/recv_v4.c##
}## 57 ##src/traceroute/recv_v4.c##
} else if (verbose) {## 58 ##src/traceroute/recv_v4.c##
printf(" (from %s: type = %d, code = %d)\n",## 59 ##src/traceroute/recv_v4.c##
Sock_ntop_host(pr->sarecv, pr->salen),## 60 ##src/traceroute/recv_v4.c##
icmp->icmp_type, icmp->icmp_code);## 61 ##src/traceroute/recv_v4.c##
}## 62 ##src/traceroute/recv_v4.c##
/* Some other ICMP error, recvfrom() again */## 63 ##src/traceroute/recv_v4.c##
}## 64 ##src/traceroute/recv_v4.c##
}## 65 ##src/traceroute/recv_v4.c##