]>
Commit | Line | Data |
---|---|---|
c182135d RC |
1 | /*** |
2 | This file is part of systemd | |
3 | ||
4 | Copyright 2014 Ronny Chevalier | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
b5efdb8a | 20 | #include "alloc-util.h" |
07630cea | 21 | #include "async.h" |
3ffd4af2 | 22 | #include "fd-util.h" |
3b653205 | 23 | #include "in-addr-util.h" |
059f6c42 | 24 | #include "log.h" |
07630cea LP |
25 | #include "macro.h" |
26 | #include "socket-util.h" | |
27 | #include "string-util.h" | |
28 | #include "util.h" | |
c182135d | 29 | |
ef76dff2 LP |
30 | static void test_ifname_valid(void) { |
31 | assert(ifname_valid("foo")); | |
32 | assert(ifname_valid("eth0")); | |
33 | ||
34 | assert(!ifname_valid("0")); | |
35 | assert(!ifname_valid("99")); | |
36 | assert(ifname_valid("a99")); | |
37 | assert(ifname_valid("99a")); | |
38 | ||
39 | assert(!ifname_valid(NULL)); | |
40 | assert(!ifname_valid("")); | |
41 | assert(!ifname_valid(" ")); | |
42 | assert(!ifname_valid(" foo")); | |
43 | assert(!ifname_valid("bar\n")); | |
44 | assert(!ifname_valid(".")); | |
45 | assert(!ifname_valid("..")); | |
46 | assert(ifname_valid("foo.bar")); | |
47 | assert(!ifname_valid("x:y")); | |
48 | ||
49 | assert(ifname_valid("xxxxxxxxxxxxxxx")); | |
50 | assert(!ifname_valid("xxxxxxxxxxxxxxxx")); | |
51 | } | |
52 | ||
c182135d RC |
53 | static void test_socket_address_parse(void) { |
54 | SocketAddress a; | |
55 | ||
56 | assert_se(socket_address_parse(&a, "junk") < 0); | |
57 | assert_se(socket_address_parse(&a, "192.168.1.1") < 0); | |
58 | assert_se(socket_address_parse(&a, ".168.1.1") < 0); | |
59 | assert_se(socket_address_parse(&a, "989.168.1.1") < 0); | |
60 | assert_se(socket_address_parse(&a, "192.168.1.1:65536") < 0); | |
61 | assert_se(socket_address_parse(&a, "192.168.1.1:0") < 0); | |
62 | assert_se(socket_address_parse(&a, "0") < 0); | |
63 | assert_se(socket_address_parse(&a, "65536") < 0); | |
64 | ||
65 | assert_se(socket_address_parse(&a, "65535") >= 0); | |
66 | ||
4ebc62ec MB |
67 | /* The checks below will pass even if ipv6 is disabled in |
68 | * kernel. The underlying glibc's inet_pton() is just a string | |
69 | * parser and doesn't make any syscalls. */ | |
70 | ||
71 | assert_se(socket_address_parse(&a, "[::1]") < 0); | |
72 | assert_se(socket_address_parse(&a, "[::1]8888") < 0); | |
73 | assert_se(socket_address_parse(&a, "::1") < 0); | |
74 | assert_se(socket_address_parse(&a, "[::1]:0") < 0); | |
75 | assert_se(socket_address_parse(&a, "[::1]:65536") < 0); | |
76 | assert_se(socket_address_parse(&a, "[a:b:1]:8888") < 0); | |
77 | ||
78 | assert_se(socket_address_parse(&a, "8888") >= 0); | |
79 | assert_se(a.sockaddr.sa.sa_family == (socket_ipv6_is_supported() ? AF_INET6 : AF_INET)); | |
80 | ||
81 | assert_se(socket_address_parse(&a, "[2001:0db8:0000:85a3:0000:0000:ac1f:8001]:8888") >= 0); | |
82 | assert_se(a.sockaddr.sa.sa_family == AF_INET6); | |
83 | ||
84 | assert_se(socket_address_parse(&a, "[::1]:8888") >= 0); | |
85 | assert_se(a.sockaddr.sa.sa_family == AF_INET6); | |
c182135d RC |
86 | |
87 | assert_se(socket_address_parse(&a, "192.168.1.254:8888") >= 0); | |
88 | assert_se(a.sockaddr.sa.sa_family == AF_INET); | |
89 | ||
90 | assert_se(socket_address_parse(&a, "/foo/bar") >= 0); | |
91 | assert_se(a.sockaddr.sa.sa_family == AF_UNIX); | |
92 | ||
93 | assert_se(socket_address_parse(&a, "@abstract") >= 0); | |
94 | assert_se(a.sockaddr.sa.sa_family == AF_UNIX); | |
95 | } | |
96 | ||
97 | static void test_socket_address_parse_netlink(void) { | |
98 | SocketAddress a; | |
99 | ||
100 | assert_se(socket_address_parse_netlink(&a, "junk") < 0); | |
101 | assert_se(socket_address_parse_netlink(&a, "") < 0); | |
102 | ||
103 | assert_se(socket_address_parse_netlink(&a, "route") >= 0); | |
104 | assert_se(socket_address_parse_netlink(&a, "route 10") >= 0); | |
105 | assert_se(a.sockaddr.sa.sa_family == AF_NETLINK); | |
106 | assert_se(a.protocol == NETLINK_ROUTE); | |
107 | } | |
108 | ||
109 | static void test_socket_address_equal(void) { | |
110 | SocketAddress a; | |
111 | SocketAddress b; | |
112 | ||
113 | assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0); | |
114 | assert_se(socket_address_parse(&b, "192.168.1.1:888") >= 0); | |
115 | assert_se(!socket_address_equal(&a, &b)); | |
116 | ||
117 | assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0); | |
118 | assert_se(socket_address_parse(&b, "192.16.1.1:8888") >= 0); | |
119 | assert_se(!socket_address_equal(&a, &b)); | |
120 | ||
121 | assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0); | |
122 | assert_se(socket_address_parse(&b, "8888") >= 0); | |
123 | assert_se(!socket_address_equal(&a, &b)); | |
124 | ||
125 | assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0); | |
126 | assert_se(socket_address_parse(&b, "/foo/bar/") >= 0); | |
127 | assert_se(!socket_address_equal(&a, &b)); | |
128 | ||
129 | assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0); | |
130 | assert_se(socket_address_parse(&b, "192.168.1.1:8888") >= 0); | |
131 | assert_se(socket_address_equal(&a, &b)); | |
132 | ||
133 | assert_se(socket_address_parse(&a, "/foo/bar") >= 0); | |
134 | assert_se(socket_address_parse(&b, "/foo/bar") >= 0); | |
135 | assert_se(socket_address_equal(&a, &b)); | |
136 | ||
137 | assert_se(socket_address_parse(&a, "[::1]:8888") >= 0); | |
138 | assert_se(socket_address_parse(&b, "[::1]:8888") >= 0); | |
139 | assert_se(socket_address_equal(&a, &b)); | |
140 | ||
141 | assert_se(socket_address_parse(&a, "@abstract") >= 0); | |
142 | assert_se(socket_address_parse(&b, "@abstract") >= 0); | |
143 | assert_se(socket_address_equal(&a, &b)); | |
144 | ||
145 | assert_se(socket_address_parse_netlink(&a, "firewall") >= 0); | |
146 | assert_se(socket_address_parse_netlink(&b, "firewall") >= 0); | |
147 | assert_se(socket_address_equal(&a, &b)); | |
148 | } | |
149 | ||
150 | static void test_socket_address_get_path(void) { | |
151 | SocketAddress a; | |
152 | ||
153 | assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0); | |
154 | assert_se(!socket_address_get_path(&a)); | |
155 | ||
156 | assert_se(socket_address_parse(&a, "@abstract") >= 0); | |
157 | assert_se(!socket_address_get_path(&a)); | |
158 | ||
159 | assert_se(socket_address_parse(&a, "[::1]:8888") >= 0); | |
160 | assert_se(!socket_address_get_path(&a)); | |
161 | ||
162 | assert_se(socket_address_parse(&a, "/foo/bar") >= 0); | |
163 | assert_se(streq(socket_address_get_path(&a), "/foo/bar")); | |
164 | } | |
165 | ||
43dc0043 RC |
166 | static void test_socket_address_is(void) { |
167 | SocketAddress a; | |
168 | ||
169 | assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0); | |
170 | assert_se(socket_address_is(&a, "192.168.1.1:8888", SOCK_STREAM)); | |
171 | assert_se(!socket_address_is(&a, "route", SOCK_STREAM)); | |
172 | assert_se(!socket_address_is(&a, "192.168.1.1:8888", SOCK_RAW)); | |
173 | } | |
174 | ||
175 | static void test_socket_address_is_netlink(void) { | |
176 | SocketAddress a; | |
177 | ||
178 | assert_se(socket_address_parse_netlink(&a, "route 10") >= 0); | |
179 | assert_se(socket_address_is_netlink(&a, "route 10")); | |
180 | assert_se(!socket_address_is_netlink(&a, "192.168.1.1:8888")); | |
181 | assert_se(!socket_address_is_netlink(&a, "route 1")); | |
182 | } | |
183 | ||
e7639886 DM |
184 | static void test_in_addr_is_null(void) { |
185 | ||
186 | union in_addr_union i = {}; | |
187 | ||
188 | assert_se(in_addr_is_null(AF_INET, &i) == true); | |
189 | assert_se(in_addr_is_null(AF_INET6, &i) == true); | |
190 | ||
191 | i.in.s_addr = 0x1000000; | |
192 | assert_se(in_addr_is_null(AF_INET, &i) == false); | |
193 | assert_se(in_addr_is_null(AF_INET6, &i) == false); | |
194 | ||
195 | assert_se(in_addr_is_null(-1, &i) == -EAFNOSUPPORT); | |
196 | } | |
197 | ||
059f6c42 LP |
198 | static void test_in_addr_prefix_intersect_one(unsigned f, const char *a, unsigned apl, const char *b, unsigned bpl, int result) { |
199 | union in_addr_union ua, ub; | |
200 | ||
201 | assert_se(in_addr_from_string(f, a, &ua) >= 0); | |
202 | assert_se(in_addr_from_string(f, b, &ub) >= 0); | |
203 | ||
204 | assert_se(in_addr_prefix_intersect(f, &ua, apl, &ub, bpl) == result); | |
205 | } | |
206 | ||
207 | static void test_in_addr_prefix_intersect(void) { | |
208 | ||
209 | test_in_addr_prefix_intersect_one(AF_INET, "255.255.255.255", 32, "255.255.255.254", 32, 0); | |
210 | test_in_addr_prefix_intersect_one(AF_INET, "255.255.255.255", 0, "255.255.255.255", 32, 1); | |
211 | test_in_addr_prefix_intersect_one(AF_INET, "0.0.0.0", 0, "47.11.8.15", 32, 1); | |
212 | ||
213 | test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.1", 24, 1); | |
214 | test_in_addr_prefix_intersect_one(AF_INET, "2.2.2.2", 24, "1.1.1.1", 24, 0); | |
215 | ||
216 | test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.127", 25, 1); | |
217 | test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 24, "1.1.1.127", 26, 1); | |
218 | test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 25, "1.1.1.127", 25, 1); | |
219 | test_in_addr_prefix_intersect_one(AF_INET, "1.1.1.1", 25, "1.1.1.255", 25, 0); | |
220 | ||
221 | test_in_addr_prefix_intersect_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe", 128, 0); | |
222 | test_in_addr_prefix_intersect_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 0, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, 1); | |
223 | test_in_addr_prefix_intersect_one(AF_INET6, "::", 0, "beef:beef:beef:beef:beef:beef:beef:beef", 128, 1); | |
224 | ||
225 | test_in_addr_prefix_intersect_one(AF_INET6, "1::2", 64, "1::2", 64, 1); | |
226 | test_in_addr_prefix_intersect_one(AF_INET6, "2::2", 64, "1::2", 64, 0); | |
227 | ||
228 | test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 120, "1::007f", 121, 1); | |
229 | test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 120, "1::007f", 122, 1); | |
230 | test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 121, "1::007f", 121, 1); | |
231 | test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 121, "1::00ff", 121, 0); | |
232 | } | |
233 | ||
234 | static void test_in_addr_prefix_next_one(unsigned f, const char *before, unsigned pl, const char *after) { | |
235 | union in_addr_union ubefore, uafter, t; | |
236 | ||
237 | assert_se(in_addr_from_string(f, before, &ubefore) >= 0); | |
238 | ||
239 | t = ubefore; | |
240 | assert_se((in_addr_prefix_next(f, &t, pl) > 0) == !!after); | |
241 | ||
242 | if (after) { | |
243 | assert_se(in_addr_from_string(f, after, &uafter) >= 0); | |
244 | assert_se(in_addr_equal(f, &t, &uafter) > 0); | |
245 | } | |
246 | } | |
247 | ||
248 | static void test_in_addr_prefix_next(void) { | |
249 | ||
250 | test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 24, "192.168.1.0"); | |
251 | test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 16, "192.169.0.0"); | |
252 | test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 20, "192.168.16.0"); | |
253 | ||
254 | test_in_addr_prefix_next_one(AF_INET, "0.0.0.0", 32, "0.0.0.1"); | |
255 | test_in_addr_prefix_next_one(AF_INET, "255.255.255.255", 32, NULL); | |
256 | test_in_addr_prefix_next_one(AF_INET, "255.255.255.0", 24, NULL); | |
257 | ||
258 | test_in_addr_prefix_next_one(AF_INET6, "4400::", 128, "4400::0001"); | |
259 | test_in_addr_prefix_next_one(AF_INET6, "4400::", 120, "4400::0100"); | |
260 | test_in_addr_prefix_next_one(AF_INET6, "4400::", 127, "4400::0002"); | |
261 | test_in_addr_prefix_next_one(AF_INET6, "4400::", 8, "4500::"); | |
262 | test_in_addr_prefix_next_one(AF_INET6, "4400::", 7, "4600::"); | |
263 | ||
264 | test_in_addr_prefix_next_one(AF_INET6, "::", 128, "::1"); | |
265 | ||
266 | test_in_addr_prefix_next_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, NULL); | |
267 | test_in_addr_prefix_next_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00", 120, NULL); | |
268 | ||
269 | } | |
270 | ||
07916bea RC |
271 | static void test_in_addr_to_string_one(int f, const char *addr) { |
272 | union in_addr_union ua; | |
273 | _cleanup_free_ char *r = NULL; | |
274 | ||
275 | assert_se(in_addr_from_string(f, addr, &ua) >= 0); | |
276 | assert_se(in_addr_to_string(f, &ua, &r) >= 0); | |
277 | printf("test_in_addr_to_string_one: %s == %s\n", addr, r); | |
278 | assert_se(streq(addr, r)); | |
279 | } | |
280 | ||
281 | static void test_in_addr_to_string(void) { | |
282 | test_in_addr_to_string_one(AF_INET, "192.168.0.1"); | |
283 | test_in_addr_to_string_one(AF_INET, "10.11.12.13"); | |
284 | test_in_addr_to_string_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); | |
285 | test_in_addr_to_string_one(AF_INET6, "::1"); | |
286 | test_in_addr_to_string_one(AF_INET6, "fe80::"); | |
287 | } | |
288 | ||
b31f535c ZJS |
289 | static void *connect_thread(void *arg) { |
290 | union sockaddr_union *sa = arg; | |
291 | _cleanup_close_ int fd = -1; | |
292 | ||
293 | fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0); | |
bdf7026e | 294 | assert_se(fd >= 0); |
b31f535c ZJS |
295 | |
296 | assert_se(connect(fd, &sa->sa, sizeof(sa->in)) == 0); | |
297 | ||
298 | return NULL; | |
299 | } | |
300 | ||
301 | static void test_nameinfo_pretty(void) { | |
eda8090b | 302 | _cleanup_free_ char *stdin_name = NULL, *localhost = NULL; |
b31f535c ZJS |
303 | |
304 | union sockaddr_union s = { | |
305 | .in.sin_family = AF_INET, | |
306 | .in.sin_port = 0, | |
307 | .in.sin_addr.s_addr = htonl(INADDR_ANY), | |
308 | }; | |
309 | int r; | |
310 | ||
311 | union sockaddr_union c = {}; | |
312 | socklen_t slen = sizeof(c.in), clen = sizeof(c.in); | |
313 | ||
eda8090b TA |
314 | _cleanup_close_ int sfd = -1, cfd = -1; |
315 | r = getnameinfo_pretty(STDIN_FILENO, &stdin_name); | |
da927ba9 | 316 | log_info_errno(r, "No connection remote: %m"); |
b31f535c ZJS |
317 | |
318 | assert_se(r < 0); | |
319 | ||
320 | sfd = socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0); | |
bdf7026e | 321 | assert_se(sfd >= 0); |
b31f535c ZJS |
322 | |
323 | assert_se(bind(sfd, &s.sa, sizeof(s.in)) == 0); | |
324 | ||
325 | /* find out the port number */ | |
326 | assert_se(getsockname(sfd, &s.sa, &slen) == 0); | |
327 | ||
328 | assert_se(listen(sfd, 1) == 0); | |
329 | ||
330 | assert_se(asynchronous_job(connect_thread, &s) == 0); | |
331 | ||
332 | log_debug("Accepting new connection on fd:%d", sfd); | |
333 | cfd = accept4(sfd, &c.sa, &clen, SOCK_CLOEXEC); | |
bdf7026e | 334 | assert_se(cfd >= 0); |
b31f535c ZJS |
335 | |
336 | r = getnameinfo_pretty(cfd, &localhost); | |
337 | log_info("Connection from %s", localhost); | |
bdf7026e | 338 | assert_se(r == 0); |
b31f535c ZJS |
339 | } |
340 | ||
43dc0043 RC |
341 | static void test_sockaddr_equal(void) { |
342 | union sockaddr_union a = { | |
343 | .in.sin_family = AF_INET, | |
344 | .in.sin_port = 0, | |
345 | .in.sin_addr.s_addr = htonl(INADDR_ANY), | |
346 | }; | |
347 | union sockaddr_union b = { | |
348 | .in.sin_family = AF_INET, | |
349 | .in.sin_port = 0, | |
350 | .in.sin_addr.s_addr = htonl(INADDR_ANY), | |
351 | }; | |
352 | union sockaddr_union c = { | |
353 | .in.sin_family = AF_INET, | |
354 | .in.sin_port = 0, | |
355 | .in.sin_addr.s_addr = htonl(1234), | |
356 | }; | |
357 | union sockaddr_union d = { | |
358 | .in6.sin6_family = AF_INET6, | |
359 | .in6.sin6_port = 0, | |
360 | .in6.sin6_addr = IN6ADDR_ANY_INIT, | |
361 | }; | |
362 | assert_se(sockaddr_equal(&a, &a)); | |
363 | assert_se(sockaddr_equal(&a, &b)); | |
364 | assert_se(sockaddr_equal(&d, &d)); | |
365 | assert_se(!sockaddr_equal(&a, &c)); | |
366 | assert_se(!sockaddr_equal(&b, &c)); | |
367 | } | |
368 | ||
fc2fffe7 LP |
369 | static void test_sockaddr_un_len(void) { |
370 | static const struct sockaddr_un fs = { | |
371 | .sun_family = AF_UNIX, | |
372 | .sun_path = "/foo/bar/waldo", | |
373 | }; | |
374 | ||
375 | static const struct sockaddr_un abstract = { | |
376 | .sun_family = AF_UNIX, | |
377 | .sun_path = "\0foobar", | |
378 | }; | |
379 | ||
380 | assert_se(SOCKADDR_UN_LEN(fs) == offsetof(struct sockaddr_un, sun_path) + strlen(fs.sun_path)); | |
381 | assert_se(SOCKADDR_UN_LEN(abstract) == offsetof(struct sockaddr_un, sun_path) + 1 + strlen(abstract.sun_path + 1)); | |
382 | } | |
383 | ||
c182135d | 384 | int main(int argc, char *argv[]) { |
059f6c42 LP |
385 | |
386 | log_set_max_level(LOG_DEBUG); | |
387 | ||
ef76dff2 LP |
388 | test_ifname_valid(); |
389 | ||
c182135d RC |
390 | test_socket_address_parse(); |
391 | test_socket_address_parse_netlink(); | |
392 | test_socket_address_equal(); | |
393 | test_socket_address_get_path(); | |
43dc0043 RC |
394 | test_socket_address_is(); |
395 | test_socket_address_is_netlink(); | |
059f6c42 | 396 | |
e7639886 | 397 | test_in_addr_is_null(); |
059f6c42 LP |
398 | test_in_addr_prefix_intersect(); |
399 | test_in_addr_prefix_next(); | |
07916bea | 400 | test_in_addr_to_string(); |
059f6c42 | 401 | |
b31f535c ZJS |
402 | test_nameinfo_pretty(); |
403 | ||
43dc0043 RC |
404 | test_sockaddr_equal(); |
405 | ||
fc2fffe7 LP |
406 | test_sockaddr_un_len(); |
407 | ||
059f6c42 | 408 | return 0; |
c182135d | 409 | } |