#include "pmix_config.h"
#include "pmix_common.h"
#include <string.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <errno.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_SOCKIO_H
# include <sys/sockio.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#ifdef HAVE_NET_IF_H
# include <net/if.h>
#endif
#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif
#ifdef HAVE_IFADDRS_H
# include <ifaddrs.h>
#endif
#include "src/mca/pif/base/base.h"
#include "src/mca/pif/pif.h"
#include "src/util/pmix_output.h"
#include "src/util/pmix_if.h"
static int if_bsdx_open(void);
pmix_pif_base_component_t pmix_mca_pif_bsdx_ipv4_component = {
PMIX_PIF_BASE_VERSION_2_0_0,
.pmix_mca_component_name = "bsdx_ipv4",
PMIX_MCA_BASE_MAKE_VERSION(component,
PMIX_MAJOR_VERSION,
PMIX_MINOR_VERSION,
PMIX_RELEASE_VERSION),
.pmix_mca_open_component = if_bsdx_open
};
static int prefix(uint32_t netmask)
{
uint32_t mask = ntohl(netmask);
int plen = 0;
if (0 == mask) {
plen = 32;
} else {
while ((mask % 2) == 0) {
plen += 1;
mask /= 2;
}
}
return (32 - plen);
}
static int if_bsdx_open(void)
{
struct ifaddrs **ifadd_list;
struct ifaddrs *cur_ifaddrs;
struct sockaddr_in *sin_addr;
ifadd_list = (struct ifaddrs **) malloc(sizeof(struct ifaddrs *));
if (getifaddrs(ifadd_list) < 0) {
pmix_output(0, "pmix_ifinit: getifaddrs() failed with error=%d\n", errno);
return PMIX_ERROR;
}
for (cur_ifaddrs = *ifadd_list; NULL != cur_ifaddrs; cur_ifaddrs = cur_ifaddrs->ifa_next) {
pmix_pif_t *intf;
struct in_addr a4;
if (AF_INET != cur_ifaddrs->ifa_addr->sa_family) {
continue;
}
if (0 == (cur_ifaddrs->ifa_flags & IFF_UP)) {
continue;
}
if (!pmix_if_retain_loopback && 0 != (cur_ifaddrs->ifa_flags & IFF_LOOPBACK)) {
continue;
}
if (0 != (cur_ifaddrs->ifa_flags & IFF_POINTOPOINT)) {
continue;
}
sin_addr = (struct sockaddr_in *) cur_ifaddrs->ifa_addr;
intf = PMIX_NEW(pmix_pif_t);
if (NULL == intf) {
pmix_output(0, "pmix_ifinit: unable to allocate %d bytes\n", (int) sizeof(pmix_pif_t));
return PMIX_ERR_OUT_OF_RESOURCE;
}
intf->af_family = AF_INET;
memcpy(&a4, &(sin_addr->sin_addr), sizeof(struct in_addr));
pmix_strncpy(intf->if_name, cur_ifaddrs->ifa_name, PMIX_IF_NAMESIZE - 1);
intf->if_index = pmix_list_get_size(&pmix_if_list) + 1;
((struct sockaddr_in *) &intf->if_addr)->sin_addr = a4;
((struct sockaddr_in *) &intf->if_addr)->sin_family = AF_INET;
((struct sockaddr_in *) &intf->if_addr)->sin_len = cur_ifaddrs->ifa_addr->sa_len;
intf->if_mask = prefix(sin_addr->sin_addr.s_addr);
intf->if_flags = cur_ifaddrs->ifa_flags;
intf->if_kernel_index = (uint16_t) if_nametoindex(cur_ifaddrs->ifa_name);
pmix_list_append(&pmix_if_list, &(intf->super));
}
return PMIX_SUCCESS;
}