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