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