diff --git a/fairmq/CMakeLists.txt b/fairmq/CMakeLists.txt index e0d64730..4e412285 100644 --- a/fairmq/CMakeLists.txt +++ b/fairmq/CMakeLists.txt @@ -8,6 +8,8 @@ Set(INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/fairmq ${CMAKE_SOURCE_DIR}/fairmq/devices + ${CMAKE_SOURCE_DIR}/fairmq/examples/req-rep + ${CMAKE_SOURCE_DIR}/fairmq/tools ) Set(SYSTEM_INCLUDE_DIRECTORIES @@ -17,7 +19,6 @@ Set(SYSTEM_INCLUDE_DIRECTORIES If(PROTOBUF_FOUND) Set(INCLUDE_DIRECTORIES ${INCLUDE_DIRECTORIES} - ${CMAKE_SOURCE_DIR}/fairmq/examples/req-rep # # following directory is only for protobuf tests and is not essential part of FairMQ #${CMAKE_SOURCE_DIR}/fairmq/prototest ) @@ -130,9 +131,6 @@ Set(FairMQHDRFiles ) install(FILES ${FairMQHDRFiles} DESTINATION include) - - - set(DEPENDENCIES ${DEPENDENCIES} boost_thread boost_timer boost_system boost_program_options diff --git a/fairmq/tools/FairMQTools.h b/fairmq/tools/FairMQTools.h new file mode 100644 index 00000000..d649731b --- /dev/null +++ b/fairmq/tools/FairMQTools.h @@ -0,0 +1,64 @@ +#ifndef FAIRMQTOOLS_H_ +#define FAIRMQTOOLS_H_ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE /* To get defns of NI_MAXSERV and NI_MAXHOST */ +#endif + +#include +#include +#include +#include +#include + +#include +#include +#include + +using namespace std; + +namespace FairMQ +{ +namespace tools +{ + +int getHostIPs(map& addressMap) +{ + struct ifaddrs *ifaddr, *ifa; + int s; + char host[NI_MAXHOST]; + + if (getifaddrs(&ifaddr) == -1) + { + perror("getifaddrs"); + return -1; + } + + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) + { + if (ifa->ifa_addr == NULL) + { + continue; + } + + if (ifa->ifa_addr->sa_family == AF_INET) + { + s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); + if (s != 0) + { + cout << "getnameinfo() failed: " << gai_strerror(s) << endl; + return -1; + } + + addressMap.insert(pair(ifa->ifa_name, host)); + } + } + freeifaddrs(ifaddr); + + return 0; +} + +} // namespace tools +} // namespace FairMQ + +#endif diff --git a/fairmq/tools/README.md b/fairmq/tools/README.md new file mode 100644 index 00000000..5bb03913 --- /dev/null +++ b/fairmq/tools/README.md @@ -0,0 +1,35 @@ +# FairMQ Tools + +Contains common tools for use by FairMQ and/or users. + +## FairMQ::tools::getHostIPs + +Fills a map with the network interfaces and their IP addresses available on the current host. + +#### Example usage + +```c++ +#include +#include +#include + +#include "FairMQTools.h" + +void main() +{ + std::map IPs; + + FairMQ::tools::getHostIPs(IPs); + + for (std::map::iterator it = IPs.begin(); it != IPs.end(); ++it) + { + std::cout << it->first << ": " << it->second << std::endl; + } +} +``` +##### Example Output +``` +eth0: 123.123.1.123 +ib0: 123.123.2.123 +lo: 127.0.0.1 +``` \ No newline at end of file