ReplyBuffer(),
NumReplies( 0 )
{
-#if 0
- try
- {
- std::string ip = "172.16.1.149";
- uint32_t port = 21;
- icmp::endpoint ep( address::from_string( ip ), port );
- Socket.bind( ep );
- }
- catch (boost::system::system_error &e)
- {
- std::cerr << "exception" << std::endl;
- }
-#endif
}
BoostPinger::~BoostPinger()
void BoostPinger::ping( const Host &host )
{
std::string destination = host.get_address();
- // TODO what if it is an DNS?
- // TODO what about ping multiple hosts?
-
icmp::resolver::query query( icmp::v4(), destination, "" );
DestinationEndpoint = *Resolver.resolve( query );
// Create an ICMP header for an echo request.
IcmpHeader echo_request;
- echo_request.type( IcmpHeader::EchoRequest );
- echo_request.code( 0 );
- echo_request.identifier( get_identifier() );
+ echo_request.set_type( IcmpHeader::EchoRequest );
+ echo_request.set_code( 0 );
+ echo_request.set_identifier( get_identifier() );
SequenceNumber++;
- echo_request.sequence_number( SequenceNumber );
+ echo_request.set_sequence_number( SequenceNumber );
compute_checksum( echo_request, body.begin(), body.end() );
// Encode the request packet.
// We can receive all ICMP packets received by the host, so we need to
// filter out only the echo replies that match the our identifier and
// expected sequence number.
- if ( is && icmp_hdr.type() == IcmpHeader::EchoReply
- && icmp_hdr.identifier() == get_identifier()
- && icmp_hdr.sequence_number() == SequenceNumber )
+ if ( is && icmp_hdr.get_type() == IcmpHeader::EchoReply
+ && icmp_hdr.get_identifier() == get_identifier()
+ && icmp_hdr.get_sequence_number() == SequenceNumber )
{
+
// If this is the first reply, interrupt the five second timeout.
- if ( NumReplies++ == 0 )
+ if ( NumReplies == 0 )
Timer.cancel();
+ NumReplies++;
+
// Print out some information about the reply packet.
posix_time::ptime now = posix_time::microsec_clock::universal_time();
- std::cout << length - ipv4_hdr.header_length() << " bytes from "
- << ipv4_hdr.source_address() << ": icmp_seq="
- << icmp_hdr.sequence_number() << ", ttl="
- << ipv4_hdr.time_to_live() << ", time="
+ std::cout << length - ipv4_hdr.get_header_length() << " bytes from "
+ << ipv4_hdr.get_source_address() << ": icmp_seq="
+ << icmp_hdr.get_sequence_number() << ", ttl="
+ << ipv4_hdr.get_time_to_live() << ", time="
<< (now - TimeSent).total_milliseconds() << " ms" << std::endl;
}
std::fill( Rep, Rep + sizeof(Rep), 0 );
}
-unsigned char IcmpHeader::type() const
+unsigned char IcmpHeader::get_type() const
{
return Rep[ 0 ];
}
-unsigned char IcmpHeader::code() const
+unsigned char IcmpHeader::get_code() const
{
return Rep[ 1 ];
}
-unsigned short IcmpHeader::checksum() const
+unsigned short IcmpHeader::get_checksum() const
{
return decode( 2, 3 );
}
-unsigned short IcmpHeader::identifier() const
+unsigned short IcmpHeader::get_identifier() const
{
return decode( 4, 5 );
}
-unsigned short IcmpHeader::sequence_number() const
+unsigned short IcmpHeader::get_sequence_number() const
{
return decode( 6, 7 );
}
-void IcmpHeader::type( unsigned char n )
+void IcmpHeader::set_type( unsigned char n )
{
Rep[ 0 ] = n;
}
-void IcmpHeader::code( unsigned char n )
+void IcmpHeader::set_code( unsigned char n )
{
Rep[ 1 ] = n;
}
-void IcmpHeader::checksum( unsigned short n )
+void IcmpHeader::set_checksum( unsigned short n )
{
encode( 2, 3, n );
}
-void IcmpHeader::identifier( unsigned short n )
+void IcmpHeader::set_identifier( unsigned short n )
{
encode( 4, 5, n );
}
-void IcmpHeader::sequence_number( unsigned short n )
+void IcmpHeader::set_sequence_number( unsigned short n )
{
encode( 6, 7, n );
}
#include <istream>
#include <ostream>
#include <algorithm>
+
//-----------------------------------------------------------------------------
// IcmpHeader
//-----------------------------------------------------------------------------
IcmpHeader();
- unsigned char type() const;
- unsigned char code() const;
- unsigned short checksum() const;
- unsigned short identifier() const;
- unsigned short sequence_number() const;
+ unsigned char get_type() const;
+ unsigned char get_code() const;
+ unsigned short get_checksum() const;
+ unsigned short get_identifier() const;
+ unsigned short get_sequence_number() const;
- void type( const unsigned char n );
- void code( const unsigned char n );
- void checksum( const unsigned short n );
- void identifier( const unsigned short n );
- void sequence_number( const unsigned short n );
+ void set_type( const unsigned char n );
+ void set_code( const unsigned char n );
+ void set_checksum( const unsigned short n );
+ void set_identifier( const unsigned short n );
+ void set_sequence_number( const unsigned short n );
friend std::istream& operator>>(
std::istream& is,
Iterator body_begin,
Iterator body_end )
{
- unsigned int sum = (header.type() << 8) + header.code()
- + header.identifier() + header.sequence_number();
+ unsigned int sum = (header.get_type() << 8) + header.get_code()
+ + header.get_identifier() + header.get_sequence_number();
Iterator body_iter = body_begin;
while ( body_iter != body_end )
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
- header.checksum( static_cast<unsigned short> ( ~sum ) );
+ header.set_checksum( static_cast<unsigned short> ( ~sum ) );
}
-#endif // ICMP_HEADER_HPP
+#endif // ICMP_HEADER_H
std::fill( Rep, Rep + sizeof(Rep), 0 );
}
-unsigned char Ipv4Header::version() const
+unsigned char Ipv4Header::get_version() const
{
return (Rep[ 0 ] >> 4) & 0xF;
}
-unsigned short Ipv4Header::header_length() const
+unsigned short Ipv4Header::get_header_length() const
{
return (Rep[ 0 ] & 0xF) * 4;
}
-unsigned char Ipv4Header::type_of_service() const
+unsigned char Ipv4Header::get_type_of_service() const
{
return Rep[ 1 ];
}
-unsigned short Ipv4Header::total_length() const
+unsigned short Ipv4Header::get_total_length() const
{
return decode( 2, 3 );
}
-unsigned short Ipv4Header::identification() const
+unsigned short Ipv4Header::get_identification() const
{
return decode( 4, 5 );
}
return (Rep[ 6 ] & 0x40) != 0;
}
-bool Ipv4Header::more_fragments() const
+bool Ipv4Header::have_more_fragments() const
{
return (Rep[ 6 ] & 0x20) != 0;
}
-unsigned short Ipv4Header::fragment_offset() const
+unsigned short Ipv4Header::get_fragment_offset() const
{
return decode( 6, 7 ) & 0x1FFF;
}
-unsigned int Ipv4Header::time_to_live() const
+unsigned int Ipv4Header::get_time_to_live() const
{
return Rep[ 8 ];
}
-unsigned char Ipv4Header::protocol() const
+unsigned char Ipv4Header::get_protocol() const
{
return Rep[ 9 ];
}
-unsigned short Ipv4Header::header_checksum() const
+unsigned short Ipv4Header::get_header_checksum() const
{
return decode( 10, 11 );
}
-boost::asio::ip::address_v4 Ipv4Header::source_address() const
+boost::asio::ip::address_v4 Ipv4Header::get_source_address() const
{
boost::asio::ip::address_v4::bytes_type bytes = { {
Rep[ 12 ],
return boost::asio::ip::address_v4( bytes );
}
-boost::asio::ip::address_v4 Ipv4Header::destination_address() const
+boost::asio::ip::address_v4 Ipv4Header::get_destination_address() const
{
boost::asio::ip::address_v4::bytes_type bytes = { {
Rep[ 16 ],
std::istream& operator>>( std::istream& is, Ipv4Header& header )
{
is.read( reinterpret_cast<char*> ( header.Rep ), 20 );
- if ( header.version() != 4 )
+ if ( header.get_version() != 4 )
is.setstate( std::ios::failbit );
- std::streamsize options_length = header.header_length() - 20;
+ std::streamsize options_length = header.get_header_length() - 20;
if ( options_length < 0 || options_length > 40 )
is.setstate( std::ios::failbit );
else
public:
Ipv4Header();
- unsigned char version() const;
- unsigned short header_length() const;
- unsigned char type_of_service() const;
- unsigned short total_length() const;
- unsigned short identification() const;
+ unsigned char get_version() const;
+ unsigned short get_header_length() const;
+ unsigned char get_type_of_service() const;
+ unsigned short get_total_length() const;
+ unsigned short get_identification() const;
bool dont_fragment() const;
- bool more_fragments() const;
- unsigned short fragment_offset() const;
- unsigned int time_to_live() const;
- unsigned char protocol() const;
- unsigned short header_checksum() const;
+ bool have_more_fragments() const;
+ unsigned short get_fragment_offset() const;
+ unsigned int get_time_to_live() const;
+ unsigned char get_protocol() const;
+ unsigned short get_header_checksum() const;
- boost::asio::ip::address_v4 source_address() const;
- boost::asio::ip::address_v4 destination_address() const;
+ boost::asio::ip::address_v4 get_source_address() const;
+ boost::asio::ip::address_v4 get_destination_address() const;
friend std::istream& operator>>( std::istream& is, Ipv4Header& header );
unsigned char Rep[ 60 ];
};
-#endif // IPV4_HEADER_HPP
+#endif // IPV4_HEADER_H