Coverage Report

Created: 2024-08-28 06:17

/src/freeradius-server/src/lib/util/encode.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *   This library is free software; you can redistribute it and/or
3
 *   modify it under the terms of the GNU Lesser General Public
4
 *   License as published by the Free Software Foundation; either
5
 *   version 2.1 of the License, or (at your option) any later version.
6
 *
7
 *   This library is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
 *   Lesser General Public License for more details.
11
 *
12
 *   You should have received a copy of the GNU Lesser General Public
13
 *   License along with this library; if not, write to the Free Software
14
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15
 */
16
17
/**
18
 * $Id: 9a776f1bccb6fda44ce026f2b5226ed31f045b25 $
19
 *
20
 * @file src/lib/util/encode.c
21
 * @brief Generic functions for decoding protocols.
22
 *
23
 * @copyright 2022 Network RADIUS SAS (legal@networkradius.com)
24
 */
25
#include <freeradius-devel/io/test_point.h>
26
#include <freeradius-devel/util/proto.h>
27
#include <freeradius-devel/util/encode.h>
28
29
/** Encode an array of values from the network
30
 *
31
 * @param[out] dbuff    buffer to write the TLV to.
32
 * @param[in] da_stack    Describing nesting of options.
33
 * @param[in] depth   in the da_stack.
34
 * @param[in,out] cursor  Current attribute we're encoding.
35
 * @param[in] encode_ctx  Containing DHCPv4 dictionary.
36
 * @param[in] encode_value  Function to perform encoding of a single value.
37
 * @return
38
 *  - >0 length of data encoded.
39
 *  - 0 if we ran out of space.
40
 *  - < 0 on error.
41
 */
42
ssize_t fr_pair_array_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, int depth,
43
         fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_value)
44
0
{
45
0
  ssize_t     slen;
46
0
  fr_dbuff_t    work_dbuff = FR_DBUFF(dbuff);
47
0
  fr_pair_t   *vp;
48
0
  fr_dict_attr_t const  *da = da_stack->da[depth];
49
50
0
  FR_PROTO_STACK_PRINT(da_stack, depth);
51
52
0
  if (!fr_cond_assert_msg(da->flags.array,
53
0
        "%s: Internal sanity check failed, attribute \"%s\" does not have array bit set",
54
0
        __FUNCTION__, da->name)) return PAIR_ENCODE_FATAL_ERROR;
55
56
0
  while (fr_dbuff_extend(&work_dbuff)) {
57
0
    fr_dbuff_t  element_dbuff = FR_DBUFF(&work_dbuff);
58
59
0
    slen = encode_value(&element_dbuff, da_stack, depth, cursor, encode_ctx);
60
0
    if (slen < 0) return slen;
61
62
0
    fr_dbuff_set(&work_dbuff, &element_dbuff);
63
64
0
    vp = fr_dcursor_current(cursor);
65
0
    if (!vp || (vp->da != da)) break;   /* Stop if we have an attribute of a different type */
66
0
  }
67
68
0
  return fr_dbuff_set(dbuff, &work_dbuff);
69
0
}
70
71
ssize_t fr_pair_cursor_to_network(fr_dbuff_t *dbuff,
72
          fr_da_stack_t *da_stack, unsigned int depth,
73
          fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_pair)
74
0
{
75
0
  fr_dbuff_t    work_dbuff = FR_DBUFF(dbuff);
76
0
  fr_pair_t const   *vp;
77
0
  fr_dict_attr_t const  *da = da_stack->da[depth];
78
0
  ssize_t     len;
79
80
0
  while (true) {
81
0
    FR_PROTO_STACK_PRINT(da_stack, depth);
82
83
0
    vp = fr_dcursor_current(cursor);
84
0
    fr_assert(!vp->da->flags.internal);
85
86
0
    len = encode_pair(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx);
87
0
    if (len < 0) return len;
88
89
    /*
90
     *  If nothing updated the attribute, stop
91
     */
92
0
    if (!fr_dcursor_current(cursor) || (vp == fr_dcursor_current(cursor))) break;
93
94
0
    vp = fr_dcursor_current(cursor);
95
0
    if (!vp) break;
96
97
0
    fr_proto_da_stack_build(da_stack, vp->da);
98
99
    /*
100
     *  We can encode multiple children, if after
101
     *  rebuilding the DA Stack, the attribute at this
102
     *  depth is the same.
103
     */
104
0
    if ((da != da_stack->da[depth]) || (da_stack->depth < da->depth)) break;
105
0
  }
106
107
0
  FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "Done cursor");
108
109
0
  return fr_dbuff_set(dbuff, &work_dbuff);
110
0
}
111
112
/** Encode a foreign reference to the network
113
 *
114
 * @param[out] dbuff    buffer to write the TLV to.
115
 * @param[in] da_stack    Describing nesting of options.
116
 * @param[in] depth   in the da_stack.
117
 * @param[in,out] cursor  Current attribute we're encoding.
118
 * @return
119
 *  - >0 length of data encoded.
120
 *  - 0 if we ran out of space.
121
 *  - < 0 on error.
122
 */
123
ssize_t fr_pair_ref_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth,
124
             fr_dcursor_t *cursor)
125
0
{
126
0
  ssize_t     slen;
127
0
  fr_dict_attr_t const  *da;
128
0
  fr_pair_t const   *vp = fr_dcursor_current(cursor);
129
0
  fr_dbuff_t    work_dbuff = FR_DBUFF(dbuff);
130
131
0
  fr_dict_attr_t const *ref;
132
0
  fr_dict_protocol_t const *proto;
133
134
0
  FR_PROTO_STACK_PRINT(da_stack, depth);
135
136
0
  da = da_stack->da[depth];
137
0
  fr_assert(da->type == FR_TYPE_GROUP);
138
139
0
  ref = fr_dict_attr_ref(da);
140
0
  if (!ref) {
141
0
    fr_strerror_printf("Invalid attribute reference for %s", da->name);
142
0
    return 0;
143
0
  }
144
145
0
  proto = fr_dict_protocol(ref->dict);
146
0
  fr_assert(proto != NULL);
147
148
0
  if (!proto->encode) {
149
0
    fr_strerror_printf("Attribute %s -> %s does not have an encoder", da->name, ref->name);
150
0
    return 0;
151
0
  }
152
153
  /*
154
   *  The foreign functions don't take a cursor, so we have to update the cursor ourselves.
155
   */
156
0
  slen = proto->encode(&work_dbuff, &vp->vp_group);
157
0
  if (slen <= 0) return slen;
158
159
0
  FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "group ref");
160
161
0
  vp = fr_dcursor_next(cursor);
162
0
  fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
163
164
0
  return fr_dbuff_set(dbuff, &work_dbuff);
165
0
}