rtread_sysctl.c 2.0 KB

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