1 /* 2 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana 3 * University Research and Technology 4 * Corporation. All rights reserved. 5 * Copyright (c) 2004-2005 The University of Tennessee and The University 6 * of Tennessee Research Foundation. All rights 7 * reserved. 8 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, 9 * University of Stuttgart. All rights reserved. 10 * Copyright (c) 2004-2005 The Regents of the University of California. 11 * All rights reserved. 12 * Copyright (c) 2007 Los Alamos National Security, LLC. All rights 13 * reserved. 14 * Copyright (c) 2008 Sun Microsystems, Inc. All rights reserved. 15 * Copyright (c) 2013 Cisco Systems, Inc. All rights reserved. 16 * Copyright (c) 2016 Intel, Inc. All rights reserved. 17 * $COPYRIGHT$ 18 * 19 * Additional copyrights may follow 20 * 21 * $HEADER$ 22 */ 23 24 /* @file */ 25 26 #ifndef PMIX_PIF_UTIL_ 27 #define PMIX_PIF_UTIL_ 28 29 #include "pmix_config.h" 30 31 #ifdef HAVE_SYS_TYPES_H 32 #include <sys/types.h> 33 #endif 34 #ifdef HAVE_SYS_SOCKET_H 35 #include <sys/socket.h> 36 #endif 37 #ifdef HAVE_NETINET_IN_H 38 #include <netinet/in.h> 39 #endif 40 41 #ifndef IF_NAMESIZE 42 #define IF_NAMESIZE 32 43 #endif 44 45 BEGIN_C_DECLS 46 47 #define PMIX_PIF_FORMAT_ADDR(n) \ 48 (((n) >> 24) & 0x000000FF), (((n) >> 16) & 0x000000FF), \ 49 (((n) >> 8) & 0x000000FF), ((n) & 0x000000FF) 50 51 #define PMIX_PIF_ASSEMBLE_NETWORK(n1, n2, n3, n4) \ 52 (((n1) << 24) & 0xFF000000) | \ 53 (((n2) << 16) & 0x00FF0000) | \ 54 (((n3) << 8) & 0x0000FF00) | \ 55 ( (n4) & 0x000000FF) 56 57 /** 58 * Lookup an interface by name and return its primary address. 59 * 60 * @param if_name (IN) Interface name 61 * @param if_addr (OUT) Interface address buffer 62 * @param size (IN) Interface address buffer size 63 */ 64 PMIX_EXPORT int pmix_ifnametoaddr(const char* if_name, 65 struct sockaddr* if_addr, 66 int size); 67 68 /** 69 * Lookup an interface by address and return its name. 70 * 71 * @param if_addr (IN) Interface address (hostname or dotted-quad) 72 * @param if_name (OUT) Interface name buffer 73 * @param size (IN) Interface name buffer size 74 */ 75 PMIX_EXPORT int pmix_ifaddrtoname(const char* if_addr, 76 char* if_name, int size); 77 78 /** 79 * Lookup an interface by name and return its pmix_list index. 80 * 81 * @param if_name (IN) Interface name 82 * @return Interface pmix_list index 83 */ 84 PMIX_EXPORT int pmix_ifnametoindex(const char* if_name); 85 86 /** 87 * Lookup an interface by name and return its kernel index. 88 * 89 * @param if_name (IN) Interface name 90 * @return Interface kernel index 91 */ 92 PMIX_EXPORT int16_t pmix_ifnametokindex(const char* if_name); 93 94 /* 95 * Attempt to resolve an address (given as either IPv4/IPv6 string 96 * or hostname) and return the kernel index of the interface 97 * that is on the same network as the specified address 98 */ 99 PMIX_EXPORT int16_t pmix_ifaddrtokindex(const char* if_addr); 100 101 /** 102 * Lookup an interface by pmix_list index and return its kernel index. 103 * 104 * @param if_name (IN) Interface pmix_list index 105 * @return Interface kernel index 106 */ 107 PMIX_EXPORT int pmix_ifindextokindex(int if_index); 108 109 /** 110 * Returns the number of available interfaces. 111 */ 112 PMIX_EXPORT int pmix_ifcount(void); 113 114 /** 115 * Returns the index of the first available interface. 116 */ 117 PMIX_EXPORT int pmix_ifbegin(void); 118 119 /** 120 * Lookup the current position in the interface list by 121 * index and return the next available index (if it exists). 122 * 123 * @param if_index Returns the next available index from the 124 * current position. 125 */ 126 PMIX_EXPORT int pmix_ifnext(int if_index); 127 128 /** 129 * Lookup an interface by index and return its name. 130 * 131 * @param if_index (IN) Interface index 132 * @param if_name (OUT) Interface name buffer 133 * @param size (IN) Interface name buffer size 134 */ 135 PMIX_EXPORT int pmix_ifindextoname(int if_index, char* if_name, int); 136 137 /** 138 * Lookup an interface by kernel index and return its name. 139 * 140 * @param if_index (IN) Interface kernel index 141 * @param if_name (OUT) Interface name buffer 142 * @param size (IN) Interface name buffer size 143 */ 144 PMIX_EXPORT int pmix_ifkindextoname(int if_kindex, char* if_name, int); 145 146 /** 147 * Lookup an interface by index and return its primary address. 148 * 149 * @param if_index (IN) Interface index 150 * @param if_name (OUT) Interface address buffer 151 * @param size (IN) Interface address buffer size 152 */ 153 PMIX_EXPORT int pmix_ifindextoaddr(int if_index, struct sockaddr*, 154 unsigned int); 155 PMIX_EXPORT int pmix_ifkindextoaddr(int if_kindex, 156 struct sockaddr* if_addr, 157 unsigned int length); 158 159 /** 160 * Lookup an interface by index and return its network mask (in CIDR 161 * notation -- NOT the actual netmask itself!). 162 * 163 * @param if_index (IN) Interface index 164 * @param if_name (OUT) Interface address buffer 165 * @param size (IN) Interface address buffer size 166 */ 167 PMIX_EXPORT int pmix_ifindextomask(int if_index, uint32_t*, int); 168 169 /** 170 * Lookup an interface by index and return its MAC address. 171 * 172 * @param if_index (IN) Interface index 173 * @param if_mac (OUT) Interface's MAC address 174 */ 175 PMIX_EXPORT int pmix_ifindextomac(int if_index, uint8_t if_mac[6]); 176 177 /** 178 * Lookup an interface by index and return its MTU. 179 * 180 * @param if_index (IN) Interface index 181 * @param if_mtu (OUT) Interface's MTU 182 */ 183 PMIX_EXPORT int pmix_ifindextomtu(int if_index, int *mtu); 184 185 /** 186 * Lookup an interface by index and return its flags. 187 * 188 * @param if_index (IN) Interface index 189 * @param if_flags (OUT) Interface flags 190 */ 191 PMIX_EXPORT int pmix_ifindextoflags(int if_index, uint32_t*); 192 193 /** 194 * Determine if given hostname / IP address is a local address 195 * 196 * @param hostname (IN) Hostname (or stringified IP address) 197 * @return true if \c hostname is local, false otherwise 198 */ 199 PMIX_EXPORT bool pmix_ifislocal(const char *hostname); 200 201 /** 202 * Convert a dot-delimited network tuple to an IP address 203 * 204 * @param addr (IN) character string tuple 205 * @param net (IN) Pointer to returned network address 206 * @param mask (IN) Pointer to returned netmask 207 * @return PMIX_SUCCESS if no problems encountered 208 * @return PMIX_ERROR if data could not be released 209 */ 210 PMIX_EXPORT int pmix_iftupletoaddr(const char *addr, uint32_t *net, uint32_t *mask); 211 212 /** 213 * Determine if given interface is loopback 214 * 215 * @param if_index (IN) Interface index 216 */ 217 PMIX_EXPORT bool pmix_ifisloopback(int if_index); 218 219 /* 220 * Determine if a specified interface is included in a NULL-terminated argv array 221 */ 222 PMIX_EXPORT int pmix_ifmatches(int kidx, char **nets); 223 224 /* 225 * Provide a list of strings that contain all known aliases for this node 226 */ 227 PMIX_EXPORT void pmix_ifgetaliases(char ***aliases); 228 229 END_C_DECLS 230 231 #endif