25#include <freeradius-devel/bio/fd_priv.h>
26#include <freeradius-devel/bio/null.h>
33# define SOL_IP IPPROTO_IP
47# ifdef IPV6_RECVPKTINFO
48# include <linux/version.h>
49# if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
50# ifdef IPV6_2292PKTINFO
51# undef IPV6_RECVPKTINFO
53# define IPV6_RECVPKTINFO IPV6_2292PKTINFO
54# define IPV6_PKTINFO IPV6_2292PKTINFO
58# elif defined(IPV6_2292PKTINFO)
59# define IPV6_RECVPKTINFO IPV6_2292PKTINFO
72# if !defined(IPV6_RECVPKTINFO) && defined(IPV6_PKTINFO)
73# define IPV6_RECVPKTINFO IPV6_PKTINFO
79# elif !defined(IPV6_PKTINFO)
80# undef IPV6_RECVPKTINFO
84#define ADDR_INIT do { \
85 addr->when = fr_time(); \
86 addr->socket.type = my->info.socket.type; \
87 addr->socket.fd = -1; \
88 addr->socket.inet.ifindex = my->info.socket.inet.ifindex; \
104 if (
my->connect.el) {
106 my->connect.el = NULL;
109 if (
my->cb.shutdown)
my->cb.shutdown(&
my->bio);
133 my->info.write_blocked =
false;
150 rcode = read(
my->info.socket.fd,
buffer, size);
187 rcode = read(
my->info.socket.fd,
buffer, size);
188 if (rcode == 0)
return rcode;
203 struct sockaddr_storage sockaddr;
206 salen =
sizeof(sockaddr);
208 rcode = recvfrom(
my->info.socket.fd,
buffer, size, 0, (
struct sockaddr *) &sockaddr, &salen);
214 addr->
socket.inet.dst_ipaddr =
my->info.socket.inet.src_ipaddr;
215 addr->
socket.inet.dst_port =
my->info.socket.inet.src_port;
221 if (rcode == 0)
return rcode;
258 rcode = write(
my->info.socket.fd,
buffer, size);
275 struct sockaddr_storage sockaddr;
286 rcode = sendto(
my->info.socket.fd,
buffer, size, 0, (
struct sockaddr *) &sockaddr, salen);
294#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) || defined(IPV6_PKTINFO)
299 struct sockaddr_storage from;
302#ifdef STATIC_ANALYZER
303 from.ss_family = AF_UNSPEC;
306 memset(&
my->cbuf, 0,
sizeof(
my->cbuf));
307 memset(&
my->msgh, 0,
sizeof(
struct msghdr));
309 my->iov = (
struct iovec) {
314 my->msgh = (
struct msghdr) {
315 .msg_control =
my->cbuf,
316 .msg_controllen =
sizeof(
my->cbuf),
318 .msg_namelen =
sizeof(from),
325 rcode = recvmsg(
my->info.socket.fd, &
my->msgh, 0);
330 &from,
my->msgh.msg_namelen);
333 if (rcode == 0)
return rcode;
341#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
348 struct cmsghdr *cmsg;
353 rcode = fd_fd_recvfromto_common(
my, packet_ctx,
buffer, size);
354 if (rcode <= 0)
return rcode;
358 for (cmsg = CMSG_FIRSTHDR(&
my->msgh);
360 cmsg = CMSG_NXTHDR(&
my->msgh, cmsg)) {
364 if ((cmsg->cmsg_level ==
SOL_IP) &&
365 (cmsg->cmsg_type == IP_PKTINFO)) {
366 struct in_pktinfo *i = (
struct in_pktinfo *) CMSG_DATA(cmsg);
367 struct sockaddr_in to;
369 to.sin_addr = i->ipi_addr;
372 (
struct sockaddr_storage *) &to,
sizeof(
struct sockaddr_in));
373 addr->
socket.inet.ifindex = i->ipi_ifindex;
379 if ((cmsg->cmsg_level == IPPROTO_IP) &&
380 (cmsg->cmsg_type == IP_RECVDSTADDR)) {
381 struct in_addr *i = (
struct in_addr *) CMSG_DATA(cmsg);
382 struct sockaddr_in to;
386 (
struct sockaddr_storage *) &to,
sizeof(
struct sockaddr_in));
392 if ((cmsg->cmsg_level ==
SOL_IP) && (cmsg->cmsg_type == SO_TIMESTAMPNS)) {
396#elif defined(SO_TIMESTAMP)
397 if ((cmsg->cmsg_level ==
SOL_IP) && (cmsg->cmsg_type == SO_TIMESTAMP)) {
412static
ssize_t fr_bio_fd_sendfromto4(
fr_bio_t *bio,
void *packet_ctx, const
void *
buffer,
size_t size)
416 struct cmsghdr *cmsg;
417 struct sockaddr_storage to;
422 memset(&
my->cbuf, 0,
sizeof(
my->cbuf));
423 memset(&
my->msgh, 0,
sizeof(
struct msghdr));
427 my->iov = (
struct iovec) {
432 my->msgh = (
struct msghdr) {
433 .msg_control =
my->cbuf,
436 .msg_namelen = to_len,
444 struct in_pktinfo *pkt;
446 my->msgh.msg_controllen = CMSG_SPACE(
sizeof(*pkt));
448 cmsg = CMSG_FIRSTHDR(&
my->msgh);
449 cmsg->cmsg_level =
SOL_IP;
450 cmsg->cmsg_type = IP_PKTINFO;
451 cmsg->cmsg_len = CMSG_LEN(
sizeof(*pkt));
453 pkt = (
struct in_pktinfo *) CMSG_DATA(cmsg);
454 memset(pkt, 0,
sizeof(*pkt));
455 pkt->ipi_spec_dst = addr->
socket.inet.src_ipaddr.addr.v4;
456 pkt->ipi_ifindex = addr->
socket.inet.ifindex;
458#elif defined(IP_SENDSRCADDR)
461 my->msgh.msg_controllen = CMSG_SPACE(
sizeof(*
in));
463 cmsg = CMSG_FIRSTHDR(&
my->msgh);
464 cmsg->cmsg_level = IPPROTO_IP;
465 cmsg->cmsg_type = IP_SENDSRCADDR;
466 cmsg->cmsg_len = CMSG_LEN(
sizeof(*
in));
468 in = (
struct in_addr *) CMSG_DATA(cmsg);
469 *
in = addr->
socket.inet.src_ipaddr.addr.v4;
474 rcode = sendmsg(
my->info.socket.fd, &
my->msgh, 0);
481static inline int fr_bio_fd_udpfromto_init4(
int fd)
483 int proto = 0, flag = 0, opt = 1;
485#ifdef HAVE_IP_PKTINFO
492#elif defined(IP_RECVDSTADDR)
498 flag = IP_RECVDSTADDR;
501 return setsockopt(fd, proto, flag, &opt,
sizeof(opt));
505#if defined(IPV6_PKTINFO)
511 struct cmsghdr *cmsg;
516 rcode = fd_fd_recvfromto_common(
my, packet_ctx,
buffer, size);
517 if (rcode <= 0)
return rcode;
521 for (cmsg = CMSG_FIRSTHDR(&
my->msgh);
523 cmsg = CMSG_NXTHDR(&
my->msgh, cmsg)) {
526 if ((cmsg->cmsg_level == IPPROTO_IPV6) &&
527 (cmsg->cmsg_type == IPV6_PKTINFO)) {
528 struct in6_pktinfo *i = (
struct in6_pktinfo *) CMSG_DATA(cmsg);
529 struct sockaddr_in6 to;
531 to.sin6_addr = i->ipi6_addr;
534 (
struct sockaddr_storage *) &to,
sizeof(
struct sockaddr_in6));
535 addr->
socket.inet.ifindex = i->ipi6_ifindex;
540 if ((cmsg->cmsg_level ==
SOL_IP) && (cmsg->cmsg_type == SO_TIMESTAMPNS)) {
544#elif defined(SO_TIMESTAMP)
545 if ((cmsg->cmsg_level ==
SOL_IP) && (cmsg->cmsg_type == SO_TIMESTAMP)) {
560static
ssize_t fr_bio_fd_sendfromto6(
fr_bio_t *bio,
void *packet_ctx, const
void *
buffer,
size_t size)
564 struct cmsghdr *cmsg;
565 struct sockaddr_storage to;
570 memset(&
my->cbuf, 0,
sizeof(
my->cbuf));
571 memset(&
my->msgh, 0,
sizeof(
struct msghdr));
575 my->iov = (
struct iovec) {
580 my->msgh = (
struct msghdr) {
581 .msg_control =
my->cbuf,
584 .msg_namelen = to_len,
591 struct in6_pktinfo *pkt;
593 my->msgh.msg_controllen = CMSG_SPACE(
sizeof(*pkt));
595 cmsg = CMSG_FIRSTHDR(&
my->msgh);
596 cmsg->cmsg_level = IPPROTO_IPV6;
597 cmsg->cmsg_type = IPV6_PKTINFO;
598 cmsg->cmsg_len = CMSG_LEN(
sizeof(*pkt));
600 pkt = (
struct in6_pktinfo *) CMSG_DATA(cmsg);
601 memset(pkt, 0,
sizeof(*pkt));
602 pkt->ipi6_addr = addr->
socket.inet.src_ipaddr.addr.v6;
603 pkt->ipi6_ifindex = addr->
socket.inet.ifindex;
607 rcode = sendmsg(
my->info.socket.fd, &
my->msgh, 0);
615static inline int fr_bio_fd_udpfromto_init6(
int fd)
619 return setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &opt,
sizeof(opt));
627 len = strlen(filename);
628 if (len >=
sizeof(sun->sun_path)) {
629 fr_strerror_const(
"Failed parsing unix domain socket filename: Name is too long");
633 sun->sun_family = AF_LOCAL;
634 memcpy(sun->sun_path, filename, len + 1);
636 *sunlen = SUN_LEN(sun);
644 struct sockaddr_storage salocal;
650 (
my->info.socket.inet.src_port != 0)) {
659 salen =
sizeof(salocal);
660 memset(&salocal, 0, salen);
661 if (getsockname(
my->info.socket.fd, (
struct sockaddr *) &salocal, &salen) < 0) {
669 my->info.socket.inet.ifindex =
my->info.socket.inet.src_ipaddr.scope_id;
677 my->info.eof =
false;
678 my->info.read_blocked =
false;
679 my->info.write_blocked =
false;
684 if (
my->cb.connected)
my->cb.connected(&
my->bio);
698 struct sockaddr_storage sockaddr;
700 if (
my->info.socket.af != AF_LOCAL) {
714 if (connect(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, salen) == 0) {
730 if (tries <= my->max_tries)
goto retry;
744 if (!
my->info.write_blocked) {
745 my->info.write_blocked =
true;
748 if (rcode < 0)
return rcode;
773 switch (
my->info.cfg->flags & (O_RDONLY | O_WRONLY | O_RDWR)) {
821 my->info.eof =
false;
823 my->info.read_blocked =
false;
824 my->info.write_blocked =
false;
837 setsockopt(
my->info.socket.fd, SOL_SOCKET, SO_NOSIGPIPE, &on,
sizeof(on));
844 if (!
my->info.cfg->async)
return 0;
847 if (rcode == 0)
return 0;
849 if (rcode !=
fr_bio_error(IO_WOULD_BLOCK))
return rcode;
862 if (
my->info.socket.type == SOCK_STREAM) {
874#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
875 }
else if (
my->info.socket.inet.src_ipaddr.af == AF_INET) {
876 if (fr_bio_fd_udpfromto_init4(
my->info.socket.fd) < 0)
return -1;
878 my->bio.read = fr_bio_fd_recvfromto4;
879 my->bio.write = fr_bio_fd_sendfromto4;
882#if defined(IPV6_PKTINFO)
883 }
else if (
my->info.socket.inet.src_ipaddr.af == AF_INET6) {
885 if (fr_bio_fd_udpfromto_init6(
my->info.socket.fd) < 0)
return -1;
887 my->bio.read = fr_bio_fd_recvfromto6;
888 my->bio.write = fr_bio_fd_sendfromto6;
910 struct sockaddr_storage sockaddr;
912 if (size <
sizeof(
int))
return fr_bio_error(BUFFER_TOO_SMALL);
914 salen =
sizeof(sockaddr);
921 fd = accept4(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, &salen, SOCK_NONBLOCK | SOCK_CLOEXEC);
923 fd = accept(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, &salen);
933 addr->
socket.inet.dst_ipaddr =
my->info.socket.inet.src_ipaddr;
934 addr->
socket.inet.dst_port =
my->info.socket.inet.src_port;
947 if (tries <= my->max_tries)
goto retry;
954#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
983 if (listen(
my->info.socket.fd, 8) < 0) {
1031 if (!
my)
return NULL;
1034 my->offset = offset;
1045 .read_blocked =
false,
1046 .write_blocked =
false,
1084 if (rcode < 0)
return rcode;
1101 (void) shutdown(
my->info.socket.fd, SHUT_RDWR);
1105 rcode =
close(
my->info.socket.fd);
1111 if (tries < my->max_tries)
goto retry;
1123 my->info.read_blocked =
true;
1124 my->info.write_blocked =
true;
1125 my->info.eof =
true;
1137 my->info.connect_errno = fd_errno;
1139 if (
my->connect.error) {
1140 my->connect.error(&
my->bio);
1167 socklen_t socklen =
sizeof(error);
1182 if (getsockopt(
my->info.socket.fd, SOL_SOCKET, SO_ERROR, (
void *)&error, &socklen) < 0) {
1207 my->connect.el = NULL;
1212 my->connect.success(&
my->bio);
1224 my->connect.timeout(&
my->bio);
1258 my->info.connect_errno = ECONNABORTED;
1260 my->info.connect_errno = ECONNREFUSED;
1262 if (error_cb) error_cb(bio);
1273 if (connected_cb) connected_cb(bio);
1295 if (!connected_cb) {
1300 if (error_cb) error_cb(bio);
1315 my->connect.success = connected_cb;
1316 my->connect.error = error_cb;
1317 my->connect.timeout = timeout_cb;
1335 my->connect.el =
el;
1360 rcode = read(
my->info.socket.fd,
buffer, size);
1361 if (rcode >= 0)
return 0;
1364#define flag_blocked read_blocked
1377 switch (
my->info.type) {
1382 if (
my->info.socket.type != SOCK_DGRAM) {
1386 goto set_recv_buff_zero;
1393 if (shutdown(
my->info.socket.fd, SHUT_RD) < 0) {
1408 if (setsockopt(
my->info.socket.fd, SOL_SOCKET, SO_RCVBUF, &opt,
sizeof(opt)) < 0) {
1439 struct sockaddr_storage sockaddr;
1443 salen =
sizeof(sockaddr);
1454 fd = accept4(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, &salen, SOCK_NONBLOCK | SOCK_CLOEXEC);
1456 fd = accept(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, &salen);
1465 if (tries <= my->max_tries)
goto retry;
1472#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1507 cfg = talloc_memdup(
out,
my->info.cfg,
sizeof(*
my->info.cfg));
1519 out->info.socket.fd = fd;
static int const char char buffer[256]
fr_bio_write_t _CONST write
write to the underlying bio
static fr_bio_t * fr_bio_prev(fr_bio_t *bio)
fr_bio_read_t _CONST read
read from the underlying bio
static fr_bio_t * fr_bio_next(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_read_discard(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Discard all reads from a UDP socket.
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.
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)
Alternative to calling fr_bio_read() on new socket.
fr_bio_fd_info_t const * fr_bio_fd_info(fr_bio_t *bio)
Returns a pointer to the bio-specific information.
static ssize_t fr_bio_fd_read_accept(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Return an fd on read()
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.
static int fr_bio_fd_destructor(fr_bio_fd_t *my)
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 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_BIO_FD_ACCEPTED
temporarily until it's connected.
@ 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_STATE_CONNECTING
@ FR_BIO_FD_STATE_OPEN
error states must be before this
fr_bio_fd_type_t type
accept, connected, unconnected, etc.
int fr_bio_fd_open(fr_bio_t *bio, fr_bio_fd_config_t const *cfg)
Opens a socket and updates sock->fd.
fr_socket_t socket
socket information, including FD.
Configuration for sockets.
Run-time status of the socket.
#define fr_bio_fd_packet_ctx(_my, _packet_ctx)
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.
void fr_bio_eof(fr_bio_t *bio)
Internal BIO function to run EOF callbacks.
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.
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 0 on write.
#define fr_time()
Allow us to arbitrarily manipulate time.
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 fd
File descriptor if this is a live socket.
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
#define fr_strerror_const(_msg)
static size_t char ** out