rtread_sysctl.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Kernel routing table read 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 "memory.h"
  24. #include "log.h"
  25. /* Kernel routing table read up by sysctl function. */
  26. int
  27. route_read ()
  28. {
  29. caddr_t buf, end, ref;
  30. size_t bufsiz;
  31. struct rt_msghdr *rtm;
  32. void rtm_read (struct rt_msghdr *);
  33. #define MIBSIZ 6
  34. int mib[MIBSIZ] =
  35. {
  36. CTL_NET,
  37. PF_ROUTE,
  38. 0,
  39. 0,
  40. NET_RT_DUMP,
  41. 0
  42. };
  43. /* Get buffer size. */
  44. if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)
  45. {
  46. zlog_warn ("sysctl fail: %s", strerror (errno));
  47. return -1;
  48. }
  49. /* Allocate buffer. */
  50. ref = buf = XMALLOC (MTYPE_TMP, bufsiz);
  51. /* Read routing table information by calling sysctl(). */
  52. if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)
  53. {
  54. zlog_warn ("sysctl() fail by %s", strerror (errno));
  55. return -1;
  56. }
  57. for (end = buf + bufsiz; buf < end; buf += rtm->rtm_msglen)
  58. {
  59. rtm = (struct rt_msghdr *) buf;
  60. rtm_read (rtm);
  61. }
  62. /* Free buffer. */
  63. XFREE (MTYPE_TMP, ref);
  64. return 0;
  65. }