if_index.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright (C) 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005
  2. Free Software Foundation, Inc.
  3. 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.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Reworked Dec 2002 by Erik Andersen <andersen@codepoet.org>
  17. */
  18. #define strndup __strndup
  19. #define __FORCE_GLIBC
  20. #include <features.h>
  21. #define __USE_GNU
  22. #include <string.h>
  23. #include <alloca.h>
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <net/if.h>
  29. #include <sys/socket.h>
  30. #include <sys/ioctl.h>
  31. #include <libc-internal.h>
  32. extern int __opensock(void) attribute_hidden;
  33. unsigned int
  34. if_nametoindex(const char* ifname)
  35. {
  36. #ifndef SIOCGIFINDEX
  37. __set_errno (ENOSYS);
  38. return 0;
  39. #else
  40. struct ifreq ifr;
  41. int fd = __opensock();
  42. if (fd < 0)
  43. return 0;
  44. __strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
  45. if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
  46. {
  47. int saved_errno = errno;
  48. __close(fd);
  49. if (saved_errno == EINVAL)
  50. __set_errno(ENOSYS);
  51. return 0;
  52. }
  53. __close(fd);
  54. return ifr.ifr_ifindex;
  55. #endif
  56. }
  57. hidden_strong_alias(if_nametoindex,__if_nametoindex)
  58. void
  59. if_freenameindex (struct if_nameindex *ifn)
  60. {
  61. struct if_nameindex *ptr = ifn;
  62. while (ptr->if_name || ptr->if_index)
  63. {
  64. free (ptr->if_name);
  65. ++ptr;
  66. }
  67. free (ifn);
  68. }
  69. struct if_nameindex *
  70. if_nameindex (void)
  71. {
  72. #ifndef SIOCGIFINDEX
  73. __set_errno (ENOSYS);
  74. return NULL;
  75. #else
  76. int fd = __opensock ();
  77. struct ifconf ifc;
  78. unsigned int nifs, i;
  79. int rq_len;
  80. struct if_nameindex *idx = NULL;
  81. # define RQ_IFS 4
  82. if (fd < 0)
  83. return NULL;
  84. ifc.ifc_buf = NULL;
  85. /* Guess on the correct buffer size... */
  86. rq_len = RQ_IFS * sizeof (struct ifreq);
  87. /* Read all the interfaces out of the kernel. */
  88. /* Note: alloca's in this loop are diff from glibc because it's smaller */
  89. do
  90. {
  91. ifc.ifc_buf = extend_alloca (ifc.ifc_buf, rq_len, 2 * rq_len);
  92. ifc.ifc_len = rq_len;
  93. if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0)
  94. {
  95. __close (fd);
  96. return NULL;
  97. }
  98. }
  99. while (ifc.ifc_len == rq_len);
  100. nifs = ifc.ifc_len / sizeof(struct ifreq);
  101. idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
  102. if (idx == NULL)
  103. {
  104. __close(fd);
  105. __set_errno(ENOBUFS);
  106. return NULL;
  107. }
  108. for (i = 0; i < nifs; ++i)
  109. {
  110. struct ifreq *ifr = &ifc.ifc_req[i];
  111. idx[i].if_name = __strdup (ifr->ifr_name);
  112. if (idx[i].if_name == NULL
  113. || __ioctl (fd, SIOCGIFINDEX, ifr) < 0)
  114. {
  115. int saved_errno = errno;
  116. unsigned int j;
  117. for (j = 0; j < i; ++j)
  118. free (idx[j].if_name);
  119. free(idx);
  120. __close(fd);
  121. if (saved_errno == EINVAL)
  122. saved_errno = ENOSYS;
  123. else if (saved_errno == ENOMEM)
  124. saved_errno = ENOBUFS;
  125. __set_errno (saved_errno);
  126. return NULL;
  127. }
  128. idx[i].if_index = ifr->ifr_ifindex;
  129. }
  130. idx[i].if_index = 0;
  131. idx[i].if_name = NULL;
  132. __close(fd);
  133. return idx;
  134. #endif
  135. }
  136. char *
  137. if_indextoname (unsigned int ifindex, char *ifname)
  138. {
  139. #if !defined SIOCGIFINDEX
  140. __set_errno (ENOSYS);
  141. return NULL;
  142. #else
  143. # ifdef SIOCGIFNAME
  144. /* Use ioctl to avoid searching the list. */
  145. struct ifreq ifr;
  146. int fd;
  147. fd = __opensock ();
  148. if (fd < 0)
  149. return NULL;
  150. ifr.ifr_ifindex = ifindex;
  151. if (__ioctl (fd, SIOCGIFNAME, &ifr) < 0)
  152. {
  153. int serrno = errno;
  154. __close (fd);
  155. if (serrno == ENODEV)
  156. /* POSIX requires ENXIO. */
  157. serrno = ENXIO;
  158. __set_errno (serrno);
  159. return NULL;
  160. }
  161. __close (fd);
  162. return __strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
  163. # else
  164. struct if_nameindex *idx;
  165. struct if_nameindex *p;
  166. char *result = NULL;
  167. idx = if_nameindex();
  168. if (idx != NULL)
  169. {
  170. for (p = idx; p->if_index || p->if_name; ++p)
  171. if (p->if_index == ifindex)
  172. {
  173. result = __strncpy (ifname, p->if_name, IFNAMSIZ);
  174. break;
  175. }
  176. if_freenameindex (idx);
  177. if (result == NULL)
  178. __set_errno (ENXIO);
  179. }
  180. return result;
  181. # endif
  182. #endif
  183. }