From 6833d97a2314ddce5a7a65402c0afba37e8913c4 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Wed, 7 Jan 2009 01:17:39 +0100 Subject: iftable: add nlif_get_ifflags to get the network interface flags This patch adds the nlif_get_ifflags to get the interface flags. This patch also modifies the example file to display if a network interface is running or not. Signed-off-by: Pablo Neira Ayuso --- include/libnfnetlink/libnfnetlink.h | 3 +++ src/iftable.c | 33 +++++++++++++++++++++++++++++++++ utils/iftest.c | 7 ++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/include/libnfnetlink/libnfnetlink.h b/include/libnfnetlink/libnfnetlink.h index 83874e3..b2f3652 100644 --- a/include/libnfnetlink/libnfnetlink.h +++ b/include/libnfnetlink/libnfnetlink.h @@ -205,6 +205,9 @@ int nlif_catch(struct nlif_handle *nlif_handle); int nlif_index2name(struct nlif_handle *nlif_handle, unsigned int if_index, char *name); +int nlif_get_ifflags(const struct nlif_handle *h, + unsigned int index, + unsigned int *flags); #ifdef __cplusplus } /* extern "C" */ diff --git a/src/iftable.c b/src/iftable.c index 3a4b0cb..f316217 100644 --- a/src/iftable.c +++ b/src/iftable.c @@ -173,6 +173,39 @@ int nlif_index2name(struct nlif_handle *h, return -1; } +/** Get the flags for an ifindex + * + * \param nlif_handle A pointer to a ::nlif_handle created + * \param index ifindex to be resolved + * \param flags pointer to variable used to store the interface flags + * \return -1 on error, 1 on success + */ +int nlif_get_ifflags(const struct nlif_handle *h, + unsigned int index, + unsigned int *flags) +{ + unsigned int hash; + struct ifindex_node *this; + + assert(h != NULL); + assert(flags != NULL); + + if (index == 0) { + errno = ENOENT; + return -1; + } + + hash = index & 0xF; + list_for_each_entry(this, &h->ifindex_hash[hash], head) { + if (this->index == index) { + *flags = this->flags; + return 1; + } + } + errno = ENOENT; + return -1; +} + /** Initialize interface table * * Initialize rtnl interface and interface table diff --git a/utils/iftest.c b/utils/iftest.c index 61d066a..2bbe3a3 100644 --- a/utils/iftest.c +++ b/utils/iftest.c @@ -3,6 +3,7 @@ #include #include #include +#include #include @@ -21,10 +22,14 @@ int main() for (i=0; i<64; i++) { char name[IFNAMSIZ]; + unsigned int flags; if (nlif_index2name(h, i, name) == -1) continue; - printf("index (%d) is %s\n", i, name); + if (nlif_get_ifflags(h, i, &flags) == -1) + continue; + printf("index (%d) is %s (%s)\n", i, name, + flags & IFF_RUNNING ? "RUNNING" : "NOT RUNNING"); } nlif_close(h); -- cgit v1.2.3