25#include <freeradius-devel/bio/fd_priv.h>
26#include <freeradius-devel/bio/fd_read.h>
27#include <freeradius-devel/bio/fd_write.h>
28#include <freeradius-devel/bio/null.h>
35# define SOL_IP IPPROTO_IP
49# ifdef IPV6_RECVPKTINFO
50# include <linux/version.h>
51# if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
52# ifdef IPV6_2292PKTINFO
53# undef IPV6_RECVPKTINFO
55# define IPV6_RECVPKTINFO IPV6_2292PKTINFO
56# define IPV6_PKTINFO IPV6_2292PKTINFO
60# elif defined(IPV6_2292PKTINFO)
61# define IPV6_RECVPKTINFO IPV6_2292PKTINFO
74# if !defined(IPV6_RECVPKTINFO) && defined(IPV6_PKTINFO)
75# define IPV6_RECVPKTINFO IPV6_PKTINFO
81# elif !defined(IPV6_PKTINFO)
82# undef IPV6_RECVPKTINFO
86#define ADDR_INIT do { \
87 addr->when = fr_time(); \
88 addr->socket.type = my->info.socket.type; \
89 addr->socket.fd = -1; \
90 addr->socket.inet.ifindex = my->info.socket.inet.ifindex; \
159 if (rcode < 0)
return rcode;
182 struct sockaddr_storage sockaddr;
184#ifdef STATIC_ANALYZER
185 sockaddr = (
struct sockaddr_storage) {};
189 salen =
sizeof(sockaddr);
191 rcode = recvfrom(my->
info.
socket.
fd,
buffer, size, 0, (
struct sockaddr *) &sockaddr, &salen);
195 fr_assert((sockaddr.ss_family == AF_INET) || (sockaddr.ss_family == AF_INET6));
201 if (rcode == 0)
return rcode;
215 struct sockaddr_storage sockaddr;
218 salen =
sizeof(sockaddr);
220 rcode = recvfrom(my->
info.
socket.
fd,
buffer, size, 0, (
struct sockaddr *) &sockaddr, &salen);
233 if (rcode == 0)
return rcode;
285 struct sockaddr_storage sockaddr;
296 rcode = sendto(my->
info.
socket.
fd,
buffer, size, 0, (
struct sockaddr *) &sockaddr, salen);
305#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) || defined(IPV6_PKTINFO)
310 struct sockaddr_storage from;
313#ifdef STATIC_ANALYZER
314 from.ss_family = AF_UNSPEC;
317 memset(&my->cbuf, 0,
sizeof(my->cbuf));
318 memset(&my->msgh, 0,
sizeof(
struct msghdr));
320 my->iov = (
struct iovec) {
325 my->msgh = (
struct msghdr) {
326 .msg_control = my->cbuf,
327 .msg_controllen =
sizeof(my->cbuf),
329 .msg_namelen =
sizeof(from),
338 if (my->msgh.msg_flags == MSG_TRUNC)
return 0;
339 if (my->msgh.msg_flags == MSG_CTRUNC)
return 0;
344 &from, my->msgh.msg_namelen);
347 if (rcode == 0)
return rcode;
354#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
361 struct cmsghdr *cmsg;
366 rcode = fd_fd_recvfromto_common(my, packet_ctx,
buffer, size);
367 if (rcode <= 0)
return rcode;
371 for (cmsg = CMSG_FIRSTHDR(&my->msgh);
373 cmsg = CMSG_NXTHDR(&my->msgh, cmsg)) {
377 if ((cmsg->cmsg_level ==
SOL_IP) &&
378 (cmsg->cmsg_type == IP_PKTINFO)) {
379 struct in_pktinfo *i = (
struct in_pktinfo *) CMSG_DATA(cmsg);
380 struct sockaddr_in to;
382 to.sin_family = AF_INET;
383 to.sin_addr = i->ipi_addr;
386 (
struct sockaddr_storage *) &to,
sizeof(
struct sockaddr_in));
387 addr->
socket.inet.ifindex = i->ipi_ifindex;
393 if ((cmsg->cmsg_level == IPPROTO_IP) &&
394 (cmsg->cmsg_type == IP_RECVDSTADDR)) {
395 struct in_addr *i = (
struct in_addr *) CMSG_DATA(cmsg);
396 struct sockaddr_in to;
398 to.sin_family = AF_INET;
401 (
struct sockaddr_storage *) &to,
sizeof(
struct sockaddr_in));
407 if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMPNS)) {
411#elif defined(SO_TIMESTAMP)
412 if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMP)) {
427static
ssize_t fr_bio_fd_sendfromto4(
fr_bio_t *bio,
void *packet_ctx, const
void *
buffer,
size_t size)
431 struct cmsghdr *cmsg;
432 struct sockaddr_storage to;
437 memset(&my->cbuf, 0,
sizeof(my->cbuf));
438 memset(&my->msgh, 0,
sizeof(
struct msghdr));
442 my->iov = (
struct iovec) {
447 my->msgh = (
struct msghdr) {
448 .msg_control = my->cbuf,
451 .msg_namelen = to_len,
459 struct in_pktinfo *pkt;
461 my->msgh.msg_controllen = CMSG_SPACE(
sizeof(*pkt));
463 cmsg = CMSG_FIRSTHDR(&my->msgh);
464 cmsg->cmsg_level =
SOL_IP;
465 cmsg->cmsg_type = IP_PKTINFO;
466 cmsg->cmsg_len = CMSG_LEN(
sizeof(*pkt));
468 pkt = (
struct in_pktinfo *) CMSG_DATA(cmsg);
469 memset(pkt, 0,
sizeof(*pkt));
470 pkt->ipi_spec_dst = addr->
socket.inet.src_ipaddr.addr.v4;
471 pkt->ipi_ifindex = addr->
socket.inet.ifindex;
473#elif defined(IP_SENDSRCADDR)
476 my->msgh.msg_controllen = CMSG_SPACE(
sizeof(*
in));
478 cmsg = CMSG_FIRSTHDR(&my->msgh);
479 cmsg->cmsg_level = IPPROTO_IP;
480 cmsg->cmsg_type = IP_SENDSRCADDR;
481 cmsg->cmsg_len = CMSG_LEN(
sizeof(*
in));
483 in = (
struct in_addr *) CMSG_DATA(cmsg);
484 *
in = addr->
socket.inet.src_ipaddr.addr.v4;
497static inline int fr_bio_fd_udpfromto_init4(
int fd)
499 int proto = 0, flag = 0, opt = 1;
501#ifdef HAVE_IP_PKTINFO
508#elif defined(IP_RECVDSTADDR)
514 flag = IP_RECVDSTADDR;
517 return setsockopt(fd, proto, flag, &opt,
sizeof(opt));
521#if defined(IPV6_PKTINFO)
527 struct cmsghdr *cmsg;
532 rcode = fd_fd_recvfromto_common(my, packet_ctx,
buffer, size);
533 if (rcode <= 0)
return rcode;
537 for (cmsg = CMSG_FIRSTHDR(&my->msgh);
539 cmsg = CMSG_NXTHDR(&my->msgh, cmsg)) {
542 if ((cmsg->cmsg_level == IPPROTO_IPV6) &&
543 (cmsg->cmsg_type == IPV6_PKTINFO)) {
544 struct in6_pktinfo *i = (
struct in6_pktinfo *) CMSG_DATA(cmsg);
545 struct sockaddr_in6 to;
547 to.sin6_family = AF_INET6;
548 to.sin6_addr = i->ipi6_addr;
551 (
struct sockaddr_storage *) &to,
sizeof(
struct sockaddr_in6));
552 addr->
socket.inet.ifindex = i->ipi6_ifindex;
557 if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMPNS)) {
561#elif defined(SO_TIMESTAMP)
562 if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMP)) {
577static
ssize_t fr_bio_fd_sendfromto6(
fr_bio_t *bio,
void *packet_ctx, const
void *
buffer,
size_t size)
581 struct cmsghdr *cmsg;
582 struct sockaddr_storage to;
587 memset(&my->cbuf, 0,
sizeof(my->cbuf));
588 memset(&my->msgh, 0,
sizeof(
struct msghdr));
592 my->iov = (
struct iovec) {
597 my->msgh = (
struct msghdr) {
598 .msg_control = my->cbuf,
601 .msg_namelen = to_len,
608 struct in6_pktinfo *pkt;
610 my->msgh.msg_controllen = CMSG_SPACE(
sizeof(*pkt));
612 cmsg = CMSG_FIRSTHDR(&my->msgh);
613 cmsg->cmsg_level = IPPROTO_IPV6;
614 cmsg->cmsg_type = IPV6_PKTINFO;
615 cmsg->cmsg_len = CMSG_LEN(
sizeof(*pkt));
617 pkt = (
struct in6_pktinfo *) CMSG_DATA(cmsg);
618 memset(pkt, 0,
sizeof(*pkt));
619 pkt->ipi6_addr = addr->
socket.inet.src_ipaddr.addr.v6;
620 pkt->ipi6_ifindex = addr->
socket.inet.ifindex;
633static inline int fr_bio_fd_udpfromto_init6(
int fd)
637 return setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &opt,
sizeof(opt));
645 len = strlen(filename);
646 if (len >=
sizeof(sun->sun_path)) {
647 fr_strerror_const(
"Failed parsing unix domain socket filename: Name is too long");
651 sun->sun_family = AF_LOCAL;
652 memcpy(sun->sun_path, filename, len + 1);
654 *sunlen = SUN_LEN(sun);
662 struct sockaddr_storage salocal;
677 salen =
sizeof(salocal);
678 memset(&salocal, 0, salen);
679 if (getsockname(my->
info.
socket.
fd, (
struct sockaddr *) &salocal, &salen) < 0) {
702 if (my->cb.connected) my->cb.connected(&my->bio);
750 if (tries <= my->max_tries)
goto retry;
768 if (rcode < 0)
return rcode;
793 switch (my->
info.
cfg->
flags & (O_RDONLY | O_WRONLY | O_RDWR)) {
857 setsockopt(my->
info.
socket.
fd, SOL_SOCKET, SO_NOSIGPIPE, &on,
sizeof(on));
867 if (rcode == 0)
return 0;
869 if (rcode !=
fr_bio_error(IO_WOULD_BLOCK))
return rcode;
894#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
895 }
else if (my->
info.
socket.inet.src_ipaddr.
af == AF_INET) {
896 if (fr_bio_fd_udpfromto_init4(my->
info.
socket.
fd) < 0)
return -1;
898 my->bio.read = fr_bio_fd_recvfromto4;
899 my->bio.write = fr_bio_fd_sendfromto4;
902#if defined(IPV6_PKTINFO)
903 }
else if (my->
info.
socket.inet.src_ipaddr.
af == AF_INET6) {
905 if (fr_bio_fd_udpfromto_init6(my->
info.
socket.
fd) < 0)
return -1;
907 my->bio.read = fr_bio_fd_recvfromto6;
908 my->bio.write = fr_bio_fd_sendfromto6;
976 if (!my)
return NULL;
991 .read_blocked =
false,
992 .write_blocked =
false,
1052 if (tries < my->max_tries)
goto retry;
1113 socklen_t socklen =
sizeof(error);
1128 if (getsockopt(my->
info.
socket.
fd, SOL_SOCKET, SO_ERROR, (
void *)&error, &socklen) < 0) {
1176 my->
connect.timeout(&my->bio);
1214 if (error_cb) error_cb(bio);
1225 if (connected_cb) connected_cb(bio);
1247 if (!connected_cb) {
1252 if (error_cb) error_cb(bio);
1267 my->
connect.success = connected_cb;
1269 my->
connect.timeout = timeout_cb;
1313 if (rcode >= 0)
return 0;
1329 if (rcode > 0 )
return 0;
1355 goto set_recv_buff_zero;
1376 if (setsockopt(my->
info.
socket.
fd, SOL_SOCKET, SO_RCVBUF, &opt,
sizeof(opt)) < 0) {
1398#if !defined(__linux__) && !defined(__FreeBSD__)
1399static int inline accept4(
int fd,
struct sockaddr *sockaddr, socklen_t *salen,
UNUSED int flags)
1401 fd = accept(fd, sockaddr, salen);
1411#ifndef SOCK_NONBLOCK
1412#define SOCK_NONBLOCK 0
1416#define SOCK_CLOEXEC 0
1440 if (errno == EINTR) {
1442 if (tries <= my->max_tries)
goto retry;
static int const char char buffer[256]
fr_bio_write_t _CONST write
write to the underlying bio
fr_bio_read_t _CONST read
read from the underlying bio
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 fr_bio_t * fr_bio_head(fr_bio_t *bio)
void(* fr_bio_callback_t)(fr_bio_t *bio)
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
#define fr_event_fd_insert(...)
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
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()
static ssize_t fr_bio_fd_write(fr_bio_t *bio, UNUSED void *packet_ctx, const void *buffer, size_t size)
Write to fd.
int fr_bio_fd_socket_name(fr_bio_fd_t *my)
static void fr_bio_fd_el_connect(NDEBUG_UNUSED fr_event_list_t *el, NDEBUG_UNUSED int fd, NDEBUG_UNUSED int flags, void *uctx)
Connect callback for when the socket is writable.
static int fr_bio_fd_write_resume(fr_bio_t *bio)
fr_bio_t * fr_bio_fd_alloc(TALLOC_CTX *ctx, fr_bio_fd_config_t const *cfg, size_t offset)
Allocate a FD bio.
static ssize_t fr_bio_fd_read_discard_stream(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Discard all reads from a TCP socket.
static int accept4(int fd, struct sockaddr *sockaddr, socklen_t *salen, UNUSED int flags)
int fr_bio_fd_init_connected(fr_bio_fd_t *my)
static ssize_t fr_bio_fd_read_connected_datagram(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Connected datagram read.
static ssize_t fr_bio_fd_try_connect(fr_bio_fd_t *my)
Try to connect().
static ssize_t fr_bio_fd_recvfrom(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Read from a UDP socket where we know our IP.
static int fr_bio_fd_eof(fr_bio_t *bio)
static ssize_t fr_bio_fd_sendto(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size)
Write to a UDP socket where we know our IP.
int fr_filename_to_sockaddr(struct sockaddr_un *sun, socklen_t *sunlen, char const *filename)
int fr_bio_fd_init_listen(fr_bio_fd_t *my)
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.
static int fr_bio_fd_shutdown(fr_bio_t *bio)
Orderly shutdown.
static int fr_bio_fd_accept_stream(fr_bio_fd_t *my, fr_bio_fd_t const *parent)
Accept a stream socket and initialize its flags.
static int fr_bio_fd_init_file(fr_bio_fd_t *my)
Files are a special case of connected sockets.
static void fr_bio_fd_el_timeout(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
We have a timeout on the conenction.
int fr_bio_fd_init_common(fr_bio_fd_t *my)
int fr_bio_fd_write_only(fr_bio_t *bio)
Mark up a bio as write-only.
static ssize_t fr_bio_fd_read_discard_datagram(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Discard all reads from a UDP socket.
static void fr_bio_fd_el_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, int fd_errno, void *uctx)
FD error when trying to connect, give up on the BIO.
int fr_bio_fd_close(fr_bio_t *bio)
Close the FD, but leave the bio allocated and alive.
static void fr_bio_fd_set_open(fr_bio_fd_t *my)
static ssize_t fr_bio_fd_read_stream(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Stream read.
fr_socket_t socket
as connected socket
char const * name
printable name of this BIO
@ FR_BIO_FD_CONNECTED
connected client sockets (UDP or TCP)
@ FR_BIO_FD_INVALID
not set
@ FR_BIO_FD_UNCONNECTED
unconnected UDP / datagram only
@ FR_BIO_FD_LISTEN
returns new fd in buffer on fr_bio_read() or fr_bio_fd_accept()
fr_bio_fd_type_t type
type of the socket
fr_bio_fd_state_t state
connecting, open, closed, etc.
@ FR_BIO_FD_STATE_CONNECTING
@ FR_BIO_FD_STATE_OPEN
error states must be before this
int connect_errno
from connect() or other APIs
fr_bio_fd_config_t const * cfg
so we know what was asked, vs what was granted.
bool read_blocked
did we block on read?
uint32_t backlog
for listen()
int fr_bio_fd_open(fr_bio_t *bio, fr_bio_fd_config_t const *cfg)
Opens a socket and updates sock->fd.
bool backlog_is_set
Whether we were provided with a backlog.
fr_socket_t socket
socket information, including FD.
bool write_blocked
did we block on write?
Configuration for sockets.
Run-time status of the socket.
static bool fr_bio_fd_errno_retry(fr_bio_fd_t *my, ssize_t *rcode_p, int *tries_p, bool is_write)
Turn a failed read / write into a bio return code.
void fr_bio_fd_name(fr_bio_fd_t *my)
Set the name of an FD BIO.
socklen_t remote_sockaddr_len
size_t offset
where fr_bio_fd_packet_ctx_t is stored
struct fr_bio_fd_s::@19 connect
int max_tries
how many times we retry on EINTR
#define fr_bio_fd_packet_ctx(_my, _packet_ctx)
struct sockaddr_storage remote_sockaddr
for connected UDP sockets.
static bool fr_bio_fd_read_retry(fr_bio_fd_t *my, ssize_t *rcode_p, int *tries_p)
Common finalization code for the read functions.
static bool fr_bio_fd_write_retry(fr_bio_fd_t *my, ssize_t *rcode_p, int *tries_p, size_t size)
Common finalization code for the write functions.
void fr_ipaddr_get_scope_id(fr_ipaddr_t *ipaddr)
int fr_ipaddr_from_sockaddr(fr_ipaddr_t *ipaddr, uint16_t *port, struct sockaddr_storage const *sa, socklen_t salen)
Convert sockaddr to our internal ip address representation.
int fr_ipaddr_is_inaddr_any(fr_ipaddr_t const *ipaddr)
Determine if an address is the INADDR_ANY address for its address family.
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.
int fr_bio_write_blocked(fr_bio_t *bio)
Internal BIO function to tell all BIOs that it's blocked.
ssize_t fr_bio_shutdown_read(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void *buffer, UNUSED size_t size)
void fr_bio_eof(fr_bio_t *bio)
Internal BIO function to run EOF callbacks.
int fr_bio_destructor(fr_bio_t *bio)
Free this bio.
ssize_t fr_bio_shutdown_write(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void const *buffer, UNUSED size_t size)
int fr_bio_shutdown(fr_bio_t *bio)
Shut down a set of BIOs.
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Stores all information relating to an event list.
int fr_nonblock(UNUSED int fd)
int fr_cloexec(UNUSED int fd)
ssize_t fr_bio_null_write(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void const *buffer, UNUSED size_t size)
Always return 0 on write.
ssize_t fr_bio_null_read(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void *buffer, UNUSED size_t size)
Always return 0 on read.
ssize_t fr_bio_fail_read(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void *buffer, UNUSED size_t size)
Always return error on read.
ssize_t fr_bio_fail_write(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void const *buffer, UNUSED size_t size)
Always return an error on write.
static int compare(const void *a, const void *b)
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
static fr_time_t fr_time_from_timeval(struct timeval const *when_tv)
Convert a timeval (wallclock time) to a fr_time_t (internal time)
#define fr_time_wrap(_time)
#define fr_time_eq(_a, _b)
static fr_time_t fr_time_from_timespec(struct timespec const *when_ts)
Convert a timespec (wallclock time) to a fr_time_t (internal time)
A time delta, a difference in time measured in nanoseconds.
#define FR_TIMER_DELETE(_ev_p)
static fr_event_list_t * el
int af
AF_INET, AF_INET6, or AF_UNIX.
int fd
File descriptor if this is a live socket.
int type
SOCK_STREAM, SOCK_DGRAM, etc.
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
#define fr_strerror_const(_msg)
static size_t char ** out