oops
[libnl.git] / src / nl-addr-add.c
1 /*
2  * src/nl-addr-add.c     Add addresses
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 int main(int argc, char *argv[])
15 {
16         struct nl_handle *nlh;
17         struct rtnl_addr *addr;
18         struct nl_addr *local;
19         int err = 1;
20
21         if (argc < 3 || !strcmp(argv[1], "-h")) {
22                 printf("Usage: nl-addr-add <addr> <ifindex>\n");
23                 goto errout;
24         }
25
26         if (nltool_init(argc, argv) < 0)
27                 goto errout;
28
29         nlh = nl_handle_alloc_nondefault(nltool_cbset);
30         if (!nlh)
31                 goto errout;
32
33         addr = rtnl_addr_alloc();
34         if (!addr)
35                 goto errout_free_handle;
36
37         if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
38                 goto errout_free_addr;
39
40         local = nltool_addr_parse(argv[1]);
41         if (!local)
42                 goto errout_close;
43
44         if (rtnl_addr_set_local(addr, local) < 0) {
45                 fprintf(stderr, "Unable to set local address: %s\n",
46                         nl_geterror());
47                 goto errout_put_addr;
48         }
49
50         rtnl_addr_set_ifindex(addr, strtoul(argv[2], NULL, 0));
51
52         if (rtnl_addr_add(nlh, addr, 0) < 0) {
53                 fprintf(stderr, "Unable to add address: %s\n", nl_geterror());
54                 goto errout_close;
55         }
56
57         err = 0;
58 errout_put_addr:
59         nl_addr_put(local);
60 errout_close:
61         nl_close(nlh);
62 errout_free_addr:
63         rtnl_addr_put(addr);
64 errout_free_handle:
65         nl_handle_destroy(nlh);
66 errout:
67         return err;
68 }