libnl 3.7.0
nl-fib-lookup.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
4 */
5
6#include <netlink/cli/utils.h>
7
8#include <linux/rtnetlink.h>
9
10static void print_usage(void)
11{
12 printf(
13 "Usage: nl-fib-lookup [options] <addr>\n"
14 "Options:\n"
15 " -t, --table <table> Table id\n"
16 " -f, --fwmark <int> Firewall mark\n"
17 " -s, --scope <scope> Routing scope\n"
18 " -T, --tos <int> Type of Service\n");
19 exit(1);
20}
21
22int main(int argc, char *argv[])
23{
24 struct nl_sock *nlh;
25 struct nl_cache *result;
26 struct flnl_request *request;
27 struct nl_addr *addr;
28 struct nl_dump_params params = {
29 .dp_fd = stdout,
30 .dp_type = NL_DUMP_DETAILS,
31 };
32 int table = RT_TABLE_UNSPEC, scope = RT_SCOPE_UNIVERSE;
33 int tos = 0, err = 1;
34 uint64_t fwmark = 0;
35
36 while (1) {
37 static struct option long_opts[] = {
38 {"table", 1, 0, 't'},
39 {"fwmark", 1, 0, 'f'},
40 {"scope", 1, 0, 's'},
41 {"tos", 1, 0, 'T'},
42 {"help", 0, 0, 'h'},
43 {0, 0, 0, 0},
44 };
45 int c, idx = 0;
46
47 c = getopt_long(argc, argv, "t:f:s:T:h", long_opts, &idx);
48 if (c == -1)
49 break;
50
51 switch (c) {
52 case 't':
53 table = strtoul(optarg, NULL, 0);
54 break;
55 case 'f':
56 fwmark = strtoul(optarg, NULL, 0);
57 break;
58 case 's':
59 scope = strtoul(optarg, NULL, 0);
60 break;
61 case 'T':
62 tos = strtoul(optarg, NULL, 0);
63 break;
64 default:
65 print_usage();
66 }
67 }
68
69 if (optind >= argc)
70 print_usage();
71
72 nlh = nl_cli_alloc_socket();
73
74 if ((err = nl_addr_parse(argv[optind], AF_INET, &addr)) < 0)
75 nl_cli_fatal(err, "Unable to parse address \"%s\": %s\n",
76 argv[optind], nl_geterror(err));
77
78 result = flnl_result_alloc_cache();
79 if (!result)
80 nl_cli_fatal(ENOMEM, "Unable to allocate cache");
81
82 request = flnl_request_alloc();
83 if (!request)
84 nl_cli_fatal(ENOMEM, "Unable to allocate request");
85
86 flnl_request_set_table(request, table);
87 flnl_request_set_fwmark(request, fwmark);
88 flnl_request_set_scope(request, scope);
89 flnl_request_set_tos(request, tos);
90
91 err = flnl_request_set_addr(request, addr);
92 nl_addr_put(addr);
93 if (err < 0)
94 nl_cli_fatal(err, "Unable to send request: %s", nl_geterror(err));
95
96 nl_cli_connect(nlh, NETLINK_FIB_LOOKUP);
97
98 err = flnl_lookup(nlh, request, result);
99 if (err < 0)
100 nl_cli_fatal(err, "Unable to lookup: %s\n", nl_geterror(err));
101
102 nl_cache_dump(result, &params);
103
104 return 0;
105}
int nl_addr_parse(const char *addrstr, int hint, struct nl_addr **result)
Allocate abstract address based on character string.
Definition: addr.c:292
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
Definition: addr.c:538
void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
Dump all elements of a cache.
Definition: cache.c:1197
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
struct nl_cache * flnl_result_alloc_cache(void)
Allocate lookup result cache.
Definition: lookup.c:177
int flnl_lookup(struct nl_sock *sk, struct flnl_request *req, struct nl_cache *cache)
Perform FIB Lookup.
Definition: lookup.c:256
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:17
Dumping parameters.
Definition: types.h:28
FILE * dp_fd
File descriptor the dumping output should go to.
Definition: types.h:77