Adding logging to ioctl fails, and replacing the nested if statements by guard clause...
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 9 Aug 2011 00:09:35 +0000 (21:09 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 9 Aug 2011 00:09:35 +0000 (21:09 -0300)
src/tcp/tcppinger.cpp

index e4d6b38..34d4580 100644 (file)
@@ -122,26 +122,26 @@ uint32_t TcpPinger::get_source_address()
     // make sure the ifr.ifr_name has enough room to receive the network
     // interface name
     size_t network_interface_name_limit = sizeof(ifr.ifr_name);
-    if ( network_interface_name_limit > SourceNetworkInterfaceName.size() )
+    if ( network_interface_name_limit <= SourceNetworkInterfaceName.size() )
     {
-        strncpy( ifr.ifr_name, SourceNetworkInterfaceName.c_str(), network_interface_name_limit );
-        ifr.ifr_addr.sa_family = AF_INET; // TODO change to AF_INET6 when IPv6
+        GlobalLogger.error() << "Error: network interface name truncated" << endl;
+        return 0;
+    }
 
-        int ioctl_resp = ioctl( Socket.native(), SIOCGIFADDR, &ifr );
-        if ( ioctl_resp == 0)
-        {
-            const sockaddr_in *source_sockaddr = reinterpret_cast<const sockaddr_in *>( &ifr.ifr_addr );
-            uint32_t source_ipv4_address = htonl( source_sockaddr->sin_addr.s_addr );
+    strncpy( ifr.ifr_name, SourceNetworkInterfaceName.c_str(), network_interface_name_limit );
+    ifr.ifr_addr.sa_family = AF_INET; // TODO change to AF_INET6 when IPv6
 
-            return source_ipv4_address;
-        }
-    }
-    else
+    int ioctl_resp = ioctl( Socket.native(), SIOCGIFADDR, &ifr );
+    if ( ioctl_resp != 0)
     {
-        GlobalLogger.error() << "Error: network interface name truncated" << endl;
+        GlobalLogger.error() << "Error: could not retrieve IP address from network interface" << endl;
+        return 0;
     }
 
-    return 0;
+    const sockaddr_in *source_sockaddr = reinterpret_cast<const sockaddr_in *>( &ifr.ifr_addr );
+    uint32_t source_ipv4_address = htonl( source_sockaddr->sin_addr.s_addr );
+
+    return source_ipv4_address;
 }
 
 uint32_t TcpPinger::get_destination_address() const