The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
base.h File Reference

Common functions for interacting with Redis via hiredis. More...

#include <freeradius-devel/server/base.h>
#include <freeradius-devel/server/map.h>
#include <freeradius-devel/server/module.h>
#include <freeradius-devel/server/trunk.h>
#include <hiredis/hiredis.h>
+ Include dependency graph for base.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  fr_redis_conf_t
 Configuration parameters for a redis connection. More...
 
struct  fr_redis_conn_t
 Connection handle, holding a redis context. More...
 

Macros

#define MAX_REDIS_ARGS   256
 
#define MAX_REDIS_COMMAND_LEN   4096
 
#define REDIS_COMMON_CONFIG
 
#define REDIS_DEFAULT_PORT   6379
 
#define REDIS_ERROR_ASK_STR   "ASK"
 
#define REDIS_ERROR_MOVED_STR   "MOVED"
 
#define REDIS_ERROR_NO_SCRIPT_STR   "NOSCRIPT"
 
#define REDIS_ERROR_TRY_AGAIN_STR   "TRYAGAIN"
 
#define REDIS_VERSION(_max, _min, _patch)   (uint32_t) (_max << 24) | (_min << 16) | _patch
 

Typedefs

typedef struct fr_redis_cluster_node_s fr_redis_cluster_node_t
 
typedef struct fr_redis_ct_s fr_redis_ct_t
 

Enumerations

enum  fr_redis_async_rcode_t {
  REDIS_ASYNC_RCODE_SUCCESS = 0 ,
  REDIS_ASYNC_RCODE_ERROR = -1 ,
  REDIS_ASYNC_RCODE_BOOTSTRAP = -2 ,
  REDIS_ASYNC_RCODE_GETMAP = -3 ,
  REDIS_ASYNC_RCODE_TRY_AGAIN = -4 ,
  REDIS_ASYNC_RCODE_ASK = -5 ,
  REDIS_ASYNC_RCODE_MOVE = -6 ,
  REDIS_ASYNC_RCODE_NO_SCRIPT = -7 ,
  REDIS_ASYNC_RCODE_FAIL = -8
}
 
enum  fr_redis_packet_code_t {
  FR_REDIS_INVALID = 0 ,
  FR_REDIS_CLUSTER_MAP_BOOTSTRAP ,
  FR_REDIS_CLUSTER_MAP_GET ,
  FR_REDIS_CLUSTER_MAP_UPDATE ,
  FR_REDIS_CLUSTER_MAP_FAIL ,
  FR_REDIS_CODE_MAX ,
  FR_REDIS_DO_NOT_RESPOND = 255
}
 
enum  fr_redis_rcode_t {
  REDIS_RCODE_SUCCESS = 0 ,
  REDIS_RCODE_ERROR = -1 ,
  REDIS_RCODE_TRY_AGAIN = -2 ,
  REDIS_RCODE_RECONNECT = -3 ,
  REDIS_RCODE_ASK = -4 ,
  REDIS_RCODE_MOVE = -5 ,
  REDIS_RCODE_NO_SCRIPT = -6
}
 Codes are ordered inversely by priority. More...
 

Functions

int fr_redis_parse_version (char *out, size_t out_len, redisReply *reply)
 Parse the reply from the Redis command INFO SERVER to extract the version.
 
void fr_redis_reply_print (fr_log_lvl_t lvl, redisReply *reply, request_t *request, int idx, fr_redis_rcode_t status)
 Print the response data in a useful treelike form.
 
int fr_redis_reply_to_map (TALLOC_CTX *ctx, map_list_t *out, request_t *request, redisReply *key, redisReply *op, redisReply *value)
 Convert a pair of redis reply objects to a map.
 
int fr_redis_reply_to_value_box (TALLOC_CTX *ctx, fr_value_box_t *out, redisReply *reply, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, bool box_error, bool shallow))
 Convert a string or integer type to fr_value_box_t of specified type.
 
int fr_redis_tuple_from_map (TALLOC_CTX *pool, char const *out[], size_t out_len[], map_t *map)
 Add a single map pair to an existing command string as three elements.
 
uint32_t fr_redis_version_num (char const *version)
 Convert version string into a 32bit unsigned integer for comparisons.
 
void fr_redis_version_print (void)
 Print the version of libhiredis the server was built against.
 
int redis_dict_init (void)
 Load the Redis dictionaries.
 

Variables

fr_table_num_sorted_t const redis_rcodes []
 
size_t redis_rcodes_len
 
fr_table_num_sorted_t const redis_reply_types []
 
size_t redis_reply_types_len
 

Detailed Description

Common functions for interacting with Redis via hiredis.

Id
78c3b2635897e5804aa245cd811dcddefd423064
Author
Arran Cudbard-Bell

Definition in file base.h.


Data Structure Documentation

◆ fr_redis_conf_t

struct fr_redis_conf_t

Configuration parameters for a redis connection.

Note
should be passed as instance data to module_rlm_connection_pool_init.

Definition at line 114 of file base.h.

+ Collaboration diagram for fr_redis_conf_t:
Data Fields
fr_time_delta_t connection_timeout
uint32_t database number on Redis server.
char const ** hostname of Redis server.
char const * inst_name Instance name for triggers.
char const * log_prefix
uint32_t max_alt Maximum alternative nodes to try.
uint8_t max_nodes Maximum number of cluster nodes to connect to.
uint32_t max_redirects Maximum number of times we can be redirected.
uint32_t max_retries Maximum number of times we attempt a command when receiving successive -TRYAGAIN messages.
char const * module_name Module name for triggers.
char const * password to authenticate to Redis.
uint16_t port of Redis daemon.
fr_time_delta_t reconnection_delay
fr_time_delta_t retry_delay How long to wait when we received a -TRYAGAIN message.
trunk_conf_t trunk_conf Configuration for trunk connections.
bool use_cluster_map use cluster map.
bool use_tls use TLS.
char const * username for acls.

◆ fr_redis_conn_t

struct fr_redis_conn_t

Connection handle, holding a redis context.

Definition at line 95 of file base.h.

Data Fields
redisContext * handle Hiredis context used when issuing commands.
fr_redis_cluster_node_t * node Node this connection is to.

Macro Definition Documentation

◆ MAX_REDIS_ARGS

#define MAX_REDIS_ARGS   256

Definition at line 45 of file base.h.

◆ MAX_REDIS_COMMAND_LEN

#define MAX_REDIS_COMMAND_LEN   4096

Definition at line 44 of file base.h.

◆ REDIS_COMMON_CONFIG

#define REDIS_COMMON_CONFIG
Value:
{ FR_CONF_OFFSET("port", fr_redis_conf_t, port), .dflt = "6379" }, \
{ FR_CONF_OFFSET("database", fr_redis_conf_t, database), .dflt = "0" }, \
{ FR_CONF_OFFSET("use_tls", fr_redis_conf_t, use_tls), .dflt = "no" }, \
{ FR_CONF_OFFSET("use_cluster_map", fr_redis_conf_t, use_cluster_map), .dflt = "yes" }, \
{ FR_CONF_OFFSET("username", fr_redis_conf_t, username) }, \
{ FR_CONF_OFFSET_FLAGS("password", CONF_FLAG_SECRET, fr_redis_conf_t, password) }, \
{ FR_CONF_OFFSET("max_nodes", fr_redis_conf_t, max_nodes), .dflt = "20" }, \
{ FR_CONF_OFFSET("max_alt", fr_redis_conf_t, max_alt), .dflt = "3" }, \
{ FR_CONF_OFFSET("max_redirects", fr_redis_conf_t, max_redirects), .dflt = "2" }, \
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
#define FR_CONF_OFFSET_FLAGS(_name, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:268
#define FR_CONF_OFFSET_SUBSECTION(_name, _flags, _struct, _field, _subcs)
conf_parser_t which populates a sub-struct using a CONF_SECTION
Definition cf_parse.h:309
@ CONF_FLAG_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
Definition cf_parse.h:429
@ CONF_FLAG_MULTI
CONF_PAIR can have multiple copies.
Definition cf_parse.h:446
@ CONF_FLAG_SECRET
Only print value if debug level >= 3.
Definition cf_parse.h:433
Configuration parameters for a redis connection.
Definition base.h:114
conf_parser_t const trunk_config[]
Config parser definitions to populate a trunk_conf_t.
Definition trunk.c:345

Definition at line 143 of file base.h.

◆ REDIS_DEFAULT_PORT

#define REDIS_DEFAULT_PORT   6379

Definition at line 51 of file base.h.

◆ REDIS_ERROR_ASK_STR

#define REDIS_ERROR_ASK_STR   "ASK"

Definition at line 48 of file base.h.

◆ REDIS_ERROR_MOVED_STR

#define REDIS_ERROR_MOVED_STR   "MOVED"

Definition at line 47 of file base.h.

◆ REDIS_ERROR_NO_SCRIPT_STR

#define REDIS_ERROR_NO_SCRIPT_STR   "NOSCRIPT"

Definition at line 50 of file base.h.

◆ REDIS_ERROR_TRY_AGAIN_STR

#define REDIS_ERROR_TRY_AGAIN_STR   "TRYAGAIN"

Definition at line 49 of file base.h.

◆ REDIS_VERSION

#define REDIS_VERSION (   _max,
  _min,
  _patch 
)    (uint32_t) (_max << 24) | (_min << 16) | _patch

Definition at line 53 of file base.h.

Typedef Documentation

◆ fr_redis_cluster_node_t

typedef struct fr_redis_cluster_node_s fr_redis_cluster_node_t

Definition at line 55 of file base.h.

◆ fr_redis_ct_t

typedef struct fr_redis_ct_s fr_redis_ct_t

Definition at line 56 of file base.h.

Enumeration Type Documentation

◆ fr_redis_async_rcode_t

Enumerator
REDIS_ASYNC_RCODE_SUCCESS 

Operation was successful.

REDIS_ASYNC_RCODE_ERROR 

Unrecoverable error.

REDIS_ASYNC_RCODE_BOOTSTRAP 

The caller should issue a request to bootstrap the cluster map.

REDIS_ASYNC_RCODE_GETMAP 

The caller should issue a request to update the cluster map.

REDIS_ASYNC_RCODE_TRY_AGAIN 

Try the operation again.

REDIS_ASYNC_RCODE_ASK 

Attempt operation on an alternative node.

REDIS_ASYNC_RCODE_MOVE 

Attempt operation on an alternative node with remap.

REDIS_ASYNC_RCODE_NO_SCRIPT 

Script doesn't exist.

REDIS_ASYNC_RCODE_FAIL 

The command set trunk request has been failed.

Definition at line 80 of file base.h.

◆ fr_redis_packet_code_t

Enumerator
FR_REDIS_INVALID 
FR_REDIS_CLUSTER_MAP_BOOTSTRAP 
FR_REDIS_CLUSTER_MAP_GET 
FR_REDIS_CLUSTER_MAP_UPDATE 
FR_REDIS_CLUSTER_MAP_FAIL 
FR_REDIS_CODE_MAX 
FR_REDIS_DO_NOT_RESPOND 

Definition at line 100 of file base.h.

◆ fr_redis_rcode_t

Codes are ordered inversely by priority.

To simplify handling the return codes from pipelined commands, the lowest status code, and the reply which accompanies it should be returned to the redis cluster code.

Enumerator
REDIS_RCODE_SUCCESS 

Operation was successful.

REDIS_RCODE_ERROR 

Unrecoverable library/server error.

REDIS_RCODE_TRY_AGAIN 

Try the operation again.

REDIS_RCODE_RECONNECT 

Transitory error, caller should retry the operation with a new connection.

REDIS_RCODE_ASK 

Attempt operation on an alternative node.

REDIS_RCODE_MOVE 

Attempt operation on an alternative node with remap.

REDIS_RCODE_NO_SCRIPT 

Script doesn't exist.

Definition at line 69 of file base.h.

Function Documentation

◆ fr_redis_parse_version()

int fr_redis_parse_version ( char *  out,
size_t  out_len,
redisReply *  reply 
)

Parse the reply from the Redis command INFO SERVER to extract the version.

Parameters
[out]outWhere to write the version string.
[in]out_lenLength of the version string buffer.
[in]replyRedis reply to parse
Returns
  • 0 on success.
  • -1 on command/response mismatch or command error.

Definition at line 535 of file redis.c.

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

◆ fr_redis_reply_print()

void fr_redis_reply_print ( fr_log_lvl_t  lvl,
redisReply *  reply,
request_t request,
int  idx,
fr_redis_rcode_t  status 
)

Print the response data in a useful treelike form.

Parameters
[in]lvlto print data at.
[in]replyto print.
[in]requestThe current request.
[in]idxResponse number.
[in]statuscode from processing last reply.

Definition at line 153 of file redis.c.

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

◆ fr_redis_reply_to_map()

int fr_redis_reply_to_map ( TALLOC_CTX *  ctx,
map_list_t *  out,
request_t request,
redisReply *  key,
redisReply *  op,
redisReply *  value 
)

Convert a pair of redis reply objects to a map.

The maps can then be applied using map_to_request.

Parameters
[in,out]ctxto allocate maps in.
[out]outWhere to write the head of the new maps list.
[in]requestThe current request.
[in]keyto process.
[in]opto process.
[in]valueto process.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 381 of file redis.c.

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

◆ fr_redis_reply_to_value_box()

int fr_redis_reply_to_value_box ( TALLOC_CTX *  ctx,
fr_value_box_t out,
redisReply *  reply,
fr_type_t  dst_type,
fr_dict_attr_t const *  dst_enumv,
bool  box_error,
bool  shallow 
)

Convert a string or integer type to fr_value_box_t of specified type.

Will work with REDIS_REPLY_STRING (which is converted to FR_TYPE_STRING then cast to dst_type), or REDIS_REPLY_INTEGER (which is converted to FR_TYPE_UINT64, then cast to dst_type).

Note
Any unsupported types will trigger an assert. You must check the reply type prior to calling this function.
Parameters
[in,out]ctxto allocate any buffers in.
[out]outWhere to write the cast type.
[in]replyto process.
[in]dst_typeto convert to. May be FR_TYPE_VOID to infer type.
[in]dst_enumvUsed to convert string types to integers for attribute with enumerated values.
[in]box_errorIf true then REDIS_REPLY_ERROR will be copied to a box, otherwise we'll return and error with the contents of the error available on the thread local error stack.
[in]shallowIf true, we shallow copy strings.
Returns
  • 1 if we received a NIL reply.
  • 0 on success.
  • -1 on cast or parse failure.

Definition at line 222 of file redis.c.

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

◆ fr_redis_tuple_from_map()

int fr_redis_tuple_from_map ( TALLOC_CTX *  pool,
char const *  out[],
size_t  out_len[],
map_t map 
)

Add a single map pair to an existing command string as three elements.

  • Integer types will be encoded as integers.
  • Strings and octets will be encoded in their raw form.
  • Other types will be converted to their printable form and will be encoded as strings.
Note
lhs must be a TMPL_TYPE_ATTR.
rhs must be a TMPL_TYPE_DATA.
Parameters
poolto allocate any buffers in.
outWhere to write pointers to the member of the tuple. Unused elements should be a multiple of three, and it should have at least three unused elements.
out_lenWhere to write the size of the data pointed to by the equivalent index in the out array.
mapto convert.
Returns
0 on success. -1 on failure.

Definition at line 474 of file redis.c.

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

◆ fr_redis_version_num()

uint32_t fr_redis_version_num ( char const *  version)

Convert version string into a 32bit unsigned integer for comparisons.

Parameters
[in]versionstring to parse.
Returns
32bit unsigned integer representing the version string.

Definition at line 567 of file redis.c.

+ Here is the caller graph for this function:

◆ fr_redis_version_print()

void fr_redis_version_print ( void  )

Print the version of libhiredis the server was built against.

Definition at line 107 of file redis.c.

+ Here is the caller graph for this function:

◆ redis_dict_init()

int redis_dict_init ( void  )

Load the Redis dictionaries.

Definition at line 136 of file redis.c.

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

Variable Documentation

◆ redis_rcodes

fr_table_num_sorted_t const redis_rcodes[]
extern

Definition at line 41 of file redis.c.

◆ redis_rcodes_len

size_t redis_rcodes_len
extern

Definition at line 49 of file redis.c.

◆ redis_reply_types

fr_table_num_sorted_t const redis_reply_types[]
extern

Definition at line 31 of file redis.c.

◆ redis_reply_types_len

size_t redis_reply_types_len
extern

Definition at line 39 of file redis.c.