The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
proto.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program 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
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /** Protocol encoder/decoder support functions
18  *
19  * @file src/lib/util/proto.c
20  *
21  * @copyright 2015 The FreeRADIUS server project
22  */
23 #include <freeradius-devel/util/pair.h>
24 #include <freeradius-devel/util/print.h>
25 #include <freeradius-devel/util/proto.h>
26 
27 void fr_proto_print(char const *file, int line, char const *fmt, ...)
28 {
29  va_list ap;
30  char *buff;
31 
32  va_start(ap, fmt);
33  buff = talloc_vasprintf(NULL, fmt, ap);
34  va_end(ap);
35 
37 
39 }
40 
41 DIAG_OFF(format-nonliteral)
42 void fr_proto_print_hex_data(char const *file, int line, uint8_t const *data, size_t data_len, char const *fmt, ...)
43 {
44  va_list ap;
45  char *msg;
46 
47  if (fmt) {
48  va_start(ap, fmt);
49  msg = talloc_vasprintf(NULL, fmt, ap);
50  va_end(ap);
51  fr_log(&default_log, L_DBG, file, line, "hex: -- %s --", msg);
53  }
54  fr_log_hex(&default_log, L_DBG, file, line, data, data_len, "hex: ");
55 }
56 
57 void fr_proto_print_hex_marker(char const *file, int line, uint8_t const *data, size_t data_len, ssize_t slen, char const *fmt, ...)
58 {
59  va_list ap;
60  char *msg;
61 
62  if (fmt) {
63  va_start(ap, fmt);
64  msg = talloc_vasprintf(NULL, fmt, ap);
65  va_end(ap);
66  fr_log(&default_log, L_DBG, file, line, "hex: -- %s --", msg);
68  }
69  fr_log_hex_marker(&default_log, L_DBG, file, line, data, data_len, slen, "current position", "hex: ");
70 }
71 DIAG_ON(format-nonliteral)
72 
73 void fr_proto_da_stack_print(char const *file, int line, char const *func, fr_da_stack_t *da_stack, unsigned int depth)
74 {
75  int i = da_stack->depth;
76 
77  fr_log(&default_log, L_DBG, file, line, "stk: Currently in %s", func);
78  for (i--; i >= 0; i--) {
80  "stk: %s [%i] %s: %s, vendor: 0x%x (%u), attr: 0x%x (%u)",
81  (i == (int)depth) ? ">" : " ", i,
82  fr_type_to_str(da_stack->da[i]->type),
83  da_stack->da[i]->name,
84  fr_dict_vendor_num_by_da(da_stack->da[i]), fr_dict_vendor_num_by_da(da_stack->da[i]),
85  da_stack->da[i]->attr, da_stack->da[i]->attr);
86  }
87  fr_log(&default_log, L_DBG, file, line, "stk:");
88 }
89 
90 /** Implements the default iterator to encode pairs belonging to a specific dictionary that are not internal
91  *
92  * @param[in] list to iterate over.
93  * @param[in] current The fr_pair_t cursor->current. Will be advanced and checked to
94  * see if it matches the specified fr_dict_t.
95  * @param[in] uctx The fr_dict_t to search for.
96  * @return
97  * - Next matching fr_pair_t.
98  * - NULL if not more matching fr_pair_ts could be found.
99  */
101 {
102  fr_pair_t *c = current;
103  fr_dict_t *dict = talloc_get_type_abort(uctx, fr_dict_t);
104 
105  while ((c = fr_dlist_next(list, c))) {
106  PAIR_VERIFY(c);
107  if ((c->da->dict == dict) && (!c->da->flags.internal)) break;
108  }
109 
110  return c;
111 }
112 
113 /** Build a complete DA stack from the da back to the root
114  *
115  * @param[out] stack to populate.
116  * @param[in] da to build the stack for.
117  */
119 {
120  fr_dict_attr_t const **cached;
121 
122  if (!da) return;
123 
124  /*
125  * See if we have a cached da stack available
126  */
127  cached = fr_dict_attr_da_stack(da);
128  if (cached) {
129  /*
130  * da->da_stack[0] is dict->root
131  */
132  memcpy(&stack->da[0], &cached[1], sizeof(stack->da[0]) * da->depth);
133 
134  } else {
135  fr_dict_attr_t const *da_p, **da_o;
136 
137  /*
138  * Unknown attributes don't have a da->da_stack.
139  */
140  da_p = da;
141  da_o = stack->da + (da->depth - 1);
142 
143  while (da_o >= stack->da) {
144  *da_o-- = da_p;
145  da_p = da_p->parent;
146  }
147  }
148 
149  stack->depth = da->depth;
150  stack->da[stack->depth] = NULL;
151 }
152 
153 /** Complete the DA stack for a child attribute
154  *
155  * @param[out] stack to populate.
156  * @param[in] parent to populate from.
157  * @param[in] da to populate to.
158  */
160 {
161  fr_dict_attr_t const *da_p, **da_q, **da_o;
162 
163  if (!parent || (parent->depth == 0)) {
165  return;
166  }
167 
168  da_p = da;
169  da_q = stack->da + (parent->depth - 1);
170  da_o = stack->da + (da->depth - 1);
171 
172  while (da_o >= da_q) {
173  *da_o-- = da_p;
174  da_p = da_p->parent;
175  }
176 
177  stack->depth = da->depth;
178  stack->da[stack->depth] = NULL;
179 }
int const char * file
Definition: acutest.h:702
va_end(args)
log_entry msg
Definition: acutest.h:794
static int const char * fmt
Definition: acutest.h:573
int const char int line
Definition: acutest.h:702
va_start(args, fmt)
static fr_dict_t * dict
Definition: fuzzer.c:46
#define DIAG_ON(_x)
Definition: build.h:456
#define DIAG_OFF(_x)
Definition: build.h:455
fr_dcursor_eval_t void const * uctx
Definition: dcursor.h:546
fr_dcursor_iter_t void * current
Definition: dcursor.h:148
static fr_dict_attr_t const ** fr_dict_attr_da_stack(fr_dict_attr_t const *da)
Return the cached da stack (if any) associated with an attribute.
Definition: dict_ext.h:167
static uint32_t fr_dict_vendor_num_by_da(fr_dict_attr_t const *da)
Return the vendor number for an attribute.
Definition: dict_ext.h:212
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
talloc_free(reap)
void fr_log_hex(fr_log_t const *log, fr_log_type_t type, char const *file, int line, uint8_t const *data, size_t data_len, char const *line_prefix_fmt,...)
Print out hex block.
Definition: log.c:784
void fr_log_hex_marker(fr_log_t const *log, fr_log_type_t type, char const *file, int line, uint8_t const *data, size_t data_len, ssize_t marker_idx, char const *marker, char const *line_prefix_fmt,...)
Print out hex block.
Definition: log.c:831
fr_log_t default_log
Definition: log.c:291
void fr_log(fr_log_t const *log, fr_log_type_t type, char const *file, int line, char const *fmt,...)
Send a server log message to its destination.
Definition: log.c:583
@ L_DBG
Only displayed when debugging is enabled.
Definition: log.h:59
static char * stack[MAX_STACK]
Definition: radmin.c:158
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
static uint8_t depth(fr_minmax_heap_index_t i)
Definition: minmax_heap.c:83
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
void fr_proto_print(char const *file, int line, char const *fmt,...)
Definition: proto.c:27
void fr_proto_da_stack_print(char const *file, int line, char const *func, fr_da_stack_t *da_stack, unsigned int depth)
Definition: proto.c:73
void * fr_proto_next_encodable(fr_dlist_head_t *list, void *current, void *uctx)
Implements the default iterator to encode pairs belonging to a specific dictionary that are not inter...
Definition: proto.c:100
void fr_proto_print_hex_data(char const *file, int line, uint8_t const *data, size_t data_len, char const *fmt,...)
Definition: proto.c:42
void fr_proto_da_stack_build_partial(fr_da_stack_t *stack, fr_dict_attr_t const *parent, fr_dict_attr_t const *da)
Complete the DA stack for a child attribute.
Definition: proto.c:159
void fr_proto_print_hex_marker(char const *file, int line, uint8_t const *data, size_t data_len, ssize_t slen, char const *fmt,...)
Definition: proto.c:57
static char buff[sizeof("18446744073709551615")+3]
Definition: size_tests.c:41
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 PAIR_VERIFY(_x)
Definition: pair.h:191
static fr_slen_t parent
Definition: pair.h:851
Structure for holding the stack of dictionary attributes being encoded.
Definition: proto.h:54
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_box_strvalue_buffer(_val)
Definition: value.h:289
static fr_slen_t data
Definition: value.h:1265
int format(printf, 5, 0))