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);
133 my->info.write_blocked =
false;
150 rcode = read(
my->info.socket.fd,
buffer, size);
175 rcode = read(
my->info.socket.fd,
buffer, size);
176 if (rcode == 0)
return rcode;
191 struct sockaddr_storage sockaddr;
194 salen =
sizeof(sockaddr);
196 rcode = recvfrom(
my->info.socket.fd,
buffer, size, 0, (
struct sockaddr *) &sockaddr, &salen);
202 addr->
socket.inet.dst_ipaddr =
my->info.socket.inet.src_ipaddr;
203 addr->
socket.inet.dst_port =
my->info.socket.inet.src_port;
209 if (rcode == 0)
return rcode;
246 rcode = write(
my->info.socket.fd,
buffer, size);
263 struct sockaddr_storage sockaddr;
274 rcode = sendto(
my->info.socket.fd,
buffer, size, 0, (
struct sockaddr *) &sockaddr, salen);
282 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR) || defined(IPV6_PKTINFO)
287 struct sockaddr_storage from;
290 #ifdef STATIC_ANALYZER
291 from.ss_family = AF_UNSPEC;
294 memset(&
my->cbuf, 0,
sizeof(
my->cbuf));
295 memset(&
my->msgh, 0,
sizeof(
struct msghdr));
297 my->iov = (
struct iovec) {
302 my->msgh = (
struct msghdr) {
303 .msg_control =
my->cbuf,
304 .msg_controllen =
sizeof(
my->cbuf),
306 .msg_namelen =
sizeof(from),
313 rcode = recvmsg(
my->info.socket.fd, &
my->msgh, 0);
318 &from,
my->msgh.msg_namelen);
321 if (rcode == 0)
return rcode;
329 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
336 struct cmsghdr *cmsg;
341 rcode = fd_fd_recvfromto_common(
my, packet_ctx,
buffer, size);
342 if (rcode <= 0)
return rcode;
346 for (cmsg = CMSG_FIRSTHDR(&
my->msgh);
348 cmsg = CMSG_NXTHDR(&
my->msgh, cmsg)) {
352 if ((cmsg->cmsg_level ==
SOL_IP) &&
353 (cmsg->cmsg_type == IP_PKTINFO)) {
354 struct in_pktinfo *i = (
struct in_pktinfo *) CMSG_DATA(cmsg);
355 struct sockaddr_in to;
357 to.sin_addr = i->ipi_addr;
360 (
struct sockaddr_storage *) &to,
sizeof(
struct sockaddr_in));
361 addr->
socket.inet.ifindex = i->ipi_ifindex;
366 #ifdef IP_RECVDSTADDR
367 if ((cmsg->cmsg_level == IPPROTO_IP) &&
368 (cmsg->cmsg_type == IP_RECVDSTADDR)) {
369 struct in_addr *i = (
struct in_addr *) CMSG_DATA(cmsg);
370 struct sockaddr_in to;
374 (
struct sockaddr_storage *) &to,
sizeof(
struct sockaddr_in));
379 #ifdef SO_TIMESTAMPNS
380 if ((cmsg->cmsg_level ==
SOL_IP) && (cmsg->cmsg_type == SO_TIMESTAMPNS)) {
384 #elif defined(SO_TIMESTAMP)
385 if ((cmsg->cmsg_level ==
SOL_IP) && (cmsg->cmsg_type == SO_TIMESTAMP)) {
400 static
ssize_t fr_bio_fd_sendfromto4(
fr_bio_t *bio,
void *packet_ctx, const
void *
buffer,
size_t size)
404 struct cmsghdr *cmsg;
405 struct sockaddr_storage to;
410 memset(&
my->cbuf, 0,
sizeof(
my->cbuf));
411 memset(&
my->msgh, 0,
sizeof(
struct msghdr));
415 my->iov = (
struct iovec) {
420 my->msgh = (
struct msghdr) {
421 .msg_control =
my->cbuf,
424 .msg_namelen = to_len,
430 cmsg = CMSG_FIRSTHDR(&
my->msgh);
434 struct in_pktinfo *pkt;
436 my->msgh.msg_controllen = CMSG_SPACE(
sizeof(*pkt));
438 cmsg->cmsg_level =
SOL_IP;
439 cmsg->cmsg_type = IP_PKTINFO;
440 cmsg->cmsg_len = CMSG_LEN(
sizeof(*pkt));
442 pkt = (
struct in_pktinfo *) CMSG_DATA(cmsg);
443 memset(pkt, 0,
sizeof(*pkt));
444 pkt->ipi_spec_dst = addr->
socket.inet.src_ipaddr.addr.v4;
445 pkt->ipi_ifindex = addr->
socket.inet.ifindex;
447 #elif defined(IP_SENDSRCADDR)
450 my->msgh.msg_controllen = CMSG_SPACE(
sizeof(*
in));
452 cmsg->cmsg_level = IPPROTO_IP;
453 cmsg->cmsg_type = IP_SENDSRCADDR;
454 cmsg->cmsg_len = CMSG_LEN(
sizeof(*
in));
456 in = (
struct in_addr *) CMSG_DATA(cmsg);
457 *
in = addr->
socket.inet.src_ipaddr.addr.v4;
462 rcode = sendmsg(
my->info.socket.fd, &
my->msgh, 0);
469 static inline int fr_bio_fd_udpfromto_init4(
int fd)
471 int proto = 0, flag = 0, opt = 1;
473 #ifdef HAVE_IP_PKTINFO
480 #elif defined(IP_RECVDSTADDR)
486 flag = IP_RECVDSTADDR;
489 return setsockopt(fd,
proto, flag, &opt,
sizeof(opt));
493 #if defined(IPV6_PKTINFO)
499 struct cmsghdr *cmsg;
504 rcode = fd_fd_recvfromto_common(
my, packet_ctx,
buffer, size);
505 if (rcode <= 0)
return rcode;
509 for (cmsg = CMSG_FIRSTHDR(&
my->msgh);
511 cmsg = CMSG_NXTHDR(&
my->msgh, cmsg)) {
514 if ((cmsg->cmsg_level == IPPROTO_IPV6) &&
515 (cmsg->cmsg_type == IPV6_PKTINFO)) {
516 struct in6_pktinfo *i = (
struct in6_pktinfo *) CMSG_DATA(cmsg);
517 struct sockaddr_in6 to;
519 to.sin6_addr = i->ipi6_addr;
522 (
struct sockaddr_storage *) &to,
sizeof(
struct sockaddr_in6));
523 addr->
socket.inet.ifindex = i->ipi6_ifindex;
527 #ifdef SO_TIMESTAMPNS
528 if ((cmsg->cmsg_level ==
SOL_IP) && (cmsg->cmsg_type == SO_TIMESTAMPNS)) {
532 #elif defined(SO_TIMESTAMP)
533 if ((cmsg->cmsg_level ==
SOL_IP) && (cmsg->cmsg_type == SO_TIMESTAMP)) {
548 static
ssize_t fr_bio_fd_sendfromto6(
fr_bio_t *bio,
void *packet_ctx, const
void *
buffer,
size_t size)
552 struct cmsghdr *cmsg;
553 struct sockaddr_storage to;
558 memset(&
my->cbuf, 0,
sizeof(
my->cbuf));
559 memset(&
my->msgh, 0,
sizeof(
struct msghdr));
563 my->iov = (
struct iovec) {
568 my->msgh = (
struct msghdr) {
569 .msg_control =
my->cbuf,
572 .msg_namelen = to_len,
578 cmsg = CMSG_FIRSTHDR(&
my->msgh);
581 struct in6_pktinfo *pkt;
583 my->msgh.msg_controllen = CMSG_SPACE(
sizeof(*pkt));
585 cmsg->cmsg_level = IPPROTO_IPV6;
586 cmsg->cmsg_type = IPV6_PKTINFO;
587 cmsg->cmsg_len = CMSG_LEN(
sizeof(*pkt));
589 pkt = (
struct in6_pktinfo *) CMSG_DATA(cmsg);
590 memset(pkt, 0,
sizeof(*pkt));
591 pkt->ipi6_addr = addr->
socket.inet.src_ipaddr.addr.v6;
592 pkt->ipi6_ifindex = addr->
socket.inet.ifindex;
596 rcode = sendmsg(
my->info.socket.fd, &
my->msgh, 0);
604 static inline int fr_bio_fd_udpfromto_init6(
int fd)
608 return setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &opt,
sizeof(opt));
616 len = strlen(filename);
617 if (len >=
sizeof(sun->sun_path)) {
618 fr_strerror_const(
"Failed parsing unix domain socket filename: Name is too long");
622 sun->sun_family = AF_LOCAL;
623 memcpy(sun->sun_path, filename, len + 1);
625 *sunlen = SUN_LEN(sun);
633 struct sockaddr_storage salocal;
645 salen =
sizeof(salocal);
646 memset(&salocal, 0, salen);
647 if (getsockname(
my->info.socket.fd, (
struct sockaddr *) &salocal, &salen) < 0) {
655 my->info.socket.inet.ifindex =
my->info.socket.inet.src_ipaddr.scope_id;
663 my->info.eof =
false;
664 my->info.read_blocked =
false;
665 my->info.write_blocked =
false;
670 if (
my->cb.connected)
my->cb.connected(&
my->bio);
684 struct sockaddr_storage sockaddr;
686 if (
my->info.socket.af != AF_LOCAL) {
700 if (connect(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, salen) == 0) {
716 if (tries <= my->max_tries)
goto retry;
730 if (!
my->info.write_blocked) {
731 my->info.write_blocked =
true;
734 if (rcode < 0)
return rcode;
759 switch (
my->info.cfg->flags & (O_RDONLY | O_WRONLY | O_RDWR)) {
807 my->info.eof =
false;
809 my->info.read_blocked =
false;
810 my->info.write_blocked =
false;
823 setsockopt(
my->info.socket.fd, SOL_SOCKET, SO_NOSIGPIPE, &on,
sizeof(on));
830 if (!
my->info.cfg->async)
return 0;
833 if (rcode == 0)
return 0;
835 if (rcode !=
fr_bio_error(IO_WOULD_BLOCK))
return rcode;
848 if (
my->info.socket.type == SOCK_STREAM) {
860 #if defined(IP_PKTINFO) || defined(IP_RECVDSTADDR)
861 }
else if (
my->info.socket.inet.src_ipaddr.af == AF_INET) {
862 if (fr_bio_fd_udpfromto_init4(
my->info.socket.fd) < 0)
return -1;
864 my->bio.read = fr_bio_fd_recvfromto4;
865 my->bio.write = fr_bio_fd_sendfromto4;
868 #if defined(IPV6_PKTINFO)
869 }
else if (
my->info.socket.inet.src_ipaddr.af == AF_INET6) {
871 if (fr_bio_fd_udpfromto_init6(
my->info.socket.fd) < 0)
return -1;
873 my->bio.read = fr_bio_fd_recvfromto6;
874 my->bio.write = fr_bio_fd_sendfromto6;
896 struct sockaddr_storage sockaddr;
898 if (size <
sizeof(
int))
return fr_bio_error(BUFFER_TOO_SMALL);
900 salen =
sizeof(sockaddr);
907 fd = accept4(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, &salen, SOCK_NONBLOCK | SOCK_CLOEXEC);
909 fd = accept(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, &salen);
919 addr->
socket.inet.dst_ipaddr =
my->info.socket.inet.src_ipaddr;
920 addr->
socket.inet.dst_port =
my->info.socket.inet.src_port;
933 if (tries <= my->max_tries)
goto retry;
940 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
969 if (listen(
my->info.socket.fd, 8) < 0) {
1017 if (!
my)
return NULL;
1020 my->offset = offset;
1031 .read_blocked =
false,
1032 .write_blocked =
false,
1070 if (rcode < 0)
return rcode;
1087 (void) shutdown(
my->info.socket.fd, SHUT_RDWR);
1091 rcode =
close(
my->info.socket.fd);
1097 if (tries < my->max_tries)
goto retry;
1109 my->info.read_blocked =
true;
1110 my->info.write_blocked =
true;
1111 my->info.eof =
true;
1123 my->info.connect_errno = fd_errno;
1125 if (
my->connect.error) {
1126 my->connect.error(&
my->bio);
1153 socklen_t socklen =
sizeof(error);
1168 if (getsockopt(
my->info.socket.fd, SOL_SOCKET, SO_ERROR, (
void *)&error, &socklen) < 0) {
1192 if (
my->connect.ev) {
1194 my->connect.ev = NULL;
1196 my->connect.el = NULL;
1201 my->connect.success(&
my->bio);
1213 my->connect.timeout(&
my->bio);
1256 if (connected_cb) connected_cb(bio);
1278 if (!connected_cb) {
1282 if (rcode < 0)
return rcode;
1295 my->connect.success = connected_cb;
1296 my->connect.error = error_cb;
1297 my->connect.timeout = timeout_cb;
1315 my->connect.el =
el;
1340 rcode = read(
my->info.socket.fd,
buffer, size);
1341 if (rcode >= 0)
return 0;
1344 #define flag_blocked read_blocked
1357 switch (
my->info.type) {
1359 if (
my->info.socket.type != SOCK_DGRAM) {
1370 if (shutdown(
my->info.socket.fd, SHUT_RD) < 0) {
1394 struct sockaddr_storage sockaddr;
1398 salen =
sizeof(sockaddr);
1409 fd = accept4(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, &salen, SOCK_NONBLOCK | SOCK_CLOEXEC);
1411 fd = accept(
my->info.socket.fd, (
struct sockaddr *) &sockaddr, &salen);
1420 if (tries <= my->max_tries)
goto retry;
1427 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
1462 cfg = talloc_memdup(
out,
my->info.cfg,
sizeof(*
my->info.cfg));
1474 out->info.socket.fd = fd;
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 fr_bio_t * fr_bio_prev(fr_bio_t *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
fr_dcursor_eval_t void const * uctx
static fr_time_delta_t timeout
#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()
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(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size)
Discard all reads from a UDP 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_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)
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.
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_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