• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Petr Vorel <pvorel@suse.cz>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <arpa/inet.h>
19 #include <errno.h>
20 
21 #define MAX_IPV4_PREFIX 32
22 #define MAX_IPV6_PREFIX 128
23 
24 #define tst_res_comment(...) { \
25 	fprintf(stderr, "# "); \
26 	tst_res(__VA_ARGS__); } \
27 
28 
29 #define tst_brk_comment(...) { \
30 	fprintf(stderr, "# "); \
31 	tst_brk(TCONF, __VA_ARGS__); } \
32 
print_svar(const char * name,const char * val)33 static inline void print_svar(const char *name, const char *val)
34 {
35 	if (name && val)
36 		printf("export %s=\"%s\"\n", name, val);
37 }
38 
print_svar_change(const char * name,const char * val)39 static inline void print_svar_change(const char *name, const char *val)
40 {
41 	if (name && val)
42 		printf("export %s=\"${%s:-%s}\"\n", name, name, val);
43 }
44 
45 /*
46  * Function bit_count is from ipcalc project, ipcalc.c.
47  */
bit_count(uint32_t i)48 static int bit_count(uint32_t i)
49 {
50 	int c = 0;
51 	unsigned int seen_one = 0;
52 
53 	while (i > 0) {
54 		if (i & 1) {
55 			seen_one = 1;
56 			c++;
57 		} else {
58 			if (seen_one)
59 				return -1;
60 		}
61 		i >>= 1;
62 	}
63 
64 	return c;
65 }
66 
67 /*
68  * Function mask2prefix is from ipcalc project, ipcalc.c.
69  */
mask2prefix(struct in_addr mask)70 static int mask2prefix(struct in_addr mask)
71 {
72 	return bit_count(ntohl(mask.s_addr));
73 }
74 
75 /*
76  * Function ipv4_mask_to_int is from ipcalc project, ipcalc.c.
77  */
ipv4_mask_to_int(const char * prefix)78 static int ipv4_mask_to_int(const char *prefix)
79 {
80 	int ret;
81 	struct in_addr in;
82 
83 	ret = inet_pton(AF_INET, prefix, &in);
84 	if (ret == 0)
85 		return -1;
86 
87 	return mask2prefix(in);
88 }
89 
90 /*
91  * Function safe_atoi is from ipcalc project, ipcalc.c.
92  */
safe_atoi(const char * s,int * ret_i)93 static int safe_atoi(const char *s, int *ret_i)
94 {
95 	char *x = NULL;
96 	long l;
97 
98 	errno = 0;
99 	l = strtol(s, &x, 0);
100 
101 	if (!x || x == s || *x || errno)
102 		return errno > 0 ? -errno : -EINVAL;
103 
104 	if ((long)(int)l != l)
105 		return -ERANGE;
106 
107 	*ret_i = (int)l;
108 
109 	return 0;
110 }
111 
112 /*
113  * Function get_prefix use code from ipcalc project, str_to_prefix/ipcalc.c.
114  */
get_prefix(const char * ip_str,int is_ipv6)115 static int get_prefix(const char *ip_str, int is_ipv6)
116 {
117 	char *prefix_str = NULL;
118 	int prefix = -1, r;
119 
120 	prefix_str = strchr(ip_str, '/');
121 	if (!prefix_str)
122 		return -1;
123 
124 	*(prefix_str++) = '\0';
125 
126 	if (!is_ipv6 && strchr(prefix_str, '.'))
127 		prefix = ipv4_mask_to_int(prefix_str);
128 	else {
129 		r = safe_atoi(prefix_str, &prefix);
130 		if (r != 0)
131 			tst_brk_comment("conversion error: '%s' is not integer",
132 					prefix_str);
133 	}
134 
135 	if (prefix < 0 || ((is_ipv6 && prefix > MAX_IPV6_PREFIX) ||
136 		(!is_ipv6 && prefix > MAX_IPV4_PREFIX)))
137 		tst_brk_comment("bad %s prefix: %s", is_ipv6 ?  "IPv6" : "IPv4",
138 				prefix_str);
139 
140 	return prefix;
141 }
142 
get_in_addr(const char * ip_str,struct in_addr * ip)143 static void get_in_addr(const char *ip_str, struct in_addr *ip)
144 {
145 	if (inet_pton(AF_INET, ip_str, ip) <= 0)
146 		tst_brk_comment("bad IPv4 address: '%s'", ip_str);
147 }
148 
get_in6_addr(const char * ip_str,struct in6_addr * ip6)149 static void get_in6_addr(const char *ip_str, struct in6_addr *ip6)
150 {
151 	if (inet_pton(AF_INET6, ip_str, ip6) <= 0)
152 		tst_brk_comment("bad IPv6 address: '%s'", ip_str);
153 }
154