if_index.c 7.6 KB

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