if_sysctl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Get interface's address and mask information by sysctl() function.
  3. * Copyright (C) 1997, 98 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "if.h"
  24. #include "sockunion.h"
  25. #include "prefix.h"
  26. #include "connected.h"
  27. #include "memory.h"
  28. #include "ioctl.h"
  29. #include "log.h"
  30. #include "zebra/rt.h"
  31. #include "zebra/kernel_socket.h"
  32. void
  33. ifstat_update_sysctl (void)
  34. {
  35. caddr_t ref, buf, end;
  36. size_t bufsiz;
  37. struct if_msghdr *ifm;
  38. struct interface *ifp;
  39. #define MIBSIZ 6
  40. int mib[MIBSIZ] =
  41. {
  42. CTL_NET,
  43. PF_ROUTE,
  44. 0,
  45. 0, /* AF_INET & AF_INET6 */
  46. NET_RT_IFLIST,
  47. 0
  48. };
  49. /* Query buffer size. */
  50. if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)
  51. {
  52. zlog_warn ("sysctl() error by %s", safe_strerror (errno));
  53. return;
  54. }
  55. /* We free this memory at the end of this function. */
  56. ref = buf = XMALLOC (MTYPE_TMP, bufsiz);
  57. /* Fetch interface informations into allocated buffer. */
  58. if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)
  59. {
  60. zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno));
  61. return;
  62. }
  63. /* Parse both interfaces and addresses. */
  64. for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen)
  65. {
  66. ifm = (struct if_msghdr *) buf;
  67. if (ifm->ifm_type == RTM_IFINFO)
  68. {
  69. ifp = if_lookup_by_index (ifm->ifm_index);
  70. if (ifp)
  71. ifp->stats = ifm->ifm_data;
  72. }
  73. }
  74. /* Free sysctl buffer. */
  75. XFREE (MTYPE_TMP, ref);
  76. return;
  77. }
  78. /* Interface listing up function using sysctl(). */
  79. void
  80. interface_list ()
  81. {
  82. caddr_t ref, buf, end;
  83. size_t bufsiz;
  84. struct if_msghdr *ifm;
  85. #define MIBSIZ 6
  86. int mib[MIBSIZ] =
  87. {
  88. CTL_NET,
  89. PF_ROUTE,
  90. 0,
  91. 0, /* AF_INET & AF_INET6 */
  92. NET_RT_IFLIST,
  93. 0
  94. };
  95. /* Query buffer size. */
  96. if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)
  97. {
  98. zlog (NULL, LOG_WARNING, "sysctl() error by %s", safe_strerror (errno));
  99. return;
  100. }
  101. /* We free this memory at the end of this function. */
  102. ref = buf = XMALLOC (MTYPE_TMP, bufsiz);
  103. /* Fetch interface informations into allocated buffer. */
  104. if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)
  105. {
  106. zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno));
  107. return;
  108. }
  109. /* Parse both interfaces and addresses. */
  110. for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen)
  111. {
  112. ifm = (struct if_msghdr *) buf;
  113. switch (ifm->ifm_type)
  114. {
  115. case RTM_IFINFO:
  116. ifm_read (ifm);
  117. break;
  118. case RTM_NEWADDR:
  119. ifam_read ((struct ifa_msghdr *) ifm);
  120. break;
  121. default:
  122. zlog_info ("interfaces_list(): unexpected message type");
  123. XFREE (MTYPE_TMP, ref);
  124. return;
  125. break;
  126. }
  127. }
  128. /* Free sysctl buffer. */
  129. XFREE (MTYPE_TMP, ref);
  130. }