mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-13 00:31:14 +00:00
get default network interface from the default route
This commit is contained in:
parent
eaa220e39e
commit
91b7a72ac5
|
@ -185,9 +185,14 @@ void FairMQDevice::InitWrapper()
|
|||
|
||||
if (vi->fMethod == "bind")
|
||||
{
|
||||
// if binding address is not specified, set it up to try getting it from the configured network interface
|
||||
// if binding address is not specified, try getting it from the configured network interface
|
||||
if (vi->fAddress == "unspecified" || vi->fAddress == "")
|
||||
{
|
||||
// if the configured network interface is default, get its name from the default route
|
||||
if (fNetworkInterface == "default")
|
||||
{
|
||||
fNetworkInterface = FairMQ::tools::getDefaultRouteNetworkInterface();
|
||||
}
|
||||
vi->fAddress = "tcp://" + FairMQ::tools::getInterfaceIP(fNetworkInterface) + ":1";
|
||||
}
|
||||
// fill the uninitialized list
|
||||
|
|
|
@ -316,7 +316,7 @@ void FairMQProgOptions::InitOptionDescription()
|
|||
("transport", po::value<string>()->default_value("zeromq"), "Transport ('zeromq'/'nanomsg').")
|
||||
("config", po::value<string>()->default_value("static"), "Config source ('static'/<config library filename>).")
|
||||
("control", po::value<string>()->default_value("interactive"), "States control ('interactive'/'static'/<control library filename>).")
|
||||
("network-interface", po::value<string>()->default_value("eth0"), "Network interface to bind on (e.g. eth0, ib0, wlan0, en0, lo...).")
|
||||
("network-interface", po::value<string>()->default_value("default"), "Network interface to bind on (e.g. eth0, ib0..., default will try to detect the interface of the default route).")
|
||||
("config-key", po::value<string>(), "Use provided value instead of device id for fetching the configuration from the config file")
|
||||
("catch-signals", po::value<int >()->default_value(1), "Enable signal handling (1/0)")
|
||||
("log-to-file", po::value<string>()->default_value(""), "Log output to a file")
|
||||
|
@ -328,7 +328,7 @@ void FairMQProgOptions::InitOptionDescription()
|
|||
("transport", po::value<string>()->default_value("zeromq"), "Transport ('zeromq'/'nanomsg').")
|
||||
("config", po::value<string>()->default_value("static"), "Config source ('static'/<config library filename>).")
|
||||
("control", po::value<string>()->default_value("interactive"), "States control ('interactive'/'static'/<control library filename>).")
|
||||
("network-interface", po::value<string>()->default_value("eth0"), "Network interface to bind on (e.g. eth0, ib0, wlan0, en0, lo...).")
|
||||
("network-interface", po::value<string>()->default_value("default"), "Network interface to bind on (e.g. eth0, ib0..., default will try to detect the interface of the default route).")
|
||||
("config-key", po::value<string>(), "Use provided value instead of device id for fetching the configuration from the config file")
|
||||
("catch-signals", po::value<int >()->default_value(1), "Enable signal handling (1/0)")
|
||||
("log-to-file", po::value<string>()->default_value(""), "Log output to a file")
|
||||
|
@ -342,7 +342,7 @@ void FairMQProgOptions::InitOptionDescription()
|
|||
("transport", po::value<string>()->default_value("zeromq"), "Transport ('zeromq'/'nanomsg').")
|
||||
("config", po::value<string>()->default_value("static"), "Config source ('static'/<config library filename>).")
|
||||
("control", po::value<string>()->default_value("interactive"), "States control ('interactive'/'static'/<control library filename>).")
|
||||
("network-interface", po::value<string>()->default_value("eth0"), "Network interface to bind on (e.g. eth0, ib0, wlan0, en0, lo...).")
|
||||
("network-interface", po::value<string>()->default_value("default"), "Network interface to bind on (e.g. eth0, ib0..., default will try to detect the interface of the default route).")
|
||||
("config-key", po::value<string>(), "Use provided value instead of device id for fetching the configuration from the config file")
|
||||
("catch-signals", po::value<int >()->default_value(1), "Enable signal handling (1/0)")
|
||||
("log-to-file", po::value<string>()->default_value(""), "Log output to a file")
|
||||
|
|
|
@ -5,16 +5,21 @@
|
|||
#define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST
|
||||
#endif
|
||||
|
||||
#include "FairMQLogger.h"
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <netdb.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <boost/algorithm/string.hpp> // trim
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
#include <array>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -23,12 +28,14 @@ namespace FairMQ
|
|||
namespace tools
|
||||
{
|
||||
|
||||
// make_unique implementation, until C++14 is default
|
||||
template<typename T, typename ...Args>
|
||||
unique_ptr<T> make_unique(Args&& ...args)
|
||||
{
|
||||
return unique_ptr<T>(new T(forward<Args>(args)...));
|
||||
}
|
||||
|
||||
// returns a map with network interface names as keys and their IP addresses as values
|
||||
int getHostIPs(map<string, string>& addressMap)
|
||||
{
|
||||
struct ifaddrs *ifaddr, *ifa;
|
||||
|
@ -65,6 +72,7 @@ int getHostIPs(map<string, string>& addressMap)
|
|||
return 0;
|
||||
}
|
||||
|
||||
// get IP address of a given interface name
|
||||
string getInterfaceIP(string interface)
|
||||
{
|
||||
map<string, string> IPs;
|
||||
|
@ -76,10 +84,50 @@ string getInterfaceIP(string interface)
|
|||
else
|
||||
{
|
||||
LOG(ERROR) << "Could not find provided network interface: \"" << interface << "\"!, exiting.";
|
||||
exit(EXIT_FAILURE);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// get name of the default route interface
|
||||
string getDefaultRouteNetworkInterface()
|
||||
{
|
||||
array<char, 128> buffer;
|
||||
string interfaceName;
|
||||
|
||||
#ifdef __APPLE__ // MacOS
|
||||
unique_ptr<FILE, decltype(pclose) *> file(popen("route -n get default | grep interface | cut -d \":\" -f 2", "r"), pclose);
|
||||
#else // Linux
|
||||
unique_ptr<FILE, decltype(pclose) *> file(popen("ip route | grep default | cut -d \" \" -f 5", "r"), pclose);
|
||||
#endif
|
||||
|
||||
if (!file)
|
||||
{
|
||||
LOG(ERROR) << "Could not detect default route network interface name - popen() failed!";
|
||||
return "";
|
||||
}
|
||||
|
||||
while (!feof(file.get()))
|
||||
{
|
||||
if (fgets(buffer.data(), 128, file.get()) != NULL)
|
||||
{
|
||||
interfaceName += buffer.data();
|
||||
}
|
||||
}
|
||||
|
||||
boost::algorithm::trim(interfaceName);
|
||||
|
||||
if (interfaceName == "")
|
||||
{
|
||||
LOG(ERROR) << "Could not detect default route network interface name";
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(DEBUG) << "Detected network interface name for the default route: " << interfaceName;
|
||||
}
|
||||
|
||||
return interfaceName;
|
||||
}
|
||||
|
||||
#if defined(__GNUC__) || defined(__GNUG__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Weffc++"
|
||||
|
|
Loading…
Reference in New Issue
Block a user