blob: f38c1be1c23f0e9ca842c4bbcefc843eabf22552 [file] [log] [blame]
Harald Welte1d8ffc62017-10-12 19:30:49 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <time.h>
6
7#include <osmocom/core/utils.h>
8#include <osmocom/core/application.h>
9#include <osmocom/core/logging.h>
10
11#include "../../lib/in46_addr.h"
12#include "../../lib/ippool.h"
13#include "../../lib/syserr.h"
14
15
16static struct ippool_t *create_pool(const char *prefix_str, unsigned int flags)
17{
18 struct ippool_t *pool;
19 struct in46_prefix pfx;
20 size_t t;
21 int rc;
22
23 /* dynamic-only v4 */
24
25 rc = ippool_aton(&pfx.addr, &t, prefix_str, flags);
26 OSMO_ASSERT(rc == 0);
27 pfx.prefixlen = t;
28
29 rc = ippool_new(&pool, &pfx, NULL, flags);
30 OSMO_ASSERT(rc == 0);
31
32 //ippool_printaddr(pool);
33
34 return pool;
35}
36
37static void test_pool_size(const char *pfx, unsigned int flags, unsigned int expected_size)
38{
39 struct ippool_t *pool;
40 struct ippoolm_t *member;
41 struct in46_addr addr;
42 int i, rc, n;
43
44 printf("testing pool for prefix %s, flags=0x%x, expected_size=%u\n", pfx, flags, expected_size);
45 pool = create_pool(pfx, flags);
46 OSMO_ASSERT(pool->listsize == expected_size);
47
48 memset(&addr, 0, sizeof(addr));
49 addr.len = pool->member[0].addr.len;
50
51 /* allocate all addresses */
52 for (i = 0; i < expected_size; i++) {
53 member = NULL;
54 rc = ippool_newip(pool, &member, &addr, 0);
55 OSMO_ASSERT(rc == 0);
56 OSMO_ASSERT(member);
57 printf("allocated address %s\n", in46a_ntoa(&member->addr));
58 }
59 /* allocate one more, expect that to fail */
60 rc = ippool_newip(pool, &member, &addr, 0);
61 OSMO_ASSERT(rc < 0);
62
63 /* release a (random) number N of (random) member address */
64 n = rand() % pool->listsize;
65 for (i = 0; i < n; i++) {
66 int r;
67 /* chose a random index that is in use */
68 do {
69 r = rand() % pool->listsize;
70 } while (!pool->member[r].inuse);
71 /* and free it... */
72 rc = ippool_freeip(pool, &pool->member[r]);
73 OSMO_ASSERT(rc == 0);
74 }
75
76 /* allocate all N previously released addresses */
77 for (i = 0; i < n; i++) {
78 member = NULL;
79 rc = ippool_newip(pool, &member, &addr, 0);
80 OSMO_ASSERT(rc == 0);
81 OSMO_ASSERT(member);
82 }
83
84 /* allocate one more, expect that to fail */
85 rc = ippool_newip(pool, &member, &addr, 0);
86 OSMO_ASSERT(rc < 0);
87
88 ippool_free(pool);
89}
90
91static void test_pool_sizes(void)
92{
Pau Espin Pedrol361cb9e2017-10-13 11:56:16 +020093 /* 256 addresses [0..255] */
94 test_pool_size("192.168.23.0/24", 0, 256);
95
96 /* 255 addresses [1..255] */
97 test_pool_size("192.168.23.0/24", IPPOOL_NONETWORK, 255);
Harald Welte1d8ffc62017-10-12 19:30:49 +080098
99 /* 254 addresses [1..254] */
Pau Espin Pedrol361cb9e2017-10-13 11:56:16 +0200100 test_pool_size("192.168.23.0/24", IPPOOL_NONETWORK | IPPOOL_NOBROADCAST, 254);
Harald Welte1d8ffc62017-10-12 19:30:49 +0800101
Pau Espin Pedrol361cb9e2017-10-13 11:56:16 +0200102 /* 65534 addresses [0.1..255.254] */
103 test_pool_size("192.168.0.0/16", IPPOOL_NONETWORK | IPPOOL_NOBROADCAST, 65534);
Harald Welte1d8ffc62017-10-12 19:30:49 +0800104
Pau Espin Pedrol361cb9e2017-10-13 11:56:16 +0200105 /* 256 prefixes of /64 each */
106 test_pool_size("2001:DB8::/56", 0, 256);
Harald Welte1d8ffc62017-10-12 19:30:49 +0800107}
108
109int main(int argc, char **argv)
110{
111 osmo_init_logging(&log_info);
112 log_set_use_color(osmo_stderr_target, 0);
113 log_set_print_filename(osmo_stderr_target, 0);
114
115 srand(time(NULL));
116
117 test_pool_sizes();
118}