• 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 <stdio.h>
19 #include <stdlib.h>
20 
21 #define TST_NO_DEFAULT_MAIN
22 #include "tst_test.h"
23 
24 #include "tst_net.h"
25 
26 #define DEFAULT_IPV4_PREFIX 24
27 #define DEFAULT_IPV6_PREFIX 64
28 
usage(const char * cmd)29 static void usage(const char *cmd)
30 {
31 	fprintf(stderr, "USAGE:\n"
32 		"%s IP_LHOST[/PREFIX]\n"
33 		"%s -r IP_RHOST[/PREFIX]\n"
34 		"%s -h\n\n"
35 		"Set IP variables without prefix and prefix for given IP.\n"
36 		"EXPORTED VARIABLES:\n"
37 		"Export one of the following variables without /prefix:\n"
38 		"IPV4_LHOST: IPv4 address of the local host\n"
39 		"IPV4_RHOST: IPv4 address of the remote host\n"
40 		"IPV6_LHOST: IPv6 address of the local host\n"
41 		"IPV6_RHOST: IPv6 address of the remote host\n"
42 		"Export one of the following variables:\n"
43 		"IPV4_LPREFIX: IPv4 prefix for IPV4_LNETWORK\n"
44 		"IPV4_RPREFIX: IPv4 prefix for IPV4_RNETWORK\n"
45 		"IPV6_LPREFIX: IPv6 prefix for IPV6_LNETWORK\n"
46 		"IPV6_RPREFIX: IPv6 prefix for IPV6_RNETWORK\n"
47 		"Default IPv4 prefix: %d.\n"
48 		"Default IPv6 prefix: %d.\n\n"
49 		"PARAMS:\n"
50 		"-h this help\n"
51 		"-r export remote environment variables\n",
52 		cmd, cmd, cmd, DEFAULT_IPV4_PREFIX, DEFAULT_IPV6_PREFIX);
53 }
54 
print_ivar(const char * name,unsigned int val)55 static void print_ivar(const char *name, unsigned int val)
56 {
57 	printf("export %s=%d\n", name, val);
58 }
59 
main(int argc,char * argv[])60 int main(int argc, char *argv[])
61 {
62 	char *ip_str = NULL, *prefix_str = NULL;
63 	int is_ipv6, is_rhost = 0, prefix;
64 	struct in_addr ip;
65 	struct in6_addr ip6;
66 
67 	int is_usage = argc > 1 && (!strcmp(argv[1], "-h") ||
68 		!strcmp(argv[1], "--help"));
69 	if (argc < 2 || is_usage) {
70 		usage(argv[0]);
71 		exit(is_usage ? EXIT_SUCCESS : EXIT_FAILURE);
72 	}
73 	if (!strcmp(argv[1], "-r"))
74 		is_rhost = 1;
75 
76 	ip_str = argv[is_rhost ? 2 : 1];
77 	is_ipv6 = !!strchr(ip_str, ':');
78 
79 	prefix_str = strchr(ip_str, '/');
80 
81 	if (prefix_str)
82 		prefix = get_prefix(ip_str, is_ipv6);
83 	else
84 		prefix = is_ipv6 ? DEFAULT_IPV6_PREFIX : DEFAULT_IPV4_PREFIX;
85 
86 	/* checks for validity of IP string */
87 	if (is_ipv6)
88 		get_in6_addr(ip_str, &ip6);
89 	else
90 		get_in_addr(ip_str, &ip);
91 
92 	if (is_ipv6) {
93 		print_ivar(is_rhost ? "IPV6_RPREFIX" : "IPV6_LPREFIX", prefix);
94 		print_svar(is_rhost ? "IPV6_RHOST" : "IPV6_LHOST", ip_str);
95 	} else {
96 		print_ivar(is_rhost ? "IPV4_RPREFIX" : "IPV4_LPREFIX", prefix);
97 		print_svar(is_rhost ? "IPV4_RHOST" : "IPV4_LHOST", ip_str);
98 	}
99 
100 	exit(EXIT_SUCCESS);
101 }
102