struct connman_ippool;
+typedef void (*ippool_collision_cb_t) (struct connman_ippool *pool,
+ void *user_data);
+
int __connman_ippool_init(void);
void __connman_ippool_cleanup(void);
void __connman_ippool_unref_debug(struct connman_ippool *pool,
const char *file, int line, const char *caller);
-struct connman_ippool *__connman_ippool_create(unsigned int start,
- unsigned int range);
+struct connman_ippool *__connman_ippool_create(int index,
+ unsigned int start,
+ unsigned int range,
+ ippool_collision_cb_t collision_cb,
+ void *user_data);
const char *__connman_ippool_get_gateway(struct connman_ippool *pool);
const char *__connman_ippool_get_broadcast(struct connman_ippool *pool);
const char *__connman_ippool_get_subnet_mask(struct connman_ippool *pool);
const char *__connman_ippool_get_start_ip(struct connman_ippool *pool);
const char *__connman_ippool_get_end_ip(struct connman_ippool *pool);
+
+void __connman_ippool_newaddr(int index, const char *address);
+void __connman_ippool_deladdr(int index, const char *address);
struct connman_ippool {
unsigned int refcount;
+ int index;
uint32_t block;
char *gateway;
char *start_ip;
char *end_ip;
char *subnet_mask;
+
+ ippool_collision_cb_t collision_cb;
+ void *user_data;
};
static GHashTable *hash_pool;
+static GHashTable *hash_addresses;
static uint32_t last_block;
static uint32_t block_16_bits;
static uint32_t block_20_bits;
struct connman_ippool *pool;
uint32_t start;
uint32_t block;
+ uint32_t *key;
if (last_block == 0)
return block_16_bits;
while (start != block) {
block = next_block(block);
- pool = g_hash_table_lookup(hash_pool, GUINT_TO_POINTER(block));
+ key = GUINT_TO_POINTER(block);
+ pool = g_hash_table_lookup(hash_pool, key);
if (pool != NULL)
continue;
+ if (g_hash_table_lookup(hash_addresses, key) != NULL)
+ continue;
+
return block;
}
return 0;
}
-struct connman_ippool *__connman_ippool_create(unsigned int start,
- unsigned int range)
+void __connman_ippool_newaddr(int index, const char *address)
+{
+ struct connman_ippool *pool;
+ struct in_addr inp;
+ uint32_t block;
+ uint32_t *key;
+ unsigned int count;
+
+ if (inet_aton(address, &inp) == 0)
+ return;
+
+ block = ntohl(inp.s_addr) & 0xffffff00;
+
+ key = GUINT_TO_POINTER(block);
+ count = GPOINTER_TO_UINT(g_hash_table_lookup(hash_addresses, key));
+ count = count + 1;
+ g_hash_table_replace(hash_addresses, key, GUINT_TO_POINTER(count));
+
+ pool = g_hash_table_lookup(hash_pool, key);
+ if (pool == NULL)
+ return;
+
+ if (pool->index == index)
+ return;
+
+ if (pool->collision_cb != NULL)
+ pool->collision_cb(pool, pool->user_data);
+}
+
+void __connman_ippool_deladdr(int index, const char *address)
+{
+ struct in_addr inp;
+ uint32_t block;
+ uint32_t *key;
+ unsigned int count;
+
+ if (inet_aton(address, &inp) == 0)
+ return;
+
+ block = ntohl(inp.s_addr) & 0xffffff00;
+
+ key = GUINT_TO_POINTER(block);
+ count = GPOINTER_TO_UINT(g_hash_table_lookup(hash_addresses, key));
+ count = count - 1;
+
+ if (count == 0)
+ g_hash_table_remove(hash_addresses, key);
+ else
+ g_hash_table_replace(hash_addresses, key, GUINT_TO_POINTER(count));
+}
+
+struct connman_ippool *__connman_ippool_create(int index,
+ unsigned int start,
+ unsigned int range,
+ ippool_collision_cb_t collision_cb,
+ void *user_data)
{
struct connman_ippool *pool;
uint32_t block;
return NULL;
pool->refcount = 1;
+ pool->index = index;
pool->block = block;
+ pool->collision_cb = collision_cb;
+ pool->user_data = user_data;
last_block = block;
hash_pool = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
pool_free);
+ hash_addresses = g_hash_table_new_full(g_direct_hash, g_direct_equal,
+ NULL, NULL);
return 0;
}
g_hash_table_destroy(hash_pool);
hash_pool = NULL;
+
+ g_hash_table_destroy(hash_addresses);
+ hash_addresses = NULL;
}
void __connman_tethering_set_enabled(void)
{
+ int index;
int err;
const char *gateway;
const char *broadcast;
if (err < 0)
return;
- dhcp_ippool = __connman_ippool_create(1, 253);
+ index = connman_inet_ifindex(BRIDGE_NAME);
+ dhcp_ippool = __connman_ippool_create(index, 1, 253, NULL, NULL);
if (dhcp_ippool == NULL) {
connman_error("Fail to create IP pool");
return;
pn->fd = fd;
pn->interface = iface;
pn->index = index;
- pn->pool = __connman_ippool_create(1, 1);
+ pn->pool = __connman_ippool_create(pn->fd, 1, 1, NULL, NULL);
if (pn->pool == NULL) {
errno = -ENOMEM;
goto error;
/* Test the IP range */
- pool = __connman_ippool_create(1, 500);
+ pool = __connman_ippool_create(23, 1, 500, NULL, NULL);
g_assert(pool == NULL);
for (i = 1; i < 254; i++) {
- pool = __connman_ippool_create(1, i);
+ pool = __connman_ippool_create(23, 1, i, NULL, NULL);
g_assert(pool);
gateway = __connman_ippool_get_gateway(pool);
*/
while (TRUE) {
- pool = __connman_ippool_create(1, 100);
+ pool = __connman_ippool_create(23, 1, 100, NULL, NULL);
if (pool == NULL)
break;
i += 1;