if_index.c 7.5 KB

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