if_index.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. void
  58. if_freenameindex (struct if_nameindex *ifn)
  59. {
  60. struct if_nameindex *ptr = ifn;
  61. while (ptr->if_name || ptr->if_index)
  62. {
  63. free (ptr->if_name);
  64. ++ptr;
  65. }
  66. free (ifn);
  67. }
  68. struct if_nameindex *
  69. if_nameindex (void)
  70. {
  71. #ifndef SIOCGIFINDEX
  72. __set_errno (ENOSYS);
  73. return NULL;
  74. #else
  75. int fd = __opensock ();
  76. struct ifconf ifc;
  77. unsigned int nifs, i;
  78. int rq_len;
  79. struct if_nameindex *idx = NULL;
  80. # define RQ_IFS 4
  81. if (fd < 0)
  82. return NULL;
  83. ifc.ifc_buf = NULL;
  84. /* Guess on the correct buffer size... */
  85. rq_len = RQ_IFS * sizeof (struct ifreq);
  86. /* Read all the interfaces out of the kernel. */
  87. /* Note: alloca's in this loop are diff from glibc because it's smaller */
  88. do
  89. {
  90. ifc.ifc_buf = extend_alloca (ifc.ifc_buf, rq_len, 2 * rq_len);
  91. ifc.ifc_len = rq_len;
  92. if (__ioctl (fd, SIOCGIFCONF, &ifc) < 0)
  93. {
  94. __close (fd);
  95. return NULL;
  96. }
  97. }
  98. while (ifc.ifc_len == rq_len);
  99. nifs = ifc.ifc_len / sizeof(struct ifreq);
  100. idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
  101. if (idx == NULL)
  102. {
  103. __close(fd);
  104. __set_errno(ENOBUFS);
  105. return NULL;
  106. }
  107. for (i = 0; i < nifs; ++i)
  108. {
  109. struct ifreq *ifr = &ifc.ifc_req[i];
  110. idx[i].if_name = __strdup (ifr->ifr_name);
  111. if (idx[i].if_name == NULL
  112. || __ioctl (fd, SIOCGIFINDEX, ifr) < 0)
  113. {
  114. int saved_errno = errno;
  115. unsigned int j;
  116. for (j = 0; j < i; ++j)
  117. free (idx[j].if_name);
  118. free(idx);
  119. __close(fd);
  120. if (saved_errno == EINVAL)
  121. saved_errno = ENOSYS;
  122. else if (saved_errno == ENOMEM)
  123. saved_errno = ENOBUFS;
  124. __set_errno (saved_errno);
  125. return NULL;
  126. }
  127. idx[i].if_index = ifr->ifr_ifindex;
  128. }
  129. idx[i].if_index = 0;
  130. idx[i].if_name = NULL;
  131. __close(fd);
  132. return idx;
  133. #endif
  134. }
  135. char *
  136. if_indextoname (unsigned int ifindex, char *ifname)
  137. {
  138. #if !defined SIOCGIFINDEX
  139. __set_errno (ENOSYS);
  140. return NULL;
  141. #else
  142. # ifdef SIOCGIFNAME
  143. /* Use ioctl to avoid searching the list. */
  144. struct ifreq ifr;
  145. int fd;
  146. fd = __opensock ();
  147. if (fd < 0)
  148. return NULL;
  149. ifr.ifr_ifindex = ifindex;
  150. if (__ioctl (fd, SIOCGIFNAME, &ifr) < 0)
  151. {
  152. int serrno = errno;
  153. __close (fd);
  154. if (serrno == ENODEV)
  155. /* POSIX requires ENXIO. */
  156. serrno = ENXIO;
  157. __set_errno (serrno);
  158. return NULL;
  159. }
  160. __close (fd);
  161. return __strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
  162. # else
  163. struct if_nameindex *idx;
  164. struct if_nameindex *p;
  165. char *result = NULL;
  166. idx = if_nameindex();
  167. if (idx != NULL)
  168. {
  169. for (p = idx; p->if_index || p->if_name; ++p)
  170. if (p->if_index == ifindex)
  171. {
  172. result = __strncpy (ifname, p->if_name, IFNAMSIZ);
  173. break;
  174. }
  175. if_freenameindex (idx);
  176. if (result == NULL)
  177. __set_errno (ENXIO);
  178. }
  179. return result;
  180. # endif
  181. #endif
  182. }