File: addr.c

package info (click to toggle)
libnl3 3.4.0-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,120 kB
  • sloc: ansic: 57,494; sh: 4,447; python: 3,702; makefile: 888; yacc: 478; cpp: 454; lex: 210
file content (135 lines) | stat: -rw-r--r-- 2,984 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * src/lib/addr.c     Address Helpers
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2008-2009 Thomas Graf <tgraf@suug.ch>
 */

/**
 * @ingroup cli
 * @defgroup cli_addr Addresses
 *
 * @{
 */

#include <netlink/cli/utils.h>
#include <netlink/cli/addr.h>

struct rtnl_addr *nl_cli_addr_alloc(void)
{
	struct rtnl_addr *addr;

	addr = rtnl_addr_alloc();
	if (!addr)
		nl_cli_fatal(ENOMEM, "Unable to allocate address object");

	return addr;
}

void nl_cli_addr_parse_family(struct rtnl_addr *addr, char *arg)
{
	int family;

	if ((family = nl_str2af(arg)) != AF_UNSPEC)
		rtnl_addr_set_family(addr, family);
}

void nl_cli_addr_parse_local(struct rtnl_addr *addr, char *arg)
{
	struct nl_addr *a;
	int err;

	a = nl_cli_addr_parse(arg, rtnl_addr_get_family(addr));
	if ((err = rtnl_addr_set_local(addr, a)) < 0)
		nl_cli_fatal(err, "Unable to set local address: %s",
			     nl_geterror(err));

	nl_addr_put(a);
}

void nl_cli_addr_parse_dev(struct rtnl_addr *addr, struct nl_cache *link_cache,
			   char *arg)
{
	int ival;

	if (!(ival = rtnl_link_name2i(link_cache, arg)))
		nl_cli_fatal(ENOENT, "Link \"%s\" does not exist", arg);

	rtnl_addr_set_ifindex(addr, ival);
}

void nl_cli_addr_parse_label(struct rtnl_addr *addr, char *arg)
{
	int err;

	if ((err = rtnl_addr_set_label(addr, arg)) < 0)
		nl_cli_fatal(err, "Unable to set address label: %s",
			     nl_geterror(err));
}

void nl_cli_addr_parse_peer(struct rtnl_addr *addr, char *arg)
{
	struct nl_addr *a;
	int err;

	a = nl_cli_addr_parse(arg, rtnl_addr_get_family(addr));
	if ((err = rtnl_addr_set_peer(addr, a)) < 0)
		nl_cli_fatal(err, "Unable to set peer address: %s",
			     nl_geterror(err));

	nl_addr_put(a);
}

void nl_cli_addr_parse_scope(struct rtnl_addr *addr, char *arg)
{
	int ival;

	if ((ival = rtnl_str2scope(arg)) < 0)
		nl_cli_fatal(EINVAL, "Unknown address scope \"%s\"", arg);

	rtnl_addr_set_scope(addr, ival);
}

void nl_cli_addr_parse_broadcast(struct rtnl_addr *addr, char *arg)
{
	struct nl_addr *a;
	int err;

	a = nl_cli_addr_parse(arg, rtnl_addr_get_family(addr));
	if ((err = rtnl_addr_set_broadcast(addr, a)) < 0)
		nl_cli_fatal(err, "Unable to set broadcast address: %s",
			     nl_geterror(err));

	nl_addr_put(a);
}

static uint32_t parse_lifetime(const char *arg)
{
	uint64_t msecs;
	int err;

	if (!strcasecmp(arg, "forever"))
		return 0xFFFFFFFFU;

	if ((err = nl_str2msec(arg, &msecs)) < 0)
		nl_cli_fatal(err, "Unable to parse time string \"%s\": %s",
			     arg, nl_geterror(err));

	return (msecs / 1000);
}

void nl_cli_addr_parse_preferred(struct rtnl_addr *addr, char *arg)
{
	rtnl_addr_set_preferred_lifetime(addr, parse_lifetime(arg));
}

void nl_cli_addr_parse_valid(struct rtnl_addr *addr, char *arg)
{
	rtnl_addr_set_valid_lifetime(addr, parse_lifetime(arg));
}

/** @} */