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