From: Alan T. DeKok Date: Fri, 9 May 2025 17:33:45 +0000 (-0400) Subject: we need separate discard functions for UDP and TCP X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c6f271b8538cdaea352d73bbf4d89f08b7a2da83;p=thirdparty%2Ffreeradius-server.git we need separate discard functions for UDP and TCP read of 0 is OK for UDP, and is EOF for TCP --- diff --git a/src/lib/bio/fd.c b/src/lib/bio/fd.c index 1cac61573a0..f1eb33d5425 100644 --- a/src/lib/bio/fd.c +++ b/src/lib/bio/fd.c @@ -1373,7 +1373,7 @@ fr_bio_fd_info_t const *fr_bio_fd_info(fr_bio_t *bio) /** Discard all reads from a UDP socket. */ -static ssize_t fr_bio_fd_read_discard(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size) +static ssize_t fr_bio_fd_read_discard_datagram(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size) { int tries = 0; ssize_t rcode; @@ -1390,6 +1390,29 @@ retry: return fr_bio_error(IO); } +/** Discard all reads from a TCP socket. + */ +static ssize_t fr_bio_fd_read_discard_stream(fr_bio_t *bio, UNUSED void *packet_ctx, void *buffer, size_t size) +{ + int tries = 0; + ssize_t rcode; + fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t); + +retry: + rcode = read(my->info.socket.fd, buffer, size); + if (rcode > 0 ) return 0; /* always return that we read no data */ + if (rcode == 0) { + fr_bio_eof(bio); + return 0; + } + +#undef flag_blocked +#define flag_blocked read_blocked +#include "fd_errno.h" + + return fr_bio_error(IO); +} + /** Mark up a bio as write-only * */ @@ -1446,7 +1469,7 @@ int fr_bio_fd_write_only(fr_bio_t *bio) * No matter what the possibilities above, we replace the read function with a "discard" * function. */ - my->bio.read = fr_bio_fd_read_discard; + my->bio.read = (my->info.socket.type == SOCK_DGRAM) ? fr_bio_fd_read_discard_datagram : fr_bio_fd_read_discard_stream; return 0; }