libnl: /data/src/libnl3/src/lib/addr.c Source File

libnl 3.10.0

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