Surrounded the receive by a try..catch, so prepare to receive another packet only...
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 13 Aug 2011 00:42:05 +0000 (21:42 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 13 Aug 2011 00:42:05 +0000 (21:42 -0300)
src/tcp/tcppinger.cpp

index ca22f07..b5247d5 100644 (file)
@@ -292,35 +292,45 @@ void TcpPinger::handle_receive_tcp_segment( const size_t &bytes_transferred )
     // can extract it using a std::istream object.
     ReplyBuffer.commit( bytes_transferred );
 
-    istream is( &ReplyBuffer );
-    if ( !is )
+    try
     {
-        GlobalLogger.error() << "Error: can't handle ReplyBuffer" << endl;
-        return;
-    }
+        istream is( &ReplyBuffer );
+        if ( !is )
+        {
+            GlobalLogger.error() << "Error: can't handle ReplyBuffer" << endl;
+            return;
+        }
 
-    Ipv4Header ipv4_header;
-    TcpHeader tcp_header;
-    is >> ipv4_header >> tcp_header;
+        Ipv4Header ipv4_header;
+        TcpHeader tcp_header;
+        is >> ipv4_header >> tcp_header;
 
-    // filter out only the TCP reset (RST) replies. Note that the sequence
-    // number from RST does not match the sent ACK's sequence number.
-    if ( is &&
-         tcp_header.reset() &&
-         ipv4_header.get_source_address() == DestinationEndpoint.address() )
-    {
-        ReceivedReply = true;
+        // filter out only the TCP reset (RST) replies. Note that the sequence
+        // number from RST does not match the sent ACK's sequence number.
+        if ( is &&
+             tcp_header.reset() &&
+             ipv4_header.get_source_address() == DestinationEndpoint.address() )
+        {
+            ReceivedReply = true;
 
-        print_rst_reply( ipv4_header, tcp_header );
+            print_rst_reply( ipv4_header, tcp_header );
 
-        set_ping_status( PingStatus_SuccessReply );
+            set_ping_status( PingStatus_SuccessReply );
 
-        TcpSegmentReceiveTimer.cancel();
+            TcpSegmentReceiveTimer.cancel();
+        }
+        // Unknown TCP reply, start another receive till timeout
+        else
+        {
+            start_receive();
+        }
+    }
+    catch ( ... )
+    {
+        GlobalLogger.notice() << "Warning: exception during ICMP parse. "
+            << "Starting another receive till timeout." << endl;
+        start_receive();
     }
-
-    // TODO need to be in the catch or else?
-    // TODO why does the ping fail? Does the callback receive false?
-    start_receive();
 }
 
 /**