libnl  3.5.0
nl-route-get.c
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 
19 static void print_usage(void)
20 {
21  printf("Usage: nl-route-get <addr>\n");
22  exit(1);
23 }
24 
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, &params);
34 }
35 
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 
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 
83 
84  if (nl_recvmsgs_default(sock) < 0)
85  nl_cli_fatal(err, "%s", nl_geterror(err));
86  }
87 
88  return 0;
89 }
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition: nl.c:1248
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition: msg.c:565
FILE * dp_fd
File descriptor the dumping output should go to.
Definition: types.h:83
unsigned int nl_addr_get_prefixlen(const struct nl_addr *addr)
Return prefix length of abstract address object.
Definition: addr.c:977
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:550
Customized handler specified by the user.
Definition: handlers.h:83
Dump all attributes but no statistics.
Definition: types.h:23
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:771
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:289
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:1094
Message is valid.
Definition: handlers.h:95
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:449
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:78
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition: msg.c:348
Dumping parameters.
Definition: types.h:33
int nl_addr_get_family(const struct nl_addr *addr)
Return address family.
Definition: addr.c:894