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