The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
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 */
28RCSIDH(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
48
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) \
81do { \
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) \
95do { \
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) \
120do { \
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:484
#define _IPPOOL_RCODE_NOT_FOUND
ippool_action_t
@ POOL_ACTION_BULK_RELEASE
@ POOL_ACTION_RELEASE
@ POOL_ACTION_ALLOCATE
@ POOL_ACTION_UPDATE
#define _IPPOOL_RCODE_FAIL
#define _IPPOOL_RCODE_POOL_EMPTY
#define _IPPOOL_RCODE_EXPIRED
ippool_rcode_t
@ IPPOOL_RCODE_EXPIRED
@ IPPOOL_RCODE_DEVICE_MISMATCH
@ IPPOOL_RCODE_NOT_FOUND
@ IPPOOL_RCODE_POOL_EMPTY
@ IPPOOL_RCODE_SUCCESS
@ IPPOOL_RCODE_FAIL
#define _IPPOOL_RCODE_DEVICE_MISMATCH
#define _IPPOOL_RCODE_SUCCESS