proc_v4.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "ping.h"
  2. void
  3. proc_v4(char *ptr, ssize_t len, struct msghdr *msg, struct timeval *tvrecv)
  4. {
  5. int hlen1, icmplen;
  6. double rtt;
  7. struct ip *ip;
  8. struct icmp *icmp;
  9. struct timeval *tvsend;
  10. ip = (struct ip *) ptr; /* start of IP header */
  11. hlen1 = ip->ip_hl << 2; /* length of IP header */
  12. if (ip->ip_p != IPPROTO_ICMP)
  13. return; /* not ICMP */
  14. icmp = (struct icmp *) (ptr + hlen1); /* start of ICMP header */
  15. if ( (icmplen = len - hlen1) < 8)
  16. return; /* malformed packet */
  17. if (icmp->icmp_type == ICMP_ECHOREPLY) {
  18. if (icmp->icmp_id != pid)
  19. return; /* not a response to our ECHO_REQUEST */
  20. if (icmplen < 16)
  21. return; /* not enough data to use */
  22. tvsend = (struct timeval *) icmp->icmp_data;
  23. tv_sub(tvrecv, tvsend);
  24. rtt = tvrecv->tv_sec * 1000.0 + tvrecv->tv_usec / 1000.0;
  25. printf("%d bytes from %s: seq=%u, ttl=%d, rtt=%.3f ms\n",
  26. icmplen, Sock_ntop_host(pr->sarecv, pr->salen),
  27. icmp->icmp_seq, ip->ip_ttl, rtt);
  28. } else if (verbose) {
  29. printf(" %d bytes from %s: type = %d, code = %d\n",
  30. icmplen, Sock_ntop_host(pr->sarecv, pr->salen),
  31. icmp->icmp_type, icmp->icmp_code);
  32. }
  33. }