The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
struct.c
Go to the documentation of this file.
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 /** Functions to encode / decode structures on the wire
18  *
19  * @file src/lib/util/struct.c
20  *
21  * @copyright 2018 The FreeRADIUS server project
22  * @copyright 2018 Alan DeKok (aland@freeradius.org)
23  */
24 RCSID("$Id: a87a9510bfb7653ae32547900fd3e44143a12ac2 $")
25 
26 #include <freeradius-devel/util/struct.h>
27 #include <freeradius-devel/util/encode.h>
28 #include <freeradius-devel/io/pair.h>
29 
30 /** Convert a STRUCT to one or more VPs
31  *
32  */
34  fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len,
35  void *decode_ctx,
37 {
38  unsigned int child_num;
39  uint8_t const *p = data, *end = data + data_len;
40  fr_dict_attr_t const *child;
41  fr_pair_list_t child_list_head;
42  fr_pair_list_t *child_list;
43  fr_pair_t *vp, *key_vp, *struct_vp = NULL;
44  unsigned int offset = 0;
45  TALLOC_CTX *child_ctx;
46  ssize_t slen;
47 
48  if (data_len == 0) {
49  fr_strerror_const("struct decoder was passed zero bytes of data");
50  return -1; /* at least one byte of data */
51  }
52 
53  FR_PROTO_HEX_DUMP(data, data_len, "fr_struct_from_network");
54 
55  /*
56  * Start a child list.
57  */
59 
60  struct_vp = fr_pair_afrom_da(ctx, parent);
61  if (!struct_vp) {
62  fr_strerror_const("out of memory");
63  return -1;
64  }
65 
66  fr_pair_list_init(&child_list_head); /* still used elsewhere */
67  child_list = &struct_vp->vp_group;
68  child_ctx = struct_vp;
69  child_num = 1;
70  key_vp = NULL;
71 
72  /*
73  * Decode structs with length prefixes.
74  */
76  size_t claimed_len, field_len, calc_len;
77 
78  /*
79  * Set how many bytes there are in the "length" field.
80  */
81  if (parent->flags.subtype == FLAG_LENGTH_UINT8) {
82  field_len = 1;
83  } else {
84  field_len = 2;
85  }
86 
87  if ((size_t) (end - p) < field_len) {
88  FR_PROTO_TRACE("Insufficient room for length field");
89  goto unknown;
90  }
91 
92  claimed_len = p[0];
93  if (field_len > 1) {
94  claimed_len <<= 8;
95  claimed_len |= p[1];
96  }
97  p += field_len;
98 
99  if (claimed_len < da_length_offset(parent)) {
100  FR_PROTO_TRACE("Length header (%zu) is smaller than minimum value (%u)",
101  claimed_len, parent->flags.type_size);
102  goto unknown;
103  }
104 
105  /*
106  * Get the calculated length of the actual data.
107  */
108  calc_len = claimed_len - da_length_offset(parent);
109 
110  if (calc_len > (size_t) (end - p)) {
111  FR_PROTO_TRACE("Length header (%zu) is larger than remaining data (%zu)",
112  claimed_len + field_len, (end - p));
113  goto unknown;
114  }
115 
116  /*
117  * Limit the size of the decoded structure to the correct length.
118  */
119  data_len = calc_len;
120  end = p + data_len;
121  }
122 
123  /*
124  * @todo - If the struct is truncated on a MEMBER boundary, we silently omit
125  * the trailing members. Maybe this should be an error?
126  */
127  while (p < end) {
128  size_t child_length;
129 
130  /*
131  * Go to the next child. If it doesn't exist, we're done.
132  */
133  child = fr_dict_attr_child_by_num(parent, child_num);
134  if (!child) break;
135 
136  FR_PROTO_HEX_DUMP(p, (end - p), "fr_struct_from_network - child %s (%d)", child->name, child->attr);
137 
138  /*
139  * Check for bit fields.
140  */
141  if (da_is_bit_field(child)) {
142  uint8_t array[8];
143  unsigned int num_bits;
144  uint64_t value;
145 
146  num_bits = offset + child->flags.length;
147  if ((size_t)(end - p) < fr_bytes_from_bits(num_bits)) {
148  FR_PROTO_TRACE("not enough data for bit decoder?");
149  goto unknown;
150  }
151 
152  memset(array, 0, sizeof(array));
153  memcpy(&array[0], p, fr_bytes_from_bits(num_bits));
154 
155  if (offset > 0) array[0] &= (1 << (8 - offset)) - 1; /* mask off bits we don't care about */
156 
157  memcpy(&value, &array[0], sizeof(value));
158  value = htonll(value);
159  value >>= (8 - offset); /* move it to the lower bits */
160  value >>= (56 - child->flags.length);
161 
162  vp = fr_pair_afrom_da(child_ctx, child);
163  if (!vp) {
164  FR_PROTO_TRACE("fr_struct_from_network - failed allocating child VP");
165  return PAIR_DECODE_OOM;
166  }
167 
168  switch (child->type) {
169  case FR_TYPE_BOOL:
170  vp->vp_bool = value;
171  break;
172 
173  case FR_TYPE_UINT8:
174  vp->vp_uint8 = value;
175  break;
176 
177  case FR_TYPE_UINT16:
178  vp->vp_uint16 = value;
179  break;
180 
181  case FR_TYPE_UINT32:
182  vp->vp_uint32 = value;
183  break;
184 
185  case FR_TYPE_UINT64:
186  vp->vp_uint64 = value;
187  break;
188 
189  default:
190  FR_PROTO_TRACE("Can't decode unknown type?");
191  goto unknown;
192  }
193 
194  vp->vp_tainted = true;
195  fr_pair_append(child_list, vp);
196  p += (num_bits >> 3); /* go to the LAST bit, not the byte AFTER the last bit */
197  offset = num_bits & 0x07;
198  child_num++;
199  continue;
200  }
201  offset = 0; /* reset for non-bit-field attributes */
202 
203  /*
204  * Decode child TLVs, according to the parent attribute.
205  */
206  if (child->type == FR_TYPE_TLV) {
207  fr_assert(!key_vp);
208 
209  if (!decode_tlv) {
210  fr_strerror_const("Decoding TLVs requires a decode_tlv() function to be passed");
211  return -(p - data);
212  }
213 
214  /*
215  * Decode EVERYTHING as a TLV.
216  */
217  while (p < end) {
218  slen = decode_tlv(child_ctx, child_list, child, p, end - p, decode_ctx);
219  if (slen < 0) {
220  FR_PROTO_TRACE("failed decoding TLV?");
221  goto unknown;
222  }
223  p += slen;
224  }
225 
226  goto done;
227  }
228 
229  child_length = child->flags.length;
230 
231  /*
232  * If this field overflows the input, then *all*
233  * of the input is suspect.
234  */
235  if (child_length > (size_t) (end - p)) {
236  FR_PROTO_TRACE("fr_struct_from_network - child length %zd overflows buffer", child_length);
237  goto unknown;
238  }
239 
240  /*
241  * The child is variable sized, OR it's an array.
242  * Eat up the rest of the data.
243  */
244  if (!child_length || (child->flags.array)) {
245  child_length = end - p;
246 
247  } else if ((size_t) (end - p) < child_length) {
248  FR_PROTO_TRACE("fr_struct_from_network - child length %zd underflows buffer", child_length);
249  goto unknown;
250  }
251 
252  /*
253  * Magic values get the callback called.
254  *
255  * @todo - if this is an array of DNS labels, we
256  * need to do decompression checks on the entire
257  * block, and then decode each field
258  * individually.
259  */
260  if (decode_value) {
261  if (child->flags.array) {
262  slen = fr_pair_array_from_network(child_ctx, child_list, child, p, child_length, decode_ctx, decode_value);
263  } else {
264  slen = decode_value(child_ctx, child_list, child, p, child_length, decode_ctx);
265  }
266  if (slen < 0) {
267  FR_PROTO_TRACE("Failed decoding value");
268  goto unknown;
269  }
270 
271  p += slen; /* not always the same as child->flags.length */
272  child_num++; /* go to the next child */
273  if (fr_dict_attr_is_key_field(child)) key_vp = fr_pair_list_tail(child_list);
274  continue;
275  }
276 
277  /*
278  * We only allow a limited number of data types
279  * inside of a struct.
280  */
281  switch (child->type) {
282  default:
283  FR_PROTO_TRACE("fr_struct_from_network - unknown child type");
284  goto unknown;
285 
286  case FR_TYPE_LEAF:
287  break;
288  }
289 
290  /*
291  * We don't handle this yet here.
292  */
293  fr_assert(!child->flags.array);
294 
295  vp = fr_pair_afrom_da(child_ctx, child);
296  if (!vp) {
297  FR_PROTO_TRACE("fr_struct_from_network - failed allocating child VP");
298  goto unknown;
299  }
300 
301  /*
302  * No protocol-specific data types here (yet).
303  *
304  * If we can't decode this field, then the entire
305  * structure is treated as a raw blob.
306  */
307  if (fr_value_box_from_network(vp, &vp->data, vp->vp_type, vp->da,
308  &FR_DBUFF_TMP(p, child_length), child_length, true) < 0) {
309  FR_PROTO_TRACE("fr_struct_from_network - failed decoding child VP %s", vp->da->name);
310  talloc_free(vp);
311  unknown:
312  TALLOC_FREE(struct_vp);
313 
314  slen = fr_pair_raw_from_network(ctx, out, parent, data, data_len);
315  if (slen < 0) return slen;
316  return data_len;
317  }
318 
319  vp->vp_tainted = true;
320  fr_pair_append(child_list, vp);
321 
322  if (fr_dict_attr_is_key_field(vp->da)) key_vp = vp;
323 
324  /*
325  * Note that we're decoding fixed fields here.
326  * So we skip the input based on the *known*
327  * length, and not on the *decoded* length.
328  */
329  p += child_length;
330  child_num++; /* go to the next child */
331  }
332 
333  /*
334  * Is there a substructure after this one? If so, go
335  * decode it.
336  */
337  if (key_vp) {
338  fr_dict_enum_value_t const *enumv;
339  child = NULL;
340 
341  FR_PROTO_HEX_DUMP(p, (end - p), "fr_struct_from_network - substruct");
342 
343  /*
344  * Nothing more to decode, don't decode it.
345  */
346  if (p >= end) {
347  FR_PROTO_TRACE("Expected substruct, but there is none. We're done decoding this structure");
348  goto done;
349  }
350 
351  enumv = fr_dict_enum_by_value(key_vp->da, &key_vp->data);
352  if (enumv) child = enumv->child_struct[0];
353 
354  if (!child) {
355  unknown_child:
356  /*
357  * Always encode the unknown child as
358  * attribute number 0. Since the unknown
359  * children have no "real" number, and
360  * are all unique da's, they are
361  * incomparable. And thus can all be
362  * given the same number.
363  */
364  child = fr_dict_attr_unknown_raw_afrom_num(child_ctx, key_vp->da, 0);
365  if (!child) {
366  FR_PROTO_TRACE("failed allocating unknown child for key VP %s - %s",
367  key_vp->da->name, fr_strerror());
368  goto unknown;
369  }
370 
371  slen = fr_pair_raw_from_network(child_ctx, child_list, child, p, end - p);
372  if (slen < 0) {
373  FR_PROTO_TRACE("Failed creating raw VP from malformed or unknown substruct for child %s", child->name);
375  return slen;
376  }
377 
378  p = end;
379  } else {
380  fr_assert(child->type == FR_TYPE_STRUCT);
381 
382  slen = fr_struct_from_network(child_ctx, child_list, child, p, end - p,
383  decode_ctx, decode_value, decode_tlv);
384  if (slen <= 0) {
385  FR_PROTO_TRACE("substruct %s decoding failed", child->name);
386  goto unknown_child;
387  }
388  p += slen;
389  }
390 
392  }
393 
394 done:
395  fr_assert(struct_vp != NULL);
396  fr_pair_append(out, struct_vp);
397 
398  FR_PROTO_TRACE("used %zd bytes", data_len);
399  return p - data;
400 }
401 
402 
403 /** Put bits into an output dbuff
404  *
405  * @param dbuff where the bytes go
406  * @param p where leftover bits go
407  * @param start_bit start bit in the dbuff where the data goes, 0..7
408  * @param num_bits number of bits to write to the output, 0..55
409  * @param data data to write, all in the lower "num_bits" of the uint64_t variable
410  * @return
411  * >= 0 the next value to pass in for start_bit
412  * < 0 no space or invalid start_bit or num_bits parameter
413  */
414 static int put_bits_dbuff(fr_dbuff_t *dbuff, uint8_t *p, int start_bit, uint8_t num_bits, uint64_t data)
415 {
416  uint64_t used_bits;
417 
418  if (start_bit < 0 || start_bit > 7) return -1;
419  if (num_bits < 1 || num_bits > 56) return -1;
420 
421  /* Get bits buffered in *p */
422  used_bits = *p & (-256 >> start_bit);
423 
424  /* Mask out all but the least significant num_bits bits of data */
425  data &= (((uint64_t) 1) << num_bits) - 1;
426 
427  /* Move it towards the most significant end and put used_bits at the top */
428  data <<= (64 - (start_bit + num_bits));
429  data |= used_bits << 56;
430 
431  data = htonll(data);
432 
433  start_bit += num_bits;
434  if (start_bit > 7) FR_DBUFF_IN_MEMCPY_RETURN(dbuff, (uint8_t const *) &data, (size_t)(start_bit / 8));
435 
436  *p = ((uint8_t *) &data)[start_bit / 8];
437  return start_bit % 8;
438 }
439 
440 static int8_t pair_sort_increasing(void const *a, void const *b)
441 {
442  fr_pair_t const *my_a = a;
443  fr_pair_t const *my_b = b;
444  int rcode;
445 
446  /*
447  * Deeper attributes come later in the list.
448  */
449  rcode = CMP_PREFER_SMALLER(my_a->da->depth, my_b->da->depth);
450  if (rcode != 0) return rcode;
451 
452  return CMP_PREFER_SMALLER(my_a->da->attr, my_b->da->attr);
453 }
454 
455 static void *struct_next_encodable(fr_dlist_head_t *list, void *current, void *uctx)
456 {
457  fr_pair_t *c = current;
458  fr_dict_attr_t *parent = talloc_get_type_abort(uctx, fr_dict_attr_t);
459 
460  while ((c = fr_dlist_next(list, c))) {
461  PAIR_VERIFY(c);
462 
463  if (c->da->dict != parent->dict || c->da->flags.internal) continue;
464  break;
465  }
466 
467  return c;
468 }
469 
471  fr_da_stack_t *da_stack, unsigned int depth,
472  fr_dcursor_t *parent_cursor, void *encode_ctx,
474 {
475  fr_dbuff_t work_dbuff;
476  fr_dbuff_marker_t hdr;
477  int offset = 0;
478  unsigned int child_num = 1;
479  bool do_length = false;
480  uint8_t bit_buffer = 0;
481  fr_pair_t const *vp = fr_dcursor_current(parent_cursor);
482  fr_dict_attr_t const *key_da, *parent, *tlv = NULL;
483  fr_dcursor_t child_cursor, *cursor;
484  size_t prefix_length = 0;
485 
486  if (!vp) {
487  fr_strerror_printf("%s: Can't encode empty struct", __FUNCTION__);
489  }
490 
491  PAIR_VERIFY(vp);
492  parent = da_stack->da[depth];
493 
494  if (parent->type != FR_TYPE_STRUCT) {
495  fr_strerror_printf("%s: Expected type \"struct\" got \"%s\"", __FUNCTION__,
496  fr_type_to_str(parent->type));
498  }
499 
500  /*
501  * If we get passed a struct VP, sort its children.
502  */
503  if (vp->vp_type == FR_TYPE_STRUCT) {
504  fr_pair_t *sorted = fr_dcursor_current(parent_cursor); /* NOT const */
505 
506  fr_pair_list_sort(&sorted->vp_group, pair_sort_increasing);
507  fr_pair_dcursor_iter_init(&child_cursor, &sorted->vp_group, struct_next_encodable, parent);
508 
509  /*
510  * Build the da_stack for the new structure.
511  */
512  vp = fr_dcursor_current(&child_cursor);
513  fr_proto_da_stack_build(da_stack, vp ? vp->da : NULL);
514 
515  FR_PROTO_TRACE("fr_struct_to_network encoding nested with parent %s", parent->name);
516  cursor = &child_cursor;
517  } else {
518  FR_PROTO_TRACE("fr_struct_to_network encoding flat");
519  cursor = parent_cursor;
520  }
521 
522  /*
523  * @todo - if we get a child which *eventually* has the
524  * given parent, then allow encoding of that struct, too.
525  * This allows us to encode structures automatically,
526  * even if key fields are omitted.
527  *
528  * Note that this check catches TLVs which are "flat" and
529  * not nested. We could fix that by adding a special
530  * case, but it's better to just fix everything to handle
531  * nested attributes.
532  */
533  if (vp && (vp->da->parent != parent)) {
534  fr_strerror_printf("%s: Asked to encode %s, but its parent %s is not the expected parent %s",
535  __FUNCTION__, vp->da->name, vp->da->parent->name, parent->name);
537  }
538 
539  key_da = NULL;
540 
541  /*
542  * Some structs are prefixed by a 16-bit length.
543  */
544  if (!da_is_length_field(parent)) {
545  work_dbuff = FR_DBUFF(dbuff);
546  } else {
547  if (parent->flags.subtype == FLAG_LENGTH_UINT8) {
548  work_dbuff = FR_DBUFF_MAX(dbuff, 256);
549  fr_dbuff_marker(&hdr, &work_dbuff);
550 
551  FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 1);
552  prefix_length = 1;
553  } else {
554  work_dbuff = FR_DBUFF_MAX(dbuff, 65536);
555  fr_dbuff_marker(&hdr, &work_dbuff);
556 
557  FR_DBUFF_ADVANCE_RETURN(&work_dbuff, 2);
558  prefix_length = 2;
559  }
560  do_length = true;
561  }
562 
563  for (;;) {
564  fr_dict_attr_t const *child;
565 
566  /*
567  * The child attributes should be in order. If
568  * they're not, we fill the struct with zeroes.
569  *
570  * The caller will encode TLVs.
571  */
572  child = fr_dict_attr_child_by_num(parent, child_num);
573  if (!child) break;
574 
575  FR_PROTO_TRACE("fr_struct_to_network child %s", child->name);
576 
577  /*
578  * Encode child TLVs at the end of a struct.
579  *
580  * In order to encode the child TLVs, we need to
581  * know the length of "T" and "L", and we don't.
582  * So just let the caller do the work.
583  */
584  if (child->type == FR_TYPE_TLV) {
585  if (offset != 0) goto leftover_bits;
586 
587  fr_assert(!key_da);
588 
589  tlv = child;
590  goto done;
591  }
592 
593  /*
594  * The MEMBER may be raw, in which case it is encoded as octets.
595  *
596  * This can happen for the last MEMBER of a struct, such as when the last member is a TLV
597  * or GROUP, and the contents are malformed.
598  *
599  * It can also happen if a middle MEMBER has the right length, but the wrong contents.
600  * e.g. when the contents have to be a well-formed IP prefix, but the prefix values are
601  * out of the permitted range.
602  */
603  if (vp && (vp->da != child) && (vp->da->parent == parent) && (vp->da->attr == child_num)) {
604  fr_assert(vp->vp_raw);
605  fr_assert(vp->vp_type == FR_TYPE_OCTETS);
606  fr_assert(!da_is_bit_field(child));
607 
608  goto raw;
609  }
610 
611  /*
612  * Skipped a VP, or left one off at the end, fill the struct with zeros.
613  */
614  if (!vp || (vp->da != child)) {
615  FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), " no child %s", child->name);
616 
617  /*
618  * Zero out the bit field.
619  */
620  if (da_is_bit_field(child)) {
621  offset = put_bits_dbuff(&work_dbuff, &bit_buffer, offset, child->flags.length, 0);
622  if (offset < 0) {
623  fr_strerror_printf("Failed encoding bit field %s", child->name);
624  return offset;
625  }
626  child_num++;
627  continue;
628  }
629 
630  if (fr_dict_attr_is_key_field(child)) {
631  key_da = child;
632  }
633 
634  /*
635  * Zero out the unused field.
636  */
637  FR_DBUFF_MEMSET_RETURN(&work_dbuff, 0, child->flags.length);
638  child_num++;
639  continue;
640  }
641 
642  /*
643  * The 'struct' encoder handles bit fields.
644  * They're just integers, so there's no need to
645  * call the protocol encoder.
646  *
647  * This limitation means that we can't have
648  * encrypted bit fields, but that's fine.
649  */
650  if (da_is_bit_field(child)) {
651  uint64_t value;
652 
653  switch (child->type) {
654  case FR_TYPE_BOOL:
655  value = vp->vp_bool;
656  break;
657 
658  case FR_TYPE_UINT8:
659  value = vp->vp_uint8;
660  break;
661 
662  case FR_TYPE_UINT16:
663  value = vp->vp_uint16;
664  break;
665 
666  case FR_TYPE_UINT32:
667  value = vp->vp_uint32;
668  break;
669 
670  case FR_TYPE_UINT64:
671  value = vp->vp_uint64;
672  break;
673 
674  default:
675  fr_strerror_const("Invalid bit field");
677  }
678 
679  offset = put_bits_dbuff(&work_dbuff, &bit_buffer, offset, child->flags.length, value);
680  if (offset < 0) {
681  fr_strerror_printf("Failed encoding bit field %s", child->name);
682  return offset;
683  }
684 
685  vp = fr_dcursor_next(cursor);
686  /* We need to continue, there may be more fields to encode */
687 
688  goto next;
689  }
690 
691  /* Not a bit field; insist that no buffered bits remain. */
692  if (offset != 0) {
693  leftover_bits:
694  fr_strerror_const("leftover bits");
696  }
697 
698  /*
699  * Remember key_da before we do any encoding.
700  */
701  if (fr_dict_attr_is_key_field(child)) {
702  key_da = child;
703  }
704 
705  if (encode_value) {
706  ssize_t len;
707  /*
708  * Call the protocol encoder for non-bit fields.
709  */
710  fr_proto_da_stack_build(da_stack, child);
711 
712  if (child->flags.array) {
713  len = fr_pair_array_to_network(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx, encode_value);
714  } else {
715  len = encode_value(&work_dbuff, da_stack, depth + 1, cursor, encode_ctx);
716  }
717  if (len < 0) return len;
718  vp = fr_dcursor_current(cursor);
719 
720  } else {
721  redo:
722  /*
723  * Hack until we find all places that don't set data.enumv
724  */
725  if (vp->da->flags.length && (vp->data.enumv != vp->da)) {
726  fr_dict_attr_t const * const *c = &vp->data.enumv;
727  fr_dict_attr_t **u;
728 
729  memcpy(&u, &c, sizeof(c)); /* const issues */
730  memcpy(u, &vp->da, sizeof(vp->da));
731  }
732 
733  /*
734  * Determine the nested type and call the appropriate encoder
735  */
736  raw:
737  if (fr_value_box_to_network(&work_dbuff, &vp->data) <= 0) return PAIR_ENCODE_FATAL_ERROR;
738 
739  vp = fr_dcursor_next(cursor);
740  if (!vp) break;
741 
742  if (child->flags.array && (vp->da == child)) goto redo;
743  }
744 
745  next:
746  FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "fr_struct_to_network after child %s", child->name);
747  child_num++;
748  }
749 
750  /* Check for leftover bits */
751  if (offset != 0) goto leftover_bits;
752 
753  /*
754  * Check for keyed data to encode.
755  */
756  if (vp && key_da) {
757  FR_PROTO_TRACE("fr_struct_to_network encoding key %s", key_da->name);
758 
759  /*
760  * If our parent is a struct, AND its parent is
761  * the key_da, then we have a keyed struct for
762  * the child. Go encode it.
763  *
764  * This check is really for "nested" VPs.
765  */
766  if ((vp->da->parent == key_da) &&
767  (vp->vp_type == FR_TYPE_STRUCT)) {
768  ssize_t len;
769  fr_proto_da_stack_build(da_stack, vp->da);
770 
771  len = fr_struct_to_network(&work_dbuff, da_stack, depth + 2, /* note + 2 !!! */
773  if (len < 0) return len;
774  goto done;
775  }
776 
777  /*
778  * If our parent is a struct, AND its parent is
779  * the key_da, then we have a keyed struct for
780  * the child. Go encode it.
781  *
782  * This check is really for "flat" VPs.
783  */
784  if ((vp->da->parent->parent == key_da) &&
785  (vp->da->parent->type == FR_TYPE_STRUCT)) {
786  ssize_t len;
787  fr_proto_da_stack_build(da_stack, vp->da->parent);
788 
789  len = fr_struct_to_network(&work_dbuff, da_stack, depth + 2, /* note + 2 !!! */
791  if (len < 0) return len;
792  goto done;
793  }
794 
795  /*
796  * The next VP is likely octets and unknown.
797  */
798  if ((vp->da->parent == key_da) &&
799  (vp->vp_type != FR_TYPE_TLV)) {
800  if (fr_value_box_to_network(&work_dbuff, &vp->data) <= 0) return PAIR_ENCODE_FATAL_ERROR;
801  (void) fr_dcursor_next(cursor);
802  goto done;
803  }
804 
805  /*
806  * We have no idea what to do. Ignore it.
807  */
808  }
809 
810 done:
811  vp = fr_dcursor_current(cursor);
812  if (tlv && vp && (vp->da == tlv) && encode_pair) {
813  ssize_t slen;
814  fr_dcursor_t tlv_cursor;
815 
816  if (!encode_pair) {
817  fr_strerror_printf("Asked to encode child attribute %s, but we were not passed an encoding function",
818  tlv->name);
820  }
821 
823 
824  vp = fr_pair_dcursor_init(&tlv_cursor, &vp->vp_group);
825  if (vp) {
826  FR_PROTO_TRACE("fr_struct_to_network trailing TLVs of %s", tlv->name);
827  fr_proto_da_stack_build(da_stack, vp->da);
828  FR_PROTO_STACK_PRINT(da_stack, depth);
829 
830  slen = fr_pair_cursor_to_network(&work_dbuff, da_stack, depth + 1, &tlv_cursor, encode_ctx, encode_pair);
831  if (slen < 0) return slen;
832  }
833  }
834 
835  if (do_length) {
836  size_t length = fr_dbuff_used(&work_dbuff);
837 
838 #ifdef __COVERITY__
839  /*
840  * Coverity somehow can't infer that length
841  * is at least as long as the prefix, instead
842  * thinkings it's zero so that it underflows.
843  * We therefore add a Coverity-only check to
844  * reassure it.
845  */
846  if (length < prefix_length) return PAIR_ENCODE_FATAL_ERROR;
847 #endif
848  if (parent->flags.subtype == FLAG_LENGTH_UINT8) {
849  length -= prefix_length;
850 
851  length += da_length_offset(parent);
852 
853  if (length > UINT8_MAX) return PAIR_ENCODE_FATAL_ERROR;
854 
855  (void) fr_dbuff_in(&hdr, (uint8_t) length);
856  } else {
857  length -= prefix_length;
858 
859  length += da_length_offset(parent);
860 
861  if (length > UINT16_MAX) return PAIR_ENCODE_FATAL_ERROR;
862 
863  (void) fr_dbuff_in(&hdr, (uint16_t) length);
864  }
865  }
866 
867  /*
868  * We've encoded the children, so tell the parent cursor
869  * that we've encoded the parent.
870  */
871  if (cursor != parent_cursor) (void) fr_dcursor_next(parent_cursor);
872 
873  FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "Done fr_struct_to_network");
874 
875  return fr_dbuff_set(dbuff, &work_dbuff);
876 }
#define RCSID(id)
Definition: build.h:481
#define CMP_PREFER_SMALLER(_a, _b)
Evaluates to +1 for a > b, and -1 for a < b.
Definition: build.h:102
#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_ADVANCE_RETURN(_dbuff_or_marker, _len)
Advance the 'current' position in dbuff or marker by _len bytes returning if _len is out of range.
Definition: dbuff.h:1088
struct fr_dbuff_marker_s fr_dbuff_marker_t
A position marker associated with a dbuff.
Definition: dbuff.h:81
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition: dbuff.h:898
#define FR_DBUFF_MEMSET_RETURN(_dbuff_or_marker, _c, _inlen)
Set _inlen bytes of a dbuff or marker to _c returning if there is insufficient space.
Definition: dbuff.h:1508
#define FR_DBUFF_IN_MEMCPY_RETURN(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into dbuff or marker returning if there's insufficient space.
Definition: dbuff.h:1382
#define fr_dbuff_in(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff or marker.
Definition: dbuff.h:1567
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition: dbuff.h:222
#define FR_DBUFF_MAX(_dbuff_or_marker, _max)
Limit the maximum number of bytes available in the dbuff when passing it to another function.
Definition: dbuff.h:301
static uint8_t * fr_dbuff_marker(fr_dbuff_marker_t *m, fr_dbuff_t *dbuff)
Initialises a new marker pointing to the 'current' position of the dbuff.
Definition: dbuff.h:1192
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition: dbuff.h:514
next
Definition: dcursor.h:178
fr_dcursor_eval_t void const * uctx
Definition: dcursor.h:546
fr_dcursor_iter_t void * current
Definition: dcursor.h:148
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
ssize_t(* fr_pair_decode_value_t)(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t const data_len, void *decode_ctx)
Decode a value from the network into an output fr_pair_list_t.
Definition: decode.h:45
@ FLAG_LENGTH_UINT8
string / octets type is prefixed by uint8 of length
Definition: dict.h:148
#define da_length_offset(_da)
Definition: dict.h:155
fr_dict_enum_value_t * fr_dict_enum_by_value(fr_dict_attr_t const *da, fr_value_box_t const *value)
Lookup the structure representing an enum value in a fr_dict_attr_t.
Definition: dict_util.c:3349
#define da_is_bit_field(_da)
Definition: dict.h:153
void fr_dict_attr_unknown_free(fr_dict_attr_t const **da)
Free dynamically allocated (unknown attributes)
Definition: dict_unknown.c:148
#define da_is_length_field(_da)
Definition: dict.h:154
static fr_dict_attr_t * fr_dict_attr_unknown_raw_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
Definition: dict.h:577
fr_dict_attr_t const * fr_dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Check if a child attribute exists in a parent using an attribute number.
Definition: dict_util.c:3328
#define fr_dict_attr_is_key_field(_da)
Definition: dict.h:152
fr_dict_attr_t const * child_struct[]
for key fields
Definition: dict.h:234
Value of an enumerated attribute.
Definition: dict.h:226
Test enumeration values.
Definition: dict_test.h:92
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition: dlist.h:555
Head of a doubly linked list.
Definition: dlist.h:51
ssize_t(* fr_encode_dbuff_t)(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx)
Typedefs for simplifying the use and declaration of protocol encoders.
Definition: encode.h:37
#define PAIR_DECODE_OOM
Fatal error - Out of memory.
Definition: pair.h:45
#define PAIR_ENCODE_FATAL_ERROR
Fatal encoding error.
Definition: pair.h:36
static ssize_t encode_value(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, int depth, fr_dcursor_t *cursor, void *encode_ctx)
Encodes the data portion of an attribute.
Definition: encode.c:272
ssize_t fr_pair_array_from_network(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, fr_pair_decode_value_t decode_value)
Decode an array of values from the network.
Definition: decode.c:41
ssize_t fr_pair_raw_from_network(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, uint8_t const *data, size_t data_len)
Create a "raw" pair from the network data.
Definition: decode.c:79
ssize_t fr_pair_array_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, int depth, fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_value)
Encode an array of values from the network.
Definition: encode.c:42
ssize_t fr_pair_cursor_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *cursor, void *encode_ctx, fr_encode_dbuff_t encode_pair)
Definition: encode.c:71
talloc_free(reap)
unsigned short uint16_t
Definition: merged_model.c:31
@ FR_TYPE_TLV
Contains nested attributes.
Definition: merged_model.c:118
@ FR_TYPE_UINT16
16 Bit unsigned integer.
Definition: merged_model.c:98
@ FR_TYPE_UINT8
8 Bit unsigned integer.
Definition: merged_model.c:97
@ FR_TYPE_UINT32
32 Bit unsigned integer.
Definition: merged_model.c:99
@ FR_TYPE_STRUCT
like TLV, but without T or L, and fixed-width children
Definition: merged_model.c:119
@ FR_TYPE_UINT64
64 Bit unsigned integer.
Definition: merged_model.c:100
@ FR_TYPE_BOOL
A truth value.
Definition: merged_model.c:95
@ FR_TYPE_OCTETS
Raw octets.
Definition: merged_model.c:84
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
#define UINT8_MAX
Definition: merged_model.c:32
static size_t array[MY_ARRAY_SIZE]
static uint8_t depth(fr_minmax_heap_index_t i)
Definition: minmax_heap.c:83
static unsigned int fr_bytes_from_bits(unsigned int bits)
Convert bits (as in prefix length) to bytes, rounding up.
Definition: nbo.h:235
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition: pair.c:283
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition: pair.c:1345
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition: pair.c:46
void fr_proto_da_stack_build(fr_da_stack_t *stack, fr_dict_attr_t const *da)
Build a complete DA stack from the da back to the root.
Definition: proto.c:118
static fr_internal_encode_ctx_t encode_ctx
static ssize_t encode_pair(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED void *encode_ctx)
Definition: base.c:39
#define decode_value
Definition: decode.c:410
static bool done
Definition: radclient.c:80
fr_assert(0)
fr_pair_t * vp
static void * struct_next_encodable(fr_dlist_head_t *list, void *current, void *uctx)
Definition: struct.c:455
ssize_t fr_struct_to_network(fr_dbuff_t *dbuff, fr_da_stack_t *da_stack, unsigned int depth, fr_dcursor_t *parent_cursor, void *encode_ctx, fr_encode_dbuff_t encode_value, fr_encode_dbuff_t encode_pair)
Definition: struct.c:470
static int8_t pair_sort_increasing(void const *a, void const *b)
Definition: struct.c:440
static int put_bits_dbuff(fr_dbuff_t *dbuff, uint8_t *p, int start_bit, uint8_t num_bits, uint64_t data)
Put bits into an output dbuff.
Definition: struct.c:414
ssize_t fr_struct_from_network(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, fr_pair_decode_value_t decode_value, fr_pair_decode_value_t decode_tlv)
Convert a STRUCT to one or more VPs.
Definition: struct.c:33
Stores an attribute, a value and various bits of other data.
Definition: pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition: pair.h:69
#define fr_pair_dcursor_iter_init(_cursor, _list, _iter, _uctx)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition: pair.h:569
fr_pair_t * fr_pair_list_tail(fr_pair_list_t const *list)
Get the tail of a valuepair list.
Definition: pair_inline.c:56
#define PAIR_VERIFY(_x)
Definition: pair.h:191
void fr_pair_list_sort(fr_pair_list_t *list, fr_cmp_t cmp)
Sort a doubly linked list of fr_pair_ts using merge sort.
Definition: pair_inline.c:140
#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_PROTO_HEX_DUMP(_data, _data_len, _fmt,...)
Definition: proto.h:41
#define FR_PROTO_TRACE(_fmt,...)
Definition: proto.h:40
#define FR_PROTO_STACK_PRINT(_stack, _depth)
Definition: proto.h:43
fr_dict_attr_t const * da[FR_DICT_MAX_TLV_STACK+1]
The stack.
Definition: proto.h:56
Structure for holding the stack of dictionary attributes being encoded.
Definition: proto.h:54
char const * fr_strerror(void)
Get the last library error.
Definition: strerror.c:554
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition: strerror.h:64
#define fr_strerror_const(_msg)
Definition: strerror.h:223
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition: types.h:433
#define fr_type_is_structural(_x)
Definition: types.h:371
#define FR_TYPE_LEAF
Definition: types.h:297
ssize_t fr_value_box_from_network(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t type, fr_dict_attr_t const *enumv, fr_dbuff_t *dbuff, size_t len, bool tainted)
Decode a fr_value_box_t from serialized binary data.
Definition: value.c:1754
return fr_dbuff_set(dbuff, &our_dbuff)
ssize_t fr_value_box_to_network(fr_dbuff_t *dbuff, fr_value_box_t const *value)
Encode a single value box, serializing its contents in generic network format.
Definition: value.c:1404
static fr_slen_t data
Definition: value.h:1265
static size_t char ** out
Definition: value.h:997