|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <boost/asio.hpp> |
| 4 | +#include <boost/bind.hpp> |
| 5 | +#include <istream> |
| 6 | +#include <iostream> |
| 7 | +#include <ostream> |
| 8 | + |
| 9 | +#include <net/icmp_header.hpp> |
| 10 | +#include <net/ipv4_header.hpp> |
| 11 | + |
| 12 | +using boost::asio::ip::icmp; |
| 13 | +using boost::asio::deadline_timer; |
| 14 | +namespace posix_time = boost::posix_time; |
| 15 | + |
| 16 | +struct result_container { |
| 17 | + result_container() : num_send_(0), num_replies_(0), num_timeouts_(0), length_(0), sequence_number_(0), ttl_(0), time_(0) {} |
| 18 | + |
| 19 | + std::string destination_; |
| 20 | + std::string ip_; |
| 21 | + std::size_t num_send_; |
| 22 | + std::size_t num_replies_; |
| 23 | + std::size_t num_timeouts_; |
| 24 | + std::size_t length_; |
| 25 | + unsigned short sequence_number_; |
| 26 | + unsigned short ttl_; |
| 27 | + std::size_t time_; |
| 28 | +}; |
| 29 | +class pinger { |
| 30 | +public: |
| 31 | + pinger(boost::asio::io_service& io_service, result_container &result, const char* destination, int timeout) |
| 32 | + : resolver_(io_service) |
| 33 | + , socket_(io_service, icmp::v4()) |
| 34 | + , timer_(io_service) |
| 35 | + , sequence_number_(0) |
| 36 | + , timeout_(timeout) |
| 37 | + , result_(result) |
| 38 | + |
| 39 | + { |
| 40 | + icmp::resolver::query query(icmp::v4(), destination, ""); |
| 41 | + destination_ = *resolver_.resolve(query); |
| 42 | + result.destination_ = destination; |
| 43 | + result.ip_ = destination_.address().to_string(); |
| 44 | + } |
| 45 | + |
| 46 | + void ping() { |
| 47 | + start_send(); |
| 48 | + start_receive(); |
| 49 | + } |
| 50 | + |
| 51 | +private: |
| 52 | + void start_send() { |
| 53 | + std::string body("Hello from NSClient++."); |
| 54 | + |
| 55 | + icmp_header echo_request; |
| 56 | + echo_request.type(icmp_header::echo_request); |
| 57 | + echo_request.code(0); |
| 58 | + echo_request.identifier(get_identifier()); |
| 59 | + echo_request.sequence_number(++sequence_number_); |
| 60 | + compute_checksum(echo_request, body.begin(), body.end()); |
| 61 | + |
| 62 | + boost::asio::streambuf request_buffer; |
| 63 | + std::ostream os(&request_buffer); |
| 64 | + os << echo_request << body; |
| 65 | + |
| 66 | + result_.num_send_++; |
| 67 | + time_sent_ = posix_time::microsec_clock::universal_time(); |
| 68 | + socket_.send_to(request_buffer.data(), destination_); |
| 69 | + |
| 70 | + timer_.expires_at(time_sent_ + posix_time::millisec(timeout_)); |
| 71 | + timer_.async_wait(boost::bind(&pinger::handle_timeout, this, boost::asio::placeholders::error)); |
| 72 | + } |
| 73 | + |
| 74 | + void handle_timeout(const boost::system::error_code ec) { |
| 75 | + if (ec != boost::asio::error::operation_aborted) { |
| 76 | + result_.num_timeouts_++; |
| 77 | + socket_.cancel(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + void start_receive() { |
| 82 | + reply_buffer_.consume(reply_buffer_.size()); |
| 83 | + socket_.async_receive(reply_buffer_.prepare(65536), boost::bind(&pinger::handle_receive, this, _2, boost::asio::placeholders::error)); |
| 84 | + } |
| 85 | + |
| 86 | + void handle_receive(std::size_t length, const boost::system::error_code ec) { |
| 87 | + if (ec == boost::asio::error::operation_aborted) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + reply_buffer_.commit(length); |
| 93 | + |
| 94 | + std::istream is(&reply_buffer_); |
| 95 | + ipv4_header ipv4_hdr; |
| 96 | + icmp_header icmp_hdr; |
| 97 | + is >> ipv4_hdr >> icmp_hdr; |
| 98 | + |
| 99 | + if (is |
| 100 | + && icmp_hdr.type() == icmp_header::echo_reply |
| 101 | + && icmp_hdr.identifier() == get_identifier() |
| 102 | + && icmp_hdr.sequence_number() == sequence_number_) |
| 103 | + { |
| 104 | + timer_.cancel(); |
| 105 | + result_.num_replies_++; |
| 106 | + |
| 107 | + posix_time::ptime now = posix_time::microsec_clock::universal_time(); |
| 108 | + result_.length_ = length - ipv4_hdr.header_length(); |
| 109 | + result_.ttl_ = ipv4_hdr.time_to_live(); |
| 110 | + result_.time_ = (now - time_sent_).total_milliseconds(); |
| 111 | + return; |
| 112 | + } |
| 113 | + //start_receive(); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + static unsigned short get_identifier() { |
| 118 | +#if defined(BOOST_WINDOWS) |
| 119 | + return static_cast<unsigned short>(::GetCurrentProcessId()); |
| 120 | +#else |
| 121 | + return static_cast<unsigned short>(::getpid()); |
| 122 | +#endif |
| 123 | + } |
| 124 | + |
| 125 | + icmp::resolver resolver_; |
| 126 | + icmp::endpoint destination_; |
| 127 | + icmp::socket socket_; |
| 128 | + deadline_timer timer_; |
| 129 | + unsigned short sequence_number_; |
| 130 | + posix_time::ptime time_sent_; |
| 131 | + boost::asio::streambuf reply_buffer_; |
| 132 | + result_container& result_; |
| 133 | + int timeout_; |
| 134 | +}; |
0 commit comments