diff options
Diffstat (limited to 'src/nl-route-get.c')
-rw-r--r-- | src/nl-route-get.c | 89 |
1 files changed, 34 insertions, 55 deletions
diff --git a/src/nl-route-get.c b/src/nl-route-get.c index fd7a503..c2f07d4 100644 --- a/src/nl-route-get.c +++ b/src/nl-route-get.c @@ -6,10 +6,12 @@ * License as published by the Free Software Foundation version 2.1 * of the License. * - * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch> + * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch> */ -#include "utils.h" +#include <netlink/cli/utils.h> +#include <netlink/cli/route.h> +#include <netlink/cli/link.h> static void print_usage(void) { @@ -17,48 +19,43 @@ static void print_usage(void) exit(1); } +static void parse_cb(struct nl_object *obj, void *arg) +{ + //struct rtnl_route *route = (struct rtnl_route *) obj; + struct nl_dump_params params = { + .dp_fd = stdout, + .dp_type = NL_DUMP_DETAILS, + }; + + nl_object_dump(obj, ¶ms); +} + static int cb(struct nl_msg *msg, void *arg) { - nl_cache_parse_and_add(arg, msg); + int err; + + if ((err = nl_msg_parse(msg, &parse_cb, NULL)) < 0) + nl_cli_fatal(err, "Unable to parse object: %s", nl_geterror(err)); return 0; } int main(int argc, char *argv[]) { - struct nl_handle *nlh; + struct nl_sock *sock; struct nl_cache *link_cache, *route_cache; struct nl_addr *dst; - struct nl_dump_params params = { - .dp_fd = stdout, - .dp_type = NL_DUMP_BRIEF - }; int err = 1; if (argc < 2 || !strcmp(argv[1], "-h")) print_usage(); - if (nltool_init(argc, argv) < 0) - goto errout; - - nlh = nltool_alloc_handle(); - if (!nlh) - goto errout; - - if (nltool_connect(nlh, NETLINK_ROUTE) < 0) - goto errout_free_handle; - - link_cache = nltool_alloc_link_cache(nlh); - if (!link_cache) - goto errout_close; + sock = nl_cli_alloc_socket(); + nl_cli_connect(sock, NETLINK_ROUTE); + link_cache = nl_cli_link_alloc_cache(sock); + route_cache = nl_cli_route_alloc_cache(sock, 0); - dst = nltool_addr_parse(argv[1]); - if (!dst) - goto errout_link_cache; - - route_cache = nltool_alloc_route_cache(nlh); - if (!route_cache) - goto errout_addr_put; + dst = nl_cli_addr_parse(argv[1], AF_INET); { struct nl_msg *m; @@ -71,36 +68,18 @@ int main(int argc, char *argv[]) nlmsg_append(m, &rmsg, sizeof(rmsg), NLMSG_ALIGNTO); nla_put_addr(m, RTA_DST, dst); - if ((err = nl_send_auto_complete(nlh, m)) < 0) { - nlmsg_free(m); - fprintf(stderr, "%s\n", nl_geterror()); - goto errout_route_cache; - } - + err = nl_send_auto_complete(sock, m); nlmsg_free(m); + if (err < 0) + nl_cli_fatal(err, "%s", nl_geterror(err)); - nl_socket_modify_cb(nlh, NL_CB_VALID, NL_CB_CUSTOM, cb, - route_cache); + nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL); - if (nl_recvmsgs_default(nlh) < 0) { - fprintf(stderr, "%s\n", nl_geterror()); - goto errout_route_cache; - } + if (nl_recvmsgs_default(sock) < 0) + nl_cli_fatal(err, "%s", nl_geterror(err)); } - nl_cache_dump(route_cache, ¶ms); - - err = 0; -errout_route_cache: - nl_cache_free(route_cache); -errout_addr_put: - nl_addr_put(dst); -errout_link_cache: - nl_cache_free(link_cache); -errout_close: - nl_close(nlh); -errout_free_handle: - nl_handle_destroy(nlh); -errout: - return err; + //nl_cache_dump(route_cache, ¶ms); + + return 0; } |