-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtst_net.h
153 lines (125 loc) · 3.24 KB
/
tst_net.h
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (c) 2017 Petr Vorel <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <arpa/inet.h>
#include <errno.h>
#define MAX_IPV4_PREFIX 32
#define MAX_IPV6_PREFIX 128
#define tst_res_comment(...) { \
fprintf(stderr, "# "); \
tst_res(__VA_ARGS__); } \
#define tst_brk_comment(...) { \
fprintf(stderr, "# "); \
tst_brk(TCONF, __VA_ARGS__); } \
static inline void print_svar(const char *name, const char *val)
{
if (name && val)
printf("export %s=\"%s\"\n", name, val);
}
static inline void print_svar_change(const char *name, const char *val)
{
if (name && val)
printf("export %s=\"${%s:-%s}\"\n", name, name, val);
}
/*
* Function bit_count is from ipcalc project, ipcalc.c.
*/
static int bit_count(uint32_t i)
{
int c = 0;
unsigned int seen_one = 0;
while (i > 0) {
if (i & 1) {
seen_one = 1;
c++;
} else {
if (seen_one)
return -1;
}
i >>= 1;
}
return c;
}
/*
* Function mask2prefix is from ipcalc project, ipcalc.c.
*/
static int mask2prefix(struct in_addr mask)
{
return bit_count(ntohl(mask.s_addr));
}
/*
* Function ipv4_mask_to_int is from ipcalc project, ipcalc.c.
*/
static int ipv4_mask_to_int(const char *prefix)
{
int ret;
struct in_addr in;
ret = inet_pton(AF_INET, prefix, &in);
if (ret == 0)
return -1;
return mask2prefix(in);
}
/*
* Function safe_atoi is from ipcalc project, ipcalc.c.
*/
static int safe_atoi(const char *s, int *ret_i)
{
char *x = NULL;
long l;
errno = 0;
l = strtol(s, &x, 0);
if (!x || x == s || *x || errno)
return errno > 0 ? -errno : -EINVAL;
if ((long)(int)l != l)
return -ERANGE;
*ret_i = (int)l;
return 0;
}
/*
* Function get_prefix use code from ipcalc project, str_to_prefix/ipcalc.c.
*/
static int get_prefix(const char *ip_str, int is_ipv6)
{
char *prefix_str = NULL;
int prefix = -1, r;
prefix_str = strchr(ip_str, '/');
if (!prefix_str)
return -1;
*(prefix_str++) = '\0';
if (!is_ipv6 && strchr(prefix_str, '.'))
prefix = ipv4_mask_to_int(prefix_str);
else {
r = safe_atoi(prefix_str, &prefix);
if (r != 0)
tst_brk_comment("conversion error: '%s' is not integer",
prefix_str);
}
if (prefix < 0 || ((is_ipv6 && prefix > MAX_IPV6_PREFIX) ||
(!is_ipv6 && prefix > MAX_IPV4_PREFIX)))
tst_brk_comment("bad %s prefix: %s", is_ipv6 ? "IPv6" : "IPv4",
prefix_str);
return prefix;
}
static void get_in_addr(const char *ip_str, struct in_addr *ip)
{
if (inet_pton(AF_INET, ip_str, ip) <= 0)
tst_brk_comment("bad IPv4 address: '%s'", ip_str);
}
static void get_in6_addr(const char *ip_str, struct in6_addr *ip6)
{
if (inet_pton(AF_INET6, ip_str, ip6) <= 0)
tst_brk_comment("bad IPv6 address: '%s'", ip_str);
}