1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2017-2019 Petr Vorel <pvorel@suse.cz>
4 * Copyright (c) 2019 Martin Doucha <mdoucha@suse.cz>
5 */
6
7 #include <errno.h>
8 #include <netdb.h>
9 #include <string.h>
10 #include <stdlib.h>
11
12 #define TST_NO_DEFAULT_MAIN
13 #include "tst_test.h"
14 #include "tst_net.h"
15 #include "tst_private.h"
16
tst_print_svar(const char * name,const char * val)17 void tst_print_svar(const char *name, const char *val)
18 {
19 if (name && val)
20 printf("export %s=\"%s\"\n", name, val);
21 }
22
tst_print_svar_change(const char * name,const char * val)23 void tst_print_svar_change(const char *name, const char *val)
24 {
25 if (name && val)
26 printf("export %s=\"${%s:-%s}\"\n", name, name, val);
27 }
28
29 /*
30 * Function bit_count is from ipcalc project, ipcalc.c.
31 */
tst_bit_count(uint32_t i)32 static int tst_bit_count(uint32_t i)
33 {
34 int c = 0;
35 unsigned int seen_one = 0;
36
37 while (i > 0) {
38 if (i & 1) {
39 seen_one = 1;
40 c++;
41 } else {
42 if (seen_one)
43 return -1;
44 }
45 i >>= 1;
46 }
47
48 return c;
49 }
50
51 /*
52 * Function mask2prefix is from ipcalc project, ipcalc.c.
53 */
tst_mask2prefix(struct in_addr mask)54 static int tst_mask2prefix(struct in_addr mask)
55 {
56 return tst_bit_count(ntohl(mask.s_addr));
57 }
58
59 /*
60 * Function ipv4_mask_to_int is from ipcalc project, ipcalc.c.
61 */
tst_ipv4_mask_to_int(const char * prefix)62 static int tst_ipv4_mask_to_int(const char *prefix)
63 {
64 int ret;
65 struct in_addr in;
66
67 ret = inet_pton(AF_INET, prefix, &in);
68 if (ret == 0)
69 return -1;
70
71 return tst_mask2prefix(in);
72 }
73
74 /*
75 * Function safe_atoi is from ipcalc project, ipcalc.c.
76 */
tst_safe_atoi(const char * s,int * ret_i)77 static int tst_safe_atoi(const char *s, int *ret_i)
78 {
79 char *x = NULL;
80 long l;
81
82 errno = 0;
83 l = strtol(s, &x, 0);
84
85 if (!x || x == s || *x || errno)
86 return errno > 0 ? -errno : -EINVAL;
87
88 if ((long)(int)l != l)
89 return -ERANGE;
90
91 *ret_i = (int)l;
92
93 return 0;
94 }
95
96 /*
97 * Function get_prefix use code from ipcalc project, str_to_prefix/ipcalc.c.
98 */
tst_get_prefix(const char * ip_str,int is_ipv6)99 int tst_get_prefix(const char *ip_str, int is_ipv6)
100 {
101 char *prefix_str = NULL;
102 int prefix = -1, r;
103
104 prefix_str = strchr(ip_str, '/');
105 if (!prefix_str)
106 return -1;
107
108 *(prefix_str++) = '\0';
109
110 if (!is_ipv6 && strchr(prefix_str, '.'))
111 prefix = tst_ipv4_mask_to_int(prefix_str);
112 else {
113 r = tst_safe_atoi(prefix_str, &prefix);
114 if (r != 0)
115 tst_brk_comment("conversion error: '%s' is not integer",
116 prefix_str);
117 }
118
119 if (prefix < 0 || ((is_ipv6 && prefix > MAX_IPV6_PREFIX) ||
120 (!is_ipv6 && prefix > MAX_IPV4_PREFIX)))
121 tst_brk_comment("bad %s prefix: %s", is_ipv6 ? "IPv6" : "IPv4",
122 prefix_str);
123
124 return prefix;
125 }
126
tst_get_in_addr(const char * ip_str,struct in_addr * ip)127 void tst_get_in_addr(const char *ip_str, struct in_addr *ip)
128 {
129 if (inet_pton(AF_INET, ip_str, ip) <= 0)
130 tst_brk_comment("bad IPv4 address: '%s'", ip_str);
131 }
132
tst_get_in6_addr(const char * ip_str,struct in6_addr * ip6)133 void tst_get_in6_addr(const char *ip_str, struct in6_addr *ip6)
134 {
135 if (inet_pton(AF_INET6, ip_str, ip6) <= 0)
136 tst_brk_comment("bad IPv6 address: '%s'", ip_str);
137 }
138
tst_get_connect_address(int sock,struct sockaddr_storage * addr)139 socklen_t tst_get_connect_address(int sock, struct sockaddr_storage *addr)
140 {
141 struct sockaddr_in *inet_ptr;
142 struct sockaddr_in6 *inet6_ptr;
143 size_t tmp_size;
144 socklen_t ret = sizeof(*addr);
145
146 SAFE_GETSOCKNAME(sock, (struct sockaddr*)addr, &ret);
147
148 /* Sanitize wildcard addresses */
149 switch (addr->ss_family) {
150 case AF_INET:
151 inet_ptr = (struct sockaddr_in*)addr;
152
153 switch (ntohl(inet_ptr->sin_addr.s_addr)) {
154 case INADDR_ANY:
155 case INADDR_BROADCAST:
156 inet_ptr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
157 break;
158 }
159
160 break;
161
162 case AF_INET6:
163 inet6_ptr = (struct sockaddr_in6*)addr;
164 tmp_size = sizeof(struct in6_addr);
165
166 if (!memcmp(&inet6_ptr->sin6_addr, &in6addr_any, tmp_size)) {
167 memcpy(&inet6_ptr->sin6_addr, &in6addr_loopback,
168 tmp_size);
169 }
170
171 break;
172 }
173
174 return ret;
175 }
176
tst_init_sockaddr_inet(struct sockaddr_in * sa,const char * ip_str,uint16_t port)177 void tst_init_sockaddr_inet(struct sockaddr_in *sa, const char *ip_str, uint16_t port)
178 {
179 memset(sa, 0, sizeof(struct sockaddr_in));
180 sa->sin_family = AF_INET;
181 sa->sin_port = htons(port);
182 tst_get_in_addr(ip_str, &sa->sin_addr);
183 }
184
tst_init_sockaddr_inet_bin(struct sockaddr_in * sa,uint32_t ip_val,uint16_t port)185 void tst_init_sockaddr_inet_bin(struct sockaddr_in *sa, uint32_t ip_val, uint16_t port)
186 {
187 memset(sa, 0, sizeof(struct sockaddr_in));
188 sa->sin_family = AF_INET;
189 sa->sin_port = htons(port);
190 sa->sin_addr.s_addr = htonl(ip_val);
191 }
192
tst_init_sockaddr_inet6(struct sockaddr_in6 * sa,const char * ip_str,uint16_t port)193 void tst_init_sockaddr_inet6(struct sockaddr_in6 *sa, const char *ip_str, uint16_t port)
194 {
195 memset(sa, 0, sizeof(struct sockaddr_in6));
196 sa->sin6_family = AF_INET6;
197 sa->sin6_port = htons(port);
198 tst_get_in6_addr(ip_str, &sa->sin6_addr);
199 }
200
tst_init_sockaddr_inet6_bin(struct sockaddr_in6 * sa,const struct in6_addr * ip_val,uint16_t port)201 void tst_init_sockaddr_inet6_bin(struct sockaddr_in6 *sa, const struct in6_addr *ip_val, uint16_t port)
202 {
203 memset(sa, 0, sizeof(struct sockaddr_in6));
204 sa->sin6_family = AF_INET6;
205 sa->sin6_port = htons(port);
206 memcpy(&sa->sin6_addr, ip_val, sizeof(struct in6_addr));
207 }
208
safe_getaddrinfo(const char * file,const int lineno,const char * src_addr,const char * port,const struct addrinfo * hints,struct addrinfo ** addr_info)209 void safe_getaddrinfo(const char *file, const int lineno, const char *src_addr,
210 const char *port, const struct addrinfo *hints,
211 struct addrinfo **addr_info)
212 {
213 int err = getaddrinfo(src_addr, port, hints, addr_info);
214
215 if (err) {
216 tst_brk_(file, lineno, TBROK, "getaddrinfo failed, %s",
217 gai_strerror(err));
218 }
219
220 if (!*addr_info)
221 tst_brk_(file, lineno, TBROK, "failed to get the address");
222 }
223