• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-2009 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 
print_usage(void)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 
main(int argc,char * argv[])26 int main(int argc, char *argv[])
27 {
28 	struct nl_sock *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_DETAILS,
35 	};
36 	int table = RT_TABLE_UNSPEC, scope = RT_SCOPE_UNIVERSE;
37 	int tos = 0, err = 1;
38 	uint64_t fwmark = 0;
39 
40 	while (1) {
41 		static struct option long_opts[] = {
42 			{"table", 1, 0, 't'},
43 			{"fwmark", 1, 0, 'f'},
44 			{"scope", 1, 0, 's'},
45 			{"tos", 1, 0, 'T'},
46 			{"help", 0, 0, 'h'},
47 			{0, 0, 0, 0},
48 		};
49 		int c, idx = 0;
50 
51 		c = getopt_long(argc, argv, "t:f:s:T:h", long_opts, &idx);
52 		if (c == -1)
53 			break;
54 
55 		switch (c) {
56 		case 't':
57 			table = strtoul(optarg, NULL, 0);
58 			break;
59 		case 'f':
60 			fwmark = strtoul(optarg, NULL, 0);
61 			break;
62 		case 's':
63 			scope = strtoul(optarg, NULL, 0);
64 			break;
65 		case 'T':
66 			tos = strtoul(optarg, NULL, 0);
67 			break;
68 		default:
69 			print_usage();
70 		}
71 	}
72 
73 	if (optind >= argc)
74 		print_usage();
75 
76 	nlh = nl_cli_alloc_socket();
77 
78 	if ((err = nl_addr_parse(argv[optind], AF_INET, &addr)) < 0)
79 		nl_cli_fatal(err, "Unable to parse address \"%s\": %s\n",
80 			argv[optind], nl_geterror(err));
81 
82 	result = flnl_result_alloc_cache();
83 	if (!result)
84 		nl_cli_fatal(ENOMEM, "Unable to allocate cache");
85 
86 	request = flnl_request_alloc();
87 	if (!request)
88 		nl_cli_fatal(ENOMEM, "Unable to allocate request");
89 
90 	flnl_request_set_table(request, table);
91 	flnl_request_set_fwmark(request, fwmark);
92 	flnl_request_set_scope(request, scope);
93 	flnl_request_set_tos(request, tos);
94 
95 	err = flnl_request_set_addr(request, addr);
96 	nl_addr_put(addr);
97 	if (err < 0)
98 		nl_cli_fatal(err, "Unable to send request: %s", nl_geterror(err));
99 
100 	nl_cli_connect(nlh, NETLINK_FIB_LOOKUP);
101 
102 	err = flnl_lookup(nlh, request, result);
103 	if (err < 0)
104 		nl_cli_fatal(err, "Unable to lookup: %s\n", nl_geterror(err));
105 
106 	nl_cache_dump(result, &params);
107 
108 	return 0;
109 }
110