summaryrefslogtreecommitdiffhomepage
path: root/networking/udhcp/leases.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-04-07 01:05:47 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-04-07 01:05:47 +0000
commitfbd2918f5c91723063ed698026217a77a0fe565b (patch)
treecddfe351592f4a876232cf4c67c23df25bf0563c /networking/udhcp/leases.c
parentbb5b01c7c711dd9ffc2abf23a05ccdfbf7fc0325 (diff)
udhcp: MAC_BCAST_ADDR and blank_chaddr are in fact constant, move to rodata.
a few global variables reduced to smallints function old new delta add_lease 75 227 +152 static.blank_chaddr - 16 +16 MAC_BCAST_ADDR - 6 +6 sockfd 4 8 +4 udhcp_run_script 1153 1155 +2 state 8 5 -3 listen_mode 4 1 -3 perform_release 152 148 -4 fd 8 4 -4 blank_chaddr 16 - -16 udhcpc_main 2518 2497 -21 .rodata 131864 131832 -32 oldest_expired_lease 61 - -61 clear_lease 127 - -127 ------------------------------------------------------------------------------ (add/remove: 2/3 grow/shrink: 3/6 up/down: 180/-271) Total: -91 bytes
Diffstat (limited to 'networking/udhcp/leases.c')
-rw-r--r--networking/udhcp/leases.c68
1 files changed, 35 insertions, 33 deletions
diff --git a/networking/udhcp/leases.c b/networking/udhcp/leases.c
index 2f7847d74..5d8775f63 100644
--- a/networking/udhcp/leases.c
+++ b/networking/udhcp/leases.c
@@ -8,25 +8,41 @@
#include "dhcpd.h"
-uint8_t blank_chaddr[] = {[0 ... 15] = 0};
+/* Find the oldest expired lease, NULL if there are no expired leases */
+static struct dhcpOfferedAddr *oldest_expired_lease(void)
+{
+ struct dhcpOfferedAddr *oldest = NULL;
+ unsigned long oldest_lease = time(0);
+ unsigned i;
+
+
+ for (i = 0; i < server_config.max_leases; i++)
+ if (oldest_lease > leases[i].expires) {
+ oldest_lease = leases[i].expires;
+ oldest = &(leases[i]);
+ }
+ return oldest;
+}
+
/* clear every lease out that chaddr OR yiaddr matches and is nonzero */
-void clear_lease(uint8_t *chaddr, uint32_t yiaddr)
+static void clear_lease(const uint8_t *chaddr, uint32_t yiaddr)
{
- unsigned int i, j;
+ unsigned i, j;
for (j = 0; j < 16 && !chaddr[j]; j++);
for (i = 0; i < server_config.max_leases; i++)
- if ((j != 16 && !memcmp(leases[i].chaddr, chaddr, 16)) ||
- (yiaddr && leases[i].yiaddr == yiaddr)) {
+ if ((j != 16 && memcmp(leases[i].chaddr, chaddr, 16) != 0)
+ || (yiaddr && leases[i].yiaddr == yiaddr)
+ ) {
memset(&(leases[i]), 0, sizeof(struct dhcpOfferedAddr));
}
}
/* add a lease into the table, clearing out any old ones */
-struct dhcpOfferedAddr *add_lease(uint8_t *chaddr, uint32_t yiaddr, unsigned long lease)
+struct dhcpOfferedAddr *add_lease(const uint8_t *chaddr, uint32_t yiaddr, unsigned long lease)
{
struct dhcpOfferedAddr *oldest;
@@ -52,31 +68,14 @@ int lease_expired(struct dhcpOfferedAddr *lease)
}
-/* Find the oldest expired lease, NULL if there are no expired leases */
-struct dhcpOfferedAddr *oldest_expired_lease(void)
-{
- struct dhcpOfferedAddr *oldest = NULL;
- unsigned long oldest_lease = time(0);
- unsigned int i;
-
-
- for (i = 0; i < server_config.max_leases; i++)
- if (oldest_lease > leases[i].expires) {
- oldest_lease = leases[i].expires;
- oldest = &(leases[i]);
- }
- return oldest;
-
-}
-
-
/* Find the first lease that matches chaddr, NULL if no match */
-struct dhcpOfferedAddr *find_lease_by_chaddr(uint8_t *chaddr)
+struct dhcpOfferedAddr *find_lease_by_chaddr(const uint8_t *chaddr)
{
- unsigned int i;
+ unsigned i;
for (i = 0; i < server_config.max_leases; i++)
- if (!memcmp(leases[i].chaddr, chaddr, 16)) return &(leases[i]);
+ if (!memcmp(leases[i].chaddr, chaddr, 16))
+ return &(leases[i]);
return NULL;
}
@@ -85,10 +84,11 @@ struct dhcpOfferedAddr *find_lease_by_chaddr(uint8_t *chaddr)
/* Find the first lease that matches yiaddr, NULL is no match */
struct dhcpOfferedAddr *find_lease_by_yiaddr(uint32_t yiaddr)
{
- unsigned int i;
+ unsigned i;
for (i = 0; i < server_config.max_leases; i++)
- if (leases[i].yiaddr == yiaddr) return &(leases[i]);
+ if (leases[i].yiaddr == yiaddr)
+ return &(leases[i]);
return NULL;
}
@@ -97,6 +97,8 @@ struct dhcpOfferedAddr *find_lease_by_yiaddr(uint32_t yiaddr)
/* check is an IP is taken, if it is, add it to the lease table */
static int check_ip(uint32_t addr)
{
+ static const uint8_t blank_chaddr[16]; /* 16 zero bytes */
+
struct in_addr temp;
if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) {
@@ -105,7 +107,8 @@ static int check_ip(uint32_t addr)
inet_ntoa(temp), server_config.conflict_time);
add_lease(blank_chaddr, addr, server_config.conflict_time);
return 1;
- } else return 0;
+ }
+ return 0;
}
@@ -125,7 +128,7 @@ uint32_t find_address(int check_expired)
/* ie, 192.168.55.255 */
if ((addr & 0xFF) == 0xFF) continue;
- /* Only do if it isn't an assigned as a static lease */
+ /* Only do if it isn't assigned as a static lease */
if (!reservedIp(server_config.static_leases, htonl(addr))) {
/* lease is not taken */
@@ -133,11 +136,10 @@ uint32_t find_address(int check_expired)
lease = find_lease_by_yiaddr(ret);
/* no lease or it expired and we are checking for expired leases */
- if ( (!lease || (check_expired && lease_expired(lease)))
+ if ((!lease || (check_expired && lease_expired(lease)))
&& /* and it isn't on the network */ !check_ip(ret)
) {
return ret;
- break;
}
}
}