summaryrefslogtreecommitdiff
path: root/sbin
diff options
context:
space:
mode:
authorKenneth R Westerback <krw@cvs.openbsd.org>2007-01-27 22:05:25 +0000
committerKenneth R Westerback <krw@cvs.openbsd.org>2007-01-27 22:05:25 +0000
commit6c19e7dc041a39463b3c894fea6ae8ccda1d7ce9 (patch)
tree84743c7172104369c45c6726e52b202fe0af94ae /sbin
parent2e9a5cd6e3a9050fccfa9a96f111f9dcf437b23e (diff)
We only poll on the bpf fd and the routing socket fd. No need for
complex list code to keep track. So nuke it. Also eliminate unnecessary variable ('dead') that means 'rfdesc is closed'. Just set rfdesc to -1 when it is closed and use that. As suggested by millert@ and claudio@, don't bother setting revents members to zero since poll() does that. Np functional change, just less code. 'looks good' claudio@ 'I like where this is going' henning@
Diffstat (limited to 'sbin')
-rw-r--r--sbin/dhclient/dhclient.c10
-rw-r--r--sbin/dhclient/dhcpd.h16
-rw-r--r--sbin/dhclient/dispatch.c100
3 files changed, 32 insertions, 94 deletions
diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c
index b8da27e6e4f..117f0249e87 100644
--- a/sbin/dhclient/dhclient.c
+++ b/sbin/dhclient/dhclient.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dhclient.c,v 1.105 2007/01/25 01:21:04 krw Exp $ */
+/* $OpenBSD: dhclient.c,v 1.106 2007/01/27 22:05:24 krw Exp $ */
/*
* Copyright 2004 Henning Brauer <henning@openbsd.org>
@@ -71,7 +71,7 @@ int privfd;
int nullfd = -1;
int no_daemon;
int unknown_ok = 1;
-int routefd;
+int routefd = -1;
struct iaddr iaddr_broadcast = { 4, { 255, 255, 255, 255 } };
struct in_addr inaddr_any;
@@ -85,7 +85,6 @@ struct client_config *config;
int findproto(char *, int);
struct sockaddr *get_ifa(char *, int);
-void routehandler(struct protocol *);
void usage(void);
int check_option(struct client_lease *l, int option);
int ipv4addrs(char * buf);
@@ -151,7 +150,7 @@ struct iaddr defaddr = { 4 };
/* ARGSUSED */
void
-routehandler(struct protocol *p)
+routehandler(void)
{
char msg[2048];
struct rt_msghdr *rtm;
@@ -358,8 +357,7 @@ main(int argc, char *argv[])
priv_script_write_params("alias_", client->alias);
priv_script_go();
- if ((routefd = socket(PF_ROUTE, SOCK_RAW, 0)) != -1)
- add_protocol("AF_ROUTE", routefd, routehandler);
+ routefd = socket(PF_ROUTE, SOCK_RAW, 0);
/* set up the interface */
discover_interface();
diff --git a/sbin/dhclient/dhcpd.h b/sbin/dhclient/dhcpd.h
index 0feca0fcbe5..eba223029fe 100644
--- a/sbin/dhclient/dhcpd.h
+++ b/sbin/dhclient/dhcpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: dhcpd.h,v 1.63 2007/01/25 01:21:04 krw Exp $ */
+/* $OpenBSD: dhcpd.h,v 1.64 2007/01/27 22:05:24 krw Exp $ */
/*
* Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
@@ -188,7 +188,6 @@ struct interface_info {
struct ifreq *ifp;
int noifmedia;
int errors;
- int dead;
u_int16_t index;
};
@@ -198,12 +197,6 @@ struct timeout {
void (*func)(void);
};
-struct protocol {
- struct protocol *next;
- int fd;
- void (*handler)(struct protocol *);
-};
-
#define _PATH_DHCLIENT_CONF "/etc/dhclient.conf"
#define _PATH_DHCLIENT_DB "/var/db/dhclient.leases"
#define _PATH_DHCLIENT_SCRIPT "/sbin/dhclient-script"
@@ -257,11 +250,9 @@ ssize_t receive_packet(struct sockaddr_in *, struct hardware *);
void discover_interface(void);
void reinitialize_interface(void);
void dispatch(void);
-void got_one(struct protocol *);
+void got_one(void);
void add_timeout(time_t, void (*)(void));
void cancel_timeout(void (*)(void));
-void add_protocol(char *, int, void (*)(struct protocol *));
-void remove_protocol(struct protocol *);
int interface_link_status(char *);
int interface_link_forceup(char *);
void interface_link_forcedown(char *);
@@ -290,6 +281,7 @@ extern char *path_dhclient_conf;
extern char *path_dhclient_db;
extern time_t cur_time;
extern int log_perror;
+extern int routefd;
void dhcpoffer(struct iaddr, struct option_data *);
void dhcpack(struct iaddr, struct option_data *);
@@ -332,6 +324,8 @@ struct client_lease *packet_to_lease(struct iaddr, struct option_data *);
void go_daemon(void);
void client_location_changed(void);
+void routehandler(void);
+
/* packet.c */
void assemble_hw_header(unsigned char *, int *, struct hardware *);
void assemble_udp_ip_header(unsigned char *, int *, u_int32_t, u_int32_t,
diff --git a/sbin/dhclient/dispatch.c b/sbin/dhclient/dispatch.c
index 8db2a9b888a..e7696e0207c 100644
--- a/sbin/dhclient/dispatch.c
+++ b/sbin/dhclient/dispatch.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dispatch.c,v 1.37 2007/01/11 02:36:29 krw Exp $ */
+/* $OpenBSD: dispatch.c,v 1.38 2007/01/27 22:05:24 krw Exp $ */
/*
* Copyright 2004 Henning Brauer <henning@openbsd.org>
@@ -47,7 +47,6 @@
#include <ifaddrs.h>
#include <poll.h>
-struct protocol *protocols;
struct timeout *timeouts;
static struct timeout *free_timeouts;
static int interfaces_invalidated;
@@ -107,7 +106,6 @@ discover_interface(void)
/* Register the interface... */
if_register_receive();
if_register_send();
- add_protocol(ifi->name, ifi->rfdesc, got_one);
freeifaddrs(ifap);
}
@@ -125,18 +123,10 @@ reinitialize_interface(void)
void
dispatch(void)
{
- int count, i, to_msec, nfds = 0;
- struct protocol *l;
- struct pollfd *fds;
+ int count, to_msec;
+ struct pollfd fds[2];
time_t howlong;
- for (l = protocols; l; l = l->next)
- nfds++;
-
- fds = malloc(nfds * sizeof(struct pollfd));
- if (fds == NULL)
- error("Can't allocate poll structures.");
-
do {
/*
* Call any expired timeouts, and then if there's still
@@ -169,51 +159,42 @@ another:
to_msec = -1;
/* Set up the descriptors to be polled. */
- for (i = 0, l = protocols; l; l = l->next) {
- if (ifi && (l->handler != got_one || !ifi->dead)) {
- fds[i].fd = l->fd;
- fds[i].events = POLLIN;
- fds[i].revents = 0;
- i++;
- }
- }
+ if (!ifi || ifi->rfdesc == -1)
+ error("No live interface to poll on");
- if (i == 0)
- error("No live interfaces to poll on - exiting.");
+ fds[0].fd = ifi->rfdesc;
+ fds[1].fd = routefd; /* Could be -1, which will be ignored. */
+ fds[0].events = fds[1].events = POLLIN;
/* Wait for a packet or a timeout... XXX */
- count = poll(fds, nfds, to_msec);
+ count = poll(fds, 2, to_msec);
+
+ /* Get the current time... */
+ time(&cur_time);
/* Not likely to be transitory... */
if (count == -1) {
if (errno == EAGAIN || errno == EINTR) {
- time(&cur_time);
continue;
} else
error("poll: %m");
}
- /* Get the current time... */
- time(&cur_time);
-
- i = 0;
- for (l = protocols; l; l = l->next) {
- if ((fds[i].revents & (POLLIN | POLLHUP))) {
- fds[i].revents = 0;
- if (ifi && (l->handler != got_one ||
- !ifi->dead))
- (*(l->handler))(l);
- if (interfaces_invalidated)
- break;
- }
- i++;
+ if ((fds[0].revents & (POLLIN | POLLHUP))) {
+ if (ifi && ifi->rfdesc != -1)
+ got_one();
+ }
+ if ((fds[1].revents & (POLLIN | POLLHUP))) {
+ if (ifi && !interfaces_invalidated)
+ routehandler();
}
+
interfaces_invalidated = 0;
} while (1);
}
void
-got_one(struct protocol *l)
+got_one(void)
{
struct sockaddr_in from;
struct hardware hfrom;
@@ -229,10 +210,9 @@ got_one(struct protocol *l)
/* our interface has gone away. */
warning("Interface %s no longer appears valid.",
ifi->name);
- ifi->dead = 1;
interfaces_invalidated = 1;
- close(l->fd);
- remove_protocol(l);
+ close(ifi->rfdesc);
+ ifi->rfdesc = -1;
}
return;
}
@@ -445,40 +425,6 @@ cancel_timeout(void (*where)(void))
}
}
-/* Add a protocol to the list of protocols... */
-void
-add_protocol(char *name, int fd, void (*handler)(struct protocol *))
-{
- struct protocol *p;
-
- p = malloc(sizeof(*p));
- if (!p)
- error("can't allocate protocol struct for %s", name);
-
- p->fd = fd;
- p->handler = handler;
- p->next = protocols;
- protocols = p;
-}
-
-void
-remove_protocol(struct protocol *proto)
-{
- struct protocol *p, *next, *prev;
-
- prev = NULL;
- for (p = protocols; p; p = next) {
- next = p->next;
- if (p == proto) {
- if (prev)
- prev->next = p->next;
- else
- protocols = p->next;
- free(p);
- }
- }
-}
-
int
interface_link_status(char *ifname)
{