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: 56f6a2bcd2b6ffceda8078890a81606720dd37b9 $
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: 56f6a2bcd2b6ffceda8078890a81606720dd37b9 $")
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  fr_rb_node_t dedup; //!< user managed dedup node
55 };
56 #endif
57 
58 typedef enum {
64 
65 /** Callback on read to see if we should receive the packet.
66  *
67  * The caller should cache dedup_ctx, unless it's a duplicate request.
68  *
69  * If it's a duplicate request, the caller should call fr_bio_dedup_respond() to write out the reply.
70  *
71  * @param bio the binary IO handler
72  * @param dedup_ctx new dedup_ctx assigned to this potential packet
73  * @param packet_ctx per-packet context for the response
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);
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 
102 void fr_bio_dedup_entry_cancel(fr_bio_t *bio, fr_bio_dedup_entry_t *dedup_ctx) CC_HINT(nonnull);
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);
Definition: base.h:112
#define RCSIDH(h, id)
Definition: build.h:482
next
Definition: dcursor.h:178
return item
Definition: dcursor.h:553
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:213
void * uctx
user-writable context
Definition: dedup.c:96
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:1035
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:934
size_t packet_size
size of the cached packet data
Definition: dedup.c:99
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:952
void * packet_ctx
packet_ctx for dedup purposes
Definition: dedup.c:97
uint8_t * reply
reply cached by the application
Definition: dedup.c:101
size_t reply_size
size of the cached reply
Definition: dedup.c:102
void * reply_ctx
reply ctx
Definition: dedup.c:100
uint8_t * packet
cached packet data for finding duplicates
Definition: dedup.c:98
fr_bio_dedup_entry_t *(* fr_bio_dedup_get_item_t)(fr_bio_t *bio, void *packet_ctx)
Definition: dedup.h:93
fr_rb_node_t dedup
user managed dedup node
Definition: dedup.c:104
bool(* fr_bio_dedup_receive_t)(fr_bio_t *bio, fr_bio_dedup_entry_t *dedup_ctx, void *packet_ctx)
Callback on read to see if we should receive the packet.
Definition: dedup.h:78
fr_bio_dedup_release_reason_t
Definition: dedup.h:58
@ FR_BIO_DEDUP_WRITE_ERROR
Definition: dedup.h:61
@ FR_BIO_DEDUP_INTERNAL_ERROR
Definition: dedup.h:62
@ FR_BIO_DEDUP_CANCELLED
Definition: dedup.h:60
@ FR_BIO_DEDUP_EXPIRED
Definition: dedup.h:59
Definition: dedup.c:95
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
unsigned char uint8_t
Definition: merged_model.c:30
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))