From: Daniel Wagner <[email protected]>

---
 src/connman.h      |   10 ++++++-
 src/ippool.c       |   73 +++++++++++++++++++++++++++++++++++++++++++++++++---
 src/tethering.c    |    4 +-
 unit/test-ippool.c |    6 ++--
 4 files changed, 83 insertions(+), 10 deletions(-)

diff --git a/src/connman.h b/src/connman.h
index 66d6fc4..74bce53 100644
--- a/src/connman.h
+++ b/src/connman.h
@@ -706,6 +706,9 @@ int __connman_6to4_check(struct connman_ipconfig *ipconfig);
 
 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);
 
@@ -720,10 +723,15 @@ 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);
+                                       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(const char *address);
+void __connman_ippool_deladdr(const char *address);
diff --git a/src/ippool.c b/src/ippool.c
index 3a91e2c..081c7b0 100644
--- a/src/ippool.c
+++ b/src/ippool.c
@@ -44,9 +44,13 @@ struct connman_ippool {
        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;
@@ -156,6 +160,7 @@ static uint32_t find_free_block()
        struct connman_ippool *pool;
        uint32_t start;
        uint32_t block;
+       uint32_t *key;
 
        if (last_block == 0)
                return block_16_bits;
@@ -176,18 +181,73 @@ static uint32_t find_free_block()
        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;
 }
 
+void __connman_ippool_newaddr(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->collision_cb != NULL)
+               pool->collision_cb(pool, pool->user_data);
+
+}
+
+void __connman_ippool_deladdr(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(unsigned int start,
-                                               unsigned int range)
+                                       unsigned int range,
+                                       ippool_collision_cb_t collision_cb,
+                                       void *user_data)
 {
        struct connman_ippool *pool;
        uint32_t block;
@@ -209,11 +269,11 @@ struct connman_ippool *__connman_ippool_create(unsigned 
int start,
 
        pool->refcount = 1;
        pool->block = block;
+       pool->collision_cb = collision_cb;
+       pool->user_data = user_data;
 
        last_block = block;
 
-
-
        if (range == 0)
                range = 1;
 
@@ -278,6 +338,8 @@ int __connman_ippool_init(void)
 
        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;
 }
@@ -288,4 +350,7 @@ void __connman_ippool_cleanup(void)
 
        g_hash_table_destroy(hash_pool);
        hash_pool = NULL;
+
+       g_hash_table_destroy(hash_addresses);
+       hash_addresses = NULL;
 }
diff --git a/src/tethering.c b/src/tethering.c
index 85dbab7..0c83797 100644
--- a/src/tethering.c
+++ b/src/tethering.c
@@ -346,7 +346,7 @@ void __connman_tethering_set_enabled(void)
        if (err < 0)
                return;
 
-       dhcp_ippool = __connman_ippool_create(1, 253);
+       dhcp_ippool = __connman_ippool_create(1, 253, NULL, NULL);
        if (dhcp_ippool == NULL) {
                connman_error("Fail to create IP pool");
                return;
@@ -583,7 +583,7 @@ int __connman_private_network_request(DBusMessage *msg, 
const char *owner)
        pn->fd = fd;
        pn->interface = iface;
        pn->index = index;
-       pn->pool = __connman_ippool_create(1, 1);
+       pn->pool = __connman_ippool_create(1, 1, NULL, NULL);
        if (pn->pool == NULL) {
                errno = -ENOMEM;
                goto error;
diff --git a/unit/test-ippool.c b/unit/test-ippool.c
index edfe1e3..cad5fb7 100644
--- a/unit/test-ippool.c
+++ b/unit/test-ippool.c
@@ -51,11 +51,11 @@ static void test_ippool_basic0(gconstpointer user_data)
 
        /* Test the IP range */
 
-       pool = __connman_ippool_create(1, 500);
+       pool = __connman_ippool_create(1, 500, NULL, NULL);
        g_assert(pool == NULL);
 
        for (i = 1; i < 254; i++) {
-               pool = __connman_ippool_create(1, i);
+               pool = __connman_ippool_create(1, i, NULL, NULL);
                g_assert(pool);
 
                gateway = __connman_ippool_get_gateway(pool);
@@ -103,7 +103,7 @@ static void test_ippool_basic1(gconstpointer user_data)
         */
 
        while (TRUE) {
-               pool = __connman_ippool_create(1, 100);
+               pool = __connman_ippool_create(1, 100, NULL, NULL);
                if (pool == NULL)
                        break;
                i += 1;
-- 
1.7.8.2.325.g247f9

_______________________________________________
connman mailing list
[email protected]
http://lists.connman.net/listinfo/connman

Reply via email to