summaryrefslogtreecommitdiffstats
path: root/src/f_addr.c
blob: 491d88f2a53dec02da58d22aeeafd008730f469f (plain)
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
/*
 * src/f_addr.c     	Address Filter
 *
 *	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) 2003-2006 Thomas Graf <tgraf@suug.ch>
 */

static void get_filter(struct rtnl_addr *addr, int argc, char **argv, int idx,
		       struct nl_cache *link_cache)
{
	struct nl_addr *a;

	while (argc > idx) {
		if (arg_match("dev")) {
			if (argc > ++idx) {
				int ifindex = rtnl_link_name2i(link_cache, argv[idx++]);
				if (ifindex == RTNL_LINK_NOT_FOUND)
					goto err_notfound;
				rtnl_addr_set_ifindex(addr, ifindex);
			}
		} else if (arg_match("family")) {
			if (argc > ++idx) {
				int family = nl_str2af(argv[idx++]);
				if (family == AF_UNSPEC)
					goto err_invaf;
				rtnl_addr_set_family(addr, family);
			}
		} else if (arg_match("label")) {
			if (argc > ++idx)
				rtnl_addr_set_label(addr, argv[idx++]);
		} else if (arg_match("scope")) {
			if (argc > ++idx) {
				int scope = rtnl_str2scope(argv[idx++]);
				if (scope < 0)
					goto err_invscope;
				rtnl_addr_set_scope(addr, scope);
			}
		} else if (arg_match("local")) {
			if (argc > ++idx) {
				a = nl_addr_parse(argv[idx++],
						  rtnl_addr_get_family(addr));
				if (!a)
					goto err_invaddr;
				rtnl_addr_set_local(addr, a);
				nl_addr_put(a);
			}
		} else if (arg_match("peer")) {
			if (argc > ++idx) {
				a = nl_addr_parse(argv[idx++],
						  rtnl_addr_get_family(addr));
				if (!a)
					goto err_invaddr;
				rtnl_addr_set_peer(addr, a);
				nl_addr_put(a);
			}
		} else if (arg_match("broadcast")) {
			if (argc > ++idx) {
				a = nl_addr_parse(argv[idx++],
						  rtnl_addr_get_family(addr));
				if (!a)
					goto err_invaddr;
				rtnl_addr_set_broadcast(addr, a);
				nl_addr_put(a);
			}
		} else if (arg_match("multicast")) {
			if (argc > ++idx) {
				a = nl_addr_parse(argv[idx++],
						  rtnl_addr_get_family(addr));
				if (!a)
					goto err_invaddr;
				rtnl_addr_set_multicast(addr, a);
				nl_addr_put(a);
			}
		} else if (arg_match("anycast")) {
			if (argc > ++idx) {
				a = nl_addr_parse(argv[idx++],
						  rtnl_addr_get_family(addr));
				if (!a)
					goto err_invaddr;
				rtnl_addr_set_anycast(addr, a);
				nl_addr_put(a);
			}
		} else {
			fprintf(stderr, "What is '%s'?\n", argv[idx]);
			exit(1);
		}
	}

	return;

err_notfound:
	fprintf(stderr, "Unknown link %s\n", argv[idx-1]);
	exit(1);
err_invscope:
	fprintf(stderr, "Invalid scope name \"%s\".\n", argv[idx-1]);
	exit(1);
err_invaf:
	fprintf(stderr, "Invalid address family \"%s\"\n", argv[idx-1]);
	exit(1);
err_invaddr:
	fprintf(stderr, "Invalid address \"%s\": %s\n", argv[idx-1], nl_geterror());
	exit(1);
}