diff options
author | Martin Pelikan <pelikan@cvs.openbsd.org> | 2014-06-11 16:45:16 +0000 |
---|---|---|
committer | Martin Pelikan <pelikan@cvs.openbsd.org> | 2014-06-11 16:45:16 +0000 |
commit | 6e4f776e349c7dbcaf1ee6a2aa65337372716ffe (patch) | |
tree | 1543fcb7c89d927a590d00f6fc1c37ff5acf2587 | |
parent | 5f8ad3a979574ecce70f5d0c85e41f138795b776 (diff) |
Prettify & refactor, no functional change.
ok beck
-rw-r--r-- | usr.sbin/dhcpd/bootp.c | 19 | ||||
-rw-r--r-- | usr.sbin/dhcpd/memory.c | 42 |
2 files changed, 33 insertions, 28 deletions
diff --git a/usr.sbin/dhcpd/bootp.c b/usr.sbin/dhcpd/bootp.c index e022b142dbf..319736a5ab2 100644 --- a/usr.sbin/dhcpd/bootp.c +++ b/usr.sbin/dhcpd/bootp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bootp.c,v 1.13 2006/03/13 19:57:42 otto Exp $ */ +/* $OpenBSD: bootp.c,v 1.14 2014/06/11 16:45:15 pelikan Exp $ */ /* * BOOTP Protocol support. @@ -53,6 +53,7 @@ bootp(struct packet *packet) struct sockaddr_in to; struct in_addr from; struct tree_cache *options[256]; + struct shared_network *s; struct subnet *subnet = NULL; struct lease *lease; struct iaddr ip_address; @@ -73,15 +74,15 @@ bootp(struct packet *packet) hp = find_hosts_by_haddr(packet->raw->htype, packet->raw->chaddr, packet->raw->hlen); - lease = find_lease(packet, packet->shared_network, 0); + s = packet->shared_network; + lease = find_lease(packet, s, 0); /* * Find an IP address in the host_decl that matches the specified * network. */ if (hp) - subnet = find_host_for_network(&hp, &ip_address, - packet->shared_network); + subnet = find_host_for_network(&hp, &ip_address, s); if (!subnet) { /* @@ -116,8 +117,7 @@ bootp(struct packet *packet) * If we've been told not to boot unknown clients, and we didn't * find any host record for this client, ignore it. */ - if (!host && - !(packet->shared_network->group->boot_unknown_clients)) { + if (!host && !(s->group->boot_unknown_clients)) { note("Ignoring unknown BOOTP client %s via %s", print_hw_addr(packet->raw->htype, packet->raw->hlen, packet->raw->chaddr), @@ -131,8 +131,7 @@ bootp(struct packet *packet) * If we've been told not to boot with bootp on this network, * ignore it. */ - if (!host && - !(packet->shared_network->group->allow_bootp)) { + if (!host && !(s->group->allow_bootp)) { note("Ignoring BOOTP request from client %s via %s", print_hw_addr(packet->raw->htype, packet->raw->hlen, packet->raw->chaddr), @@ -147,7 +146,7 @@ bootp(struct packet *packet) * dynamic bootp addresses on the network it came in on, drop * it on the floor. */ - if (!(packet->shared_network->group->dynamic_bootp)) { + if (!(s->group->dynamic_bootp)) { lose: note("No applicable record for BOOTP host %s via %s", print_hw_addr(packet->raw->htype, @@ -191,7 +190,7 @@ lose: * If there are dynamic bootp addresses that might be * available, try to snag one. */ - for (lease = packet->shared_network->last_lease; + for (lease = s->last_lease; lease && lease->ends <= cur_time; lease = lease->prev) { if ((lease->flags & DYNAMIC_BOOTP_OK)) { diff --git a/usr.sbin/dhcpd/memory.c b/usr.sbin/dhcpd/memory.c index f89943dd12d..c9bce640202 100644 --- a/usr.sbin/dhcpd/memory.c +++ b/usr.sbin/dhcpd/memory.c @@ -1,4 +1,4 @@ -/* $OpenBSD: memory.c,v 1.21 2013/10/18 13:44:35 krw Exp $ */ +/* $OpenBSD: memory.c,v 1.22 2014/06/11 16:45:15 pelikan Exp $ */ /* * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium. @@ -423,6 +423,27 @@ enter_lease(struct lease *lease) } } +static inline int +hwaddrcmp(struct hardware *a, struct hardware *b) +{ + return ((a->htype != b->htype) || (a->hlen != b->hlen) || + memcmp(a->haddr, b->haddr, b->hlen)); +} + +static inline int +uidcmp(struct lease *a, struct lease *b) +{ + return (a->uid_len != b->uid_len || memcmp(a->uid, b->uid, b->uid_len)); +} + +static inline int +uid_or_hwaddr_cmp(struct lease *a, struct lease *b) +{ + if (a->uid && b->uid) + return uidcmp(a, b); + return hwaddrcmp(&a->hardware_addr, &b->hardware_addr); +} + /* * Replace the data in an existing lease with the data in a new lease; * adjust hash tables to suit, and insertion sort the lease into the @@ -452,17 +473,7 @@ supersede_lease(struct lease *comp, struct lease *lease, int commit) * requested *after* a DHCP lease has been assigned. */ if (!(lease->flags & ABANDONED_LEASE) && - comp->ends > cur_time && - (((comp->uid && lease->uid) && - (comp->uid_len != lease->uid_len || - memcmp (comp->uid, lease->uid, comp->uid_len))) || - (!comp->uid && - ((comp->hardware_addr.htype != - lease->hardware_addr.htype) || - (comp->hardware_addr.hlen != - lease->hardware_addr.hlen) || - memcmp(comp->hardware_addr.haddr, lease->hardware_addr.haddr, - comp->hardware_addr.hlen))))) { + comp->ends > cur_time && uid_or_hwaddr_cmp(comp, lease)) { warning("Lease conflict at %s", piaddr(comp->ip_addr)); return 0; } else { @@ -481,12 +492,7 @@ supersede_lease(struct lease *comp, struct lease *lease, int commit) enter_uid = 1; if (comp->hardware_addr.htype && - ((comp->hardware_addr.hlen != - lease->hardware_addr.hlen) || - (comp->hardware_addr.htype != - lease->hardware_addr.htype) || - memcmp(comp->hardware_addr.haddr, lease->hardware_addr.haddr, - comp->hardware_addr.hlen))) { + hwaddrcmp(&comp->hardware_addr, &lease->hardware_addr)) { hw_hash_delete(comp); enter_hwaddr = 1; do_pftable = 1; |