From 3b6a03141f99b6f6c1e9dec6ca35f15b69f71286 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Mon, 21 Nov 2011 20:58:45 -0200 Subject: [PATCH] The IP version used to transport TCP segments can be choosen, IPv4 or IPv6. --- src/host/pingerfactory.cpp | 4 ++++ src/host/pingprotocol.cpp | 1 + src/host/pingprotocol.h | 3 ++- src/tcp/tcppinger.cpp | 11 ++++++----- src/tcp/tcppinger.h | 12 +++++++----- src/tcp/tcpsegmentfactory.cpp | 10 +--------- 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/host/pingerfactory.cpp b/src/host/pingerfactory.cpp index fb85587..51a87f0 100644 --- a/src/host/pingerfactory.cpp +++ b/src/host/pingerfactory.cpp @@ -84,6 +84,10 @@ shared_ptr PingerFactory::createPinger( return shared_ptr( new TcpPinger( io_serv, tcp_raw_protocol::v4(), network_interface, ping_reply_timeout_in_sec ) ); + case PingProtocol_TCP_IPv6: + return shared_ptr( + new TcpPinger( io_serv, tcp_raw_protocol::v6(), network_interface, ping_reply_timeout_in_sec ) + ); default: BOOST_ASSERT( !"Try to create a pinger from an invalid protocol" ); return shared_ptr(); diff --git a/src/host/pingprotocol.cpp b/src/host/pingprotocol.cpp index 548e157..7337947 100644 --- a/src/host/pingprotocol.cpp +++ b/src/host/pingprotocol.cpp @@ -36,6 +36,7 @@ PingProtocol get_ping_protocol_from_string( string protocol_string ) protocol_string_map[ "ICMP" ] = PingProtocol_ICMP; protocol_string_map[ "ICMPv6" ] = PingProtocol_ICMPv6; protocol_string_map[ "TCP" ] = PingProtocol_TCP; + protocol_string_map[ "TCP_IPv6" ] = PingProtocol_TCP_IPv6; return protocol_string_map[ protocol_string ]; } diff --git a/src/host/pingprotocol.h b/src/host/pingprotocol.h index 4ec8715..086c446 100644 --- a/src/host/pingprotocol.h +++ b/src/host/pingprotocol.h @@ -29,7 +29,8 @@ enum PingProtocol PingProtocol_ICMP = PingProtocol_First, PingProtocol_ICMPv6, PingProtocol_TCP, - PingProtocol_Last = PingProtocol_TCP + PingProtocol_TCP_IPv6, + PingProtocol_Last = PingProtocol_TCP_IPv6 }; PingProtocol get_ping_protocol_from_string( std::string protocol_string ); diff --git a/src/tcp/tcppinger.cpp b/src/tcp/tcppinger.cpp index 9a52a7e..99fc96e 100644 --- a/src/tcp/tcppinger.cpp +++ b/src/tcp/tcppinger.cpp @@ -73,7 +73,8 @@ TcpPinger::TcpPinger( ) : IoService( io_serv ), DestinationEndpoint(), - Socket( IoService, protocol ), + Protocol( protocol ), + Socket( IoService, Protocol ), NetInterface( source_network_interface_name, Socket ), TcpSegmentReceiveTimer( IoService ), Identifier( 0 ), @@ -134,7 +135,7 @@ void TcpPinger::ping( address TcpPinger::get_source_address() { - return NetInterface.get_address( tcp_raw_protocol::v4() ); + return NetInterface.get_address( Protocol ); } address TcpPinger::get_destination_address() const @@ -178,7 +179,7 @@ void TcpPinger::start_send() uint16_t source_port = get_source_port(); uint16_t destination_port = get_destination_port(); TcpSegmentItem tcp_segment = TcpSegmentFactory::create_tcp_segment_ack_request( - tcp_raw_protocol::v4(), + Protocol, source_address, destination_address, source_port, destination_port, SequenceNumber ); @@ -207,7 +208,7 @@ void TcpPinger::send_ack_request( const TcpSegmentItem tcp_segment ) if ( bytes_sent != buffer_size( data ) ) { GlobalLogger.error() << "Error: fail sending ping data." - << endl; + << "Amount of bytes sent differs from the buffer." << endl; } } catch ( const exception &ex ) @@ -281,7 +282,7 @@ void TcpPinger::handle_receive_tcp_segment( const size_t &bytes_transferred ) } // Decode the reply segment. - TcpSegmentItem tcp_segment = TcpSegmentFactory::create_tcp_segment( tcp_raw_protocol::v4(), is ); + TcpSegmentItem tcp_segment = TcpSegmentFactory::create_tcp_segment( Protocol, is ); // Filter out only the TCP reset (RST) replies. Note that the sequence // number from RST does not match the sent ACK's sequence number. diff --git a/src/tcp/tcppinger.h b/src/tcp/tcppinger.h index e625114..2f55f26 100644 --- a/src/tcp/tcppinger.h +++ b/src/tcp/tcppinger.h @@ -83,22 +83,24 @@ private: boost::asio::io_service &IoService; /// the destination host boost::asio::ip::tcp_raw_protocol::endpoint DestinationEndpoint; - /// the socket object + /// Network layer protocol used to ping, IPv4 or IPv6 + boost::asio::ip::tcp_raw_protocol::socket::protocol_type Protocol; + /// The socket object boost::asio::ip::tcp_raw_protocol::socket Socket; /// This object represents the network interface NetworkInterface NetInterface; - /// the timer of TCP segment receive, triggers the timeout to avoid infinite + /// The timer of TCP segment receive, triggers the timeout to avoid infinite /// wait boost::asio::deadline_timer TcpSegmentReceiveTimer; /// TCP packet identifier uint16_t Identifier; /// TCP packet sequence_number uint16_t SequenceNumber; - /// the time when the last ICMP packet was sent + /// The time when the last ICMP packet was sent boost::posix_time::ptime TimeSent; - /// the buffer where the data received will be placed + /// The buffer where the data received will be placed boost::asio::streambuf ReplyBuffer; - /// flag to indicate if we got a reply or not + /// A flag to indicate if we got a reply or not bool ReceivedReply; /// the amount of time to wait for the reply int RstReplyTimeoutInSec; diff --git a/src/tcp/tcpsegmentfactory.cpp b/src/tcp/tcpsegmentfactory.cpp index 937c9e2..9a4319a 100644 --- a/src/tcp/tcpsegmentfactory.cpp +++ b/src/tcp/tcpsegmentfactory.cpp @@ -26,9 +26,7 @@ #include "tcp/tcpheader.h" #include "tcp/tcpipv4segment.h" -#ifdef IPV6_WORKING #include "tcp/tcpipv6segment.h" -#endif using namespace std; using boost::asio::ip::address; @@ -62,12 +60,10 @@ TcpSegmentItem TcpSegmentFactory::create_tcp_segment( { tcp_segment.reset( new TcpIpv4Segment() ); } -#ifdef IPV6_WORKING else if ( tcp_raw_protocol::v6() == protocol ) { tcp_segment.reset( new TcpIpv6Segment() ); } -#endif else { BOOST_ASSERT( !"Invalid TCP Segment Type." ); @@ -113,14 +109,12 @@ TcpSegmentItem TcpSegmentFactory::create_tcp_segment_ack_request( source_address, destination_address, source_port, destination_port, sequence_number ); } -#ifdef IPV6_WORKING else if ( tcp_raw_protocol::v6() == protocol ) { tcp_segment = create_tcp_ipv6_segment_ack_request( source_address, destination_address, source_port, destination_port, sequence_number ); } -#endif else { BOOST_ASSERT( !"Invalid TCP Segment Type." ); @@ -198,7 +192,6 @@ TcpSegmentItem TcpSegmentFactory::create_tcp_ipv6_segment_ack_request( const uint16_t sequence_number ) { -#ifdef IPV6_WORKING BOOST_ASSERT( source_address.is_v6() ); BOOST_ASSERT( destination_address.is_v6() ); @@ -224,7 +217,6 @@ TcpSegmentItem TcpSegmentFactory::create_tcp_ipv6_segment_ack_request( tcp_header.set_checksum( cksum ); TcpSegmentItem tcp_segment( new TcpIpv6Segment( tcp_header ) ); -#endif - TcpSegmentItem tcp_segment; + return tcp_segment; } -- 1.7.1