get default network interface from the default route

This commit is contained in:
Alexey Rybalchenko 2017-03-01 11:25:42 +01:00 committed by Mohammad Al-Turany
parent eaa220e39e
commit 91b7a72ac5
3 changed files with 58 additions and 5 deletions

View File

@ -185,9 +185,14 @@ void FairMQDevice::InitWrapper()
if (vi->fMethod == "bind") 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 (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"; vi->fAddress = "tcp://" + FairMQ::tools::getInterfaceIP(fNetworkInterface) + ":1";
} }
// fill the uninitialized list // fill the uninitialized list

View File

@ -316,7 +316,7 @@ void FairMQProgOptions::InitOptionDescription()
("transport", po::value<string>()->default_value("zeromq"), "Transport ('zeromq'/'nanomsg').") ("transport", po::value<string>()->default_value("zeromq"), "Transport ('zeromq'/'nanomsg').")
("config", po::value<string>()->default_value("static"), "Config source ('static'/<config library filename>).") ("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>).") ("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") ("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)") ("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") ("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').") ("transport", po::value<string>()->default_value("zeromq"), "Transport ('zeromq'/'nanomsg').")
("config", po::value<string>()->default_value("static"), "Config source ('static'/<config library filename>).") ("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>).") ("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") ("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)") ("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") ("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').") ("transport", po::value<string>()->default_value("zeromq"), "Transport ('zeromq'/'nanomsg').")
("config", po::value<string>()->default_value("static"), "Config source ('static'/<config library filename>).") ("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>).") ("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") ("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)") ("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") ("log-to-file", po::value<string>()->default_value(""), "Log output to a file")

View File

@ -5,16 +5,21 @@
#define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST #define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST
#endif #endif
#include "FairMQLogger.h"
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <netdb.h> #include <netdb.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <stdio.h> #include <stdio.h>
#include <boost/algorithm/string.hpp> // trim
#include <map> #include <map>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <type_traits> #include <type_traits>
#include <array>
using namespace std; using namespace std;
@ -23,12 +28,14 @@ namespace FairMQ
namespace tools namespace tools
{ {
// make_unique implementation, until C++14 is default
template<typename T, typename ...Args> template<typename T, typename ...Args>
unique_ptr<T> make_unique(Args&& ...args) unique_ptr<T> make_unique(Args&& ...args)
{ {
return unique_ptr<T>(new T(forward<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) int getHostIPs(map<string, string>& addressMap)
{ {
struct ifaddrs *ifaddr, *ifa; struct ifaddrs *ifaddr, *ifa;
@ -65,6 +72,7 @@ int getHostIPs(map<string, string>& addressMap)
return 0; return 0;
} }
// get IP address of a given interface name
string getInterfaceIP(string interface) string getInterfaceIP(string interface)
{ {
map<string, string> IPs; map<string, string> IPs;
@ -76,10 +84,50 @@ string getInterfaceIP(string interface)
else else
{ {
LOG(ERROR) << "Could not find provided network interface: \"" << interface << "\"!, exiting."; 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__) #if defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++" #pragma GCC diagnostic ignored "-Weffc++"