All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 /**
18  * $Id: a313d95b530e324babccb45e4446dfa9dc964b9e $
19  *
20  * @file proto.c
21  * @brief functions common to protocol encoders/decoders.
22  *
23  * @copyright 2015 The FreeRADIUS server project
24  */
25 #include <freeradius-devel/libradius.h>
26 
27 static unsigned int proto_log_indent = 30;
28 static char spaces[] = " ";
29 
30 void fr_proto_print(char const *file, int line, char const *fmt, ...)
31 {
32  va_list ap;
33  size_t len;
34  char prefix[256];
35 
36  len = snprintf(prefix, sizeof(prefix), "%s:%i", file, line);
37  if (len > proto_log_indent) proto_log_indent = len;
38 
39  fprintf(fr_log_fp, "msg: %s%.*s: ", prefix, (int)(proto_log_indent - len), spaces);
40 
41  va_start(ap, fmt);
42  vfprintf(fr_log_fp, fmt, ap);
43  va_end(ap);
44 
45  fprintf(fr_log_fp, "\n");
46  fflush(fr_log_fp);
47 }
48 
49 void fr_proto_print_hex_data(char const *file, int line, char const *msg, uint8_t const *data, size_t data_len)
50 {
51  size_t i;
52  size_t len;
53  char prefix[256];
54 
55  len = snprintf(prefix, sizeof(prefix), "%s:%i", file, line);
56  if (len > proto_log_indent) proto_log_indent = len;
57 
58  if (msg) fprintf(fr_log_fp, "hex: %s%.*s: -- %s --\n", prefix, (int)(proto_log_indent - len), spaces, msg);
59  for (i = 0; i < data_len; i++) {
60  if ((i & 0x0f) == 0) fprintf(fr_log_fp, "hex: %s%.*s: %04x: ", prefix,
61  (int)(proto_log_indent - len), spaces, (unsigned int) i);
62  fprintf(fr_log_fp, "%02x ", data[i]);
63  if ((i & 0x0f) == 0x0f) fprintf(fr_log_fp, "\n");
64  }
65  if ((data_len == 0x0f) || ((data_len & 0x0f) != 0x0f)) fprintf(fr_log_fp, "\n");
66  fflush(fr_log_fp);
67 }
68 
69 void fr_proto_tlv_stack_print(char const *file, int line, char const *func, fr_dict_attr_t const **tlv_stack, unsigned int depth)
70 {
71  int i;
72  char prefix[256];
73  size_t len;
74 
75  len = snprintf(prefix, sizeof(prefix), "%s:%i", file, line);
76  if (len > proto_log_indent) proto_log_indent = len;
77 
78  for (i = 0; (i < FR_DICT_MAX_TLV_STACK) && tlv_stack[i]; i++);
79  if (!i) return;
80 
81  fprintf(fr_log_fp, "stk: %s%.*s: Encoder in %s\n",
82  prefix, (int)(proto_log_indent - len), spaces, func);
83  for (i--; i >= 0; i--) {
84  fprintf(fr_log_fp, "stk: %s%.*s: %s [%i] %s: %s, vendor: 0x%x (%u), attr: 0x%x (%u)\n",
85  prefix, (int)(proto_log_indent - len), spaces, (i == (int)depth) ? ">" : " ", i,
86  fr_int2str(dict_attr_types, tlv_stack[i]->type, "?Unknown?"),
87  tlv_stack[i]->name, tlv_stack[i]->vendor, tlv_stack[i]->vendor,
88  tlv_stack[i]->attr, tlv_stack[i]->attr);
89  }
90  fprintf(fr_log_fp, "\n");
91  fflush(fr_log_fp);
92 }
93 
94 void fr_proto_tlv_stack_build(fr_dict_attr_t const **tlv_stack, fr_dict_attr_t const *da)
95 {
96  int i;
97  fr_dict_attr_t const *da_p;
98 
99  memset(tlv_stack, 0, sizeof(*tlv_stack) * (FR_DICT_MAX_TLV_STACK + 1));
100 
101  if (!da) return;
102 
103  /*
104  * We've finished encoding one nested structure
105  * now we need to rebuild the tlv_stack and determine
106  * where the common point is.
107  */
108  for (i = da->depth, da_p = da;
109  da_p->parent && (i >= 0);
110  i--, da_p = da_p->parent) tlv_stack[i - 1] = da_p;
111 }
FILE * fr_log_fp
Definition: radius.c:81
void fr_proto_print_hex_data(char const *file, int line, char const *msg, uint8_t const *data, size_t data_len)
Definition: proto.c:49
Dictionary attribute.
Definition: dict.h:77
static char const * name
#define FR_DICT_MAX_TLV_STACK
Maximum TLV stack size.
Definition: dict.h:123
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition: snprintf.c:686
unsigned int depth
Depth of nesting for this attribute.
Definition: dict.h:86
static char spaces[]
Definition: proto.c:28
const FR_NAME_NUMBER dict_attr_types[]
Map data types to names representing those types.
Definition: dict.c:85
fr_dict_attr_t const * parent
Immediate parent of this attribute.
Definition: dict.h:82
static unsigned int proto_log_indent
Definition: proto.c:27
static char const * prefix
Definition: mainconfig.c:81
uint8_t data[]
Definition: eap_pwd.h:625
void fr_proto_print(char const *file, int line, char const *fmt,...)
Definition: proto.c:30
char const * fr_int2str(FR_NAME_NUMBER const *table, int number, char const *def)
Definition: token.c:506
void fr_proto_tlv_stack_print(char const *file, int line, char const *func, fr_dict_attr_t const **tlv_stack, unsigned int depth)
Definition: proto.c:69
void fr_proto_tlv_stack_build(fr_dict_attr_t const **tlv_stack, fr_dict_attr_t const *da)
Definition: proto.c:94