| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2003-2009 Thomas Graf <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "nl-default.h" |
| 7 |
|
| 8 |
#include <linux/rtnetlink.h> |
| 9 |
|
| 10 |
#include <netlink/cli/utils.h> |
| 11 |
#include <netlink/cli/route.h> |
| 12 |
#include <netlink/cli/link.h> |
| 13 |
|
| 14 |
static void print_usage(void) |
| 15 |
{ |
| 16 |
printf("Usage: nl-route-get <addr>\n"); |
| 17 |
exit(1); |
| 18 |
} |
| 19 |
|
| 20 |
static void parse_cb(struct nl_object *obj, void *arg) |
| 21 |
{ |
| 22 |
//struct rtnl_route *route = (struct rtnl_route *) obj; |
| 23 |
struct nl_dump_params params = { |
| 24 |
.dp_fd = stdout, |
| 25 |
.dp_type = NL_DUMP_DETAILS, |
| 26 |
}; |
| 27 |
|
| 28 |
nl_object_dump(obj, ¶ms); |
| 29 |
} |
| 30 |
|
| 31 |
static int cb(struct nl_msg *msg, void *arg) |
| 32 |
{ |
| 33 |
int err; |
| 34 |
|
| 35 |
if ((err = nl_msg_parse(msg, &parse_cb, NULL)) < 0) |
| 36 |
nl_cli_fatal(err, "Unable to parse object: %s", nl_geterror(err)); |
| 37 |
|
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
int main(int argc, char *argv[]) |
| 42 |
{ |
| 43 |
struct nl_sock *sock; |
| 44 |
struct nl_addr *dst; |
| 45 |
int err = 1; |
| 46 |
|
| 47 |
if (argc < 2 || !strcmp(argv[1], "-h")) |
| 48 |
print_usage(); |
| 49 |
|
| 50 |
sock = nl_cli_alloc_socket(); |
| 51 |
nl_cli_connect(sock, NETLINK_ROUTE); |
| 52 |
nl_cli_link_alloc_cache(sock); |
| 53 |
nl_cli_route_alloc_cache(sock, 0); |
| 54 |
|
| 55 |
dst = nl_cli_addr_parse(argv[1], AF_INET); |
| 56 |
|
| 57 |
{ |
| 58 |
struct nl_msg *m; |
| 59 |
struct rtmsg rmsg = { |
| 60 |
.rtm_family = nl_addr_get_family(dst), |
| 61 |
.rtm_dst_len = nl_addr_get_prefixlen(dst), |
| 62 |
}; |
| 63 |
|
| 64 |
m = nlmsg_alloc_simple(RTM_GETROUTE, 0); |
| 65 |
if (!m) |
| 66 |
nl_cli_fatal(ENOMEM, "out of memory"); |
| 67 |
if (nlmsg_append(m, &rmsg, sizeof(rmsg), NLMSG_ALIGNTO) < 0) |
| 68 |
nl_cli_fatal(ENOMEM, "out of memory"); |
| 69 |
if (nla_put_addr(m, RTA_DST, dst) < 0) |
| 70 |
nl_cli_fatal(ENOMEM, "out of memory"); |
| 71 |
|
| 72 |
err = nl_send_auto_complete(sock, m); |
| 73 |
nlmsg_free(m); |
| 74 |
if (err < 0) |
| 75 |
nl_cli_fatal(err, "%s", nl_geterror(err)); |
| 76 |
|
| 77 |
nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL); |
| 78 |
|
| 79 |
if (nl_recvmsgs_default(sock) < 0) |
| 80 |
nl_cli_fatal(err, "%s", nl_geterror(err)); |
| 81 |
} |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |