if_index.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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; see the file COPYING.LIB. If
  14. not, see <http://www.gnu.org/licenses/>.
  15. Reworked Dec 2002 by Erik Andersen <andersen@codepoet.org>
  16. */
  17. #include <string.h>
  18. #include <alloca.h>
  19. #include <errno.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <net/if.h>
  24. #include <sys/socket.h>
  25. #include <sys/ioctl.h>
  26. #include <not-cancel.h>
  27. #include "netlinkaccess.h"
  28. extern int __opensock(void) attribute_hidden;
  29. unsigned int
  30. if_nametoindex(const char* ifname)
  31. {
  32. #ifndef SIOCGIFINDEX
  33. __set_errno (ENOSYS);
  34. return 0;
  35. #else
  36. struct ifreq ifr;
  37. int fd = __opensock();
  38. if (fd < 0)
  39. return 0;
  40. strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
  41. if (ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
  42. {
  43. /* close never fails here, fd is just a unconnected socket.
  44. *int saved_errno = errno; */
  45. close_not_cancel_no_status(fd);
  46. /*if (saved_errno == EINVAL)
  47. * __set_errno(ENOSYS); */
  48. return 0;
  49. }
  50. close_not_cancel_no_status(fd);
  51. return ifr.ifr_ifindex;
  52. #endif
  53. }
  54. libc_hidden_def(if_nametoindex)
  55. void
  56. if_freenameindex (struct if_nameindex *ifn)
  57. {
  58. struct if_nameindex *ptr = ifn;
  59. while (ptr->if_name || ptr->if_index)
  60. {
  61. free (ptr->if_name);
  62. ++ptr;
  63. }
  64. free (ifn);
  65. }
  66. libc_hidden_def(if_freenameindex)
  67. #if !__ASSUME_NETLINK_SUPPORT
  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_not_cancel_no_status (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_not_cancel_no_status (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_not_cancel_no_status (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_not_cancel_no_status (fd);
  132. return idx;
  133. #endif
  134. }
  135. #else
  136. struct if_nameindex *
  137. if_nameindex (void)
  138. {
  139. unsigned int nifs = 0;
  140. struct netlink_handle nh = { 0, 0, 0, NULL, NULL };
  141. struct if_nameindex *idx = NULL;
  142. struct netlink_res *nlp;
  143. if (__netlink_open (&nh) < 0)
  144. return NULL;
  145. /* Tell the kernel that we wish to get a list of all
  146. active interfaces. Collect all data for every interface. */
  147. if (__netlink_request (&nh, RTM_GETLINK) < 0)
  148. goto exit_free;
  149. /* Count the interfaces. */
  150. for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
  151. {
  152. struct nlmsghdr *nlh;
  153. size_t size = nlp->size;
  154. if (nlp->nlh == NULL)
  155. continue;
  156. /* Walk through all entries we got from the kernel and look, which
  157. message type they contain. */
  158. for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
  159. {
  160. /* Check if the message is what we want. */
  161. if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
  162. continue;
  163. if (nlh->nlmsg_type == NLMSG_DONE)
  164. break; /* ok */
  165. if (nlh->nlmsg_type == RTM_NEWLINK)
  166. ++nifs;
  167. }
  168. }
  169. idx = malloc ((nifs + 1) * sizeof (struct if_nameindex));
  170. if (idx == NULL)
  171. {
  172. nomem:
  173. __set_errno (ENOBUFS);
  174. goto exit_free;
  175. }
  176. /* Add the interfaces. */
  177. nifs = 0;
  178. for (nlp = nh.nlm_list; nlp; nlp = nlp->next)
  179. {
  180. struct nlmsghdr *nlh;
  181. size_t size = nlp->size;
  182. if (nlp->nlh == NULL)
  183. continue;
  184. /* Walk through all entries we got from the kernel and look, which
  185. message type they contain. */
  186. for (nlh = nlp->nlh; NLMSG_OK (nlh, size); nlh = NLMSG_NEXT (nlh, size))
  187. {
  188. /* Check if the message is what we want. */
  189. if ((pid_t) nlh->nlmsg_pid != nh.pid || nlh->nlmsg_seq != nlp->seq)
  190. continue;
  191. if (nlh->nlmsg_type == NLMSG_DONE)
  192. break; /* ok */
  193. if (nlh->nlmsg_type == RTM_NEWLINK)
  194. {
  195. struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlh);
  196. struct rtattr *rta = IFLA_RTA (ifim);
  197. size_t rtasize = IFLA_PAYLOAD (nlh);
  198. idx[nifs].if_index = ifim->ifi_index;
  199. while (RTA_OK (rta, rtasize))
  200. {
  201. char *rta_data = RTA_DATA (rta);
  202. size_t rta_payload = RTA_PAYLOAD (rta);
  203. if (rta->rta_type == IFLA_IFNAME)
  204. {
  205. idx[nifs].if_name = strndup (rta_data, rta_payload);
  206. if (idx[nifs].if_name == NULL)
  207. {
  208. idx[nifs].if_index = 0;
  209. if_freenameindex (idx);
  210. idx = NULL;
  211. goto nomem;
  212. }
  213. break;
  214. }
  215. rta = RTA_NEXT (rta, rtasize);
  216. }
  217. ++nifs;
  218. }
  219. }
  220. }
  221. idx[nifs].if_index = 0;
  222. idx[nifs].if_name = NULL;
  223. exit_free:
  224. __netlink_free_handle (&nh);
  225. __netlink_close (&nh);
  226. return idx;
  227. }
  228. #endif
  229. libc_hidden_def(if_nameindex)
  230. char *
  231. if_indextoname (unsigned int ifindex, char *ifname)
  232. {
  233. #if !defined SIOCGIFINDEX
  234. __set_errno (ENOSYS);
  235. return NULL;
  236. #else
  237. # ifdef SIOCGIFNAME
  238. /* Use ioctl to avoid searching the list. */
  239. struct ifreq ifr;
  240. int fd;
  241. fd = __opensock ();
  242. if (fd < 0)
  243. return NULL;
  244. ifr.ifr_ifindex = ifindex;
  245. if (ioctl (fd, SIOCGIFNAME, &ifr) < 0)
  246. {
  247. int serrno = errno;
  248. close_not_cancel_no_status (fd);
  249. if (serrno == ENODEV)
  250. /* POSIX requires ENXIO. */
  251. serrno = ENXIO;
  252. __set_errno (serrno);
  253. return NULL;
  254. }
  255. close_not_cancel_no_status (fd);
  256. return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
  257. # else
  258. struct if_nameindex *idx;
  259. struct if_nameindex *p;
  260. char *result = NULL;
  261. idx = if_nameindex();
  262. if (idx != NULL)
  263. {
  264. for (p = idx; p->if_index || p->if_name; ++p)
  265. if (p->if_index == ifindex)
  266. {
  267. result = strncpy (ifname, p->if_name, IFNAMSIZ);
  268. break;
  269. }
  270. if_freenameindex (idx);
  271. if (result == NULL)
  272. __set_errno (ENXIO);
  273. }
  274. return result;
  275. # endif
  276. #endif
  277. }