if_sysctl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. int
  31. ifstat_update_sysctl ()
  32. {
  33. caddr_t ref, buf, end;
  34. size_t bufsiz;
  35. struct if_msghdr *ifm;
  36. struct interface *ifp;
  37. #define MIBSIZ 6
  38. int mib[MIBSIZ] =
  39. {
  40. CTL_NET,
  41. PF_ROUTE,
  42. 0,
  43. 0, /* AF_INET & AF_INET6 */
  44. NET_RT_IFLIST,
  45. 0
  46. };
  47. /* Query buffer size. */
  48. if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)
  49. {
  50. zlog_warn ("sysctl() error by %s", strerror (errno));
  51. return -1;
  52. }
  53. /* We free this memory at the end of this function. */
  54. ref = buf = XMALLOC (MTYPE_TMP, bufsiz);
  55. /* Fetch interface informations into allocated buffer. */
  56. if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)
  57. {
  58. zlog (NULL, LOG_WARNING, "sysctl error by %s", strerror (errno));
  59. return -1;
  60. }
  61. /* Parse both interfaces and addresses. */
  62. for (end = buf + bufsiz; buf < end; buf += ifm->ifm_msglen)
  63. {
  64. ifm = (struct if_msghdr *) buf;
  65. if (ifm->ifm_type == RTM_IFINFO)
  66. {
  67. ifp = if_lookup_by_index (ifm->ifm_index);
  68. if (ifp)
  69. ifp->stats = ifm->ifm_data;
  70. }
  71. }
  72. /* Free sysctl buffer. */
  73. XFREE (MTYPE_TMP, ref);
  74. return 0;
  75. }
  76. /* Interface listing up function using sysctl(). */
  77. void
  78. interface_list ()
  79. {
  80. caddr_t ref, buf, end;
  81. size_t bufsiz;
  82. struct if_msghdr *ifm;
  83. int ifm_read (struct if_msghdr *);
  84. int ifam_read (struct ifa_msghdr *);
  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", 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", 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. }