if_index.c 7.4 KB

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