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