Libecoli 0.11.1
Extensible COmmand LIne library
Loading...
Searching...
No Matches
ip_pool.c
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2025, Olivier MATZ <zer0@droids-corp.org>
3 */
4
5#include <errno.h>
6#include <stdint.h>
7#include <stdlib.h>
8
9#include "ip_pool.h"
10
11#include <ecoli.h>
12
13struct ip_pool {
14 struct ec_dict *addrs;
15};
16
17static struct ec_dict *pools = NULL;
18
19int ip_pool_init(void)
20{
21 pools = ec_dict();
22 if (pools == NULL)
23 return -1;
24
25 return 0;
26}
27
28void ip_pool_exit(void)
29{
30 ec_dict_free(pools);
31}
32
33static void __ip_pool_free(void *_pool)
34{
35 struct ip_pool *pool = _pool;
36
37 if (pool == NULL)
38 return;
39
40 ec_dict_free(pool->addrs);
41 free(pool);
42}
43
44/* create an ip pool */
45struct ip_pool *ip_pool(const char *name)
46{
47 struct ip_pool *pool = NULL;
48
49 if (ec_dict_has_key(pools, name)) {
50 errno = EEXIST;
51 goto fail;
52 }
53
54 pool = calloc(1, sizeof(*pool));
55 if (pool == NULL)
56 goto fail;
57
58 pool->addrs = ec_dict();
59 if (pool->addrs == NULL)
60 goto fail;
61
62 if (ec_dict_set(pools, name, pool, __ip_pool_free) < 0)
63 goto fail;
64
65 return pool;
66
67fail:
68 __ip_pool_free(pool);
69 return NULL;
70}
71
72/* lookup for an ip pool */
73struct ip_pool *ip_pool_lookup(const char *name)
74{
75 return ec_dict_get(pools, name);
76}
77
78/* list ip pool names */
79struct ec_strvec *ip_pool_list(void)
80{
81 struct ec_dict_elt_ref *iter = NULL;
82 struct ec_strvec *names = NULL;
83 const char *key;
84
85 names = ec_strvec();
86 if (names == NULL)
87 goto fail;
88
89 for (iter = ec_dict_iter(pools); iter != NULL; iter = ec_dict_iter_next(iter)) {
90 key = ec_dict_iter_get_key(iter);
91
92 if (ec_strvec_add(names, key) < 0)
93 goto fail;
94 }
95
96 return names;
97
98fail:
99 ec_strvec_free(names);
100 return NULL;
101}
102
103/* destroy the ip pool */
104void ip_pool_free(const char *name)
105{
106 struct ip_pool *pool;
107
108 pool = ec_dict_get(pools, name);
109 if (pool == NULL)
110 return;
111
112 ec_dict_del(pools, name);
113}
114
115/* add an IP */
116int ip_pool_addr_add(struct ip_pool *pool, const char *addr)
117{
118 if (ec_dict_has_key(pool->addrs, addr)) {
119 errno = EEXIST;
120 return -1;
121 }
122
123 if (ec_dict_set(pool->addrs, addr, NULL, NULL) < 0)
124 return -1;
125
126 return 0;
127}
128
129/* del an IP */
130int ip_pool_addr_del(struct ip_pool *pool, const char *addr)
131{
132 return ec_dict_del(pool->addrs, addr);
133}
134
135/* list pool IP addresses */
136struct ec_strvec *ip_pool_addr_list(const struct ip_pool *pool)
137{
138 struct ec_dict_elt_ref *iter = NULL;
139 struct ec_strvec *addrs = NULL;
140 const char *key;
141
142 addrs = ec_strvec();
143 if (addrs == NULL)
144 goto fail;
145
146 for (iter = ec_dict_iter(pool->addrs); iter != NULL; iter = ec_dict_iter_next(iter)) {
147 key = ec_dict_iter_get_key(iter);
148
149 if (ec_strvec_add(addrs, key) < 0)
150 goto fail;
151 }
152
153 return addrs;
154
155fail:
156 ec_strvec_free(addrs);
157 return NULL;
158}
bool ec_dict_has_key(const struct ec_dict *dict, const char *key)
void ec_dict_free(struct ec_dict *dict)
struct ec_dict * ec_dict(void)
struct ec_dict_elt_ref * ec_dict_iter_next(struct ec_dict_elt_ref *iter)
void * ec_dict_get(const struct ec_dict *dict, const char *key)
const char * ec_dict_iter_get_key(const struct ec_dict_elt_ref *iter)
struct ec_dict_elt_ref * ec_dict_iter(const struct ec_dict *dict)
int ec_dict_del(struct ec_dict *dict, const char *key)
int ec_dict_set(struct ec_dict *dict, const char *key, void *val, ec_dict_elt_free_t free_cb)
void ec_strvec_free(struct ec_strvec *strvec)
struct ec_strvec * ec_strvec(void)
int ec_strvec_add(struct ec_strvec *strvec, const char *s)