From 01db81597502c6016888411983db33b7718ca810 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Sun, 12 Aug 2012 20:56:25 +0200 Subject: utils: iftest: pass the device name you want to obtain information for Instead of brute-force information extraction based on the device index. ./iftest eth0 index (2) is eth0 (NOT RUNNING) (UP) Signed-off-by: Pablo Neira Ayuso --- utils/iftest.c | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/utils/iftest.c b/utils/iftest.c index 40825a3..63cf163 100644 --- a/utils/iftest.c +++ b/utils/iftest.c @@ -7,10 +7,17 @@ #include -int main(void) +int main(int argc, char *argv[]) { - int i; + int idx; struct nlif_handle *h; + char name[IFNAMSIZ]; + unsigned int flags; + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } h = nlif_open(); if (h == NULL) { @@ -20,19 +27,23 @@ int main(void) nlif_query(h); - for (i=0; i<64; i++) { - char name[IFNAMSIZ]; - unsigned int flags; - - if (nlif_index2name(h, i, name) == -1) - continue; - if (nlif_get_ifflags(h, i, &flags) == -1) - continue; - printf("index (%d) is %s (%s) (%s)\n", i, name, - flags & IFF_RUNNING ? "RUNNING" : "NOT RUNNING", - flags & IFF_UP ? "UP" : "DOWN"); + idx = if_nametoindex(argv[1]); + + /* Superfluous: just to make sure nlif_index2name is working fine */ + if (nlif_index2name(h, idx, name) == -1) { + fprintf(stderr, "Cannot translate device idx=%u\n", idx); + exit(EXIT_FAILURE); } + if (nlif_get_ifflags(h, idx, &flags) == -1) { + fprintf(stderr, "Cannot get flags for device `%s'\n", argv[1]); + exit(EXIT_FAILURE); + } + + printf("index (%d) is %s (%s) (%s)\n", idx, argv[1], + flags & IFF_RUNNING ? "RUNNING" : "NOT RUNNING", + flags & IFF_UP ? "UP" : "DOWN"); + nlif_close(h); return EXIT_SUCCESS; } -- cgit v1.2.3