int main(int argc, char *argv[])
{
	struct nl_handle *nlh;
	struct rtnl_addr *addr;
	struct nl_addr *local;
	int err = 1;

	if (argc < 3 || !strcmp(argv[1], "-h")) {
		printf("Usage: nl-addr-delete <addr> <ifindex>\n");
		goto errout;
	}

	if (nltool_init(argc, argv) < 0)
		goto errout;

	nlh = nltool_alloc_handle();
	if (!nlh)
		goto errout;

	addr = rtnl_addr_alloc();
	if (!addr)
		goto errout_free_handle;

	if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
		goto errout_free_addr;

	local = nltool_addr_parse(argv[1]);
	if (!local)
		goto errout_close;

	if (rtnl_addr_set_local(addr, local) < 0) {
		fprintf(stderr, "Unable to set local address: %s\n",
			nl_geterror());
		goto errout_addr_put;
	}

	rtnl_addr_set_ifindex(addr, strtoul(argv[2], NULL, 0));

	if (rtnl_addr_delete(nlh, addr, 0) < 0) {
		fprintf(stderr, "Unable to delete address: %s\n",
			nl_geterror());
		goto errout_addr_put;
	}

	err = 0;

errout_addr_put:
	nl_addr_put(local);
errout_close:
	nl_close(nlh);
errout_free_addr:
	rtnl_addr_put(addr);
errout_free_handle:
	nl_handle_destroy(nlh);
errout:
	return err;
}
Exemple #2
0
int main(int argc, char *argv[])
{
    struct nl_handle *nlh;
    struct nl_cache *link_cache, *addr_cache;
    struct rtnl_addr *addr;
    struct nl_dump_params params = {
        .dp_fd = stdout,
        .dp_type = NL_DUMP_BRIEF
    };
    int err = 1;

    if (nltool_init(argc, argv) < 0)
        return -1;

    nlh = nltool_alloc_handle();
    if (!nlh)
        return -1;

    addr = rtnl_addr_alloc();
    if (!addr)
        goto errout;

    if (argc < 2 || !strcmp(argv[1], "-h"))
        print_usage();

    if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
        goto errout_free;

    link_cache = nltool_alloc_link_cache(nlh);
    if (!link_cache)
        goto errout_close;

    addr_cache = nltool_alloc_addr_cache(nlh);
    if (!addr_cache)
        goto errout_link_cache;

    params.dp_type = nltool_parse_dumptype(argv[1]);
    if (params.dp_type < 0)
        goto errout_addr_cache;
    get_filter(addr, argc, argv, 2, link_cache);

    nl_cache_dump_filter(addr_cache, &params, (struct nl_object *) addr);

    err = 0;

errout_addr_cache:
    nl_cache_free(addr_cache);
errout_link_cache:
    nl_cache_free(link_cache);
errout_close:
    nl_close(nlh);
errout_free:
    rtnl_addr_put(addr);
errout:
    return err;
}
int main(int argc, char *argv[])
{
	struct nl_handle *nlh;
	struct nl_cache *link_cache;
	int err = 1;

	if (nltool_init(argc, argv) < 0)
		return -1;

	if (argc < 3 || !strcmp(argv[1], "-h"))
		print_usage();

	nlh = nltool_alloc_handle();
	if (!nlh)
		return -1;

	if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
		goto errout;

	link_cache = nltool_alloc_link_cache(nlh);
	if (!link_cache)
		goto errout_close;

	gargv = &argv[2];
	gargc = argc - 2;

	if (!strcasecmp(argv[1], "all"))
		nl_cache_foreach(link_cache, dump_stats, NULL);
	else {
		int ifindex = strtoul(argv[1], NULL, 0);
		struct rtnl_link *link = rtnl_link_get(link_cache, ifindex);

		if (!link) {
			fprintf(stderr, "Could not find ifindex %d\n", ifindex);
			goto errout_link_cache;
		}

		dump_stats((struct nl_object *) link, NULL);
		rtnl_link_put(link);
	}

	err = 0;
errout_link_cache:
	nl_cache_free(link_cache);
errout_close:
	nl_close(nlh);
errout:
	nl_handle_destroy(nlh);
	return err;
}
Exemple #4
0
int main(int argc, char *argv[])
{
	struct nl_cache *link_cache;

	if (nltool_init(argc, argv) < 0)
		return -1;

	dump_params.dp_fd = stdout;

	if (argc > 1) {
		if (!strcasecmp(argv[1], "brief"))
			dump_params.dp_type = NL_DUMP_BRIEF;
		else if (!strcasecmp(argv[1], "full"))
			dump_params.dp_type = NL_DUMP_FULL;
		else if (!strcasecmp(argv[1], "stats"))
			dump_params.dp_type = NL_DUMP_STATS;
	}

	nl_handle = nl_handle_alloc_nondefault(nltool_cbset);
	if (!nl_handle)
		return 1;

	if (nltool_connect(nl_handle, NETLINK_ROUTE) < 0)
		return 1;

	link_cache = nltool_alloc_link_cache(nl_handle);
	if (!link_cache)
		return 1;

	qdisc_cache = nltool_alloc_qdisc_cache(nl_handle);
	if (!qdisc_cache)
		return 1;

	nl_cache_foreach(link_cache, &print_link, NULL);

	nl_cache_free(qdisc_cache);
	nl_cache_free(link_cache);

	nl_close(nl_handle);
	nl_handle_destroy(nl_handle);
	return 0;
}
int main(int argc, char *argv[])
{
	struct nl_handle *nlh;
	struct nl_cache *link_cache;
	int err = -1, ifindex;

	if (nltool_init(argc, argv) < 0)
		return -1;

	if (argc < 2 || !strcmp(argv[1], "-h")) {
		printf("Usage: nl-link-name2ifindex <name>\n");
		return -1;
	}

	nlh = nl_handle_alloc_nondefault(nltool_cbset);
	if (!nlh)
		return -1;

	if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
		goto errout;

	link_cache = nltool_alloc_link_cache(nlh);
	if (!link_cache)
		goto errout;

	if ((ifindex = rtnl_link_name2i(link_cache, argv[1])) == RTNL_LINK_NOT_FOUND)
		fprintf(stderr, "Interface %s does not exist\n", argv[1]);
	else
		printf("%d\n", ifindex);

	nl_cache_free(link_cache);
	err = 0;
errout:
	nl_close(nlh);
	nl_handle_destroy(nlh);

	return err;
}
int main(int argc, char *argv[])
{
	struct nl_sock *nlh;
	struct rtnl_qdisc *qdisc;
	uint32_t handle, parent;
	int err = 1;

	if (nltool_init(argc, argv) < 0)
		return -1;

	if (argc < 5 || !strcmp(argv[1], "-h"))
		print_usage();

	nlh = nltool_alloc_handle();
	if (!nlh)
		goto errout;

	qdisc = rtnl_qdisc_alloc();
	if (!qdisc)
		goto errout_free_handle;

	rtnl_qdisc_set_ifindex(qdisc, strtoul(argv[1], NULL, 0));

	if (rtnl_tc_str2handle(argv[2], &handle) < 0) {
		fprintf(stderr, "%s\n", nl_geterror());
		goto errout_free_qdisc;
	}

	if (rtnl_tc_str2handle(argv[3], &parent) < 0) {
		fprintf(stderr, "%s\n", nl_geterror());
		goto errout_free_qdisc;
	}

	rtnl_qdisc_set_handle(qdisc, handle);
	rtnl_qdisc_set_parent(qdisc, parent);
	rtnl_qdisc_set_kind(qdisc, argv[4]);

	if (!strcasecmp(argv[4], "blackhole"))
		err = parse_blackhole_opts(qdisc, &argv[5], argc-5);
	else if (!strcasecmp(argv[4], "pfifo"))
		err = parse_pfifo_opts(qdisc, &argv[5], argc-5);
	else if (!strcasecmp(argv[4], "bfifo"))
		err = parse_bfifo_opts(qdisc, &argv[5], argc-5);
	else if (!strcasecmp(argv[4], "prio"))
		err = parse_prio_opts(qdisc, &argv[5], argc-5);
	else {
		fprintf(stderr, "Unknown qdisc \"%s\"\n", argv[4]);
		goto errout_free_qdisc;
	}

	if (err < 0)
		goto errout_free_qdisc;

	if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
		goto errout_free_qdisc;

	if (rtnl_qdisc_add(nlh, qdisc, NLM_F_REPLACE) < 0) {
		fprintf(stderr, "Unable to add Qdisc: %s\n", nl_geterror());
		goto errout_close;
	}

	err = 0;
errout_close:
	nl_close(nlh);
errout_free_qdisc:
	rtnl_qdisc_put(qdisc);
errout_free_handle:
	nl_handle_destroy(nlh);
errout:
	return err;
}
int main(int argc, char *argv[])
{
	struct nl_handle *nlh;
	struct nl_cache *link_cache, *route_cache;
	struct nl_addr *dst;
	struct rtnl_route *route;
        struct ip_lookup_res res;
	struct nl_dump_params params = {
		.dp_fd = stdout,
		.dp_type = NL_DUMP_FULL
	};
	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;

	route = rtnl_route_alloc();
	if (!route)
		goto errout_free_handle;

	if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
		goto errout_free_route;

	link_cache = nltool_alloc_link_cache(nlh);
	if (!link_cache)
		goto errout_close;

	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;

	{
		struct nl_msg *m;
		struct rtmsg rmsg = {
			.rtm_family = nl_addr_get_family(dst),
			.rtm_dst_len = nl_addr_get_prefixlen(dst),
		};

		m = nlmsg_alloc_simple(RTM_GETROUTE, 0);
		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;
		}

		nlmsg_free(m);

		nl_socket_modify_cb(nlh, NL_CB_VALID, NL_CB_CUSTOM, cb,
				 route_cache);

		if (nl_recvmsgs_default(nlh) < 0) {
			fprintf(stderr, "%s\n", nl_geterror());
			goto errout_route_cache;
		}
	}

        rtnl_route_set_dst(route, dst);
	nl_cache_dump_filter(route_cache, &params, (struct nl_object *) route);
        memset(&res, 0, sizeof(res));
        nl_cache_foreach_filter(route_cache, (struct nl_object *) route, route_proc_cb,
                                &res);

        printf("ip lookup result: oif idx: %d oif name %s ",
                res.oif, res.oifname);
        if (res.nh_addr) {
                char buf[INET_ADDRSTRLEN];
                inet_ntop(AF_INET, &res.nh_addr, buf, sizeof(buf));
                printf("via %s", buf);
        }
        printf ("\n");

	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_route:
        rtnl_route_put(route);
errout_free_handle:
	nl_handle_destroy(nlh);
errout:
	return err;
}
Exemple #8
0
int main(int argc, char *argv[])
{
	struct nl_handle *nlh;
	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 = nl_handle_alloc_nondefault(nltool_cbset);
	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;

	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;

	{
		struct nl_msg *m;
		struct rtmsg rmsg = {
			.rtm_family = nl_addr_get_family(dst),
			.rtm_dst_len = nl_addr_get_prefixlen(dst),
		};

		m = nlmsg_build_simple(RTM_GETROUTE, 0);
		nlmsg_append(m, &rmsg, sizeof(rmsg), 1);
		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;
		}

		nlmsg_free(m);

		nl_cb_set(nl_handle_get_cb(nlh), NL_CB_VALID, NL_CB_CUSTOM,
			  cb, route_cache);

		if (nl_recvmsgs_def(nlh) < 0) {
			fprintf(stderr, "%s\n", nl_geterror());
			goto errout_route_cache;
		}
	}

	nl_cache_dump(route_cache, &params);

	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;
}