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