Home | History | Annotate | Download | only in src
      1 /*
      2  * src/nl-neigh-list.c      List Neighbours
      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/neigh.h>
     14 #include <netlink/cli/link.h>
     15 
     16 static void print_usage(void)
     17 {
     18 	printf(
     19 	"Usage: nl-neigh-list [OPTION]... [NEIGHBOUR]\n"
     20 	"\n"
     21 	"Options\n"
     22 	" -f, --format=TYPE     Output format { brief | details | stats }\n"
     23 	" -h, --help            Show this help\n"
     24 	" -v, --version         Show versioning information\n"
     25 	"\n"
     26 	"Neighbour Options\n"
     27 	" -a, --addr=ADDR       Destination address of neighbour\n"
     28 	" -l, --lladdr=ADDR     Link layer address of neighbour\n"
     29 	" -d, --dev=DEV         Device the neighbour is connected to\n"
     30 	"     --family=FAMILY   Destination address family\n"
     31 	"     --state=STATE     Neighbour state, (default = permanent)\n"
     32 	);
     33 	exit(0);
     34 }
     35 
     36 int main(int argc, char *argv[])
     37 {
     38 	struct nl_sock *sock;
     39 	struct rtnl_neigh *neigh;
     40 	struct nl_cache *link_cache, *neigh_cache;
     41 	struct nl_dump_params params = {
     42 		.dp_type = NL_DUMP_LINE,
     43 		.dp_fd = stdout,
     44 	};
     45 
     46 	sock = nl_cli_alloc_socket();
     47 	nl_cli_connect(sock, NETLINK_ROUTE);
     48 	link_cache = nl_cli_link_alloc_cache(sock);
     49 	neigh_cache = nl_cli_neigh_alloc_cache(sock);
     50  	neigh = nl_cli_neigh_alloc();
     51 
     52 	for (;;) {
     53 		int c, optidx = 0;
     54 		enum {
     55 			ARG_FAMILY = 257,
     56 			ARG_STATE = 258,
     57 		};
     58 		static struct option long_opts[] = {
     59 			{ "format", 1, 0, 'f' },
     60 			{ "help", 0, 0, 'h' },
     61 			{ "version", 0, 0, 'v' },
     62 			{ "addr", 1, 0, 'a' },
     63 			{ "lladdr", 1, 0, 'l' },
     64 			{ "dev", 1, 0, 'd' },
     65 			{ "family", 1, 0, ARG_FAMILY },
     66 			{ "state", 1, 0, ARG_STATE },
     67 			{ 0, 0, 0, 0 }
     68 		};
     69 
     70 		c = getopt_long(argc, argv, "f:hva:l:d:", long_opts, &optidx);
     71 		if (c == -1)
     72 			break;
     73 
     74 		switch (c) {
     75 		case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
     76 		case 'h': print_usage(); break;
     77 		case 'v': nl_cli_print_version(); break;
     78 		case 'a': nl_cli_neigh_parse_dst(neigh, optarg); break;
     79 		case 'l': nl_cli_neigh_parse_lladdr(neigh, optarg); break;
     80 		case 'd': nl_cli_neigh_parse_dev(neigh, link_cache, optarg); break;
     81 		case ARG_FAMILY: nl_cli_neigh_parse_family(neigh, optarg); break;
     82 		case ARG_STATE: nl_cli_neigh_parse_state(neigh, optarg); break;
     83 		}
     84  	}
     85 
     86 	nl_cache_dump_filter(neigh_cache, &params, OBJ_CAST(neigh));
     87 
     88 	return 0;
     89 }
     90