if_index.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 __FORCE_GLIBC
  19. #include <features.h>
  20. #include <string.h>
  21. #include <alloca.h>
  22. #include <errno.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 <libc-internal.h>
  30. #include "netlinkaccess.h"
  31. extern int __opensock(void) attribute_hidden;
  32. unsigned int
  33. if_nametoindex(const char* ifname)
  34. {
  35. #ifndef SIOCGIFINDEX
  36. __set_errno (ENOSYS);
  37. return 0;
  38. #else
  39. struct ifreq ifr;
  40. int fd = __opensock();
  41. if (fd < 0)
  42. return 0;
  43. strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
  44. if (ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
  45. {
  46. /* close never fails here, fd is just a unconnected socket.
  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. libc_hidden_def(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. libc_hidden_def(if_freenameindex)
  70. #if !__ASSUME_NETLINK_SUPPORT
  71. struct if_nameindex *
  72. if_nameindex (void)
  73. {
  74. #ifndef SIOCGIFINDEX
  75. __set_errno (ENOSYS);
  76. return NULL;
  77. #else
  78. int fd = __opensock ();
  79. struct ifconf ifc;
  80. unsigned int nifs, i;
  81. int rq_len;
  82. struct if_nameindex *idx = NULL;
  83. # define RQ_IFS 4
  84. if (fd < 0)
  85. return NULL;
  86. ifc.ifc_buf = NULL;
  87. /* Guess on the correct buffer size... */
  88. rq_len = RQ_IFS * sizeof (struct ifreq);
  89. /* Read all the interfaces out of the kernel. */
  90. /* Note: alloca's in this loop are diff from glibc because it's smaller */
  91. do
  92. {
  93. ifc.ifc_buf = extend_alloca (ifc.ifc_buf, rq_len, 2 * rq_len);
  94. ifc.ifc_len = rq_len;
  95. if (ioctl (fd, SIOCGIFCONF, &ifc) < 0)
  96. {
  97. close (fd);
  98. return NULL;
  99. }
  100. }
  101. while (ifc.ifc_len == rq_len);
  102. nifs = ifc.ifc_len / sizeof(struct ifreq);
  103. idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
  104. if (idx == NULL)
  105. {
  106. close(fd);
  107. __set_errno(ENOBUFS);
  108. return NULL;
  109. }
  110. for (i = 0; i < nifs; ++i)
  111. {
  112. struct ifreq *ifr = &ifc.ifc_req[i];
  113. idx[i].if_name = strdup (ifr->ifr_name);
  114. if (idx[i].if_name == NULL
  115. || ioctl (fd, SIOCGIFINDEX, ifr) < 0)
  116. {
  117. int saved_errno = errno;
  118. unsigned int j;
  119. for (j = 0; j < i; ++j)
  120. free (idx[j].if_name);
  121. free(idx);
  122. close(fd);
  123. if (saved_errno == EINVAL)
  124. saved_errno = ENOSYS;
  125. else if (saved_errno == ENOMEM)
  126. saved_errno = ENOBUFS;
  127. __set_errno (saved_errno);
  128. return NULL;
  129. }
  130. idx[i].if_index = ifr->ifr_ifindex;
  131. }
  132. idx[i].if_index = 0;
  133. idx[i].if_name = NULL;
  134. close(fd);
  135. return idx;
  136. #endif
  137. }
  138. #else
  139. struct if_nameindex *
  140. if_nameindex (void)
  141. {
  142. unsigned int nifs = 0;
  143. struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
  144. struct if_nameindex *idx = NULL;
  145. struct netlink_res *nlp;
  146. if (__netlink_open (&nh) < 0)
  147. return NULL;
  148. /* Tell the kernel that we wish to get a list of all
  149. active interfaces. Collect all data for every interface. */
  150. if (__netlink_request (&nh, RTM_GETLINK) < 0)
  151. goto exit_free;
  152. /* Count the interfaces. */
  153. for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
  154. {
  155. struct nlmsghdr *nlh;
  156. size_t size = nlp->size;
  157. if (nlp->nlh == NULL)
  158. continue;
  159. /* Walk through all entries we got from the kernel and look, which
  160. message type they contain. */
  161. for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
  162. {
  163. /* Check if the message is what we want. */
  164. if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
  165. continue;
  166. if (nlh->nlmsg_type == NLMSG_DONE)
  167. break; /* ok */
  168. if (nlh->nlmsg_type == RTM_NEWLINK)
  169. ++nifs;
  170. }
  171. }
  172. idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
  173. if (idx == NULL)
  174. {
  175. nomem:
  176. __set_errno (ENOBUFS);
  177. goto exit_free;
  178. }
  179. /* Add the interfaces. */
  180. nifs = 0;
  181. for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
  182. {
  183. struct nlmsghdr *nlh;
  184. size_t size = nlp->size;
  185. if (nlp->nlh == NULL)
  186. continue;
  187. /* Walk through all entries we got from the kernel and look, which
  188. message type they contain. */
  189. for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
  190. {
  191. /* Check if the message is what we want. */
  192. if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
  193. continue;
  194. if (nlh->nlmsg_type == NLMSG_DONE)
  195. break; /* ok */
  196. if (nlh->nlmsg_type == RTM_NEWLINK)
  197. {
  198. struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
  199. struct rtattr *rta = IFLA_RTA (ifim);
  200. size_t rtasize = IFLA_PAYLOAD (nlh);
  201. idx[nifs].if_index = ifim->ifi_index;
  202. while (RTA_OK (rta, rtasize))
  203. {
  204. char *rta_data = RTA_DATA (rta);
  205. size_t rta_payload = RTA_PAYLOAD (rta);
  206. if (rta->rta_type == IFLA_IFNAME)
  207. {
  208. idx[nifs].if_name = strndup (rta_data, rta_payload);
  209. if (idx[nifs].if_name == NULL)
  210. {
  211. idx[nifs].if_index = 0;
  212. if_freenameindex (idx);
  213. idx = NULL;
  214. goto nomem;
  215. }
  216. break;
  217. }
  218. rta = RTA_NEXT (rta, rtasize);
  219. }
  220. ++nifs;
  221. }
  222. }
  223. }
  224. idx[nifs].if_index = 0;
  225. idx[nifs].if_name = NULL;
  226. exit_free:
  227. __netlink_free_handle (&nh);
  228. __netlink_close (&nh);
  229. return idx;
  230. }
  231. #endif
  232. libc_hidden_def(if_nameindex)
  233. char *
  234. if_indextoname (unsigned int ifindex, char *ifname)
  235. {
  236. #if !defined SIOCGIFINDEX
  237. __set_errno (ENOSYS);
  238. return NULL;
  239. #else
  240. # ifdef SIOCGIFNAME
  241. /* Use ioctl to avoid searching the list. */
  242. struct ifreq ifr;
  243. int fd;
  244. fd = __opensock ();
  245. if (fd < 0)
  246. return NULL;
  247. ifr.ifr_ifindex = ifindex;
  248. if (ioctl (fd, SIOCGIFNAME, &ifr) < 0)
  249. {
  250. int serrno = errno;
  251. close (fd);
  252. if (serrno == ENODEV)
  253. /* POSIX requires ENXIO. */
  254. serrno = ENXIO;
  255. __set_errno (serrno);
  256. return NULL;
  257. }
  258. close (fd);
  259. return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
  260. # else
  261. struct if_nameindex *idx;
  262. struct if_nameindex *p;
  263. char *result = NULL;
  264. idx = if_nameindex();
  265. if (idx != NULL)
  266. {
  267. for (p = idx; p->if_index || p->if_name; ++p)
  268. if (p->if_index == ifindex)
  269. {
  270. result = strncpy (ifname, p->if_name, IFNAMSIZ);
  271. break;
  272. }
  273. if_freenameindex (idx);
  274. if (result == NULL)
  275. __set_errno (ENXIO);
  276. }
  277. return result;
  278. # endif
  279. #endif
  280. }