rtread_sysctl.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #include "vrf.h"
  26. #include "zebra/zserv.h"
  27. #include "zebra/rt.h"
  28. #include "zebra/kernel_socket.h"
  29. /* Kernel routing table read up by sysctl function. */
  30. void
  31. route_read (struct zebra_vrf *zvrf)
  32. {
  33. caddr_t buf, end, ref;
  34. size_t bufsiz;
  35. struct rt_msghdr *rtm;
  36. #define MIBSIZ 6
  37. int mib[MIBSIZ] =
  38. {
  39. CTL_NET,
  40. PF_ROUTE,
  41. 0,
  42. 0,
  43. NET_RT_DUMP,
  44. 0
  45. };
  46. if (zvrf->vrf_id != VRF_DEFAULT)
  47. return;
  48. /* Get buffer size. */
  49. if (sysctl (mib, MIBSIZ, NULL, &bufsiz, NULL, 0) < 0)
  50. {
  51. zlog_warn ("sysctl fail: %s", safe_strerror (errno));
  52. return;
  53. }
  54. /* Allocate buffer. */
  55. ref = buf = XMALLOC (MTYPE_TMP, bufsiz);
  56. /* Read routing table information by calling sysctl(). */
  57. if (sysctl (mib, MIBSIZ, buf, &bufsiz, NULL, 0) < 0)
  58. {
  59. zlog_warn ("sysctl() fail by %s", safe_strerror (errno));
  60. return;
  61. }
  62. for (end = buf + bufsiz; buf < end; buf += rtm->rtm_msglen)
  63. {
  64. rtm = (struct rt_msghdr *) buf;
  65. /* We must set RTF_DONE here, so rtm_read() doesn't ignore the message. */
  66. SET_FLAG (rtm->rtm_flags, RTF_DONE);
  67. rtm_read (rtm);
  68. }
  69. /* Free buffer. */
  70. XFREE (MTYPE_TMP, ref);
  71. return;
  72. }