1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * src/lib/neigh.c CLI Neighbour Helpers
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation version 2.1
8 * of the License.
9 *
10 * Copyright (c) 2008-2009 Thomas Graf <tgraf@suug.ch>
11 */
12
13 /**
14 * @ingroup cli
15 * @defgroup cli_neigh Neighbour
16 *
17 * @{
18 */
19
20 #include <netlink/cli/utils.h>
21 #include <netlink/cli/neigh.h>
22
nl_cli_neigh_alloc(void)23 struct rtnl_neigh *nl_cli_neigh_alloc(void)
24 {
25 struct rtnl_neigh *neigh;
26
27 neigh = rtnl_neigh_alloc();
28 if (!neigh)
29 nl_cli_fatal(ENOMEM, "Unable to allocate neighbour object");
30
31 return neigh;
32 }
33
nl_cli_neigh_parse_dst(struct rtnl_neigh * neigh,char * arg)34 void nl_cli_neigh_parse_dst(struct rtnl_neigh *neigh, char *arg)
35 {
36 struct nl_addr *a;
37 int err;
38
39 a = nl_cli_addr_parse(arg, rtnl_neigh_get_family(neigh));
40 if ((err = rtnl_neigh_set_dst(neigh, a)) < 0)
41 nl_cli_fatal(err, "Unable to set local address: %s",
42 nl_geterror(err));
43
44 nl_addr_put(a);
45 }
46
nl_cli_neigh_parse_lladdr(struct rtnl_neigh * neigh,char * arg)47 void nl_cli_neigh_parse_lladdr(struct rtnl_neigh *neigh, char *arg)
48 {
49 struct nl_addr *a;
50
51 a = nl_cli_addr_parse(arg, AF_UNSPEC);
52 rtnl_neigh_set_lladdr(neigh, a);
53 nl_addr_put(a);
54 }
55
nl_cli_neigh_parse_dev(struct rtnl_neigh * neigh,struct nl_cache * link_cache,char * arg)56 void nl_cli_neigh_parse_dev(struct rtnl_neigh *neigh,
57 struct nl_cache *link_cache, char *arg)
58 {
59 int ival;
60
61 if (!(ival = rtnl_link_name2i(link_cache, arg)))
62 nl_cli_fatal(ENOENT, "Link \"%s\" does not exist", arg);
63
64 rtnl_neigh_set_ifindex(neigh, ival);
65 }
66
nl_cli_neigh_parse_family(struct rtnl_neigh * neigh,char * arg)67 void nl_cli_neigh_parse_family(struct rtnl_neigh *neigh, char *arg)
68 {
69 int family;
70
71 if ((family = nl_str2af(arg)) == AF_UNSPEC)
72 nl_cli_fatal(EINVAL,
73 "Unable to translate address family \"%s\"", arg);
74
75 rtnl_neigh_set_family(neigh, family);
76 }
77
nl_cli_neigh_parse_state(struct rtnl_neigh * neigh,char * arg)78 void nl_cli_neigh_parse_state(struct rtnl_neigh *neigh, char *arg)
79 {
80 int state;
81
82 if ((state = rtnl_neigh_str2state(arg)) < 0)
83 nl_cli_fatal(state, "Unable to translate state \"%s\": %s",
84 arg, state);
85
86 rtnl_neigh_set_state(neigh, state);
87 }
88
89 /** @} */
90