]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
we need separate discard functions for UDP and TCP
authorAlan T. DeKok <aland@freeradius.org>
Fri, 9 May 2025 17:33:45 +0000 (13:33 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Fri, 9 May 2025 18:32:03 +0000 (14:32 -0400)
read of 0 is OK for UDP, and is EOF for TCP

src/lib/bio/fd.c

index 1cac61573a0cd3639e5f5b7fa7430e439c451c20..f1eb33d5425b8eff530cfd9b39a593e9b1048781 100644 (file)
@@ -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;
 }