The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
redis_ippool.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or (at
6  * your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16  */
17 
18 /**
19  * $Id: 31f45df57f6ae956d692be5048c904442b170ace $
20  * @file cluster.h
21  * @brief Common functions for interacting with Redis cluster via Hiredis
22  *
23  * @author Arran Cudbard-Bell
24  *
25  * @copyright 2015 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
26  * @copyright 2015 The FreeRADIUS server project
27  */
28 RCSIDH(redis_ippool_h, "$Id: 31f45df57f6ae956d692be5048c904442b170ace $")
29 
30 /*
31  * Can't stringify enum macros *sigh*
32  */
33 #define _IPPOOL_RCODE_SUCCESS 0
34 #define _IPPOOL_RCODE_NOT_FOUND -1
35 #define _IPPOOL_RCODE_EXPIRED -2
36 #define _IPPOOL_RCODE_DEVICE_MISMATCH -3
37 #define _IPPOOL_RCODE_POOL_EMPTY -4
38 #define _IPPOOL_RCODE_FAIL -5
39 
40 typedef enum {
48 
49 typedef enum {
55 
56 #define IPPOOL_MAX_KEY_PREFIX_SIZE 128
57 #define IPPOOL_POOL_KEY "pool"
58 #define IPPOOL_ADDRESS_KEY "ip"
59 #define IPPOOL_OWNER_KEY "device"
60 #define IPPOOL_STATIC_BIT 0x10000000000000 /* A high bit which Redis ZSCORE will represent accurately*/
61 
62 /** {prefix}:pool
63  */
64 #define IPPOOL_MAX_POOL_KEY_SIZE IPPOOL_MAX_KEY_PREFIX_SIZE + (sizeof("{}:" IPPOOL_POOL_KEY) - 1) + 2
65 
66 /** {prefix}:ipaddr/prefix
67  */
68 #define IPPOOL_MAX_IP_KEY_SIZE IPPOOL_MAX_KEY_PREFIX_SIZE + (sizeof("{}:" IPPOOL_ADDRESS_KEY ":") - 1) + INET6_ADDRSTRLEN + 4
69 
70 /** {prefix}:device
71  */
72 #define IPPOOL_MAX_OWNER_KEY_SIZE IPPOOL_MAX_KEY_PREFIX_SIZE + (sizeof("{}:" IPPOOL_OWNER_KEY ":") - 1) + 128
73 
74 
75 #define IPADDR_LEN(_af) ((_af == AF_UNSPEC) ? 0 : ((_af == AF_INET6) ? 128 : 32))
76 
77 /** Wrap the prefix in {} and add the pool suffix
78  *
79  */
80 #define IPPOOL_BUILD_KEY(_buff, _p, _key, _key_len) \
81 do { \
82  *_p++ = '{'; \
83  memcpy(_p, _key, _key_len); \
84  _p += _key_len; \
85  *_p++ = '}'; \
86  *_p++ = ':'; \
87  memcpy(_p, IPPOOL_POOL_KEY, sizeof(IPPOOL_POOL_KEY) - 1); \
88  _p += sizeof(IPPOOL_POOL_KEY) - 1; \
89 } while (0)
90 
91 /** Build the IP key {prefix}:ip
92  *
93  */
94 #define IPPOOL_BUILD_IP_KEY(_buff, _p, _key, _key_len, _ip) \
95 do { \
96  ssize_t _slen; \
97  *_p++ = '{'; \
98  memcpy(_p, _key, _key_len); \
99  _p += _key_len; \
100  _slen = strlcpy((char *)_p, "}:"IPPOOL_ADDRESS_KEY":", sizeof(_buff) - (_p - _buff)); \
101  if (is_truncated((size_t)_slen, sizeof(_buff) - (_p - _buff))) { \
102  REDEBUG("IP key too long"); \
103  ret = IPPOOL_RCODE_FAIL; \
104  goto finish; \
105  } \
106  _p += (size_t)_slen;\
107  _slen = fr_pair_print_value_quoted(&FR_SBUFF_OUT((char *)_p, sizeof(_buff) - (_p - _buff)), _ip, T_BARE_WORD); \
108  if (_slen < 0) { \
109  REDEBUG("IP key too long"); \
110  ret = IPPOOL_RCODE_FAIL; \
111  goto finish; \
112  } \
113  _p += (size_t)_slen;\
114 } while (0)
115 
116 /** If the prefix is as wide as the AF data size then print it without CIDR notation.
117  *
118  */
119 #define IPPOOL_SPRINT_IP(_buff, _ip, _prefix) \
120 do { \
121  if (_prefix == IPADDR_LEN((_ip)->af)) { \
122  inet_ntop((_ip)->af, &((_ip)->addr), _buff, sizeof(_buff)); \
123  } else { \
124  uint8_t _net = (_ip)->prefix; \
125  (_ip)->prefix = _prefix; \
126  fr_inet_ntop_prefix(_buff, sizeof(_buff), _ip); \
127  (_ip)->prefix = _net; \
128  } \
129 } while (0)
#define RCSIDH(h, id)
Definition: build.h:482
#define _IPPOOL_RCODE_NOT_FOUND
Definition: redis_ippool.h:34
ippool_action_t
Definition: redis_ippool.h:49
@ POOL_ACTION_BULK_RELEASE
Definition: redis_ippool.h:53
@ POOL_ACTION_RELEASE
Definition: redis_ippool.h:52
@ POOL_ACTION_ALLOCATE
Definition: redis_ippool.h:50
@ POOL_ACTION_UPDATE
Definition: redis_ippool.h:51
#define _IPPOOL_RCODE_FAIL
Definition: redis_ippool.h:38
#define _IPPOOL_RCODE_POOL_EMPTY
Definition: redis_ippool.h:37
#define _IPPOOL_RCODE_EXPIRED
Definition: redis_ippool.h:35
ippool_rcode_t
Definition: redis_ippool.h:40
@ IPPOOL_RCODE_EXPIRED
Definition: redis_ippool.h:43
@ IPPOOL_RCODE_DEVICE_MISMATCH
Definition: redis_ippool.h:44
@ IPPOOL_RCODE_NOT_FOUND
Definition: redis_ippool.h:42
@ IPPOOL_RCODE_POOL_EMPTY
Definition: redis_ippool.h:45
@ IPPOOL_RCODE_SUCCESS
Definition: redis_ippool.h:41
@ IPPOOL_RCODE_FAIL
Definition: redis_ippool.h:46
#define _IPPOOL_RCODE_DEVICE_MISMATCH
Definition: redis_ippool.h:36
#define _IPPOOL_RCODE_SUCCESS
Definition: redis_ippool.h:33