sdhcp

simple dhcp client
git clone git://git.2f30.org/sdhcp.git
Log | Files | Refs | LICENSE

commit 5506c81d9930e68cfb29960a4211454fd46262b9
parent 80bdf55532fadbf9398ce7e12fe494282f45f394
Author: Michael Forney <mforney@mforney.org>
Date:   Wed, 14 Nov 2018 18:40:39 -0800

Resend DHCPREQUEST in case server does not respond

The delays were chosen to follow section 4.1 of RFC 2131.

Diffstat:
sdhcp.c | 29+++++++++++++++++++----------
1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/sdhcp.c b/sdhcp.c @@ -386,7 +386,7 @@ calctimeout(int n, struct itimerspec *ts) static void run(void) { - int forked = 0; + int forked = 0, t; struct itimerspec timeout = { 0 }; Init: @@ -399,8 +399,6 @@ Selecting: for (;;) { switch (dhcprecv()) { case DHCPoffer: - timeout.it_value.tv_sec = 0; - settimeout(0, &timeout); memcpy(client, bp.yiaddr, sizeof(client)); optget(&bp, server, ODserverid, sizeof(server)); optget(&bp, mask, OBmask, sizeof(mask)); @@ -418,15 +416,26 @@ Selecting: } } Requesting: - dhcpsend(DHCPrequest, Broadcast); - for (;;) { - switch (dhcprecv()) { - case DHCPack: - goto Bound; - case DHCPnak: - goto Init; + for (t = 4; t <= 64; t *= 2) { + dhcpsend(DHCPrequest, Broadcast); + timeout.it_value.tv_sec = t; + settimeout(0, &timeout); + for (;;) { + switch (dhcprecv()) { + case DHCPack: + goto Bound; + case DHCPnak: + goto Init; + case Timeout0: + break; + default: + continue; + } + break; } } + /* no response from DHCPREQUEST after several attempts, go to INIT */ + goto Init; Bound: acceptlease(); fputs("Congrats! You should be on the 'net.\n", stdout);