All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
redis.h File Reference

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

#include <freeradius-devel/radiusd.h>
#include <hiredis/hiredis.h>
+ Include dependency graph for redis.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  redis_common_conf
 Configuration parameters for a redis connection. More...
 
struct  redis_conn
 Connection handle, holding a redis context. More...
 

Macros

#define fr_redis_pipeline_free(_r, _n)
 
#define fr_redis_reply_free(_p)   if (_p) freeReplyObject(_p)
 Wrap freeReplyObject so we consistently check for NULL pointers. More...
 
#define MAX_REDIS_ARGS   16
 
#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"
 

Typedefs

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

Enumerations

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

fr_redis_rcode_t fr_redis_command_status (fr_redis_conn_t *conn, redisReply *reply)
 Check the reply for errors. More...
 
fr_redis_rcode_t fr_redis_get_version (char *out, size_t out_len, fr_redis_conn_t *conn)
 Get the version of Redis running on the remote server. More...
 
fr_redis_rcode_t fr_redis_pipeline_result (fr_redis_rcode_t *rcode, redisReply *out[], size_t out_len, fr_redis_conn_t *conn, int pipelined)
 Simplifies handling of pipelined commands with Redis cluster. More...
 
void fr_redis_reply_print (log_lvl_t lvl, redisReply *reply, REQUEST *request, int idx)
 Print the response data in a useful treelike form. More...
 
int fr_redis_reply_to_map (TALLOC_CTX *ctx, vp_map_t **out, REQUEST *request, redisReply *key, redisReply *op, redisReply *value)
 Convert a pair of redis reply objects to a map. More...
 
int fr_redis_reply_to_value_data (TALLOC_CTX *ctx, value_data_t *out, redisReply *reply, PW_TYPE dst_type, fr_dict_attr_t const *dst_enumv)
 Convert a string or integer type to value_data_t of specified type. More...
 
int fr_redis_tuple_from_map (TALLOC_CTX *pool, char const *out[], size_t out_len[], vp_map_t *map)
 Add a single map pair to an existing command string as three elements. More...
 
uint32_t fr_redis_version_num (char const *version)
 Convert version string into a 32bit unsigned integer for comparisons. More...
 
void fr_redis_version_print (void)
 Print the version of libhiredis the server was built against. More...
 

Variables

const FR_NAME_NUMBER redis_rcodes []
 
const FR_NAME_NUMBER redis_reply_types []
 

Detailed Description

Common functions for interacting with Redis via hiredis.

Id:
4d2fb9c1cc2b625064923f60994cc2b2507a7af7
Author
Arran Cudbard-Bell

Definition in file redis.h.


Data Structure Documentation

struct redis_common_conf

Configuration parameters for a redis connection.

Note
should be passed as instance data to module_connection_pool_init.

Definition at line 88 of file redis.h.

Data Fields
uint32_t database number on Redis server.
char const ** hostname of Redis server.
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 * password to authenticate to Redis.
uint16_t port of Redis daemon.
char const * prefix Logging prefix for errors in fr_redis_cluster_conn_create.
struct timeval retry_delay How long to wait when we received a -TRYAGAIN message.
struct redis_conn

Connection handle, holding a redis context.

Definition at line 80 of file redis.h.

Data Fields
redisContext * handle Hiredis context used when issuing commands.

Macro Definition Documentation

#define fr_redis_pipeline_free (   _r,
  _n 
)
Value:
do {\
size_t _i; \
for (_i = 0; _i < _n; _i++) {\
_r[_i] = NULL; \
} \
} while (0)
#define fr_redis_reply_free(_p)
Wrap freeReplyObject so we consistently check for NULL pointers.
Definition: redis.h:56

Definition at line 142 of file redis.h.

#define fr_redis_reply_free (   _p)    if (_p) freeReplyObject(_p)

Wrap freeReplyObject so we consistently check for NULL pointers.

Older versions such as 0.10 (which ship with Ubuntu <= 14.10) don't check for NULL pointer before attempting to free, so we get a NULL pointer dereference in some cases.

Rather than go back through the many calls to freeReplyObject and attempt to determine code paths that may result in it being called on a NULL pointer, we use this to always check.

Definition at line 56 of file redis.h.

#define MAX_REDIS_ARGS   16

Definition at line 38 of file redis.h.

#define MAX_REDIS_COMMAND_LEN   4096

Definition at line 37 of file redis.h.

#define REDIS_COMMON_CONFIG
Value:
{ FR_CONF_OFFSET("port", PW_TYPE_SHORT, fr_redis_conf_t, port), .dflt = "6379" }, \
{ FR_CONF_OFFSET("database", PW_TYPE_INTEGER, fr_redis_conf_t, database), .dflt = "0" }, \
{ FR_CONF_OFFSET("max_nodes", PW_TYPE_BYTE, fr_redis_conf_t, max_nodes), .dflt = "20" }, \
{ FR_CONF_OFFSET("max_alt", PW_TYPE_INTEGER, fr_redis_conf_t, max_alt), .dflt = "3" }, \
{ FR_CONF_OFFSET("max_redirects", PW_TYPE_INTEGER, fr_redis_conf_t, max_redirects), .dflt = "2" }
Configuration parameters for a redis connection.
Definition: redis.h:88
static char const * hostname(char *buf, size_t buflen, uint32_t ipaddr)
Definition: radwho.c:149
#define PW_TYPE_SECRET
Only print value if debug level >= 3.
Definition: conffile.h:202
8 Bit unsigned integer.
Definition: radius.h:42
32 Bit unsigned integer.
Definition: radius.h:34
#define PW_TYPE_MULTI
CONF_PAIR can have multiple copies.
Definition: conffile.h:210
#define FR_CONF_OFFSET(_n, _t, _s, _f)
Definition: conffile.h:168
#define PW_TYPE_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
Definition: conffile.h:200
String of printable characters.
Definition: radius.h:33
16 Bit unsigned integer.
Definition: radius.h:43

Definition at line 106 of file redis.h.

#define REDIS_DEFAULT_PORT   6379

Definition at line 44 of file redis.h.

#define REDIS_ERROR_ASK_STR   "ASK"

Definition at line 41 of file redis.h.

#define REDIS_ERROR_MOVED_STR   "MOVED"

Definition at line 40 of file redis.h.

#define REDIS_ERROR_NO_SCRIPT_STR   "NOSCRIPT"

Definition at line 43 of file redis.h.

#define REDIS_ERROR_TRY_AGAIN_STR   "TRYAGAIN"

Definition at line 42 of file redis.h.

Typedef Documentation

Configuration parameters for a redis connection.

Note
should be passed as instance data to module_connection_pool_init.
typedef struct redis_conn fr_redis_conn_t

Connection handle, holding a redis context.

Enumeration Type Documentation

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 successfull.

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 67 of file redis.h.

Function Documentation

fr_redis_rcode_t fr_redis_command_status ( fr_redis_conn_t conn,
redisReply *  reply 
)

Check the reply for errors.

Parameters
connused to issue the command.
replyto process.
Returns
  • REDIS_RCODE_TRY_AGAIN - If the operation should be retries.
  • REDIS_RCODE_MOVED - If the key has been permanently moved.
  • REDIS_RCODE_ASK - If the key has been temporarily moved.
  • REDIS_RCODE_SUCCESS - if no errors.
  • REDIS_RCODE_ERROR - on command/server error.
  • REDIS_RCODE_NO_SCRIPT - script specified by evalsha doesn't exist.
  • REDIS_RCODE_RECONNECT - on connection error (probably needs reconnecting).

Definition at line 76 of file redis.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

fr_redis_rcode_t fr_redis_get_version ( char *  out,
size_t  out_len,
fr_redis_conn_t conn 
)

Get the version of Redis running on the remote server.

This can be useful for some modules, as it allows adaptive behaviour, or early termination.

Parameters
[out]outWhere to write the version string.
[in]out_lenLength of the version string buffer.
[in]connUsed to query the version string.
Returns

Definition at line 549 of file redis.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

fr_redis_rcode_t fr_redis_pipeline_result ( fr_redis_rcode_t rcode,
redisReply *  out[],
size_t  out_len,
fr_redis_conn_t conn,
int  pipelined 
)

Simplifies handling of pipelined commands with Redis cluster.

Retrieve all available pipelined responses, and write them to the array.

On encountering an error, all previously retrieved responses are freed, and the reply containing the error is written to the first element of out. All responses after the error are also freed.

If the number of responses != pipelined, that's also an error, a very serious one, in libhiredis or Redis. We can't really do much here apart from error out.

Parameters
[out]rcodeStatus of the first errored response, or REDIS_RCODE_SUCCESS if all responses were processed.
[out]outWhere to write the replies from pipelined commands. Will contain exactly 1 element on error, else the number passed in pipelined.
[in]out_lennumber of elements in out.
[in]connthe pipelined commands were issued on.
[in]pipelinedNumber of pipelined commands we sent to the server.
Returns

Definition at line 460 of file redis.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void fr_redis_reply_print ( log_lvl_t  lvl,
redisReply *  reply,
REQUEST request,
int  idx 
)

Print the response data in a useful treelike form.

Parameters
lvlto print data at.
replyto print.
requestThe current request.
idxResponse number.

Definition at line 139 of file redis.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_redis_reply_to_map ( TALLOC_CTX *  ctx,
vp_map_t **  out,
REQUEST 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 282 of file redis.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_redis_reply_to_value_data ( TALLOC_CTX *  ctx,
value_data_t out,
redisReply *  reply,
PW_TYPE  dst_type,
fr_dict_attr_t const *  dst_enumv 
)

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

Will work with REDIS_REPLY_STRING (which is converted to PW_TYPE_STRING then cast to dst_type), or REDIS_REPLY_INTEGER (which is converted to PW_TYPE_INTEGER64, 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.
[in]dst_enumvUsed to convert string types to integers for attributes with enumerated values.
Returns
  • 1 if we received a NIL reply. Out will remain uninitialized.
  • 0 on success.
  • -1 on cast or parse failure.

Definition at line 197 of file redis.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_redis_tuple_from_map ( TALLOC_CTX *  pool,
char const *  out[],
size_t  out_len[],
vp_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 381 of file redis.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 597 of file redis.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void fr_redis_version_print ( void  )

Print the version of libhiredis the server was built against.

Definition at line 52 of file redis.c.

+ Here is the caller graph for this function:

Variable Documentation

const FR_NAME_NUMBER redis_rcodes[]

Definition at line 39 of file redis.c.

const FR_NAME_NUMBER redis_reply_types[]

Definition at line 29 of file redis.c.