📄 ch3i_get_business_card.c
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- *//* * (C) 2001 by Argonne National Laboratory. * See COPYRIGHT in top-level directory. */#include "mpidi_ch3_impl.h"/* FIXME: This doesn't need to be in a separate file. And is there a better way to specify which business card routines to use ? */#define MAX_NUM_NICS 16#ifdef HAVE_WINDOWS_Hstatic int GetLocalIPs(int32_t *pIP, int max){ char hostname[100], **hlist; struct hostent *h = NULL; int n = 0; DWORD len = sizeof(hostname); /*if (!GetComputerName(hostname, &len))*/ if (!GetComputerNameEx(ComputerNameDnsFullyQualified, hostname, &len)) { return 0; } h = gethostbyname(hostname); if (h == NULL) { return 0; } hlist = h->h_addr_list; while (*hlist != NULL && n<max) { pIP[n] = *(int32_t*)(*hlist); /*{ unsigned int a, b, c, d; a = ((unsigned char *)(&pIP[n]))[0]; b = ((unsigned char *)(&pIP[n]))[1]; c = ((unsigned char *)(&pIP[n]))[2]; d = ((unsigned char *)(&pIP[n]))[3]; MPIU_DBG_PRINTF(("ip: %u.%u.%u.%u\n", a, b, c, d)); }*/ hlist++; n++; } return n;}#else /* HAVE_WINDOWS_H *//* FIXME: We are no longer using this code to pick interfaces from what is available, as the design and user-requriements typically require that particular, selected interfaces are used, not whatever is available */#if 0#define NUM_IFREQS 10#ifdef HAVE_SYS_TYPES_H#include <sys/types.h>#endif#ifdef HAVE_SYS_SOCKET_H#include <sys/socket.h>#endif#ifdef HAVE_NETDB_H#include <netdb.h>#endif#ifdef HAVE_NET_IF_H#ifdef __STRICT_ANSI__/* FIXME: THIS IS WRONG. INSTEAD, SPECIFY THE SPECIFIC FEATURE LEVEL NEEDED, AND THEN ONLY IF A CONFIGURE TEST SHOWS THAT IT IS REQUIRED THESE NEED TO BE SET FOR ALL COMPILATIONS TO AVOID HAVING DIFFERENT FILES COMPILED WITH DIFFERENT AND INCOMPATIBLE HEADER FILES. WHAT IS APPARENTLY NEEDED (SEE /usr/include/features.h ON A LINUX SYSTEM) IS EITHER _BSD_SOURCE OR _SVID_SOURCE; THIS MUST BE SET CONSISTENTLY FOR ALL FILES COMPILED AS PART OF MPICH2 */#define __USE_MISC /* This must be defined to get struct ifreq defined */#endif#include <net/if.h>#endif#ifdef HAVE_NETINET_IN_H#include <netinet/in.h>#endif#ifdef HAVE_SYS_IOCTL_H#include <sys/ioctl.h>#endif#ifdef HAVE_SYS_SOCKIO_H#include <sys/sockio.h>#endif#ifdef HAVE_ERRNO_H#include <errno.h>#endifstatic int GetLocalIPs(int32_t *pIP, int max){ int fd; char * buf_ptr; int buf_len; int buf_len_prev; char * ptr; int n = 0; fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) return 0; /* * Obtain the interface information from the operating system * * Note: much of this code is borrowed from W. Richard Stevens' book * entitled "UNIX Network Programming", Volume 1, Second Edition. See * section 16.6 for details. */ buf_len = NUM_IFREQS * sizeof(struct ifreq); buf_len_prev = 0; for(;;) { struct ifconf ifconf; int rc; buf_ptr = (char *) MPIU_Malloc(buf_len); if (buf_ptr == NULL) return 0; ifconf.ifc_buf = buf_ptr; ifconf.ifc_len = buf_len; rc = ioctl(fd, SIOCGIFCONF, &ifconf); if (rc < 0) { if (errno != EINVAL || buf_len_prev != 0) return 0; } else { if (ifconf.ifc_len == buf_len_prev) { buf_len = ifconf.ifc_len; break; } buf_len_prev = ifconf.ifc_len; } MPIU_Free(buf_ptr); buf_len += NUM_IFREQS * sizeof(struct ifreq); } /* * Now that we've got the interface information, we need to run through * the interfaces and save the ip addresses */ ptr = buf_ptr; while(ptr < buf_ptr + buf_len) { struct ifreq * ifreq; ifreq = (struct ifreq *) ptr; if (ifreq->ifr_addr.sa_family == AF_INET) { struct in_addr addr; addr = ((struct sockaddr_in *) &(ifreq->ifr_addr))->sin_addr;/* if ((addr.s_addr & net_mask_p->s_addr) == (net_addr_p->s_addr & net_mask_p->s_addr)) { *if_addr_p = addr; break; }*/ pIP[n] = addr.s_addr; n++; } /* * Increment pointer to the next ifreq; some adjustment may be * required if the address is an IPv6 address */ ptr += sizeof(struct ifreq); # if defined(AF_INET6) { if (ifreq->ifr_addr.sa_family == AF_INET6) { ptr += sizeof(struct sockaddr_in6) - sizeof(struct sockaddr); } }# endif } MPIU_Free(buf_ptr); close(fd); return n;}#endif /* 0 */#endif /* HAVE_WINDOWS_H */#undef FUNCNAME#define FUNCNAME MPIDI_CH3I_Get_business_card#undef FCNAME#define FCNAME MPIDI_QUOTE(FUNCNAME)int MPIDI_CH3I_Get_business_card(char *value, int length){ int mpi_errno; MPIDI_STATE_DECL(MPID_STATE_MPIDI_CH3I_GET_BUSINESS_CARD); MPIDI_FUNC_ENTER(MPID_STATE_MPIDI_CH3I_GET_BUSINESS_CARD); mpi_errno = MPIDI_CH3U_Get_business_card_sock(&value, &length); if (mpi_errno != MPI_SUCCESS) { mpi_errno = MPIR_Err_create_code(mpi_errno, MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER, "**buscard", 0); MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3I_GET_BUSINESS_CARD); return mpi_errno; } mpi_errno = MPIDI_CH3U_Get_business_card_sshm(&value, &length); if (mpi_errno != MPI_SUCCESS) { mpi_errno = MPIR_Err_create_code(mpi_errno, MPIR_ERR_FATAL, FCNAME, __LINE__, MPI_ERR_OTHER, "**buscard", 0); MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3I_GET_BUSINESS_CARD); return mpi_errno; } MPIDI_FUNC_EXIT(MPID_STATE_MPIDI_CH3I_GET_BUSINESS_CARD); return mpi_errno;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -