• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Petr Vorel <pvorel@suse.cz>
3  * Copyright (c) 1997-2015 Red Hat, Inc. All rights reserved.
4  * Copyright (c) 2011-2013 Rich Felker, et al.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <arpa/inet.h>
21 #include <arpa/nameser.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <linux/rtnetlink.h>
25 #include <net/if.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <stdarg.h>
31 
32 
33 #define TST_NO_DEFAULT_MAIN
34 #include "tst_test.h"
35 
36 #include "tst_net.h"
37 
38 #define BASE_IPV4_PREFIX 8
39 #define BASE_IPV6_PREFIX 16
40 
41 #define MAX_IPV4_PREFIX 32
42 #define MAX_IPV6_PREFIX 128
43 
44 #define DEFAULT_IPV4_UNUSED_PART1 10
45 #define DEFAULT_IPV6_UNUSED_PART1 0xfd
46 
47 #define DEFAULT_IPV4_UNUSED_PART2 23
48 #define DEFAULT_IPV6_UNUSED_PART2 0x23
49 
50 struct ltp_net_variables {
51 	char *ipv4_lbroadcast;
52 	char *ipv4_rbroadcast;
53 	char *ipv4_lnetmask;
54 	char *ipv4_rnetmask;
55 	char *ipv4_lnetwork;
56 	char *ipv4_rnetwork;
57 	char *lhost_ipv4_host;
58 	char *rhost_ipv4_host;
59 	char *ipv6_lnetmask;
60 	char *ipv6_rnetmask;
61 	char *ipv6_lnetwork;
62 	char *ipv6_rnetwork;
63 	char *lhost_ipv6_host;
64 	char *rhost_ipv6_host;
65 	char *ipv4_net16_unused;
66 	char *ipv6_net32_unused;
67 };
68 static struct ltp_net_variables vars;
69 
usage(const char * cmd)70 static void usage(const char *cmd)
71 {
72 	fprintf(stderr, "USAGE:\n"
73 		"%s IP_LHOST[/PREFIX] IP_RHOST[/PREFIX]\n"
74 		"%s -h\n\n"
75 		"Exported variables:\n"
76 		"IPV4_LBROADCAST: IPv4 broadcast of the local host\n"
77 		"IPV4_RBROADCAST: IPv4 broadcast of the remote host\n"
78 		"IPV4_LNETMASK: IPv4 netmask of the local host\n"
79 		"IPV4_RNETMASK: IPv4 netmask of the remote host\n"
80 		"IPV4_LNETWORK: IPv4 network part of IPV4_LHOST\n"
81 		"IPV4_RNETWORK: IPv4 network part of IPV4_RHOST\n"
82 		"LHOST_IPV4_HOST IPv4 host part of IPV4_LHOST\n"
83 		"RHOST_IPV4_HOST IPv4 host part of IPV4_RHOST\n"
84 		"IPV4_NET16_UNUSED: IPv4 16 bit unused subnet\n"
85 		"IPV6_LNETMASK: IPv6 netmask of the local host\n"
86 		"IPV6_RNETMASK: IPv6 netmask of the remote host\n"
87 		"IPV6_LNETWORK: IPv6 network part of IPV6_LHOST\n"
88 		"IPV6_RNETWORK: IPv6 network part of IPV6_LHOST\n"
89 		"LHOST_IPV6_HOST: IPv6 unique part of IPV6_LHOST\n"
90 		"RHOST_IPV6_HOST: IPv6 unique part of IPV6_RHOST\n"
91 		"IPV6_NET32_UNUSED: IPv6 32 bit unused subnet\n\n"
92 		"NOTE: Prefixes, ifaces and lhosts are expected to be set by tst_net_iface_prefix.\n"
93 		"OPTIONS:\n"
94 		"-h this help\n",
95 		cmd, cmd);
96 }
97 
98 /*
99  * Function prefix2mask is from ipcalc project, ipcalc.c.
100  */
prefix2mask(unsigned int prefix)101 static struct in_addr prefix2mask(unsigned int prefix)
102 {
103 	struct in_addr mask;
104 
105 	memset(&mask, 0x0, sizeof(mask));
106 
107 	if (prefix)
108 		mask.s_addr = htonl(~((1 << (32 - prefix)) - 1));
109 	else
110 		mask.s_addr = htonl(0);
111 
112 	return mask;
113 }
114 
115 /*
116  * Function calc_network is based on ipcalc project,
117  * calc_network/ipcalc.c.
118  */
calc_network(const struct in_addr * ip,struct in_addr * mask)119 static struct in_addr calc_network(const struct in_addr *ip,
120 	struct in_addr *mask)
121 {
122 	struct in_addr network;
123 
124 	memset(&network, 0, sizeof(network));
125 
126 	network.s_addr = ip->s_addr & mask->s_addr;
127 	return network;
128 }
129 
is_in_subnet_ipv4(const struct in_addr * network,const struct in_addr * mask,const struct in_addr * ip)130 static int is_in_subnet_ipv4(const struct in_addr *network,
131 	const struct in_addr *mask, const struct in_addr *ip)
132 {
133 	return (ip->s_addr & mask->s_addr) ==
134 		(network->s_addr & mask->s_addr);
135 }
136 
is_in_subnet_ipv6(const struct in6_addr * network,const struct in6_addr * mask,const struct in6_addr * ip6)137 static int is_in_subnet_ipv6(const struct in6_addr *network,
138 	const struct in6_addr *mask, const struct in6_addr *ip6)
139 {
140 	unsigned int i;
141 
142 	for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++) {
143 		if (((((int *) ip6)[i] & ((int *) mask)[i])) !=
144 			(((int *) network)[i] & ((int *) mask)[i]))
145 			return 0;
146 	}
147 	return 1;
148 }
149 
150 /*
151  * Function get_ipv4_netmask uses code from calc_network, which is from
152  * ipcalc project, ipcalc.c.
153  */
get_ipv4_netmask(unsigned int prefix)154 static char *get_ipv4_netmask(unsigned int prefix)
155 {
156 	char buf[INET_ADDRSTRLEN + 1];
157 	struct in_addr mask = prefix2mask(prefix);
158 
159 	if (prefix > MAX_IPV4_PREFIX)
160 		return NULL;
161 
162 	if (!inet_ntop(AF_INET, &mask, buf, sizeof(buf)))
163 		tst_brk_comment("error calculating IPv4 address");
164 
165 	return strdup(buf);
166 }
167 
168 /*
169  * Function get_ipv4_netmask uses code from ipv6_prefix_to_mask and
170  * ipv6_mask_to_str, which are from ipcalc project, ipcalc.c.
171  */
get_ipv6_netmask(unsigned int prefix)172 static char *get_ipv6_netmask(unsigned int prefix)
173 {
174 	struct in6_addr in6;
175 	char buf[128];
176 	int i, j;
177 
178 	if (prefix > MAX_IPV6_PREFIX)
179 		return NULL;
180 
181 	memset(&in6, 0x0, sizeof(in6));
182 	for (i = prefix, j = 0; i > 0; i -= 8, j++) {
183 		if (i >= 8)
184 			in6.s6_addr[j] = 0xff;
185 		else
186 			in6.s6_addr[j] = (unsigned long)(0xffU << (8 - i));
187 	}
188 
189 	if (!inet_ntop(AF_INET6, &in6, buf, sizeof(buf)))
190 		tst_brk_comment("error calculating IPv6 address");
191 
192 	return strdup(buf);
193 }
194 
195 /*
196  * Function get_ipv4_broadcast uses code from calc_broadcast, which is from
197  * ipcalc project, ipcalc.c.
198  */
get_ipv4_broadcast(struct in_addr ip,unsigned int prefix)199 static char *get_ipv4_broadcast(struct in_addr ip, unsigned int prefix)
200 {
201 	struct in_addr mask = prefix2mask(prefix);
202 	struct in_addr broadcast;
203 	char buf[INET_ADDRSTRLEN + 1];
204 
205 	memset(&broadcast, 0, sizeof(broadcast));
206 	broadcast.s_addr = (ip.s_addr & mask.s_addr) | ~mask.s_addr;
207 
208 	if (!inet_ntop(AF_INET, &broadcast, buf, sizeof(buf)))
209 		tst_brk_comment("error calculating IPv4 address");
210 
211 	return strdup(buf);
212 }
213 
214 /*
215  * For unused network we use
216  * DEFAULT_IPV4_UNUSED_PART1:DEFAULT_IPV4_UNUSED_PART2 or
217  * {DEFAULT_IPV4_UNUSED_PART1}.XY, when there is a collision with IP.
218  */
get_ipv4_net16_unused(const struct in_addr * ip,unsigned int prefix)219 static char *get_ipv4_net16_unused(const struct in_addr *ip,
220 	unsigned int prefix)
221 {
222 	struct in_addr mask, network;
223 	char buf[128], net_unused[128];
224 
225 	mask = prefix2mask(prefix);
226 	network = calc_network(ip, &mask);
227 
228 	sprintf(net_unused, "%d.%d", DEFAULT_IPV4_UNUSED_PART1,
229 			DEFAULT_IPV4_UNUSED_PART2);
230 	sprintf(buf, "%s.0.0", net_unused);
231 
232 	get_in_addr(buf, &network);
233 
234 	if (!is_in_subnet_ipv4(ip, &mask, &network))
235 		return strdup(net_unused);
236 
237 	srand(time(NULL));
238 
239 	/* try to randomize second group */
240 	sprintf(net_unused, "%d.%d", DEFAULT_IPV4_UNUSED_PART1,
241 		(rand() % 128) + (((ip->s_addr >> 8) & 0xff) < 128 ? 128 : 0));
242 	sprintf(buf, "%s.0.0", net_unused);
243 
244 	get_in_addr(buf, &network);
245 
246 	if (!is_in_subnet_ipv4(ip, &mask, &network))
247 		return strdup(net_unused);
248 
249 	/* try to randomize first group */
250 	sprintf(net_unused, "%d.%d", (rand() % 128) + (((ip->s_addr) & 0xff)
251 			< 128 ? 128 : 0), DEFAULT_IPV4_UNUSED_PART2);
252 	sprintf(buf, "%s.0.0", net_unused);
253 
254 	get_in_addr(buf, &network);
255 
256 	if (!is_in_subnet_ipv4(ip, &mask, &network))
257 		return strdup(net_unused);
258 
259 	return NULL;
260 }
261 
262 /*
263  * Function get_ipv6_net32_unused is inspired by ipcalc project,
264  * get_ipv6_info/ipcalc.c.
265  *
266  * For unused network we use DEFAULT_IPV6_UNUSED_PART1:DEFAULT_IPV6_UNUSED_PART2
267  * if no collision with existing IP range.
268  * Otherwise we try to use
269  * {DEFAULT_IPV6_UNUSED_PART1}XY:DEFAULT_IPV6_UNUSED_PART2 or
270  * XY:DEFAULT_IPV6_UNUSED_PART2.
271  */
get_ipv6_net32_unused(const struct in6_addr * ip6,unsigned int prefix)272 static char *get_ipv6_net32_unused(const struct in6_addr *ip6,
273 	unsigned int prefix)
274 {
275 	int i, j;
276 	struct in6_addr mask, network;
277 	char buf[128], net_unused[128];
278 
279 	memset(&mask, 0x0, sizeof(mask));
280 
281 	if (prefix > 128)
282 		return NULL;
283 
284 
285 	for (i = prefix, j = 0; i > 0; i -= 8, j++) {
286 		if (i >= 8)
287 			mask.s6_addr[j] = 0xff;
288 		else
289 			mask.s6_addr[j] = (unsigned long)(0xffU << (8 - i));
290 	}
291 
292 	sprintf(net_unused, "%x:%x", 256 * DEFAULT_IPV6_UNUSED_PART1,
293 			DEFAULT_IPV6_UNUSED_PART2);
294 	sprintf(buf, "%s::", net_unused);
295 
296 	get_in6_addr(buf, &network);
297 
298 	if (!is_in_subnet_ipv6(ip6, &mask, &network))
299 		return strdup(net_unused);
300 
301 	srand(time(NULL));
302 
303 	/* try to randomize second group */
304 	sprintf(net_unused, "%x:%x", 256 * DEFAULT_IPV6_UNUSED_PART1 +
305 			(rand() % 128) + (ip6->s6_addr[1] < 128 ? 128 : 0),
306 			DEFAULT_IPV6_UNUSED_PART2);
307 	sprintf(buf, "%s::", net_unused);
308 
309 	get_in6_addr(buf, &network);
310 
311 	if (!is_in_subnet_ipv6(ip6, &mask, &network))
312 		return strdup(net_unused);
313 
314 	/* try to randomize first group */
315 	sprintf(net_unused, "%x:%x",
316 			256 * (rand() % 128) + (256 * ip6->s6_addr[0] < 128 ?
317 			128 : 0), DEFAULT_IPV6_UNUSED_PART2);
318 	sprintf(buf, "%s::", net_unused);
319 
320 	get_in6_addr(buf, &network);
321 
322 	if (!is_in_subnet_ipv6(ip6, &mask, &network))
323 		return strdup(net_unused);
324 
325 	return NULL;
326 }
327 
328 /*
329  * Function get_ipv6_network is based on musl libc project,
330  * inet_ntop/inet_ntop.c.
331  */
get_ipv6_network(const unsigned char * a0,unsigned int prefix)332 static char *get_ipv6_network(const unsigned char *a0, unsigned int prefix)
333 {
334 	const unsigned char *a = a0;
335 	unsigned int i, j, max, best, border = 0;
336 	char buf[100];
337 	char ret[100];
338 	char tmp[100];
339 	char *p_ret = ret;
340 	char *p_tmp = tmp;
341 	size_t offset;
342 
343 	if (prefix > MAX_IPV6_PREFIX)
344 		return NULL;
345 
346 	if (prefix == MAX_IPV6_PREFIX)
347 		return strdup("\0");
348 
349 	snprintf(buf, sizeof(buf),
350 		"%x:%x:%x:%x:%x:%x:%x:%x",
351 		256 * a[0] + a[1], 256 * a[2] + a[3],
352 		256 * a[4] + a[5], 256 * a[6] + a[7],
353 		256 * a[8] + a[9], 256 * a[10] + a[11],
354 		256 * a[12] + a[13], 256 * a[14] + a[15]);
355 
356 	for (i = 0; i < 8; i++) {
357 		if (i < prefix >> 4) {
358 			border += sprintf(p_tmp, "%x", 256 * a[2 * i] +
359 				a[2 * i + 1]);
360 			if (i > 0)
361 				border++;
362 		}
363 
364 		if (i >= prefix >> 4)
365 			break;
366 
367 		/* ':' only if no leading in host or ending in net */
368 		if (i > 0)
369 			*p_ret++ = ':';
370 
371 		offset = sprintf(p_ret, "%x", 256 * a[2 * i] + a[2 * i + 1]);
372 		p_ret += offset;
373 	}
374 
375 	*p_ret = '\0';
376 
377 	/* Find longest /(^0|:)[:0]{2,}/ */
378 	for (i = best = 0, max = 2; buf[i]; i++) {
379 		if (i && buf[i] != ':')
380 			continue;
381 		j = strspn(buf + i, ":0");
382 
383 		if (j > max)
384 			best = i, max = j;
385 	}
386 
387 	size_t length = strlen(ret);
388 	size_t best_end = best + max - 1;
389 
390 	if (max > 2 && best < border) {
391 		p_ret = ret;
392 		/* Replace longest /(^0|:)[:0]{2,}/ with "::" */
393 		if (best == 0 && best_end >= border) {
394 			/* zeros in whole net part or continue to host */
395 			ret[0] = ':';
396 			ret[1] = '\0';
397 		} else if (best == 0 && best_end < border) {
398 			/* zeros on beginning, not whole part */
399 			ret[0] = ':';
400 			memmove(p_ret + 1, p_ret + best_end, border - best_end
401 				+ 1);
402 		} else if (best > 0 && best_end >= border) {
403 			/*
404 			 * zeros not from beginning to border or continue to
405 			 * host
406 			 */
407 			ret[best] = ':';
408 			ret[best + 1] = '\0';
409 		} else {
410 			/* zeros somewhere in the middle */
411 			ret[best] = ':';
412 			memmove(p_ret + best + 1, p_ret + best_end,
413 					border - best + 1);
414 		}
415 	}
416 
417 	if (length < INET6_ADDRSTRLEN)
418 		return strdup(ret);
419 
420 	return NULL;
421 }
422 
423 /*
424  * Strip host part from ip address.
425  */
get_host_from_ip(const char * ip,const char * net)426 static char *get_host_from_ip(const char *ip, const char *net)
427 {
428 	if (ip == NULL || net == NULL)
429 		return NULL;
430 
431 	char *result = strstr(ip, net);
432 
433 	if (!result || result != ip)
434 		return NULL;
435 
436 	char *buf = strdup(ip);
437 	unsigned int index = strlen(net);
438 	int len;
439 
440 	/* prefix < 8 (IPv4) or 128 (IPv6) */
441 	if (index == strlen(ip))
442 		return strdup("\0");
443 
444 	/* prefix > 0 && prefix < 32 (IPv4) or 128 (IPv6) */
445 	if (index > 0 && index < strlen(ip)) {
446 		len = strlen(ip) - index - 1;
447 		assert(ip[index] == ':' || ip[index] == '.');
448 		memmove(buf, buf + index + 1, len);
449 		buf[len] = '\0';
450 	}
451 
452 	return buf;
453 }
454 
check_prefix_range(unsigned int prefix,int is_ipv6,int is_lhost)455 static void check_prefix_range(unsigned int prefix, int is_ipv6, int is_lhost)
456 {
457 	unsigned int base_prefix = is_ipv6 ? BASE_IPV6_PREFIX :
458 		BASE_IPV4_PREFIX;
459 	unsigned int max_prefix = is_ipv6 ? MAX_IPV6_PREFIX : MAX_IPV4_PREFIX;
460 
461 	if (prefix < base_prefix || (is_ipv6 && prefix == 128) ||
462 		(!is_ipv6 && prefix == 32))
463 		tst_res_comment(TWARN,
464 			"prefix %d for %s will be unsuitable for some stress tests which need %s variable. To avoid this use prefix >= %d and prefix < %d.",
465 			prefix, is_ipv6 ?  "IPv6" : "IPv4",
466 			is_ipv6 ?
467 				(is_lhost ? "IPV6_LNETWORK" : "IPV6_RNETWORK") :
468 				(is_lhost ? "IPV4_LNETWORK" : "IPV4_RNETWORK"),
469 			base_prefix, max_prefix);
470 }
471 
get_ipv4_network(int ip,unsigned int prefix)472 static char *get_ipv4_network(int ip, unsigned int prefix)
473 {
474 	char buf[INET_ADDRSTRLEN + 1];
475 	char *p_buf = buf;
476 	unsigned char byte;
477 	unsigned int i;
478 
479 	if (prefix > MAX_IPV4_PREFIX)
480 		return NULL;
481 
482 	if (prefix == MAX_IPV4_PREFIX)
483 		return strdup("\0");
484 
485 	prefix &= 0x18;
486 
487 	for (i = 0; i < MAX_IPV4_PREFIX && (prefix == 0 || i < prefix);
488 	     i += 8) {
489 		if (i == 0) {
490 			byte = ip & 0xff;
491 			sprintf(p_buf, "%d", byte);
492 		} else {
493 			byte = (ip >> i) & 0xff;
494 			sprintf(p_buf, ".%d", byte);
495 		}
496 		p_buf += strlen(p_buf);
497 	}
498 
499 	return strdup(buf);
500 }
501 
502 /*
503  * Round down prefix.
504  */
round_down_prefix(unsigned int prefix,int is_ipv6)505 static int round_down_prefix(unsigned int prefix, int is_ipv6)
506 {
507 	unsigned int base_prefix = is_ipv6 ? BASE_IPV6_PREFIX :
508 		BASE_IPV4_PREFIX;
509 
510 	return prefix / base_prefix * base_prefix;
511 }
512 
get_ipv4_info(const char * lip_str,const char * rip_str,int lprefix,int rprefix)513 static void get_ipv4_info(const char *lip_str, const char *rip_str, int lprefix,
514 	int rprefix)
515 {
516 	struct in_addr lip, rip;
517 	int lprefix_round, rprefix_round;
518 
519 	lprefix_round = round_down_prefix(lprefix, 0);
520 	rprefix_round = round_down_prefix(rprefix, 0);
521 
522 	get_in_addr(lip_str, &lip);
523 	get_in_addr(rip_str, &rip);
524 
525 	vars.ipv4_lbroadcast = get_ipv4_broadcast(lip, lprefix);
526 	vars.ipv4_rbroadcast = get_ipv4_broadcast(rip, rprefix);
527 
528 	vars.ipv4_lnetmask = get_ipv4_netmask(lprefix);
529 	vars.ipv4_rnetmask = get_ipv4_netmask(rprefix);
530 
531 	vars.ipv4_lnetwork = get_ipv4_network(lip.s_addr, lprefix_round);
532 	vars.ipv4_rnetwork = get_ipv4_network(rip.s_addr, rprefix_round);
533 
534 	vars.lhost_ipv4_host = get_host_from_ip(lip_str, vars.ipv4_lnetwork);
535 	vars.rhost_ipv4_host = get_host_from_ip(rip_str, vars.ipv4_rnetwork);
536 
537 	vars.ipv4_net16_unused = get_ipv4_net16_unused(&lip, lprefix_round);
538 }
539 
get_ipv6_info(const char * lip_str,const char * rip_str,int lprefix,int rprefix)540 static void get_ipv6_info(const char *lip_str, const char *rip_str,
541 	int lprefix, int rprefix)
542 {
543 	struct in6_addr lip, rip;
544 	int lprefix_round, rprefix_round;
545 
546 	lprefix_round = round_down_prefix(lprefix, 1);
547 	rprefix_round = round_down_prefix(rprefix, 1);
548 
549 	get_in6_addr(lip_str, &lip);
550 	get_in6_addr(rip_str, &rip);
551 
552 	vars.ipv6_lnetmask = get_ipv6_netmask(lprefix);
553 	vars.ipv6_rnetmask = get_ipv6_netmask(rprefix);
554 
555 	vars.ipv6_lnetwork = get_ipv6_network(lip.s6_addr, lprefix_round);
556 	vars.ipv6_rnetwork = get_ipv6_network(rip.s6_addr, rprefix_round);
557 
558 	vars.lhost_ipv6_host = get_host_from_ip(lip_str, vars.ipv6_lnetwork);
559 	vars.rhost_ipv6_host = get_host_from_ip(rip_str, vars.ipv6_rnetwork);
560 
561 	vars.ipv6_net32_unused = get_ipv6_net32_unused(&lip, lprefix_round);
562 }
563 
print_vars(int is_ipv6)564 static void print_vars(int is_ipv6)
565 {
566 	if (is_ipv6) {
567 		print_svar("IPV6_LNETMASK", vars.ipv6_lnetmask);
568 		print_svar_change("IPV6_RNETMASK", vars.ipv6_rnetmask);
569 		print_svar("IPV6_LNETWORK", vars.ipv6_lnetwork);
570 		print_svar("IPV6_RNETWORK", vars.ipv6_rnetwork);
571 		print_svar("LHOST_IPV6_HOST", vars.lhost_ipv6_host);
572 		print_svar("RHOST_IPV6_HOST", vars.rhost_ipv6_host);
573 		print_svar("IPV6_NET32_UNUSED", vars.ipv6_net32_unused);
574 	} else {
575 		print_svar("IPV4_LBROADCAST", vars.ipv4_lbroadcast);
576 		print_svar_change("IPV4_RBROADCAST", vars.ipv4_rbroadcast);
577 		print_svar("IPV4_LNETMASK", vars.ipv4_lnetmask);
578 		print_svar_change("IPV4_RNETMASK", vars.ipv4_rnetmask);
579 		print_svar("IPV4_LNETWORK", vars.ipv4_lnetwork);
580 		print_svar("IPV4_RNETWORK", vars.ipv4_rnetwork);
581 		print_svar("LHOST_IPV4_HOST", vars.lhost_ipv4_host);
582 		print_svar("RHOST_IPV4_HOST", vars.rhost_ipv4_host);
583 		print_svar("IPV4_NET16_UNUSED", vars.ipv4_net16_unused);
584 	}
585 }
586 
main(int argc,char * argv[])587 int main(int argc, char *argv[])
588 {
589 	char *lip_str = NULL, *rip_str = NULL;
590 	int is_ipv6, lprefix, rprefix, tmp;
591 	struct in_addr ip;
592 	struct in6_addr ip6;
593 
594 	int is_usage = argc > 1 && (!strcmp(argv[1], "-h") ||
595 		!strcmp(argv[1], "--help"));
596 	if (argc < 3 || is_usage) {
597 		usage(argv[0]);
598 		exit(is_usage ? EXIT_SUCCESS : EXIT_FAILURE);
599 	}
600 
601 	lip_str = argv[1];
602 	rip_str = argv[2];
603 
604 	is_ipv6 = !!strchr(lip_str, ':');
605 	lprefix = get_prefix(lip_str, is_ipv6);
606 	rprefix = get_prefix(rip_str, is_ipv6);
607 
608 	if (is_ipv6)
609 		get_in6_addr(lip_str, &ip6);
610 	else
611 		get_in_addr(lip_str, &ip);
612 
613 	tmp = !!strchr(rip_str, ':');
614 	if (tmp)
615 		get_in6_addr(rip_str, &ip6);
616 	else
617 		get_in_addr(rip_str, &ip);
618 
619 	if (is_ipv6 != tmp)
620 		tst_brk_comment("mixed IPv4 and IPv6 addresses ('%s', '%s')",
621 				lip_str, rip_str);
622 
623 	check_prefix_range(lprefix, is_ipv6, 1);
624 	check_prefix_range(rprefix, is_ipv6, 0);
625 
626 	if (!strcmp(lip_str, rip_str))
627 		tst_brk_comment("IP addresses cannot be the same ('%s', '%s')",
628 				lip_str, rip_str);
629 
630 	if (is_ipv6)
631 		get_ipv6_info(lip_str, rip_str, lprefix, rprefix);
632 	else
633 		get_ipv4_info(lip_str, rip_str, lprefix, rprefix);
634 
635 	print_vars(is_ipv6);
636 
637 	exit(EXIT_SUCCESS);
638 }
639