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