X-Git-Url: http://git.onelab.eu/?p=libnl.git;a=blobdiff_plain;f=src%2Fnl-neigh-add.c;fp=src%2Fnl-neigh-add.c;h=7bc854eeb9e5dd0c966a9a363136cfc2280d2127;hp=0000000000000000000000000000000000000000;hb=4cee2ecb3b8afa0637e6f5fe4c57985a4bc740ff;hpb=2df2fbe518d5a221ce6e3ee88a3fb23fb1b94b27 diff --git a/src/nl-neigh-add.c b/src/nl-neigh-add.c new file mode 100644 index 0000000..7bc854e --- /dev/null +++ b/src/nl-neigh-add.c @@ -0,0 +1,79 @@ +/* + * src/ nl-neigh-add.c Add a neighbour + * + * 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 + */ + +#include "utils.h" + +int main(int argc, char *argv[]) +{ + struct nl_handle *nlh; + struct rtnl_neigh *neigh; + struct nl_addr *addr; + int err = 1; + + if (nltool_init(argc, argv) < 0) + return -1; + + if (argc < 4 || !strcmp(argv[1], "-h")) { + printf("Usage: nl-neigh-add " + " []\n"); + return 1; + } + + nlh = nl_handle_alloc_nondefault(nltool_cbset); + if (!nlh) + return -1; + + neigh = rtnl_neigh_alloc(); + if (!neigh) + goto errout; + + if (nltool_connect(nlh, NETLINK_ROUTE) < 0) + goto errout_free; + + addr = nltool_addr_parse(argv[1]); + if (!addr) + goto errout_close; + rtnl_neigh_set_dst(neigh, addr); + nl_addr_put(addr); + + addr = nltool_addr_parse(argv[2]); + if (!addr) + goto errout_close; + rtnl_neigh_set_lladdr(neigh, addr); + nl_addr_put(addr); + + rtnl_neigh_set_ifindex(neigh, strtoul(argv[3], NULL, 0)); + + if (argc > 4) { + int state = rtnl_neigh_str2state(argv[4]); + if (state < 0) { + fprintf(stderr, "Unknown state \"%s\"\n", argv[4]); + goto errout_close; + } + rtnl_neigh_set_state(neigh, state); + } else + rtnl_neigh_set_state(neigh, NUD_PERMANENT); + + if (rtnl_neigh_add(nlh, neigh, 0) < 0) { + fprintf(stderr, "Unable to add address: %s\n", nl_geterror()); + goto errout_close; + } + + err = 0; + +errout_close: + nl_close(nlh); +errout_free: + rtnl_neigh_put(neigh); +errout: + nl_handle_destroy(nlh); + return err; +}