summaryrefslogtreecommitdiffstats
path: root/src/nl-neigh-add.c
blob: 14b99de2d8757b2a1169b6d230b17c971f15159c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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 <tgraf@suug.ch>
 */

#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 <addr> <lladdr> "
		       "<ifindex> [<state>]\n");
		return 1;
	}

	nlh = nltool_alloc_handle();
	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;
}