This commit was generated by cvs2svn to compensate for changes in r786,
[libnl.git] / src / nl-neigh-add.c
1 /*
2  * src/ nl-neigh-add.c     Add a neighbour
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_neigh *neigh;
18         struct nl_addr *addr;
19         int err = 1;
20
21         if (nltool_init(argc, argv) < 0)
22                 return -1;
23
24         if (argc < 4 || !strcmp(argv[1], "-h")) {
25                 printf("Usage: nl-neigh-add <addr> <lladdr> "
26                        "<ifindex> [<state>]\n");
27                 return 1;
28         }
29
30         nlh = nl_handle_alloc_nondefault(nltool_cbset);
31         if (!nlh)
32                 return -1;
33
34         neigh = rtnl_neigh_alloc();
35         if (!neigh)
36                 goto errout;
37
38         if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
39                 goto errout_free;
40
41         addr = nltool_addr_parse(argv[1]);
42         if (!addr)
43                 goto errout_close;
44         rtnl_neigh_set_dst(neigh, addr);
45         nl_addr_put(addr);
46
47         addr = nltool_addr_parse(argv[2]);
48         if (!addr)
49                 goto errout_close;
50         rtnl_neigh_set_lladdr(neigh, addr);
51         nl_addr_put(addr);
52
53         rtnl_neigh_set_ifindex(neigh, strtoul(argv[3], NULL, 0));
54
55         if (argc > 4) {
56                 int state = rtnl_neigh_str2state(argv[4]);
57                 if (state < 0) {
58                         fprintf(stderr, "Unknown state \"%s\"\n", argv[4]);
59                         goto errout_close;
60                 }
61                 rtnl_neigh_set_state(neigh, state);
62         } else
63                 rtnl_neigh_set_state(neigh, NUD_PERMANENT);
64
65         if (rtnl_neigh_add(nlh, neigh, 0) < 0) {
66                 fprintf(stderr, "Unable to add address: %s\n", nl_geterror());
67                 goto errout_close;
68         }
69
70         err = 0;
71
72 errout_close:
73         nl_close(nlh);
74 errout_free:
75         rtnl_neigh_put(neigh);
76 errout:
77         nl_handle_destroy(nlh);
78         return err;
79 }