Home | History | Annotate | Download | only in src
      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 (at) 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_cache *link_cache, *route_cache;
     47 	struct nl_addr *dst;
     48 	int err = 1;
     49 
     50 	if (argc < 2 || !strcmp(argv[1], "-h"))
     51 		print_usage();
     52 
     53 	sock = nl_cli_alloc_socket();
     54 	nl_cli_connect(sock, NETLINK_ROUTE);
     55 	link_cache = nl_cli_link_alloc_cache(sock);
     56 	route_cache = nl_cli_route_alloc_cache(sock, 0);
     57 
     58 	dst = nl_cli_addr_parse(argv[1], AF_INET);
     59 
     60 	{
     61 		struct nl_msg *m;
     62 		struct rtmsg rmsg = {
     63 			.rtm_family = nl_addr_get_family(dst),
     64 			.rtm_dst_len = nl_addr_get_prefixlen(dst),
     65 		};
     66 
     67 		m = nlmsg_alloc_simple(RTM_GETROUTE, 0);
     68 		if (!m)
     69 			nl_cli_fatal(ENOMEM, "out of memory");
     70 		if (nlmsg_append(m, &rmsg, sizeof(rmsg), NLMSG_ALIGNTO) < 0)
     71 			nl_cli_fatal(ENOMEM, "out of memory");
     72 		if (nla_put_addr(m, RTA_DST, dst) < 0)
     73 			nl_cli_fatal(ENOMEM, "out of memory");
     74 
     75 		err = nl_send_auto_complete(sock, m);
     76 		nlmsg_free(m);
     77 		if (err < 0)
     78 			nl_cli_fatal(err, "%s", nl_geterror(err));
     79 
     80 		nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL);
     81 
     82 		if (nl_recvmsgs_default(sock) < 0)
     83 			nl_cli_fatal(err, "%s", nl_geterror(err));
     84 	}
     85 
     86 	//nl_cache_dump(route_cache, &params);
     87 
     88 	return 0;
     89 }
     90