if_sysctl.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "interface.h"
  31. #include "zebra/rt.h"
  32. #include "zebra/kernel_socket.h"
  33. void
  34. ifstat_update_sysctl (void)
  35. {
  36. caddr_t ref, buf, end;
  37. size_t bufsiz;
  38. struct if_msghdr *ifm;
  39. struct interface *ifp;
  40. #define MIBSIZ 6
  41. int mib[MIBSIZ] =
  42. {
  43. CTL_NET,
  44. PF_ROUTE,
  45. 0,
  46. 0, /* AF_INET & AF_INET6 */
  47. NET_RT_IFLIST,
  48. 0
  49. };
  50. /* Query buffer size. */
  51. if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)
  52. {
  53. zlog_warn ("sysctl() error by %s", safe_strerror (errno));
  54. return;
  55. }
  56. /* We free this memory at the end of this function. */
  57. ref = buf = XMALLOC (MTYPE_TMP, bufsiz);
  58. /* Fetch interface informations into allocated buffer. */
  59. if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)
  60. {
  61. zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno));
  62. return;
  63. }
  64. /* Parse both interfaces and addresses. */
  65. for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen)
  66. {
  67. ifm = (struct if_msghdr *) buf;
  68. if (ifm->ifm_type == RTM_IFINFO)
  69. {
  70. ifp = if_lookup_by_index (ifm->ifm_index);
  71. if (ifp)
  72. ifp->stats = ifm->ifm_data;
  73. }
  74. }
  75. /* Free sysctl buffer. */
  76. XFREE (MTYPE_TMP, ref);
  77. return;
  78. }
  79. /* Interface listing up function using sysctl(). */
  80. void
  81. interface_list ()
  82. {
  83. caddr_t ref, buf, end;
  84. size_t bufsiz;
  85. struct if_msghdr *ifm;
  86. #define MIBSIZ 6
  87. int mib[MIBSIZ] =
  88. {
  89. CTL_NET,
  90. PF_ROUTE,
  91. 0,
  92. 0, /* AF_INET & AF_INET6 */
  93. NET_RT_IFLIST,
  94. 0
  95. };
  96. /* Query buffer size. */
  97. if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)
  98. {
  99. zlog (NULL, LOG_WARNING, "sysctl() error by %s", safe_strerror (errno));
  100. return;
  101. }
  102. /* We free this memory at the end of this function. */
  103. ref = buf = XMALLOC (MTYPE_TMP, bufsiz);
  104. /* Fetch interface informations into allocated buffer. */
  105. if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)
  106. {
  107. zlog (NULL, LOG_WARNING, "sysctl error by %s", safe_strerror (errno));
  108. return;
  109. }
  110. /* Parse both interfaces and addresses. */
  111. for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen)
  112. {
  113. ifm = (struct if_msghdr *) buf;
  114. switch (ifm->ifm_type)
  115. {
  116. case RTM_IFINFO:
  117. ifm_read (ifm);
  118. break;
  119. case RTM_NEWADDR:
  120. ifam_read ((struct ifa_msghdr *) ifm);
  121. break;
  122. default:
  123. zlog_info ("interfaces_list(): unexpected message type");
  124. XFREE (MTYPE_TMP, ref);
  125. return;
  126. break;
  127. }
  128. }
  129. /* Free sysctl buffer. */
  130. XFREE (MTYPE_TMP, ref);
  131. }