udhcp: code shrink; disable time and log server options

function                                             old     new   delta
add_server_options                                     -     100    +100
udhcp_add_simple_option                               92      90      -2
nobody_responds_to_arp                                88      85      -3
dhcp_options                                          66      62      -4
udhcp_add_option_string                              104      94     -10
udhcp_run_script                                     665     654     -11
dhcp_option_strings                                  203     188     -15
static.blank_chaddr                                   16       -     -16
send_ACK                                             211     180     -31
add_bootp_options                                     61       -     -61
udhcpd_main                                         1925    1846     -79
------------------------------------------------------------------------------
(add/remove: 1/2 grow/shrink: 0/8 up/down: 100/-232)         Total: -132 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko
2010-03-21 00:43:11 +01:00
parent 87fa216e1e
commit e5ce91b41b
7 changed files with 126 additions and 127 deletions

View File

@ -75,7 +75,9 @@ static int send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadca
server_config.ifindex);
}
/* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */
/* Send the dhcp packet.
* If force broadcast is set, the packet will be broadcast.
*/
static int send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
{
if (dhcp_pkt->gateway_nip)
@ -94,10 +96,21 @@ static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacke
add_simple_option(packet->options, DHCP_SERVER_ID, server_config.server_nip);
}
/* add in the bootp options */
static void add_bootp_options(struct dhcp_packet *packet)
/* Fill options field, siaddr_nip, and sname and boot_file fields.
* TODO: teach this code to use overload option.
*/
static void add_server_options(struct dhcp_packet *packet)
{
struct option_set *curr = server_config.options;
while (curr) {
if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
add_option_string(packet->options, curr->data);
curr = curr->next;
}
packet->siaddr_nip = server_config.siaddr_nip;
if (server_config.sname)
strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
if (server_config.boot_file)
@ -123,17 +136,17 @@ static uint32_t select_lease_time(struct dhcp_packet *packet)
static int send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease)
{
struct dhcp_packet packet;
uint32_t req_nip;
uint32_t lease_time_sec = server_config.max_lease_sec;
uint8_t *req_ip_opt;
const char *p_host_name;
struct option_set *curr;
struct in_addr addr;
init_packet(&packet, oldpacket, DHCPOFFER);
/* ADDME: if static, short circuit */
if (!static_lease_nip) {
uint32_t req_nip;
uint8_t *req_ip_opt;
/* The client is in our lease/offered table */
if (lease) {
packet.yiaddr = lease->lease_nip;
@ -145,10 +158,10 @@ static int send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip,
/* and the IP is in the lease range */
&& ntohl(req_nip) >= server_config.start_ip
&& ntohl(req_nip) <= server_config.end_ip
/* and is not already taken/offered */
&& (!(lease = find_lease_by_nip(req_nip))
/* or its taken, but expired */
|| is_expired_lease(lease))
/* and */
&& ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */
|| is_expired_lease(lease) /* or is taken, but expired */
)
) {
packet.yiaddr = req_nip;
}
@ -178,19 +191,11 @@ static int send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip,
}
add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
curr = server_config.options;
while (curr) {
if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
add_option_string(packet.options, curr->data);
curr = curr->next;
}
add_bootp_options(&packet);
add_server_options(&packet);
addr.s_addr = packet.yiaddr;
bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
return send_packet(&packet, 0);
return send_packet(&packet, /*force_bcast:*/ 0);
}
static int send_NAK(struct dhcp_packet *oldpacket)
@ -200,13 +205,12 @@ static int send_NAK(struct dhcp_packet *oldpacket)
init_packet(&packet, oldpacket, DHCPNAK);
log1("Sending NAK");
return send_packet(&packet, 1);
return send_packet(&packet, /*force_bcast:*/ 1);
}
static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
{
struct dhcp_packet packet;
struct option_set *curr;
uint32_t lease_time_sec;
struct in_addr addr;
const char *p_host_name;
@ -217,20 +221,12 @@ static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
lease_time_sec = select_lease_time(oldpacket);
add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec));
curr = server_config.options;
while (curr) {
if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
add_option_string(packet.options, curr->data);
curr = curr->next;
}
add_bootp_options(&packet);
add_server_options(&packet);
addr.s_addr = packet.yiaddr;
bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
if (send_packet(&packet, 0) < 0)
if (send_packet(&packet, /*force_bcast:*/ 0) < 0)
return -1;
p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME);
@ -250,20 +246,11 @@ static int send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
static int send_inform(struct dhcp_packet *oldpacket)
{
struct dhcp_packet packet;
struct option_set *curr;
init_packet(&packet, oldpacket, DHCPACK);
add_server_options(&packet);
curr = server_config.options;
while (curr) {
if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
add_option_string(packet.options, curr->data);
curr = curr->next;
}
add_bootp_options(&packet);
return send_packet(&packet, 0);
return send_packet(&packet, /*force_bcast:*/ 0);
}