2004-01-15 02:24:15 +03:00
|
|
|
/*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2004-10-20 05:03:09 +04:00
|
|
|
#include "ompi_config.h"
|
2004-01-14 21:24:30 +03:00
|
|
|
#include <string.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-01-14 21:24:30 +03:00
|
|
|
#include <unistd.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
2004-01-14 21:24:30 +03:00
|
|
|
#include <errno.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
2004-01-14 21:24:30 +03:00
|
|
|
#include <sys/types.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2004-01-14 21:24:30 +03:00
|
|
|
#include <sys/socket.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
2004-01-14 21:24:30 +03:00
|
|
|
#include <sys/ioctl.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2004-01-14 21:24:30 +03:00
|
|
|
#include <netinet/in.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
2004-01-14 21:24:30 +03:00
|
|
|
#include <arpa/inet.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NET_IF_H
|
2004-01-14 21:24:30 +03:00
|
|
|
#include <net/if.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
2004-01-14 21:24:30 +03:00
|
|
|
#include <netdb.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
2004-01-15 02:24:15 +03:00
|
|
|
|
2004-03-19 00:35:28 +03:00
|
|
|
#include "include/constants.h"
|
2004-06-07 19:33:53 +04:00
|
|
|
#include "class/ompi_list.h"
|
2004-03-17 21:45:16 +03:00
|
|
|
#include "util/if.h"
|
|
|
|
#include "util/output.h"
|
|
|
|
#include "util/strncpy.h"
|
2004-01-14 21:24:30 +03:00
|
|
|
|
|
|
|
#ifndef IF_NAMESIZE
|
|
|
|
#define IF_NAMESIZE 32
|
|
|
|
#endif
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
struct ompi_if_t {
|
|
|
|
ompi_list_item_t super;
|
2004-01-14 21:24:30 +03:00
|
|
|
char if_name[IF_NAMESIZE];
|
|
|
|
int if_index;
|
2004-10-22 20:06:05 +04:00
|
|
|
#ifndef WIN32
|
2004-01-14 21:24:30 +03:00
|
|
|
int if_flags;
|
2004-10-22 20:06:05 +04:00
|
|
|
#else
|
|
|
|
u_long if_flags;
|
|
|
|
#endif
|
2004-02-05 20:12:59 +03:00
|
|
|
int if_speed;
|
2004-01-14 21:24:30 +03:00
|
|
|
struct sockaddr_in if_addr;
|
|
|
|
struct sockaddr_in if_mask;
|
2004-10-22 20:06:05 +04:00
|
|
|
#ifdef WIN32
|
|
|
|
struct sockaddr_in if_bcast;
|
|
|
|
#endif
|
2004-01-26 21:41:37 +03:00
|
|
|
uint32_t if_bandwidth;
|
2004-01-14 21:24:30 +03:00
|
|
|
};
|
2004-06-07 19:33:53 +04:00
|
|
|
typedef struct ompi_if_t ompi_if_t;
|
2004-01-14 21:24:30 +03:00
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
static ompi_list_t ompi_if_list;
|
2004-01-14 21:24:30 +03:00
|
|
|
|
|
|
|
|
2004-01-15 02:24:15 +03:00
|
|
|
/*
|
|
|
|
* Discover the list of configured interfaces. Don't care about any
|
|
|
|
* interfaces that are not up or are local loopbacks.
|
|
|
|
*/
|
2004-01-14 21:24:30 +03:00
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
static int ompi_ifinit(void)
|
2004-01-14 21:24:30 +03:00
|
|
|
{
|
2004-10-22 20:06:05 +04:00
|
|
|
#ifndef WIN32
|
2004-01-14 21:24:30 +03:00
|
|
|
char buff[1024];
|
|
|
|
char *ptr;
|
|
|
|
struct ifconf ifconf;
|
2004-03-25 22:18:13 +03:00
|
|
|
int sd;
|
2004-01-14 21:24:30 +03:00
|
|
|
ifconf.ifc_len = sizeof(buff);
|
|
|
|
ifconf.ifc_buf = buff;
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
if (ompi_list_get_size(&ompi_if_list) > 0)
|
|
|
|
return OMPI_SUCCESS;
|
2004-03-25 22:18:13 +03:00
|
|
|
|
2004-01-14 21:24:30 +03:00
|
|
|
if((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0, "ompi_ifinit: socket() failed with errno=%d\n", errno);
|
|
|
|
return OMPI_ERROR;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(ioctl(sd, SIOCGIFCONF, &ifconf) < 0) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0, "ompi_ifinit: ioctl(SIOCGIFCONF) failed with errno=%d", errno);
|
2004-01-14 21:24:30 +03:00
|
|
|
close(sd);
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_ERROR;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
2004-06-07 19:33:53 +04:00
|
|
|
OBJ_CONSTRUCT(&ompi_if_list, ompi_list_t);
|
2004-01-14 21:24:30 +03:00
|
|
|
|
|
|
|
for(ptr = buff; ptr < buff + ifconf.ifc_len; ) {
|
|
|
|
struct ifreq* ifr = (struct ifreq*)ptr;
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_if_t intf;
|
|
|
|
ompi_if_t *intf_ptr;
|
2004-01-14 21:24:30 +03:00
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
OBJ_CONSTRUCT(&intf, ompi_list_item_t);
|
2004-02-02 19:11:59 +03:00
|
|
|
|
2004-01-14 21:24:30 +03:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
ptr += (sizeof(ifr->ifr_name) +
|
|
|
|
MAX(sizeof(struct sockaddr),ifr->ifr_addr.sa_len));
|
|
|
|
#else
|
2004-10-29 22:52:57 +04:00
|
|
|
#if 0
|
2004-01-14 21:24:30 +03:00
|
|
|
switch(ifr->ifr_addr.sa_family) {
|
|
|
|
case AF_INET6:
|
|
|
|
ptr += sizeof(ifr->ifr_name) + sizeof(struct sockaddr_in6);
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
default:
|
|
|
|
ptr += sizeof(ifr->ifr_name) + sizeof(struct sockaddr);
|
|
|
|
break;
|
|
|
|
}
|
2004-10-29 22:52:57 +04:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* Jeff/George/Graham fix. According to LAM/MPI and FT-MPI
|
|
|
|
and /usr/include/linux/if.h, you should really be advancing
|
|
|
|
by sizeof(struct ifreq) -- sizeof(ifr->ifr_name) +
|
|
|
|
sizeof(struct sockaddr) may be smaller than sizeof(struct
|
|
|
|
ifreq). More specifically, the second element of struct
|
|
|
|
ifreq may be a union, of which struct sockaddr is only one
|
|
|
|
element (and may not be the smallest). */
|
|
|
|
|
|
|
|
ptr += sizeof(struct ifreq);
|
|
|
|
#endif
|
2004-01-14 21:24:30 +03:00
|
|
|
#endif
|
|
|
|
if(ifr->ifr_addr.sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
|
2004-02-02 19:11:59 +03:00
|
|
|
if(ioctl(sd, SIOCGIFFLAGS, ifr) < 0) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0, "ompi_ifinit: ioctl(SIOCGIFFLAGS) failed with errno=%d", errno);
|
2004-01-14 21:24:30 +03:00
|
|
|
continue;
|
|
|
|
}
|
2004-02-13 16:56:55 +03:00
|
|
|
if ((ifr->ifr_flags & IFF_UP) == 0)
|
2004-01-14 21:24:30 +03:00
|
|
|
continue;
|
2004-08-28 05:15:19 +04:00
|
|
|
#if 0
|
2004-02-13 16:56:55 +03:00
|
|
|
if ((ifr->ifr_flags & IFF_LOOPBACK) != 0)
|
|
|
|
continue;
|
2004-08-28 05:15:19 +04:00
|
|
|
#endif
|
2004-01-14 21:24:30 +03:00
|
|
|
|
|
|
|
strcpy(intf.if_name, ifr->ifr_name);
|
|
|
|
intf.if_flags = ifr->ifr_flags;
|
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
2004-06-07 19:33:53 +04:00
|
|
|
intf.if_index = ompi_list_get_size(&ompi_if_list)+1;
|
2004-01-14 21:24:30 +03:00
|
|
|
#else
|
2004-02-02 19:11:59 +03:00
|
|
|
if(ioctl(sd, SIOCGIFINDEX, ifr) < 0) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0,"ompi_ifinit: ioctl(SIOCGIFINDEX) failed with errno=%d", errno);
|
2004-01-14 21:24:30 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#if defined(ifr_ifindex)
|
|
|
|
intf.if_index = ifr->ifr_ifindex;
|
|
|
|
#elif defined(ifr_index)
|
|
|
|
intf.if_index = ifr->ifr_index;
|
|
|
|
#else
|
|
|
|
intf.if_index = -1;
|
|
|
|
#endif
|
|
|
|
#endif /* __APPLE__ */
|
|
|
|
|
2004-02-02 19:11:59 +03:00
|
|
|
if(ioctl(sd, SIOCGIFADDR, ifr) < 0) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0, "ompi_ifinit: ioctl(SIOCGIFADDR) failed with errno=%d", errno);
|
2004-01-14 21:24:30 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(ifr->ifr_addr.sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
memcpy(&intf.if_addr, &ifr->ifr_addr, sizeof(intf.if_addr));
|
2004-02-02 19:11:59 +03:00
|
|
|
if(ioctl(sd, SIOCGIFNETMASK, ifr) < 0) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0, "ompi_ifinit: ioctl(SIOCGIFNETMASK) failed with errno=%d", errno);
|
2004-01-14 21:24:30 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
memcpy(&intf.if_mask, &ifr->ifr_addr, sizeof(intf.if_mask));
|
|
|
|
|
2004-10-18 19:38:19 +04:00
|
|
|
intf_ptr = (ompi_if_t*) malloc(sizeof(ompi_if_t));
|
2004-01-14 21:24:30 +03:00
|
|
|
if(intf_ptr == 0) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0, "ompi_ifinit: unable to allocated %d bytes\n", sizeof(ompi_if_t));
|
|
|
|
return OMPI_ERR_OUT_OF_RESOURCE;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
2004-02-02 19:11:59 +03:00
|
|
|
memcpy(intf_ptr, &intf, sizeof(intf));
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_list_append(&ompi_if_list, (ompi_list_item_t*)intf_ptr);
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
|
|
|
close(sd);
|
2004-10-22 20:06:05 +04:00
|
|
|
|
|
|
|
#else /* WIN32 implementation begins */
|
|
|
|
|
|
|
|
/*
|
|
|
|
1. check if the interface info list is already populated. If so, return
|
|
|
|
2. get the interface information which is required using WSAIoctl
|
|
|
|
3. construct ompi_if_list and populate it with the list of interfaces we have
|
|
|
|
CAVEAT: Does not support the following options which are supported in SIOCGIFCONF
|
|
|
|
- kernel table index
|
|
|
|
- interface name
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_INTERFACES 10 /* Anju: for now assume there are no more than this */
|
|
|
|
int ret;
|
|
|
|
WSADATA win_sock_data;
|
|
|
|
SOCKET sd;
|
|
|
|
INTERFACE_INFO if_list[MAX_INTERFACES];
|
|
|
|
int num_interfaces;
|
|
|
|
unsigned long num_bytes_returned;
|
|
|
|
int i;
|
|
|
|
SOCKADDR_IN *sock_address;
|
|
|
|
unsigned int interface_counter = 0;
|
|
|
|
ompi_if_t intf;
|
|
|
|
ompi_if_t *intf_ptr;
|
|
|
|
|
|
|
|
/* return if this has been done before */
|
|
|
|
if (0 > ompi_list_get_size(&ompi_if_list)) {
|
|
|
|
return OMPI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* else initialise the use of Winsock2.DLL */
|
|
|
|
if (WSAStartup (MAKEWORD (2, 2), &win_sock_data) != 0) {
|
|
|
|
ompi_output(0, "ompi_ifinit: WSAStartup failed with errno=%d\n",WSAGetLastError());
|
|
|
|
return OMPI_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a socket */
|
|
|
|
sd = WSASocket (AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, 0);
|
|
|
|
if (sd == SOCKET_ERROR) {
|
|
|
|
ompi_output(0, "ompi_ifinit: WSASocket failed with errno=%d\n",WSAGetLastError());
|
|
|
|
return OMPI_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the information about the interfaces */
|
|
|
|
if (SOCKET_ERROR == WSAIoctl (sd,
|
|
|
|
SIO_GET_INTERFACE_LIST,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
&if_list,
|
|
|
|
sizeof (if_list),
|
|
|
|
&num_bytes_returned,
|
|
|
|
0,
|
|
|
|
0)) {
|
|
|
|
ompi_output(0, "ompi_ifinit: WSAIoctl failed with errno=%d\n",WSAGetLastError());
|
|
|
|
return OMPI_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create and populate ompi_if_list */
|
|
|
|
OBJ_CONSTRUCT (&ompi_if_list, ompi_list_t);
|
|
|
|
|
|
|
|
|
|
|
|
/* loop through all the interfaces and create the list */
|
|
|
|
num_interfaces = num_bytes_returned / sizeof (INTERFACE_INFO);
|
|
|
|
for (i = 0; i < num_interfaces; ++i) {
|
|
|
|
/* do all this only if the interface is up */
|
|
|
|
if (if_list[i].iiFlags & IFF_UP) {
|
|
|
|
|
|
|
|
OBJ_CONSTRUCT (&intf, ompi_list_item_t);
|
|
|
|
|
|
|
|
/* fill in the interface address */
|
|
|
|
memcpy (&intf.if_addr, &(if_list[i].iiAddress), sizeof(intf.if_addr));
|
|
|
|
|
|
|
|
/* fill in the netmask information */
|
|
|
|
memcpy (&intf.if_mask, &(if_list[i].iiNetmask), sizeof(intf.if_mask));
|
|
|
|
|
|
|
|
/* fill in the bcast address */
|
|
|
|
memcpy (&intf.if_bcast, &(if_list[i].iiBroadcastAddress), sizeof(intf.if_bcast));
|
|
|
|
|
|
|
|
/* fill in the flags */
|
|
|
|
intf.if_flags = if_list[i].iiFlags;
|
|
|
|
|
|
|
|
/* fill in the index in the table */
|
|
|
|
intf.if_index = ompi_list_get_size(&ompi_if_list)+1;
|
|
|
|
|
|
|
|
/* generate the interface name on your own ....
|
|
|
|
loopback: lo
|
|
|
|
Rest: eth0, eth1, ..... */
|
|
|
|
|
|
|
|
if (if_list[i].iiFlags & IFF_LOOPBACK) {
|
|
|
|
sprintf (intf.if_name, "lo");
|
|
|
|
} else {
|
|
|
|
sprintf (intf.if_name, "eth%u", interface_counter++);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy all this into a persistent form and store it in the list */
|
|
|
|
intf_ptr = malloc(sizeof(ompi_if_t));
|
|
|
|
if (NULL == intf_ptr) {
|
|
|
|
ompi_output (0,"ompi_ifinit: Unable to malloc %d bytes",sizeof(ompi_list_t));
|
|
|
|
return OMPI_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy (intf_ptr, &intf, sizeof(intf));
|
|
|
|
ompi_list_append(&ompi_if_list, (ompi_list_item_t *)intf_ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this takes care of the cleanup for us :-D */
|
|
|
|
WSACleanup ();
|
|
|
|
|
|
|
|
#endif
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_SUCCESS;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-15 02:24:15 +03:00
|
|
|
/*
|
|
|
|
* Look for interface by name and returns its address
|
|
|
|
* as a dotted decimal formatted string.
|
|
|
|
*/
|
2004-01-14 21:24:30 +03:00
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
int ompi_ifnametoaddr(const char* if_name, struct sockaddr* addr, int length)
|
2004-01-14 21:24:30 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_if_t* intf;
|
|
|
|
int rc = ompi_ifinit();
|
|
|
|
if(rc != OMPI_SUCCESS)
|
2004-01-14 21:24:30 +03:00
|
|
|
return rc;
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
for(intf = (ompi_if_t*)ompi_list_get_first(&ompi_if_list);
|
|
|
|
intf != (ompi_if_t*)ompi_list_get_end(&ompi_if_list);
|
|
|
|
intf = (ompi_if_t*)ompi_list_get_next(intf)) {
|
2004-01-14 21:24:30 +03:00
|
|
|
if(strcmp(intf->if_name, if_name) == 0) {
|
2004-01-15 03:57:33 +03:00
|
|
|
memcpy(addr, &intf->if_addr, length);
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_SUCCESS;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
|
|
|
}
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_ERROR;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
|
|
|
|
2004-01-29 18:34:47 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for interface by name and returns its
|
|
|
|
* corresponding kernel index.
|
|
|
|
*/
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
int ompi_ifnametoindex(const char* if_name)
|
2004-01-29 18:34:47 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_if_t* intf;
|
|
|
|
int rc = ompi_ifinit();
|
|
|
|
if(rc != OMPI_SUCCESS)
|
2004-01-29 18:34:47 +03:00
|
|
|
return rc;
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
for(intf = (ompi_if_t*)ompi_list_get_first(&ompi_if_list);
|
|
|
|
intf != (ompi_if_t*)ompi_list_get_end(&ompi_if_list);
|
|
|
|
intf = (ompi_if_t*)ompi_list_get_next(intf)) {
|
2004-01-29 18:34:47 +03:00
|
|
|
if(strcmp(intf->if_name, if_name) == 0) {
|
|
|
|
return intf->if_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-15 02:24:15 +03:00
|
|
|
/*
|
|
|
|
* Attempt to resolve the adddress as either a dotted decimal formated
|
|
|
|
* string or a hostname and lookup corresponding interface.
|
|
|
|
*/
|
2004-01-14 21:24:30 +03:00
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
int ompi_ifaddrtoname(const char* if_addr, char* if_name, int length)
|
2004-01-14 21:24:30 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_if_t* intf;
|
2004-10-22 20:06:05 +04:00
|
|
|
#ifndef WIN32
|
|
|
|
in_addr_t inaddr;
|
|
|
|
#else
|
|
|
|
unsigned long inaddr;
|
|
|
|
#endif
|
|
|
|
int rc;
|
|
|
|
struct hostent *h;
|
|
|
|
|
|
|
|
inaddr = inet_addr(if_addr);
|
|
|
|
|
|
|
|
rc = ompi_ifinit();
|
2004-06-07 19:33:53 +04:00
|
|
|
if(rc != OMPI_SUCCESS)
|
2004-01-14 21:24:30 +03:00
|
|
|
return rc;
|
|
|
|
|
2004-10-31 22:01:53 +03:00
|
|
|
if(inaddr == INADDR_NONE) {
|
2004-10-22 20:06:05 +04:00
|
|
|
h = gethostbyname(if_addr);
|
2004-01-14 21:24:30 +03:00
|
|
|
if(h == 0) {
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_output(0,"ompi_ifaddrtoname: unable to resolve %s\n", if_addr);
|
2004-10-31 22:01:53 +03:00
|
|
|
return OMPI_ERR_NOT_FOUND;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
|
|
|
memcpy(&inaddr, h->h_addr, sizeof(inaddr));
|
|
|
|
}
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
for(intf = (ompi_if_t*)ompi_list_get_first(&ompi_if_list);
|
|
|
|
intf != (ompi_if_t*)ompi_list_get_end(&ompi_if_list);
|
|
|
|
intf = (ompi_if_t*)ompi_list_get_next(intf)) {
|
2004-01-14 21:24:30 +03:00
|
|
|
if(intf->if_addr.sin_addr.s_addr == inaddr) {
|
|
|
|
strncpy(if_name, intf->if_name, length);
|
2004-10-31 22:01:53 +03:00
|
|
|
return OMPI_SUCCESS;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
|
|
|
}
|
2004-10-31 22:01:53 +03:00
|
|
|
return OMPI_ERR_NOT_FOUND;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
|
|
|
|
2004-01-15 02:24:15 +03:00
|
|
|
/*
|
|
|
|
* Return the number of discovered interface.
|
|
|
|
*/
|
2004-01-14 21:24:30 +03:00
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
int ompi_ifcount()
|
2004-01-14 21:24:30 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
if(ompi_ifinit() != OMPI_SUCCESS)
|
2004-01-14 21:24:30 +03:00
|
|
|
return (-1);
|
2004-06-07 19:33:53 +04:00
|
|
|
return ompi_list_get_size(&ompi_if_list);
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-15 02:24:15 +03:00
|
|
|
/*
|
|
|
|
* Return the kernels interface index for the first
|
|
|
|
* interface in our list.
|
|
|
|
*/
|
2004-01-14 21:24:30 +03:00
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
int ompi_ifbegin()
|
2004-01-14 21:24:30 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_if_t *intf;
|
|
|
|
if(ompi_ifinit() != OMPI_SUCCESS)
|
2004-01-14 21:24:30 +03:00
|
|
|
return (-1);
|
2004-06-07 19:33:53 +04:00
|
|
|
intf = (ompi_if_t*)ompi_list_get_first(&ompi_if_list);
|
2004-07-07 20:28:01 +04:00
|
|
|
if(NULL != intf)
|
2004-01-14 21:24:30 +03:00
|
|
|
return intf->if_index;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-15 02:24:15 +03:00
|
|
|
/*
|
|
|
|
* Located the current position in the list by if_index and
|
|
|
|
* return the interface index of the next element in our list
|
|
|
|
* (if it exists).
|
|
|
|
*/
|
2004-01-14 21:24:30 +03:00
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
int ompi_ifnext(int if_index)
|
2004-01-14 21:24:30 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_if_t *intf;
|
|
|
|
if(ompi_ifinit() != OMPI_SUCCESS)
|
2004-01-14 21:24:30 +03:00
|
|
|
return (-1);
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
for(intf = (ompi_if_t*)ompi_list_get_first(&ompi_if_list);
|
|
|
|
intf != (ompi_if_t*)ompi_list_get_end(&ompi_if_list);
|
|
|
|
intf = (ompi_if_t*)ompi_list_get_next(intf)) {
|
2004-01-14 21:24:30 +03:00
|
|
|
if(intf->if_index == if_index) {
|
2004-07-07 18:01:55 +04:00
|
|
|
do {
|
|
|
|
ompi_if_t* if_next = (ompi_if_t*)ompi_list_get_next(intf);
|
2004-07-07 20:28:01 +04:00
|
|
|
ompi_if_t* if_end = (ompi_if_t*)ompi_list_get_end(&ompi_if_list);
|
|
|
|
if (if_next == if_end) {
|
2004-07-07 18:01:55 +04:00
|
|
|
return -1;
|
2004-07-07 20:28:01 +04:00
|
|
|
}
|
|
|
|
intf = if_next;
|
2004-07-07 18:01:55 +04:00
|
|
|
} while(intf->if_index == if_index);
|
|
|
|
return intf->if_index;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-15 02:24:15 +03:00
|
|
|
/*
|
|
|
|
* Lookup the interface by kernel index and return the
|
|
|
|
* primary address assigned to the interface.
|
|
|
|
*/
|
2004-01-14 21:24:30 +03:00
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
int ompi_ifindextoaddr(int if_index, struct sockaddr* if_addr, int length)
|
2004-01-14 21:24:30 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_if_t* intf;
|
|
|
|
int rc = ompi_ifinit();
|
|
|
|
if(rc != OMPI_SUCCESS)
|
2004-01-14 21:24:30 +03:00
|
|
|
return rc;
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
for(intf = (ompi_if_t*)ompi_list_get_first(&ompi_if_list);
|
|
|
|
intf != (ompi_if_t*)ompi_list_get_end(&ompi_if_list);
|
|
|
|
intf = (ompi_if_t*)ompi_list_get_next(intf)) {
|
2004-01-14 21:24:30 +03:00
|
|
|
if(intf->if_index == if_index) {
|
2004-01-15 03:57:33 +03:00
|
|
|
memcpy(if_addr, &intf->if_addr, length);
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_SUCCESS;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
|
|
|
}
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_ERROR;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-26 21:41:37 +03:00
|
|
|
/*
|
|
|
|
* Lookup the interface by kernel index and return the
|
|
|
|
* network mask assigned to the interface.
|
|
|
|
*/
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
int ompi_ifindextomask(int if_index, struct sockaddr* if_mask, int length)
|
2004-01-26 21:41:37 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_if_t* intf;
|
|
|
|
int rc = ompi_ifinit();
|
|
|
|
if(rc != OMPI_SUCCESS)
|
2004-01-26 21:41:37 +03:00
|
|
|
return rc;
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
for(intf = (ompi_if_t*)ompi_list_get_first(&ompi_if_list);
|
|
|
|
intf != (ompi_if_t*)ompi_list_get_end(&ompi_if_list);
|
|
|
|
intf = (ompi_if_t*)ompi_list_get_next(intf)) {
|
2004-01-26 21:41:37 +03:00
|
|
|
if(intf->if_index == if_index) {
|
|
|
|
memcpy(if_mask, &intf->if_mask, length);
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_SUCCESS;
|
2004-01-26 21:41:37 +03:00
|
|
|
}
|
|
|
|
}
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_ERROR;
|
2004-01-26 21:41:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-01-15 02:24:15 +03:00
|
|
|
/*
|
|
|
|
* Lookup the interface by kernel index and return
|
|
|
|
* the associated name.
|
|
|
|
*/
|
2004-01-14 21:24:30 +03:00
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
int ompi_ifindextoname(int if_index, char* if_name, int length)
|
2004-01-14 21:24:30 +03:00
|
|
|
{
|
2004-06-07 19:33:53 +04:00
|
|
|
ompi_if_t *intf;
|
|
|
|
int rc = ompi_ifinit();
|
|
|
|
if(rc != OMPI_SUCCESS)
|
2004-01-14 21:24:30 +03:00
|
|
|
return rc;
|
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
for(intf = (ompi_if_t*)ompi_list_get_first(&ompi_if_list);
|
|
|
|
intf != (ompi_if_t*)ompi_list_get_end(&ompi_if_list);
|
|
|
|
intf = (ompi_if_t*)ompi_list_get_next(intf)) {
|
2004-01-14 21:24:30 +03:00
|
|
|
if(intf->if_index == if_index) {
|
|
|
|
strncpy(if_name, intf->if_name, length);
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_SUCCESS;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
|
|
|
}
|
2004-06-07 19:33:53 +04:00
|
|
|
return OMPI_ERROR;
|
2004-01-14 21:24:30 +03:00
|
|
|
}
|
2004-10-31 22:01:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
ompi_ifislocal(char *hostname)
|
|
|
|
{
|
|
|
|
const int len = 100;
|
|
|
|
char addrname[len - 1];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ompi_ifaddrtoname(hostname, addrname, len);
|
|
|
|
if (OMPI_SUCCESS == ret) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|