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

IP population tool. More...

#include <freeradius-devel/libradius.h>
#include <freeradius-devel/rad_assert.h>
#include "redis.h"
#include "cluster.h"
#include "redis_ippool.h"
#include <sys/wait.h>
+ Include dependency graph for rlm_redis_ippool_tool.c:

Go to the source code of this file.

Data Structures

struct  ippool_tool
 
struct  ippool_tool_lease
 
struct  ippool_tool_operation
 A single pool operation. More...
 
struct  redis_driver_conf
 

Macros

#define ADD_ACTION(_action)
 
#define IPPOOL_BUILD_IP_KEY_FROM_STR(_buff, _p, _key, _key_len, _ip_str)
 
#define MAX_PIPELINED   1000
 

Typedefs

typedef enum ippool_tool_action ippool_tool_action_t
 Pool management actions. More...
 
typedef struct ippool_tool_lease ippool_tool_lease_t
 
typedef struct
ippool_tool_operation 
ippool_tool_operation_t
 A single pool operation. More...
 
typedef struct ippool_tool ippool_tool_t
 
typedef struct redis_driver_conf redis_driver_conf_t
 
typedef int(* redis_ippool_process_t )(void *out, fr_ipaddr_t const *ipaddr, redisReply const *reply)
 
typedef int(* redis_ippool_queue_t )(redis_driver_conf_t *inst, fr_redis_conn_t *conn, uint8_t const *key_prefix, size_t key_prefix_len, uint8_t const *range, size_t range_len, fr_ipaddr_t *ipaddr, uint8_t prefix)
 

Enumerations

enum  ippool_tool_action {
  IPPOOL_TOOL_NOOP = 0,
  IPPOOL_TOOL_ADD,
  IPPOOL_TOOL_REMOVE,
  IPPOOL_TOOL_RELEASE,
  IPPOOL_TOOL_SHOW
}
 Pool management actions. More...
 

Functions

static int _driver_add_lease_enqueue (UNUSED redis_driver_conf_t *inst, fr_redis_conn_t *conn, uint8_t const *key_prefix, size_t key_prefix_len, uint8_t const *range, size_t range_len, fr_ipaddr_t *ipaddr, uint8_t prefix)
 Enqueue lease addition commands. More...
 
static int _driver_add_lease_process (void *out, UNUSED fr_ipaddr_t const *ipaddr, redisReply const *reply)
 Count the number of leases we actually added. More...
 
static int _driver_release_lease_enqueue (UNUSED redis_driver_conf_t *inst, fr_redis_conn_t *conn, uint8_t const *key_prefix, size_t key_prefix_len, UNUSED uint8_t const *range, UNUSED size_t range_len, fr_ipaddr_t *ipaddr, uint8_t prefix)
 Release a lease by setting its score back to zero. More...
 
static int _driver_release_lease_process (void *out, UNUSED fr_ipaddr_t const *ipaddr, redisReply const *reply)
 Count the number of leases we released. More...
 
static int _driver_remove_lease_enqueue (UNUSED redis_driver_conf_t *inst, fr_redis_conn_t *conn, uint8_t const *key_prefix, size_t key_prefix_len, UNUSED uint8_t const *range, UNUSED size_t range_len, fr_ipaddr_t *ipaddr, uint8_t prefix)
 Enqueue lease removal commands. More...
 
static int _driver_remove_lease_process (void *out, UNUSED fr_ipaddr_t const *ipaddr, redisReply const *reply)
 Count the number of leases we removed. More...
 
static int _driver_show_lease_enqueue (UNUSED redis_driver_conf_t *inst, fr_redis_conn_t *conn, uint8_t const *key_prefix, size_t key_prefix_len, UNUSED uint8_t const *range, UNUSED size_t range_len, fr_ipaddr_t *ipaddr, uint8_t prefix)
 Enqueue commands to retrieve lease information. More...
 
static int _driver_show_lease_process (void *out, fr_ipaddr_t const *ipaddr, redisReply const *reply)
 Enqueue commands to retrieve lease information. More...
 
static int driver_add_lease (void *out, void *instance, ippool_tool_operation_t const *op)
 Add a range of prefixes. More...
 
static int driver_do_lease (void *out, void *instance, ippool_tool_operation_t const *op, redis_ippool_queue_t enqueue, redis_ippool_process_t process)
 Add a net to the pool. More...
 
static int driver_init (TALLOC_CTX *ctx, CONF_SECTION *conf, void **instance)
 Driver initialization function. More...
 
static int driver_release_lease (void *out, void *instance, ippool_tool_operation_t const *op)
 Release a range of leases. More...
 
static int driver_remove_lease (void *out, void *instance, ippool_tool_operation_t const *op)
 Remove a range of leases. More...
 
static int driver_show_lease (void *out, void *instance, ippool_tool_operation_t const *op)
 Show information about leases. More...
 
static bool ipaddr_next (fr_ipaddr_t *ipaddr, fr_ipaddr_t const *end, uint8_t prefix)
 Iterate over range of IP addresses. More...
 
int main (int argc, char *argv[])
 
static int parse_ip_range (fr_ipaddr_t *start_out, fr_ipaddr_t *end_out, char const *ip_str, uint8_t prefix)
 Convert an IP range or CIDR mask to a start and stop address. More...
 
pid_t rad_fork (void)
 
pid_t rad_waitpid (pid_t pid, int *status)
 
static uint128_t uint128_add (uint128_t a, uint128_t b)
 Add two 128bit unsigned integers. More...
 
static uint128_t uint128_band (uint128_t a, uint128_t b)
 Perform bitwise & of two 128bit unsigned integers. More...
 
static uint128_t uint128_bor (uint128_t a, uint128_t b)
 Perform bitwise | of two 128bit unsigned integers. More...
 
static bool uint128_eq (uint128_t a, uint128_t b)
 Return whether the integers are equal. More...
 
static uint128_t uint128_gen_mask (uint8_t bits)
 Create a 128 bit integer value with n bits high. More...
 
static bool uint128_gt (uint128_t a, uint128_t b)
 Return whether one integer is greater than the other. More...
 
static uint128_t uint128_lshift (uint128_t num, uint8_t bits)
 Left shift 128 bit integer. More...
 
static uint128_t uint128_new (uint64_t h, uint64_t l)
 Creates a new uint128_t from an uint64_t. More...
 
static uint128_t uint128_sub (uint128_t a, uint128_t b)
 Subtract one 128bit integer from another. More...
 
static uint32_t uint32_gen_mask (uint8_t bits)
 
static void NEVER_RETURNS usage (int ret)
 

Variables

static char const * name
 
static CONF_PARSER redis_config []
 

Detailed Description

IP population tool.

Id:
7a49fb331ce0bb8aeff5bea8e9f2419a3b63668c
Author
Arran Cudbard-Bell

Definition in file rlm_redis_ippool_tool.c.


Data Structure Documentation

struct ippool_tool

Definition at line 101 of file rlm_redis_ippool_tool.c.

+ Collaboration diagram for ippool_tool:
Data Fields
CONF_SECTION * cs
void * driver
struct ippool_tool_lease

Definition at line 80 of file rlm_redis_ippool_tool.c.

+ Collaboration diagram for ippool_tool_lease:
Data Fields
uint8_t const * device Last device id.
size_t device_len
uint8_t const * gateway Last gateway id.
size_t gateway_len
fr_ipaddr_t ipaddr Prefix or address.
time_t next_event Last state change.
uint8_t const * range Range the lease belongs to.
size_t range_len
struct ippool_tool_operation

A single pool operation.

Definition at line 64 of file rlm_redis_ippool_tool.c.

+ Collaboration diagram for ippool_tool_operation:
Data Fields
ippool_tool_action_t action What to do to the leases described by net/prefix.
fr_ipaddr_t end End address.
char const * name Original range or CIDR string.
uint8_t const * pool Pool identifier.
size_t pool_len Length of the pool identifier.
uint8_t prefix Prefix - The bits between the address mask, and the prefix form the addresses to be modified in the pool.
uint8_t const * range Range identifier.
size_t range_len Length of the range identifier.
fr_ipaddr_t start Start address.
struct redis_driver_conf

Definition at line 96 of file rlm_redis_ippool_tool.c.

+ Collaboration diagram for redis_driver_conf:
Data Fields
fr_redis_cluster_t * cluster
fr_redis_conf_t conf Connection parameters for the Redis server.

Macro Definition Documentation

#define ADD_ACTION (   _action)
Value:
do { \
if ((size_t)(p - ops) >= sizeof(ops)) { \
ERROR("Too many actions, max is " STRINGIFY(sizeof(ops))); \
usage(64); \
} \
p->action = _action; \
p->name = optarg; \
p++; \
} while (0);
#define STRINGIFY(x)
Definition: build.h:34
static void NEVER_RETURNS usage(int ret)
#define ERROR(fmt,...)
Definition: log.h:145
#define IPPOOL_BUILD_IP_KEY_FROM_STR (   _buff,
  _p,
  _key,
  _key_len,
  _ip_str 
)
Value:
do { \
ssize_t _slen; \
*_p++ = '{'; \
memcpy(_p, _key, _key_len); \
_p += _key_len; \
_slen = strlcpy((char *)_p, "}:"IPPOOL_ADDRESS_KEY":", sizeof(_buff) - (_p - _buff)); \
if (is_truncated((size_t)_slen, sizeof(_buff) - (_p - _buff))) { \
ERROR("IP key too long"); \
return 0;\
} \
_p += (size_t)_slen;\
_p += strlcpy((char *)_p, _ip_str, sizeof(_buff) - (_p - _buff)); \
} while (0)
#define is_truncated(_ret, _max)
Definition: libradius.h:204
size_t strlcpy(char *dst, char const *src, size_t siz)
Definition: strlcpy.c:38
#define ERROR(fmt,...)
Definition: log.h:145
#define IPPOOL_ADDRESS_KEY
Definition: redis_ippool.h:61

Definition at line 113 of file rlm_redis_ippool_tool.c.

#define MAX_PIPELINED   1000

Definition at line 35 of file rlm_redis_ippool_tool.c.

Typedef Documentation

Pool management actions.

A single pool operation.

typedef struct ippool_tool ippool_tool_t
typedef int(* redis_ippool_process_t)(void *out, fr_ipaddr_t const *ipaddr, redisReply const *reply)

Definition at line 111 of file rlm_redis_ippool_tool.c.

typedef int(* redis_ippool_queue_t)(redis_driver_conf_t *inst, fr_redis_conn_t *conn, uint8_t const *key_prefix, size_t key_prefix_len, uint8_t const *range, size_t range_len, fr_ipaddr_t *ipaddr, uint8_t prefix)

Definition at line 106 of file rlm_redis_ippool_tool.c.

Enumeration Type Documentation

Pool management actions.

Enumerator
IPPOOL_TOOL_NOOP 
IPPOOL_TOOL_ADD 
IPPOOL_TOOL_REMOVE 
IPPOOL_TOOL_RELEASE 
IPPOOL_TOOL_SHOW 

Definition at line 53 of file rlm_redis_ippool_tool.c.

Function Documentation

static int _driver_add_lease_enqueue ( UNUSED redis_driver_conf_t inst,
fr_redis_conn_t conn,
uint8_t const *  key_prefix,
size_t  key_prefix_len,
uint8_t const *  range,
size_t  range_len,
fr_ipaddr_t ipaddr,
uint8_t  prefix 
)
static

Enqueue lease addition commands.

Definition at line 657 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static int _driver_add_lease_process ( void *  out,
UNUSED fr_ipaddr_t const *  ipaddr,
redisReply const *  reply 
)
static

Count the number of leases we actually added.

This isn't necessarily the same as the number of ZADDs, as leases may already exist.

Definition at line 638 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static int _driver_release_lease_enqueue ( UNUSED redis_driver_conf_t inst,
fr_redis_conn_t conn,
uint8_t const *  key_prefix,
size_t  key_prefix_len,
UNUSED uint8_t const *  range,
UNUSED size_t  range_len,
fr_ipaddr_t ipaddr,
uint8_t  prefix 
)
static

Release a lease by setting its score back to zero.

Definition at line 547 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static int _driver_release_lease_process ( void *  out,
UNUSED fr_ipaddr_t const *  ipaddr,
redisReply const *  reply 
)
static

Count the number of leases we released.

Definition at line 530 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static int _driver_remove_lease_enqueue ( UNUSED redis_driver_conf_t inst,
fr_redis_conn_t conn,
uint8_t const *  key_prefix,
size_t  key_prefix_len,
UNUSED uint8_t const *  range,
UNUSED size_t  range_len,
fr_ipaddr_t ipaddr,
uint8_t  prefix 
)
static

Enqueue lease removal commands.

This removes the lease from the expiry heap, and the data associated with the lease.

Definition at line 600 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static int _driver_remove_lease_process ( void *  out,
UNUSED fr_ipaddr_t const *  ipaddr,
redisReply const *  reply 
)
static

Count the number of leases we removed.

Because the ZREM and DEL have to occur in a transaction, we need some fancier processing to just count the number of ZREMs.

Definition at line 579 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static int _driver_show_lease_enqueue ( UNUSED redis_driver_conf_t inst,
fr_redis_conn_t conn,
uint8_t const *  key_prefix,
size_t  key_prefix_len,
UNUSED uint8_t const *  range,
UNUSED size_t  range_len,
fr_ipaddr_t ipaddr,
uint8_t  prefix 
)
static

Enqueue commands to retrieve lease information.

Definition at line 493 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static int _driver_show_lease_process ( void *  out,
fr_ipaddr_t const *  ipaddr,
redisReply const *  reply 
)
static

Enqueue commands to retrieve lease information.

Definition at line 452 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static int driver_add_lease ( void *  out,
void *  instance,
ippool_tool_operation_t const *  op 
)
static

Add a range of prefixes.

Definition at line 684 of file rlm_redis_ippool_tool.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int driver_do_lease ( void *  out,
void *  instance,
ippool_tool_operation_t const *  op,
redis_ippool_queue_t  enqueue,
redis_ippool_process_t  process 
)
static

Add a net to the pool.

Returns
the number of new addresses added.

Definition at line 371 of file rlm_redis_ippool_tool.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int driver_init ( TALLOC_CTX *  ctx,
CONF_SECTION conf,
void **  instance 
)
static

Driver initialization function.

Definition at line 692 of file rlm_redis_ippool_tool.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int driver_release_lease ( void *  out,
void *  instance,
ippool_tool_operation_t const *  op 
)
inlinestatic

Release a range of leases.

Definition at line 568 of file rlm_redis_ippool_tool.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int driver_remove_lease ( void *  out,
void *  instance,
ippool_tool_operation_t const *  op 
)
static

Remove a range of leases.

Definition at line 627 of file rlm_redis_ippool_tool.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int driver_show_lease ( void *  out,
void *  instance,
ippool_tool_operation_t const *  op 
)
inlinestatic

Show information about leases.

Definition at line 522 of file rlm_redis_ippool_tool.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static bool ipaddr_next ( fr_ipaddr_t ipaddr,
fr_ipaddr_t const *  end,
uint8_t  prefix 
)
static

Iterate over range of IP addresses.

Mutates the ipaddr passed in, adding one to the prefix bits on each call.

Parameters
[in,out]ipaddrto increment.
[in]endipaddr to stop at.
[in]prefixLength of the prefix.
Returns
  • true if the prefix bits are not high (continue).
  • false if the prefix bits are high (stop).

Definition at line 316 of file rlm_redis_ippool_tool.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 886 of file rlm_redis_ippool_tool.c.

+ Here is the call graph for this function:

static int parse_ip_range ( fr_ipaddr_t start_out,
fr_ipaddr_t end_out,
char const *  ip_str,
uint8_t  prefix 
)
static

Convert an IP range or CIDR mask to a start and stop address.

Parameters
[out]start_outWhere to write the start address.
[out]end_outWhere to write the end address.
[in]ip_strUnparsed IP string.
[in]prefixlength of prefixes we'll be allocating.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 728 of file rlm_redis_ippool_tool.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

pid_t rad_fork ( void  )

Definition at line 40 of file rlm_redis_ippool_tool.c.

pid_t rad_waitpid ( pid_t  pid,
int *  status 
)

Definition at line 45 of file rlm_redis_ippool_tool.c.

static uint128_t uint128_add ( uint128_t  a,
uint128_t  b 
)
static

Add two 128bit unsigned integers.

Author
Jacob F. W
Note
copied from http://www.codeproject.com/Tips/617214/UInt-Addition-Subtraction

Definition at line 214 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static uint128_t uint128_band ( uint128_t  a,
uint128_t  b 
)
static

Perform bitwise & of two 128bit unsigned integers.

Definition at line 243 of file rlm_redis_ippool_tool.c.

static uint128_t uint128_bor ( uint128_t  a,
uint128_t  b 
)
static

Perform bitwise | of two 128bit unsigned integers.

Definition at line 254 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static bool uint128_eq ( uint128_t  a,
uint128_t  b 
)
static

Return whether the integers are equal.

Definition at line 265 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static uint128_t uint128_gen_mask ( uint8_t  bits)
static

Create a 128 bit integer value with n bits high.

Definition at line 172 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static bool uint128_gt ( uint128_t  a,
uint128_t  b 
)
static

Return whether one integer is greater than the other.

Definition at line 273 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static uint128_t uint128_lshift ( uint128_t  num,
uint8_t  bits 
)
static

Left shift 128 bit integer.

Note
shift must be 127 bits or less.

Definition at line 194 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static uint128_t uint128_new ( uint64_t  h,
uint64_t  l 
)
static

Creates a new uint128_t from an uint64_t.

Definition at line 283 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static uint128_t uint128_sub ( uint128_t  a,
uint128_t  b 
)
static

Subtract one 128bit integer from another.

Author
Jacob F. W
Note
copied from http://www.codeproject.com/Tips/617214/UInt-Addition-Subtraction

Definition at line 228 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static uint32_t uint32_gen_mask ( uint8_t  bits)
static

Definition at line 157 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

static void NEVER_RETURNS usage ( int  ret)
static

Definition at line 131 of file rlm_redis_ippool_tool.c.

+ Here is the caller graph for this function:

Variable Documentation

char const* name
static

Definition at line 129 of file rlm_redis_ippool_tool.c.

CONF_PARSER redis_config[]
static
Initial value:
= {
}
#define CONF_PARSER_TERMINATOR
Definition: conffile.h:289
#define REDIS_COMMON_CONFIG
Definition: redis.h:106

Definition at line 91 of file rlm_redis_ippool_tool.c.