Updated to fedora-glibc-20071212T1051
[glibc.git] / sysdeps / unix / sysv / linux / if_index.c
blob6bd6bda3b0c1e28e3f41eec54f11e5cad98f7262
1 /* Copyright (C) 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <alloca.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <net/if.h>
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
29 #include <bits/libc-lock.h>
30 #include <not-cancel.h>
31 #include <kernel-features.h>
33 #include "netlinkaccess.h"
36 /* Variable to signal whether SIOCGIFCONF is not available. */
37 # if __ASSUME_SIOCGIFNAME == 0
38 static int old_siocgifconf;
39 #else
40 # define old_siocgifconf 0
41 #endif
44 unsigned int
45 if_nametoindex (const char *ifname)
47 #ifndef SIOCGIFINDEX
48 __set_errno (ENOSYS);
49 return 0;
50 #else
51 struct ifreq ifr;
52 int fd = __opensock ();
54 if (fd < 0)
55 return 0;
57 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
58 if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
60 int saved_errno = errno;
61 close_not_cancel_no_status (fd);
62 if (saved_errno == EINVAL)
63 __set_errno (ENOSYS);
64 return 0;
66 close_not_cancel_no_status (fd);
67 return ifr.ifr_ifindex;
68 #endif
70 libc_hidden_def (if_nametoindex)
73 void
74 if_freenameindex (struct if_nameindex *ifn)
76 struct if_nameindex *ptr = ifn;
77 while (ptr->if_name || ptr->if_index)
79 free (ptr->if_name);
80 ++ptr;
82 free (ifn);
84 libc_hidden_def (if_freenameindex)
87 #if __ASSUME_NETLINK_SUPPORT == 0
88 static struct if_nameindex *
89 if_nameindex_ioctl (void)
91 int fd = __opensock ();
92 struct ifconf ifc;
93 unsigned int nifs, i;
94 int rq_len;
95 struct if_nameindex *idx = NULL;
96 # define RQ_IFS 4
98 if (fd < 0)
99 return NULL;
101 ifc.ifc_buf = NULL;
103 /* We may be able to get the needed buffer size directly, rather than
104 guessing. */
105 if (! old_siocgifconf)
107 ifc.ifc_buf = NULL;
108 ifc.ifc_len = 0;
109 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0 || ifc.ifc_len == 0)
111 # if __ASSUME_SIOCGIFNAME == 0
112 old_siocgifconf = 1;
113 # endif
114 rq_len = RQ_IFS * sizeof (struct ifreq);
116 else
117 rq_len = ifc.ifc_len;
119 else
120 rq_len = RQ_IFS * sizeof (struct ifreq);
122 /* Read all the interfaces out of the kernel. */
123 ifc.ifc_buf = alloca (rq_len);
124 ifc.ifc_len = rq_len;
125 while (1)
127 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0)
129 close_not_cancel_no_status (fd);
130 return NULL;
132 if (ifc.ifc_len < rq_len || ! old_siocgifconf)
133 break;
135 ifc.ifc_buf = extend_alloca (ifc.ifc_buf, rq_len, 2 * rq_len);
136 ifc.ifc_len = rq_len;
139 nifs = ifc.ifc_len / sizeof (struct ifreq);
141 idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
142 if (idx == NULL)
144 close_not_cancel_no_status (fd);
145 __set_errno (ENOBUFS);
146 return NULL;
149 for (i = 0; i < nifs; ++i)
151 struct ifreq *ifr = &ifc.ifc_req[i];
152 idx[i].if_name = __strdup (ifr->ifr_name);
153 if (idx[i].if_name == NULL
154 || __ioctl (fd, SIOCGIFINDEX, ifr) < 0)
156 int saved_errno = errno;
157 unsigned int j;
159 for (j = 0; j < i; ++j)
160 free (idx[j].if_name);
161 free (idx);
162 close_not_cancel_no_status (fd);
163 if (saved_errno == EINVAL)
164 saved_errno = ENOSYS;
165 else if (saved_errno == ENOMEM)
166 saved_errno = ENOBUFS;
167 __set_errno (saved_errno);
168 return NULL;
170 idx[i].if_index = ifr->ifr_ifindex;
173 idx[i].if_index = 0;
174 idx[i].if_name = NULL;
176 close_not_cancel_no_status (fd);
177 return idx;
179 #endif
182 static struct if_nameindex *
183 if_nameindex_netlink (void)
185 struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
186 struct if_nameindex *idx = NULL;
188 if (__no_netlink_support || __netlink_open (&nh) < 0)
189 return NULL;
192 /* Tell the kernel that we wish to get a list of all
193 active interfaces. Collect all data for every interface. */
194 if (__netlink_request (&nh, RTM_GETLINK) < 0)
195 goto exit_free;
197 /* Count the interfaces. */
198 unsigned int nifs = 0;
199 for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next)
201 struct nlmsghdr *nlh;
202 size_t size = nlp->size;
204 if (nlp->nlh == NULL)
205 continue;
207 /* Walk through all entries we got from the kernel and look, which
208 message type they contain. */
209 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
211 /* Check if the message is what we want. */
212 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
213 continue;
215 if (nlh->nlmsg_type == NLMSG_DONE)
216 break; /* ok */
218 if (nlh->nlmsg_type == RTM_NEWLINK)
219 ++nifs;
223 idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
224 if (idx == NULL)
226 nomem:
227 __set_errno (ENOBUFS);
228 goto exit_free;
231 /* Add the interfaces. */
232 nifs = 0;
233 for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next)
235 struct nlmsghdr *nlh;
236 size_t size = nlp->size;
238 if (nlp->nlh == NULL)
239 continue;
241 /* Walk through all entries we got from the kernel and look, which
242 message type they contain. */
243 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
245 /* Check if the message is what we want. */
246 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
247 continue;
249 if (nlh->nlmsg_type == NLMSG_DONE)
250 break; /* ok */
252 if (nlh->nlmsg_type == RTM_NEWLINK)
254 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
255 struct rtattr *rta = IFLA_RTA (ifim);
256 size_t rtasize = IFLA_PAYLOAD (nlh);
258 idx[nifs].if_index = ifim->ifi_index;
260 while (RTA_OK (rta, rtasize))
262 char *rta_data = RTA_DATA (rta);
263 size_t rta_payload = RTA_PAYLOAD (rta);
265 if (rta->rta_type == IFLA_IFNAME)
267 idx[nifs].if_name = __strndup (rta_data, rta_payload);
268 if (idx[nifs].if_name == NULL)
270 idx[nifs].if_index = 0;
271 if_freenameindex (idx);
272 idx = NULL;
273 goto nomem;
275 break;
278 rta = RTA_NEXT (rta, rtasize);
281 ++nifs;
286 idx[nifs].if_index = 0;
287 idx[nifs].if_name = NULL;
289 exit_free:
290 __netlink_free_handle (&nh);
291 __netlink_close (&nh);
293 return idx;
297 struct if_nameindex *
298 if_nameindex (void)
300 #ifndef SIOCGIFINDEX
301 __set_errno (ENOSYS);
302 return NULL;
303 #else
304 struct if_nameindex *result = if_nameindex_netlink ();
305 # if __ASSUME_NETLINK_SUPPORT == 0
306 if (__no_netlink_support)
307 result = if_nameindex_ioctl ();
308 # endif
309 return result;
310 #endif
312 libc_hidden_def (if_nameindex)
315 char *
316 if_indextoname (unsigned int ifindex, char *ifname)
318 #if !defined SIOCGIFINDEX && __ASSUME_SIOCGIFNAME == 0
319 __set_errno (ENOSYS);
320 return NULL;
321 #else
322 # if __ASSUME_SIOCGIFNAME == 0
323 struct if_nameindex *idx;
324 struct if_nameindex *p;
325 char *result = NULL;
326 # endif
328 # if defined SIOCGIFNAME || __ASSUME_SIOCGIFNAME > 0
329 /* We may be able to do the conversion directly, rather than searching a
330 list. This ioctl is not present in kernels before version 2.1.50. */
331 struct ifreq ifr;
332 int fd;
333 # if __ASSUME_SIOCGIFNAME == 0
334 static int siocgifname_works_not;
336 if (!siocgifname_works_not)
337 # endif
339 # if __ASSUME_SIOCGIFNAME == 0
340 int serrno = errno;
341 # endif
342 int status;
344 fd = __opensock ();
346 if (fd < 0)
347 return NULL;
349 ifr.ifr_ifindex = ifindex;
350 status = __ioctl (fd, SIOCGIFNAME, &ifr);
352 close_not_cancel_no_status (fd);
354 if (status < 0)
356 # if __ASSUME_SIOCGIFNAME == 0
357 if (errno == EINVAL)
358 siocgifname_works_not = 1; /* Don't make the same mistake twice. */
359 else
360 # endif
362 if (errno == ENODEV)
363 /* POSIX requires ENXIO. */
364 __set_errno (ENXIO);
366 return NULL;
369 else
370 return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
372 # if __ASSUME_SIOCGIFNAME == 0
373 __set_errno (serrno);
374 # endif
376 # endif
378 # if __ASSUME_SIOCGIFNAME == 0
379 idx = if_nameindex ();
381 if (idx != NULL)
383 for (p = idx; p->if_index || p->if_name; ++p)
384 if (p->if_index == ifindex)
386 result = strncpy (ifname, p->if_name, IFNAMSIZ);
387 break;
390 if_freenameindex (idx);
392 if (result == NULL)
393 __set_errno (ENXIO);
395 return result;
396 # endif
397 #endif
399 libc_hidden_def (if_indextoname)
402 #if 0
403 void
404 internal_function
405 __protocol_available (int *have_inet, int *have_inet6)
407 int fd = __opensock ();
408 unsigned int nifs;
409 int rq_len;
410 struct ifconf ifc;
411 # define RQ_IFS 4
413 /* Wirst case assumption. */
414 *have_inet = 0;
415 *have_inet6 = 0;
417 if (fd < 0)
418 /* We cannot open the socket. No networking at all? */
419 return;
421 /* We may be able to get the needed buffer size directly, rather than
422 guessing. */
423 if (! old_siocgifconf)
425 ifc.ifc_buf = NULL;
426 ifc.ifc_len = 0;
427 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0 || ifc.ifc_len == 0)
429 # if __ASSUME_SIOCGIFNAME == 0
430 old_siocgifconf = 1;
431 # endif
432 rq_len = RQ_IFS * sizeof (struct ifreq);
434 else
435 rq_len = ifc.ifc_len;
437 else
438 rq_len = RQ_IFS * sizeof (struct ifreq);
440 /* Read all the interfaces out of the kernel. */
443 ifc.ifc_buf = alloca (ifc.ifc_len = rq_len);
444 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0)
446 close_not_cancel_no_status (fd);
447 return;
449 rq_len *= 2;
451 while (ifc.ifc_len == rq_len && old_siocgifconf);
453 nifs = ifc.ifc_len / sizeof (struct ifreq);
455 /* Go through all the interfaces and get the address. */
456 while (nifs-- > 0)
457 if (__ioctl (fd, SIOCGIFADDR, &ifc.ifc_req[nifs]) >= 0)
459 /* We successfully got information about this interface. Now
460 test whether it is an IPv4 or IPv6 address. */
461 if (ifc.ifc_req[nifs].ifr_addr.sa_family == AF_INET)
462 *have_inet = 1;
463 else if (ifc.ifc_req[nifs].ifr_addr.sa_family == AF_INET6)
464 *have_inet6 = 1;
466 /* Note, this is & not &&. It works since the values are always
467 0 or 1. */
468 if (*have_inet & *have_inet6)
469 /* We can stop early. */
470 break;
473 close_not_cancel_no_status (fd);
475 #endif