The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
pair.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: 037708240c294b95845d0abd2565b80283fc48ae $
20  *
21  * @file io/pair.h
22  * @brief Encoder/decoder library interface
23  *
24  * @copyright 2017-2020 The FreeRADIUS project
25  */
26 #include <freeradius-devel/util/dcursor.h>
27 #include <freeradius-devel/util/value.h>
28 #include <freeradius-devel/util/pair.h>
29 #include <freeradius-devel/server/request.h>
30 
31 /** @name Encoder errors
32  * @{
33  */
34 
35 /** Fatal encoding error
36  */
37 #define PAIR_ENCODE_FATAL_ERROR SSIZE_MIN
38 
39 /** @} */
40 
41 /** @name Decode errors
42  * @{
43  */
44 /** Fatal error - Out of memory
45  */
46 #define PAIR_DECODE_OOM FR_VALUE_BOX_NET_OOM
47 
48 /** Fatal error - Failed decoding the packet
49  */
50 #define PAIR_DECODE_FATAL_ERROR FR_VALUE_BOX_NET_ERROR
51 
52 /** Return the correct adjusted slen for errors
53  *
54  * @param[in] slen returned from the function we called.
55  * @param[in] start of the buffer.
56  * @param[in] p offset passed to function which returned the slen.
57  */
58 static inline ssize_t fr_pair_decode_slen(ssize_t slen, uint8_t const *start, uint8_t const *p)
59 {
60  if (slen > 0) return slen;
61 
62  switch (slen) {
63  case PAIR_DECODE_OOM:
65  return slen;
66 
67  default:
68  return slen - (p - start);
69  }
70 }
71 
72 /** Determine if the return code for an encoding function is a fatal error
73  *
74  */
75 static inline bool fr_pair_encode_is_error(ssize_t slen)
76 {
77  if (slen == PAIR_ENCODE_FATAL_ERROR) return true;
78  return false;
79 }
80 
81 /** Checks if we have sufficient buffer space, and returns how much space we'd need as a negative integer
82  *
83  */
84 #define FR_PAIR_ENCODE_HAVE_SPACE(_p, _end, _num) if (((_p) + (_num)) > (_end)) return (_end) - ((_p) + (_num));
85 
86 /** @} */
87 
88 /** Generic interface for encoding one or more fr_pair_ts
89  *
90  * An encoding function should consume at most, one top level fr_pair_t and encode
91  * it in the appropriate wire format for the protocol, writing the encoded data to
92  * out, and returning the encoded length.
93  *
94  * The exception to processing one fr_pair_t is if multiple fr_pair_ts can be aggregated
95  * into a single TLV, in which case the encoder may consume as many fr_pair_ts as will
96  * fit into that TLV.
97  *
98  * Outlen provides the length of the buffer to write the encoded data to. The return
99  * value must not be greater than outlen.
100  *
101  * The cursor is used to track how many pairs there are remaining.
102  *
103  * @param[out] out Where to write the encoded data.
104  * @param[in] cursor Cursor containing the list of attributes to process.
105  * @param[in] encode_ctx Any encoder specific data such as secrets or configurables.
106  * @return
107  * - PAIR_ENCODE_FATAL_ERROR - Encoding failed in a fatal way. Encoding the packet should be
108  * aborted in its entirety.
109  * - <0 - The encoder ran out of space and returned the number of bytes as a negative
110  * integer that would be required to encode the attribute.
111  * - >0 - The number of bytes written to out.
112  */
114 
115 /** A generic interface for decoding fr_pair_ts
116  *
117  * A decoding function should decode a single top level fr_pair_t from wire format.
118  * If this top level fr_pair_t is a TLV, multiple child attributes may also be decoded.
119  *
120  * @param[in] ctx to allocate new pairs in.
121  * @param[in] out to insert new pairs into.
122  * @param[in] parent to use for decoding
123  * @param[in] data to decode.
124  * @param[in] data_len The length of the incoming data.
125  * @param[in] decode_ctx Any decode specific data such as secrets or temporary allocation contexts
126  * @return
127  * - <= 0 on error. May be the offset (as a negative value) where the error occurred.
128  * - > 0 on success. How many bytes were decoded.
129  */
130 typedef ssize_t (*fr_pair_decode_t)(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent,
131  uint8_t const *data, size_t data_len, void *decode_ctx);
#define PAIR_DECODE_OOM
Fatal error - Out of memory.
Definition: pair.h:46
#define PAIR_DECODE_FATAL_ERROR
Fatal error - Failed decoding the packet.
Definition: pair.h:50
ssize_t(* fr_pair_encode_t)(fr_dbuff_t *out, fr_dcursor_t *cursor, void *encode_ctx)
Generic interface for encoding one or more fr_pair_ts.
Definition: pair.h:113
ssize_t(* fr_pair_decode_t)(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, void *decode_ctx)
A generic interface for decoding fr_pair_ts.
Definition: pair.h:130
#define PAIR_ENCODE_FATAL_ERROR
Fatal encoding error.
Definition: pair.h:37
static ssize_t fr_pair_decode_slen(ssize_t slen, uint8_t const *start, uint8_t const *p)
Return the correct adjusted slen for errors.
Definition: pair.h:58
static bool fr_pair_encode_is_error(ssize_t slen)
Determine if the return code for an encoding function is a fatal error.
Definition: pair.h:75
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
static fr_internal_encode_ctx_t encode_ctx
static fr_slen_t parent
Definition: pair.h:844
static fr_slen_t data
Definition: value.h:1259
static size_t char ** out
Definition: value.h:984