6#include <netlink/cli/utils.h> 
    7#include <netlink/cli/route.h> 
    8#include <netlink/cli/link.h> 
   10#include <linux/netlink.h> 
   12static int interactive = 0, default_yes = 0, quiet = 0;
 
   13static int deleted = 0;
 
   14static struct nl_sock *sock;
 
   16static void print_version(
void)
 
   18        fprintf(stderr, 
"%s\n", LIBNL_STRING);
 
   22static void print_usage(
void)
 
   25        "Usage: nl-route-delete [OPTION]... [ROUTE]\n" 
   28        " -i, --interactive     Run interactively\n" 
   29        "     --yes             Set default answer to yes\n" 
   30        " -q, --quiet           Do not print informal notifications\n" 
   31        " -h, --help            Show this help\n" 
   32        " -v, --version         Show versioning information\n" 
   35        " -d, --dst=ADDR        destination prefix, e.g. 10.10.0.0/16\n" 
   36        " -n, --nexthop=NH      nexthop configuration:\n" 
   37        "                         dev=DEV         route via device\n" 
   38        "                         weight=WEIGHT   weight of nexthop\n" 
   40        "                         via=GATEWAY     route via other node\n" 
   42        "                         e.g. dev=eth0,via=192.168.1.12\n" 
   43        " -t, --table=TABLE     Routing table\n" 
   44        "     --family=FAMILY   Address family\n" 
   45        "     --src=ADDR        Source prefix\n" 
   46        "     --iif=DEV         Incomming interface\n" 
   47        "     --pref-src=ADDR   Preferred source address\n" 
   48        "     --metrics=OPTS    Metrics configurations\n" 
   49        "     --priority=NUM    Priotity\n" 
   50        "     --scope=SCOPE     Scope\n" 
   51        "     --protocol=PROTO  Protocol\n" 
   52        "     --type=TYPE       { unicast | local | broadcast | multicast }\n" 
   57static void delete_cb(
struct nl_object *obj, 
void *arg)
 
   59        struct rtnl_route *route = (
struct rtnl_route *) obj;
 
   66        if (interactive && !nl_cli_confirm(obj, ¶ms, default_yes))
 
   69        if ((err = rtnl_route_delete(sock, route, 0)) < 0)
 
   70                nl_cli_fatal(err, 
"Unable to delete route: %s", nl_geterror(err));
 
   80int main(
int argc, 
char *argv[])
 
   82        struct nl_cache *link_cache, *route_cache;
 
   83        struct rtnl_route *route;
 
   86        sock = nl_cli_alloc_socket();
 
   87        nl_cli_connect(sock, NETLINK_ROUTE);
 
   88        link_cache = nl_cli_link_alloc_cache(sock);
 
   89        route_cache = nl_cli_route_alloc_cache(sock, 0);
 
   90        route = nl_cli_route_alloc();
 
  106                static struct option long_opts[] = {
 
  107                        { 
"interactive", 0, 0, 
'i' },
 
  108                        { 
"yes", 0, 0, ARG_YES },
 
  109                        { 
"quiet", 0, 0, 
'q' },
 
  110                        { 
"help", 0, 0, 
'h' },
 
  111                        { 
"version", 0, 0, 
'v' },
 
  112                        { 
"dst", 1, 0, 
'd' },
 
  113                        { 
"nexthop", 1, 0, 
'n' },
 
  114                        { 
"table", 1, 0, 
't' },
 
  115                        { 
"family", 1, 0, ARG_FAMILY },
 
  116                        { 
"src", 1, 0, ARG_SRC },
 
  117                        { 
"iif", 1, 0, ARG_IIF },
 
  118                        { 
"pref-src", 1, 0, ARG_PREF_SRC },
 
  119                        { 
"metrics", 1, 0, ARG_METRICS },
 
  120                        { 
"priority", 1, 0, ARG_PRIORITY },
 
  121                        { 
"scope", 1, 0, ARG_SCOPE },
 
  122                        { 
"protocol", 1, 0, ARG_PROTOCOL },
 
  123                        { 
"type", 1, 0, ARG_TYPE },
 
  127                c = getopt_long(argc, argv, 
"iqhvd:n:t:", long_opts, &optidx);
 
  132                case 'i': interactive = 1; 
break;
 
  133                case ARG_YES: default_yes = 1; 
break;
 
  134                case 'q': quiet = 1; 
break;
 
  135                case 'h': print_usage(); 
break;
 
  136                case 'v': print_version(); 
break;
 
  137                case 'd': nf++; nl_cli_route_parse_dst(route, optarg); 
break;
 
  138                case 'n': nf++; nl_cli_route_parse_nexthop(route, optarg, link_cache); 
break;
 
  139                case 't': nf++; nl_cli_route_parse_table(route, optarg); 
break;
 
  140                case ARG_FAMILY: nf++; nl_cli_route_parse_family(route, optarg); 
break;
 
  141                case ARG_SRC: nf++; nl_cli_route_parse_src(route, optarg); 
break;
 
  142                case ARG_IIF: nf++; nl_cli_route_parse_iif(route, optarg, link_cache); 
break;
 
  143                case ARG_PREF_SRC: nf++; nl_cli_route_parse_pref_src(route, optarg); 
break;
 
  144                case ARG_METRICS: nf++; nl_cli_route_parse_metric(route, optarg); 
break;
 
  145                case ARG_PRIORITY: nf++; nl_cli_route_parse_prio(route, optarg); 
break;
 
  146                case ARG_SCOPE: nf++; nl_cli_route_parse_scope(route, optarg); 
break;
 
  147                case ARG_PROTOCOL: nf++; nl_cli_route_parse_protocol(route, optarg); 
break;
 
  148                case ARG_TYPE: nf++; nl_cli_route_parse_type(route, optarg); 
break;
 
  152        if (nf == 0 && !interactive && !default_yes) {
 
  153                fprintf(stderr, 
"You attempted to delete all routes in " 
  154                        "non-interactive mode, aborting.\n");
 
  161                printf(
"Deleted %d routes\n", deleted);
 
void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache (filtered).
 
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
 
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
 
@ NL_DUMP_LINE
Dump object briefly on one line.
 
enum nl_dump_type dp_type
Specifies the type of dump that is requested.