The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
retry.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: f2ed5ea934c3b90a2151f23f65d1c8b64c382f6a $
20  * @file lib/bio/retry.h
21  * @brief Binary IO abstractions for retrying of raw packets
22  *
23  * Write packets of data to bios. Once a packet is written, it is
24  * retried until a response is returned. Note that this ONLY works
25  * for datagram bios, where the packets can be retransmitted
26  * identically without any changes.
27  *
28  * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
29  */
30 RCSIDH(lib_bio_retry_h, "$Id: f2ed5ea934c3b90a2151f23f65d1c8b64c382f6a $")
31 
32 #include <freeradius-devel/util/retry.h>
33 #include <freeradius-devel/util/event.h>
34 
35 typedef struct {
36  fr_event_list_t *el; //!< event list
37 
38  fr_retry_config_t retry_config; //!< base retry config
40 
42 
43 typedef ssize_t (*fr_bio_retry_rewrite_t)(fr_bio_t *bio, fr_bio_retry_entry_t *retry_ctx, const void *buffer, size_t size);
44 
45 #ifndef _BIO_RETRY_PRIVATE
46 struct fr_bio_retry_entry_s {
47  void *uctx; //!< user-writable context
48  void *packet_ctx; //!< packet_ctx from the write() call
49  fr_bio_retry_rewrite_t rewrite; //!< per-packet rewrite callback
50  void *rewrite_ctx; //!< context specifically for rewriting this packet
51 };
52 #endif
53 
54 typedef enum {
61 
62 /** Callback for when a packet is sent
63  *
64  * The purpose of the callback is for the application to save the retry_ctx, in case the packet needs to be
65  * cancelled at a later point in time.
66  *
67  * @param bio the binary IO handler
68  * @param packet_ctx per-packet context
69  * @param buffer raw data for the packet
70  * @param size size of the raw data
71  * @param retry_ctx pointer to save for use with later cancellation
72  */
73 typedef void (*fr_bio_retry_sent_t)(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size,
74  fr_bio_retry_entry_t *retry_ctx);
75 
76 typedef ssize_t (*fr_bio_retry_rewrite_t)(fr_bio_t *bio, fr_bio_retry_entry_t *retry_ctx, const void *buffer, size_t size);
77 
78 /** Callback on read to see if a packet is a response
79  *
80  * The callback should manually associate the response with a request, but allow the packet to be returned
81  * to the application.
82  *
83  * The callback should also set item_p, _even if the response is duplicate_. That way the retry bio can
84  * properly track multiple requests and responses.
85  *
86  * If there is a fatal error reading the response, the callback should set a flag (in the bio)? and return.
87  * It should NOT call any bio shutdown routine.
88  *
89  * @param bio the binary IO handler
90  * @param[out] item_p item pointer for request, from #fr_bio_retry_saved_t
91  * @param packet_ctx per-packet context for the response
92  * @param buffer raw data for the response
93  * @param size size of the raw response data
94  * @return
95  * - false - do not pass the packet to the reader
96  * - true - pass the packet through to the reader
97  */
98 typedef bool (*fr_bio_retry_response_t)(fr_bio_t *bio, fr_bio_retry_entry_t **item_p, void *packet_ctx, const void *buffer, size_t size);
99 
100 /** Callback on release the packet (timeout or have all replies)
101  *
102  * The callback function should clean up any resources associated with the packet. The resources MUST NOT be
103  * released until the data is either "released" or "cancelled".
104  *
105  * The packet will be cancelled after this call returns. The cancellation callback will NOT be run.
106  *
107  * @param bio the binary IO handler
108  * @param retry_ctx the retry ctx to release
109  * @param reason why this packet is being released
110  */
112 
113 fr_bio_t *fr_bio_retry_alloc(TALLOC_CTX *ctx, size_t max_saved,
114  fr_bio_retry_sent_t sent,
115  fr_bio_retry_response_t response,
116  fr_bio_retry_rewrite_t rewrite,
117  fr_bio_retry_release_t release,
118  fr_bio_retry_config_t const *cfg,
119  fr_bio_t *next) CC_HINT(nonnull(1,3,4,6,7,8));
120 
122 
124 
126 
127 ssize_t fr_bio_retry_rewrite(fr_bio_t *bio, fr_bio_retry_entry_t *retry_ctx, const void *buffer, size_t size) CC_HINT(nonnull(1,2));
static int const char char buffer[256]
Definition: acutest.h:574
Definition: base.h:103
fr_bio_t * fr_bio_retry_alloc(TALLOC_CTX *ctx, size_t max_saved, fr_bio_retry_sent_t sent, fr_bio_retry_response_t response, fr_bio_retry_rewrite_t rewrite, fr_bio_retry_release_t release, fr_bio_retry_config_t const *cfg, fr_bio_t *next))
Allocate a fr_bio_retry_t.
Definition: retry.c:852
int fr_bio_retry_entry_start(fr_bio_t *bio, fr_bio_retry_entry_t *retry_ctx, fr_retry_config_t const *cfg)
fr_retry_config_t retry_config
base retry config
Definition: retry.h:38
void(* fr_bio_retry_release_t)(fr_bio_t *bio, fr_bio_retry_entry_t *retry_ctx, fr_bio_retry_release_reason_t reason)
Callback on release the packet (timeout or have all replies)
Definition: retry.h:111
fr_bio_retry_release_reason_t
Definition: retry.h:54
@ FR_BIO_RETRY_WRITE_ERROR
Definition: retry.h:58
@ FR_BIO_RETRY_FATAL_ERROR
Definition: retry.h:59
@ FR_BIO_RETRY_CANCELLED
Definition: retry.h:57
@ FR_BIO_RETRY_DONE
Definition: retry.h:55
@ FR_BIO_RETRY_NO_REPLY
Definition: retry.h:56
void * rewrite_ctx
context specifically for rewriting this packet
Definition: retry.h:50
void(* fr_bio_retry_sent_t)(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size, fr_bio_retry_entry_t *retry_ctx)
Callback for when a packet is sent.
Definition: retry.h:73
ssize_t(* fr_bio_retry_rewrite_t)(fr_bio_t *bio, fr_bio_retry_entry_t *retry_ctx, const void *buffer, size_t size)
Definition: retry.h:43
ssize_t fr_bio_retry_rewrite(fr_bio_t *bio, fr_bio_retry_entry_t *retry_ctx, const void *buffer, size_t size))
Resend a packet.
Definition: retry.c:447
bool(* fr_bio_retry_response_t)(fr_bio_t *bio, fr_bio_retry_entry_t **item_p, void *packet_ctx, const void *buffer, size_t size)
Callback on read to see if a packet is a response.
Definition: retry.h:98
fr_event_list_t * el
event list
Definition: retry.h:36
void * packet_ctx
packet_ctx from the write() call
Definition: retry.c:44
const fr_retry_t * fr_bio_retry_entry_info(fr_bio_t *bio, fr_bio_retry_entry_t *retry_ctx)
int fr_bio_retry_entry_cancel(fr_bio_t *bio, fr_bio_retry_entry_t *retry_ctx))
Cancel one item.
Definition: retry.c:767
fr_bio_retry_rewrite_t rewrite
per-packet rewrite callback
Definition: retry.c:45
void * uctx
user-writable context
Definition: retry.c:43
Definition: retry.c:42
#define RCSIDH(h, id)
Definition: build.h:445
Stores all information relating to an event list.
Definition: event.c:411
long int ssize_t
Definition: merged_model.c:24
unsigned char bool
Definition: merged_model.c:19
static fr_bio_t * bio
Definition: radclient-ng.c:86
int nonnull(2, 5))