diff options
author | Thomas Graf <tgr@lsx.localdomain> | 2008-04-29 23:31:30 +0200 |
---|---|---|
committer | Thomas Graf <tgr@lsx.localdomain> | 2008-04-29 23:31:30 +0200 |
commit | 535e83162249ed6274ba46bc72d8cc683ba20e17 (patch) | |
tree | fa3b60b4116668b86d23c11b695ff371d771b997 /src/nl-route-add.c | |
parent | 8ac78f1552fa6b6340776513c8a7c36f7b72e498 (diff) | |
download | libnl-535e83162249ed6274ba46bc72d8cc683ba20e17.tar.gz |
Big routing code rework (API/ABI BREAK!)
Adds all missing routing attributes and brings the routing
related code to a working state. In the process the API
was broken several times with the justification that nobody
is using this code yet.
The changes include new example code which is also a prototype
for how plain CLI tools could look like to control routes.
Diffstat (limited to 'src/nl-route-add.c')
-rw-r--r-- | src/nl-route-add.c | 116 |
1 files changed, 82 insertions, 34 deletions
diff --git a/src/nl-route-add.c b/src/nl-route-add.c index 2686397..eb63a22 100644 --- a/src/nl-route-add.c +++ b/src/nl-route-add.c @@ -1,76 +1,124 @@ /* - * src/nl-route-dump.c Dump route attributes + * src/nl-route-add.c Route addition utility * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * 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-2008 Thomas Graf <tgraf@suug.ch> */ -#include "utils.h" +#include "route-utils.h" + +static struct nl_cache *link_cache, *route_cache; static void print_usage(void) { printf( - "Usage: nl-route-add [<filter>]\n"); + "Usage: nl-route-add [OPTION]... --dst=ADDR --nh=NEXTHOP [--nh=...]\n" + " nl-route-add [OPTION]... ADDR NEXTHOP\n" + "\n" + "Required Options\n" + " -d, --dst=ADDR destination prefix, e.g. 10.10.0.0/16\n" + " -n, --nh=NEXTHOP nexthop configuration:\n" + " dev=DEV route via device\n" + " weight=WEIGHT weight of nexthop\n" + " flags=FLAGS\n" + " via=GATEWAY route via other node\n" + " realms=REALMS\n" + "\n" + " e.g. dev=eth0,via=192.168.1.12\n" + "\n" + "Options\n" + " -s, --src=ADDR source prefix\n" + " -i, --iif=DEV incomming interface\n" + " -P, --pref-src=ADDR preferred source address\n" + " -t, --table=TABLE routing table\n" + " -m, --metric=OPTS metrics\n" + " -p, --prio=NUM priotity\n" + " -S, --scope=SCOPE scope\n" + " -x, --proto=PROTO protocol\n" + " -T, --type=TYPE routing type\n" + " -h, --help show this help\n"); exit(1); } -#include "f_route.c" - int main(int argc, char *argv[]) { struct nl_handle *nlh; - struct nl_cache *link_cache, *route_cache; struct rtnl_route *route; int err = 1; - if (nltool_init(argc, argv) < 0) - return -1; - - if (argc < 2 || !strcmp(argv[1], "-h")) - print_usage(); - nlh = nltool_alloc_handle(); - if (!nlh) - goto errout; + nltool_connect(nlh, NETLINK_ROUTE); + link_cache = nltool_alloc_link_cache(nlh); + route_cache = nltool_alloc_route_cache(nlh); route = rtnl_route_alloc(); if (!route) goto errout; - if (nltool_connect(nlh, NETLINK_ROUTE) < 0) - goto errout_free; + for (;;) { + int c, optidx = 0; + static struct option long_opts[] = { + { "dst", 1, 0, 'd' }, + { "src", 1, 0, 's' }, + { "iif", 1, 0, 'i' }, + { "nh", 1, 0, 'n' }, + { "pref-src", 1, 0, 'P' }, + { "table", 1, 0, 't' }, + { "metric", 1, 0, 'm' }, + { "prio", 1, 0, 'p' }, + { "scope", 1, 0, 'S' }, + { "proto", 1, 0, 'x' }, + { "type", 1, 0, 'T' }, + { "help", 0, 0, 'h' }, + { 0, 0, 0, 0 } + }; - link_cache = nltool_alloc_link_cache(nlh); - if (!link_cache) - goto errout_close; + c = getopt_long(argc, argv, "d:s:i:n:P:t:m:p:S:x:T:h", long_opts, &optidx); + if (c == -1) + break; - route_cache = nltool_alloc_route_cache(nlh); - if (!route_cache) - goto errout_link_cache; + switch (c) { + case 'd': parse_dst(route, optarg); break; + case 's': parse_src(route, optarg); break; + case 'i': parse_iif(route, optarg, link_cache); break; + case 'n': parse_nexthop(route, optarg, link_cache); break; + case 'P': parse_pref_src(route, optarg); break; + case 't': parse_table(route, optarg); break; + case 'm': parse_metric(route, optarg); break; + case 'p': parse_prio(route, optarg); break; + case 'S': parse_scope(route, optarg); break; + case 'x': parse_protocol(route, optarg); break; + case 'T': parse_type(route, optarg); break; + case 'h': print_usage(); break; + } + } - get_filter(route, argc, argv, 1, route_cache, link_cache); + while (optind < argc) { + if (!rtnl_route_get_dst(route)) { + parse_dst(route, argv[optind++]); + continue; + } + + /* This must all be nexthop configuration */ + } if (rtnl_route_add(nlh, route, 0) < 0) { - fprintf(stderr, "rtnl_route_add failed: %s\n", - nl_geterror()); - goto errout_route_cache; + fprintf(stderr, "rtnl_route_add failed: %s\n", nl_geterror()); + goto errout_free; } err = 0; - -errout_route_cache: - nl_cache_free(route_cache); -errout_link_cache: - nl_cache_free(link_cache); -errout_close: - nl_close(nlh); errout_free: rtnl_route_put(route); errout: + nl_cache_free(route_cache); + nl_cache_free(link_cache); + nl_close(nlh); nl_handle_destroy(nlh); + return err; } |