summaryrefslogtreecommitdiff
path: root/sbin/dhclient/dhclient.c
diff options
context:
space:
mode:
authorKenneth R Westerback <krw@cvs.openbsd.org>2019-07-01 15:06:39 +0000
committerKenneth R Westerback <krw@cvs.openbsd.org>2019-07-01 15:06:39 +0000
commit27ea020f83168df9b2af9cd43e609b9d2cac13ff (patch)
treeb16103fcc4ce8872be7154296a17cc3b8385fec1 /sbin/dhclient/dhclient.c
parent337fe061a76b93a1f17d9b49a16bc4548389e1ac (diff)
Simplify and clarify logic in take_charge().
Use #define's instead of magic numbers. Use total elapsed time to decide when to give up. No intentional functional change.
Diffstat (limited to 'sbin/dhclient/dhclient.c')
-rw-r--r--sbin/dhclient/dhclient.c39
1 files changed, 22 insertions, 17 deletions
diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c
index fd16da36661..ec07700efb6 100644
--- a/sbin/dhclient/dhclient.c
+++ b/sbin/dhclient/dhclient.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dhclient.c,v 1.639 2019/06/30 19:19:08 krw Exp $ */
+/* $OpenBSD: dhclient.c,v 1.640 2019/07/01 15:06:38 krw Exp $ */
/*
* Copyright 2004 Henning Brauer <henning@openbsd.org>
@@ -2428,11 +2428,16 @@ take_charge(struct interface_info *ifi, int routefd)
{
struct pollfd fds[1];
struct rt_msghdr rtm;
- time_t start_time, cur_time;
+ time_t cur_time, sent_time, start_time;
int nfds, retries;
+#define MAXSECONDS 9
+#define SENTSECONDS 3
+#define POLLMILLISECONDS 3
+
if (time(&start_time) == -1)
fatal("time");
+ sent_time = start_time;
/*
* Send RTM_PROPOSAL with RTF_PROTO3 set.
@@ -2457,31 +2462,31 @@ take_charge(struct interface_info *ifi, int routefd)
retries = 0;
while ((ifi->flags & IFI_IN_CHARGE) == 0) {
- time(&cur_time);
- if ((cur_time - start_time) > 3) {
- if (++retries <= 3) {
- if (time(&start_time) == -1)
- fatal("time");
- rtm.rtm_seq = ifi->xid = arc4random();
- if (write(routefd, &rtm, sizeof(rtm)) == -1)
- fatal("write(routefd)");
- } else {
- fatalx("failed to take charge");
- }
+ if (time(&cur_time) == -1)
+ fatal("time");
+ if (cur_time - start_time >= MAXSECONDS)
+ fatalx("failed to take charge");
+
+ if ((cur_time - sent_time) >= SENTSECONDS) {
+ sent_time = cur_time;
+ rtm.rtm_seq = ifi->xid = arc4random();
+ if (write(routefd, &rtm, sizeof(rtm)) == -1)
+ fatal("write(routefd)");
}
+
fds[0].fd = routefd;
fds[0].events = POLLIN;
- nfds = poll(fds, 1, 3);
+ nfds = poll(fds, 1, POLLMILLISECONDS);
if (nfds == -1) {
if (errno == EINTR)
continue;
fatal("poll(routefd)");
}
+
if ((fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) != 0)
fatalx("routefd: ERR|HUP|NVAL");
- if (nfds == 0 || (fds[0].revents & POLLIN) == 0)
- continue;
- routefd_handler(ifi, routefd);
+ if (nfds == 1 && (fds[0].revents & POLLIN) == POLLIN)
+ routefd_handler(ifi, routefd);
}
}