add packaging
[platform/upstream/libnl1.git] / src / nl-route-add.c
1 /*
2  * src/nl-route-dump.c     Dump route attributes
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-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #include "utils.h"
13
14 static void print_usage(void)
15 {
16         printf(
17         "Usage: nl-route-add [<filter>]\n");
18         exit(1);
19 }
20
21 #include "f_route.c"
22
23 int main(int argc, char *argv[])
24 {
25         struct nl_handle *nlh;
26         struct nl_cache *link_cache, *route_cache;
27         struct rtnl_route *route;
28         int err = 1;
29
30         if (nltool_init(argc, argv) < 0)
31                 return -1;
32
33         if (argc < 2 || !strcmp(argv[1], "-h"))
34                 print_usage();
35
36         nlh = nltool_alloc_handle();
37         if (!nlh)
38                 goto errout;
39
40         route = rtnl_route_alloc();
41         if (!route)
42                 goto errout;
43
44         if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
45                 goto errout_free;
46
47         link_cache = nltool_alloc_link_cache(nlh);
48         if (!link_cache)
49                 goto errout_close;
50
51         route_cache = nltool_alloc_route_cache(nlh);
52         if (!route_cache)
53                 goto errout_link_cache;
54
55         get_filter(route, argc, argv, 1, route_cache, link_cache);
56
57         if (rtnl_route_add(nlh, route, 0) < 0) {
58                 fprintf(stderr, "rtnl_route_add failed: %s\n",
59                 nl_geterror());
60                 goto errout_route_cache;
61         }
62
63         err = 0;
64
65 errout_route_cache:
66         nl_cache_free(route_cache);
67 errout_link_cache:
68         nl_cache_free(link_cache);
69 errout_close:
70         nl_close(nlh);
71 errout_free:
72         rtnl_route_put(route);
73 errout:
74         nl_handle_destroy(nlh);
75         return err;
76 }