lib: check for integer-overflow in nlmsg_reserve()
[platform/upstream/libnl3.git] / src / nl-classid-lookup.c
1 /*
2  * src/nl-classid-lookup.c     Lookup classid
3  *
4  *      This library is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU Lesser General Public
6  *      License as published by the Free Software Foundation version 2.1
7  *      of the License.
8  *
9  * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #include <netlink/cli/utils.h>
13
14 static void print_usage(void)
15 {
16         printf(
17 "Usage: nl-classid-lookup [OPTIONS]... NAME\n"
18 "\n"
19 "OPTIONS\n"
20 " -h, --help                Show this help text.\n"
21 " -v, --version             Show versioning information.\n"
22 " -r, --reverse             Do a reverse lookup, i.e. classid to name.\n"
23 "     --raw                 Print the raw classid, not pretty printed.\n"
24 "\n"
25 "EXAMPLE\n"
26 "   $ nl-classid-lookup low_latency\n"
27 "   $ nl-classid-lookup -r 1:12\n"
28 "\n"
29         );
30         exit(0);
31 }
32
33 int main(int argc, char *argv[])
34 {
35         uint32_t classid;
36         char *name;
37         int err, reverse = 0, raw = 0;
38
39         for (;;) {
40                 int c, optidx = 0;
41                 enum {
42                         ARG_RAW = 257,
43                 };
44                 static struct option long_opts[] = {
45                         { "help", 0, 0, 'h' },
46                         { "version", 0, 0, 'v' },
47                         { "reverse", 0, 0, 'r' },
48                         { "raw", 0, 0, ARG_RAW },
49                         { 0, 0, 0, 0 }
50                 };
51         
52                 c = getopt_long(argc, argv, "hvr", long_opts, &optidx);
53                 if (c == -1)
54                         break;
55
56                 switch (c) {
57                 case 'h': print_usage(); break;
58                 case 'v': nl_cli_print_version(); break;
59                 case 'r': reverse = 1; break;
60                 case ARG_RAW: raw = 1; break;
61                 }
62         }
63
64         if (optind >= argc)
65                 print_usage();
66
67         name = argv[optind++];
68
69         /*
70          * We use rtnl_tc_str2handle() even while doing a reverse lookup. This
71          * allows for name -> name lookups. This is intentional, it does not
72          * do any harm and avoids duplicating a lot of code.
73          */
74         if ((err = rtnl_tc_str2handle(name, &classid)) < 0)
75                 nl_cli_fatal(err, "Unable to lookup classid \"%s\": %s",
76                              name, nl_geterror(err));
77
78         if (reverse) {
79                 char buf[64];
80                 printf("%s\n", rtnl_tc_handle2str(classid, buf, sizeof(buf)));
81         } else if (raw)
82                 printf("%#x\n", classid);
83         else
84                 printf("%x:%x\n", TC_H_MAJ(classid) >> 16, TC_H_MIN(classid));
85
86         return 0;
87 }