nl-neigh-add.c revision 10cf5a586c149fdb7e2639000dbfae5e6f8522a5
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-2008 Thomas Graf <tgraf@suug.ch>
10 */
11
12#include "neigh-utils.h"
13
14static int quiet = 0;
15
16static void print_usage(void)
17{
18	printf(
19	"Usage: nl-neigh-add [OPTION]... NEIGHBOUR\n"
20	"\n"
21	"Options\n"
22	"     --update-only     Do not create neighbour, updates exclusively\n"
23	"     --create-only     Do not update neighbour if it exists already.\n"
24	" -q, --quiet           Do not print informal notifications\n"
25	" -h, --help            Show this help\n"
26	" -v, --version         Show versioning information\n"
27	"\n"
28	"Neighbour Options\n"
29	" -a, --addr=ADDR       Destination address of neighbour\n"
30	" -l, --lladdr=ADDR     Link layer address of neighbour\n"
31	" -d, --dev=DEV         Device the neighbour is connected to\n"
32	"     --state=STATE     Neighbour state, (default = permanent)\n"
33	"\n"
34	"Example\n"
35	"  nl-neigh-add --create-only --addr=10.0.0.1 --dev=eth0 \\\n"
36	"               --lladdr=AA:BB:CC:DD:EE:FF\n"
37	);
38
39	exit(0);
40}
41
42int main(int argc, char *argv[])
43{
44	struct nl_sock *sock;
45	struct rtnl_neigh *neigh;
46	struct nl_cache *link_cache;
47	struct nl_dump_params dp = {
48		.dp_type = NL_DUMP_ONELINE,
49		.dp_fd = stdout,
50	};
51	int err, ok = 0, nlflags = NLM_F_REPLACE | NLM_F_CREATE;
52
53	sock = nlt_alloc_socket();
54	nlt_connect(sock, NETLINK_ROUTE);
55	link_cache = nlt_alloc_link_cache(sock);
56 	neigh = nlt_alloc_neigh();
57
58	for (;;) {
59		int c, optidx = 0;
60		enum {
61			ARG_UPDATE_ONLY = 257,
62			ARG_CREATE_ONLY = 258,
63			ARG_STATE,
64		};
65		static struct option long_opts[] = {
66			{ "update-only", 0, 0, ARG_UPDATE_ONLY },
67			{ "create-only", 0, 0, ARG_CREATE_ONLY },
68			{ "quiet", 0, 0, 'q' },
69			{ "help", 0, 0, 'h' },
70			{ "version", 0, 0, 'v' },
71			{ "addr", 1, 0, 'a' },
72			{ "lladdr", 1, 0, 'l' },
73			{ "dev", 1, 0, 'd' },
74			{ "state", 1, 0, ARG_STATE },
75			{ 0, 0, 0, 0 }
76		};
77
78		c = getopt_long(argc, argv, "qhva:l:d:", long_opts, &optidx);
79		if (c == -1)
80			break;
81
82		switch (c) {
83		case ARG_UPDATE_ONLY: nlflags &= ~NLM_F_CREATE; break;
84		case ARG_CREATE_ONLY: nlflags |= NLM_F_EXCL; break;
85		case 'q': quiet = 1; break;
86		case 'h': print_usage(); break;
87		case 'v': nlt_print_version(); break;
88		case 'a': ok++; parse_dst(neigh, optarg); break;
89		case 'l': parse_lladdr(neigh, optarg); break;
90		case 'd': parse_dev(neigh, link_cache, optarg); break;
91		case ARG_STATE: parse_state(neigh, optarg); break;
92		}
93 	}
94
95	if (!ok)
96		print_usage();
97
98	if ((err = rtnl_neigh_add(sock, neigh, nlflags)) < 0)
99		fatal(err, "Unable to add neighbour: %s", nl_geterror(err));
100
101	if (!quiet) {
102		printf("Added ");
103		nl_object_dump(OBJ_CAST(neigh), &dp);
104 	}
105
106	return 0;
107}
108