summaryrefslogtreecommitdiff
path: root/sbin
diff options
context:
space:
mode:
authorKenneth R Westerback <krw@cvs.openbsd.org>2013-05-02 14:48:36 +0000
committerKenneth R Westerback <krw@cvs.openbsd.org>2013-05-02 14:48:36 +0000
commit3a4fe79c2140b22104c6748e938e2cf520e5ee36 (patch)
treea36508186fc1e59427acfe298be92055e30ee97c /sbin
parent93b471d93a6f50c4aa79db3b83e96c873b138eaa (diff)
Enough of the hand-rolling of queues. Turn client->leases and
client->offered_leases into TAILQs.
Diffstat (limited to 'sbin')
-rw-r--r--sbin/dhclient/clparse.c21
-rw-r--r--sbin/dhclient/dhclient.c79
-rw-r--r--sbin/dhclient/dhcpd.h8
3 files changed, 44 insertions, 64 deletions
diff --git a/sbin/dhclient/clparse.c b/sbin/dhclient/clparse.c
index d84dae96370..ccde4ecf29a 100644
--- a/sbin/dhclient/clparse.c
+++ b/sbin/dhclient/clparse.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: clparse.c,v 1.55 2013/03/22 23:58:51 krw Exp $ */
+/* $OpenBSD: clparse.c,v 1.56 2013/05/02 14:48:35 krw Exp $ */
/* Parser for dhclient config and lease files... */
@@ -461,17 +461,11 @@ parse_client_lease_statement(FILE *cfile, int is_static)
* looking for a lease with the same address, and if we find it,
* toss it.
*/
- pl = NULL;
- for (lp = client->leases; lp; lp = lp->next) {
+ TAILQ_FOREACH_SAFE(lp, &client->leases, next, pl) {
if (lp->address.s_addr == lease->address.s_addr) {
- if (pl)
- pl->next = lp->next;
- else
- client->leases = lp->next;
+ TAILQ_REMOVE(&client->leases, lp, next);
free_client_lease(lp);
- break;
- } else
- pl = lp;
+ }
}
/*
@@ -479,8 +473,7 @@ parse_client_lease_statement(FILE *cfile, int is_static)
* recorded leases - don't make it the active lease.
*/
if (is_static) {
- lease->next = client->leases;
- client->leases = lease;
+ TAILQ_INSERT_HEAD(&client->leases, lease, next);
return;
}
@@ -505,8 +498,8 @@ parse_client_lease_statement(FILE *cfile, int is_static)
lease->address.s_addr)
free_client_lease(client->active);
else {
- client->active->next = client->leases;
- client->leases = client->active;
+ TAILQ_INSERT_HEAD(&client->leases, client->active,
+ next);
}
}
client->active = lease;
diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c
index cb044d9d9cc..7b392f9a0d8 100644
--- a/sbin/dhclient/dhclient.c
+++ b/sbin/dhclient/dhclient.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dhclient.c,v 1.244 2013/04/27 17:54:24 krw Exp $ */
+/* $OpenBSD: dhclient.c,v 1.245 2013/05/02 14:48:35 krw Exp $ */
/*
* Copyright 2004 Henning Brauer <henning@openbsd.org>
@@ -395,6 +395,8 @@ main(int argc, char *argv[])
client = calloc(1, sizeof(*client));
if (client == NULL)
error("client calloc");
+ TAILQ_INIT(&client->leases);
+ TAILQ_INIT(&client->offered_leases);
config = calloc(1, sizeof(*config));
if (config == NULL)
error("config calloc");
@@ -644,28 +646,25 @@ state_init(void)
void
state_selecting(void)
{
- struct client_lease *lp, *next, *picked;
+ struct client_lease *lp, *picked;
time_t cur_time;
/* Cancel state_selecting and send_discover timeouts, since either
one could have got us here. */
cancel_timeout();
- /* We have received one or more DHCPOFFER packets. Currently,
- the only criterion by which we judge leases is whether or
- not we get a response when we arp for them. */
- picked = NULL;
- for (lp = client->offered_leases; lp; lp = next) {
- next = lp->next;
- if (!picked) {
- picked = lp;
- } else {
- make_decline(lp);
- send_decline();
- free_client_lease(lp);
- }
+ /* Take the first DHCPOFFER, discard the rest. */
+ picked = TAILQ_FIRST(&client->offered_leases);
+ if (picked)
+ TAILQ_REMOVE(&client->offered_leases, picked, next);
+
+ while (!TAILQ_EMPTY(&client->offered_leases)) {
+ lp = TAILQ_FIRST(&client->offered_leases);
+ TAILQ_REMOVE(&client->leases, lp, next);
+ make_decline(lp);
+ send_decline();
+ free_client_lease(lp);
}
- client->offered_leases = NULL;
/*
* If we just tossed all the leases we were offered, go back
@@ -676,8 +675,6 @@ state_selecting(void)
return;
}
- picked->next = NULL;
-
time(&cur_time);
/* If it was a BOOTREPLY, we can just take the address right now. */
@@ -885,7 +882,7 @@ dhcpoffer(struct in_addr client_addr, struct option_data *options, char *info)
}
/* If we've already seen this lease, don't record it again. */
- for (lp = client->offered_leases; lp; lp = lp->next) {
+ TAILQ_FOREACH(lp, &client->offered_leases, next) {
if (!memcmp(&lp->address.s_addr, &client->packet.yiaddr,
sizeof(in_addr_t))) {
#ifdef DEBUG
@@ -916,22 +913,14 @@ dhcpoffer(struct in_addr client_addr, struct option_data *options, char *info)
/* Figure out when we're supposed to stop selecting. */
stop_selecting = client->first_sending + config->select_interval;
- /* If this is the lease we asked for, put it at the head of the
- list, and don't mess with the arp request timeout. */
- if (lease->address.s_addr == client->requested_address.s_addr) {
- lease->next = client->offered_leases;
- client->offered_leases = lease;
+ if (TAILQ_EMPTY(&client->offered_leases)) {
+ TAILQ_INSERT_HEAD(&client->offered_leases, lease, next);
+ } else if (lease->address.s_addr == client->requested_address.s_addr) {
+ /* The lease we expected - put it at the head of the list. */
+ TAILQ_INSERT_HEAD(&client->offered_leases, lease, next);
} else {
- /* Put the lease at the end of the list. */
- lease->next = NULL;
- if (!client->offered_leases)
- client->offered_leases = lease;
- else {
- for (lp = client->offered_leases; lp->next;
- lp = lp->next)
- ; /* nothing */
- lp->next = lease;
- }
+ /* Not the lease we expected - put it at the end of the list. */
+ TAILQ_INSERT_TAIL(&client->offered_leases, lease, next);
}
note("%s", info);
@@ -1151,7 +1140,6 @@ void
state_panic(void)
{
struct client_lease *loop = client->active;
- struct client_lease *lp;
time_t cur_time;
time(&cur_time);
@@ -1159,7 +1147,7 @@ state_panic(void)
/* We may not have an active lease, but we may have some
predefined leases that we can try. */
- if (!client->active && client->leases)
+ if (!client->active && !TAILQ_EMPTY(&client->leases))
goto activate_next;
/* Run through the list of leases and see if one can be used. */
@@ -1190,8 +1178,9 @@ state_panic(void)
}
/* If there are no other leases, give up. */
- if (!client->leases) {
- client->leases = client->active;
+ if (TAILQ_EMPTY(&client->leases)) {
+ TAILQ_INSERT_HEAD(&client->leases, client->active,
+ next);
client->active = NULL;
break;
}
@@ -1199,13 +1188,11 @@ state_panic(void)
activate_next:
/* Otherwise, put the active lease at the end of the
lease list, and try another lease.. */
- for (lp = client->leases; lp->next; lp = lp->next)
- ;
- lp->next = client->active;
- if (lp->next)
- lp->next->next = NULL;
- client->active = client->leases;
- client->leases = client->leases->next;
+ if (client->active)
+ TAILQ_INSERT_TAIL(&client->leases, client->active,
+ next);
+ client->active = TAILQ_FIRST(&client->leases);
+ TAILQ_REMOVE(&client->leases, client->active, next);
/* If we already tried this lease, we've exhausted the
set of leases, so we might as well give up for
@@ -1559,7 +1546,7 @@ rewrite_client_leases(void)
fflush(leaseFile);
rewind(leaseFile);
- for (lp = client->leases; lp; lp = lp->next) {
+ TAILQ_FOREACH(lp, &client->leases, next) {
/* Skip any leases that duplicate the active lease address. */
if (client->active && lp->address.s_addr ==
client->active->address.s_addr)
diff --git a/sbin/dhclient/dhcpd.h b/sbin/dhclient/dhcpd.h
index 485db698e7c..1f4e6e0be4c 100644
--- a/sbin/dhclient/dhcpd.h
+++ b/sbin/dhclient/dhcpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: dhcpd.h,v 1.114 2013/04/27 17:54:24 krw Exp $ */
+/* $OpenBSD: dhcpd.h,v 1.115 2013/05/02 14:48:35 krw Exp $ */
/*
* Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
@@ -99,7 +99,7 @@ struct hardware {
};
struct client_lease {
- struct client_lease *next;
+ TAILQ_ENTRY(client_lease) next;
time_t expiry, renewal, rebind;
struct in_addr address;
char *server_name;
@@ -153,8 +153,8 @@ struct client_config {
struct client_state {
struct client_lease *active;
struct client_lease *new;
- struct client_lease *offered_leases;
- struct client_lease *leases;
+ TAILQ_HEAD(, client_lease) offered_leases;
+ TAILQ_HEAD(, client_lease) leases;
enum dhcp_state state;
struct in_addr destination;
u_int32_t xid;