Remove sys/syscall.h, sys/types.h, linux/posix_types.h, sysdep.h and pthread-function...
[glibc.git] / sysdeps / unix / sysv / linux / if_index.c
blob377ccf51993c2a488fdf31170d4c5763b9777aa5
1 /* Copyright (C) 1997,98,99,2000,2002,2003,2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <alloca.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <net/if.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
28 #include <bits/libc-lock.h>
29 #include <not-cancel.h>
31 #include "netlinkaccess.h"
34 /* Variable to signal whether SIOCGIFCONF is not available. */
35 # if __ASSUME_SIOCGIFNAME == 0
36 static int old_siocgifconf;
37 #else
38 # define old_siocgifconf 0
39 #endif
42 unsigned int
43 if_nametoindex (const char *ifname)
45 #ifndef SIOCGIFINDEX
46 __set_errno (ENOSYS);
47 return 0;
48 #else
49 struct ifreq ifr;
50 int fd = __opensock ();
52 if (fd < 0)
53 return 0;
55 strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
56 if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
58 int saved_errno = errno;
59 close_not_cancel_no_status (fd);
60 if (saved_errno == EINVAL)
61 __set_errno (ENOSYS);
62 return 0;
64 close_not_cancel_no_status (fd);
65 return ifr.ifr_ifindex;
66 #endif
68 libc_hidden_def (if_nametoindex)
71 void
72 if_freenameindex (struct if_nameindex *ifn)
74 struct if_nameindex *ptr = ifn;
75 while (ptr->if_name || ptr->if_index)
77 free (ptr->if_name);
78 ++ptr;
80 free (ifn);
82 libc_hidden_def (if_freenameindex)
85 #if __ASSUME_NETLINK_SUPPORT == 0
86 static struct if_nameindex *
87 if_nameindex_ioctl (void)
89 int fd = __opensock ();
90 struct ifconf ifc;
91 unsigned int nifs, i;
92 int rq_len;
93 struct if_nameindex *idx = NULL;
94 # define RQ_IFS 4
96 if (fd < 0)
97 return NULL;
99 ifc.ifc_buf = NULL;
101 /* We may be able to get the needed buffer size directly, rather than
102 guessing. */
103 if (! old_siocgifconf)
105 ifc.ifc_buf = NULL;
106 ifc.ifc_len = 0;
107 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0 || ifc.ifc_len == 0)
109 # if __ASSUME_SIOCGIFNAME == 0
110 old_siocgifconf = 1;
111 # endif
112 rq_len = RQ_IFS * sizeof (struct ifreq);
114 else
115 rq_len = ifc.ifc_len;
117 else
118 rq_len = RQ_IFS * sizeof (struct ifreq);
120 /* Read all the interfaces out of the kernel. */
121 ifc.ifc_buf = alloca (rq_len);
122 ifc.ifc_len = rq_len;
123 while (1)
125 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0)
127 close_not_cancel_no_status (fd);
128 return NULL;
130 if (ifc.ifc_len < rq_len || ! old_siocgifconf)
131 break;
133 ifc.ifc_buf = extend_alloca (ifc.ifc_buf, rq_len, 2 * rq_len);
134 ifc.ifc_len = rq_len;
137 nifs = ifc.ifc_len / sizeof (struct ifreq);
139 idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
140 if (idx == NULL)
142 close_not_cancel_no_status (fd);
143 __set_errno (ENOBUFS);
144 return NULL;
147 for (i = 0; i < nifs; ++i)
149 struct ifreq *ifr = &ifc.ifc_req[i];
150 idx[i].if_name = __strdup (ifr->ifr_name);
151 if (idx[i].if_name == NULL
152 || __ioctl (fd, SIOCGIFINDEX, ifr) < 0)
154 int saved_errno = errno;
155 unsigned int j;
157 for (j = 0; j < i; ++j)
158 free (idx[j].if_name);
159 free (idx);
160 close_not_cancel_no_status (fd);
161 if (saved_errno == EINVAL)
162 saved_errno = ENOSYS;
163 else if (saved_errno == ENOMEM)
164 saved_errno = ENOBUFS;
165 __set_errno (saved_errno);
166 return NULL;
168 idx[i].if_index = ifr->ifr_ifindex;
171 idx[i].if_index = 0;
172 idx[i].if_name = NULL;
174 close_not_cancel_no_status (fd);
175 return idx;
177 #endif
180 static struct if_nameindex *
181 if_nameindex_netlink (void)
183 struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
184 struct if_nameindex *idx = NULL;
186 if (__no_netlink_support || __netlink_open (&nh) < 0)
187 return NULL;
190 /* Tell the kernel that we wish to get a list of all
191 active interfaces. */
192 if (__netlink_sendreq (&nh, RTM_GETLINK) < 0)
193 goto exit_close;
195 /* Collect all data for every interface. */
196 if (__netlink_receive (&nh) < 0)
197 goto exit_free;
199 /* Count the interfaces. */
200 unsigned int nifs = 0;
201 for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next)
203 struct nlmsghdr *nlh;
204 size_t size = nlp->size;
206 if (nlp->nlh == NULL)
207 continue;
209 /* Walk through all entries we got from the kernel and look, which
210 message type they contain. */
211 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
213 /* Check if the message is what we want. */
214 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
215 continue;
217 if (nlh->nlmsg_type == NLMSG_DONE)
218 break; /* ok */
220 if (nlh->nlmsg_type == RTM_NEWLINK)
221 ++nifs;
225 idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
226 if (idx == NULL)
228 nomem:
229 __set_errno (ENOBUFS);
230 goto exit_free;
233 /* Add the interfaces. */
234 nifs = 0;
235 for (struct netlink_res *nlp = nh.nlm_list; nlp; nlp = nlp->next)
237 struct nlmsghdr *nlh;
238 size_t size = nlp->size;
240 if (nlp->nlh == NULL)
241 continue;
243 /* Walk through all entries we got from the kernel and look, which
244 message type they contain. */
245 for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
247 /* Check if the message is what we want. */
248 if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
249 continue;
251 if (nlh->nlmsg_type == NLMSG_DONE)
252 break; /* ok */
254 if (nlh->nlmsg_type == RTM_NEWLINK)
256 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
257 struct rtattr *rta = IFLA_RTA (ifim);
258 size_t rtasize = IFLA_PAYLOAD (nlh);
260 idx[nifs].if_index = ifim->ifi_index;
262 while (RTA_OK (rta, rtasize))
264 char *rta_data = RTA_DATA (rta);
265 size_t rta_payload = RTA_PAYLOAD (rta);
267 if (rta->rta_type == IFLA_IFNAME)
269 idx[nifs].if_name = __strndup (rta_data, rta_payload);
270 if (idx[nifs].if_name == NULL)
272 idx[nifs].if_index = 0;
273 if_freenameindex (idx);
274 idx = NULL;
275 goto nomem;
277 break;
280 rta = RTA_NEXT (rta, rtasize);
283 ++nifs;
288 idx[nifs].if_index = 0;
289 idx[nifs].if_name = NULL;
291 exit_free:
292 __netlink_free_handle (&nh);
293 exit_close:
294 __netlink_close (&nh);
296 return idx;
300 struct if_nameindex *
301 if_nameindex (void)
303 #ifndef SIOCGIFINDEX
304 __set_errno (ENOSYS);
305 return NULL;
306 #else
307 struct if_nameindex *result = if_nameindex_netlink ();
308 # if __ASSUME_NETLINK_SUPPORT == 0
309 if (__no_netlink_support)
310 result = if_nameindex_ioctl ();
311 # endif
312 return result;
313 #endif
315 libc_hidden_def (if_nameindex)
318 char *
319 if_indextoname (unsigned int ifindex, char *ifname)
321 #if !defined SIOCGIFINDEX && __ASSUME_SIOCGIFNAME == 0
322 __set_errno (ENOSYS);
323 return NULL;
324 #else
325 # if __ASSUME_SIOCGIFNAME == 0
326 struct if_nameindex *idx;
327 struct if_nameindex *p;
328 char *result = NULL;
329 # endif
331 # if defined SIOCGIFNAME || __ASSUME_SIOCGIFNAME > 0
332 /* We may be able to do the conversion directly, rather than searching a
333 list. This ioctl is not present in kernels before version 2.1.50. */
334 struct ifreq ifr;
335 int fd;
336 # if __ASSUME_SIOCGIFNAME == 0
337 static int siocgifname_works_not;
339 if (!siocgifname_works_not)
340 # endif
342 # if __ASSUME_SIOCGIFNAME == 0
343 int serrno = errno;
344 # endif
345 int status;
347 fd = __opensock ();
349 if (fd < 0)
350 return NULL;
352 ifr.ifr_ifindex = ifindex;
353 status = __ioctl (fd, SIOCGIFNAME, &ifr);
355 close_not_cancel_no_status (fd);
357 if (status < 0)
359 # if __ASSUME_SIOCGIFNAME == 0
360 if (errno == EINVAL)
361 siocgifname_works_not = 1; /* Don't make the same mistake twice. */
362 else
363 # endif
365 if (errno == ENODEV)
366 /* POSIX requires ENXIO. */
367 __set_errno (ENXIO);
369 return NULL;
372 else
373 return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
375 # if __ASSUME_SIOCGIFNAME == 0
376 __set_errno (serrno);
377 # endif
379 # endif
381 # if __ASSUME_SIOCGIFNAME == 0
382 idx = if_nameindex ();
384 if (idx != NULL)
386 for (p = idx; p->if_index || p->if_name; ++p)
387 if (p->if_index == ifindex)
389 result = strncpy (ifname, p->if_name, IFNAMSIZ);
390 break;
393 if_freenameindex (idx);
395 if (result == NULL)
396 __set_errno (ENXIO);
398 return result;
399 # endif
400 #endif
402 libc_hidden_def (if_indextoname)
405 #if 0
406 void
407 internal_function
408 __protocol_available (int *have_inet, int *have_inet6)
410 int fd = __opensock ();
411 unsigned int nifs;
412 int rq_len;
413 struct ifconf ifc;
414 # define RQ_IFS 4
416 /* Wirst case assumption. */
417 *have_inet = 0;
418 *have_inet6 = 0;
420 if (fd < 0)
421 /* We cannot open the socket. No networking at all? */
422 return;
424 /* We may be able to get the needed buffer size directly, rather than
425 guessing. */
426 if (! old_siocgifconf)
428 ifc.ifc_buf = NULL;
429 ifc.ifc_len = 0;
430 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0 || ifc.ifc_len == 0)
432 # if __ASSUME_SIOCGIFNAME == 0
433 old_siocgifconf = 1;
434 # endif
435 rq_len = RQ_IFS * sizeof (struct ifreq);
437 else
438 rq_len = ifc.ifc_len;
440 else
441 rq_len = RQ_IFS * sizeof (struct ifreq);
443 /* Read all the interfaces out of the kernel. */
446 ifc.ifc_buf = alloca (ifc.ifc_len = rq_len);
447 if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0)
449 close_not_cancel_no_status (fd);
450 return;
452 rq_len *= 2;
454 while (ifc.ifc_len == rq_len && old_siocgifconf);
456 nifs = ifc.ifc_len / sizeof (struct ifreq);
458 /* Go through all the interfaces and get the address. */
459 while (nifs-- > 0)
460 if (__ioctl (fd, SIOCGIFADDR, &ifc.ifc_req[nifs]) >= 0)
462 /* We successfully got information about this interface. Now
463 test whether it is an IPv4 or IPv6 address. */
464 if (ifc.ifc_req[nifs].ifr_addr.sa_family == AF_INET)
465 *have_inet = 1;
466 else if (ifc.ifc_req[nifs].ifr_addr.sa_family == AF_INET6)
467 *have_inet6 = 1;
469 /* Note, this is & not &&. It works since the values are always
470 0 or 1. */
471 if (*have_inet & *have_inet6)
472 /* We can stop early. */
473 break;
476 close_not_cancel_no_status (fd);
478 #endif