diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/.gitignore | 1 | ||||
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/nl-classid-lookup.c | 79 |
3 files changed, 85 insertions, 1 deletions
diff --git a/src/.gitignore b/src/.gitignore index 60233c8..12859cb 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -29,3 +29,4 @@ nl-rule-list nl-tctree-list nl-util-addr nf-queue +nl-classid-lookup diff --git a/src/Makefile.am b/src/Makefile.am index 5144bb4..4a7f98f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,7 +6,8 @@ AM_CPPFLAGS = -Wall -I${top_srcdir}/include -I${top_builddir}/include -D_GNU_SO AM_LDFLAGS = -L${top_builddir}/lib -L${top_builddir}/src/lib -lnl-cli sbin_PROGRAMS = \ - nl-qdisc-add + nl-qdisc-add \ + nl-classid-lookup noinst_PROGRAMS = \ genl-ctrl-list \ @@ -104,3 +105,6 @@ nl_util_addr_LDADD = -lnl-route nl_pktloc_lookup_SOURCES = nl-pktloc-lookup.c nl_pktloc_lookup_LDADD = -lnl-route + +nl_classid_lookup_SOURCES = nl-classid-lookup.c +nl_classid_lookup_LDADD = -lnl-route diff --git a/src/nl-classid-lookup.c b/src/nl-classid-lookup.c new file mode 100644 index 0000000..faa65dd --- /dev/null +++ b/src/nl-classid-lookup.c @@ -0,0 +1,79 @@ +/* + * src/nl-classid-lookup.c Lookup classid + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch> + */ + +#include <netlink/cli/utils.h> + +static void print_usage(void) +{ + printf( +"Usage: nl-classid-lookup [OPTIONS]... NAME\n" +"\n" +"OPTIONS\n" +" -h, --help Show this help text.\n" +" -v, --version Show versioning information.\n" +" -r, --reverse Do a reverse lookup, i.e. classid to name.\n" +"\n" +"EXAMPLE\n" +" $ nl-classid-lookup low_latency\n" +" $ nl-classid-lookup -r 1:12\n" +"\n" + ); + exit(0); +} + +int main(int argc, char *argv[]) +{ + uint32_t classid; + char *name; + int err, reverse = 0; + + for (;;) { + int c, optidx = 0; + static struct option long_opts[] = { + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'v' }, + { "reverse", 0, 0, 'r' }, + { 0, 0, 0, 0 } + }; + + c = getopt_long(argc, argv, "hvr", long_opts, &optidx); + if (c == -1) + break; + + switch (c) { + case 'h': print_usage(); break; + case 'v': nl_cli_print_version(); break; + case 'r': reverse = 1; break; + } + } + + if (optind >= argc) + print_usage(); + + name = argv[optind++]; + + /* + * We use rtnl_tc_str2handle() even while doing a reverse lookup. This + * allows for name -> name lookups. This is intentional, it does not + * do any harm and avoids duplicating a lot of code. + */ + if ((err = rtnl_tc_str2handle(name, &classid)) < 0) + nl_cli_fatal(err, "Unable to lookup classid \"%s\": %s", + name, nl_geterror(err)); + + if (reverse) { + char buf[64]; + printf("%s\n", rtnl_tc_handle2str(classid, buf, sizeof(buf))); + } else + printf("%x:%x\n", TC_H_MAJ(classid) >> 16, TC_H_MIN(classid)); + + return 0; +} |