The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
base.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /**
19  * $Id: b3549cfb06fa84d6e66f22e90b8686ded7c8c447 $
20  * @file lib/bio/base.h
21  * @brief Binary IO abstractions.
22  *
23  * Create abstract binary input / output buffers.
24  *
25  * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
26  */
27 RCSIDH(lib_bio_base_h, "$Id: b3549cfb06fa84d6e66f22e90b8686ded7c8c447 $")
28 
29 #include <freeradius-devel/util/debug.h>
30 #include <freeradius-devel/util/dlist.h>
31 
32 #ifdef NDEBUG
33 #define XDEBUG(_x)
34 #else
35 #define XDEBUG(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
36 #endif
37 
38 #ifdef _CONST
39 # error _CONST can only be defined in the local header
40 #endif
41 #ifndef _BIO_PRIVATE
42 # define _CONST const
43 #else
44 # define _CONST
45 #endif
46 
47 typedef enum {
49  FR_BIO_ERROR_IO_WOULD_BLOCK, //!< IO would block
50 
51  FR_BIO_ERROR_IO, //!< IO error - check errno
52  FR_BIO_ERROR_GENERIC, //!< generic "failed" error - check fr_strerror()
53  FR_BIO_ERROR_VERIFY, //!< some packet verification error
54  FR_BIO_ERROR_BUFFER_FULL, //!< the buffer is full
55  FR_BIO_ERROR_BUFFER_TOO_SMALL, //!< the output buffer is too small for the data
56 
57  FR_BIO_ERROR_EOF, //!< at EOF
59 
60 typedef struct fr_bio_s fr_bio_t;
61 
62 /** Do a raw read from a socket, or other data source
63  *
64  * These functions should be careful about packet_ctx. This handling depends on a number of factors. Note
65  * that the packet_ctx may be NULL!
66  *
67  * Stream sockets will generally ignore packet_ctx.
68  *
69  * Datagram sockets generally write src/dst IP/port to the packet context. This same packet_ctx is then
70  * passed to bio->write(), which can use it to send the data to the correct destination.
71  *
72  * @param bio the binary IO handler
73  * @param packet_ctx where the function can store per-packet information, such as src/dst IP/port for datagram sockets
74  * @param buffer where the function should store data it reads
75  * @param size the maximum amount of data to read.
76  * @return
77  * - <0 for error
78  * - 0 for "no data available". Note that this does NOT mean EOF! It could mean "we do not have a full packet"
79  * - >0 for amount of data which was read.
80  */
81 typedef ssize_t (*fr_bio_read_t)(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size);
82 typedef ssize_t (*fr_bio_write_t)(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size);
83 
84 typedef int (*fr_bio_callback_t)(fr_bio_t *bio); /* activate / shutdown callbacks */
85 
86 typedef struct {
90 
91 /** Accept a new connection on a bio
92  *
93  * @param bio the binary IO handler
94  * @param ctx the talloc ctx for the new bio.
95  * @param[out] accepted the accepted bio
96  * @return
97  * - <0 on error
98  * - 0 for "we did nothing, and there is no new bio available"
99  * - 1 for "the accepted bio is available"
100  */
101 typedef int (*fr_bio_accept_t)(fr_bio_t *bio, TALLOC_CTX *ctx, fr_bio_t **accepted);
102 
103 struct fr_bio_s {
104  void *uctx; //!< user ctx, caller can manually set it.
105 
106  fr_bio_read_t _CONST read; //!< read from the underlying bio
107  fr_bio_write_t _CONST write; //!< write to the underlying bio
108 
109  fr_dlist_t _CONST entry; //!< in the linked list of multiple bios
110 };
111 
112 static inline CC_HINT(nonnull) fr_bio_t *fr_bio_prev(fr_bio_t *bio)
113 {
114  fr_dlist_t *prev = bio->entry.prev;
115 
116  if (!prev) return NULL;
117 
118  return fr_dlist_entry_to_item(offsetof(fr_bio_t, entry), prev);
119 }
120 
121 static inline CC_HINT(nonnull) fr_bio_t *fr_bio_next(fr_bio_t *bio)
122 {
123  fr_dlist_t *next = bio->entry.next;
124 
125  if (!next) return NULL;
126 
127  return fr_dlist_entry_to_item(offsetof(fr_bio_t, entry), next);
128 }
129 
130 /** Read raw data from a bio
131  *
132  * @param bio the binary IO handler
133  * @param packet_ctx packet-specific data associated with the buffer
134  * @param buffer where to read the data
135  * @param size amount of data to read.
136  * @return
137  * - <0 for error. The return code will be fr_bio_error(ERROR_NAME)
138  * - 0 for "did not read any data". Note that EOF is an error return.
139  * - >0 for amount of data read.
140  */
141 static inline ssize_t CC_HINT(nonnull(1,3)) fr_bio_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
142 {
143  if (size == 0) return 0;
144 
145  /*
146  * We cannot read from the middle of a chain.
147  */
149 
150  return bio->read(bio, packet_ctx, buffer, size);
151 }
152 
153 /** Write raw data to a bio
154  *
155  * @param bio the binary IO handler
156  * @param packet_ctx packet-specific data associated with the buffer
157  * @param buffer the data to write. If NULL, will "flush" any pending data.
158  * @param size amount of data to write. For flush, it should be SIZE_MAX
159  * @return
160  * - <0 for error. The return code will be fr_bio_error(ERROR_NAME)
161  * - 0 for "did not write any data"
162  * - >0 for amount of data written. Should always be equal to size!
163  */
164 static inline ssize_t CC_HINT(nonnull(1)) fr_bio_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
165 {
166  if (size == 0) return 0;
167 
168  /*
169  * We cannot write to the middle of a chain.
170  */
172 
173  return bio->write(bio, packet_ctx, buffer, size);
174 }
175 
177 
178 #ifndef NDEBUG
179 int fr_bio_destructor(fr_bio_t *bio) CC_HINT(nonnull);
180 #else
181 #define fr_bio_destructor (NULL)
182 #endif
183 
184 #define fr_bio_error(_x) (-(FR_BIO_ERROR_ ## _x))
185 
186 int fr_bio_shutdown(fr_bio_t *bio) CC_HINT(nonnull);
187 
188 int fr_bio_free(fr_bio_t *bio) CC_HINT(nonnull);
189 
190 char const *fr_bio_strerror(ssize_t error);
static int const char char buffer[256]
Definition: acutest.h:574
fr_bio_write_t _CONST write
write to the underlying bio
Definition: base.h:107
int fr_bio_free(fr_bio_t *bio)
Free this bio, and everything it calls.
Definition: base.c:113
fr_bio_read_t _CONST read
read from the underlying bio
Definition: base.h:106
int(* fr_bio_accept_t)(fr_bio_t *bio, TALLOC_CTX *ctx, fr_bio_t **accepted)
Accept a new connection on a bio.
Definition: base.h:101
fr_dlist_t _CONST entry
in the linked list of multiple bios
Definition: base.h:109
static ssize_t fr_bio_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
Write raw data to a bio.
Definition: base.h:164
ssize_t(* fr_bio_read_t)(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Do a raw read from a socket, or other data source.
Definition: base.h:81
static ssize_t fr_bio_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Read raw data from a bio.
Definition: base.h:141
fr_bio_error_type_t
Definition: base.h:47
@ FR_BIO_ERROR_NONE
Definition: base.h:48
@ FR_BIO_ERROR_BUFFER_FULL
the buffer is full
Definition: base.h:54
@ FR_BIO_ERROR_GENERIC
generic "failed" error - check fr_strerror()
Definition: base.h:52
@ FR_BIO_ERROR_IO
IO error - check errno.
Definition: base.h:51
@ FR_BIO_ERROR_VERIFY
some packet verification error
Definition: base.h:53
@ FR_BIO_ERROR_IO_WOULD_BLOCK
IO would block.
Definition: base.h:49
@ FR_BIO_ERROR_EOF
at EOF
Definition: base.h:57
@ FR_BIO_ERROR_BUFFER_TOO_SMALL
the output buffer is too small for the data
Definition: base.h:55
int(* fr_bio_callback_t)(fr_bio_t *bio)
Definition: base.h:84
fr_bio_callback_t activate
Definition: base.h:87
ssize_t(* fr_bio_write_t)(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size)
Definition: base.h:82
fr_bio_callback_t shutdown
Definition: base.h:88
#define _CONST
Definition: base.h:42
int fr_bio_shutdown_intermediate(fr_bio_t *bio)
Like fr_bio_shutdown(), but can be called by anyone in the chain.
Definition: base.c:186
static fr_bio_t * fr_bio_prev(fr_bio_t *bio)
Definition: base.h:112
static fr_bio_t * fr_bio_next(fr_bio_t *bio)
Definition: base.h:121
void * uctx
user ctx, caller can manually set it.
Definition: base.h:104
char const * fr_bio_strerror(ssize_t error)
Definition: base.c:197
int fr_bio_destructor(fr_bio_t *bio)
Free this bio.
Definition: base.c:34
int fr_bio_shutdown(fr_bio_t *bio)
Shut down a set of BIOs.
Definition: base.c:152
Definition: base.h:103
#define RCSIDH(h, id)
Definition: build.h:445
fr_dlist_t * next
Definition: dlist.h:43
static void * fr_dlist_entry_to_item(size_t offset, fr_dlist_t const *entry)
Get the item from a fr_dlist_t.
Definition: dlist.h:130
fr_dlist_t * prev
Definition: dlist.h:42
Entry in a doubly linked list.
Definition: dlist.h:41
long int ssize_t
Definition: merged_model.c:24
static fr_bio_t * bio
Definition: radclient-ng.c:86
fr_assert(0)
int nonnull(2, 5))