mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-13 08:41:16 +00:00
Detect network interface of the default route without use of ip
This commit is contained in:
parent
f2da29a650
commit
15de80cfd3
|
@ -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);
|
||||
|
|
|
@ -6,28 +6,31 @@
|
|||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#include <fairlogger/Logger.h>
|
||||
#include <fairmq/tools/Network.h>
|
||||
|
||||
#ifndef _GNU_SOURCE
|
||||
#define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <boost/algorithm/string.hpp> // trim
|
||||
#include <boost/asio.hpp>
|
||||
#include <cstdio>
|
||||
#include <exception>
|
||||
#include <fairlogger/Logger.h>
|
||||
|
||||
#include <ifaddrs.h>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <netdb.h>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <exception>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace fair
|
||||
|
@ -98,6 +101,30 @@ string getDefaultRouteNetworkInterface()
|
|||
#ifdef __APPLE__ // MacOS
|
||||
unique_ptr<FILE, decltype(pclose) *> 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, decltype(pclose) *> 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;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
// 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<std::string, std::string> getHostIPs();
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user