udhcpd: disable opton to have absolute lease times in lease file

(that does not work with dumpleases)
dumpleases: fix -a option.
networking/udhcp/*: code shrink, more compact static leases struture,
 better comments, etc

function                                             old     new   delta
find_free_or_expired_address                           -     147    +147
nobody_responds_to_arp                                 -      84     +84
read_opt                                             781     830     +49
dumpleases_main                                      435     447     +12
send_ACK                                             229     232      +3
read_staticlease                                      90      93      +3
addStaticLease                                        60      61      +1
getIpByMac                                            46      43      -3
reservedIp                                            31      20     -11
keywords                                             304     288     -16
send_offer                                           428     403     -25
write_leases                                         225     193     -32
read_leases                                          184     143     -41
read_yn                                               64       -     -64
find_address                                         191       -    -191
------------------------------------------------------------------------------
(add/remove: 2/2 grow/shrink: 5/6 up/down: 299/-383)          Total: -84 bytes
This commit is contained in:
Denis Vlasenko
2009-01-01 17:52:09 +00:00
parent b2ec03813c
commit 0416e3dde1
7 changed files with 191 additions and 183 deletions

View File

@@ -102,48 +102,44 @@ static void add_bootp_options(struct dhcpMessage *packet)
int FAST_FUNC send_offer(struct dhcpMessage *oldpacket)
{
struct dhcpMessage packet;
struct dhcpOfferedAddr *lease = NULL;
uint32_t req_align;
uint32_t lease_time_aligned = server_config.lease;
uint32_t static_lease_ip;
uint8_t *req, *lease_time;
struct option_set *curr;
struct in_addr addr;
uint32_t static_lease_ip;
init_packet(&packet, oldpacket, DHCPOFFER);
static_lease_ip = getIpByMac(server_config.static_leases, oldpacket->chaddr);
/* ADDME: if static, short circuit */
if (!static_lease_ip) {
/* the client is in our lease/offered table */
struct dhcpOfferedAddr *lease;
lease = find_lease_by_chaddr(oldpacket->chaddr);
/* the client is in our lease/offered table */
if (lease) {
if (!lease_expired(lease))
lease_time_aligned = lease->expires - time(0);
signed_leasetime_t tmp = lease->expires - time(NULL);
if (tmp >= 0)
lease_time_aligned = tmp;
packet.yiaddr = lease->yiaddr;
/* Or the client has a requested ip */
} else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP))
/* Or the client has requested an ip */
} else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL
/* Don't look here (ugly hackish thing to do) */
&& memcpy(&req_align, req, 4)
&& (move_from_unaligned32(req_align, req), 1)
/* and the ip is in the lease range */
&& ntohl(req_align) >= server_config.start_ip
&& ntohl(req_align) <= server_config.end_ip
&& !static_lease_ip /* Check that its not a static lease */
/* and is not already taken/offered */
&& (!(lease = find_lease_by_yiaddr(req_align))
/* or its taken, but expired */ /* ADDME: or maybe in here */
/* or its taken, but expired */
|| lease_expired(lease))
) {
packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */
/* otherwise, find a free IP */
packet.yiaddr = req_align;
/* otherwise, find a free IP */
} else {
/* Is it a static lease? (No, because find_address skips static lease) */
packet.yiaddr = find_address(0);
/* try for an expired lease */
if (!packet.yiaddr)
packet.yiaddr = find_address(1);
packet.yiaddr = find_free_or_expired_address();
}
if (!packet.yiaddr) {
@@ -164,8 +160,7 @@ int FAST_FUNC send_offer(struct dhcpMessage *oldpacket)
/* Make sure we aren't just using the lease time from the previous offer */
if (lease_time_aligned < server_config.min_lease)
lease_time_aligned = server_config.lease;
/* ADDME: end of short circuit */
lease_time_aligned = server_config.min_lease;
} else {
/* It is a static lease... use it */
packet.yiaddr = static_lease_ip;
@@ -217,7 +212,7 @@ int FAST_FUNC send_ACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)
if (lease_time_aligned > server_config.lease)
lease_time_aligned = server_config.lease;
else if (lease_time_aligned < server_config.min_lease)
lease_time_aligned = server_config.lease;
lease_time_aligned = server_config.min_lease;
}
add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_aligned));