The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
packet.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: 6426d5d9f6f456d0d160edab6a2be4fac1b341ba $
20  * @file lib/bio/packet.h
21  * @brief Binary IO abstractions for #fr_packet_t
22  *
23  * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
24  */
25 RCSIDH(lib_bio_packet_h, "$Id: 6426d5d9f6f456d0d160edab6a2be4fac1b341ba $")
26 
27 #include <freeradius-devel/util/packet.h>
28 #include <freeradius-devel/bio/base.h>
29 
30 // @todo - _CONST
31 
32 typedef struct fr_bio_packet_s fr_bio_packet_t;
33 
34 /** Read a packet and pairs from the network
35  *
36  * @param bio the packet-based bio
37  * @param request_ctx_p the request context associated with the response
38  * @param packet_p the response packet. Contains raw protocol data (IDs, counts, etc.)
39  * @param out_ctx talloc context for the list
40  * @param out the decoded pairs from the packet
41  * @return
42  * - <0 on error
43  * - 0 for success (*packet_p may still be NULL tho)
44  */
45 typedef int (*fr_bio_packet_read_t)(fr_bio_packet_t *bio, void **request_ctx_p, fr_packet_t **packet_p, TALLOC_CTX *out_ctx, fr_pair_list_t *out);
46 
47 /** Write a packet and pairs from the network
48  *
49  * @param bio the packet-based bio
50  * @param request_ctx the request context
51  * @param packet the request packet. Contains raw protocol data (IDs, counts, etc.)
52  * @param list the pairs to encode in the packet
53  * @return
54  * - <0 on error (EOF, fail, etc,)
55  * - 0 for success
56  */
58 
59 /** Release an outgoing packet.
60  *
61  * @param bio the packet-based bio
62  * @param packet the output packet descriptor. Contains raw protocol data (IDs, counts, etc.)
63  * @return
64  * - <0 on error
65  * - 0 for success
66  */
68 
70  void *uctx; //!< user ctx, caller can manually set it.
71 
72  fr_bio_packet_read_t read; //!< read from the underlying bio
73  fr_bio_packet_write_t write; //!< write to the underlying bio
74 
75  fr_bio_t *bio; //!< underlying bio for IO
76 };
77 
78 
79 /** Read a packet from a packet BIO
80  *
81  * Note that the bio MAY return fr_bio_error(IO_WOULD_BLOCK), which is not a fatal error. The caller has to
82  * check for that case, and handle blocking errors. Typically by pushing the packet to a queue, and trying
83  * it again later.
84  *
85  * @param bio the packet-based bio
86  * @param[out] request_ctx_p the larger context for the original request packet
87  * @param[out] packet_p Where the allocated #fr_packet_t will be stored
88  * @param[out] out_ctx for the output pairs
89  * @param[out] out decoded output pairs
90  * @return
91  * - <0 on error. This is fr_bio_error(FOO)
92  * - 0 for success
93  */
94 static inline CC_HINT(nonnull) int fr_bio_packet_read(fr_bio_packet_t *bio, void **request_ctx_p, fr_packet_t **packet_p, TALLOC_CTX *out_ctx, fr_pair_list_t *out)
95 {
96  return bio->read(bio, request_ctx_p, packet_p, out_ctx, out);
97 }
98 
99 /** Write a packet to a packet BIO
100  *
101  * Note that the bio MAY return fr_bio_error(IO_WOULD_BLOCK), which is not a fatal error. The caller has to
102  * check for that case, and handle blocking errors. Typically by pushing the packet to a queue, and trying
103  * it again later.
104  *
105  * @param bio the packet-based bio
106  * @param request_ctx the larger context for the packet
107  * @param packet the output packet descriptor. Contains raw protocol data (IDs, counts, etc.)
108  * @param list of pairs to write
109  * @return
110  * - <0 on error. This is fr_bio_error(FOO)
111  * - 0 for success
112  */
113 static inline CC_HINT(nonnull) int fr_bio_packet_write(fr_bio_packet_t *bio, void *request_ctx, fr_packet_t *packet, fr_pair_list_t *list)
114 {
115  return bio->write(bio, request_ctx, packet, list);
116 }
fr_bio_write_t _CONST write
write to the underlying bio
Definition: base.h:107
fr_bio_read_t _CONST read
read from the underlying bio
Definition: base.h:106
Definition: base.h:103
fr_bio_t * bio
underlying bio for IO
Definition: packet.h:75
static int fr_bio_packet_read(fr_bio_packet_t *bio, void **request_ctx_p, fr_packet_t **packet_p, TALLOC_CTX *out_ctx, fr_pair_list_t *out)
Read a packet from a packet BIO.
Definition: packet.h:94
fr_bio_packet_write_t write
write to the underlying bio
Definition: packet.h:73
int(* fr_bio_packet_read_t)(fr_bio_packet_t *bio, void **request_ctx_p, fr_packet_t **packet_p, TALLOC_CTX *out_ctx, fr_pair_list_t *out)
Read a packet and pairs from the network.
Definition: packet.h:45
static int fr_bio_packet_write(fr_bio_packet_t *bio, void *request_ctx, fr_packet_t *packet, fr_pair_list_t *list)
Write a packet to a packet BIO.
Definition: packet.h:113
int(* fr_bio_packet_write_t)(fr_bio_packet_t *bio, void *request_ctx, fr_packet_t *packet, fr_pair_list_t *list)
Write a packet and pairs from the network.
Definition: packet.h:57
int(* fr_bio_packet_release_t)(fr_bio_packet_t *bio, fr_packet_t *packet)
Release an outgoing packet.
Definition: packet.h:67
void * uctx
user ctx, caller can manually set it.
Definition: packet.h:70
fr_bio_packet_read_t read
read from the underlying bio
Definition: packet.h:72
#define RCSIDH(h, id)
Definition: build.h:445
static fr_bio_t * bio
Definition: radclient-ng.c:86
#define request_ctx
Talloc ctx for allocating request pairs under.
Definition: request.h:94
int nonnull(2, 5))
static size_t char ** out
Definition: value.h:984