23#include <freeradius-devel/util/test/acutest_common_init.h>
24#include <freeradius-devel/util/test/acutest_helpers.h>
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>
56 struct sockaddr_in sin = {
57 .sin_family = AF_INET,
58 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
60 socklen_t
len =
sizeof(sin);
63 fd = socket(AF_INET, SOCK_STREAM, 0);
64 if (fd < 0)
return -1;
66 if ((bind(fd, (
struct sockaddr *) &sin,
sizeof(sin)) < 0) ||
67 (listen(fd, 5) < 0) ||
68 (getsockname(fd, (
struct sockaddr *) &sin, &
len) < 0)) {
73 *port = ntohs(sin.sin_port);
106 if (listen_fd < 0)
goto done;
114 .socket_type = SOCK_STREAM,
119 .addr.v4.s_addr = htonl(INADDR_LOOPBACK),
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;
152 TEST_MSG(
"connected_count = %d, error_count = %d, timeout_count = %d, connect_errno = %d",
155 TEST_CASE(
"neither the error callback nor the timeout callback ran");
163 if (listen_fd >= 0) close(listen_fd);
178 struct sockaddr_in6 sin6 = {
179 .sin6_family = AF_INET6,
180 .sin6_addr = IN6ADDR_LOOPBACK_INIT,
185 fd = socket(AF_INET6, SOCK_DGRAM, 0);
186 if (fd < 0)
return errno;
188 if (bind(fd, (
struct sockaddr *) &sin6,
sizeof(sin6)) < 0) error = errno;
214 struct sockaddr_storage to;
219 struct timeval recv_timeout = { .tv_sec = 5 };
230 .socket_type = SOCK_DGRAM,
234 .prefix = (af == AF_INET) ? 32 : 128,
245 TEST_MSG(
"the bio did not learn the port which the kernel picked");
251 loopback = (
fr_ipaddr_t) { .
af = AF_INET, .prefix = 32, .addr.v4.s_addr = htonl(INADDR_LOOPBACK) };
253 loopback = (
fr_ipaddr_t) { .
af = AF_INET6, .prefix = 128, .addr.v6 = in6addr_loopback };
258 fd = socket(af, SOCK_DGRAM, 0);
260 if (fd < 0)
goto done;
265 rcode = sendto(fd,
"x", 1, 0, (
struct sockaddr *) &to, to_len);
269 if (rcode != 1)
goto done;
275 TEST_CHECK(setsockopt(info->
socket.
fd, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout,
sizeof(recv_timeout)) == 0);
279 TEST_CASE(
"the packet is read, not dropped");
281 TEST_MSG(
"fr_bio_read returned %zd", rcode);
282 if (rcode != 1)
goto done;
284 TEST_CASE(
"the destination is the loopback address and the port of the bio");
290 if (fd >= 0) close(fd);
314 fr_bio_t *listener, *accepted = NULL;
317 fr_ipaddr_t loopback = { .
af = AF_INET, .prefix = 32, .addr.v4.s_addr = htonl(INADDR_LOOPBACK) };
318 struct sockaddr_storage to;
320 struct sockaddr_in client_addr = {};
321 socklen_t client_len =
sizeof(client_addr);
334 if (fd < 0)
goto done;
339 .socket_type = SOCK_STREAM,
342 .src_ipaddr = loopback,
349 if (!listener)
goto done;
352 TEST_CASE(
"an accept with no connection waiting returns 0, and leaves the listener open");
355 TEST_MSG(
"fr_bio_fd_accept returned %zd", rcode);
358 if ((rcode != 0) || accepted)
goto done;
361 client = socket(AF_INET, SOCK_STREAM, 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);
371 pfd = (
struct pollfd) { .fd = linfo->
socket.
fd, .events = POLLIN };
374 TEST_CASE(
"an accept with a connection waiting returns 1 and a connected bio");
377 TEST_MSG(
"fr_bio_fd_accept returned %zd", rcode);
379 if ((rcode != 1) || !accepted)
goto done;
387 TEST_CASE(
"the remote end of the new bio is the client");
390 TEST_MSG(
"dst_port = %u, expected %u", ainfo->
socket.inet.dst_port, ntohs(client_addr.sin_port));
396 TEST_CASE(
"the new bio carries data in both directions");
406 pfd = (
struct pollfd) { .fd = ainfo->
socket.
fd, .events = POLLIN };
412 if (client >= 0) close(client);
434 struct sockaddr_in sin = {
435 .sin_family = AF_INET,
436 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
438 socklen_t
len =
sizeof(sin);
450 fd = socket(AF_INET, SOCK_STREAM, 0);
451 if (fd < 0)
return -1;
453 (void) setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf,
sizeof(rcvbuf));
455 if ((bind(fd, (
struct sockaddr *) &sin,
sizeof(sin)) < 0) ||
456 (listen(fd, 5) < 0) ||
457 (getsockname(fd, (
struct sockaddr *) &sin, &
len) < 0)) {
470 .socket_type = SOCK_STREAM,
475 .addr.v4.s_addr = htonl(INADDR_LOOPBACK),
478 .dst_port = ntohs(sin.sin_port),
480 .send_buff_is_set =
true,
525 for (i = 0; i < (16 * 1024 * 1024); i++) {
530 if (slen < 0)
return slen;
566 TEST_MSG(
"wrote %zd bytes before the bio blocked", written);
571 TEST_CASE(
"the write_blocked callback of the head bio ran once");
576 if (listen_fd >= 0) close(listen_fd);
602 TEST_MSG(
"wrote %zd bytes before the bio blocked", written);
604 TEST_CASE(
"the write_blocked callback ran once");
609 if (listen_fd >= 0) close(listen_fd);
635 char const *transport,
cfg_pair_t const *pairs)
648 if (!subcs)
return -1;
650 for (; pairs && pairs->
name; pairs++) {
668 {
"src_ipaddr",
"127.0.0.1" },
691 {
"src_ipaddr",
"127.0.0.1" },
698 TEST_CASE(
"the socket is a stream socket");
702 TEST_CASE(
"the source address is parsed");
705 TEST_CASE(
"the socket stays unconnected, with no destination");
719 {
"src_ipaddr",
"127.0.0.1" },
720 {
"delay_tcp_writes",
"yes" },
721 {
"send_buff",
"8192" },
742 {
"filename",
"/tmp/unconnected.sock" },
764 {
"ipaddr",
"127.0.0.1" },
static int const char char buffer[256]
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.
static ssize_t fr_bio_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Read raw data from a bio.
static int packet_ctx
the tests pass only the address of packet_ctx
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 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.
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.
static void test_client_config_unconnected_tcp(void)
A client configuration for an unconnected socket accepts "tcp".
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 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.
#define cf_section_rules_push(_cs, _rule)
A section grouping multiple CONF_PAIR.
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.
#define cf_section_alloc(_ctx, _parent, _name1, _name2)
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()
fr_bio_t * fr_bio_fd_alloc(TALLOC_CTX *ctx, fr_bio_fd_config_t const *cfg, size_t offset)
Allocate a FD bio.
int fr_bio_fd_accept(TALLOC_CTX *ctx, fr_bio_t **out_p, fr_bio_t *bio)
Accept a new connection on a socket.
fr_bio_fd_info_t const * fr_bio_fd_info(fr_bio_t *bio)
Returns a pointer to the bio-specific information.
fr_socket_t socket
as connected socket
char const * name
printable name of this BIO
fr_bio_fd_type_t type
type of the socket
fr_bio_fd_state_t state
connecting, open, closed, etc.
fr_ipaddr_t dst_ipaddr
their IP address
fr_bio_fd_type_t type
accept, connected, unconnected, etc.
const conf_parser_t fr_bio_fd_client_config[]
uint32_t send_buff
How big the kernel's send buffer should be.
@ FR_BIO_FD_CONNECTED
connected client sockets (UDP or TCP)
@ FR_BIO_FD_UNCONNECTED
unconnected UDP / datagram, but also allowed
@ FR_BIO_FD_LISTEN
call fr_bio_fd_accept() for each new connection
@ FR_BIO_FD_TRANSPORT_TCP
TCP over IPv4 or IPv6.
@ FR_BIO_FD_TRANSPORT_UDP
UDP over IPv4 or IPv6.
int socket_type
SOCK_STREAM or SOCK_DGRAM.
uint16_t dst_port
their port
bool tcp_delay
We do tcp_nodelay by default.
fr_bio_fd_transport_t transport_type
which transport, as a number instead of a name
@ FR_BIO_FD_STATE_OPEN
error states must be before this
bool send_buff_is_set
Whether we were provided with a send_buf.
fr_ipaddr_t src_ipaddr
our IP address
Configuration for sockets.
Run-time status of the socket.
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.
fr_cmp_ret_t fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b)
Compare two ip addresses.
void fr_bio_cb_set(fr_bio_t *bio, fr_bio_cb_funcs_t const *cb)
void fr_event_service(fr_event_list_t *el)
Service any outstanding timer or file descriptor events.
int fr_event_corral(fr_event_list_t *el, fr_time_t now, bool wait)
Gather outstanding timer and file descriptor events.
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.
Stores all information relating to an event list.
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.
fr_aka_sim_id_type_t type
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
static TALLOC_CTX * talloc_init_const(char const *name)
Allocate a top level chunk with a constant name.
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
A time delta, a difference in time measured in nanoseconds.
static fr_event_list_t * el
int fd
File descriptor if this is a live socket.
char const * fr_strerror(void)
Get the last library error.
static fr_sbuff_err_t char size_t * len