| 82 | } |
| 83 | |
| 84 | void handle_receive(std::size_t length) |
| 85 | { |
| 86 | // The actual number of bytes received is committed to the buffer so that we |
| 87 | // can extract it using a std::istream object. |
| 88 | reply_buffer_.commit(length); |
| 89 | |
| 90 | // Decode the reply packet. |
| 91 | std::istream is(&reply_buffer_); |
| 92 | ipv4_header ipv4_hdr; |
| 93 | icmp_header icmp_hdr; |
| 94 | is >> ipv4_hdr >> icmp_hdr; |
| 95 | |
| 96 | // We can receive all ICMP packets received by the host, so we need to |
| 97 | // filter out only the echo replies that match the our identifier and |
| 98 | // expected sequence number. |
| 99 | if (is && icmp_hdr.type() == icmp_header::echo_reply |
| 100 | && icmp_hdr.identifier() == get_identifier() |
| 101 | && icmp_hdr.sequence_number() == sequence_number_) |
| 102 | { |
| 103 | // If this is the first reply, interrupt the five second timeout. |
| 104 | if (num_replies_++ == 0) |
| 105 | timer_.cancel(); |
| 106 | |
| 107 | // Print out some information about the reply packet. |
| 108 | chrono::steady_clock::time_point now = chrono::steady_clock::now(); |
| 109 | chrono::steady_clock::duration elapsed = now - time_sent_; |
| 110 | std::cout << length - ipv4_hdr.header_length() |
| 111 | << " bytes from " << ipv4_hdr.source_address() |
| 112 | << ": icmp_seq=" << icmp_hdr.sequence_number() |
| 113 | << ", ttl=" << ipv4_hdr.time_to_live() |
| 114 | << ", time=" |
| 115 | << chrono::duration_cast<chrono::milliseconds>(elapsed).count() |
| 116 | << std::endl; |
| 117 | } |
| 118 | |
| 119 | start_receive(); |
| 120 | } |
| 121 | |
| 122 | static unsigned short get_identifier() |
| 123 | { |
nothing calls this directly
no test coverage detected