libnl: /data/src/libnl3/src/nl-neigh-add.c Source File

libnl 3.7.0

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