The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
bio_fd_tests.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/** Tests for the FD bio
18 *
19 * @file src/lib/bio/test/bio_fd_tests.c
20 *
21 * @copyright 2026 Network RADIUS SAS (legal@networkradius.com)
22 */
23#include <freeradius-devel/util/test/acutest_common_init.h>
24#include <freeradius-devel/util/test/acutest_helpers.h>
25
26#include <freeradius-devel/bio/bio_priv.h>
27#include <freeradius-devel/bio/fd.h>
28#include <freeradius-devel/bio/mem.h>
29#include <freeradius-devel/server/cf_parse.h>
30#include <freeradius-devel/server/cf_util.h>
31#include <freeradius-devel/util/syserror.h>
32
33#include <poll.h>
34
35static int connected_count;
36static int error_count;
37static int timeout_count;
38
39static void cb_connected(fr_bio_t *bio) { (void) bio; connected_count++; }
40static void cb_error(fr_bio_t *bio) { (void) bio; error_count++; }
41static void cb_timeout(fr_bio_t *bio) { (void) bio; timeout_count++; }
42
43/** Open a TCP socket listening on loopback
44 *
45 * The kernel completes the TCP handshake for any listening socket, and the test needs only the
46 * handshake. The test never calls accept(), so the kernel holds the new connection in the backlog of
47 * the listening socket.
48 *
49 * @param[out] port the port which the kernel picked.
50 * @return
51 * - >=0 the listening socket.
52 * - <0 on error.
53 */
54static int loopback_listen(uint16_t *port)
55{
56 struct sockaddr_in sin = {
57 .sin_family = AF_INET,
58 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
59 };
60 socklen_t len = sizeof(sin);
61 int fd;
62
63 fd = socket(AF_INET, SOCK_STREAM, 0);
64 if (fd < 0) return -1;
65
66 if ((bind(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0) ||
67 (listen(fd, 5) < 0) ||
68 (getsockname(fd, (struct sockaddr *) &sin, &len) < 0)) {
69 close(fd);
70 return -1;
71 }
72
73 *port = ntohs(sin.sin_port);
74 return fd;
75}
76
77/** A deferred connect which succeeds has to call the connected callback
78 *
79 * A non-blocking connect() to a listening loopback port can return EINPROGRESS. On EINPROGRESS,
80 * fr_bio_fd_connect_full() defers the connect, and the event loop calls fr_bio_fd_el_connect() once
81 * the socket is writable. fr_bio_fd_el_connect() then calls connect() a second time. If the
82 * handshake has finished, the second connect() can fail with EISCONN, so fr_bio_fd_try_connect() has
83 * to treat EISCONN as success.
84 *
85 * The test needs a connect which succeeds, because a connect which fails may not reach
86 * fr_bio_fd_el_connect(). kqueue can report a refused connect with EV_EOF, and event.c passes EV_EOF
87 * to the error callback, fr_bio_fd_el_error().
88 */
90{
91 TALLOC_CTX *ctx = talloc_init_const("test");
93 fr_bio_t *bio;
96 uint16_t port;
97 int listen_fd;
98 int rcode;
99
100 connected_count = 0;
101 error_count = 0;
102 timeout_count = 0;
103
104 listen_fd = loopback_listen(&port);
105 TEST_CHECK(listen_fd >= 0);
106 if (listen_fd < 0) goto done;
107
108 el = fr_event_list_alloc(ctx, NULL, NULL);
109 TEST_CHECK(el != NULL);
110 if (!el) goto done;
111
112 cfg = (fr_bio_fd_config_t) {
114 .socket_type = SOCK_STREAM,
115 .transport_type = FR_BIO_FD_TRANSPORT_TCP,
116 .async = true,
117 .src_ipaddr = {
118 .af = AF_INET,
119 .addr.v4.s_addr = htonl(INADDR_LOOPBACK),
120 .prefix = 32,
121 },
122 .dst_port = port,
123 };
124 cfg.dst_ipaddr = cfg.src_ipaddr;
125
126 bio = fr_bio_fd_alloc(ctx, &cfg, 0);
127 TEST_CHECK(bio != NULL);
128 if (!bio) goto done;
129
130 rcode = fr_bio_fd_connect_full(bio, el, cb_connected, cb_error, &timeout, cb_timeout);
131
132 /*
133 * FreeBSD completes a loopback TCP handshake inside connect(), so the connect can
134 * finish at once (rcode 1). Linux and macOS return EINPROGRESS and defer it (rcode 0).
135 */
136 TEST_CHECK(rcode >= 0);
137 TEST_MSG("connect_full returned %d. 0 means deferred, 1 means connected at once, <0 means failed", rcode);
138 if (rcode < 0) goto done;
139
140 /*
141 * Wait in the event loop until one of the three callbacks runs. The connect timeout
142 * is a timer, so a wait always ends, and a connect which never finishes cannot hang
143 * the test.
144 */
145 while (!connected_count && !error_count && !timeout_count) {
146 if (fr_event_corral(el, fr_time(), true) < 0) break;
148 }
149
150 TEST_CASE("the connected callback ran");
152 TEST_MSG("connected_count = %d, error_count = %d, timeout_count = %d, connect_errno = %d",
154
155 TEST_CASE("neither the error callback nor the timeout callback ran");
158
159 TEST_CASE("the bio is open");
161
162done:
163 if (listen_fd >= 0) close(listen_fd);
164 talloc_free(ctx);
165}
166
167/** See if the IPv6 loopback address can be used
168 *
169 * Some hosts and containers turn IPv6 off. The socket() or bind() fails there.
170 *
171 * @return
172 * - 0 the IPv6 loopback address can be used.
173 * - the errno from the socket() or bind() which failed. The errno is saved before close() can
174 * change it.
175 */
176static int ipv6_loopback_usable(void)
177{
178 struct sockaddr_in6 sin6 = {
179 .sin6_family = AF_INET6,
180 .sin6_addr = IN6ADDR_LOOPBACK_INIT,
181 };
182 int fd;
183 int error = 0;
184
185 fd = socket(AF_INET6, SOCK_DGRAM, 0);
186 if (fd < 0) return errno;
187
188 if (bind(fd, (struct sockaddr *) &sin6, sizeof(sin6)) < 0) error = errno;
189 close(fd);
190
191 return error;
192}
193
194/** Read one packet through an unconnected UDP bio bound to the wildcard address
195 *
196 * The local address of a bio bound to the wildcard address does not identify the address that a
197 * packet was sent to. fr_bio_fd_init_common() therefore installs fr_bio_fd_recvfromto4() or
198 * fr_bio_fd_recvfromto6(). Those two functions read the destination address from a control message
199 * (data which recvmsg() returns beside the packet), and copy the destination port from the port of
200 * the bio.
201 *
202 * fr_bio_fd_common_datagram() also enables receive timestamps, so each packet carries two control
203 * messages. If cbuf in fr_bio_fd_t is too small for both control messages, then the bio drops every
204 * packet.
205 */
206static void recvfromto_test(int af)
207{
208 TALLOC_CTX *ctx = talloc_init_const("test");
210 fr_bio_t *bio;
211 fr_bio_fd_info_t const *info;
213 fr_ipaddr_t loopback;
214 struct sockaddr_storage to;
215 socklen_t to_len;
216 uint8_t buffer[16];
217 ssize_t rcode;
218 int fd = -1;
219 struct timeval recv_timeout = { .tv_sec = 5 };
220 int error;
221
222 if ((af == AF_INET6) && ((error = ipv6_loopback_usable()) != 0)) {
223 TEST_SKIP("the IPv6 loopback address is not available: %s", fr_syserror(error));
224 talloc_free(ctx);
225 return;
226 }
227
228 cfg = (fr_bio_fd_config_t) {
230 .socket_type = SOCK_DGRAM,
231 .transport_type = FR_BIO_FD_TRANSPORT_UDP,
232 .src_ipaddr = {
233 .af = af,
234 .prefix = (af == AF_INET) ? 32 : 128,
235 },
236 };
237
238 bio = fr_bio_fd_alloc(ctx, &cfg, 0);
239 TEST_CHECK(bio != NULL);
240 TEST_MSG("fr_bio_fd_alloc failed: %s", fr_strerror());
241 if (!bio) goto done;
242
243 info = fr_bio_fd_info(bio);
244 TEST_CHECK(info->socket.inet.src_port != 0);
245 TEST_MSG("the bio did not learn the port which the kernel picked");
246
247 /*
248 * Send one packet to the loopback address and the port of the bio.
249 */
250 if (af == AF_INET) {
251 loopback = (fr_ipaddr_t) { .af = AF_INET, .prefix = 32, .addr.v4.s_addr = htonl(INADDR_LOOPBACK) };
252 } else {
253 loopback = (fr_ipaddr_t) { .af = AF_INET6, .prefix = 128, .addr.v6 = in6addr_loopback };
254 }
255
256 TEST_CHECK(fr_ipaddr_to_sockaddr(&to, &to_len, &loopback, info->socket.inet.src_port) == 0);
257
258 fd = socket(af, SOCK_DGRAM, 0);
259 TEST_CHECK(fd >= 0);
260 if (fd < 0) goto done;
261
262 /*
263 * A failed TEST_CHECK() calls printf(), which may change errno, so save errno first.
264 */
265 rcode = sendto(fd, "x", 1, 0, (struct sockaddr *) &to, to_len);
266 error = errno;
267 TEST_CHECK(rcode == 1);
268 TEST_MSG("sendto failed: %s", fr_syserror(error));
269 if (rcode != 1) goto done;
270
271 /*
272 * The socket of the bio is blocking, so fr_bio_read() waits until the packet arrives. The
273 * receive timeout stops the test from waiting forever if the packet never arrives.
274 */
275 TEST_CHECK(setsockopt(info->socket.fd, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof(recv_timeout)) == 0);
276
277 rcode = fr_bio_read(bio, &packet_ctx, buffer, sizeof(buffer));
278
279 TEST_CASE("the packet is read, not dropped");
280 TEST_CHECK(rcode == 1);
281 TEST_MSG("fr_bio_read returned %zd", rcode);
282 if (rcode != 1) goto done;
283
284 TEST_CASE("the destination is the loopback address and the port of the bio");
285 TEST_CHECK(fr_ipaddr_cmp(&packet_ctx.socket.inet.dst_ipaddr, &loopback) == 0);
286 TEST_CHECK(packet_ctx.socket.inet.dst_port == info->socket.inet.src_port);
287 TEST_MSG("dst_port = %u, expected %u", packet_ctx.socket.inet.dst_port, info->socket.inet.src_port);
288
289done:
290 if (fd >= 0) close(fd);
291 talloc_free(ctx);
292}
293
294static void test_recvfromto4(void)
295{
296 recvfromto_test(AF_INET);
297}
298
299static void test_recvfromto6(void)
300{
301 recvfromto_test(AF_INET6);
302}
303
304/** Accept a TCP connection through a listening bio
305 *
306 * fr_bio_fd_accept() returns 0 when no connection is waiting, and 1 with a new connected bio when one
307 * is. The listening bio must not change: the new socket, its name, and its read and write routines
308 * all belong to the new bio.
309 */
310static void test_listen_accept(void)
311{
312 TALLOC_CTX *ctx = talloc_init_const("test");
314 fr_bio_t *listener, *accepted = NULL;
315 fr_bio_fd_info_t const *linfo, *ainfo;
317 fr_ipaddr_t loopback = { .af = AF_INET, .prefix = 32, .addr.v4.s_addr = htonl(INADDR_LOOPBACK) };
318 struct sockaddr_storage to;
319 socklen_t to_len;
320 struct sockaddr_in client_addr = {};
321 socklen_t client_len = sizeof(client_addr);
322 struct pollfd pfd;
323 char buffer[16];
324 ssize_t rcode;
325 int client = -1;
326 int fd;
327 uint16_t port;
328
329 /*
330 * A server socket needs a port. Ask the kernel for a free one, and release it.
331 */
332 fd = loopback_listen(&port);
333 TEST_CHECK(fd >= 0);
334 if (fd < 0) goto done;
335 close(fd);
336
337 cfg = (fr_bio_fd_config_t) {
339 .socket_type = SOCK_STREAM,
340 .transport_type = FR_BIO_FD_TRANSPORT_TCP,
341 .async = true, /* so that an accept() with nothing waiting does not block */
342 .src_ipaddr = loopback,
343 .src_port = port,
344 };
345
346 listener = fr_bio_fd_alloc(ctx, &cfg, 0);
347 TEST_CHECK(listener != NULL);
348 TEST_MSG("fr_bio_fd_alloc failed: %s", fr_strerror());
349 if (!listener) goto done;
350
351 linfo = fr_bio_fd_info(listener);
352 TEST_CASE("an accept with no connection waiting returns 0, and leaves the listener open");
353 rcode = fr_bio_fd_accept(ctx, &accepted, listener);
354 TEST_CHECK(rcode == 0);
355 TEST_MSG("fr_bio_fd_accept returned %zd", rcode);
356 TEST_CHECK(accepted == NULL);
357 TEST_CHECK(fcntl(linfo->socket.fd, F_GETFD) >= 0);
358 if ((rcode != 0) || accepted) goto done;
359
360 TEST_CHECK(fr_ipaddr_to_sockaddr(&to, &to_len, &loopback, linfo->socket.inet.src_port) == 0);
361 client = socket(AF_INET, SOCK_STREAM, 0);
362 TEST_CHECK(client >= 0);
363 if (client < 0) goto done;
364 TEST_CHECK(connect(client, (struct sockaddr *) &to, to_len) == 0);
365 TEST_CHECK(getsockname(client, (struct sockaddr *) &client_addr, &client_len) == 0);
366
367 /*
368 * connect() can return before the connection is queued on the listener. A listening
369 * socket becomes readable once a connection is waiting, so wait for that.
370 */
371 pfd = (struct pollfd) { .fd = linfo->socket.fd, .events = POLLIN };
372 TEST_CHECK(poll(&pfd, 1, 5000) == 1);
373
374 TEST_CASE("an accept with a connection waiting returns 1 and a connected bio");
375 rcode = fr_bio_fd_accept(ctx, &accepted, listener);
376 TEST_CHECK(rcode == 1);
377 TEST_MSG("fr_bio_fd_accept returned %zd", rcode);
378 TEST_CHECK(accepted != NULL);
379 if ((rcode != 1) || !accepted) goto done;
380
381 ainfo = fr_bio_fd_info(accepted);
384 TEST_CHECK((ainfo->socket.fd >= 0) && (ainfo->socket.fd != linfo->socket.fd));
385 TEST_CHECK(ainfo->name != NULL);
386
387 TEST_CASE("the remote end of the new bio is the client");
388 TEST_CHECK(fr_ipaddr_cmp(&ainfo->socket.inet.dst_ipaddr, &loopback) == 0);
389 TEST_CHECK(ainfo->socket.inet.dst_port == ntohs(client_addr.sin_port));
390 TEST_MSG("dst_port = %u, expected %u", ainfo->socket.inet.dst_port, ntohs(client_addr.sin_port));
391
392 TEST_CASE("the listener is unchanged");
394 TEST_CHECK(fr_bio_read(listener, &packet_ctx, buffer, sizeof(buffer)) < 0);
395
396 TEST_CASE("the new bio carries data in both directions");
397 TEST_CHECK(fr_bio_write(accepted, &packet_ctx, "x", 1) == 1);
398 TEST_CHECK(recv(client, buffer, sizeof(buffer), 0) == 1);
399 TEST_CHECK(buffer[0] == 'x');
400
401 TEST_CHECK(send(client, "y", 1, 0) == 1);
402
403 /*
404 * The new socket is non-blocking, so wait until the byte has arrived.
405 */
406 pfd = (struct pollfd) { .fd = ainfo->socket.fd, .events = POLLIN };
407 TEST_CHECK(poll(&pfd, 1, 5000) == 1);
408 TEST_CHECK(fr_bio_read(accepted, &packet_ctx, buffer, sizeof(buffer)) == 1);
409 TEST_CHECK(buffer[0] == 'y');
410
411done:
412 if (client >= 0) close(client);
413 talloc_free(ctx);
414}
415
416static int blocked_count;
417
418static int cb_write_blocked(fr_bio_t *bio) { (void) bio; blocked_count++; return 1; }
419
420/** Open a non-blocking TCP bio to a listening socket which never calls accept()
421 *
422 * The listening socket has a small receive buffer, and the bio has a small send buffer, so a few
423 * kilobytes of writes fill both buffers. The kernel then has no room for another byte.
424 *
425 * @param[in] ctx to allocate the bio in.
426 * @param[out] listen_out the listening socket. The caller closes the socket.
427 * @param[out] bio_out the connected FD bio.
428 * @return
429 * - 0 on success.
430 * - <0 on error.
431 */
432static int blocked_connect(TALLOC_CTX *ctx, int *listen_out, fr_bio_t **bio_out)
433{
434 struct sockaddr_in sin = {
435 .sin_family = AF_INET,
436 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
437 };
438 socklen_t len = sizeof(sin);
439 int rcvbuf = 4096;
440 static fr_bio_fd_config_t cfg; //!< the FD bio keeps a pointer to this, and outlives this function
441 fr_bio_t *bio;
444 int fd;
445
446 connected_count = 0;
447 error_count = 0;
448 timeout_count = 0;
449
450 fd = socket(AF_INET, SOCK_STREAM, 0);
451 if (fd < 0) return -1;
452
453 (void) setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
454
455 if ((bind(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0) ||
456 (listen(fd, 5) < 0) ||
457 (getsockname(fd, (struct sockaddr *) &sin, &len) < 0)) {
458 close(fd);
459 return -1;
460 }
461
462 el = fr_event_list_alloc(ctx, NULL, NULL);
463 if (!el) {
464 close(fd);
465 return -1;
466 }
467
468 cfg = (fr_bio_fd_config_t) {
470 .socket_type = SOCK_STREAM,
471 .transport_type = FR_BIO_FD_TRANSPORT_TCP,
472 .async = true,
473 .src_ipaddr = {
474 .af = AF_INET,
475 .addr.v4.s_addr = htonl(INADDR_LOOPBACK),
476 .prefix = 32,
477 },
478 .dst_port = ntohs(sin.sin_port),
479 .send_buff = 4096,
480 .send_buff_is_set = true,
481 };
482 cfg.dst_ipaddr = cfg.src_ipaddr;
483
484 bio = fr_bio_fd_alloc(ctx, &cfg, 0);
485 if (!bio) {
486 close(fd);
487 return -1;
488 }
489
490 if (fr_bio_fd_connect_full(bio, el, cb_connected, cb_error, &timeout, cb_timeout) < 0) {
491 close(fd);
492 return -1;
493 }
494
496 if (fr_event_corral(el, fr_time(), true) < 0) break;
498 }
499
500 if (fr_bio_fd_info(bio)->state != FR_BIO_FD_STATE_OPEN) {
501 close(fd);
502 return -1;
503 }
504
505 *listen_out = fd;
506 *bio_out = bio;
507 return 0;
508}
509
510/** Write one byte at a time until the socket has no room
511 *
512 * A one byte write cannot be partial. The kernel either takes the byte, or fails the write with
513 * EAGAIN. The test therefore exercises the "no bytes accepted" path of the FD bio, which is
514 * different from the "some bytes accepted" path.
515 *
516 * @return
517 * - the number of bytes written before the bio blocked.
518 * - <0 if the bio never blocked, or returned an error other than "would block".
519 */
521{
522 ssize_t total = 0;
523 int i;
524
525 for (i = 0; i < (16 * 1024 * 1024); i++) {
526 ssize_t slen;
527
528 slen = fr_bio_write(bio, NULL, "x", 1);
529 if (slen == fr_bio_error(IO_WOULD_BLOCK)) return total;
530 if (slen < 0) return slen;
531
532 total += slen;
533 }
534
535 return -1;
536}
537
538/** A write which blocks with no bytes accepted has to run the write_blocked callback of the head bio
539 *
540 * The application sets write_blocked on the head of the chain, not on the FD bio. The FD bio runs
541 * fr_bio_write_blocked() when a write is partial. When the socket has no room at all, the write fails
542 * with EAGAIN, and the FD bio has to run fr_bio_write_blocked() for that case, too.
543 */
545{
546 TALLOC_CTX *ctx = talloc_init_const("test");
547 fr_bio_t *fd_bio = NULL, *mem;
548 int listen_fd = -1;
549 ssize_t written;
550
551 if (blocked_connect(ctx, &listen_fd, &fd_bio) < 0) {
552 TEST_CHECK(0);
553 TEST_MSG("failed opening the connection: %s", fr_strerror());
554 goto done;
555 }
556
557 mem = fr_bio_mem_alloc(ctx, 4096, 4096, fd_bio);
558 TEST_CHECK(mem != NULL);
559 if (!mem) goto done;
560
561 blocked_count = 0;
562 fr_bio_cb_set(mem, &(fr_bio_cb_funcs_t) { .write_blocked = cb_write_blocked });
563
564 written = write_until_blocked(mem);
565 TEST_CHECK(written > 0);
566 TEST_MSG("wrote %zd bytes before the bio blocked", written);
567
568 TEST_CASE("the FD bio is blocked");
569 TEST_CHECK(fr_bio_fd_info(fd_bio)->write_blocked);
570
571 TEST_CASE("the write_blocked callback of the head bio ran once");
573 TEST_MSG("blocked_count = %d", blocked_count);
574
575done:
576 if (listen_fd >= 0) close(listen_fd);
577 talloc_free(ctx);
578}
579
580/** The write_blocked callback of an FD bio with no other bios still runs on EAGAIN
581 *
582 * The FD bio is the head of the chain, so the head callback is the callback of the FD bio.
583 */
585{
586 TALLOC_CTX *ctx = talloc_init_const("test");
587 fr_bio_t *fd_bio = NULL;
588 int listen_fd = -1;
589 ssize_t written;
590
591 if (blocked_connect(ctx, &listen_fd, &fd_bio) < 0) {
592 TEST_CHECK(0);
593 TEST_MSG("failed opening the connection: %s", fr_strerror());
594 goto done;
595 }
596
597 blocked_count = 0;
598 fr_bio_cb_set(fd_bio, &(fr_bio_cb_funcs_t) { .write_blocked = cb_write_blocked });
599
600 written = write_until_blocked(fd_bio);
601 TEST_CHECK(written > 0);
602 TEST_MSG("wrote %zd bytes before the bio blocked", written);
603
604 TEST_CASE("the write_blocked callback ran once");
606 TEST_MSG("blocked_count = %d", blocked_count);
607
608done:
609 if (listen_fd >= 0) close(listen_fd);
610 talloc_free(ctx);
611}
612
613/** One key / value pair in the transport subsection of a test configuration
614 */
615typedef struct {
616 char const *name;
617 char const *value;
618} cfg_pair_t;
619
620/** Parse "transport" and the transport subsection with fr_bio_fd_client_config
621 *
622 * The caller sets the socket type before the parse. rlm_radius sets the type of the socket in the
623 * same way, as the "mode" decides if the sockets are connected.
624 *
625 * @param[in] ctx to allocate the configuration in.
626 * @param[out] cfg where to write the parsed configuration.
627 * @param[in] type of the socket, set before the parse.
628 * @param[in] transport name of the transport, which is also the name of the subsection.
629 * @param[in] pairs for the subsection, terminated by an entry with a NULL name. May be NULL.
630 * @return
631 * - 0 on success.
632 * - <0 on error.
633 */
635 char const *transport, cfg_pair_t const *pairs)
636{
637 CONF_SECTION *cs, *subcs;
638 int rcode;
639
640 *cfg = (fr_bio_fd_config_t) { .type = type };
641
642 cs = cf_section_alloc(ctx, NULL, "test", NULL);
643 if (!cs) return -1;
644
645 if (!cf_pair_alloc(cs, "transport", transport, T_OP_EQ, T_BARE_WORD, T_BARE_WORD)) return -1;
646
647 subcs = cf_section_alloc(cs, cs, transport, NULL);
648 if (!subcs) return -1;
649
650 for (; pairs && pairs->name; pairs++) {
651 if (!cf_pair_alloc(subcs, pairs->name, pairs->value, T_OP_EQ, T_BARE_WORD, T_BARE_WORD)) return -1;
652 }
653
654 if (cf_section_rules_push(cs, fr_bio_fd_client_config) < 0) return -1;
655
656 rcode = cf_section_parse(ctx, cfg, cs);
657 talloc_free(cs);
658 return rcode;
659}
660
661/** A client configuration for an unconnected socket accepts "udp"
662 */
664{
665 TALLOC_CTX *ctx = talloc_init_const("test");
667 cfg_pair_t const pairs[] = {
668 { "src_ipaddr", "127.0.0.1" },
669 { NULL, NULL }
670 };
671
672 TEST_CHECK(parse_client_config(ctx, &cfg, FR_BIO_FD_UNCONNECTED, "udp", pairs) == 0);
673 TEST_MSG("failed parsing: %s", fr_strerror());
674
675 TEST_CHECK(cfg.socket_type == SOCK_DGRAM);
677 TEST_CHECK(cfg.src_ipaddr.af == AF_INET);
678
679 talloc_free(ctx);
680}
681
682/** A client configuration for an unconnected socket accepts "tcp"
683 *
684 * The destination of the socket is not in the configuration, as the caller sets the destination later.
685 */
687{
688 TALLOC_CTX *ctx = talloc_init_const("test");
690 cfg_pair_t const pairs[] = {
691 { "src_ipaddr", "127.0.0.1" },
692 { NULL, NULL }
693 };
694
695 TEST_CHECK(parse_client_config(ctx, &cfg, FR_BIO_FD_UNCONNECTED, "tcp", pairs) == 0);
696 TEST_MSG("failed parsing: %s", fr_strerror());
697
698 TEST_CASE("the socket is a stream socket");
699 TEST_CHECK(cfg.socket_type == SOCK_STREAM);
701
702 TEST_CASE("the source address is parsed");
703 TEST_CHECK(cfg.src_ipaddr.af == AF_INET);
704
705 TEST_CASE("the socket stays unconnected, with no destination");
707 TEST_CHECK(cfg.dst_ipaddr.af == AF_UNSPEC);
708
709 talloc_free(ctx);
710}
711
712/** The "tcp" subsection of an unconnected socket takes the stream socket options
713 */
715{
716 TALLOC_CTX *ctx = talloc_init_const("test");
718 cfg_pair_t const pairs[] = {
719 { "src_ipaddr", "127.0.0.1" },
720 { "delay_tcp_writes", "yes" },
721 { "send_buff", "8192" },
722 { NULL, NULL }
723 };
724
725 TEST_CHECK(parse_client_config(ctx, &cfg, FR_BIO_FD_UNCONNECTED, "tcp", pairs) == 0);
726 TEST_MSG("failed parsing: %s", fr_strerror());
727
728 TEST_CHECK(cfg.tcp_delay == true);
729 TEST_CHECK(cfg.send_buff_is_set == true);
730 TEST_CHECK(cfg.send_buff == 8192);
731
732 talloc_free(ctx);
733}
734
735/** A client configuration for an unconnected socket rejects every transport except "udp" and "tcp"
736 */
738{
739 TALLOC_CTX *ctx = talloc_init_const("test");
741 cfg_pair_t const pairs[] = {
742 { "filename", "/tmp/unconnected.sock" },
743 { NULL, NULL }
744 };
745
746 TEST_CHECK(parse_client_config(ctx, &cfg, FR_BIO_FD_UNCONNECTED, "unix", pairs) < 0);
747
748 TEST_CASE("the same holds for a file");
749 TEST_CHECK(parse_client_config(ctx, &cfg, FR_BIO_FD_UNCONNECTED, "file", pairs) < 0);
750
751 talloc_free(ctx);
752}
753
754/** A client configuration for a connected socket still takes the destination of the "tcp" subsection
755 *
756 * The unconnected sockets have their own "tcp" subsection with no destination, and the change must
757 * leave the connected sockets alone.
758 */
760{
761 TALLOC_CTX *ctx = talloc_init_const("test");
763 cfg_pair_t const pairs[] = {
764 { "ipaddr", "127.0.0.1" },
765 { "port", "2083" },
766 { NULL, NULL }
767 };
768
769 TEST_CHECK(parse_client_config(ctx, &cfg, FR_BIO_FD_CONNECTED, "tcp", pairs) == 0);
770 TEST_MSG("failed parsing: %s", fr_strerror());
771
772 TEST_CHECK(cfg.socket_type == SOCK_STREAM);
774 TEST_CHECK(cfg.dst_ipaddr.af == AF_INET);
775 TEST_CHECK(cfg.dst_port == 2083);
776
777 talloc_free(ctx);
778}
779
781 { "deferred_connect_success_calls_connected_cb", test_deferred_connect_success_calls_connected_cb },
782 { "recvfromto4", test_recvfromto4 },
783 { "recvfromto6", test_recvfromto6 },
784 { "listen_accept", test_listen_accept },
785 { "eagain_calls_head_write_blocked_cb", test_eagain_calls_head_write_blocked_cb },
786 { "eagain_calls_fd_write_blocked_cb", test_eagain_calls_fd_write_blocked_cb },
787 { "client_config_unconnected_udp", test_client_config_unconnected_udp },
788 { "client_config_unconnected_tcp", test_client_config_unconnected_tcp },
789 { "client_config_unconnected_tcp_options", test_client_config_unconnected_tcp_options },
790 { "client_config_unconnected_rejects_other_transports", test_client_config_unconnected_rejects_other_transports },
791 { "client_config_connected_tcp", test_client_config_connected_tcp },
793};
static int const char char buffer[256]
Definition acutest.h:641
#define TEST_CHECK(cond)
Definition acutest.h:101
#define TEST_CASE(name)
Definition acutest.h:203
#define TEST_SKIP(...)
Definition acutest.h:290
#define TEST_TERMINATOR
Definition acutest.h:77
#define TEST_MSG(...)
Definition acutest.h:234
static ssize_t fr_bio_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
Write raw data to a bio.
Definition base.h:184
static ssize_t fr_bio_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Read raw data from a bio.
Definition base.h:161
#define fr_bio_error(_x)
Definition base.h:200
static int packet_ctx
the tests pass only the address of packet_ctx
TEST_LIST
static void cb_timeout(fr_bio_t *bio)
static void recvfromto_test(int af)
Read one packet through an unconnected UDP bio bound to the wildcard address.
static void test_deferred_connect_success_calls_connected_cb(void)
A deferred connect which succeeds has to call the connected callback.
static int cb_write_blocked(fr_bio_t *bio)
static int blocked_count
static ssize_t write_until_blocked(fr_bio_t *bio)
Write one byte at a time until the socket has no room.
static void test_recvfromto6(void)
static int loopback_listen(uint16_t *port)
Open a TCP socket listening on loopback.
static void cb_error(fr_bio_t *bio)
static int connected_count
static void test_eagain_calls_fd_write_blocked_cb(void)
The write_blocked callback of an FD bio with no other bios still runs on EAGAIN.
static void test_recvfromto4(void)
static void test_eagain_calls_head_write_blocked_cb(void)
A write which blocks with no bytes accepted has to run the write_blocked callback of the head bio.
static void test_listen_accept(void)
Accept a TCP connection through a listening bio.
char const * name
static int parse_client_config(TALLOC_CTX *ctx, fr_bio_fd_config_t *cfg, fr_bio_fd_type_t type, char const *transport, cfg_pair_t const *pairs)
Parse "transport" and the transport subsection with fr_bio_fd_client_config.
static void test_client_config_unconnected_tcp_options(void)
The "tcp" subsection of an unconnected socket takes the stream socket options.
char const * value
static void test_client_config_unconnected_tcp(void)
A client configuration for an unconnected socket accepts "tcp".
static int error_count
static void test_client_config_connected_tcp(void)
A client configuration for a connected socket still takes the destination of the "tcp" subsection.
static void test_client_config_unconnected_udp(void)
A client configuration for an unconnected socket accepts "udp".
static int ipv6_loopback_usable(void)
See if the IPv6 loopback address can be used.
static int blocked_connect(TALLOC_CTX *ctx, int *listen_out, fr_bio_t **bio_out)
Open a non-blocking TCP bio to a listening socket which never calls accept()
static void cb_connected(fr_bio_t *bio)
static int timeout_count
static void test_client_config_unconnected_rejects_other_transports(void)
A client configuration for an unconnected socket rejects every transport except "udp" and "tcp".
One key / value pair in the transport subsection of a test configuration.
int cf_section_parse(TALLOC_CTX *ctx, void *base, CONF_SECTION *cs)
Parse a configuration section into user-supplied variables.
Definition cf_parse.c:1360
#define cf_section_rules_push(_cs, _rule)
Definition cf_parse.h:754
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
CONF_PAIR * cf_pair_alloc(CONF_SECTION *parent, char const *attr, char const *value, fr_token_t op, fr_token_t lhs_quote, fr_token_t rhs_quote)
Allocate a CONF_PAIR.
Definition cf_util.c:1487
#define cf_section_alloc(_ctx, _parent, _name1, _name2)
Definition cf_util.h:198
int fr_bio_fd_connect_full(fr_bio_t *bio, fr_event_list_t *el, fr_bio_callback_t connected_cb, fr_bio_callback_t error_cb, fr_time_delta_t *timeout, fr_bio_callback_t timeout_cb)
Finalize a connect()
Definition fd.c:1349
fr_bio_t * fr_bio_fd_alloc(TALLOC_CTX *ctx, fr_bio_fd_config_t const *cfg, size_t offset)
Allocate a FD bio.
Definition fd.c:1116
int fr_bio_fd_accept(TALLOC_CTX *ctx, fr_bio_t **out_p, fr_bio_t *bio)
Accept a new connection on a socket.
Definition fd.c:1861
fr_bio_fd_info_t const * fr_bio_fd_info(fr_bio_t *bio)
Returns a pointer to the bio-specific information.
Definition fd.c:1446
fr_socket_t socket
as connected socket
Definition fd.h:145
char const * name
printable name of this BIO
Definition fd.h:150
fr_bio_fd_type_t type
type of the socket
Definition fd.h:147
fr_bio_fd_state_t state
connecting, open, closed, etc.
Definition fd.h:148
fr_ipaddr_t dst_ipaddr
their IP address
Definition fd.h:102
fr_bio_fd_type_t type
accept, connected, unconnected, etc.
Definition fd.h:93
const conf_parser_t fr_bio_fd_client_config[]
Definition fd_config.c:352
uint32_t send_buff
How big the kernel's send buffer should be.
Definition fd.h:115
fr_bio_fd_type_t
Definition fd.h:58
@ FR_BIO_FD_CONNECTED
connected client sockets (UDP or TCP)
Definition fd.h:63
@ FR_BIO_FD_UNCONNECTED
unconnected UDP / datagram, but also allowed
Definition fd.h:60
@ FR_BIO_FD_LISTEN
call fr_bio_fd_accept() for each new connection
Definition fd.h:64
@ FR_BIO_FD_TRANSPORT_TCP
TCP over IPv4 or IPv6.
Definition fd.h:79
@ FR_BIO_FD_TRANSPORT_UDP
UDP over IPv4 or IPv6.
Definition fd.h:80
int socket_type
SOCK_STREAM or SOCK_DGRAM.
Definition fd.h:95
uint16_t dst_port
their port
Definition fd.h:105
bool tcp_delay
We do tcp_nodelay by default.
Definition fd.h:127
fr_bio_fd_transport_t transport_type
which transport, as a number instead of a name
Definition fd.h:97
@ FR_BIO_FD_STATE_OPEN
error states must be before this
Definition fd.h:54
bool send_buff_is_set
Whether we were provided with a send_buf.
Definition fd.h:134
fr_ipaddr_t src_ipaddr
our IP address
Definition fd.h:101
Configuration for sockets.
Definition fd.h:92
Run-time status of the socket.
Definition fd.h:144
Per-packet context.
Definition fd.h:46
talloc_free(hp)
int fr_ipaddr_to_sockaddr(struct sockaddr_storage *sa, socklen_t *salen, fr_ipaddr_t const *ipaddr, uint16_t port)
Convert our internal ip address representation to a sockaddr.
Definition inet.c:1399
fr_cmp_ret_t fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b)
Compare two ip addresses.
Definition inet.c:1353
int af
Address family.
Definition inet.h:64
IPv4/6 prefix.
void fr_bio_cb_set(fr_bio_t *bio, fr_bio_cb_funcs_t const *cb)
Definition base.c:260
void fr_event_service(fr_event_list_t *el)
Service any outstanding timer or file descriptor events.
Definition event.c:2401
int fr_event_corral(fr_event_list_t *el, fr_time_t now, bool wait)
Gather outstanding timer and file descriptor events.
Definition event.c:2088
#define fr_time()
Definition event.c:58
fr_event_list_t * fr_event_list_alloc(TALLOC_CTX *ctx, fr_event_status_cb_t status, void *status_uctx)
Initialise a new event list.
Definition event.c:2608
Stores all information relating to an event list.
Definition event.c:375
fr_bio_t * fr_bio_mem_alloc(TALLOC_CTX *ctx, size_t read_size, size_t write_size, fr_bio_t *next)
Allocate a memory buffer bio.
Definition mem.c:686
unsigned short uint16_t
long int ssize_t
unsigned char uint8_t
static bool done
Definition radclient.c:80
fr_aka_sim_id_type_t type
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
static TALLOC_CTX * talloc_init_const(char const *name)
Allocate a top level chunk with a constant name.
Definition talloc.h:127
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:599
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
@ T_BARE_WORD
Definition token.h:118
@ T_OP_EQ
Definition token.h:81
static fr_event_list_t * el
int fd
File descriptor if this is a live socket.
Definition socket.h:86
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:558
static fr_sbuff_err_t char size_t * len
Definition value.h:1061