libnl  3.4.0
nl-route-get.c
1 /*
2  * src/nl-route-get.c Get Route Attributes
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 #include <netlink/cli/route.h>
14 #include <netlink/cli/link.h>
15 
16 #include <linux/rtnetlink.h>
17 
18 static void print_usage(void)
19 {
20  printf("Usage: nl-route-get <addr>\n");
21  exit(1);
22 }
23 
24 static void parse_cb(struct nl_object *obj, void *arg)
25 {
26  //struct rtnl_route *route = (struct rtnl_route *) obj;
27  struct nl_dump_params params = {
28  .dp_fd = stdout,
29  .dp_type = NL_DUMP_DETAILS,
30  };
31 
32  nl_object_dump(obj, &params);
33 }
34 
35 static int cb(struct nl_msg *msg, void *arg)
36 {
37  int err;
38 
39  if ((err = nl_msg_parse(msg, &parse_cb, NULL)) < 0)
40  nl_cli_fatal(err, "Unable to parse object: %s", nl_geterror(err));
41 
42  return 0;
43 }
44 
45 int main(int argc, char *argv[])
46 {
47  struct nl_sock *sock;
48  struct nl_addr *dst;
49  int err = 1;
50 
51  if (argc < 2 || !strcmp(argv[1], "-h"))
52  print_usage();
53 
54  sock = nl_cli_alloc_socket();
55  nl_cli_connect(sock, NETLINK_ROUTE);
56  nl_cli_link_alloc_cache(sock);
57  nl_cli_route_alloc_cache(sock, 0);
58 
59  dst = nl_cli_addr_parse(argv[1], AF_INET);
60 
61  {
62  struct nl_msg *m;
63  struct rtmsg rmsg = {
64  .rtm_family = nl_addr_get_family(dst),
65  .rtm_dst_len = nl_addr_get_prefixlen(dst),
66  };
67 
68  m = nlmsg_alloc_simple(RTM_GETROUTE, 0);
69  if (!m)
70  nl_cli_fatal(ENOMEM, "out of memory");
71  if (nlmsg_append(m, &rmsg, sizeof(rmsg), NLMSG_ALIGNTO) < 0)
72  nl_cli_fatal(ENOMEM, "out of memory");
73  if (nla_put_addr(m, RTA_DST, dst) < 0)
74  nl_cli_fatal(ENOMEM, "out of memory");
75 
76  err = nl_send_auto_complete(sock, m);
77  nlmsg_free(m);
78  if (err < 0)
79  nl_cli_fatal(err, "%s", nl_geterror(err));
80 
82 
83  if (nl_recvmsgs_default(sock) < 0)
84  nl_cli_fatal(err, "%s", nl_geterror(err));
85  }
86 
87  return 0;
88 }
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition: nl.c:1247
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition: msg.c:562
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:968
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:549
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:770
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:288
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
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:446
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:77
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition: msg.c:347
Dumping parameters.
Definition: types.h:33
int nl_addr_get_family(const struct nl_addr *addr)
Return address family.
Definition: addr.c:885