2 * Get interface's address and mask information by sysctl() function.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
26 #include "sockunion.h"
28 #include "connected.h"
34 #include "zebra/kernel_socket.h"
37 ifstat_update_sysctl (void)
39 caddr_t ref
, buf
, end
;
41 struct if_msghdr
*ifm
;
42 struct interface
*ifp
;
50 0, /* AF_INET & AF_INET6 */
55 /* Query buffer size. */
56 if (sysctl (mib
, MIBSIZ
, NULL
, &bufsiz
, NULL
, 0) < 0)
58 zlog_warn ("sysctl() error by %s", safe_strerror (errno
));
62 /* We free this memory at the end of this function. */
63 ref
= buf
= XMALLOC (MTYPE_TMP
, bufsiz
);
65 /* Fetch interface informations into allocated buffer. */
66 if (sysctl (mib
, MIBSIZ
, buf
, &bufsiz
, NULL
, 0) < 0)
68 zlog (NULL
, LOG_WARNING
, "sysctl error by %s", safe_strerror (errno
));
72 /* Parse both interfaces and addresses. */
73 for (end
= buf
+ bufsiz
; buf
< end
; buf
+= ifm
->ifm_msglen
)
75 ifm
= (struct if_msghdr
*) buf
;
76 if (ifm
->ifm_type
== RTM_IFINFO
)
78 ifp
= if_lookup_by_index (ifm
->ifm_index
);
80 ifp
->stats
= ifm
->ifm_data
;
84 /* Free sysctl buffer. */
85 XFREE (MTYPE_TMP
, ref
);
90 /* Interface listing up function using sysctl(). */
94 caddr_t ref
, buf
, end
;
96 struct if_msghdr
*ifm
;
104 0, /* AF_INET & AF_INET6 */
109 /* Query buffer size. */
110 if (sysctl (mib
, MIBSIZ
, NULL
, &bufsiz
, NULL
, 0) < 0)
112 zlog (NULL
, LOG_WARNING
, "sysctl() error by %s", safe_strerror (errno
));
116 /* We free this memory at the end of this function. */
117 ref
= buf
= XMALLOC (MTYPE_TMP
, bufsiz
);
119 /* Fetch interface informations into allocated buffer. */
120 if (sysctl (mib
, MIBSIZ
, buf
, &bufsiz
, NULL
, 0) < 0)
122 zlog (NULL
, LOG_WARNING
, "sysctl error by %s", safe_strerror (errno
));
126 /* Parse both interfaces and addresses. */
127 for (end
= buf
+ bufsiz
; buf
< end
; buf
+= ifm
->ifm_msglen
)
129 ifm
= (struct if_msghdr
*) buf
;
131 switch (ifm
->ifm_type
)
137 ifam_read ((struct ifa_msghdr
*) ifm
);
140 zlog_info ("interfaces_list(): unexpected message type");
141 XFREE (MTYPE_TMP
, ref
);
147 /* Free sysctl buffer. */
148 XFREE (MTYPE_TMP
, ref
);