diff options
author | Kenneth R Westerback <krw@cvs.openbsd.org> | 2006-12-26 21:19:53 +0000 |
---|---|---|
committer | Kenneth R Westerback <krw@cvs.openbsd.org> | 2006-12-26 21:19:53 +0000 |
commit | 1316aeaf3105a839bff95ceb79d1f443f1ba3d42 (patch) | |
tree | 1b8d0cd0157312c711bdd51e55c57361750916fa /sbin/dhclient/dispatch.c | |
parent | 0b1d423f0950146516bcdce64d586ff37f5fe927 (diff) |
Our dhclient only handles one interface. Simplify code by using the
single, global, instance of 'struct interface_info' (ifi) rather than
passing around pointers to it.
"I agree" henning@
Testing moritz@ stevesk@, ok stevesk@
Diffstat (limited to 'sbin/dhclient/dispatch.c')
-rw-r--r-- | sbin/dhclient/dispatch.c | 93 |
1 files changed, 41 insertions, 52 deletions
diff --git a/sbin/dhclient/dispatch.c b/sbin/dhclient/dispatch.c index 38ca77f17aa..d23020454fd 100644 --- a/sbin/dhclient/dispatch.c +++ b/sbin/dhclient/dispatch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dispatch.c,v 1.34 2006/08/29 04:09:27 deraadt Exp $ */ +/* $OpenBSD: dispatch.c,v 1.35 2006/12/26 21:19:52 krw Exp $ */ /* * Copyright 2004 Henning Brauer <henning@openbsd.org> @@ -51,11 +51,10 @@ struct protocol *protocols; struct timeout *timeouts; static struct timeout *free_timeouts; static int interfaces_invalidated; -void (*bootp_packet_handler)(struct interface_info *, - struct dhcp_packet *, int, unsigned int, +void (*bootp_packet_handler)(struct dhcp_packet *, int, unsigned int, struct iaddr, struct hardware *); -static int interface_status(struct interface_info *ifinfo); +static int interface_status(void); /* * Use getifaddrs() to get a list of all the attached interfaces. For @@ -64,7 +63,7 @@ static int interface_status(struct interface_info *ifinfo); * what subnet it's on, and add it to the list of interfaces. */ void -discover_interfaces(struct interface_info *iface) +discover_interface(void) { struct ifaddrs *ifap, *ifa; struct ifreq *tif; @@ -79,7 +78,7 @@ discover_interfaces(struct interface_info *iface) (!(ifa->ifa_flags & IFF_UP))) continue; - if (strcmp(iface->name, ifa->ifa_name)) + if (strcmp(ifi->name, ifa->ifa_name)) continue; /* @@ -90,32 +89,32 @@ discover_interfaces(struct interface_info *iface) struct sockaddr_dl *foo = (struct sockaddr_dl *)ifa->ifa_addr; - iface->index = foo->sdl_index; - iface->hw_address.hlen = foo->sdl_alen; - iface->hw_address.htype = HTYPE_ETHER; /* XXX */ - memcpy(iface->hw_address.haddr, + ifi->index = foo->sdl_index; + ifi->hw_address.hlen = foo->sdl_alen; + ifi->hw_address.htype = HTYPE_ETHER; /* XXX */ + memcpy(ifi->hw_address.haddr, LLADDR(foo), foo->sdl_alen); } - if (!iface->ifp) { + if (!ifi->ifp) { if ((tif = malloc(len)) == NULL) error("no space to remember ifp"); strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ); - iface->ifp = tif; + ifi->ifp = tif; } } - if (!iface->ifp) - error("%s: not found", iface->name); + if (!ifi->ifp) + error("%s: not found", ifi->name); /* Register the interface... */ - if_register_receive(iface); - if_register_send(iface); - add_protocol(iface->name, iface->rfdesc, got_one, iface); + if_register_receive(); + if_register_send(); + add_protocol(ifi->name, ifi->rfdesc, got_one); freeifaddrs(ifap); } void -reinitialize_interfaces(void) +reinitialize_interface(void) { interfaces_invalidated = 1; } @@ -153,7 +152,7 @@ another: if (timeouts->when <= cur_time) { t = timeouts; timeouts = timeouts->next; - (*(t->func))(t->what); + (*(t->func))(); t->next = free_timeouts; free_timeouts = t; goto another; @@ -174,9 +173,7 @@ another: /* Set up the descriptors to be polled. */ for (i = 0, l = protocols; l; l = l->next) { - struct interface_info *ip = l->local; - - if (ip && (l->handler != got_one || !ip->dead)) { + if (ifi && (l->handler != got_one || !ifi->dead)) { fds[i].fd = l->fd; fds[i].events = POLLIN; fds[i].revents = 0; @@ -204,12 +201,10 @@ another: i = 0; for (l = protocols; l; l = l->next) { - struct interface_info *ip; - ip = l->local; if ((fds[i].revents & (POLLIN | POLLHUP))) { fds[i].revents = 0; - if (ip && (l->handler != got_one || - !ip->dead)) + if (ifi && (l->handler != got_one || + !ifi->dead)) (*(l->handler))(l); if (interfaces_invalidated) break; @@ -235,23 +230,21 @@ got_one(struct protocol *l) unsigned char packbuf[4095]; struct dhcp_packet packet; } u; - struct interface_info *ip = l->local; - if ((result = receive_packet(ip, u.packbuf, sizeof(u), &from, - &hfrom)) == -1) { - warning("receive_packet failed on %s: %s", ip->name, + if ((result = receive_packet(u.packbuf, sizeof(u), &from, &hfrom)) == + -1) { + warning("receive_packet failed on %s: %s", ifi->name, strerror(errno)); - ip->errors++; - if ((!interface_status(ip)) || - (ip->noifmedia && ip->errors > 20)) { + ifi->errors++; + if ((!interface_status()) || + (ifi->noifmedia && ifi->errors > 20)) { /* our interface has gone away. */ warning("Interface %s no longer appears valid.", - ip->name); - ip->dead = 1; + ifi->name); + ifi->dead = 1; interfaces_invalidated = 1; close(l->fd); remove_protocol(l); - free(ip); } return; } @@ -262,7 +255,7 @@ got_one(struct protocol *l) ifrom.len = 4; memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len); - (*bootp_packet_handler)(ip, &u.packet, result, + (*bootp_packet_handler)(&u.packet, result, from.sin_port, ifrom, &hfrom); } } @@ -324,10 +317,10 @@ interface_link_forcedown(char *ifname) } int -interface_status(struct interface_info *ifinfo) +interface_status(void) { - char *ifname = ifinfo->name; - int ifsock = ifinfo->rfdesc; + char *ifname = ifi->name; + int ifsock = ifi->rfdesc; struct ifreq ifr; struct ifmediareq ifmr; @@ -347,7 +340,7 @@ interface_status(struct interface_info *ifinfo) goto inactive; /* Next, check carrier on the interface, if possible */ - if (ifinfo->noifmedia) + if (ifi->noifmedia) goto active; memset(&ifmr, 0, sizeof(ifmr)); strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name)); @@ -356,14 +349,14 @@ interface_status(struct interface_info *ifinfo) syslog(LOG_DEBUG, "ioctl(SIOCGIFMEDIA) on %s: %m", ifname); - ifinfo->noifmedia = 1; + ifi->noifmedia = 1; goto active; } /* * EINVAL (or ENOTTY) simply means that the interface * does not support the SIOCGIFMEDIA ioctl. We regard it alive. */ - ifinfo->noifmedia = 1; + ifi->noifmedia = 1; goto active; } if (ifmr.ifm_status & IFM_AVALID) { @@ -385,14 +378,14 @@ active: } void -add_timeout(time_t when, void (*where)(void *), void *what) +add_timeout(time_t when, void (*where)(void)) { struct timeout *t, *q; /* See if this timeout supersedes an existing timeout. */ t = NULL; for (q = timeouts; q; q = q->next) { - if (q->func == where && q->what == what) { + if (q->func == where) { if (t) t->next = q->next; else @@ -409,13 +402,11 @@ add_timeout(time_t when, void (*where)(void *), void *what) q = free_timeouts; free_timeouts = q->next; q->func = where; - q->what = what; } else { q = malloc(sizeof(struct timeout)); if (!q) error("Can't allocate timeout structure!"); q->func = where; - q->what = what; } } @@ -445,14 +436,14 @@ add_timeout(time_t when, void (*where)(void *), void *what) } void -cancel_timeout(void (*where)(void *), void *what) +cancel_timeout(void (*where)(void)) { struct timeout *t, *q; /* Look for this timeout on the list, and unlink it if we find it. */ t = NULL; for (q = timeouts; q; q = q->next) { - if (q->func == where && q->what == what) { + if (q->func == where) { if (t) t->next = q->next; else @@ -471,8 +462,7 @@ cancel_timeout(void (*where)(void *), void *what) /* Add a protocol to the list of protocols... */ void -add_protocol(char *name, int fd, void (*handler)(struct protocol *), - void *local) +add_protocol(char *name, int fd, void (*handler)(struct protocol *)) { struct protocol *p; @@ -482,7 +472,6 @@ add_protocol(char *name, int fd, void (*handler)(struct protocol *), p->fd = fd; p->handler = handler; - p->local = local; p->next = protocols; protocols = p; } |