From: Guilherme Maciel Ferreira Date: Thu, 24 Feb 2011 13:46:58 +0000 (+0100) Subject: Changed getter and setter methods names to keep standardized (and verb+object advice... X-Git-Tag: v1.0~184 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=c85c0309cfa3a74042902942b1bf26ecacc5fdb9;p=pingcheck Changed getter and setter methods names to keep standardized (and verb+object advice from chapter 7 of code complete) --- diff --git a/src/ping/boostpinger.cpp b/src/ping/boostpinger.cpp index af7d321..73a2df6 100644 --- a/src/ping/boostpinger.cpp +++ b/src/ping/boostpinger.cpp @@ -29,19 +29,6 @@ BoostPinger::BoostPinger( 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() @@ -51,9 +38,6 @@ 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 ); @@ -67,11 +51,11 @@ void BoostPinger::start_send() // 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. @@ -126,20 +110,23 @@ void BoostPinger::handle_receive( std::size_t length ) // 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; } diff --git a/src/ping/icmp_header.cpp b/src/ping/icmp_header.cpp index 1305440..1540891 100644 --- a/src/ping/icmp_header.cpp +++ b/src/ping/icmp_header.cpp @@ -9,52 +9,52 @@ IcmpHeader::IcmpHeader() 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 ); } diff --git a/src/ping/icmp_header.h b/src/ping/icmp_header.h index f090aa2..dc9ddb4 100644 --- a/src/ping/icmp_header.h +++ b/src/ping/icmp_header.h @@ -4,6 +4,7 @@ #include #include #include + //----------------------------------------------------------------------------- // IcmpHeader //----------------------------------------------------------------------------- @@ -44,17 +45,17 @@ public: 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, @@ -80,8 +81,8 @@ template 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 ) @@ -93,7 +94,7 @@ template sum = (sum >> 16) + (sum & 0xFFFF); sum += (sum >> 16); - header.checksum( static_cast ( ~sum ) ); + header.set_checksum( static_cast ( ~sum ) ); } -#endif // ICMP_HEADER_HPP +#endif // ICMP_HEADER_H diff --git a/src/ping/ipv4_header.cpp b/src/ping/ipv4_header.cpp index deb3e80..4532afc 100644 --- a/src/ping/ipv4_header.cpp +++ b/src/ping/ipv4_header.cpp @@ -9,27 +9,27 @@ Ipv4Header::Ipv4Header() 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 ); } @@ -39,32 +39,32 @@ bool Ipv4Header::dont_fragment() const 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 ], @@ -74,7 +74,7 @@ boost::asio::ip::address_v4 Ipv4Header::source_address() const 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 ], @@ -87,9 +87,9 @@ boost::asio::ip::address_v4 Ipv4Header::destination_address() const std::istream& operator>>( std::istream& is, Ipv4Header& header ) { is.read( reinterpret_cast ( 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 diff --git a/src/ping/ipv4_header.h b/src/ping/ipv4_header.h index a681afa..11ca6a7 100644 --- a/src/ping/ipv4_header.h +++ b/src/ping/ipv4_header.h @@ -46,21 +46,21 @@ class Ipv4Header 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 ); @@ -70,4 +70,4 @@ private: unsigned char Rep[ 60 ]; }; -#endif // IPV4_HEADER_HPP +#endif // IPV4_HEADER_H