libnl: /data/src/libnl/src/nl-fib-lookup.c Source File

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