File: nl-fib-lookup.c

package info (click to toggle)
libnl3 3.4.0-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,120 kB
  • sloc: ansic: 57,494; sh: 4,447; python: 3,702; makefile: 888; yacc: 478; cpp: 454; lex: 210
file content (111 lines) | stat: -rw-r--r-- 2,540 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * src/nl-fib-lookup.c		FIB Route Lookup
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
 */

#include <netlink/cli/utils.h>

#include <linux/rtnetlink.h>

static void print_usage(void)
{
	printf(
	"Usage: nl-fib-lookup [options] <addr>\n"
	"Options:\n"
	"   -t, --table <table>		Table id\n"
	"   -f, --fwmark <int>		Firewall mark\n"
	"   -s, --scope <scope>		Routing scope\n"
	"   -T, --tos <int>		Type of Service\n");
	exit(1);
}

int main(int argc, char *argv[])
{
	struct nl_sock *nlh;
	struct nl_cache *result;
	struct flnl_request *request;
	struct nl_addr *addr;
	struct nl_dump_params params = {
		.dp_fd = stdout,
		.dp_type = NL_DUMP_DETAILS,
	};
	int table = RT_TABLE_UNSPEC, scope = RT_SCOPE_UNIVERSE;
	int tos = 0, err = 1;
	uint64_t fwmark = 0;

	while (1) {
		static struct option long_opts[] = {
			{"table", 1, 0, 't'},
			{"fwmark", 1, 0, 'f'},
			{"scope", 1, 0, 's'},
			{"tos", 1, 0, 'T'},
			{"help", 0, 0, 'h'},
			{0, 0, 0, 0},
		};
		int c, idx = 0;

		c = getopt_long(argc, argv, "t:f:s:T:h", long_opts, &idx);
		if (c == -1)
			break;

		switch (c) {
		case 't':
			table = strtoul(optarg, NULL, 0);
			break;
		case 'f':
			fwmark = strtoul(optarg, NULL, 0);
			break;
		case 's':
			scope = strtoul(optarg, NULL, 0);
			break;
		case 'T':
			tos = strtoul(optarg, NULL, 0);
			break;
		default:
			print_usage();
		}
	}

	if (optind >= argc)
		print_usage();

	nlh = nl_cli_alloc_socket();

	if ((err = nl_addr_parse(argv[optind], AF_INET, &addr)) < 0)
		nl_cli_fatal(err, "Unable to parse address \"%s\": %s\n",
			argv[optind], nl_geterror(err));

	result = flnl_result_alloc_cache();
	if (!result)
		nl_cli_fatal(ENOMEM, "Unable to allocate cache");

	request = flnl_request_alloc();
	if (!request)
		nl_cli_fatal(ENOMEM, "Unable to allocate request");

	flnl_request_set_table(request, table);
	flnl_request_set_fwmark(request, fwmark);
	flnl_request_set_scope(request, scope);
	flnl_request_set_tos(request, tos);

	err = flnl_request_set_addr(request, addr);
	nl_addr_put(addr);
	if (err < 0)
		nl_cli_fatal(err, "Unable to send request: %s", nl_geterror(err));

	nl_cli_connect(nlh, NETLINK_FIB_LOOKUP);

	err = flnl_lookup(nlh, request, result);
	if (err < 0)
		nl_cli_fatal(err, "Unable to lookup: %s\n", nl_geterror(err));

	nl_cache_dump(result, &params);

	return 0;
}