mirror of
git://git.code.sf.net/p/dhcpd-pools/code
synced 2025-12-17 08:16:59 +00:00
analyse: critical bug in the counting code
The problem is, that you simply count all lease occurrences in dhcpd.leases, but only the last ones for each ip address are valid. The lease file is more like a logfile of what has been done, than a real database. To fix the counting issue, I'm using a single hash (from uthash.h [1]) for the counting. This way only the last lease entry for each IP gets into my counting structure. When you remove the duplicates in prepare_data(), you don't have the information anymore, if the active lease entry or the free lease entry came last. Simply deleting each ip from the touches array, that is already in the leases array, gives you a big chance to count wrong. Another way of fixing this would be to not only store the ips in your arrays, but a structure containing the ip and a global lease entry counter. Then you could delete all entries except for the latest. [1] http://uthash.sourceforge.net/ Reported-by: Huangy Signed-off-by: Enno Grper <groepeen@cms.hu-berlin.de> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
3ef5d6c07f
commit
ae7747db87
10 changed files with 166 additions and 150 deletions
80
src/hash.c
Normal file
80
src/hash.c
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* The dhcpd-pools has BSD 2-clause license which also known as "Simplified
|
||||
* BSD License" or "FreeBSD License".
|
||||
*
|
||||
* Copyright 2012- Enno Gröper. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are
|
||||
* those of the authors and should not be interpreted as representing
|
||||
* official policies, either expressed or implied, of Sami Kerola.
|
||||
*/
|
||||
|
||||
#include "dhcpd-pools.h"
|
||||
|
||||
void add_lease(int ip, enum ltype type)
|
||||
{
|
||||
struct leases_t *l;
|
||||
l = safe_malloc(sizeof(struct leases_t));
|
||||
l->ip = ip;
|
||||
l->type = type;
|
||||
HASH_ADD_INT(leases, ip, l);
|
||||
}
|
||||
|
||||
struct leases_t *find_lease(int ip)
|
||||
{
|
||||
struct leases_t *l;
|
||||
|
||||
HASH_FIND_INT(leases, &ip, l);
|
||||
return l;
|
||||
}
|
||||
|
||||
void delete_lease(struct leases_t *lease)
|
||||
{
|
||||
HASH_DEL(leases, lease);
|
||||
free(lease);
|
||||
}
|
||||
|
||||
/* uthash >= 1.9.2
|
||||
void delete_all_leases()
|
||||
{
|
||||
struct leases_t *l, *tmp;
|
||||
HASH_ITER(hh, leases, l, tmp) {
|
||||
HASH_DEL(leases, l);
|
||||
free(l);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
void delete_all_leases()
|
||||
{
|
||||
struct leases_t *l;
|
||||
while (leases) {
|
||||
l = leases;
|
||||
HASH_DEL(leases, l); /* leases advances to next on delete */
|
||||
free(l);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue