libnl 3.10.0
nl-route-get.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
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
14static void print_usage(void)
15{
16 printf("Usage: nl-route-get <addr>\n");
17 exit(1);
18}
19
20static 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, &params);
29}
30
31static 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
41int 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
78
79 if (nl_recvmsgs_default(sock) < 0)
80 nl_cli_fatal(err, "%s", nl_geterror(err));
81 }
82
83 return 0;
84}
int nl_addr_get_family(const struct nl_addr *addr)
Return address family.
Definition addr.c:895
unsigned int nl_addr_get_prefixlen(const struct nl_addr *addr)
Return prefix length of abstract address object.
Definition addr.c:978
int nla_put_addr(struct nl_msg *msg, int attrtype, struct nl_addr *addr)
Add abstract address as unspecific attribute to netlink message.
Definition attr.c:552
@ NL_CB_VALID
Message is valid.
Definition handlers.h:89
@ NL_CB_CUSTOM
Customized handler specified by the user.
Definition handlers.h:77
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition utils.c:71
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition msg.c:352
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition msg.c:572
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition msg.c:456
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition object.c:294
int nl_recvmsgs_default(struct nl_sock *sk)
Receive a set of message from a netlink socket using handlers in nl_sock.
Definition nl.c:1093
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition nl.c:1247
int nl_socket_modify_cb(struct nl_sock *sk, enum nl_cb_type type, enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func, void *arg)
Modify the callback handler associated with the socket.
Definition socket.c:795
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
Dumping parameters.
Definition types.h:32
FILE * dp_fd
File descriptor the dumping output should go to.
Definition types.h:81