if_index.c 7.5 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 strndup __strndup
  19. #define __FORCE_GLIBC
  20. #include <features.h>
  21. #define __USE_GNU
  22. #include <string.h>
  23. #include <alloca.h>
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <net/if.h>
  29. #include <sys/socket.h>
  30. #include <sys/ioctl.h>
  31. #include <libc-internal.h>
  32. #include "netlinkaccess.h"
  33. extern int __opensock(void) attribute_hidden;
  34. unsigned int
  35. if_nametoindex(const char* ifname)
  36. {
  37. #ifndef SIOCGIFINDEX
  38. __set_errno (ENOSYS);
  39. return 0;
  40. #else
  41. struct ifreq ifr;
  42. int fd = __opensock();
  43. if (fd < 0)
  44. return 0;
  45. __strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
  46. if (__ioctl (fd, SIOCGIFINDEX, &ifr) < 0)
  47. {
  48. int saved_errno = errno;
  49. __close(fd);
  50. if (saved_errno == EINVAL)
  51. __set_errno(ENOSYS);
  52. return 0;
  53. }
  54. __close(fd);
  55. return ifr.ifr_ifindex;
  56. #endif
  57. }
  58. hidden_strong_alias(if_nametoindex,__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. hidden_strong_alias(if_freenameindex,__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 (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(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(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(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. hidden_strong_alias(if_nameindex,__if_nameindex)
  234. #if 0
  235. struct if_nameindex *
  236. if_nameindex (void)
  237. {
  238. return (if_nameindex_netlink () != NULL ? : if_nameindex_ioctl ());
  239. }
  240. #endif
  241. char *
  242. if_indextoname (unsigned int ifindex, char *ifname)
  243. {
  244. #if !defined SIOCGIFINDEX
  245. __set_errno (ENOSYS);
  246. return NULL;
  247. #else
  248. # ifdef SIOCGIFNAME
  249. /* Use ioctl to avoid searching the list. */
  250. struct ifreq ifr;
  251. int fd;
  252. fd = __opensock ();
  253. if (fd < 0)
  254. return NULL;
  255. ifr.ifr_ifindex = ifindex;
  256. if (__ioctl (fd, SIOCGIFNAME, &ifr) < 0)
  257. {
  258. int serrno = errno;
  259. __close (fd);
  260. if (serrno == ENODEV)
  261. /* POSIX requires ENXIO. */
  262. serrno = ENXIO;
  263. __set_errno (serrno);
  264. return NULL;
  265. }
  266. __close (fd);
  267. return __strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
  268. # else
  269. struct if_nameindex *idx;
  270. struct if_nameindex *p;
  271. char *result = NULL;
  272. idx = __if_nameindex();
  273. if (idx != NULL)
  274. {
  275. for (p = idx; p->if_index || p->if_name; ++p)
  276. if (p->if_index == ifindex)
  277. {
  278. result = __strncpy (ifname, p->if_name, IFNAMSIZ);
  279. break;
  280. }
  281. __if_freenameindex (idx);
  282. if (result == NULL)
  283. __set_errno (ENXIO);
  284. }
  285. return result;
  286. # endif
  287. #endif
  288. }