From: Guilherme Maciel Ferreira Date: Tue, 9 Aug 2011 00:09:35 +0000 (-0300) Subject: Adding logging to ioctl fails, and replacing the nested if statements by guard clause... X-Git-Tag: v1.1^2~26 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=5ecd10bfd513b0a70ee1cfdca8cb0a8335560903;p=pingcheck Adding logging to ioctl fails, and replacing the nested if statements by guard clauses (early returns) --- diff --git a/src/tcp/tcppinger.cpp b/src/tcp/tcppinger.cpp index e4d6b38..34d4580 100644 --- a/src/tcp/tcppinger.cpp +++ b/src/tcp/tcppinger.cpp @@ -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( &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( &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