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; \
103 if (
my->connect.ev) {
105 my->connect.ev = NULL;
108 if (
my->connect.el) {
110 my->connect.el = NULL;
113 if (
my->cb.shutdown)
my->cb.shutdown(&
my->bio);
137 my->info.write_blocked =
false;
154 rcode = read(
my->info.socket.fd,
buffer, size);
179 rcode = read(
my->info.socket.fd,
buffer, size);
180 if (rcode == 0)
return rcode;
195 struct sockaddr_storage sockaddr;
198 salen =
sizeof(sockaddr);
200 rcode = recvfrom(
my->info.socket.fd,
buffer, size, 0, (
struct sockaddr *) &sockaddr, &salen);
206 addr->
socket.inet.dst_ipaddr =
my->info.socket.inet.src_ipaddr;
207 addr->
socket.inet.dst_port =
my->info.socket.inet.src_port;
213 if (rcode == 0)
return rcode;
250 rcode = write(
my->info.socket.fd,
buffer, size);
267 struct sockaddr_storage sockaddr;
278 rcode = sendto(
my->info.socket.fd,
buffer, size, 0, (
struct sockaddr *) &sockaddr, salen);
286#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) || defined(IPV6_PKTINFO)
291 struct sockaddr_storage from;
294#ifdef STATIC_ANALYZER
295 from.ss_family = AF_UNSPEC;
298 memset(&
my->cbuf, 0,
sizeof(
my->cbuf));
299 memset(&
my->msgh, 0,
sizeof(
struct msghdr));
301 my->iov = (
struct iovec) {
306 my->msgh = (
struct msghdr) {
307 .msg_control =
my->cbuf,
308 .msg_controllen =
sizeof(
my->cbuf),
310 .msg_namelen =
sizeof(from),
317 rcode = recvmsg(
my->info.socket.fd, &
my->msgh, 0);
322 &from,
my->msgh.msg_namelen);
325 if (rcode == 0)
return rcode;
333#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
340 struct cmsghdr *cmsg;
345 rcode = fd_fd_recvfromto_common(
my, packet_ctx,
buffer, size);
346 if (rcode <= 0)
return rcode;
350 for (cmsg = CMSG_FIRSTHDR(&
my->msgh);
352 cmsg = CMSG_NXTHDR(&
my->msgh, cmsg)) {
356 if ((cmsg->cmsg_level ==
SOL_IP) &&
357 (cmsg->cmsg_type == IP_PKTINFO)) {
358 struct in_pktinfo *i = (
struct in_pktinfo *) CMSG_DATA(cmsg);
359 struct sockaddr_in to;
361 to.sin_addr = i->ipi_addr;
364 (
struct sockaddr_storage *) &to,
sizeof(
struct sockaddr_in));
365 addr->
socket.inet.ifindex = i->ipi_ifindex;
371 if ((cmsg->cmsg_level == IPPROTO_IP) &&
372 (cmsg->cmsg_type == IP_RECVDSTADDR)) {
373 struct in_addr *i = (
struct in_addr *) CMSG_DATA(cmsg);
374 struct sockaddr_in to;
378 (
struct sockaddr_storage *) &to,
sizeof(
struct sockaddr_in));
384 if ((cmsg->cmsg_level ==
SOL_IP) && (cmsg->cmsg_type == SO_TIMESTAMPNS)) {
388#elif defined(SO_TIMESTAMP)
389 if ((cmsg->cmsg_level ==
SOL_IP) && (cmsg->cmsg_type == SO_TIMESTAMP)) {
404static
ssize_t fr_bio_fd_sendfromto4(
fr_bio_t *bio,
void *packet_ctx, const
void *
buffer,
size_t size)
408 struct cmsghdr *cmsg;
409 struct sockaddr_storage to;
414 memset(&
my->cbuf, 0,
sizeof(
my->cbuf));
415 memset(&
my->msgh, 0,
sizeof(
struct msghdr));
419 my->iov = (
struct iovec) {
424 my->msgh = (
struct msghdr) {
425 .msg_control =
my->cbuf,
428 .msg_namelen = to_len,
436 struct in_pktinfo *pkt;
438 my->msgh.msg_controllen = CMSG_SPACE(
sizeof(*pkt));
440 cmsg = CMSG_FIRSTHDR(&
my->msgh);
441 cmsg->cmsg_level =
SOL_IP;
442 cmsg->cmsg_type = IP_PKTINFO;
443 cmsg->cmsg_len = CMSG_LEN(
sizeof(*pkt));
445 pkt = (
struct in_pktinfo *) CMSG_DATA(cmsg);
446 memset(pkt, 0,
sizeof(*pkt));
447 pkt->ipi_spec_dst = addr->
socket.inet.src_ipaddr.addr.v4;
448 pkt->ipi_ifindex = addr->
socket.inet.ifindex;
450#elif defined(IP_SENDSRCADDR)
453 my->msgh.msg_controllen = CMSG_SPACE(
sizeof(*
in));
455 cmsg = CMSG_FIRSTHDR(&
my->msgh);
456 cmsg->cmsg_level = IPPROTO_IP;
457 cmsg->cmsg_type = IP_SENDSRCADDR;
458 cmsg->cmsg_len = CMSG_LEN(
sizeof(*
in));
460 in = (
struct in_addr *) CMSG_DATA(cmsg);
461 *
in = addr->
socket.inet.src_ipaddr.addr.v4;
466 rcode = sendmsg(
my->info.socket.fd, &
my->msgh, 0);
473static inline int fr_bio_fd_udpfromto_init4(
int fd)
475 int proto = 0, flag = 0, opt = 1;
477#ifdef HAVE_IP_PKTINFO
484#elif defined(IP_RECVDSTADDR)
490 flag = IP_RECVDSTADDR;
493 return setsockopt(fd,
proto, flag, &opt,
sizeof(opt));
497#if defined(IPV6_PKTINFO)
503 struct cmsghdr *cmsg;
508 rcode = fd_fd_recvfromto_common(
my, packet_ctx,
buffer, size);
509 if (rcode <= 0)
return rcode;
513 for (cmsg = CMSG_FIRSTHDR(&
my->msgh);
515 cmsg = CMSG_NXTHDR(&
my->msgh, cmsg)) {
518 if ((cmsg->cmsg_level == IPPROTO_IPV6) &&
519 (cmsg->cmsg_type == IPV6_PKTINFO)) {
520 struct in6_pktinfo *i = (
struct in6_pktinfo *) CMSG_DATA(cmsg);
521 struct sockaddr_in6 to;
523 to.sin6_addr = i->ipi6_addr;
526 (
struct sockaddr_storage *) &to,
sizeof(
struct sockaddr_in6));
527 addr->
socket.inet.ifindex = i->ipi6_ifindex;
532 if ((cmsg->cmsg_level ==
SOL_IP) && (cmsg->cmsg_type == SO_TIMESTAMPNS)) {
536#elif defined(SO_TIMESTAMP)
537 if ((cmsg->cmsg_level ==
SOL_IP) && (cmsg->cmsg_type == SO_TIMESTAMP)) {
552static
ssize_t fr_bio_fd_sendfromto6(
fr_bio_t *bio,
void *packet_ctx, const
void *
buffer,
size_t size)
556 struct cmsghdr *cmsg;
557 struct sockaddr_storage to;
562 memset(&
my->cbuf, 0,
sizeof(
my->cbuf));
563 memset(&
my->msgh, 0,
sizeof(
struct msghdr));
567 my->iov = (
struct iovec) {
572 my->msgh = (
struct msghdr) {
573 .msg_control =
my->cbuf,
576 .msg_namelen = to_len,
583 struct in6_pktinfo *pkt;
585 my->msgh.msg_controllen = CMSG_SPACE(
sizeof(*pkt));
587 cmsg = CMSG_FIRSTHDR(&
my->msgh);
588 cmsg->cmsg_level = IPPROTO_IPV6;
589 cmsg->cmsg_type = IPV6_PKTINFO;
590 cmsg->cmsg_len = CMSG_LEN(
sizeof(*pkt));
592 pkt = (
struct in6_pktinfo *) CMSG_DATA(cmsg);
593 memset(pkt, 0,
sizeof(*pkt));
594 pkt->ipi6_addr = addr->
socket.inet.src_ipaddr.addr.v6;
595 pkt->ipi6_ifindex = addr->
socket.inet.ifindex;
599 rcode = sendmsg(
my->info.socket.fd, &
my->msgh, 0);
607static inline int fr_bio_fd_udpfromto_init6(
int fd)
611 return setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &opt,
sizeof(opt));
619 len = strlen(filename);
620 if (len >=
sizeof(sun->sun_path)) {
621 fr_strerror_const(
"Failed parsing unix domain socket filename: Name is too long");
625 sun->sun_family = AF_LOCAL;
626 memcpy(sun->sun_path, filename, len + 1);
628 *sunlen = SUN_LEN(sun);
636 struct sockaddr_storage salocal;
642 (
my->info.socket.inet.src_port != 0)) {
651 salen =
sizeof(salocal);
652 memset(&salocal, 0, salen);
653 if (getsockname(
my->info.socket.fd, (
struct sockaddr *) &salocal, &salen) < 0) {
661 my->info.socket.inet.ifindex =
my->info.socket.inet.src_ipaddr.scope_id;
669 my->info.eof =
false;
670 my->info.read_blocked =
false;
671 my->info.write_blocked =
false;
676 if (
my->cb.connected)
my->cb.connected(&
my->bio);
690 struct sockaddr_storage sockaddr;
692 if (
my->info.socket.af != AF_LOCAL) {
706 if (connect(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, salen) == 0) {
722 if (tries <= my->max_tries)
goto retry;
736 if (!
my->info.write_blocked) {
737 my->info.write_blocked =
true;
740 if (rcode < 0)
return rcode;
765 switch (
my->info.cfg->flags & (O_RDONLY | O_WRONLY | O_RDWR)) {
813 my->info.eof =
false;
815 my->info.read_blocked =
false;
816 my->info.write_blocked =
false;
829 setsockopt(
my->info.socket.fd, SOL_SOCKET, SO_NOSIGPIPE, &on,
sizeof(on));
836 if (!
my->info.cfg->async)
return 0;
839 if (rcode == 0)
return 0;
841 if (rcode !=
fr_bio_error(IO_WOULD_BLOCK))
return rcode;
854 if (
my->info.socket.type == SOCK_STREAM) {
866#if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
867 }
else if (
my->info.socket.inet.src_ipaddr.af == AF_INET) {
868 if (fr_bio_fd_udpfromto_init4(
my->info.socket.fd) < 0)
return -1;
870 my->bio.read = fr_bio_fd_recvfromto4;
871 my->bio.write = fr_bio_fd_sendfromto4;
874#if defined(IPV6_PKTINFO)
875 }
else if (
my->info.socket.inet.src_ipaddr.af == AF_INET6) {
877 if (fr_bio_fd_udpfromto_init6(
my->info.socket.fd) < 0)
return -1;
879 my->bio.read = fr_bio_fd_recvfromto6;
880 my->bio.write = fr_bio_fd_sendfromto6;
902 struct sockaddr_storage sockaddr;
904 if (size <
sizeof(
int))
return fr_bio_error(BUFFER_TOO_SMALL);
906 salen =
sizeof(sockaddr);
913 fd = accept4(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, &salen, SOCK_NONBLOCK | SOCK_CLOEXEC);
915 fd = accept(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, &salen);
925 addr->
socket.inet.dst_ipaddr =
my->info.socket.inet.src_ipaddr;
926 addr->
socket.inet.dst_port =
my->info.socket.inet.src_port;
939 if (tries <= my->max_tries)
goto retry;
946#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
975 if (listen(
my->info.socket.fd, 8) < 0) {
1023 if (!
my)
return NULL;
1026 my->offset = offset;
1037 .read_blocked =
false,
1038 .write_blocked =
false,
1076 if (rcode < 0)
return rcode;
1093 (void) shutdown(
my->info.socket.fd, SHUT_RDWR);
1097 rcode =
close(
my->info.socket.fd);
1103 if (tries < my->max_tries)
goto retry;
1115 my->info.read_blocked =
true;
1116 my->info.write_blocked =
true;
1117 my->info.eof =
true;
1129 my->info.connect_errno = fd_errno;
1131 if (
my->connect.error) {
1132 my->connect.error(&
my->bio);
1159 socklen_t socklen =
sizeof(error);
1174 if (getsockopt(
my->info.socket.fd, SOL_SOCKET, SO_ERROR, (
void *)&error, &socklen) < 0) {
1198 if (
my->connect.ev) {
1200 my->connect.ev = NULL;
1202 my->connect.el = NULL;
1207 my->connect.success(&
my->bio);
1219 my->connect.timeout(&
my->bio);
1253 my->info.connect_errno = ECONNABORTED;
1255 my->info.connect_errno = ECONNREFUSED;
1257 if (error_cb) error_cb(bio);
1268 if (connected_cb) connected_cb(bio);
1290 if (!connected_cb) {
1295 if (error_cb) error_cb(bio);
1310 my->connect.success = connected_cb;
1311 my->connect.error = error_cb;
1312 my->connect.timeout = timeout_cb;
1330 my->connect.el =
el;
1355 rcode = read(
my->info.socket.fd,
buffer, size);
1356 if (rcode >= 0)
return 0;
1359#define flag_blocked read_blocked
1372 switch (
my->info.type) {
1377 if (
my->info.socket.type != SOCK_DGRAM) {
1381 goto set_recv_buff_zero;
1388 if (shutdown(
my->info.socket.fd, SHUT_RD) < 0) {
1403 if (setsockopt(
my->info.socket.fd, SOL_SOCKET, SO_RCVBUF, &opt,
sizeof(opt)) < 0) {
1434 struct sockaddr_storage sockaddr;
1438 salen =
sizeof(sockaddr);
1449 fd = accept4(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, &salen, SOCK_NONBLOCK | SOCK_CLOEXEC);
1451 fd = accept(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, &salen);
1460 if (tries <= my->max_tries)
goto retry;
1467#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1502 cfg = talloc_memdup(
out,
my->info.cfg,
sizeof(*
my->info.cfg));
1514 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/.
#define fr_event_timer_in(...)
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 void fr_bio_fd_el_timeout(UNUSED fr_event_list_t *el, UNUSED fr_time_t now, void *uctx)
We have a timeout on the conenction.
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 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.
static char const * proto(int id, int porttype)
#define fr_time()
Allow us to arbitrarily manipulate time.
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
static int talloc_const_free(void const *ptr)
Free const'd memory.
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.
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