• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2008-2009 Thomas Graf <tgraf@suug.ch>
4  */
5 
6 /**
7  * @ingroup cli
8  * @defgroup cli_addr Addresses
9  *
10  * @{
11  */
12 
13 #include <netlink/cli/utils.h>
14 #include <netlink/cli/addr.h>
15 
nl_cli_addr_alloc(void)16 struct rtnl_addr *nl_cli_addr_alloc(void)
17 {
18 	struct rtnl_addr *addr;
19 
20 	addr = rtnl_addr_alloc();
21 	if (!addr)
22 		nl_cli_fatal(ENOMEM, "Unable to allocate address object");
23 
24 	return addr;
25 }
26 
nl_cli_addr_parse_family(struct rtnl_addr * addr,char * arg)27 void nl_cli_addr_parse_family(struct rtnl_addr *addr, char *arg)
28 {
29 	int family;
30 
31 	if ((family = nl_str2af(arg)) != AF_UNSPEC)
32 		rtnl_addr_set_family(addr, family);
33 }
34 
nl_cli_addr_parse_local(struct rtnl_addr * addr,char * arg)35 void nl_cli_addr_parse_local(struct rtnl_addr *addr, char *arg)
36 {
37 	struct nl_addr *a;
38 	int err;
39 
40 	a = nl_cli_addr_parse(arg, rtnl_addr_get_family(addr));
41 	if ((err = rtnl_addr_set_local(addr, a)) < 0)
42 		nl_cli_fatal(err, "Unable to set local address: %s",
43 			     nl_geterror(err));
44 
45 	nl_addr_put(a);
46 }
47 
nl_cli_addr_parse_dev(struct rtnl_addr * addr,struct nl_cache * link_cache,char * arg)48 void nl_cli_addr_parse_dev(struct rtnl_addr *addr, struct nl_cache *link_cache,
49 			   char *arg)
50 {
51 	int ival;
52 
53 	if (!(ival = rtnl_link_name2i(link_cache, arg)))
54 		nl_cli_fatal(ENOENT, "Link \"%s\" does not exist", arg);
55 
56 	rtnl_addr_set_ifindex(addr, ival);
57 }
58 
nl_cli_addr_parse_label(struct rtnl_addr * addr,char * arg)59 void nl_cli_addr_parse_label(struct rtnl_addr *addr, char *arg)
60 {
61 	int err;
62 
63 	if ((err = rtnl_addr_set_label(addr, arg)) < 0)
64 		nl_cli_fatal(err, "Unable to set address label: %s",
65 			     nl_geterror(err));
66 }
67 
nl_cli_addr_parse_peer(struct rtnl_addr * addr,char * arg)68 void nl_cli_addr_parse_peer(struct rtnl_addr *addr, char *arg)
69 {
70 	struct nl_addr *a;
71 	int err;
72 
73 	a = nl_cli_addr_parse(arg, rtnl_addr_get_family(addr));
74 	if ((err = rtnl_addr_set_peer(addr, a)) < 0)
75 		nl_cli_fatal(err, "Unable to set peer address: %s",
76 			     nl_geterror(err));
77 
78 	nl_addr_put(a);
79 }
80 
nl_cli_addr_parse_scope(struct rtnl_addr * addr,char * arg)81 void nl_cli_addr_parse_scope(struct rtnl_addr *addr, char *arg)
82 {
83 	int ival;
84 
85 	if ((ival = rtnl_str2scope(arg)) < 0)
86 		nl_cli_fatal(EINVAL, "Unknown address scope \"%s\"", arg);
87 
88 	rtnl_addr_set_scope(addr, ival);
89 }
90 
nl_cli_addr_parse_broadcast(struct rtnl_addr * addr,char * arg)91 void nl_cli_addr_parse_broadcast(struct rtnl_addr *addr, char *arg)
92 {
93 	struct nl_addr *a;
94 	int err;
95 
96 	a = nl_cli_addr_parse(arg, rtnl_addr_get_family(addr));
97 	if ((err = rtnl_addr_set_broadcast(addr, a)) < 0)
98 		nl_cli_fatal(err, "Unable to set broadcast address: %s",
99 			     nl_geterror(err));
100 
101 	nl_addr_put(a);
102 }
103 
parse_lifetime(const char * arg)104 static uint32_t parse_lifetime(const char *arg)
105 {
106 	uint64_t msecs;
107 	int err;
108 
109 	if (!strcasecmp(arg, "forever"))
110 		return 0xFFFFFFFFU;
111 
112 	if ((err = nl_str2msec(arg, &msecs)) < 0)
113 		nl_cli_fatal(err, "Unable to parse time string \"%s\": %s",
114 			     arg, nl_geterror(err));
115 
116 	return (msecs / 1000);
117 }
118 
nl_cli_addr_parse_preferred(struct rtnl_addr * addr,char * arg)119 void nl_cli_addr_parse_preferred(struct rtnl_addr *addr, char *arg)
120 {
121 	rtnl_addr_set_preferred_lifetime(addr, parse_lifetime(arg));
122 }
123 
nl_cli_addr_parse_valid(struct rtnl_addr * addr,char * arg)124 void nl_cli_addr_parse_valid(struct rtnl_addr *addr, char *arg)
125 {
126 	rtnl_addr_set_valid_lifetime(addr, parse_lifetime(arg));
127 }
128 
129 /** @} */
130