if_index.c 7.4 KB

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