The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
radict.c File Reference

Utility to print attribute data in tab delimited format. More...

#include <freeradius-devel/util/conf.h>
#include <freeradius-devel/util/syserror.h>
#include <freeradius-devel/util/atexit.h>
#include <freeradius-devel/util/dict_priv.h>
#include <freeradius-devel/util/hash.h>
#include <ctype.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
+ Include dependency graph for radict.c:

Go to the source code of this file.

Data Structures

struct  raddict_stats_t
 Totals gathered while exporting a dictionary. More...
 

Macros

#define CHECK_TYPE(_parent)
 
#define DEBUG(fmt, ...)   if (fr_log_fp && (fr_debug_lvl > 1)) fr_fprintf(fr_log_fp , fmt "\n", ## __VA_ARGS__)
 
#define DEBUG2(fmt, ...)   if (fr_log_fp && (fr_debug_lvl > 2)) fr_fprintf(fr_log_fp , fmt "\n", ## __VA_ARGS__)
 
#define ENUM_NAME(_x)   [_x] = STRINGIFY(_x)
 Map data types to enum names representing those types.
 
#define INFO(fmt, ...)   if (fr_log_fp && (fr_debug_lvl > 0)) fr_fprintf(fr_log_fp , fmt "\n", ## __VA_ARGS__)
 
#define RADICT_POOL_MAX_SIZE   ((size_t)64 * 1000 * 1000)
 Largest pool that radict -P loads a dictionary with.
 
#define RADICT_POOL_STEP_SIZE   ((size_t)10 * 1000 * 1000)
 Bytes that radict -P adds to the largest overflowed pool to set the minimum pool size for the next reload.
 

Typedefs

typedef void(* da_print_func_t) (FILE *fp, fr_dict_attr_t const *da)
 

Enumerations

enum  radict_out_t : uint8_t {
  RADICT_OUT_INVALID = 0 ,
  RADICT_OUT_FANCY ,
  RADICT_OUT_CSV ,
  RADICT_OUT_DICT ,
  RADICT_OUT_STRUCT ,
  RADICT_OUT_STATS_LINK ,
  RADICT_OUT_BASE_C_DA_DEF ,
  RADICT_OUT_ATTR_AUTOLOAD ,
  RADICT_OUT_STATS_H
}
 

Functions

static bool children_ok (fr_dict_attr_t const *parent)
 
static void da_normalize_name (fr_dict_attr_t const *da, char buffer[static FR_DICT_ATTR_MAX_NAME_LEN *2+1])
 
static void da_print_attr_autoload (FILE *fp, fr_dict_attr_t const *parent)
 
static void da_print_base_c_da_def (FILE *fp, fr_dict_attr_t const *parent)
 
static void da_print_info (fr_dict_t const *dict, fr_dict_attr_t const *da, int depth)
 
static void da_print_name (FILE *fp, fr_dict_attr_t const *da)
 
static void da_print_stats_h (FILE *fp, fr_dict_attr_t const *parent)
 
static void da_print_stats_link (FILE *fp, fr_dict_attr_t const *parent)
 
static void da_print_struct (FILE *fp, fr_dict_attr_t const *parent)
 Print structures and mappings, mainly for statistics.
 
static int dicts_free (fr_dict_gctx_t *gctx)
 Release radict's references to the dictionaries, then free the global dictionary context.
 
static int dicts_load (fr_dict_gctx_t **gctx_out, char const *dict_dir, char const *protocol, size_t pool_size_min)
 Load the internal dictionary, then the protocol dictionaries.
 
static size_t dicts_overflow_max (fr_dict_gctx_t *gctx)
 Find the size of the largest overflowed pool among the loaded protocol dictionaries.
 
static int dicts_pool_grow (fr_dict_gctx_t **gctx_p, char const *dict_dir, char const *protocol)
 Reload the dictionaries with larger pools until no pool overflows.
 
static int file_replace (char const *filename, mode_t mode, char const *data, size_t len)
 Write data to a temporary file, then rename the temporary file over filename.
 
static bool line_field_is (char const *field, char const *field_end, char const *keyword)
 Check whether a field is a dictionary keyword.
 
static char const * line_field_next (char const **field_end, char const *p, char const *end)
 Find the next whitespace-separated field on a dictionary line.
 
static int load_dicts (char const *dict_dir, char const *protocol)
 
int main (int argc, char *argv[])
 
static int protocol_line_pool_set (fr_dict_t const *dict, size_t pool_mb)
 Set pool=<size> on the PROTOCOL line that defines a dictionary.
 
static int protocol_lines_pool_set (fr_dict_gctx_t *gctx, unsigned int overhead)
 Set pool= on the PROTOCOL line of every loaded protocol dictionary.
 
static int protocol_pools_check (fr_dict_gctx_t *gctx)
 Print the pool size and the bytes used for every loaded protocol dictionary.
 
static void raddict_export (raddict_stats_t *stats, fr_dict_t const *dict, fr_dict_attr_t const *da)
 
static void usage (void)
 

Variables

static fr_dict_t ** dict_end = dicts
 
static fr_dict_t * dicts [255]
 
static fr_table_num_ordered_t const format_table []
 
static size_t format_table_len = NUM_ELEMENTS(format_table)
 
static char const * fr_type_to_enum_name []
 
static fr_table_ptr_ordered_t const function_table []
 
static size_t function_table_len = NUM_ELEMENTS(function_table)
 
static char const * length_to_c_type []
 
static char const * mib = NULL
 
static radict_out_t output_format = RADICT_OUT_FANCY
 
static char const * parent_oid = NULL
 
static bool print_headers = false
 
static bool print_recursive = false
 
static bool print_values = false
 
static const char * spaces = " "
 
static const bool type_allowed [FR_TYPE_MAX]
 
static char const * type_to_c_type []
 

Detailed Description

Utility to print attribute data in tab delimited format.

Id
d93822134153d8dd3659dd2569a3927e9138d845

Definition in file radict.c.


Data Structure Documentation

◆ raddict_stats_t

struct raddict_stats_t

Totals gathered while exporting a dictionary.

Definition at line 690 of file radict.c.

Data Fields
uint64_t count Attributes exported.
bool found low and high hold an address.
uintptr_t high Highest attribute address seen.
uintptr_t low Lowest attribute address seen.

Macro Definition Documentation

◆ CHECK_TYPE

#define CHECK_TYPE (   _parent)
Value:
do { \
if ((parent->type != FR_TYPE_STRUCT) && (parent->type != FR_TYPE_TLV)) { \
fprintf(stderr, "%s is not a struct or tlv\n", parent->name); \
return; \
} \
if (!children_ok(parent)) fr_exit(EXIT_FAILURE); \
} while (0)
#define fr_exit(_x)
Exit, producing a log message in debug builds.
Definition debug.h:266
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_STRUCT
like TLV, but without T or L, and fixed-width children
static bool children_ok(fr_dict_attr_t const *parent)
Definition radict.c:417
static fr_slen_t parent
Definition pair.h:864

Definition at line 429 of file radict.c.

◆ DEBUG

#define DEBUG (   fmt,
  ... 
)    if (fr_log_fp && (fr_debug_lvl > 1)) fr_fprintf(fr_log_fp , fmt "\n", ## __VA_ARGS__)

Definition at line 66 of file radict.c.

◆ DEBUG2

#define DEBUG2 (   fmt,
  ... 
)    if (fr_log_fp && (fr_debug_lvl > 2)) fr_fprintf(fr_log_fp , fmt "\n", ## __VA_ARGS__)

Definition at line 65 of file radict.c.

◆ ENUM_NAME

#define ENUM_NAME (   _x)    [_x] = STRINGIFY(_x)

Map data types to enum names representing those types.

Definition at line 538 of file radict.c.

◆ INFO

#define INFO (   fmt,
  ... 
)    if (fr_log_fp && (fr_debug_lvl > 0)) fr_fprintf(fr_log_fp , fmt "\n", ## __VA_ARGS__)

Definition at line 67 of file radict.c.

◆ RADICT_POOL_MAX_SIZE

#define RADICT_POOL_MAX_SIZE   ((size_t)64 * 1000 * 1000)

Largest pool that radict -P loads a dictionary with.

radict -P reports an error, rather than reloading, when the next pool would be larger than RADICT_POOL_MAX_SIZE.

Definition at line 773 of file radict.c.

◆ RADICT_POOL_STEP_SIZE

#define RADICT_POOL_STEP_SIZE   ((size_t)10 * 1000 * 1000)

Bytes that radict -P adds to the largest overflowed pool to set the minimum pool size for the next reload.

Definition at line 777 of file radict.c.

Typedef Documentation

◆ da_print_func_t

typedef void(* da_print_func_t) (FILE *fp, fr_dict_attr_t const *da)

Definition at line 765 of file radict.c.

Enumeration Type Documentation

◆ radict_out_t

Enumerator
RADICT_OUT_INVALID 
RADICT_OUT_FANCY 
RADICT_OUT_CSV 
RADICT_OUT_DICT 
RADICT_OUT_STRUCT 
RADICT_OUT_STATS_LINK 
RADICT_OUT_BASE_C_DA_DEF 
RADICT_OUT_ATTR_AUTOLOAD 
RADICT_OUT_STATS_H 

Definition at line 43 of file radict.c.

Function Documentation

◆ children_ok()

static bool children_ok ( fr_dict_attr_t const *  parent)
static

Definition at line 417 of file radict.c.

+ Here is the call graph for this function:

◆ da_normalize_name()

static void da_normalize_name ( fr_dict_attr_t const *  da,
char  buffer[static FR_DICT_ATTR_MAX_NAME_LEN *2+1] 
)
static

Definition at line 332 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ da_print_attr_autoload()

static void da_print_attr_autoload ( FILE *  fp,
fr_dict_attr_t const *  parent 
)
static

Definition at line 636 of file radict.c.

+ Here is the call graph for this function:

◆ da_print_base_c_da_def()

static void da_print_base_c_da_def ( FILE *  fp,
fr_dict_attr_t const *  parent 
)
static

Definition at line 514 of file radict.c.

+ Here is the call graph for this function:

◆ da_print_info()

static void da_print_info ( fr_dict_t const *  dict,
fr_dict_attr_t const *  da,
int  depth 
)
static

Definition at line 187 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ da_print_name()

static void da_print_name ( FILE *  fp,
fr_dict_attr_t const *  da 
)
static

Definition at line 393 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ da_print_stats_h()

static void da_print_stats_h ( FILE *  fp,
fr_dict_attr_t const *  parent 
)
static

Definition at line 667 of file radict.c.

+ Here is the call graph for this function:

◆ da_print_stats_link()

static void da_print_stats_link ( FILE *  fp,
fr_dict_attr_t const *  parent 
)
static

Definition at line 585 of file radict.c.

+ Here is the call graph for this function:

◆ da_print_struct()

static void da_print_struct ( FILE *  fp,
fr_dict_attr_t const *  parent 
)
static

Print structures and mappings, mainly for statistics.

Definition at line 440 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dicts_free()

static int dicts_free ( fr_dict_gctx_t *  gctx)
static

Release radict's references to the dictionaries, then free the global dictionary context.

fr_dict_free() does not set the entry to NULL, so dicts_free() does. The release at the end of main() always visits dicts[0], even after a failed reload, and fr_dict_free() ignores a NULL entry.

Parameters
[in]gctxto free.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1182 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dicts_load()

static int dicts_load ( fr_dict_gctx_t **  gctx_out,
char const *  dict_dir,
char const *  protocol,
size_t  pool_size_min 
)
static

Load the internal dictionary, then the protocol dictionaries.

Parameters
[out]gctx_outThe new global dictionary context.
[in]dict_dirto load the dictionaries from.
[in]protocolto load, or NULL to load every protocol.
[in]pool_size_minSmallest pool, in bytes, that dict_alloc() creates.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1140 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dicts_overflow_max()

static size_t dicts_overflow_max ( fr_dict_gctx_t *  gctx)
static

Find the size of the largest overflowed pool among the loaded protocol dictionaries.

Parameters
[in]gctxholding the loaded dictionaries.
Returns
  • 0 if no dictionary overflowed the dictionary's pool.
  • The pool size, in bytes, of the largest pool that overflowed.

Definition at line 786 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dicts_pool_grow()

static int dicts_pool_grow ( fr_dict_gctx_t **  gctx_p,
char const *  dict_dir,
char const *  protocol 
)
static

Reload the dictionaries with larger pools until no pool overflows.

Each reload sets the minimum pool size to the size of the largest overflowed pool plus RADICT_POOL_STEP_SIZE bytes.

Parameters
[in,out]gctx_pGlobal dictionary context. dicts_pool_grow() replaces *gctx_p on each reload.
[in]dict_dirto load the dictionaries from.
[in]protocolto load, or NULL to load every protocol.
Returns
  • 0 when no pool overflows.
  • -1 if the next pool would be larger than RADICT_POOL_MAX_SIZE, or if a reload fails.

Definition at line 1214 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ file_replace()

static int file_replace ( char const *  filename,
mode_t  mode,
char const *  data,
size_t  len 
)
static

Write data to a temporary file, then rename the temporary file over filename.

Definition at line 843 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ line_field_is()

static bool line_field_is ( char const *  field,
char const *  field_end,
char const *  keyword 
)
static

Check whether a field is a dictionary keyword.

Definition at line 834 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ line_field_next()

static char const * line_field_next ( char const **  field_end,
char const *  p,
char const *  end 
)
static

Find the next whitespace-separated field on a dictionary line.

Parameters
[out]field_endFirst byte after the field.
[in]pPosition on the line to start the search from.
[in]endEnd of the line.
Returns
  • The start of the field.
  • NULL if no field remains before end or before a comment.

Definition at line 817 of file radict.c.

+ Here is the caller graph for this function:

◆ load_dicts()

static int load_dicts ( char const *  dict_dir,
char const *  protocol 
)
static

Definition at line 94 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 1242 of file radict.c.

◆ protocol_line_pool_set()

static int protocol_line_pool_set ( fr_dict_t const *  dict,
size_t  pool_mb 
)
static

Set pool=<size> on the PROTOCOL line that defines a dictionary.

protocol_line_pool_set() replaces any pool= option already on the PROTOCOL line, and keeps every other option, the whitespace between fields, and any comment.

Worked example: BEGIN PROTOCOL RADIUS 1 verify=lib keyword: BEGIN PROTOCOL, name: RADIUS, number: 1, options: verify=lib The line becomes: BEGIN PROTOCOL RADIUS 1 verify=lib,pool=12MB

Parameters
[in]dictwhose PROTOCOL line to rewrite.
[in]pool_mbNew pool size in megabytes (MB) of 1,000,000 bytes.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 915 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ protocol_lines_pool_set()

static int protocol_lines_pool_set ( fr_dict_gctx_t *  gctx,
unsigned int  overhead 
)
static

Set pool= on the PROTOCOL line of every loaded protocol dictionary.

The new pool= is the measured size plus overhead percent, rounded up to whole megabytes (MB) of 1,000,000 bytes. The overhead covers space that loading used at the top of the pool and freed again, which talloc_pool_used() does not count.

Call protocol_lines_pool_set() only after dicts_pool_grow() returns 0, so that no pool has overflowed.

Parameters
[in]gctxholding the loaded dictionaries.
[in]overheadPercentage to add to the measured size.
Returns
  • 0 on success.
  • -1 if radict failed to rewrite any dictionary.

Definition at line 1040 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ protocol_pools_check()

static int protocol_pools_check ( fr_dict_gctx_t *  gctx)
static

Print the pool size and the bytes used for every loaded protocol dictionary.

Parameters
[in]gctxholding the loaded dictionaries.
Returns
  • 0 if no protocol dictionary overflowed its pool.
  • -1 if any protocol dictionary overflowed its pool.

Definition at line 1091 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ raddict_export()

static void raddict_export ( raddict_stats_t *  stats,
fr_dict_t const *  dict,
fr_dict_attr_t const *  da 
)
static

Definition at line 697 of file radict.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ usage()

static void usage ( void  )
static

Definition at line 70 of file radict.c.

+ Here is the caller graph for this function:

Variable Documentation

◆ dict_end

fr_dict_t** dict_end = dicts
static

Definition at line 62 of file radict.c.

◆ dicts

fr_dict_t* dicts[255]
static

Definition at line 55 of file radict.c.

◆ format_table

fr_table_num_ordered_t const format_table[]
static
Initial value:
= {
{ L("fancy"), RADICT_OUT_FANCY },
{ L("csv"), RADICT_OUT_CSV },
{ L("dict"), RADICT_OUT_DICT },
{ L("struct"), RADICT_OUT_STRUCT },
{ L("stats_link"), RADICT_OUT_STATS_LINK },
{ L("da_def"), RADICT_OUT_BASE_C_DA_DEF },
{ L("attr_autoload"), RADICT_OUT_ATTR_AUTOLOAD },
{ L("stats.h"), RADICT_OUT_STATS_H },
}
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:245
@ RADICT_OUT_STRUCT
Definition radict.c:48
@ RADICT_OUT_DICT
Definition radict.c:47
@ RADICT_OUT_STATS_H
Definition radict.c:52
@ RADICT_OUT_ATTR_AUTOLOAD
Definition radict.c:51
@ RADICT_OUT_BASE_C_DA_DEF
Definition radict.c:50
@ RADICT_OUT_STATS_LINK
Definition radict.c:49
@ RADICT_OUT_FANCY
Definition radict.c:45
@ RADICT_OUT_CSV
Definition radict.c:46

Definition at line 741 of file radict.c.

◆ format_table_len

size_t format_table_len = NUM_ELEMENTS(format_table)
static

Definition at line 751 of file radict.c.

◆ fr_type_to_enum_name

char const* fr_type_to_enum_name[]
static

Definition at line 540 of file radict.c.

◆ function_table

fr_table_ptr_ordered_t const function_table[]
static
Initial value:
= {
{ L("fancy"), NULL },
{ L("csv"), NULL },
{ L("dict"), NULL },
{ L("struct"), (void *) da_print_struct },
{ L("stats_link"), (void *) da_print_stats_link },
{ L("da_def"), (void *) da_print_base_c_da_def },
{ L("attr_autoload"), (void *) da_print_attr_autoload },
{ L("stats.h"), (void *) da_print_stats_h },
}
static void da_print_struct(FILE *fp, fr_dict_attr_t const *parent)
Print structures and mappings, mainly for statistics.
Definition radict.c:440
static void da_print_stats_link(FILE *fp, fr_dict_attr_t const *parent)
Definition radict.c:585
static void da_print_stats_h(FILE *fp, fr_dict_attr_t const *parent)
Definition radict.c:667
static void da_print_attr_autoload(FILE *fp, fr_dict_attr_t const *parent)
Definition radict.c:636
static void da_print_base_c_da_def(FILE *fp, fr_dict_attr_t const *parent)
Definition radict.c:514

Definition at line 753 of file radict.c.

◆ function_table_len

size_t function_table_len = NUM_ELEMENTS(function_table)
static

Definition at line 763 of file radict.c.

◆ length_to_c_type

char const* length_to_c_type[]
static
Initial value:
= {
[2] = "uint16_t",
[4] = "uint32_t",
[8] = "uint64_t",
}

Definition at line 326 of file radict.c.

◆ mib

char const* mib = NULL
static

Definition at line 59 of file radict.c.

◆ output_format

radict_out_t output_format = RADICT_OUT_FANCY
static

Definition at line 61 of file radict.c.

◆ parent_oid

char const* parent_oid = NULL
static

Definition at line 60 of file radict.c.

◆ print_headers

bool print_headers = false
static

Definition at line 57 of file radict.c.

◆ print_recursive

bool print_recursive = false
static

Definition at line 58 of file radict.c.

◆ print_values

bool print_values = false
static

Definition at line 56 of file radict.c.

◆ spaces

const char* spaces = " "
static

Definition at line 185 of file radict.c.

◆ type_allowed

const bool type_allowed[FR_TYPE_MAX]
static
Initial value:
= {
[FR_TYPE_STRING] = true,
[FR_TYPE_OCTETS] = true,
[FR_TYPE_UINT16] = true,
[FR_TYPE_UINT32] = true,
[FR_TYPE_UINT64] = true,
[FR_TYPE_DATE] = true,
}
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_UINT16
16 Bit unsigned integer.
@ FR_TYPE_DATE
Unix time stamp, always has value >2^31.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_UINT64
64 Bit unsigned integer.
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_OCTETS
Raw octets.

Definition at line 401 of file radict.c.

◆ type_to_c_type

char const* type_to_c_type[]
static
Initial value:
= {
[FR_TYPE_STRING] = "char",
[FR_TYPE_OCTETS] = "uint8_t",
[FR_TYPE_IPV4_ADDR] = "struct in_addr",
[FR_TYPE_IPV6_ADDR] = "struct in6_addr",
[FR_TYPE_UINT8] = "uint8_t",
[FR_TYPE_UINT16] = "uint16_t",
[FR_TYPE_UINT32] = "uint32_t",
[FR_TYPE_UINT64] = "uint64_t",
[FR_TYPE_INT8] = "int8_t",
[FR_TYPE_INT16] = "int16_t",
[FR_TYPE_INT32] = "int32_t",
[FR_TYPE_INT64] = "int64_t",
[FR_TYPE_DATE] = "fr_time_t",
[FR_TYPE_TIME_DELTA] = "fr_time_delta_t",
[FR_TYPE_MAX] = 0
}
@ FR_TYPE_INT8
8 Bit signed integer.
@ FR_TYPE_MAX
Number of defined data types.
@ FR_TYPE_INT64
64 Bit signed integer.
@ FR_TYPE_INT16
16 Bit signed integer.
@ FR_TYPE_UINT8
8 Bit unsigned integer.
@ FR_TYPE_INT32
32 Bit signed integer.

Definition at line 300 of file radict.c.