The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
base.c
Go to the documentation of this file.
1#include <freeradius-devel/util/cbor.h>
2#include <freeradius-devel/io/test_point.h>
3
4static ssize_t decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent,
5 uint8_t const *data, size_t data_len, UNUSED void *decode_ctx)
6{
7 fr_dbuff_t dbuff;
8 uint8_t field = 0;
9 ssize_t slen;
10
11 fr_dbuff_init(&dbuff, data, data_len);
12
13 FR_DBUFF_OUT_RETURN(&field, &dbuff);
14 if (field != 0x9f) {
15 fr_strerror_printf("Invalid cbor header - expected indefinite array 9f, got %02x",
16 field);
17 return -1;
18 }
19
20 do {
21 if (fr_dbuff_extend_lowat(NULL, &dbuff, 1) == 0) {
22 fr_strerror_printf("Invalid cbor header - unexpected end of data");
23 return -fr_dbuff_used(&dbuff);
24 }
25
26 field = *fr_dbuff_current(&dbuff);
27 if (field == 0xff) {
28 fr_dbuff_advance(&dbuff, 1);
29 break;
30 }
31
32 slen = fr_cbor_decode_pair(ctx, out, &dbuff, parent, false);
33 if (slen <= 0) return slen - fr_dbuff_used(&dbuff);
34 } while (true);
35
36 return fr_dbuff_used(&dbuff);
37}
38
40{
41 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
43
44 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t) 0x9f); /* indefinite array */
45
46 for (vp = fr_dcursor_current(cursor);
47 vp != NULL;
48 vp = fr_dcursor_next(cursor)) {
49 ssize_t slen;
50
51 slen = fr_cbor_encode_pair(&work_dbuff, vp);
52 if (slen <= 0) return slen - fr_dbuff_used(&work_dbuff);
53 }
54
55 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, (uint8_t) 0xff); /* end of indefinite array */
56
57 return fr_dbuff_set(dbuff, &work_dbuff);
58}
59
60/*
61 * Test points
62 */
67
72
73static int decode_test_ctx(void **out, UNUSED TALLOC_CTX *ctx, fr_dict_t const *dict)
74{
75 *out = UNCONST(fr_dict_t *, dict);
76
77 return 0;
78}
79
80static ssize_t encode_proto(UNUSED TALLOC_CTX *ctx, fr_pair_list_t *vps, uint8_t *data, size_t data_len, UNUSED void *proto_ctx)
81{
82 fr_dbuff_t dbuff;
83 fr_dcursor_t cursor;
84
85 FR_DBUFF_INIT(&dbuff, data, data_len);
86
87 fr_pair_dcursor_init(&cursor, vps);
88 return encode_pair(&dbuff, &cursor, NULL);
89}
90
91static ssize_t decode_proto(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len, void *proto_ctx)
92{
93 fr_dict_t *dict = proto_ctx;
94
95 return decode_pair(ctx, out, fr_dict_root(dict), data, data_len, proto_ctx);
96}
97
102
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define UNUSED
Definition build.h:315
ssize_t fr_cbor_encode_pair(fr_dbuff_t *dbuff, fr_pair_t *vp)
Encode a pair.
Definition cbor.c:1437
ssize_t fr_cbor_decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dbuff_t *dbuff, fr_dict_attr_t const *parent, bool tainted)
Definition cbor.c:1638
#define fr_dbuff_advance(_dbuff_or_marker, _len)
Advance 'current' position in dbuff or marker by _len bytes.
Definition dbuff.h:1072
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition dbuff.h:767
#define fr_dbuff_current(_dbuff_or_marker)
Return the 'current' position of a dbuff or marker.
Definition dbuff.h:911
#define fr_dbuff_set(_dst, _src)
Set the 'current' position in a dbuff or marker using another dbuff or marker, a char pointer,...
Definition dbuff.h:1004
#define fr_dbuff_init(_out, _start, _len_or_end)
Initialise an dbuff for encoding or decoding.
Definition dbuff.h:354
#define FR_DBUFF_INIT(_out, _start, _len_or_end)
Definition dbuff.h:377
#define fr_dbuff_extend_lowat(_status, _dbuff_or_marker, _lowat)
Extend if we're below _lowat.
Definition dbuff.h:660
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:222
#define FR_DBUFF_OUT_RETURN(_out, _dbuff_or_marker)
Copy data from a dbuff or marker to a fixed sized C type returning if there is insufficient data.
Definition dbuff.h:1818
#define FR_DBUFF_IN_BYTES_RETURN(_dbuff_or_marker,...)
Copy a byte sequence into a dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1472
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2400
long int ssize_t
unsigned char uint8_t
static fr_internal_encode_ctx_t encode_ctx
fr_test_point_proto_encode_t cbor_tp_encode_proto
Definition base.c:99
fr_test_point_pair_encode_t cbor_tp_encode_pair
Definition base.c:64
fr_test_point_pair_decode_t cbor_tp_decode_pair
Definition base.c:69
static ssize_t encode_proto(UNUSED TALLOC_CTX *ctx, fr_pair_list_t *vps, uint8_t *data, size_t data_len, UNUSED void *proto_ctx)
Definition base.c:80
static ssize_t decode_proto(TALLOC_CTX *ctx, fr_pair_list_t *out, uint8_t const *data, size_t data_len, void *proto_ctx)
Definition base.c:91
static int decode_test_ctx(void **out, UNUSED TALLOC_CTX *ctx, fr_dict_t const *dict)
Definition base.c:73
fr_test_point_proto_decode_t cbor_tp_decode_proto
Definition base.c:104
static ssize_t decode_pair(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len, UNUSED void *decode_ctx)
Definition base.c:4
static ssize_t encode_pair(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED void *encode_ctx)
Definition base.c:39
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_tp_proto_encode_t func
Encoder for proto layer.
Definition test_point.h:76
fr_pair_decode_t func
Decoder for pairs.
Definition test_point.h:86
fr_pair_encode_t func
Encoder for pairs.
Definition test_point.h:94
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:67
Entry point for pair decoders.
Definition test_point.h:84
Entry point for pair encoders.
Definition test_point.h:92
Entry point for protocol decoders.
Definition test_point.h:66
Entry point for protocol encoders.
Definition test_point.h:74
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:591
static fr_slen_t parent
Definition pair.h:851
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
static fr_slen_t data
Definition value.h:1265
static size_t char ** out
Definition value.h:997