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$ 17 * 18 * Additional copyrights may follow 19 * 20 * $HEADER$ 21 */ 22 23 /* @file */ 24 25 #ifndef OPAL_IF_UTIL_ 26 #define OPAL_IF_UTIL_ 27 28 #include "opal_config.h" 29 30 #ifdef HAVE_SYS_TYPES_H 31 #include <sys/types.h> 32 #endif 33 #ifdef HAVE_SYS_SOCKET_H 34 #include <sys/socket.h> 35 #endif 36 #ifdef HAVE_NETINET_IN_H 37 #include <netinet/in.h> 38 #endif 39 40 #ifndef IF_NAMESIZE 41 #define IF_NAMESIZE 32 42 #endif 43 44 BEGIN_C_DECLS 45 46 #define OPAL_IF_FORMAT_ADDR(n) \ 47 (((n) >> 24) & 0x000000FF), (((n) >> 16) & 0x000000FF), \ 48 (((n) >> 8) & 0x000000FF), ((n) & 0x000000FF) 49 50 #define OPAL_IF_ASSEMBLE_NETWORK(n1, n2, n3, n4) \ 51 (((n1) << 24) & 0xFF000000) | \ 52 (((n2) << 16) & 0x00FF0000) | \ 53 (((n3) << 8) & 0x0000FF00) | \ 54 ( (n4) & 0x000000FF) 55 56 /** 57 * Lookup an interface by name and return its primary address. 58 * 59 * @param if_name (IN) Interface name 60 * @param if_addr (OUT) Interface address buffer 61 * @param size (IN) Interface address buffer size 62 */ 63 OPAL_DECLSPEC int opal_ifnametoaddr(const char* if_name, 64 struct sockaddr* if_addr, 65 int size); 66 67 /** 68 * Lookup an interface by address and return its name. 69 * 70 * @param if_addr (IN) Interface address (hostname or dotted-quad) 71 * @param if_name (OUT) Interface name buffer 72 * @param size (IN) Interface name buffer size 73 */ 74 OPAL_DECLSPEC int opal_ifaddrtoname(const char* if_addr, 75 char* if_name, int size); 76 77 /** 78 * Lookup an interface by name and return its opal_list index. 79 * 80 * @param if_name (IN) Interface name 81 * @return Interface opal_list index 82 */ 83 OPAL_DECLSPEC int opal_ifnametoindex(const char* if_name); 84 85 /** 86 * Lookup an interface by name and return its kernel index. 87 * 88 * @param if_name (IN) Interface name 89 * @return Interface kernel index 90 */ 91 OPAL_DECLSPEC int opal_ifnametokindex(const char* if_name); 92 93 /* 94 * Attempt to resolve an address (given as either IPv4/IPv6 string 95 * or hostname) and return the kernel index of the interface 96 * that is on the same network as the specified address 97 */ 98 OPAL_DECLSPEC int opal_ifaddrtokindex(const char* if_addr); 99 100 /** 101 * Lookup an interface by opal_list index and return its kernel index. 102 * 103 * @param if_name (IN) Interface opal_list index 104 * @return Interface kernel index 105 */ 106 OPAL_DECLSPEC int opal_ifindextokindex(int if_index); 107 108 /** 109 * Returns the number of available interfaces. 110 */ 111 OPAL_DECLSPEC int opal_ifcount(void); 112 113 /** 114 * Returns the index of the first available interface. 115 */ 116 OPAL_DECLSPEC int opal_ifbegin(void); 117 118 /** 119 * Lookup the current position in the interface list by 120 * index and return the next available index (if it exists). 121 * 122 * @param if_index Returns the next available index from the 123 * current position. 124 */ 125 OPAL_DECLSPEC int opal_ifnext(int if_index); 126 127 /** 128 * Lookup an interface by index and return its name. 129 * 130 * @param if_index (IN) Interface index 131 * @param if_name (OUT) Interface name buffer 132 * @param size (IN) Interface name buffer size 133 */ 134 OPAL_DECLSPEC int opal_ifindextoname(int if_index, char* if_name, int); 135 136 /** 137 * Lookup an interface by kernel index and return its name. 138 * 139 * @param if_index (IN) Interface kernel index 140 * @param if_name (OUT) Interface name buffer 141 * @param size (IN) Interface name buffer size 142 */ 143 OPAL_DECLSPEC int opal_ifkindextoname(int if_kindex, char* if_name, int); 144 145 /** 146 * Lookup an interface by index and return its primary address. 147 * 148 * @param if_index (IN) Interface index 149 * @param if_name (OUT) Interface address buffer 150 * @param size (IN) Interface address buffer size 151 */ 152 OPAL_DECLSPEC int opal_ifindextoaddr(int if_index, struct sockaddr*, 153 unsigned int); 154 OPAL_DECLSPEC int opal_ifkindextoaddr(int if_kindex, 155 struct sockaddr* if_addr, 156 unsigned int length); 157 158 /** 159 * Lookup an interface by index and return its network mask (in CIDR 160 * notation -- NOT the actual netmask itself!). 161 * 162 * @param if_index (IN) Interface index 163 * @param if_name (OUT) Interface address buffer 164 * @param size (IN) Interface address buffer size 165 */ 166 OPAL_DECLSPEC int opal_ifindextomask(int if_index, uint32_t*, int); 167 168 /** 169 * Lookup an interface by index and return its MAC address. 170 * 171 * @param if_index (IN) Interface index 172 * @param if_mac (OUT) Interface's MAC address 173 */ 174 OPAL_DECLSPEC int opal_ifindextomac(int if_index, uint8_t if_mac[6]); 175 176 /** 177 * Lookup an interface by index and return its MTU. 178 * 179 * @param if_index (IN) Interface index 180 * @param if_mtu (OUT) Interface's MTU 181 */ 182 OPAL_DECLSPEC int opal_ifindextomtu(int if_index, int *mtu); 183 184 /** 185 * Lookup an interface by index and return its flags. 186 * 187 * @param if_index (IN) Interface index 188 * @param if_flags (OUT) Interface flags 189 */ 190 OPAL_DECLSPEC int opal_ifindextoflags(int if_index, uint32_t*); 191 192 /** 193 * Determine if given hostname / IP address is a local address 194 * 195 * @param hostname (IN) Hostname (or stringified IP address) 196 * @return true if \c hostname is local, false otherwise 197 */ 198 OPAL_DECLSPEC bool opal_ifislocal(const char *hostname); 199 200 /** 201 * Convert a dot-delimited network tuple to an IP address 202 * 203 * @param addr (IN) character string tuple 204 * @param net (IN) Pointer to returned network address 205 * @param mask (IN) Pointer to returned netmask 206 * @return OPAL_SUCCESS if no problems encountered 207 * @return OPAL_ERROR if data could not be released 208 */ 209 OPAL_DECLSPEC int opal_iftupletoaddr(const char *addr, uint32_t *net, uint32_t *mask); 210 211 /** 212 * Determine if given interface is loopback 213 * 214 * @param if_index (IN) Interface index 215 */ 216 OPAL_DECLSPEC bool opal_ifisloopback(int if_index); 217 218 /* 219 * Determine if a specified interface is included in a NULL-terminated argv array 220 */ 221 OPAL_DECLSPEC int opal_ifmatches(int kidx, char **nets); 222 223 /* 224 * Provide a list of strings that contain all known aliases for this node 225 */ 226 OPAL_DECLSPEC void opal_ifgetaliases(char ***aliases); 227 228 END_C_DECLS 229 230 #endif 231