5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
6 * Copyright (C) 2012 BMW Car IT GmbH. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/errno.h>
33 #include <sys/socket.h>
37 struct connman_ippool {
38 unsigned int refcount;
49 static GHashTable *hash_pool;
50 static uint32_t last_block;
51 static uint32_t block_16_bits;
52 static uint32_t block_20_bits;
53 static uint32_t block_24_bits;
54 static uint32_t subnet_mask_24;
56 struct connman_ippool *
57 __connman_ippool_ref_debug(struct connman_ippool *pool,
58 const char *file, int line, const char *caller)
60 DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount + 1,
63 __sync_fetch_and_add(&pool->refcount, 1);
68 void __connman_ippool_unref_debug(struct connman_ippool *pool,
69 const char *file, int line, const char *caller)
74 DBG("%p ref %d by %s:%d:%s()", pool, pool->refcount - 1,
77 if (__sync_fetch_and_sub(&pool->refcount, 1) != 1)
80 g_hash_table_remove(hash_pool, GUINT_TO_POINTER(pool->block));
83 static char *get_ip(uint32_t ip)
87 addr.s_addr = htonl(ip);
89 return g_strdup(inet_ntoa(addr));
92 static uint32_t next_block(uint32_t block)
97 * Return the next IP block within the private IP range
99 * 16-bit block 192.168.0.0 – 192.168.255.255
100 * 20-bit block 172.16.0.0 – 172.31.255.255
101 * 24-bit block 10.0.0.0 – 10.255.255.255
104 next = (block & 0x0000ff00) >> 8;
108 if ((block & 0xffffff00) == block_16_bits) {
110 * Reached the end of the 16 bit block, switch
111 * to the 20-bit block.
113 return block_20_bits;
116 if ((block & 0xffff0000) >= block_20_bits) {
117 next = (block & 0x00ff0000) >> 16;
118 if (next >= 16 && next < 32)
123 * Reached the end of the 20 bit
124 * block, switch to the 24-bit block.
126 return block_24_bits;
129 return (block & 0xff000000) |
130 ((next << 16) & 0x00ff0000);
133 if ((block & 0xff000000) == block_24_bits) {
134 next = (block & 0x00ff0000) >> 16;
140 * Reached the end of the 24 bit
141 * block, switch to the 16-bit block.
143 return block_16_bits;
146 return (block & 0xff000000) |
147 ((next << 16) & 0x00ff0000);
151 return (block & 0xffff0000) | ((next << 8) & 0x0000ff00);
154 static uint32_t find_free_block()
156 struct connman_ippool *pool;
161 return block_16_bits;
164 * Instead starting always from the 16 bit block, we start
165 * from the last assigned block. This is a simple optimimazion
166 * for the case where a lot of blocks have been assigned, e.g.
167 * the first half of the private IP pool is in use and a new
168 * we need to find a new block.
170 * To only thing we have to make sure is that we terminated if
171 * there is no block left.
175 block = next_block(start);
176 while (start != block) {
177 block = next_block(block);
179 pool = g_hash_table_lookup(hash_pool, GUINT_TO_POINTER(block));
189 struct connman_ippool *__connman_ippool_create(unsigned int start,
192 struct connman_ippool *pool;
196 * The range is at max 255 and we don't support overlapping
199 if (start + range > 254)
202 block = find_free_block();
206 pool = g_try_new0(struct connman_ippool, 1);
218 pool->gateway = get_ip(block + 1);
219 pool->broadcast = get_ip(block + 255);
220 pool->subnet_mask = get_ip(subnet_mask_24);
221 pool->start_ip = get_ip(block + start);
222 pool->end_ip = get_ip(block + start + range);
224 g_hash_table_insert(hash_pool, GUINT_TO_POINTER(pool->block), pool);
229 const char *__connman_ippool_get_gateway(struct connman_ippool *pool)
231 return pool->gateway;
234 const char *__connman_ippool_get_broadcast(struct connman_ippool *pool)
236 return pool->broadcast;
239 const char *__connman_ippool_get_start_ip(struct connman_ippool *pool)
241 return pool->start_ip;
244 const char *__connman_ippool_get_end_ip(struct connman_ippool *pool)
249 const char *__connman_ippool_get_subnet_mask(struct connman_ippool *pool)
251 return pool->subnet_mask;
254 static void pool_free(gpointer data)
256 struct connman_ippool *pool = data;
258 g_free(pool->gateway);
259 g_free(pool->broadcast);
260 g_free(pool->start_ip);
261 g_free(pool->end_ip);
262 g_free(pool->subnet_mask);
267 int __connman_ippool_init(void)
271 /* We start at 254 by default to avoid common addresses */
272 block_16_bits = ntohl(inet_addr("192.168.254.0"));
273 block_20_bits = ntohl(inet_addr("172.16.0.0"));
274 block_24_bits = ntohl(inet_addr("10.0.0.0"));
275 subnet_mask_24 = ntohl(inet_addr("255.255.255.0"));
277 hash_pool = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
283 void __connman_ippool_cleanup(void)
287 g_hash_table_destroy(hash_pool);