diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp index 9ad7c52fa05c2c7512ded6412e40a524938768d2..8ee17c4ffdd0eccac9cc95d27c979db7aa3be1f0 100644 --- a/src/daemon/daemon.cpp +++ b/src/daemon/daemon.cpp @@ -40,6 +40,11 @@ #include #include +#include +#include +#include +#include +#include #ifdef GKFS_ENABLE_AGIOS #include #endif @@ -389,6 +394,45 @@ initialize_loggers() { gkfs::log::setup(logger_names, level, path); } +string +get_host_default(){ + char buf[1024]; + struct ifconf ifc; + struct ifreq *ifr; + int sck; + int nInterfaces; + int i; + + /* Get a socket handle. */ + sck = socket(AF_INET, SOCK_DGRAM, 0); + if(sck < 0){ + perror("socket"); + return ""s; + } + + /* Query available interfaces. */ + ifc.ifc_len = sizeof(buf); + ifc.ifc_buf = buf; + if(ioctl(sck, SIOCGIFCONF, &ifc) < 0){ + perror("ioctl(SIOCGIFCONF)"); + return ""s; + } + + /* Iterate through the list of interfaces. */ + ifr = ifc.ifc_req; + nInterfaces = ifc.ifc_len / sizeof(struct ifreq); + for(i = 0; i < nInterfaces; i++){ + struct ifreq *item = &ifr[i]; + + if(strcmp(item->ifr_name, "lo") != 0) + return inet_ntoa(((struct sockaddr_in *)&item->ifr_addr)->sin_addr); + } + if(nInterfaces > 0) + return inet_ntoa(((struct sockaddr_in *)&(&ifr[0])->ifr_addr)->sin_addr); + return ""s; +} + + /** * * @param vm @@ -430,7 +474,8 @@ parse_input(const po::variables_map& vm) { } } else { if(rpc_protocol != gkfs::rpc::protocol::ofi_verbs) - addr = gkfs::rpc::get_my_hostname(true); + addr = get_host_default(); + } GKFS_DATA->rpc_protocol(rpc_protocol); @@ -512,7 +557,7 @@ main(int argc, const char* argv[]) { "Metadata directory where GekkoFS' RocksDB data directory is located. If not set, rootdir is used."); desc.add_options()( "listen,l", po::value(), - "Address or interface to bind the daemon to. Default: local hostname.\n" + "Address or interface to bind the daemon to. Default: First non-localhost interface.\n" "When used with ofi+verbs the FI_VERBS_IFACE environment variable is set accordingly " "which associates the verbs device with the network interface. In case FI_VERBS_IFACE " "is already defined, the argument is ignored. Default 'ib'.");