• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/nl-fib-lookup.c		FIB Route Lookup
4  *
5  *	This library is free software; you can redistribute it and/or
6  *	modify it under the terms of the GNU Lesser General Public
7  *	License as published by the Free Software Foundation version 2.1
8  *	of the License.
9  *
10  * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
11  */
12 
13 #include <netlink/cli/utils.h>
14 
15 #include <linux/rtnetlink.h>
16 
print_usage(void)17 static void print_usage(void)
18 {
19 	printf(
20 	"Usage: nl-fib-lookup [options] <addr>\n"
21 	"Options:\n"
22 	"   -t, --table <table>		Table id\n"
23 	"   -f, --fwmark <int>		Firewall mark\n"
24 	"   -s, --scope <scope>		Routing scope\n"
25 	"   -T, --tos <int>		Type of Service\n");
26 	exit(1);
27 }
28 
main(int argc,char * argv[])29 int main(int argc, char *argv[])
30 {
31 	struct nl_sock *nlh;
32 	struct nl_cache *result;
33 	struct flnl_request *request;
34 	struct nl_addr *addr;
35 	struct nl_dump_params params = {
36 		.dp_fd = stdout,
37 		.dp_type = NL_DUMP_DETAILS,
38 	};
39 	int table = RT_TABLE_UNSPEC, scope = RT_SCOPE_UNIVERSE;
40 	int tos = 0, err = 1;
41 	uint64_t fwmark = 0;
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_cli_alloc_socket();
80 
81 	if ((err = nl_addr_parse(argv[optind], AF_INET, &addr)) < 0)
82 		nl_cli_fatal(err, "Unable to parse address \"%s\": %s\n",
83 			argv[optind], nl_geterror(err));
84 
85 	result = flnl_result_alloc_cache();
86 	if (!result)
87 		nl_cli_fatal(ENOMEM, "Unable to allocate cache");
88 
89 	request = flnl_request_alloc();
90 	if (!request)
91 		nl_cli_fatal(ENOMEM, "Unable to allocate request");
92 
93 	flnl_request_set_table(request, table);
94 	flnl_request_set_fwmark(request, fwmark);
95 	flnl_request_set_scope(request, scope);
96 	flnl_request_set_tos(request, tos);
97 
98 	err = flnl_request_set_addr(request, addr);
99 	nl_addr_put(addr);
100 	if (err < 0)
101 		nl_cli_fatal(err, "Unable to send request: %s", nl_geterror(err));
102 
103 	nl_cli_connect(nlh, NETLINK_FIB_LOOKUP);
104 
105 	err = flnl_lookup(nlh, request, result);
106 	if (err < 0)
107 		nl_cli_fatal(err, "Unable to lookup: %s\n", nl_geterror(err));
108 
109 	nl_cache_dump(result, &params);
110 
111 	return 0;
112 }
113