Browse code

pool: restyle ipv4/ipv6 members to improve readability

(This is only code refactoring)

IPv4 and IPv6 members are all part of the same flat hierarchy
in the pool data structure, without a proper name convention.

Create 2 sub-structures to properly separate IPv4 from IPv6
related members. This should make the structure more organized
and also slightly improve code readability.

Signed-off-by: Antonio Quartulli <antonio@openvpn.net>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <20180605090421.9746-3-a@unstable.cc>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg16944.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Antonio Quartulli authored on 2018/06/05 18:04:18
Showing 2 changed files
... ...
@@ -66,7 +66,7 @@ ifconfig_pool_find(struct ifconfig_pool *pool, const char *common_name)
66 66
     int previous_usage = -1;
67 67
     int new_usage = -1;
68 68
 
69
-    for (i = 0; i < pool->size; ++i)
69
+    for (i = 0; i < pool->ipv4.size; ++i)
70 70
     {
71 71
         struct ifconfig_pool_entry *ipe = &pool->list[i];
72 72
         if (!ipe->in_use)
... ...
@@ -158,19 +158,19 @@ ifconfig_pool_init(int type, in_addr_t start, in_addr_t end,
158 158
     ASSERT(start <= end && end - start < IFCONFIG_POOL_MAX);
159 159
     ALLOC_OBJ_CLEAR(pool, struct ifconfig_pool);
160 160
 
161
-    pool->type = type;
161
+    pool->ipv4.type = type;
162 162
     pool->duplicate_cn = duplicate_cn;
163 163
 
164
-    switch (type)
164
+    switch (pool->ipv4.type)
165 165
     {
166 166
         case IFCONFIG_POOL_30NET:
167
-            pool->base = start & ~3;
168
-            pool->size = (((end | 3) + 1) - pool->base) >> 2;
167
+            pool->ipv4.base = start & ~3;
168
+            pool->ipv4.size = (((end | 3) + 1) - pool->ipv4.base) >> 2;
169 169
             break;
170 170
 
171 171
         case IFCONFIG_POOL_INDIV:
172
-            pool->base = start;
173
-            pool->size = end - start + 1;
172
+            pool->ipv4.base = start;
173
+            pool->ipv4.size = end - start + 1;
174 174
             break;
175 175
 
176 176
         default:
... ...
@@ -178,30 +178,30 @@ ifconfig_pool_init(int type, in_addr_t start, in_addr_t end,
178 178
     }
179 179
 
180 180
     /* IPv6 pools are always "INDIV" type */
181
-    pool->ipv6 = ipv6_pool;
181
+    pool->ipv6.enabled = ipv6_pool;
182 182
 
183
-    if (pool->ipv6)
183
+    if (pool->ipv6.enabled)
184 184
     {
185
-        pool->base_ipv6 = ipv6_base;
186
-        pool->size_ipv6 = ipv6_netbits>96 ? ( 1<<(128-ipv6_netbits) )
185
+        pool->ipv6.base = ipv6_base;
186
+        pool->ipv6.size = ipv6_netbits > 96 ? (1 << (128 - ipv6_netbits))
187 187
                           : IFCONFIG_POOL_MAX;
188 188
 
189 189
         msg( D_IFCONFIG_POOL, "IFCONFIG POOL IPv6: (IPv4) size=%d, size_ipv6=%d, netbits=%d, base_ipv6=%s",
190
-             pool->size, pool->size_ipv6, ipv6_netbits,
191
-             print_in6_addr( pool->base_ipv6, 0, &gc ));
190
+             pool->ipv4.size, pool->ipv6.size, ipv6_netbits,
191
+             print_in6_addr(pool->ipv6.base, 0, &gc));
192 192
 
193 193
         /* the current code is very simple and assumes that the IPv6
194 194
          * pool is at least as big as the IPv4 pool, and we don't need
195 195
          * to do separate math etc. for IPv6
196 196
          */
197
-        ASSERT( pool->size < pool->size_ipv6 );
197
+        ASSERT(pool->ipv4.size < pool->ipv6.size);
198 198
     }
199 199
 
200
-    ALLOC_ARRAY_CLEAR(pool->list, struct ifconfig_pool_entry, pool->size);
200
+    ALLOC_ARRAY_CLEAR(pool->list, struct ifconfig_pool_entry, pool->ipv4.size);
201 201
 
202 202
     msg(D_IFCONFIG_POOL, "IFCONFIG POOL: base=%s size=%d, ipv6=%d",
203
-        print_in_addr_t(pool->base, 0, &gc),
204
-        pool->size, pool->ipv6 );
203
+        print_in_addr_t(pool->ipv4.base, 0, &gc),
204
+        pool->ipv4.size, pool->ipv6.enabled);
205 205
 
206 206
     gc_free(&gc);
207 207
     return pool;
... ...
@@ -213,7 +213,7 @@ ifconfig_pool_free(struct ifconfig_pool *pool)
213 213
     if (pool)
214 214
     {
215 215
         int i;
216
-        for (i = 0; i < pool->size; ++i)
216
+        for (i = 0; i < pool->ipv4.size; ++i)
217 217
         {
218 218
             ifconfig_pool_entry_free(&pool->list[i], true);
219 219
         }
... ...
@@ -239,11 +239,11 @@ ifconfig_pool_acquire(struct ifconfig_pool *pool, in_addr_t *local, in_addr_t *r
239 239
             ipe->common_name = string_alloc(common_name, NULL);
240 240
         }
241 241
 
242
-        switch (pool->type)
242
+        switch (pool->ipv4.type)
243 243
         {
244 244
             case IFCONFIG_POOL_30NET:
245 245
             {
246
-                in_addr_t b = pool->base + (i << 2);
246
+                in_addr_t b = pool->ipv4.base + (i << 2);
247 247
                 *local = b + 1;
248 248
                 *remote = b + 2;
249 249
                 break;
... ...
@@ -251,7 +251,7 @@ ifconfig_pool_acquire(struct ifconfig_pool *pool, in_addr_t *local, in_addr_t *r
251 251
 
252 252
             case IFCONFIG_POOL_INDIV:
253 253
             {
254
-                in_addr_t b = pool->base + i;
254
+                in_addr_t b = pool->ipv4.base + i;
255 255
                 *local = 0;
256 256
                 *remote = b;
257 257
                 break;
... ...
@@ -262,9 +262,9 @@ ifconfig_pool_acquire(struct ifconfig_pool *pool, in_addr_t *local, in_addr_t *r
262 262
         }
263 263
 
264 264
         /* IPv6 pools are always INDIV (--linear) */
265
-        if (pool->ipv6 && remote_ipv6)
265
+        if (pool->ipv6.enabled && remote_ipv6)
266 266
         {
267
-            *remote_ipv6 = add_in6_addr( pool->base_ipv6, i );
267
+            *remote_ipv6 = add_in6_addr(pool->ipv6.base, i);
268 268
         }
269 269
     }
270 270
     return i;
... ...
@@ -274,7 +274,7 @@ bool
274 274
 ifconfig_pool_release(struct ifconfig_pool *pool, ifconfig_pool_handle hand, const bool hard)
275 275
 {
276 276
     bool ret = false;
277
-    if (pool && hand >= 0 && hand < pool->size)
277
+    if (pool && hand >= 0 && hand < pool->ipv4.size)
278 278
     {
279 279
         ifconfig_pool_entry_free(&pool->list[hand], hard);
280 280
         ret = true;
... ...
@@ -291,17 +291,17 @@ ifconfig_pool_ip_base_to_handle(const struct ifconfig_pool *pool, const in_addr_
291 291
 {
292 292
     ifconfig_pool_handle ret = -1;
293 293
 
294
-    switch (pool->type)
294
+    switch (pool->ipv4.type)
295 295
     {
296 296
         case IFCONFIG_POOL_30NET:
297 297
         {
298
-            ret = (addr - pool->base) >> 2;
298
+            ret = (addr - pool->ipv4.base) >> 2;
299 299
             break;
300 300
         }
301 301
 
302 302
         case IFCONFIG_POOL_INDIV:
303 303
         {
304
-            ret = (addr - pool->base);
304
+            ret = (addr - pool->ipv4.base);
305 305
             break;
306 306
         }
307 307
 
... ...
@@ -309,7 +309,7 @@ ifconfig_pool_ip_base_to_handle(const struct ifconfig_pool *pool, const in_addr_
309 309
             ASSERT(0);
310 310
     }
311 311
 
312
-    if (ret < 0 || ret >= pool->size)
312
+    if (ret < 0 || ret >= pool->ipv4.size)
313 313
     {
314 314
         ret = -1;
315 315
     }
... ...
@@ -322,19 +322,19 @@ ifconfig_pool_handle_to_ip_base(const struct ifconfig_pool *pool, ifconfig_pool_
322 322
 {
323 323
     in_addr_t ret = 0;
324 324
 
325
-    if (hand >= 0 && hand < pool->size)
325
+    if (hand >= 0 && hand < pool->ipv4.size)
326 326
     {
327
-        switch (pool->type)
327
+        switch (pool->ipv4.type)
328 328
         {
329 329
             case IFCONFIG_POOL_30NET:
330 330
             {
331
-                ret = pool->base + (hand << 2);
331
+                ret = pool->ipv4.base + (hand << 2);
332 332
                 break;
333 333
             }
334 334
 
335 335
             case IFCONFIG_POOL_INDIV:
336 336
             {
337
-                ret = pool->base + hand;
337
+                ret = pool->ipv4.base + hand;
338 338
                 break;
339 339
             }
340 340
 
... ...
@@ -352,9 +352,9 @@ ifconfig_pool_handle_to_ipv6_base(const struct ifconfig_pool *pool, ifconfig_poo
352 352
     struct in6_addr ret = in6addr_any;
353 353
 
354 354
     /* IPv6 pools are always INDIV (--linear) */
355
-    if (hand >= 0 && hand < pool->size_ipv6)
355
+    if (hand >= 0 && hand < pool->ipv6.size)
356 356
     {
357
-        ret = add_in6_addr( pool->base_ipv6, hand );
357
+        ret = add_in6_addr( pool->ipv6.base, hand );
358 358
     }
359 359
     return ret;
360 360
 }
... ...
@@ -382,13 +382,13 @@ ifconfig_pool_list(const struct ifconfig_pool *pool, struct status_output *out)
382 382
         struct gc_arena gc = gc_new();
383 383
         int i;
384 384
 
385
-        for (i = 0; i < pool->size; ++i)
385
+        for (i = 0; i < pool->ipv4.size; ++i)
386 386
         {
387 387
             const struct ifconfig_pool_entry *e = &pool->list[i];
388 388
             if (e->common_name)
389 389
             {
390 390
                 const in_addr_t ip = ifconfig_pool_handle_to_ip_base(pool, i);
391
-                if (pool->ipv6)
391
+                if (pool->ipv6.enabled)
392 392
                 {
393 393
                     struct in6_addr ip6 = ifconfig_pool_handle_to_ipv6_base(pool, i);
394 394
                     status_printf(out, "%s,%s,%s",
... ...
@@ -47,13 +47,17 @@ struct ifconfig_pool_entry
47 47
 
48 48
 struct ifconfig_pool
49 49
 {
50
-    in_addr_t base;
51
-    int size;
52
-    int type;
53 50
     bool duplicate_cn;
54
-    bool ipv6;
55
-    struct in6_addr base_ipv6;
56
-    unsigned int size_ipv6;
51
+    struct {
52
+        int type;
53
+        in_addr_t base;
54
+        int size;
55
+    } ipv4;
56
+    struct {
57
+        bool enabled;
58
+        struct in6_addr base;
59
+        unsigned int size;
60
+    } ipv6;
57 61
     struct ifconfig_pool_entry *list;
58 62
 };
59 63