recv_v4.c 2.5 KB

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