This commit was generated by cvs2svn to compensate for changes in r786,
[libnl.git] / src / nl-fib-lookup.c
1 /*
2  * src/nl-fib-lookup.c          FIB Route Lookup
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) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #include "utils.h"
13
14 static void print_usage(void)
15 {
16         printf(
17         "Usage: nl-fib-lookup [options] <addr>\n"
18         "Options:\n"
19         "   -t, --table <table>         Table id\n"
20         "   -f, --fwmark <int>          Firewall mark\n"
21         "   -s, --scope <scope>         Routing scope\n"
22         "   -T, --tos <int>             Type of Service\n");
23         exit(1);
24 }
25
26 int main(int argc, char *argv[])
27 {
28         struct nl_handle *nlh;
29         struct nl_cache *result;
30         struct flnl_request *request;
31         struct nl_addr *addr;
32         struct nl_dump_params params = {
33                 .dp_fd = stdout,
34                 .dp_type = NL_DUMP_FULL,
35         };
36         int table = RT_TABLE_UNSPEC, scope = RT_SCOPE_UNIVERSE;
37         int tos = 0, err = 1;
38         uint64_t fwmark = 0;
39
40         if (nltool_init(argc, argv) < 0)
41                 return -1;
42
43         while (1) {
44                 static struct option long_opts[] = {
45                         {"table", 1, 0, 't'},
46                         {"fwmark", 1, 0, 'f'},
47                         {"scope", 1, 0, 's'},
48                         {"tos", 1, 0, 'T'},
49                         {"help", 0, 0, 'h'},
50                         {0, 0, 0, 0},
51                 };
52                 int c, idx = 0;
53
54                 c = getopt_long(argc, argv, "t:f:s:T:h", long_opts, &idx);
55                 if (c == -1)
56                         break;
57
58                 switch (c) {
59                 case 't':
60                         table = strtoul(optarg, NULL, 0);
61                         break;
62                 case 'f':
63                         fwmark = strtoul(optarg, NULL, 0);
64                         break;
65                 case 's':
66                         scope = strtoul(optarg, NULL, 0);
67                         break;
68                 case 'T':
69                         tos = strtoul(optarg, NULL, 0);
70                         break;
71                 default:
72                         print_usage();
73                 }
74         }
75
76         if (optind >= argc)
77                 print_usage();
78
79         nlh = nl_handle_alloc_nondefault(nltool_cbset);
80         if (!nlh)
81                 return -1;
82
83         addr = nl_addr_parse(argv[optind], AF_INET);
84         if (!addr) {
85                 fprintf(stderr, "Unable to parse address \"%s\": %s\n",
86                         argv[optind], nl_geterror());
87                 goto errout;
88         }
89
90         result = flnl_result_alloc_cache();
91         if (!result)
92                 goto errout_addr;
93
94         request = flnl_request_alloc();
95         if (!request)
96                 goto errout_result;
97
98         flnl_request_set_table(request, table);
99         flnl_request_set_fwmark(request, fwmark);
100         flnl_request_set_scope(request, scope);
101         flnl_request_set_tos(request, tos);
102
103         err = flnl_request_set_addr(request, addr);
104         nl_addr_put(addr);
105         if (err < 0)
106                 goto errout_put;
107
108         if (nltool_connect(nlh, NETLINK_FIB_LOOKUP) < 0)
109                 goto errout_put;
110
111         err = flnl_lookup(nlh, request, result);
112         if (err < 0) {
113                 fprintf(stderr, "Unable to lookup: %s\n", nl_geterror());
114                 goto errout_put;
115         }
116
117         nl_cache_dump(result, &params);
118
119         err = 0;
120 errout_put:
121         flnl_request_put(request);
122 errout_result:
123         nl_cache_free(result);
124 errout_addr:
125         nl_addr_put(addr);
126 errout:
127         nl_close(nlh);
128         nl_handle_destroy(nlh);
129         return err;
130 }