From de23d2241d6fd8923c353296ab5c5b7db412970d Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Wed, 30 Mar 2011 18:58:09 -0400 Subject: [PATCH] Improve timeout backoff. It's a capped linear backoff. RFC specifies capped randomized exponential, but I don't feel like incurring the cost when the cap is so low. Add comments for the dhcpMessage structure members. --- ndhc/options.c | 2 +- ndhc/packet.h | 31 +++++++++++++++++-------------- ndhc/timeout.c | 5 ++++- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/ndhc/options.c b/ndhc/options.c index bbd1f76..017635d 100644 --- a/ndhc/options.c +++ b/ndhc/options.c @@ -1,5 +1,5 @@ /* options.c - DHCP options handling - * Time-stamp: <2011-03-30 16:41:03 nk> + * Time-stamp: <2011-03-30 18:29:18 nk> * * (c) 2004-2011 Nicholas J. Kain * diff --git a/ndhc/packet.h b/ndhc/packet.h index aec2141..e7b4360 100644 --- a/ndhc/packet.h +++ b/ndhc/packet.h @@ -7,20 +7,23 @@ #include "config.h" struct dhcpMessage { - uint8_t op; - uint8_t htype; - uint8_t hlen; - uint8_t hops; - uint32_t xid; - uint16_t secs; - uint16_t flags; - uint32_t ciaddr; - uint32_t yiaddr; - uint32_t siaddr; - uint32_t giaddr; - uint8_t chaddr[16]; - uint8_t sname[64]; - uint8_t file[128]; + uint8_t op; // Message type: 1 = BOOTREQUEST for clients. + uint8_t htype; // ARP HW address type: always '1' for 10MB ethernet. + uint8_t hlen; // Hardware address length: always '6' for 10MB ethernet. + uint8_t hops; // Client sets to zero. + uint32_t xid; // Transaction ID: random number identifying session + uint16_t secs; // Filled by client: seconds since client began address + // aquisition or renewal process. + uint16_t flags; // DHCP flags + uint32_t ciaddr; // Client IP: only filled in if client is inBOUND, RENEW, + // or REBINDING and can reply to ARP requests + uint32_t yiaddr; // 'your' (client) IP address + uint32_t siaddr; // IP address of next server to use in bootstrap; returned + // in DHCPOFFER or DHCPACK by server + uint32_t giaddr; // relay agent IP: used when booting via relay agent + uint8_t chaddr[16]; // Client MAC address + uint8_t sname[64]; // Server host name (optional); null-terminated string + uint8_t file[128]; // boot file name, null-terminated string uint32_t cookie; uint8_t options[308]; /* 312 - cookie */ }; diff --git a/ndhc/timeout.c b/ndhc/timeout.c index 88dc3f3..9d2f2cb 100644 --- a/ndhc/timeout.c +++ b/ndhc/timeout.c @@ -9,6 +9,8 @@ #include "arp.h" #include "log.h" + +#define DELAY_SEC (((RETRY_DELAY - (RETRY_DELAY / NUMPACKETS)) / NUMPACKETS) + 1) static void init_selecting_timeout(struct client_state_t *cs) { if (cs->packetNum < NUMPACKETS) { @@ -17,7 +19,7 @@ static void init_selecting_timeout(struct client_state_t *cs) /* broadcast */ send_discover(cs->xid, cs->requestedIP); - cs->timeout = ((cs->packetNum == NUMPACKETS - 1) ? 4 : 2) * 1000; + cs->timeout = DELAY_SEC * (cs->packetNum + 1); cs->packetNum++; } else { if (client_config.background_if_no_lease) { @@ -32,6 +34,7 @@ static void init_selecting_timeout(struct client_state_t *cs) cs->timeout = RETRY_DELAY * 1000; } } +#undef DELAY_SEC static void renew_requested_timeout(struct client_state_t *cs) {