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