The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
dedup.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: 1860730646f4a58ce740b49c08a9f5e7b93f5c89 $
20  * @file lib/bio/dedup.h
21  * @brief Binary IO abstractions for deduping of raw packets
22  *
23  * Read packets from a dedup bio. Once a packet is read, it is checked
24  * against a dedup tree. Duplicate packets are suppressed.
25  *
26  * The caller has to manage the actual dedup tree and comparisons.
27  * Each protocol has its own requirements for dedup, and it is too
28  * awkward to make a generic method which works everywhere.
29  *
30  * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
31  */
32 RCSIDH(lib_bio_dedup_h, "$Id: 1860730646f4a58ce740b49c08a9f5e7b93f5c89 $")
33 
34 #include <freeradius-devel/util/event.h>
35 
36 typedef struct {
37  fr_event_list_t *el; //!< event list
38 
39  fr_time_delta_t lifetime; //!< default lifetime of dedup entry
41 
43 
44 #ifndef _BIO_DEDUP_PRIVATE
45 struct fr_bio_dedup_entry_s {
46  void *uctx; //!< user-writable context
47  void *packet_ctx; //!< packet_ctx for dedup purposes
48  uint8_t *packet; //!< cached packet data.
49  size_t packet_size; //!< size of the cached packet data
50  void *reply_ctx; //!< reply ctx
51  uint8_t *reply; //!< reply cached by the application
52  size_t reply_size; //!< size of the cached reply
53 };
54 #endif
55 
56 typedef enum {
62 
63 /** Callback on read to see if we should receive the packet.
64  *
65  * The caller should cache dedup_ctx, unless it's a duplicate request.
66  *
67  * If it's a duplicate request, the caller should call fr_bio_dedup_respond() to write out the reply.
68  *
69  * @param bio the binary IO handler
70  * @param dedup_ctx new dedup_ctx assigned to this potential packet
71  * @param packet_ctx per-packet context for the response
72  * @param buffer raw data for the request
73  * @param size size of the raw request data
74  * @return
75  * - false - discard the packet
76  * - true - create a new entry for the packet
77  */
78 typedef bool (*fr_bio_dedup_receive_t)(fr_bio_t *bio, fr_bio_dedup_entry_t *dedup_ctx, void *packet_ctx, const void *buffer, size_t size);
79 
80 /** Callback on release the packet (timeout, or cancelled by the application)
81  *
82  * The callback function should clean up any resources associated with the packet. The resources MUST NOT be
83  * released until the data is either "released" or "cancelled".
84  *
85  * The packet will be cancelled after this call returns. The cancellation callback will NOT be run.
86  *
87  * @param bio the binary IO handler
88  * @param dedup_ctx the dedup ctx to release
89  * @param reason why this packet is being released
90  */
92 
93 typedef fr_bio_dedup_entry_t *(*fr_bio_dedup_get_item_t)(fr_bio_t *bio, void *packet_ctx);
94 
95 fr_bio_t *fr_bio_dedup_alloc(TALLOC_CTX *ctx, size_t max_saved,
96  fr_bio_dedup_receive_t receive,
97  fr_bio_dedup_release_t release,
98  fr_bio_dedup_get_item_t get_item,
99  fr_bio_dedup_config_t const *cfg,
100  fr_bio_t *next) CC_HINT(nonnull(1,3,4,6,7));
101 
103 
105 
106 int fr_bio_dedup_entry_extend(fr_bio_t *bio, fr_bio_dedup_entry_t *dedup_ctx, fr_time_t expires) CC_HINT(nonnull);
static int const char char buffer[256]
Definition: acutest.h:574
Definition: base.h:103
#define RCSIDH(h, id)
Definition: build.h:445
ssize_t fr_bio_dedup_respond(fr_bio_t *bio, fr_bio_dedup_entry_t *item)
Resend a reply when we receive a duplicate request.
Definition: dedup.c:173
void * uctx
user-writable context
Definition: dedup.c:58
void(* fr_bio_dedup_release_t)(fr_bio_t *bio, fr_bio_dedup_entry_t *dedup_ctx, fr_bio_dedup_release_reason_t reason)
Callback on release the packet (timeout, or cancelled by the application)
Definition: dedup.h:91
fr_event_list_t * el
event list
Definition: dedup.h:37
fr_bio_t * fr_bio_dedup_alloc(TALLOC_CTX *ctx, size_t max_saved, fr_bio_dedup_receive_t receive, fr_bio_dedup_release_t release, fr_bio_dedup_get_item_t get_item, fr_bio_dedup_config_t const *cfg, fr_bio_t *next))
Allocate a fr_bio_dedup_t.
Definition: dedup.c:987
bool(* fr_bio_dedup_receive_t)(fr_bio_t *bio, fr_bio_dedup_entry_t *dedup_ctx, void *packet_ctx, const void *buffer, size_t size)
Callback on read to see if we should receive the packet.
Definition: dedup.h:78
fr_time_delta_t lifetime
default lifetime of dedup entry
Definition: dedup.h:39
void fr_bio_dedup_entry_cancel(fr_bio_t *bio, fr_bio_dedup_entry_t *dedup_ctx)
Cancel one item.
Definition: dedup.c:887
size_t packet_size
size of the cached packet data
Definition: dedup.c:61
int fr_bio_dedup_entry_extend(fr_bio_t *bio, fr_bio_dedup_entry_t *dedup_ctx, fr_time_t expires)
Extend the expiry time for an entry.
Definition: dedup.c:905
void * packet_ctx
packet_ctx for dedup purposes
Definition: dedup.c:59
uint8_t * reply
reply cached by the application
Definition: dedup.c:63
size_t reply_size
size of the cached reply
Definition: dedup.c:64
void * reply_ctx
reply ctx
Definition: dedup.c:62
uint8_t * packet
cached packet data for finding duplicates
Definition: dedup.c:60
fr_bio_dedup_entry_t *(* fr_bio_dedup_get_item_t)(fr_bio_t *bio, void *packet_ctx)
Definition: dedup.h:93
fr_bio_dedup_release_reason_t
Definition: dedup.h:56
@ FR_BIO_DEDUP_WRITE_ERROR
Definition: dedup.h:59
@ FR_BIO_DEDUP_INTERNAL_ERROR
Definition: dedup.h:60
@ FR_BIO_DEDUP_CANCELLED
Definition: dedup.h:58
@ FR_BIO_DEDUP_EXPIRED
Definition: dedup.h:57
Definition: dedup.c:57
Stores all information relating to an event list.
Definition: event.c:411
static void * item(fr_lst_t const *lst, fr_lst_index_t idx)
Definition: lst.c:122
long int ssize_t
Definition: merged_model.c:24
unsigned char bool
Definition: merged_model.c:19
unsigned char uint8_t
Definition: merged_model.c:30
static fr_bio_t * bio
Definition: radclient-ng.c:86
A time delta, a difference in time measured in nanoseconds.
Definition: time.h:80
"server local" time.
Definition: time.h:69
int nonnull(2, 5))