Skip to content

Commit

Permalink
Merge pull request #990 from JasonRuonanWang/helper-ipaddress
Browse files Browse the repository at this point in the history
added AvailableIpAddresses in helper
  • Loading branch information
JasonRuonanWang committed Nov 7, 2018
2 parents 1f43693 + 3e8e09b commit fb90b51
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
48 changes: 47 additions & 1 deletion source/adios2/helper/adiosSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
* Author: William F Godoy godoywf@ornl.gov
*/
#include "adiosSystem.h"

#ifndef _WIN32
#include <arpa/inet.h> //AvailableIpAddresses() inet_ntoa
#include <net/if.h> //AvailableIpAddresses() struct if_nameindex
#include <string.h> //AvailableIpAddresses() strncp
#include <sys/ioctl.h> //AvailableIpAddresses() ioctl
#include <unistd.h> //AvailableIpAddresses() close
#endif
#include <chrono> //system_clock, now
#include <ctime>
#include <iostream> //std::cerr
Expand Down Expand Up @@ -74,6 +80,46 @@ bool IsZeroIndexed(const std::string hostLanguage) noexcept
return isZeroIndexed;
}

#ifndef _WIN32
std::vector<std::string> AvailableIpAddresses() noexcept
{
std::vector<std::string> ips;
int socket_handler = -1;
struct if_nameindex *p = 0;
struct if_nameindex *head = 0;
if ((socket_handler = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
{
return ips;
}
head = if_nameindex();
p = if_nameindex();
while ((p != NULL) && (p->if_name != NULL))
{
struct ifreq req;
strncpy(req.ifr_name, p->if_name, IFNAMSIZ);
if (ioctl(socket_handler, SIOCGIFADDR, &req) < 0)
{
if (errno == EADDRNOTAVAIL)
{
++p;
continue;
}
close(socket_handler);
return ips;
}
const std::string ip =
inet_ntoa(((struct sockaddr_in *)&req.ifr_addr)->sin_addr);
if (ip != "127.0.0.1")
{
ips.emplace_back(ip);
}
++p;
}
if_freenameindex(head);
close(socket_handler);
return ips;
}
#endif
int ExceptionToError(const std::string &function)
{
try
Expand Down
7 changes: 7 additions & 0 deletions source/adios2/helper/adiosSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ bool IsRowMajor(const std::string hostLanguage) noexcept;
*/
bool IsZeroIndexed(const std::string hostLanguage) noexcept;

/**
* returns a vector of strings with all available IP addresses on the node
* @return vector of strings
*/
#ifndef _WIN32
std::vector<std::string> AvailableIpAddresses() noexcept;
#endif
/**
* Function to be called inside a catch(...) block to convert C++ exceptions to
* error integers. Used by C, Fortran bindings.
Expand Down

0 comments on commit fb90b51

Please sign in to comment.