if_index.c 7.6 KB

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