diff --git a/fairmq/FairMQDevice.cxx b/fairmq/FairMQDevice.cxx index 4e79f8c8..c3fe73c2 100644 --- a/fairmq/FairMQDevice.cxx +++ b/fairmq/FairMQDevice.cxx @@ -299,10 +299,15 @@ void FairMQDevice::InitWrapper() // if binding address is not specified, try getting it from the configured network interface if (subChannel.fAddress == "unspecified" || subChannel.fAddress == "") { // if the configured network interface is default, get its name from the default route - if (networkInterface == "default") { - networkInterface = tools::getDefaultRouteNetworkInterface(); + try { + if (networkInterface == "default") { + networkInterface = tools::getDefaultRouteNetworkInterface(); + } + subChannel.fAddress = "tcp://" + tools::getInterfaceIP(networkInterface) + ":1"; + } catch(const tools::DefaultRouteDetectionError& e) { + LOG(debug) << "binding on tcp://*:1"; + subChannel.fAddress = "tcp://*:1"; } - subChannel.fAddress = "tcp://" + tools::getInterfaceIP(networkInterface) + ":1"; } // fill the uninitialized list fUninitializedBindingChannels.push_back(&subChannel); diff --git a/fairmq/tools/Network.cxx b/fairmq/tools/Network.cxx index db82733a..8c6085e2 100644 --- a/fairmq/tools/Network.cxx +++ b/fairmq/tools/Network.cxx @@ -6,28 +6,31 @@ * copied verbatim in the file "LICENSE" * ********************************************************************************/ +#include #include #ifndef _GNU_SOURCE #define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST #endif -#include -#include #include // trim #include -#include -#include -#include + #include -#include -#include #include -#include -#include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include + using namespace std; namespace fair @@ -98,6 +101,30 @@ string getDefaultRouteNetworkInterface() #ifdef __APPLE__ // MacOS unique_ptr file(popen("route -n get default | grep interface | cut -d \":\" -f 2", "r"), pclose); #else // Linux + ifstream is("/proc/net/route"); + string line; + + // discard header + getline(is, line); + + // check each line, until 00000000 destination is found + while (!is.eof()) { + getline(is, line); + size_t pos = line.find('\t'); + + if (pos == string::npos) { + break; + } + + if (line.substr(pos + 1, 8) == "00000000") { + interfaceName = line.substr(0, pos); + LOG(debug) << "Detected network interface name for the default route: " << interfaceName; + return interfaceName; + } + } + + LOG(debug) << "could not get network interface of the default route from /proc/net/route, going to try via 'ip route'"; + unique_ptr file(popen("ip route | grep default | cut -d \" \" -f 5 | head -n 1", "r"), pclose); #endif @@ -115,7 +142,8 @@ string getDefaultRouteNetworkInterface() boost::algorithm::trim(interfaceName); if (interfaceName.empty()) { - LOG(error) << "Could not detect default route network interface name"; + LOG(debug) << "Could not detect default route network interface name from /proc/net/route nor 'ip route'"; + throw DefaultRouteDetectionError("Could not detect default route network interface name from /proc/net/route nor 'ip route'"); } else { LOG(debug) << "Detected network interface name for the default route: " << interfaceName; } diff --git a/fairmq/tools/Network.h b/fairmq/tools/Network.h index e62aff74..ed45ff9e 100644 --- a/fairmq/tools/Network.h +++ b/fairmq/tools/Network.h @@ -11,6 +11,7 @@ #include #include +#include // forward declarations namespace boost @@ -31,6 +32,8 @@ namespace mq namespace tools { +struct DefaultRouteDetectionError : std::runtime_error { using std::runtime_error::runtime_error; }; + // returns a map with network interface names as keys and their IP addresses as values std::map getHostIPs();