2 * src/nl-route-get.c Get Route Attributes
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
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
14 static void print_usage(void)
16 printf("Usage: nl-route-get <addr>\n");
20 static int cb(struct nl_msg *msg, void *arg)
22 nl_cache_parse_and_add(arg, msg);
27 int main(int argc, char *argv[])
29 struct nl_handle *nlh;
30 struct nl_cache *link_cache, *route_cache;
32 struct nl_dump_params params = {
34 .dp_type = NL_DUMP_BRIEF
38 if (argc < 2 || !strcmp(argv[1], "-h"))
41 if (nltool_init(argc, argv) < 0)
44 nlh = nltool_alloc_handle();
48 if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
49 goto errout_free_handle;
51 link_cache = nltool_alloc_link_cache(nlh);
55 dst = nltool_addr_parse(argv[1]);
57 goto errout_link_cache;
59 route_cache = nltool_alloc_route_cache(nlh);
66 .rtm_family = nl_addr_get_family(dst),
67 .rtm_dst_len = nl_addr_get_prefixlen(dst),
70 m = nlmsg_alloc_simple(RTM_GETROUTE, 0);
71 nlmsg_append(m, &rmsg, sizeof(rmsg), NLMSG_ALIGNTO);
72 nla_put_addr(m, RTA_DST, dst);
74 if ((err = nl_send_auto_complete(nlh, m)) < 0) {
76 fprintf(stderr, "%s\n", nl_geterror());
77 goto errout_route_cache;
82 nl_socket_modify_cb(nlh, NL_CB_VALID, NL_CB_CUSTOM, cb,
85 if (nl_recvmsgs_default(nlh) < 0) {
86 fprintf(stderr, "%s\n", nl_geterror());
87 goto errout_route_cache;
91 nl_cache_dump(route_cache, ¶ms);
95 nl_cache_free(route_cache);
99 nl_cache_free(link_cache);
103 nl_handle_destroy(nlh);