diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2004-04-16 04:30:10 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2004-04-16 04:30:10 +0000 |
commit | 27838d159af1013338d85a71c521ecc43a8d0e68 (patch) | |
tree | 35d4d04bf5683cec8f43ed8867f90ef48a64c961 | |
parent | bb823301160915b076bd09ee468d5e675c858a45 (diff) |
a bunch of knf, ok henning
-rw-r--r-- | usr.sbin/dhcpd/dhcp.c | 1417 | ||||
-rw-r--r-- | usr.sbin/dhcpd/dhcpd.c | 4 | ||||
-rw-r--r-- | usr.sbin/dhcpd/dispatch.c | 453 | ||||
-rw-r--r-- | usr.sbin/dhcpd/inet.c | 7 | ||||
-rw-r--r-- | usr.sbin/dhcpd/memory.c | 941 | ||||
-rw-r--r-- | usr.sbin/dhcpd/print.c | 22 | ||||
-rw-r--r-- | usr.sbin/dhcpd/tree.c | 5 |
7 files changed, 1376 insertions, 1473 deletions
diff --git a/usr.sbin/dhcpd/dhcp.c b/usr.sbin/dhcpd/dhcp.c index c65396df161..b025227a4f0 100644 --- a/usr.sbin/dhcpd/dhcp.c +++ b/usr.sbin/dhcpd/dhcp.c @@ -44,84 +44,84 @@ int outstanding_pings; -static char dhcp_message [256]; +static char dhcp_message[256]; -void dhcp (packet) - struct packet *packet; +void +dhcp(struct packet *packet) { - if (!locate_network (packet) && packet -> packet_type != DHCPREQUEST) + if (!locate_network(packet) && packet->packet_type != DHCPREQUEST) return; - switch (packet -> packet_type) { - case DHCPDISCOVER: - dhcpdiscover (packet); + switch (packet->packet_type) { + case DHCPDISCOVER: + dhcpdiscover(packet); break; - case DHCPREQUEST: - dhcprequest (packet); + case DHCPREQUEST: + dhcprequest(packet); break; - case DHCPRELEASE: - dhcprelease (packet); + case DHCPRELEASE: + dhcprelease(packet); break; - case DHCPDECLINE: - dhcpdecline (packet); + case DHCPDECLINE: + dhcpdecline(packet); break; - case DHCPINFORM: - dhcpinform (packet); + case DHCPINFORM: + dhcpinform(packet); break; - default: + default: break; } } -void dhcpdiscover (packet) +void dhcpdiscover(packet) struct packet *packet; { - struct lease *lease = find_lease (packet, packet -> shared_network, 0); + struct lease *lease = find_lease(packet, packet->shared_network, 0); struct host_decl *hp; - note ("DHCPDISCOVER from %s via %s", - print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr), - packet -> raw -> giaddr.s_addr - ? inet_ntoa (packet -> raw -> giaddr) - : packet -> interface -> name); + note("DHCPDISCOVER from %s via %s", + print_hw_addr(packet->raw->htype, + packet->raw->hlen, + packet->raw->chaddr), + packet->raw->giaddr.s_addr + ? inet_ntoa(packet->raw->giaddr) + : packet->interface->name); /* Sourceless packets don't make sense here. */ - if (!packet -> shared_network) { - note ("Packet from unknown subnet: %s", - inet_ntoa (packet -> raw -> giaddr)); + if (!packet->shared_network) { + note("Packet from unknown subnet: %s", + inet_ntoa(packet->raw->giaddr)); return; } /* If we didn't find a lease, try to allocate one... */ if (!lease) { - lease = packet -> shared_network -> last_lease; + lease = packet->shared_network->last_lease; /* If there are no leases in that subnet that have expired, we have nothing to offer this client. */ - if (!lease || lease -> ends > cur_time) { - note ("no free leases on subnet %s", - packet -> shared_network -> name); + if (!lease || lease->ends > cur_time) { + note("no free leases on subnet %s", + packet->shared_network->name); return; } /* If we find an abandoned lease, take it, but print a warning message, so that if it continues to lose, the administrator will eventually investigate. */ - if ((lease -> flags & ABANDONED_LEASE)) { + if ((lease->flags & ABANDONED_LEASE)) { struct lease *lp; /* See if we can find an unabandoned lease first. */ - for (lp = lease; lp; lp = lp -> prev) { - if (lp -> ends > cur_time) + for (lp = lease; lp; lp = lp->prev) { + if (lp->ends > cur_time) break; - if (!(lp -> flags & ABANDONED_LEASE)) { + if (!(lp->flags & ABANDONED_LEASE)) { lease = lp; break; } @@ -129,10 +129,10 @@ void dhcpdiscover (packet) /* If we can't find an unabandoned lease, reclaim the abandoned lease. */ - if ((lease -> flags & ABANDONED_LEASE)) { - warn ("Reclaiming abandoned IP address %s.", - piaddr (lease -> ip_addr)); - lease -> flags &= ~ABANDONED_LEASE; + if ((lease->flags & ABANDONED_LEASE)) { + warn("Reclaiming abandoned IP address %s.", + piaddr(lease->ip_addr)); + lease->flags &= ~ABANDONED_LEASE; } } @@ -141,48 +141,48 @@ void dhcpdiscover (packet) has no fixed IP address. If there is one, hang it off the lease so that its option definitions can be used. */ - if (((packet -> options [DHO_DHCP_CLIENT_IDENTIFIER].len + if (((packet->options[DHO_DHCP_CLIENT_IDENTIFIER].len != 0) && ((hp = find_hosts_by_uid - (packet -> options [DHO_DHCP_CLIENT_IDENTIFIER].data, - packet -> options [DHO_DHCP_CLIENT_IDENTIFIER].len)) + (packet->options[DHO_DHCP_CLIENT_IDENTIFIER].data, + packet->options[DHO_DHCP_CLIENT_IDENTIFIER].len)) != (struct host_decl *)0)) || - ((hp = find_hosts_by_haddr (packet -> raw -> htype, - packet -> raw -> chaddr, - packet -> raw -> hlen)) + ((hp = find_hosts_by_haddr(packet->raw->htype, + packet->raw->chaddr, + packet->raw->hlen)) != (struct host_decl *)0)) { - for (; hp; hp = hp -> n_ipaddr) { - if (!hp -> fixed_addr) { - lease -> host = hp; + for (; hp; hp = hp->n_ipaddr) { + if (!hp->fixed_addr) { + lease->host = hp; break; } } } else { - lease -> host = (struct host_decl *)0; + lease->host = (struct host_decl *)0; } } /* If this subnet won't boot unknown clients, ignore the request. */ - if (!lease -> host && - !lease -> subnet -> group -> boot_unknown_clients) { - note ("Ignoring unknown client %s", - print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr)); - } else if (lease -> host && - !lease -> host -> group -> allow_booting) { - note ("Declining to boot client %s", - lease -> host -> name - ? lease -> host -> name - : print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr)); + if (!lease->host && + !lease->subnet->group->boot_unknown_clients) { + note("Ignoring unknown client %s", + print_hw_addr(packet->raw->htype, + packet->raw->hlen, + packet->raw->chaddr)); + } else if (lease->host && + !lease->host->group->allow_booting) { + note("Declining to boot client %s", + lease->host->name + ? lease->host->name + : print_hw_addr(packet->raw->htype, + packet->raw->hlen, + packet->raw->chaddr)); } else - ack_lease (packet, lease, DHCPOFFER, cur_time + 120); + ack_lease(packet, lease, DHCPOFFER, cur_time + 120); } -void dhcprequest (packet) +void dhcprequest(packet) struct packet *packet; { struct lease *lease; @@ -190,33 +190,33 @@ void dhcprequest (packet) struct subnet *subnet; int ours = 0; - if (packet -> options [DHO_DHCP_REQUESTED_ADDRESS].len) { + if (packet->options[DHO_DHCP_REQUESTED_ADDRESS].len) { cip.len = 4; - memcpy (cip.iabuf, - packet -> options [DHO_DHCP_REQUESTED_ADDRESS].data, + memcpy(cip.iabuf, + packet->options[DHO_DHCP_REQUESTED_ADDRESS].data, 4); } else { cip.len = 4; - memcpy (cip.iabuf, &packet -> raw -> ciaddr.s_addr, 4); + memcpy(cip.iabuf, &packet->raw->ciaddr.s_addr, 4); } - subnet = find_subnet (cip); + subnet = find_subnet(cip); /* Find the lease that matches the address requested by the client. */ if (subnet) - lease = find_lease (packet, subnet -> shared_network, &ours); + lease = find_lease(packet, subnet->shared_network, &ours); else - lease = (struct lease *)0; + lease = NULL; - note ("DHCPREQUEST for %s from %s via %s", - piaddr (cip), - print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr), - packet -> raw -> giaddr.s_addr - ? inet_ntoa (packet -> raw -> giaddr) - : packet -> interface -> name); + note("DHCPREQUEST for %s from %s via %s", + piaddr(cip), + print_hw_addr(packet->raw->htype, + packet->raw->hlen, + packet->raw->chaddr), + packet->raw->giaddr.s_addr + ? inet_ntoa(packet->raw->giaddr) + : packet->interface->name); /* If a client on a given network REQUESTs a lease on an address on a different network, NAK it. If the Requested @@ -260,20 +260,20 @@ void dhcprequest (packet) doesn't really allow us to NAK a DHCPREQUEST from a RENEWING client, so we can punt on this issue. */ - if (!packet -> shared_network || - (packet -> raw -> ciaddr.s_addr && - packet -> raw -> giaddr.s_addr) || - (packet -> options [DHO_DHCP_REQUESTED_ADDRESS].len && - !packet -> raw -> ciaddr.s_addr)) { + if (!packet->shared_network || + (packet->raw->ciaddr.s_addr && + packet->raw->giaddr.s_addr) || + (packet->options[DHO_DHCP_REQUESTED_ADDRESS].len && + !packet->raw->ciaddr.s_addr)) { /* If we don't know where it came from but we do know where it claims to have come from, it didn't come from there. Fry it. */ - if (!packet -> shared_network) { + if (!packet->shared_network) { if (subnet && - subnet -> shared_network -> group -> authoritative) + subnet->shared_network->group->authoritative) { - nak_lease (packet, &cip); + nak_lease(packet, &cip); return; } /* Otherwise, ignore it. */ @@ -282,10 +282,10 @@ void dhcprequest (packet) /* If we do know where it came from and it asked for an address that is not on that shared network, nak it. */ - subnet = find_grouped_subnet (packet -> shared_network, cip); + subnet = find_grouped_subnet(packet->shared_network, cip); if (!subnet) { - if (packet -> shared_network -> group -> authoritative) - nak_lease (packet, &cip); + if (packet->shared_network->group->authoritative) + nak_lease(packet, &cip); return; } } @@ -293,56 +293,56 @@ void dhcprequest (packet) /* If we found a lease for the client but it's not the one the client asked for, don't send it - some other server probably made the cut. */ - if (lease && !addr_eq (lease -> ip_addr, cip)) { + if (lease && !addr_eq(lease->ip_addr, cip)) { /* If we found the address the client asked for, but it wasn't what got picked, the lease belongs to us, so we should NAK it. */ if (ours) - nak_lease (packet, &cip); + nak_lease(packet, &cip); return; } /* If the address the client asked for is ours, but it wasn't available for the client, NAK it. */ if (!lease && ours) { - nak_lease (packet, &cip); + nak_lease(packet, &cip); return; } /* If we're not allowed to serve this client anymore, don't. */ if (lease && - !lease -> host && - !lease -> subnet -> group -> boot_unknown_clients) { - note ("Ignoring unknown client %s", - print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr)); + !lease->host && + !lease->subnet->group->boot_unknown_clients) { + note("Ignoring unknown client %s", + print_hw_addr(packet->raw->htype, + packet->raw->hlen, + packet->raw->chaddr)); return; - } else if (lease && lease -> host && - !lease -> host -> group -> allow_booting) { - note ("Declining to renew client %s", - lease -> host -> name - ? lease -> host -> name - : print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr)); + } else if (lease && lease->host && + !lease->host->group->allow_booting) { + note("Declining to renew client %s", + lease->host->name + ? lease->host->name + : print_hw_addr(packet->raw->htype, + packet->raw->hlen, + packet->raw->chaddr)); return; } /* If we own the lease that the client is asking for, and it's already been assigned to the client, ack it. */ if (lease && - ((lease -> uid_len && lease -> uid_len == - packet -> options [DHO_DHCP_CLIENT_IDENTIFIER].len && - !memcmp (packet -> options - [DHO_DHCP_CLIENT_IDENTIFIER].data, - lease -> uid, lease -> uid_len)) || - (lease -> hardware_addr.hlen == packet -> raw -> hlen && - lease -> hardware_addr.htype == packet -> raw -> htype && - !memcmp (lease -> hardware_addr.haddr, - packet -> raw -> chaddr, - packet -> raw -> hlen)))) { - ack_lease (packet, lease, DHCPACK, 0); + ((lease->uid_len && lease->uid_len == + packet->options[DHO_DHCP_CLIENT_IDENTIFIER].len && + !memcmp(packet->options + [DHO_DHCP_CLIENT_IDENTIFIER].data, + lease->uid, lease->uid_len)) || + (lease->hardware_addr.hlen == packet->raw->hlen && + lease->hardware_addr.htype == packet->raw->htype && + !memcmp(lease->hardware_addr.haddr, + packet->raw->chaddr, + packet->raw->hlen)))) { + ack_lease(packet, lease, DHCPACK, 0); return; } @@ -354,10 +354,10 @@ void dhcprequest (packet) The thing we probably should not do is to remain silent. For now, we'll just assign the lease to the client anyway. */ if (lease) - ack_lease (packet, lease, DHCPACK, 0); + ack_lease(packet, lease, DHCPACK, 0); } -void dhcprelease (packet) +void dhcprelease(packet) struct packet *packet; { struct lease *lease; @@ -367,23 +367,23 @@ void dhcprelease (packet) /* DHCPRELEASE must not specify address in requested-address option, but old protocol specs weren't explicit about this, so let it go. */ - if (packet -> options [DHO_DHCP_REQUESTED_ADDRESS].len) { - note ("DHCPRELEASE from %s specified requested-address.", - print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr)); + if (packet->options[DHO_DHCP_REQUESTED_ADDRESS].len) { + note("DHCPRELEASE from %s specified requested-address.", + print_hw_addr(packet->raw->htype, + packet->raw->hlen, + packet->raw->chaddr)); } i = DHO_DHCP_CLIENT_IDENTIFIER; - if (packet -> options [i].len) { - lease = find_lease_by_uid (packet -> options [i].data, - packet -> options [i].len); + if (packet->options[i].len) { + lease = find_lease_by_uid(packet->options[i].data, + packet->options[i].len); /* See if we can find a lease that matches the IP address the client is claiming. */ - for (; lease; lease = lease -> n_uid) { - if (!memcmp (&packet -> raw -> ciaddr, - lease -> ip_addr.iabuf, 4)) { + for (; lease; lease = lease->n_uid) { + if (!memcmp(&packet->raw->ciaddr, + lease->ip_addr.iabuf, 4)) { break; } } @@ -392,105 +392,105 @@ void dhcprelease (packet) but the spec on this has changed historically, so try the IP address in ciaddr if the client-identifier fails. */ cip.len = 4; - memcpy (cip.iabuf, &packet -> raw -> ciaddr, 4); - lease = find_lease_by_ip_addr (cip); + memcpy(cip.iabuf, &packet->raw->ciaddr, 4); + lease = find_lease_by_ip_addr(cip); } - note ("DHCPRELEASE of %s from %s via %s (%sfound)", - inet_ntoa (packet -> raw -> ciaddr), - print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr), - packet -> raw -> giaddr.s_addr - ? inet_ntoa (packet -> raw -> giaddr) - : packet -> interface -> name, + note("DHCPRELEASE of %s from %s via %s (%sfound)", + inet_ntoa(packet->raw->ciaddr), + print_hw_addr(packet->raw->htype, + packet->raw->hlen, + packet->raw->chaddr), + packet->raw->giaddr.s_addr + ? inet_ntoa(packet->raw->giaddr) + : packet->interface->name, lease ? "" : "not "); /* If we found a lease, release it. */ - if (lease && lease -> ends > cur_time) { + if (lease && lease->ends > cur_time) { /* first, we ping this lease to see if it's still * there. if it is, we don't release it. * this avoids the problem of spoofed releases * being used to liberate addresses from the * server. */ - if (! lease->releasing) { - note ("DHCPRELEASE of %s from %s via %s (found)", - inet_ntoa (packet -> raw -> ciaddr), - print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr), - packet -> raw -> giaddr.s_addr - ? inet_ntoa (packet -> raw -> giaddr) - : packet -> interface -> name); + if (!lease->releasing) { + note("DHCPRELEASE of %s from %s via %s (found)", + inet_ntoa(packet->raw->ciaddr), + print_hw_addr(packet->raw->htype, + packet->raw->hlen, + packet->raw->chaddr), + packet->raw->giaddr.s_addr + ? inet_ntoa(packet->raw->giaddr) + : packet->interface->name); lease->releasing = 1; - add_timeout (cur_time + 1, lease_ping_timeout, lease); - icmp_echorequest (&(lease -> ip_addr)); + add_timeout(cur_time + 1, lease_ping_timeout, lease); + icmp_echorequest(&(lease->ip_addr)); ++outstanding_pings; } else { - note ("DHCPRELEASE of %s from %s via %s ignored (release already pending)", - inet_ntoa (packet -> raw -> ciaddr), - print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr), - packet -> raw -> giaddr.s_addr - ? inet_ntoa (packet -> raw -> giaddr) - : packet -> interface -> name); + note("DHCPRELEASE of %s from %s via %s ignored (release already pending)", + inet_ntoa(packet->raw->ciaddr), + print_hw_addr(packet->raw->htype, + packet->raw->hlen, + packet->raw->chaddr), + packet->raw->giaddr.s_addr + ? inet_ntoa(packet->raw->giaddr) + : packet->interface->name); } } else { - note ("DHCPRELEASE of %s from %s via %s for nonexistent lease", - inet_ntoa (packet -> raw -> ciaddr), - print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr), - packet -> raw -> giaddr.s_addr - ? inet_ntoa (packet -> raw -> giaddr) - : packet -> interface -> name); + note("DHCPRELEASE of %s from %s via %s for nonexistent lease", + inet_ntoa(packet->raw->ciaddr), + print_hw_addr(packet->raw->htype, + packet->raw->hlen, + packet->raw->chaddr), + packet->raw->giaddr.s_addr + ? inet_ntoa(packet->raw->giaddr) + : packet->interface->name); } } -void dhcpdecline (packet) +void dhcpdecline(packet) struct packet *packet; { struct lease *lease; struct iaddr cip; /* DHCPDECLINE must specify address. */ - if (!packet -> options [DHO_DHCP_REQUESTED_ADDRESS].len) { + if (!packet->options[DHO_DHCP_REQUESTED_ADDRESS].len) { return; } cip.len = 4; - memcpy (cip.iabuf, - packet -> options [DHO_DHCP_REQUESTED_ADDRESS].data, 4); - lease = find_lease_by_ip_addr (cip); - - note ("DHCPDECLINE on %s from %s via %s", - piaddr (cip), - print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr), - packet -> raw -> giaddr.s_addr - ? inet_ntoa (packet -> raw -> giaddr) - : packet -> interface -> name); + memcpy(cip.iabuf, + packet->options[DHO_DHCP_REQUESTED_ADDRESS].data, 4); + lease = find_lease_by_ip_addr(cip); + + note("DHCPDECLINE on %s from %s via %s", + piaddr(cip), + print_hw_addr(packet->raw->htype, + packet->raw->hlen, + packet->raw->chaddr), + packet->raw->giaddr.s_addr + ? inet_ntoa(packet->raw->giaddr) + : packet->interface->name); /* If we found a lease, mark it as unusable and complain. */ if (lease) { - abandon_lease (lease, "declined."); + abandon_lease(lease, "declined."); } } -void dhcpinform (packet) +void dhcpinform(packet) struct packet *packet; { - note ("DHCPINFORM from %s", - inet_ntoa (packet -> raw -> ciaddr)); + note("DHCPINFORM from %s", + inet_ntoa(packet->raw->ciaddr)); } -void nak_lease (packet, cip) +void nak_lease(packet, cip) struct packet *packet; struct iaddr *cip; { @@ -503,86 +503,80 @@ void nak_lease (packet, cip) struct hardware hto; int i; - struct tree_cache *options [256]; + struct tree_cache *options[256]; struct tree_cache dhcpnak_tree; struct tree_cache dhcpmsg_tree; - memset (options, 0, sizeof options); - memset (&outgoing, 0, sizeof outgoing); - memset (&raw, 0, sizeof raw); + memset(options, 0, sizeof options); + memset(&outgoing, 0, sizeof outgoing); + memset(&raw, 0, sizeof raw); outgoing.raw = &raw; /* Set DHCP_MESSAGE_TYPE to DHCPNAK */ - options [DHO_DHCP_MESSAGE_TYPE] = &dhcpnak_tree; - options [DHO_DHCP_MESSAGE_TYPE] -> value = &nak; - options [DHO_DHCP_MESSAGE_TYPE] -> len = sizeof nak; - options [DHO_DHCP_MESSAGE_TYPE] -> buf_size = sizeof nak; - options [DHO_DHCP_MESSAGE_TYPE] -> timeout = 0xFFFFFFFF; - options [DHO_DHCP_MESSAGE_TYPE] -> tree = (struct tree *)0; + options[DHO_DHCP_MESSAGE_TYPE] = &dhcpnak_tree; + options[DHO_DHCP_MESSAGE_TYPE]->value = &nak; + options[DHO_DHCP_MESSAGE_TYPE]->len = sizeof nak; + options[DHO_DHCP_MESSAGE_TYPE]->buf_size = sizeof nak; + options[DHO_DHCP_MESSAGE_TYPE]->timeout = 0xFFFFFFFF; + options[DHO_DHCP_MESSAGE_TYPE]->tree = (struct tree *)0; /* Set DHCP_MESSAGE to whatever the message is */ - options [DHO_DHCP_MESSAGE] = &dhcpmsg_tree; - options [DHO_DHCP_MESSAGE] -> value = (unsigned char *)dhcp_message; - options [DHO_DHCP_MESSAGE] -> len = - options [DHO_DHCP_MESSAGE] -> buf_size = strlen (dhcp_message); - options [DHO_DHCP_MESSAGE] -> timeout = 0xFFFFFFFF; - options [DHO_DHCP_MESSAGE] -> tree = (struct tree *)0; + options[DHO_DHCP_MESSAGE] = &dhcpmsg_tree; + options[DHO_DHCP_MESSAGE]->value = (unsigned char *)dhcp_message; + options[DHO_DHCP_MESSAGE]->len = + options[DHO_DHCP_MESSAGE]->buf_size = strlen(dhcp_message); + options[DHO_DHCP_MESSAGE]->timeout = 0xFFFFFFFF; + options[DHO_DHCP_MESSAGE]->tree = (struct tree *)0; /* Do not use the client's requested parameter list. */ i = DHO_DHCP_PARAMETER_REQUEST_LIST; - if (packet -> options [i].data) { - packet -> options [i].len = 0; - dfree (packet -> options [i].data, "nak_lease"); - packet -> options [i].data = (unsigned char *)0; + if (packet->options[i].data) { + packet->options[i].len = 0; + dfree(packet->options[i].data, "nak_lease"); + packet->options[i].data = (unsigned char *)0; } /* Set up the option buffer... */ outgoing.packet_length = - cons_options (packet, outgoing.raw, 0, options, 0, 0, 0, + cons_options(packet, outgoing.raw, 0, options, 0, 0, 0, (u_int8_t *)0, 0); -/* memset (&raw.ciaddr, 0, sizeof raw.ciaddr);*/ - raw.siaddr = packet -> interface -> primary_address; - raw.giaddr = packet -> raw -> giaddr; - memcpy (raw.chaddr, packet -> raw -> chaddr, sizeof raw.chaddr); - raw.hlen = packet -> raw -> hlen; - raw.htype = packet -> raw -> htype; - - raw.xid = packet -> raw -> xid; - raw.secs = packet -> raw -> secs; - raw.flags = packet -> raw -> flags | htons (BOOTP_BROADCAST); - raw.hops = packet -> raw -> hops; +/* memset(&raw.ciaddr, 0, sizeof raw.ciaddr);*/ + raw.siaddr = packet->interface->primary_address; + raw.giaddr = packet->raw->giaddr; + memcpy(raw.chaddr, packet->raw->chaddr, sizeof raw.chaddr); + raw.hlen = packet->raw->hlen; + raw.htype = packet->raw->htype; + + raw.xid = packet->raw->xid; + raw.secs = packet->raw->secs; + raw.flags = packet->raw->flags | htons(BOOTP_BROADCAST); + raw.hops = packet->raw->hops; raw.op = BOOTREPLY; /* Report what we're sending... */ - note ("DHCPNAK on %s to %s via %s", - piaddr (*cip), - print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr), - packet -> raw -> giaddr.s_addr - ? inet_ntoa (packet -> raw -> giaddr) - : packet -> interface -> name); - - + note("DHCPNAK on %s to %s via %s", piaddr(*cip), + print_hw_addr(packet->raw->htype, packet->raw->hlen, + packet->raw->chaddr), packet->raw->giaddr.s_addr ? + inet_ntoa(packet->raw->giaddr) : packet->interface->name); #ifdef DEBUG_PACKET dump_packet (packet); - dump_raw ((unsigned char *)packet -> raw, packet -> packet_length); + dump_raw ((unsigned char *)packet->raw, packet->packet_length); dump_packet (&outgoing); dump_raw ((unsigned char *)&raw, outgoing.packet_length); #endif - hto.htype = packet -> raw -> htype; - hto.hlen = packet -> raw -> hlen; - memcpy (hto.haddr, packet -> raw -> chaddr, hto.hlen); + hto.htype = packet->raw->htype; + hto.hlen = packet->raw->hlen; + memcpy(hto.haddr, packet->raw->chaddr, hto.hlen); /* Set up the common stuff... */ memset (&to, 0, sizeof to); to.sin_family = AF_INET; to.sin_len = sizeof to; - from = packet -> interface -> primary_address; + from = packet->interface->primary_address; /* Make sure that the packet is at least as big as a BOOTP packet. */ if (outgoing.packet_length < BOOTP_MIN_LEN) @@ -600,7 +594,7 @@ void nak_lease (packet, cip) outgoing.packet_length, from, &to, &hto); if (result == -1) - warn ("send_fallback: %m"); + warn("send_fallback: %m"); return; } } else { @@ -609,7 +603,7 @@ void nak_lease (packet, cip) } errno = 0; - result = send_packet (packet -> interface, + result = send_packet (packet->interface, packet, &raw, outgoing.packet_length, from, &to, (struct hardware *)0); } @@ -632,29 +626,29 @@ void ack_lease (packet, lease, offer, when) int i; /* If we're already acking this lease, don't do it again. */ - if (lease -> state) { - note ("already acking lease %s", piaddr (lease -> ip_addr)); + if (lease->state) { + note ("already acking lease %s", piaddr (lease->ip_addr)); return; } - if (packet -> options [DHO_DHCP_CLASS_IDENTIFIER].len) { + if (packet->options[DHO_DHCP_CLASS_IDENTIFIER].len) { vendor_class = find_class (0, packet -> - options [DHO_DHCP_CLASS_IDENTIFIER].data, + options[DHO_DHCP_CLASS_IDENTIFIER].data, packet -> - options [DHO_DHCP_CLASS_IDENTIFIER].len); + options[DHO_DHCP_CLASS_IDENTIFIER].len); } else { vendor_class = (struct class *)0; } - if (packet -> options [DHO_DHCP_USER_CLASS_ID].len) { + if (packet->options[DHO_DHCP_USER_CLASS_ID].len) { user_class = find_class (1, packet -> - options [DHO_DHCP_USER_CLASS_ID].data, + options[DHO_DHCP_USER_CLASS_ID].data, packet -> - options [DHO_DHCP_USER_CLASS_ID].len); + options[DHO_DHCP_USER_CLASS_ID].len); } else { user_class = (struct class *)0; } @@ -664,13 +658,13 @@ void ack_lease (packet, lease, offer, when) * vendor class or user class (if they exist) deny booting, * then bug out. */ - if (!lease -> host) { - if (vendor_class && !vendor_class -> group -> allow_booting) { + if (!lease->host) { + if (vendor_class && !vendor_class->group->allow_booting) { debug ("Booting denied by vendor class"); return; } - if (user_class && !user_class -> group -> allow_booting) { + if (user_class && !user_class->group->allow_booting) { debug ("Booting denied by user class"); return; } @@ -679,72 +673,72 @@ void ack_lease (packet, lease, offer, when) /* Allocate a lease state structure... */ state = new_lease_state ("ack_lease"); if (!state) - error ("unable to allocate lease state!"); + error("unable to allocate lease state!"); memset (state, 0, sizeof *state); - state -> got_requested_address = packet -> got_requested_address; - state -> shared_network = packet -> interface -> shared_network; + state->got_requested_address = packet->got_requested_address; + state->shared_network = packet->interface->shared_network; /* Remember if we got a server identifier option. */ - if (packet -> options [DHO_DHCP_SERVER_IDENTIFIER].len) - state -> got_server_identifier = 1; + if (packet->options[DHO_DHCP_SERVER_IDENTIFIER].len) + state->got_server_identifier = 1; /* Replace the old lease hostname with the new one, if it's changed. */ - if (packet -> options [DHO_HOST_NAME].len && - lease -> client_hostname && - (strlen (lease -> client_hostname) == - packet -> options [DHO_HOST_NAME].len) && - !memcmp (lease -> client_hostname, - packet -> options [DHO_HOST_NAME].data, - packet -> options [DHO_HOST_NAME].len)) { - } else if (packet -> options [DHO_HOST_NAME].len) { - if (lease -> client_hostname) - free (lease -> client_hostname); - lease -> client_hostname = - malloc (packet -> options [DHO_HOST_NAME].len + 1); - if (!lease -> client_hostname) - error ("no memory for client hostname.\n"); - memcpy (lease -> client_hostname, - packet -> options [DHO_HOST_NAME].data, - packet -> options [DHO_HOST_NAME].len); - lease -> client_hostname - [packet -> options [DHO_HOST_NAME].len] = 0; - } else if (lease -> client_hostname) { - free (lease -> client_hostname); - lease -> client_hostname = 0; + if (packet->options[DHO_HOST_NAME].len && + lease->client_hostname && + (strlen (lease->client_hostname) == + packet->options[DHO_HOST_NAME].len) && + !memcmp (lease->client_hostname, + packet->options[DHO_HOST_NAME].data, + packet->options[DHO_HOST_NAME].len)) { + } else if (packet->options[DHO_HOST_NAME].len) { + if (lease->client_hostname) + free (lease->client_hostname); + lease->client_hostname = + malloc (packet->options[DHO_HOST_NAME].len + 1); + if (!lease->client_hostname) + error("no memory for client hostname.\n"); + memcpy(lease->client_hostname, + packet->options[DHO_HOST_NAME].data, + packet->options[DHO_HOST_NAME].len); + lease->client_hostname + [packet->options[DHO_HOST_NAME].len] = 0; + } else if (lease->client_hostname) { + free (lease->client_hostname); + lease->client_hostname = 0; } /* Choose a filename; first from the host_decl, if any, then from the user class, then from the vendor class. */ - if (lease -> host && lease -> host -> group -> filename) - strlcpy (state -> filename, lease -> host -> group -> filename, - sizeof state -> filename); - else if (user_class && user_class -> group -> filename) - strlcpy (state -> filename, user_class -> group -> filename, - sizeof state -> filename); - else if (vendor_class && vendor_class -> group -> filename) - strlcpy (state -> filename, vendor_class -> group -> filename, - sizeof state -> filename); - else if (packet -> raw -> file [0]) - strlcpy (state -> filename, packet -> raw -> file, - sizeof state -> filename); - else if (lease -> subnet -> group -> filename) - strlcpy (state -> filename, - lease -> subnet -> group -> filename, - sizeof state -> filename); + if (lease->host && lease->host->group->filename) + strlcpy(state->filename, lease->host->group->filename, + sizeof state->filename); + else if (user_class && user_class->group->filename) + strlcpy(state->filename, user_class->group->filename, + sizeof state->filename); + else if (vendor_class && vendor_class->group->filename) + strlcpy(state->filename, vendor_class->group->filename, + sizeof state->filename); + else if (packet->raw->file[0]) + strlcpy(state->filename, packet->raw->file, + sizeof state->filename); + else if (lease->subnet->group->filename) + strlcpy(state->filename, + lease->subnet->group->filename, + sizeof state->filename); else - strlcpy (state -> filename, "", sizeof state -> filename); + strlcpy(state->filename, "", sizeof state->filename); /* Choose a server name as above. */ - if (lease -> host && lease -> host -> group -> server_name) - state -> server_name = lease -> host -> group -> server_name; - else if (user_class && user_class -> group -> server_name) - state -> server_name = user_class -> group -> server_name; - else if (vendor_class && vendor_class -> group -> server_name) - state -> server_name = vendor_class -> group -> server_name; - else if (lease -> subnet -> group -> server_name) - state -> server_name = - lease -> subnet -> group -> server_name; - else state -> server_name = (char *)0; + if (lease->host && lease->host->group->server_name) + state->server_name = lease->host->group->server_name; + else if (user_class && user_class->group->server_name) + state->server_name = user_class->group->server_name; + else if (vendor_class && vendor_class->group->server_name) + state->server_name = vendor_class->group->server_name; + else if (lease->subnet->group->server_name) + state->server_name = + lease->subnet->group->server_name; + else state->server_name = (char *)0; /* At this point, we have a lease that we can offer the client. Now we construct a lease structure that contains what we want, @@ -754,33 +748,33 @@ void ack_lease (packet, lease, offer, when) /* Use the ip address of the lease that we finally found in the database. */ - lt.ip_addr = lease -> ip_addr; + lt.ip_addr = lease->ip_addr; /* Start now. */ lt.starts = cur_time; /* Figure out maximum lease time. */ - if (lease -> host && - lease -> host -> group -> max_lease_time) - max_lease_time = lease -> host -> group -> max_lease_time; + if (lease->host && + lease->host->group->max_lease_time) + max_lease_time = lease->host->group->max_lease_time; else - max_lease_time = lease -> subnet -> group -> max_lease_time; + max_lease_time = lease->subnet->group->max_lease_time; /* Figure out default lease time. */ - if (lease -> host - && lease -> host -> group -> default_lease_time) + if (lease->host + && lease->host->group->default_lease_time) default_lease_time = - lease -> host -> group -> default_lease_time; + lease->host->group->default_lease_time; else default_lease_time = - lease -> subnet -> group -> default_lease_time; + lease->subnet->group->default_lease_time; /* Figure out how long a lease to assign. If this is a dynamic BOOTP lease, its duration must be infinite. */ if (offer) { - if (packet -> options [DHO_DHCP_LEASE_TIME].len == 4) { + if (packet->options[DHO_DHCP_LEASE_TIME].len == 4) { lease_time = getULong - (packet -> options [DHO_DHCP_LEASE_TIME].data); + (packet->options[DHO_DHCP_LEASE_TIME].data); /* Don't let the client ask for a longer lease than is supported for this subnet or host. */ @@ -789,68 +783,68 @@ void ack_lease (packet, lease, offer, when) } else lease_time = default_lease_time; - state -> offered_expiry = cur_time + lease_time; + state->offered_expiry = cur_time + lease_time; if (when) lt.ends = when; else - lt.ends = state -> offered_expiry; + lt.ends = state->offered_expiry; } else { - if (lease -> host && - lease -> host -> group -> bootp_lease_length) + if (lease->host && + lease->host->group->bootp_lease_length) lt.ends = (cur_time + - lease -> host -> - group -> bootp_lease_length); - else if (lease -> subnet -> group -> bootp_lease_length) + lease->host -> + group->bootp_lease_length); + else if (lease->subnet->group->bootp_lease_length) lt.ends = (cur_time + - lease -> subnet -> - group -> bootp_lease_length); - else if (lease -> host && - lease -> host -> group -> bootp_lease_cutoff) - lt.ends = lease -> host -> group -> bootp_lease_cutoff; + lease->subnet -> + group->bootp_lease_length); + else if (lease->host && + lease->host->group->bootp_lease_cutoff) + lt.ends = lease->host->group->bootp_lease_cutoff; else - lt.ends = (lease -> subnet -> - group -> bootp_lease_cutoff); - state -> offered_expiry = lt.ends; + lt.ends = (lease->subnet -> + group->bootp_lease_cutoff); + state->offered_expiry = lt.ends; lt.flags = BOOTP_LEASE; } /* Record the uid, if given... */ i = DHO_DHCP_CLIENT_IDENTIFIER; - if (packet -> options [i].len) { - if (packet -> options [i].len <= sizeof lt.uid_buf) { - memcpy (lt.uid_buf, packet -> options [i].data, - packet -> options [i].len); + if (packet->options[i].len) { + if (packet->options[i].len <= sizeof lt.uid_buf) { + memcpy(lt.uid_buf, packet->options[i].data, + packet->options[i].len); lt.uid = lt.uid_buf; lt.uid_max = sizeof lt.uid_buf; - lt.uid_len = packet -> options [i].len; + lt.uid_len = packet->options[i].len; } else { - lt.uid_max = lt.uid_len = packet -> options [i].len; + lt.uid_max = lt.uid_len = packet->options[i].len; lt.uid = (unsigned char *)malloc (lt.uid_max); if (!lt.uid) - error ("can't allocate memory for large uid."); - memcpy (lt.uid, - packet -> options [i].data, lt.uid_len); + error("can't allocate memory for large uid."); + memcpy(lt.uid, + packet->options[i].data, lt.uid_len); } } - lt.host = lease -> host; - lt.subnet = lease -> subnet; - lt.shared_network = lease -> shared_network; + lt.host = lease->host; + lt.subnet = lease->subnet; + lt.shared_network = lease->shared_network; /* Don't call supersede_lease on a mocked-up lease. */ - if (lease -> flags & STATIC_LEASE) { + if (lease->flags & STATIC_LEASE) { /* Copy the hardware address into the static lease structure. */ - lease -> hardware_addr.hlen = packet -> raw -> hlen; - lease -> hardware_addr.htype = packet -> raw -> htype; - memcpy (lease -> hardware_addr.haddr, packet -> raw -> chaddr, - sizeof packet -> raw -> chaddr); /* XXX */ + lease->hardware_addr.hlen = packet->raw->hlen; + lease->hardware_addr.htype = packet->raw->htype; + memcpy(lease->hardware_addr.haddr, packet->raw->chaddr, + sizeof packet->raw->chaddr); /* XXX */ } else { /* Record the hardware address, if given... */ - lt.hardware_addr.hlen = packet -> raw -> hlen; - lt.hardware_addr.htype = packet -> raw -> htype; - memcpy (lt.hardware_addr.haddr, packet -> raw -> chaddr, - sizeof packet -> raw -> chaddr); + lt.hardware_addr.hlen = packet->raw->hlen; + lt.hardware_addr.htype = packet->raw->htype; + memcpy(lt.hardware_addr.haddr, packet->raw->chaddr, + sizeof packet->raw->chaddr); /* Install the new information about this lease in the database. If this is a DHCPACK or a dynamic BOOTREPLY @@ -863,250 +857,250 @@ void ack_lease (packet, lease, offer, when) } /* Remember the interface on which the packet arrived. */ - state -> ip = packet -> interface; + state->ip = packet->interface; /* Set a flag if this client is a lame Microsoft client that NUL terminates string options and expects us to do likewise. */ - if (packet -> options [DHO_HOST_NAME].data && - packet -> options [DHO_HOST_NAME].data - [packet -> options [DHO_HOST_NAME].len - 1] == '\0') - lease -> flags |= MS_NULL_TERMINATION; + if (packet->options[DHO_HOST_NAME].data && + packet->options[DHO_HOST_NAME].data + [packet->options[DHO_HOST_NAME].len - 1] == '\0') + lease->flags |= MS_NULL_TERMINATION; else - lease -> flags &= ~MS_NULL_TERMINATION; + lease->flags &= ~MS_NULL_TERMINATION; /* Remember the giaddr, xid, secs, flags and hops. */ - state -> giaddr = packet -> raw -> giaddr; - state -> ciaddr = packet -> raw -> ciaddr; - state -> xid = packet -> raw -> xid; - state -> secs = packet -> raw -> secs; - state -> bootp_flags = packet -> raw -> flags; - state -> hops = packet -> raw -> hops; - state -> offer = offer; + state->giaddr = packet->raw->giaddr; + state->ciaddr = packet->raw->ciaddr; + state->xid = packet->raw->xid; + state->secs = packet->raw->secs; + state->bootp_flags = packet->raw->flags; + state->hops = packet->raw->hops; + state->offer = offer; /* Figure out what options to send to the client: */ /* Start out with the subnet options... */ - memcpy (state -> options, - lease -> subnet -> group -> options, - sizeof state -> options); + memcpy(state->options, + lease->subnet->group->options, + sizeof state->options); /* Vendor and user classes are only supported for DHCP clients. */ - if (state -> offer) { + if (state->offer) { /* If we have a vendor class, install those options, superseding any subnet options. */ if (vendor_class) { for (i = 0; i < 256; i++) - if (vendor_class -> group -> options [i]) - state -> options [i] = - (vendor_class -> group -> - options [i]); + if (vendor_class->group->options[i]) + state->options[i] = + (vendor_class->group -> + options[i]); } /* If we have a user class, install those options, superseding any subnet and vendor class options. */ if (user_class) { for (i = 0; i < 256; i++) - if (user_class -> group -> options [i]) - state -> options [i] = - (user_class -> group -> - options [i]); + if (user_class->group->options[i]) + state->options[i] = + (user_class->group -> + options[i]); } } /* If we have a host_decl structure, install the associated options, superseding anything that's in the way. */ - if (lease -> host) { + if (lease->host) { for (i = 0; i < 256; i++) - if (lease -> host -> group -> options [i]) - state -> options [i] = (lease -> host -> - group -> options [i]); + if (lease->host->group->options[i]) + state->options[i] = (lease->host -> + group->options[i]); } /* Get the Maximum Message Size option from the packet, if one was sent. */ i = DHO_DHCP_MAX_MESSAGE_SIZE; - if (packet -> options [i].data && - (packet -> options [i].len == sizeof (u_int16_t))) - state -> max_message_size = - getUShort (packet -> options [i].data); + if (packet->options[i].data && + (packet->options[i].len == sizeof (u_int16_t))) + state->max_message_size = + getUShort (packet->options[i].data); /* Otherwise, if a maximum message size was specified, use that. */ - else if (state -> options [i] && state -> options [i] -> value) - state -> max_message_size = - getUShort (state -> options [i] -> value); + else if (state->options[i] && state->options[i]->value) + state->max_message_size = + getUShort (state->options[i]->value); /* Save the parameter request list if there is one. */ i = DHO_DHCP_PARAMETER_REQUEST_LIST; - if (packet -> options [i].data) { - state -> prl = dmalloc (packet -> options [i].len, + if (packet->options[i].data) { + state->prl = dmalloc (packet->options[i].len, "ack_lease: prl"); - if (!state -> prl) - warn ("no memory for parameter request list"); + if (!state->prl) + warn("no memory for parameter request list"); else { - memcpy (state -> prl, - packet -> options [i].data, - packet -> options [i].len); - state -> prl_len = packet -> options [i].len; + memcpy(state->prl, + packet->options[i].data, + packet->options[i].len); + state->prl_len = packet->options[i].len; } } /* If we didn't get a hostname from an option somewhere, see if we can get one from the lease. */ i = DHO_HOST_NAME; - if (!state -> options [i] && lease -> hostname) { - state -> options [i] = new_tree_cache ("hostname"); - state -> options [i] -> flags = TC_TEMPORARY; - state -> options [i] -> value = - (unsigned char *)lease -> hostname; - state -> options [i] -> len = strlen (lease -> hostname); - state -> options [i] -> buf_size = state -> options [i] -> len; - state -> options [i] -> timeout = 0xFFFFFFFF; - state -> options [i] -> tree = (struct tree *)0; + if (!state->options[i] && lease->hostname) { + state->options[i] = new_tree_cache ("hostname"); + state->options[i]->flags = TC_TEMPORARY; + state->options[i]->value = + (unsigned char *)lease->hostname; + state->options[i]->len = strlen (lease->hostname); + state->options[i]->buf_size = state->options[i]->len; + state->options[i]->timeout = 0xFFFFFFFF; + state->options[i]->tree = (struct tree *)0; } /* Now, if appropriate, put in DHCP-specific options that override those. */ - if (state -> offer) { + if (state->offer) { i = DHO_DHCP_MESSAGE_TYPE; - state -> options [i] = new_tree_cache ("message-type"); - state -> options [i] -> flags = TC_TEMPORARY; - state -> options [i] -> value = &state -> offer; - state -> options [i] -> len = sizeof state -> offer; - state -> options [i] -> buf_size = sizeof state -> offer; - state -> options [i] -> timeout = 0xFFFFFFFF; - state -> options [i] -> tree = (struct tree *)0; + state->options[i] = new_tree_cache ("message-type"); + state->options[i]->flags = TC_TEMPORARY; + state->options[i]->value = &state->offer; + state->options[i]->len = sizeof state->offer; + state->options[i]->buf_size = sizeof state->offer; + state->options[i]->timeout = 0xFFFFFFFF; + state->options[i]->tree = (struct tree *)0; i = DHO_DHCP_SERVER_IDENTIFIER; - if (!state -> options [i]) { + if (!state->options[i]) { use_primary: - state -> options [i] = new_tree_cache ("server-id"); - state -> options [i] -> value = + state->options[i] = new_tree_cache ("server-id"); + state->options[i]->value = (unsigned char *)&state -> - ip -> primary_address; - state -> options [i] -> len = - sizeof state -> ip -> primary_address; - state -> options [i] -> buf_size - = state -> options [i] -> len; - state -> options [i] -> timeout = 0xFFFFFFFF; - state -> options [i] -> tree = (struct tree *)0; - state -> from.len = - sizeof state -> ip -> primary_address; - memcpy (state -> from.iabuf, - &state -> ip -> primary_address, - state -> from.len); + ip->primary_address; + state->options[i]->len = + sizeof state->ip->primary_address; + state->options[i]->buf_size + = state->options[i]->len; + state->options[i]->timeout = 0xFFFFFFFF; + state->options[i]->tree = (struct tree *)0; + state->from.len = + sizeof state->ip->primary_address; + memcpy(state->from.iabuf, + &state->ip->primary_address, + state->from.len); } else { /* Find the value of the server identifier... */ - if (!tree_evaluate (state -> options [i])) + if (!tree_evaluate (state->options[i])) goto use_primary; - if (!state -> options [i] -> value || - (state -> options [i] -> len > - sizeof state -> from.iabuf)) + if (!state->options[i]->value || + (state->options[i]->len > + sizeof state->from.iabuf)) goto use_primary; - state -> from.len = state -> options [i] -> len; - memcpy (state -> from.iabuf, - state -> options [i] -> value, - state -> from.len); + state->from.len = state->options[i]->len; + memcpy(state->from.iabuf, + state->options[i]->value, + state->from.len); } /* Sanity check the lease time. */ - if ((state -> offered_expiry - cur_time) < 15) + if ((state->offered_expiry - cur_time) < 15) offered_lease_time = default_lease_time; - else if (state -> offered_expiry - cur_time > max_lease_time) + else if (state->offered_expiry - cur_time > max_lease_time) offered_lease_time = max_lease_time; else offered_lease_time = - state -> offered_expiry - cur_time; + state->offered_expiry - cur_time; - putULong ((unsigned char *)&state -> expiry, + putULong ((unsigned char *)&state->expiry, offered_lease_time); i = DHO_DHCP_LEASE_TIME; - state -> options [i] = new_tree_cache ("lease-expiry"); - state -> options [i] -> flags = TC_TEMPORARY; - state -> options [i] -> value = - (unsigned char *)&state -> expiry; - state -> options [i] -> len = sizeof state -> expiry; - state -> options [i] -> buf_size = sizeof state -> expiry; - state -> options [i] -> timeout = 0xFFFFFFFF; - state -> options [i] -> tree = (struct tree *)0; + state->options[i] = new_tree_cache ("lease-expiry"); + state->options[i]->flags = TC_TEMPORARY; + state->options[i]->value = + (unsigned char *)&state->expiry; + state->options[i]->len = sizeof state->expiry; + state->options[i]->buf_size = sizeof state->expiry; + state->options[i]->timeout = 0xFFFFFFFF; + state->options[i]->tree = (struct tree *)0; /* Renewal time is lease time * 0.5. */ offered_lease_time /= 2; - putULong ((unsigned char *)&state -> renewal, + putULong ((unsigned char *)&state->renewal, offered_lease_time); i = DHO_DHCP_RENEWAL_TIME; - state -> options [i] = new_tree_cache ("renewal-time"); - state -> options [i] -> flags = TC_TEMPORARY; - state -> options [i] -> value = - (unsigned char *)&state -> renewal; - state -> options [i] -> len = sizeof state -> renewal; - state -> options [i] -> buf_size = sizeof state -> renewal; - state -> options [i] -> timeout = 0xFFFFFFFF; - state -> options [i] -> tree = (struct tree *)0; + state->options[i] = new_tree_cache ("renewal-time"); + state->options[i]->flags = TC_TEMPORARY; + state->options[i]->value = + (unsigned char *)&state->renewal; + state->options[i]->len = sizeof state->renewal; + state->options[i]->buf_size = sizeof state->renewal; + state->options[i]->timeout = 0xFFFFFFFF; + state->options[i]->tree = (struct tree *)0; /* Rebinding time is lease time * 0.875. */ offered_lease_time += (offered_lease_time / 2 + offered_lease_time / 4); - putULong ((unsigned char *)&state -> rebind, + putULong ((unsigned char *)&state->rebind, offered_lease_time); i = DHO_DHCP_REBINDING_TIME; - state -> options [i] = new_tree_cache ("rebind-time"); - state -> options [i] -> flags = TC_TEMPORARY; - state -> options [i] -> value = - (unsigned char *)&state -> rebind; - state -> options [i] -> len = sizeof state -> rebind; - state -> options [i] -> buf_size = sizeof state -> rebind; - state -> options [i] -> timeout = 0xFFFFFFFF; - state -> options [i] -> tree = (struct tree *)0; + state->options[i] = new_tree_cache ("rebind-time"); + state->options[i]->flags = TC_TEMPORARY; + state->options[i]->value = + (unsigned char *)&state->rebind; + state->options[i]->len = sizeof state->rebind; + state->options[i]->buf_size = sizeof state->rebind; + state->options[i]->timeout = 0xFFFFFFFF; + state->options[i]->tree = (struct tree *)0; /* If we used the vendor class the client specified, we have to return it. */ if (vendor_class) { i = DHO_DHCP_CLASS_IDENTIFIER; - state -> options [i] = + state->options[i] = new_tree_cache ("class-identifier"); - state -> options [i] -> flags = TC_TEMPORARY; - state -> options [i] -> value = - (unsigned char *)vendor_class -> name; - state -> options [i] -> len = - strlen (vendor_class -> name); - state -> options [i] -> buf_size = - state -> options [i] -> len; - state -> options [i] -> timeout = 0xFFFFFFFF; - state -> options [i] -> tree = (struct tree *)0; + state->options[i]->flags = TC_TEMPORARY; + state->options[i]->value = + (unsigned char *)vendor_class->name; + state->options[i]->len = + strlen (vendor_class->name); + state->options[i]->buf_size = + state->options[i]->len; + state->options[i]->timeout = 0xFFFFFFFF; + state->options[i]->tree = (struct tree *)0; } /* If we used the user class the client specified, we have to return it. */ if (user_class) { i = DHO_DHCP_USER_CLASS_ID; - state -> options [i] = new_tree_cache ("user-class"); - state -> options [i] -> flags = TC_TEMPORARY; - state -> options [i] -> value = - (unsigned char *)user_class -> name; - state -> options [i] -> len = - strlen (user_class -> name); - state -> options [i] -> buf_size = - state -> options [i] -> len; - state -> options [i] -> timeout = 0xFFFFFFFF; - state -> options [i] -> tree = (struct tree *)0; + state->options[i] = new_tree_cache ("user-class"); + state->options[i]->flags = TC_TEMPORARY; + state->options[i]->value = + (unsigned char *)user_class->name; + state->options[i]->len = + strlen (user_class->name); + state->options[i]->buf_size = + state->options[i]->len; + state->options[i]->timeout = 0xFFFFFFFF; + state->options[i]->tree = (struct tree *)0; } } /* Use the subnet mask from the subnet declaration if no other mask has been provided. */ i = DHO_SUBNET_MASK; - if (!state -> options [i]) { - state -> options [i] = new_tree_cache ("subnet-mask"); - state -> options [i] -> flags = TC_TEMPORARY; - state -> options [i] -> value = - lease -> subnet -> netmask.iabuf; - state -> options [i] -> len = lease -> subnet -> netmask.len; - state -> options [i] -> buf_size = - lease -> subnet -> netmask.len; - state -> options [i] -> timeout = 0xFFFFFFFF; - state -> options [i] -> tree = (struct tree *)0; + if (!state->options[i]) { + state->options[i] = new_tree_cache ("subnet-mask"); + state->options[i]->flags = TC_TEMPORARY; + state->options[i]->value = + lease->subnet->netmask.iabuf; + state->options[i]->len = lease->subnet->netmask.len; + state->options[i]->buf_size = + lease->subnet->netmask.len; + state->options[i]->timeout = 0xFFFFFFFF; + state->options[i]->tree = (struct tree *)0; } /* If so directed, use the leased IP address as the router address. @@ -1114,52 +1108,52 @@ void ack_lease (packet, lease, offer, when) so if the local router does proxy arp, you win. */ ulafdr = 0; - if (lease -> host) { - if (lease -> host -> group -> use_lease_addr_for_default_route) + if (lease->host) { + if (lease->host->group->use_lease_addr_for_default_route) ulafdr = 1; } else if (user_class) { - if (user_class -> group -> use_lease_addr_for_default_route) + if (user_class->group->use_lease_addr_for_default_route) ulafdr = 1; } else if (vendor_class) { - if (vendor_class -> group -> use_lease_addr_for_default_route) + if (vendor_class->group->use_lease_addr_for_default_route) ulafdr = 1; - } else if (lease -> subnet -> group -> + } else if (lease->subnet->group -> use_lease_addr_for_default_route) ulafdr = 1; else ulafdr = 0; i = DHO_ROUTERS; - if (ulafdr && !state -> options [i]) { - state -> options [i] = new_tree_cache ("routers"); - state -> options [i] -> flags = TC_TEMPORARY; - state -> options [i] -> value = - lease -> ip_addr.iabuf; - state -> options [i] -> len = - lease -> ip_addr.len; - state -> options [i] -> buf_size = - lease -> ip_addr.len; - state -> options [i] -> timeout = 0xFFFFFFFF; - state -> options [i] -> tree = (struct tree *)0; + if (ulafdr && !state->options[i]) { + state->options[i] = new_tree_cache ("routers"); + state->options[i]->flags = TC_TEMPORARY; + state->options[i]->value = + lease->ip_addr.iabuf; + state->options[i]->len = + lease->ip_addr.len; + state->options[i]->buf_size = + lease->ip_addr.len; + state->options[i]->timeout = 0xFFFFFFFF; + state->options[i]->tree = (struct tree *)0; } #ifdef DEBUG_PACKET dump_packet (packet); - dump_raw ((unsigned char *)packet -> raw, packet -> packet_length); + dump_raw ((unsigned char *)packet->raw, packet->packet_length); #endif - lease -> state = state; + lease->state = state; /* If this is a DHCPOFFER, ping the lease address before actually sending the offer. */ - if (offer == DHCPOFFER && !(lease -> flags & STATIC_LEASE) && - cur_time - lease -> timestamp > 60) { - lease -> timestamp = cur_time; - icmp_echorequest (&lease -> ip_addr); + if (offer == DHCPOFFER && !(lease->flags & STATIC_LEASE) && + cur_time - lease->timestamp > 60) { + lease->timestamp = cur_time; + icmp_echorequest (&lease->ip_addr); add_timeout (cur_time + 1, lease_ping_timeout, lease); ++outstanding_pings; } else { - lease -> timestamp = cur_time; + lease->timestamp = cur_time; dhcp_reply (lease); } } @@ -1175,57 +1169,57 @@ void dhcp_reply (lease) struct hardware hto; int result; int i; - struct lease_state *state = lease -> state; + struct lease_state *state = lease->state; int nulltp, bootpp; u_int8_t *prl; int prl_len; if (!state) - error ("dhcp_reply was supplied lease with no state!"); + error("dhcp_reply was supplied lease with no state!"); /* Compose a response for the client... */ memset (&raw, 0, sizeof raw); /* Copy in the filename if given; otherwise, flag the filename buffer as available for options. */ - if (state -> filename [0]) - strlcpy (raw.file, state -> filename, sizeof raw.file); + if (state->filename[0]) + strlcpy(raw.file, state->filename, sizeof raw.file); else bufs |= 1; /* Copy in the server name if given; otherwise, flag the server_name buffer as available for options. */ - if (state -> server_name) - strlcpy (raw.sname, state -> server_name, sizeof raw.sname); + if (state->server_name) + strlcpy(raw.sname, state->server_name, sizeof raw.sname); else bufs |= 2; /* XXX */ - memcpy (raw.chaddr, lease -> hardware_addr.haddr, sizeof raw.chaddr); - raw.hlen = lease -> hardware_addr.hlen; - raw.htype = lease -> hardware_addr.htype; + memcpy(raw.chaddr, lease->hardware_addr.haddr, sizeof raw.chaddr); + raw.hlen = lease->hardware_addr.hlen; + raw.htype = lease->hardware_addr.htype; /* See if this is a Microsoft client that NUL-terminates its strings and expects us to do likewise... */ - if (lease -> flags & MS_NULL_TERMINATION) + if (lease->flags & MS_NULL_TERMINATION) nulltp = 1; else nulltp = 0; /* See if this is a bootp client... */ - if (state -> offer) + if (state->offer) bootpp = 0; else bootpp = 1; - if (state -> options [DHO_DHCP_PARAMETER_REQUEST_LIST] && - state -> options [DHO_DHCP_PARAMETER_REQUEST_LIST] -> value) { - prl = state -> options - [DHO_DHCP_PARAMETER_REQUEST_LIST] -> value; - prl_len = state -> options - [DHO_DHCP_PARAMETER_REQUEST_LIST] -> len; - } else if (state -> prl) { - prl = state -> prl; - prl_len = state -> prl_len; + if (state->options[DHO_DHCP_PARAMETER_REQUEST_LIST] && + state->options[DHO_DHCP_PARAMETER_REQUEST_LIST]->value) { + prl = state->options + [DHO_DHCP_PARAMETER_REQUEST_LIST]->value; + prl_len = state->options + [DHO_DHCP_PARAMETER_REQUEST_LIST]->len; + } else if (state->prl) { + prl = state->prl; + prl_len = state->prl_len; } else { prl = (u_int8_t *)0; prl_len = 0; @@ -1233,59 +1227,59 @@ void dhcp_reply (lease) /* Insert such options as will fit into the buffer. */ packet_length = cons_options ((struct packet *)0, &raw, - state -> max_message_size, - state -> options, + state->max_message_size, + state->options, bufs, nulltp, bootpp, prl, prl_len); /* Having done the cons_options(), we can release the tree_cache entries. */ for (i = 0; i < 256; i++) { - if (state -> options [i] && - state -> options [i] -> flags & TC_TEMPORARY) - free_tree_cache (state -> options [i], "dhcp_reply"); + if (state->options[i] && + state->options[i]->flags & TC_TEMPORARY) + free_tree_cache (state->options[i], "dhcp_reply"); } - memcpy (&raw.ciaddr, &state -> ciaddr, sizeof raw.ciaddr); - memcpy (&raw.yiaddr, lease -> ip_addr.iabuf, 4); + memcpy(&raw.ciaddr, &state->ciaddr, sizeof raw.ciaddr); + memcpy(&raw.yiaddr, lease->ip_addr.iabuf, 4); /* Figure out the address of the next server. */ - if (lease -> host && lease -> host -> group -> next_server.len) - memcpy (&raw.siaddr, - lease -> host -> group -> next_server.iabuf, 4); - else if (lease -> subnet -> group -> next_server.len) - memcpy (&raw.siaddr, - lease -> subnet -> group -> next_server.iabuf, 4); - else if (lease -> subnet -> interface_address.len) - memcpy (&raw.siaddr, - lease -> subnet -> interface_address.iabuf, 4); + if (lease->host && lease->host->group->next_server.len) + memcpy(&raw.siaddr, + lease->host->group->next_server.iabuf, 4); + else if (lease->subnet->group->next_server.len) + memcpy(&raw.siaddr, + lease->subnet->group->next_server.iabuf, 4); + else if (lease->subnet->interface_address.len) + memcpy(&raw.siaddr, + lease->subnet->interface_address.iabuf, 4); else - raw.siaddr = state -> ip -> primary_address; + raw.siaddr = state->ip->primary_address; - raw.giaddr = state -> giaddr; + raw.giaddr = state->giaddr; - raw.xid = state -> xid; - raw.secs = state -> secs; - raw.flags = state -> bootp_flags; - raw.hops = state -> hops; + raw.xid = state->xid; + raw.secs = state->secs; + raw.flags = state->bootp_flags; + raw.hops = state->hops; raw.op = BOOTREPLY; /* Say what we're doing... */ note ("%s on %s to %s via %s", - (state -> offer - ? (state -> offer == DHCPACK ? "DHCPACK" : "DHCPOFFER") + (state->offer + ? (state->offer == DHCPACK ? "DHCPACK" : "DHCPOFFER") : "BOOTREPLY"), - piaddr (lease -> ip_addr), - print_hw_addr (lease -> hardware_addr.htype, - lease -> hardware_addr.hlen, - lease -> hardware_addr.haddr), - state -> giaddr.s_addr - ? inet_ntoa (state -> giaddr) - : state -> ip -> name); + piaddr (lease->ip_addr), + print_hw_addr (lease->hardware_addr.htype, + lease->hardware_addr.hlen, + lease->hardware_addr.haddr), + state->giaddr.s_addr + ? inet_ntoa (state->giaddr) + : state->ip->name); /* Set up the hardware address... */ - hto.htype = lease -> hardware_addr.htype; - hto.hlen = lease -> hardware_addr.hlen; - memcpy (hto.haddr, lease -> hardware_addr.haddr, hto.hlen); + hto.htype = lease->hardware_addr.htype; + hto.hlen = lease->hardware_addr.hlen; + memcpy(hto.haddr, lease->hardware_addr.haddr, hto.hlen); memset (&to, 0, sizeof to); to.sin_family = AF_INET; @@ -1315,7 +1309,7 @@ void dhcp_reply (lease) &to, (struct hardware *)0); free_lease_state (state, "dhcp_reply fallback 1"); - lease -> state = (struct lease_state *)0; + lease->state = (struct lease_state *)0; return; } @@ -1331,11 +1325,11 @@ void dhcp_reply (lease) and will therefore get a relayed response from the above code. */ } else if (raw.ciaddr.s_addr && - !((state -> got_server_identifier || + !((state->got_server_identifier || (raw.flags & htons (BOOTP_BROADCAST))) && /* XXX This won't work if giaddr isn't zero, but it is: */ - (state -> shared_network == lease -> shared_network)) && - state -> offer == DHCPACK) { + (state->shared_network == lease->shared_network)) && + state->offer == DHCPACK) { to.sin_addr = raw.ciaddr; to.sin_port = remote_port; @@ -1347,7 +1341,7 @@ void dhcp_reply (lease) (struct hardware *)0); free_lease_state (state, "dhcp_reply fallback 2"); - lease -> state = (struct lease_state *)0; + lease->state = (struct lease_state *)0; return; } @@ -1365,14 +1359,14 @@ void dhcp_reply (lease) to.sin_port = remote_port; } - memcpy (&from, state -> from.iabuf, sizeof from); + memcpy(&from, state->from.iabuf, sizeof from); - result = send_packet (state -> ip, + result = send_packet (state->ip, (struct packet *)0, &raw, packet_length, from, &to, &hto); free_lease_state (state, "dhcp_reply"); - lease -> state = (struct lease_state *)0; + lease->state = (struct lease_state *)0; } struct lease *find_lease (packet, share, ours) @@ -1381,65 +1375,65 @@ struct lease *find_lease (packet, share, ours) int *ours; { struct lease *uid_lease, *ip_lease, *hw_lease; - struct lease *lease = (struct lease *)0; + struct lease *lease = NULL; struct iaddr cip; struct host_decl *hp, *host = (struct host_decl *)0; struct lease *fixed_lease; /* Figure out what IP address the client is requesting, if any. */ - if (packet -> options [DHO_DHCP_REQUESTED_ADDRESS].len && - packet -> options [DHO_DHCP_REQUESTED_ADDRESS].len == 4) { - packet -> got_requested_address = 1; + if (packet->options[DHO_DHCP_REQUESTED_ADDRESS].len && + packet->options[DHO_DHCP_REQUESTED_ADDRESS].len == 4) { + packet->got_requested_address = 1; cip.len = 4; - memcpy (cip.iabuf, - packet -> options [DHO_DHCP_REQUESTED_ADDRESS].data, + memcpy(cip.iabuf, + packet->options[DHO_DHCP_REQUESTED_ADDRESS].data, cip.len); - } else if (packet -> raw -> ciaddr.s_addr) { + } else if (packet->raw->ciaddr.s_addr) { cip.len = 4; - memcpy (cip.iabuf, &packet -> raw -> ciaddr, 4); + memcpy(cip.iabuf, &packet->raw->ciaddr, 4); } else cip.len = 0; /* Try to find a host or lease that's been assigned to the specified unique client identifier. */ - if (packet -> options [DHO_DHCP_CLIENT_IDENTIFIER].len) { + if (packet->options[DHO_DHCP_CLIENT_IDENTIFIER].len) { /* First, try to find a fixed host entry for the specified client identifier... */ - hp = find_hosts_by_uid (packet -> options + hp = find_hosts_by_uid (packet->options [DHO_DHCP_CLIENT_IDENTIFIER].data, - packet -> options + packet->options [DHO_DHCP_CLIENT_IDENTIFIER].len); if (hp) { host = hp; fixed_lease = mockup_lease (packet, share, hp); - uid_lease = (struct lease *)0; + uid_lease = NULL; } else { uid_lease = find_lease_by_uid - (packet -> options - [DHO_DHCP_CLIENT_IDENTIFIER].data, - packet -> options - [DHO_DHCP_CLIENT_IDENTIFIER].len); + (packet->options + [DHO_DHCP_CLIENT_IDENTIFIER].data, + packet->options + [DHO_DHCP_CLIENT_IDENTIFIER].len); /* Find the lease matching this uid that's on the network the packet came from (if any). */ - for (; uid_lease; uid_lease = uid_lease -> n_uid) - if (uid_lease -> shared_network == share) + for (; uid_lease; uid_lease = uid_lease->n_uid) + if (uid_lease->shared_network == share) break; - fixed_lease = (struct lease *)0; + fixed_lease = NULL; if (uid_lease && - (uid_lease -> flags & ABANDONED_LEASE)) - uid_lease = (struct lease *)0; + (uid_lease->flags & ABANDONED_LEASE)) + uid_lease = NULL; } } else { - uid_lease = (struct lease *)0; - fixed_lease = (struct lease *)0; + uid_lease = NULL; + fixed_lease = NULL; } /* If we didn't find a fixed lease using the uid, try doing it with the hardware address... */ if (!fixed_lease) { - hp = find_hosts_by_haddr (packet -> raw -> htype, - packet -> raw -> chaddr, - packet -> raw -> hlen); + hp = find_hosts_by_haddr (packet->raw->htype, + packet->raw->chaddr, + packet->raw->hlen); if (hp) { host = hp; /* Save it for later. */ fixed_lease = mockup_lease (packet, share, hp); @@ -1449,30 +1443,30 @@ struct lease *find_lease (packet, share, ours) /* If fixed_lease is present but does not match the requested IP address, and this is a DHCPREQUEST, then we can't return any other lease, so we might as well return now. */ - if (packet -> packet_type == DHCPREQUEST && fixed_lease && - (fixed_lease -> ip_addr.len != cip.len || - memcmp (fixed_lease -> ip_addr.iabuf, + if (packet->packet_type == DHCPREQUEST && fixed_lease && + (fixed_lease->ip_addr.len != cip.len || + memcmp (fixed_lease->ip_addr.iabuf, cip.iabuf, cip.len))) { if (ours) *ours = 1; - strlcpy (dhcp_message, "requested address is incorrect", + strlcpy(dhcp_message, "requested address is incorrect", sizeof (dhcp_message)); - return (struct lease *)0; + return NULL; } /* Try to find a lease that's been attached to the client's hardware address... */ - hw_lease = find_lease_by_hw_addr (packet -> raw -> chaddr, - packet -> raw -> hlen); + hw_lease = find_lease_by_hw_addr(packet->raw->chaddr, + packet->raw->hlen); /* Find the lease that's on the network the packet came from (if any). */ - for (; hw_lease; hw_lease = hw_lease -> n_hw) { - if (hw_lease -> shared_network == share) { - if ((hw_lease -> flags & ABANDONED_LEASE)) + for (; hw_lease; hw_lease = hw_lease->n_hw) { + if (hw_lease->shared_network == share) { + if ((hw_lease->flags & ABANDONED_LEASE)) continue; - if (packet -> packet_type) + if (packet->packet_type) break; - if (hw_lease -> flags & + if (hw_lease->flags & (BOOTP_LEASE | DYNAMIC_BOOTP_OK)) break; } @@ -1483,7 +1477,7 @@ struct lease *find_lease (packet, share, ours) if (cip.len) ip_lease = find_lease_by_ip_addr (cip); else - ip_lease = (struct lease *)0; + ip_lease = NULL; /* If ip_lease is valid at this point, set ours to one, so that even if we choose a different lease, we know that the address @@ -1498,72 +1492,72 @@ struct lease *find_lease (packet, share, ours) that thought it had this lease answered an ARP or PING, causing the lease to be abandoned. If so, this request probably came from that client. */ - if (ip_lease && (ip_lease -> shared_network != share)) { - ip_lease = (struct lease *)0; - strlcpy (dhcp_message, "requested address on bad subnet", + if (ip_lease && (ip_lease->shared_network != share)) { + ip_lease = NULL; + strlcpy(dhcp_message, "requested address on bad subnet", sizeof(dhcp_message)); } /* Toss ip_lease if it hasn't yet expired and isn't owned by the client. */ if (ip_lease && - ip_lease -> ends >= cur_time && + ip_lease->ends >= cur_time && ip_lease != uid_lease) { int i = DHO_DHCP_CLIENT_IDENTIFIER; /* Make sure that ip_lease actually belongs to the client, and toss it if not. */ - if ((ip_lease -> uid_len && - packet -> options [i].data && - ip_lease -> uid_len == packet -> options [i].len && - !memcmp (packet -> options [i].data, - ip_lease -> uid, ip_lease -> uid_len)) || - (!ip_lease -> uid_len && - (ip_lease -> hardware_addr.htype == - packet -> raw -> htype) && - ip_lease -> hardware_addr.hlen == packet -> raw -> hlen && - !memcmp (ip_lease -> hardware_addr.haddr, - packet -> raw -> chaddr, - ip_lease -> hardware_addr.hlen))) { + if ((ip_lease->uid_len && + packet->options[i].data && + ip_lease->uid_len == packet->options[i].len && + !memcmp (packet->options[i].data, + ip_lease->uid, ip_lease->uid_len)) || + (!ip_lease->uid_len && + (ip_lease->hardware_addr.htype == + packet->raw->htype) && + ip_lease->hardware_addr.hlen == packet->raw->hlen && + !memcmp (ip_lease->hardware_addr.haddr, + packet->raw->chaddr, + ip_lease->hardware_addr.hlen))) { if (uid_lease) { - if (uid_lease -> ends > cur_time) { - warn ("client %s has duplicate leases on %s", - print_hw_addr (packet -> raw -> htype, - packet -> raw -> hlen, - packet -> raw -> chaddr), - ip_lease -> shared_network -> name); + if (uid_lease->ends > cur_time) { + warn("client %s has duplicate leases on %s", + print_hw_addr (packet->raw->htype, + packet->raw->hlen, + packet->raw->chaddr), + ip_lease->shared_network->name); if (uid_lease && - !packet -> raw -> ciaddr.s_addr) + !packet->raw->ciaddr.s_addr) release_lease (uid_lease); } uid_lease = ip_lease; } } else { - strlcpy (dhcp_message, + strlcpy(dhcp_message, "requested address is not available", sizeof(dhcp_message)); - ip_lease = (struct lease *)0; + ip_lease = NULL; } /* If we get to here and fixed_lease is not null, that means that there are both a dynamic lease and a fixed-address declaration for the same IP address. */ - if (packet -> packet_type == DHCPREQUEST && fixed_lease) { - fixed_lease = (struct lease *)0; + if (packet->packet_type == DHCPREQUEST && fixed_lease) { + fixed_lease = NULL; db_conflict: - warn ("Both dynamic and static leases present for %s.", + warn("Both dynamic and static leases present for %s.", piaddr (cip)); - warn ("Either remove host declaration %s or remove %s", - (fixed_lease && fixed_lease -> host - ? (fixed_lease -> host -> name - ? fixed_lease -> host -> name : piaddr (cip)) + warn("Either remove host declaration %s or remove %s", + (fixed_lease && fixed_lease->host + ? (fixed_lease->host->name + ? fixed_lease->host->name : piaddr (cip)) : piaddr (cip)), piaddr (cip)); - warn ("from the dynamic address pool for %s", - share -> name); + warn("from the dynamic address pool for %s", + share->name); if (fixed_lease) - ip_lease = (struct lease *)0; - strlcpy (dhcp_message, + ip_lease = NULL; + strlcpy(dhcp_message, "database conflict - call for help!", sizeof(dhcp_message)); } @@ -1571,7 +1565,7 @@ struct lease *find_lease (packet, share, ours) /* If we get to here with both fixed_lease and ip_lease not null, then we have a configuration file bug. */ - if (packet -> packet_type == DHCPREQUEST && fixed_lease && ip_lease) + if (packet->packet_type == DHCPREQUEST && fixed_lease && ip_lease) goto db_conflict; /* Toss hw_lease if it hasn't yet expired and the uid doesn't @@ -1579,53 +1573,53 @@ struct lease *find_lease (packet, share, ours) client is now doing dynamic BOOTP (and thus hasn't provided a uid) we let the client get away with it. */ if (hw_lease && - hw_lease -> ends >= cur_time && - hw_lease -> uid && - packet -> options [DHO_DHCP_CLIENT_IDENTIFIER].len && + hw_lease->ends >= cur_time && + hw_lease->uid && + packet->options[DHO_DHCP_CLIENT_IDENTIFIER].len && hw_lease != uid_lease) - hw_lease = (struct lease *)0; + hw_lease = NULL; /* Toss extra pointers to the same lease... */ if (hw_lease == uid_lease) - hw_lease = (struct lease *)0; + hw_lease = NULL; if (ip_lease == hw_lease) - hw_lease = (struct lease *)0; + hw_lease = NULL; if (ip_lease == uid_lease) - uid_lease = (struct lease *)0; + uid_lease = NULL; /* If we've already eliminated the lease, it wasn't there to begin with. If we have come up with a matching lease, set the message to bad network in case we have to throw it out. */ if (!ip_lease) { - strlcpy (dhcp_message, "requested address not available", - sizeof(dhcp_message)); + strlcpy(dhcp_message, "requested address not available", + sizeof(dhcp_message)); } /* Now eliminate leases that are on the wrong network... */ if (ip_lease && - (share != ip_lease -> shared_network)) { - if (packet -> packet_type == DHCPREQUEST) - release_lease (ip_lease); - ip_lease = (struct lease *)0; + (share != ip_lease->shared_network)) { + if (packet->packet_type == DHCPREQUEST) + release_lease(ip_lease); + ip_lease = NULL; } if (uid_lease && - (share != uid_lease -> shared_network)) { - if (packet -> packet_type == DHCPREQUEST) + (share != uid_lease->shared_network)) { + if (packet->packet_type == DHCPREQUEST) release_lease (uid_lease); - uid_lease = (struct lease *)0; + uid_lease = NULL; } if (hw_lease && - (share != hw_lease -> shared_network)) { - if (packet -> packet_type == DHCPREQUEST) + (share != hw_lease->shared_network)) { + if (packet->packet_type == DHCPREQUEST) release_lease (hw_lease); - hw_lease = (struct lease *)0; + hw_lease = NULL; } /* If this is a DHCPREQUEST, make sure the lease we're going to return matches the requested IP address. If it doesn't, don't return a lease at all. */ - if (packet -> packet_type == DHCPREQUEST && !ip_lease && !fixed_lease) - return (struct lease *)0; + if (packet->packet_type == DHCPREQUEST && !ip_lease && !fixed_lease) + return NULL; /* At this point, if fixed_lease is nonzero, we can assign it to this client. */ @@ -1637,11 +1631,11 @@ struct lease *find_lease (packet, share, ours) a better offer, use that; otherwise, release it. */ if (ip_lease) { if (lease) { - if (packet -> packet_type == DHCPREQUEST) + if (packet->packet_type == DHCPREQUEST) release_lease (ip_lease); } else { lease = ip_lease; - lease -> host = (struct host_decl *)0; + lease->host = (struct host_decl *)0; } } @@ -1650,22 +1644,22 @@ struct lease *find_lease (packet, share, ours) the lease that matched the client identifier. */ if (uid_lease) { if (lease) { - if (packet -> packet_type == DHCPREQUEST) + if (packet->packet_type == DHCPREQUEST) release_lease (uid_lease); } else { lease = uid_lease; - lease -> host = (struct host_decl *)0; + lease->host = (struct host_decl *)0; } } /* The lease that matched the hardware address is treated likewise. */ if (hw_lease) { if (lease) { - if (packet -> packet_type == DHCPREQUEST) + if (packet->packet_type == DHCPREQUEST) release_lease (hw_lease); } else { lease = hw_lease; - lease -> host = (struct host_decl *)0; + lease->host = (struct host_decl *)0; } } @@ -1673,10 +1667,10 @@ struct lease *find_lease (packet, share, ours) find a host_decl that has no address, and if there is one, hang it off the lease so that we can use the supplied options. */ - if (lease && host && !lease -> host) { - for (; host; host = host -> n_ipaddr) { - if (!host -> fixed_addr) { - lease -> host = host; + if (lease && host && !lease->host) { + for (; host; host = host->n_ipaddr) { + if (!host->fixed_addr) { + lease->host = host; break; } } @@ -1685,47 +1679,44 @@ struct lease *find_lease (packet, share, ours) /* If we find an abandoned lease, take it, but print a warning message, so that if it continues to lose, the administrator will eventually investigate. */ - if (lease && (lease -> flags & ABANDONED_LEASE)) { - if (packet -> packet_type == DHCPREQUEST) { - warn ("Reclaiming REQUESTed abandoned IP address %s.", - piaddr (lease -> ip_addr)); - lease -> flags &= ~ABANDONED_LEASE; + if (lease && (lease->flags & ABANDONED_LEASE)) { + if (packet->packet_type == DHCPREQUEST) { + warn("Reclaiming REQUESTed abandoned IP address %s.", + piaddr (lease->ip_addr)); + lease->flags &= ~ABANDONED_LEASE; } else - lease = (struct lease *)0; + lease = NULL; } - return lease; } -/* Search the provided host_decl structure list for an address that's on - the specified shared network. If one is found, mock up and return a - lease structure for it; otherwise return the null pointer. */ - -struct lease *mockup_lease (packet, share, hp) - struct packet *packet; - struct shared_network *share; - struct host_decl *hp; +/* + * Search the provided host_decl structure list for an address that's on + * the specified shared network. If one is found, mock up and return a + * lease structure for it; otherwise return the null pointer. + */ +struct lease * +mockup_lease(struct packet *packet, struct shared_network *share, + struct host_decl *hp) { static struct lease mock; - mock.subnet = find_host_for_network (&hp, &mock.ip_addr, share); + mock.subnet = find_host_for_network(&hp, &mock.ip_addr, share); if (!mock.subnet) - return (struct lease *)0; - mock.next = mock.prev = (struct lease *)0; - mock.shared_network = mock.subnet -> shared_network; + return (NULL); + mock.next = mock.prev = NULL; + mock.shared_network = mock.subnet->shared_network; mock.host = hp; - if (hp -> group -> options [DHO_DHCP_CLIENT_IDENTIFIER]) { - mock.uid = hp -> group -> - options [DHO_DHCP_CLIENT_IDENTIFIER] -> value; - mock.uid_len = hp -> group -> - options [DHO_DHCP_CLIENT_IDENTIFIER] -> len; + if (hp->group->options[DHO_DHCP_CLIENT_IDENTIFIER]) { + mock.uid = hp->group->options[DHO_DHCP_CLIENT_IDENTIFIER]->value; + mock.uid_len = hp->group ->options[DHO_DHCP_CLIENT_IDENTIFIER]->len; } else { - mock.uid = (unsigned char *)0; + mock.uid = NULL; mock.uid_len = 0; } - mock.hardware_addr = hp -> interface; + mock.hardware_addr = hp->interface; mock.starts = mock.timestamp = mock.ends = MIN_TIME; mock.flags = STATIC_LEASE; return &mock; diff --git a/usr.sbin/dhcpd/dhcpd.c b/usr.sbin/dhcpd/dhcpd.c index bab0e1251c9..331c9e81abf 100644 --- a/usr.sbin/dhcpd/dhcpd.c +++ b/usr.sbin/dhcpd/dhcpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcpd.c,v 1.11 2004/04/15 23:20:42 henning Exp $ */ +/* $OpenBSD: dhcpd.c,v 1.12 2004/04/16 04:30:09 deraadt Exp $ */ /* * Copyright (c) 2004 Henning Brauer <henning@cvs.openbsd.org> @@ -204,7 +204,7 @@ lease_pinged(struct iaddr from, u_int8_t *packet, int length) return; } - if (!lp->state && ! lp->releasing) { + if (!lp->state && !lp->releasing) { warn("ICMP Echo Reply for %s arrived late or is spurious.", piaddr(from)); return; diff --git a/usr.sbin/dhcpd/dispatch.c b/usr.sbin/dhcpd/dispatch.c index 313e5334563..be2af3acaaa 100644 --- a/usr.sbin/dhcpd/dispatch.c +++ b/usr.sbin/dhcpd/dispatch.c @@ -67,8 +67,8 @@ int quiet_interface_discovery; register that interface with the network I/O software, figure out what subnet it's on, and add it to the list of interfaces. */ -void discover_interfaces (state) - int state; +void +discover_interfaces(int state) { struct interface_info *tmp; struct interface_info *last, *next; @@ -83,13 +83,12 @@ void discover_interfaces (state) #endif if (getifaddrs(&ifap) != 0) - error ("getifaddrs failed"); + error("getifaddrs failed"); /* If we already have a list of interfaces, and we're running as a DHCP server, the interfaces were requested. */ if (interfaces && (state == DISCOVER_SERVER || - state == DISCOVER_RELAY || - state == DISCOVER_REQUESTED)) + state == DISCOVER_RELAY || state == DISCOVER_REQUESTED)) ir = 0; else if (state == DISCOVER_UNCONFIGURED) ir = INTERFACE_REQUESTED | INTERFACE_AUTOMATIC; @@ -98,10 +97,12 @@ void discover_interfaces (state) /* Cycle through the list of interfaces looking for IP addresses. */ for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { - /* See if this is the sort of interface we want to - deal with. Skip loopback, point-to-point and down - interfaces, except don't skip down interfaces if we're - trying to get a list of configurable interfaces. */ + /* + * See if this is the sort of interface we want to + * deal with. Skip loopback, point-to-point and down + * interfaces, except don't skip down interfaces if we're + * trying to get a list of configurable interfaces. + */ if ((ifa->ifa_flags & IFF_LOOPBACK) || (ifa->ifa_flags & IFF_POINTOPOINT) || (!(ifa->ifa_flags & IFF_UP) && @@ -109,36 +110,36 @@ void discover_interfaces (state) continue; /* See if we've seen an interface that matches this one. */ - for (tmp = interfaces; tmp; tmp = tmp -> next) - if (!strcmp (tmp -> name, ifa -> ifa_name)) + for (tmp = interfaces; tmp; tmp = tmp->next) + if (!strcmp(tmp->name, ifa->ifa_name)) break; /* If there isn't already an interface by this name, allocate one. */ if (!tmp) { - tmp = ((struct interface_info *) - dmalloc (sizeof *tmp, "discover_interfaces")); + tmp = ((struct interface_info *)dmalloc(sizeof *tmp, + "discover_interfaces")); if (!tmp) - error ("Insufficient memory to %s %s", - "record interface", ifa -> ifa_name); - strlcpy (tmp -> name, ifa -> ifa_name, sizeof(tmp->name)); - tmp -> next = interfaces; - tmp -> flags = ir; - tmp -> noifmedia = tmp -> dead = tmp->errors = 0; + error("Insufficient memory to %s %s", + "record interface", ifa->ifa_name); + strlcpy(tmp->name, ifa->ifa_name, sizeof(tmp->name)); + tmp->next = interfaces; + tmp->flags = ir; + tmp->noifmedia = tmp->dead = tmp->errors = 0; interfaces = tmp; } /* If we have the capability, extract link information and record it in a linked list. */ - if (ifa -> ifa_addr->sa_family == AF_LINK) { - struct sockaddr_dl *foo = ((struct sockaddr_dl *) - (ifa -> ifa_addr)); - tmp -> index = foo->sdl_index; - tmp -> hw_address.hlen = foo -> sdl_alen; - tmp -> hw_address.htype = HTYPE_ETHER; /* XXX */ - memcpy (tmp -> hw_address.haddr, - LLADDR (foo), foo -> sdl_alen); - } else if (ifa -> ifa_addr->sa_family == AF_INET) { + if (ifa->ifa_addr->sa_family == AF_LINK) { + struct sockaddr_dl *foo = + ((struct sockaddr_dl *)(ifa->ifa_addr)); + tmp->index = foo->sdl_index; + tmp->hw_address.hlen = foo->sdl_alen; + tmp->hw_address.htype = HTYPE_ETHER; /* XXX */ + memcpy(tmp->hw_address.haddr, + LLADDR (foo), foo->sdl_alen); + } else if (ifa->ifa_addr->sa_family == AF_INET) { struct iaddr addr; /* Get a pointer to the address... */ @@ -151,58 +152,56 @@ void discover_interfaces (state) /* If this is the first real IP address we've found, keep a pointer to ifreq structure in which we found it. */ - if (!tmp -> ifp) { - int len = (IFNAMSIZ + - ifa -> ifa_addr->sa_len); - tif = (struct ifreq *)malloc (len); + if (!tmp->ifp) { + int len = (IFNAMSIZ + ifa->ifa_addr->sa_len); + tif = (struct ifreq *)malloc(len); if (!tif) - error ("no space to remember ifp."); + error("no space to remember ifp."); strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ); memcpy(&tif->ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len); - tmp -> ifp = tif; - tmp -> primary_address = foo.sin_addr; + tmp->ifp = tif; + tmp->primary_address = foo.sin_addr; } /* Grab the address... */ addr.len = 4; - memcpy (addr.iabuf, &foo.sin_addr.s_addr, - addr.len); + memcpy(addr.iabuf, &foo.sin_addr.s_addr, addr.len); /* If there's a registered subnet for this address, connect it together... */ - if ((subnet = find_subnet (addr))) { + if ((subnet = find_subnet(addr))) { /* If this interface has multiple aliases on the same subnet, ignore all but the first we encounter. */ - if (!subnet -> interface) { - subnet -> interface = tmp; - subnet -> interface_address = addr; - } else if (subnet -> interface != tmp) { - warn ("Multiple %s %s: %s %s", - "interfaces match the", - "same subnet", - subnet -> interface -> name, - tmp -> name); + if (!subnet->interface) { + subnet->interface = tmp; + subnet->interface_address = addr; + } else if (subnet->interface != tmp) { + warn("Multiple %s %s: %s %s", + "interfaces match the", + "same subnet", + subnet->interface->name, + tmp->name); } - share = subnet -> shared_network; - if (tmp -> shared_network && - tmp -> shared_network != share) { - warn ("Interface %s matches %s", - tmp -> name, - "multiple shared networks"); + share = subnet->shared_network; + if (tmp->shared_network && + tmp->shared_network != share) { + warn("Interface %s matches %s", + tmp->name, + "multiple shared networks"); } else { - tmp -> shared_network = share; + tmp->shared_network = share; } - if (!share -> interface) { - share -> interface = tmp; - } else if (share -> interface != tmp) { - warn ("Multiple %s %s: %s %s", - "interfaces match the", - "same shared network", - share -> interface -> name, - tmp -> name); + if (!share->interface) { + share->interface = tmp; + } else if (share->interface != tmp) { + warn("Multiple %s %s: %s %s", + "interfaces match the", + "same shared network", + share->interface->name, + tmp->name); } } } @@ -219,138 +218,136 @@ void discover_interfaces (state) /* Weed out the interfaces that did not have IP addresses. */ last = (struct interface_info *)0; for (tmp = interfaces; tmp; tmp = next) { - next = tmp -> next; - if ((tmp -> flags & INTERFACE_AUTOMATIC) && + next = tmp->next; + if ((tmp->flags & INTERFACE_AUTOMATIC) && state == DISCOVER_REQUESTED) - tmp -> flags &= ~(INTERFACE_AUTOMATIC | - INTERFACE_REQUESTED); - if (!tmp -> ifp || !(tmp -> flags & INTERFACE_REQUESTED)) { - if ((tmp -> flags & INTERFACE_REQUESTED) != ir) - error ("%s: not found", tmp -> name); + tmp->flags &= ~(INTERFACE_AUTOMATIC | INTERFACE_REQUESTED); + if (!tmp->ifp || !(tmp->flags & INTERFACE_REQUESTED)) { + if ((tmp->flags & INTERFACE_REQUESTED) != ir) + error("%s: not found", tmp->name); if (!last) - interfaces = interfaces -> next; + interfaces = interfaces->next; else - last -> next = tmp -> next; + last->next = tmp->next; /* Remember the interface in case we need to know about it later. */ - tmp -> next = dummy_interfaces; + tmp->next = dummy_interfaces; dummy_interfaces = tmp; continue; } last = tmp; - memcpy (&foo, &tmp -> ifp -> ifr_addr, - sizeof tmp -> ifp -> ifr_addr); + memcpy(&foo, &tmp->ifp->ifr_addr, sizeof tmp->ifp->ifr_addr); /* We must have a subnet declaration for each interface. */ - if (!tmp -> shared_network && (state == DISCOVER_SERVER)) { - warn ("No subnet declaration for %s (%s).", - tmp -> name, inet_ntoa (foo.sin_addr)); - warn ("Please write a subnet declaration in your %s", - "dhcpd.conf file for the"); - error ("network segment to which interface %s %s", - tmp -> name, "is attached."); + if (!tmp->shared_network && (state == DISCOVER_SERVER)) { + warn("No subnet declaration for %s (%s).", + tmp->name, inet_ntoa(foo.sin_addr)); + warn("Please write a subnet declaration in your %s", + "dhcpd.conf file for the"); + error("network segment to which interface %s %s", + tmp->name, "is attached."); } /* Find subnets that don't have valid interface addresses... */ - for (subnet = (tmp -> shared_network - ? tmp -> shared_network -> subnets - : (struct subnet *)0); - subnet; subnet = subnet -> next_sibling) { - if (!subnet -> interface_address.len) { - /* Set the interface address for this subnet - to the first address we found. */ - subnet -> interface_address.len = 4; - memcpy (subnet -> interface_address.iabuf, - &foo.sin_addr.s_addr, 4); + for (subnet = (tmp->shared_network ? tmp->shared_network->subnets : + NULL); subnet; subnet = subnet->next_sibling) { + if (!subnet->interface_address.len) { + /* + * Set the interface address for this subnet + * to the first address we found. + */ + subnet->interface_address.len = 4; + memcpy(subnet->interface_address.iabuf, + &foo.sin_addr.s_addr, 4); } } /* Register the interface... */ - if_register_receive (tmp); - if_register_send (tmp); + if_register_receive(tmp); + if_register_send(tmp); } /* Now register all the remaining interfaces as protocols. */ - for (tmp = interfaces; tmp; tmp = tmp -> next) { - add_protocol (tmp -> name, tmp -> rfdesc, got_one, tmp); - } + for (tmp = interfaces; tmp; tmp = tmp->next) + add_protocol(tmp->name, tmp->rfdesc, got_one, tmp); freeifaddrs(ifap); } -struct interface_info *setup_fallback () +struct interface_info * +setup_fallback(void) { - fallback_interface = - ((struct interface_info *) - dmalloc (sizeof *fallback_interface, "discover_interfaces")); + fallback_interface = ((struct interface_info *)dmalloc( + sizeof *fallback_interface, "discover_interfaces")); + if (!fallback_interface) - error ("Insufficient memory to record fallback interface."); - memset (fallback_interface, 0, sizeof *fallback_interface); - strlcpy (fallback_interface -> name, "fallback", IFNAMSIZ); - fallback_interface -> shared_network = - new_shared_network ("parse_statement"); - if (!fallback_interface -> shared_network) - error ("No memory for shared subnet"); - memset (fallback_interface -> shared_network, 0, - sizeof (struct shared_network)); - fallback_interface -> shared_network -> name = "fallback-net"; + error("Insufficient memory to record fallback interface."); + memset(fallback_interface, 0, sizeof *fallback_interface); + strlcpy(fallback_interface->name, "fallback", IFNAMSIZ); + fallback_interface->shared_network = new_shared_network("parse_statement"); + if (!fallback_interface->shared_network) + error("No memory for shared subnet"); + memset(fallback_interface->shared_network, 0, + sizeof (struct shared_network)); + fallback_interface->shared_network->name = "fallback-net"; return fallback_interface; } -void reinitialize_interfaces () +void +reinitialize_interfaces(void) { interfaces_invalidated = 1; } -/* Wait for packets to come in using poll(). When a packet comes in, - call receive_packet to receive the packet and possibly strip hardware - addressing information from it, and then call through the - bootp_packet_handler hook to try to do something with it. */ - -void dispatch () +/* + * Wait for packets to come in using poll(). When a packet comes in, + * call receive_packet to receive the packet and possibly strip hardware + * addressing information from it, and then call through the + * bootp_packet_handler hook to try to do something with it. + */ +void +dispatch(void) { + int count, i, nfds = 0, to_msec; struct protocol *l; - int nfds = 0; struct pollfd *fds; - int count; - int i; time_t howlong; - int to_msec; - nfds = 0; - for (l = protocols; l; l = l -> next) { + for (l = protocols; l; l = l->next) ++nfds; - } - fds = (struct pollfd *)malloc ((nfds) * sizeof (struct pollfd)); + fds = (struct pollfd *)malloc((nfds) * sizeof (struct pollfd)); if (fds == NULL) - error ("Can't allocate poll structures."); + error("Can't allocate poll structures."); do { - /* Call any expired timeouts, and then if there's - still a timeout registered, time out the select - call then. */ - another: + /* + * Call any expired timeouts, and then if there's + * still a timeout registered, time out the select + * call then. + */ +another: if (timeouts) { struct timeout *t; - if (timeouts -> when <= cur_time) { + + if (timeouts->when <= cur_time) { t = timeouts; - timeouts = timeouts -> next; - (*(t -> func)) (t -> what); - t -> next = free_timeouts; + timeouts = timeouts->next; + (*(t->func))(t->what); + t->next = free_timeouts; free_timeouts = t; goto another; } + /* * Figure timeout in milliseconds, and check for * potential overflow, so we can cram into an int * for poll, while not polling with a negative * timeout and blocking indefinetely. */ - - howlong = timeouts -> when - cur_time; + howlong = timeouts->when - cur_time; if (howlong > INT_MAX / 1000) howlong = INT_MAX / 1000; to_msec = howlong * 1000; @@ -360,12 +357,13 @@ void dispatch () /* Set up the descriptors to be polled. */ i = 0; - for (l = protocols; l; l = l -> next) { - struct interface_info *ip = l -> local; + for (l = protocols; l; l = l->next) { + struct interface_info *ip = l->local; + if (ip && (l->handler != got_one || !ip->dead)) { - fds [i].fd = l -> fd; - fds [i].events = POLLIN; - fds [i].revents = 0; + fds[i].fd = l->fd; + fds[i].events = POLLIN; + fds[i].revents = 0; ++i; } } @@ -374,30 +372,29 @@ void dispatch () error("No live interfaces to poll on - exiting."); /* Wait for a packet or a timeout... XXX */ - count = poll (fds, nfds, to_msec); + count = poll(fds, nfds, to_msec); /* Not likely to be transitory... */ if (count == -1) { if (errno == EAGAIN || errno == EINTR) { time(&cur_time); continue; - } - else - error ("poll: %m"); + } else + error("poll: %m"); } /* Get the current time... */ time(&cur_time); i = 0; - for (l = protocols; l; l = l -> next) { - struct interface_info *ip; - ip = l->local; + for (l = protocols; l; l = l->next) { + struct interface_info *ip = l->local; + if ((fds [i].revents & POLLIN)) { fds [i].revents = 0; if (ip && (l->handler != got_one || - !ip->dead)) - (*(l -> handler)) (l); + !ip->dead)) + (*(l->handler))(l); if (interfaces_invalidated) break; } @@ -408,31 +405,29 @@ void dispatch () } -void got_one (l) - struct protocol *l; +void +got_one(struct protocol *l) { struct sockaddr_in from; struct hardware hfrom; struct iaddr ifrom; size_t result; union { - unsigned char packbuf [4095]; /* Packet input buffer. - Must be as large as largest - possible MTU. */ + unsigned char packbuf [4095]; struct dhcp_packet packet; } u; - struct interface_info *ip = l -> local; + struct interface_info *ip = l->local; - if ((result = - receive_packet (ip, u.packbuf, sizeof u, &from, &hfrom)) == -1) { - warn ("receive_packet failed on %s: %s", ip -> name, - strerror(errno)); + if ((result = receive_packet (ip, u.packbuf, sizeof u, + &from, &hfrom)) == -1) { + warn("receive_packet failed on %s: %s", ip->name, + strerror(errno)); ip->errors++; - if ((! interface_status(ip)) - || (ip->noifmedia && ip->errors > 20)) { + if ((!interface_status(ip)) || + (ip->noifmedia && ip->errors > 20)) { /* our interface has gone away. */ warn("Interface %s no longer appears valid.", - ip->name); + ip->name); ip->dead = 1; interfaces_invalidated = 1; close(l->fd); @@ -446,10 +441,10 @@ void got_one (l) if (bootp_packet_handler) { ifrom.len = 4; - memcpy (ifrom.iabuf, &from.sin_addr, ifrom.len); + memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len); - (*bootp_packet_handler) (ip, &u.packet, result, - from.sin_port, ifrom, &hfrom); + (*bootp_packet_handler)(ip, &u.packet, result, + from.sin_port, ifrom, &hfrom); } } @@ -473,9 +468,9 @@ interface_status(struct interface_info *ifinfo) * if one of UP and RUNNING flags is dropped, * the interface is not active. */ - if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { + if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) goto inactive; - } + /* Next, check carrier on the interface, if possible */ if (ifinfo->noifmedia) goto active; @@ -484,7 +479,7 @@ interface_status(struct interface_info *ifinfo) if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) { if (errno != EINVAL) { syslog(LOG_DEBUG, "ioctl(SIOCGIFMEDIA) on %s: %m", - ifname); + ifname); ifinfo->noifmedia = 1; goto active; } @@ -496,7 +491,7 @@ interface_status(struct interface_info *ifinfo) goto active; } if (ifmr.ifm_status & IFM_AVALID) { - switch(ifmr.ifm_active & IFM_NMASK) { + switch (ifmr.ifm_active & IFM_NMASK) { case IFM_ETHER: if (ifmr.ifm_status & IFM_ACTIVE) goto active; @@ -513,45 +508,43 @@ interface_status(struct interface_info *ifinfo) return(1); } -int locate_network (packet) - struct packet *packet; +int +locate_network(struct packet *packet) { struct iaddr ia; /* If this came through a gateway, find the corresponding subnet... */ - if (packet -> raw -> giaddr.s_addr) { + if (packet->raw->giaddr.s_addr) { struct subnet *subnet; + ia.len = 4; - memcpy (ia.iabuf, &packet -> raw -> giaddr, 4); - subnet = find_subnet (ia); + memcpy(ia.iabuf, &packet->raw->giaddr, 4); + subnet = find_subnet(ia); if (subnet) - packet -> shared_network = subnet -> shared_network; + packet->shared_network = subnet->shared_network; else - packet -> shared_network = (struct shared_network *)0; + packet->shared_network = NULL; } else { - packet -> shared_network = - packet -> interface -> shared_network; + packet->shared_network = packet->interface->shared_network; } - if (packet -> shared_network) + if (packet->shared_network) return 1; return 0; } -void add_timeout (when, where, what) - time_t when; - void (*where)(void *); - void *what; +void +add_timeout(time_t when, void (*where)(void *), void *what) { struct timeout *t, *q; /* See if this timeout supersedes an existing timeout. */ t = (struct timeout *)0; - for (q = timeouts; q; q = q -> next) { - if (q -> func == where && q -> what == what) { + for (q = timeouts; q; q = q->next) { + if (q->func == where && q->what == what) { if (t) - t -> next = q -> next; + t->next = q->next; else - timeouts = q -> next; + timeouts = q->next; break; } t = q; @@ -562,57 +555,56 @@ void add_timeout (when, where, what) if (!q) { if (free_timeouts) { q = free_timeouts; - free_timeouts = q -> next; - q -> func = where; - q -> what = what; + free_timeouts = q->next; + q->func = where; + q->what = what; } else { - q = (struct timeout *)malloc (sizeof (struct timeout)); + q = (struct timeout *)malloc(sizeof (struct timeout)); if (!q) - error ("Can't allocate timeout structure!"); - q -> func = where; - q -> what = what; + error("Can't allocate timeout structure!"); + q->func = where; + q->what = what; } } - q -> when = when; + q->when = when; /* Now sort this timeout into the timeout list. */ /* Beginning of list? */ - if (!timeouts || timeouts -> when > q -> when) { - q -> next = timeouts; + if (!timeouts || timeouts->when > q->when) { + q->next = timeouts; timeouts = q; return; } /* Middle of list? */ - for (t = timeouts; t -> next; t = t -> next) { - if (t -> next -> when > q -> when) { - q -> next = t -> next; - t -> next = q; + for (t = timeouts; t->next; t = t->next) { + if (t->next->when > q->when) { + q->next = t->next; + t->next = q; return; } } /* End of list. */ - t -> next = q; - q -> next = (struct timeout *)0; + t->next = q; + q->next = NULL; } -void cancel_timeout (where, what) - void (*where)(void *); - void *what; +void +cancel_timeout(void (*where)(void *), void *what) { struct timeout *t, *q; /* Look for this timeout on the list, and unlink it if we find it. */ t = (struct timeout *)0; - for (q = timeouts; q; q = q -> next) { - if (q -> func == where && q -> what == what) { + for (q = timeouts; q; q = q->next) { + if (q->func == where && q->what == what) { if (t) - t -> next = q -> next; + t->next = q->next; else - timeouts = q -> next; + timeouts = q->next; break; } t = q; @@ -620,46 +612,41 @@ void cancel_timeout (where, what) /* If we found the timeout, put it on the free list. */ if (q) { - q -> next = free_timeouts; + q->next = free_timeouts; free_timeouts = q; } } /* Add a protocol to the list of protocols... */ -void add_protocol (name, fd, handler, local) - char *name; - int fd; - void (*handler)(struct protocol *); - void *local; +void +add_protocol(char *name, int fd, void (*handler)(struct protocol *), + void *local) { struct protocol *p; - p = (struct protocol *)malloc (sizeof *p); + p = (struct protocol *)malloc(sizeof *p); if (!p) - error ("can't allocate protocol struct for %s", name); - - p -> fd = fd; - p -> handler = handler; - p -> local = local; - - p -> next = protocols; + error("can't allocate protocol struct for %s", name); + p->fd = fd; + p->handler = handler; + p->local = local; + p->next = protocols; protocols = p; } -void remove_protocol (proto) - struct protocol *proto; +void +remove_protocol(struct protocol *proto) { - struct protocol *p, *next, *prev; + struct protocol *p, *next, *prev = NULL; - prev = (struct protocol *)0; for (p = protocols; p; p = next) { - next = p -> next; + next = p->next; if (p == proto) { if (prev) - prev -> next = p -> next; + prev->next = p->next; else - protocols = p -> next; - free (p); + protocols = p->next; + free(p); } } } diff --git a/usr.sbin/dhcpd/inet.c b/usr.sbin/dhcpd/inet.c index dcec0b55ff4..d59c85921af 100644 --- a/usr.sbin/dhcpd/inet.c +++ b/usr.sbin/dhcpd/inet.c @@ -1,4 +1,4 @@ -/* $OpenBSD: inet.c,v 1.2 2004/04/14 01:27:49 henning Exp $ */ +/* $OpenBSD: inet.c,v 1.3 2004/04/16 04:30:09 deraadt Exp $ */ /* * Subroutines to manipulate internet addresses in a safely portable @@ -160,11 +160,12 @@ addr_eq(struct iaddr addr1, struct iaddr addr2) return (memcmp(addr1.iabuf, addr2.iabuf, addr1.len) == 0); } -char *piaddr(struct iaddr addr) +char * +piaddr(struct iaddr addr) { static char pbuf[32]; - char *s; struct in_addr a; + char *s; memcpy(&a, &(addr.iabuf), sizeof(struct in_addr)); diff --git a/usr.sbin/dhcpd/memory.c b/usr.sbin/dhcpd/memory.c index e38a249bd12..3d814ef10a1 100644 --- a/usr.sbin/dhcpd/memory.c +++ b/usr.sbin/dhcpd/memory.c @@ -1,7 +1,3 @@ -/* memory.c - - Memory-resident database... */ - /* * Copyright (c) 1995, 1996, 1997, 1998 The Internet Software Consortium. * All rights reserved. @@ -54,128 +50,112 @@ static struct lease *dangling_leases; static struct hash_table *vendor_class_hash; static struct hash_table *user_class_hash; -void enter_host (hd) - struct host_decl *hd; +void +enter_host(struct host_decl *hd) { - struct host_decl *hp = (struct host_decl *)0; - struct host_decl *np = (struct host_decl *)0; - - hd -> n_ipaddr = (struct host_decl *)0; + struct host_decl *hp = NULL, *np = NULL; - if (hd -> interface.hlen) { + hd->n_ipaddr = NULL; + if (hd->interface.hlen) { if (!host_hw_addr_hash) - host_hw_addr_hash = new_hash (); + host_hw_addr_hash = new_hash(); else - hp = (struct host_decl *) - hash_lookup (host_hw_addr_hash, - hd -> interface.haddr, - hd -> interface.hlen); + hp = (struct host_decl *)hash_lookup(host_hw_addr_hash, + hd->interface.haddr, hd->interface.hlen); - /* If there isn't already a host decl matching this - address, add it to the hash table. */ + /* + * If there isn't already a host decl matching this + * address, add it to the hash table. + */ if (!hp) - add_hash (host_hw_addr_hash, - hd -> interface.haddr, hd -> interface.hlen, - (unsigned char *)hd); + add_hash(host_hw_addr_hash, hd->interface.haddr, + hd->interface.hlen, (unsigned char *)hd); } - /* If there was already a host declaration for this hardware - address, add this one to the end of the list. */ - + /* + * If there was already a host declaration for this hardware + * address, add this one to the end of the list. + */ if (hp) { - for (np = hp; np -> n_ipaddr; np = np -> n_ipaddr) + for (np = hp; np->n_ipaddr; np = np->n_ipaddr) ; - np -> n_ipaddr = hd; + np->n_ipaddr = hd; } - if (hd -> group -> options [DHO_DHCP_CLIENT_IDENTIFIER]) { - if (!tree_evaluate (hd -> group -> options - [DHO_DHCP_CLIENT_IDENTIFIER])) + if (hd->group->options[DHO_DHCP_CLIENT_IDENTIFIER]) { + if (!tree_evaluate(hd->group->options[DHO_DHCP_CLIENT_IDENTIFIER])) return; /* If there's no uid hash, make one; otherwise, see if there's already an entry in the hash for this host. */ if (!host_uid_hash) { - host_uid_hash = new_hash (); - hp = (struct host_decl *)0; + host_uid_hash = new_hash(); + hp = NULL; } else - hp = (struct host_decl *) hash_lookup - (host_uid_hash, - hd -> group -> options - [DHO_DHCP_CLIENT_IDENTIFIER] -> value, - hd -> group -> options - [DHO_DHCP_CLIENT_IDENTIFIER] -> len); - - /* If there's already a host declaration for this - client identifier, add this one to the end of the - list. Otherwise, add it to the hash table. */ + hp = (struct host_decl *)hash_lookup(host_uid_hash, + hd->group->options[DHO_DHCP_CLIENT_IDENTIFIER]->value, + hd->group->options[DHO_DHCP_CLIENT_IDENTIFIER]->len); + + /* + * If there's already a host declaration for this + * client identifier, add this one to the end of the + * list. Otherwise, add it to the hash table. + */ if (hp) { /* Don't link it in twice... */ if (!np) { - for (np = hp; np -> n_ipaddr; - np = np -> n_ipaddr) + for (np = hp; np->n_ipaddr; + np = np->n_ipaddr) ; - np -> n_ipaddr = hd; + np->n_ipaddr = hd; } } else { - add_hash (host_uid_hash, - hd -> group -> options - [DHO_DHCP_CLIENT_IDENTIFIER] -> value, - hd -> group -> options - [DHO_DHCP_CLIENT_IDENTIFIER] -> len, - (unsigned char *)hd); + add_hash(host_uid_hash, + hd->group->options[DHO_DHCP_CLIENT_IDENTIFIER]->value, + hd->group->options[DHO_DHCP_CLIENT_IDENTIFIER]->len, + (unsigned char *)hd); } } } -struct host_decl *find_hosts_by_haddr (htype, haddr, hlen) - int htype; - unsigned char *haddr; - int hlen; +struct host_decl * +find_hosts_by_haddr(int htype, unsigned char *haddr, int hlen) { - struct host_decl *foo; - - foo = (struct host_decl *)hash_lookup (host_hw_addr_hash, - haddr, hlen); - return foo; + return (struct host_decl *)hash_lookup(host_hw_addr_hash, + haddr, hlen); } -struct host_decl *find_hosts_by_uid (data, len) - unsigned char *data; - int len; +struct host_decl * +find_hosts_by_uid(unsigned char *data, int len) { - struct host_decl *foo; - - foo = (struct host_decl *)hash_lookup (host_uid_hash, data, len); - return foo; + return (struct host_decl *)hash_lookup(host_uid_hash, data, len); } -/* More than one host_decl can be returned by find_hosts_by_haddr or - find_hosts_by_uid, and each host_decl can have multiple addresses. - Loop through the list of hosts, and then for each host, through the - list of addresses, looking for an address that's in the same shared - network as the one specified. Store the matching address through - the addr pointer, update the host pointer to point at the host_decl - that matched, and return the subnet that matched. */ - -struct subnet *find_host_for_network (host, addr, share) - struct host_decl **host; - struct iaddr *addr; - struct shared_network *share; +/* + * More than one host_decl can be returned by find_hosts_by_haddr or + * find_hosts_by_uid, and each host_decl can have multiple addresses. + * Loop through the list of hosts, and then for each host, through the + * list of addresses, looking for an address that's in the same shared + * network as the one specified. Store the matching address through + * the addr pointer, update the host pointer to point at the host_decl + * that matched, and return the subnet that matched. + */ +struct subnet * +find_host_for_network(struct host_decl **host, struct iaddr *addr, + struct shared_network *share) { - int i; struct subnet *subnet; struct iaddr ip_address; struct host_decl *hp; + int i; - for (hp = *host; hp; hp = hp -> n_ipaddr) { - if (!hp -> fixed_addr || !tree_evaluate (hp -> fixed_addr)) + for (hp = *host; hp; hp = hp->n_ipaddr) { + if (!hp->fixed_addr || !tree_evaluate(hp->fixed_addr)) continue; - for (i = 0; i < hp -> fixed_addr -> len; i += 4) { + for (i = 0; i < hp->fixed_addr->len; i += 4) { ip_address.len = 4; - memcpy (ip_address.iabuf, - hp -> fixed_addr -> value + i, 4); - subnet = find_grouped_subnet (share, ip_address); + memcpy(ip_address.iabuf, hp->fixed_addr->value + i, 4); + subnet = find_grouped_subnet(share, ip_address); if (subnet) { *addr = ip_address; *host = hp; @@ -183,10 +163,11 @@ struct subnet *find_host_for_network (host, addr, share) } } } - return (struct subnet *)0; + return NULL; } -void new_address_range (low, high, subnet, dynamic) +void +new_address_range(low, high, subnet, dynamic) struct iaddr low, high; struct subnet *subnet; int dynamic; @@ -194,188 +175,181 @@ void new_address_range (low, high, subnet, dynamic) struct lease *address_range, *lp, *plp; struct iaddr net; int min, max, i; - char lowbuf [16], highbuf [16], netbuf [16]; - struct shared_network *share = subnet -> shared_network; + char lowbuf[16], highbuf[16], netbuf[16]; + struct shared_network *share = subnet->shared_network; struct hostent *h; struct in_addr ia; /* All subnets should have attached shared network structures. */ if (!share) { - strlcpy (netbuf, piaddr (subnet -> net), sizeof(netbuf)); - error ("No shared network for network %s (%s)", - netbuf, piaddr (subnet -> netmask)); + strlcpy(netbuf, piaddr(subnet->net), sizeof(netbuf)); + error("No shared network for network %s (%s)", + netbuf, piaddr(subnet->netmask)); } /* Initialize the hash table if it hasn't been done yet. */ if (!lease_uid_hash) - lease_uid_hash = new_hash (); + lease_uid_hash = new_hash(); if (!lease_ip_addr_hash) - lease_ip_addr_hash = new_hash (); + lease_ip_addr_hash = new_hash(); if (!lease_hw_addr_hash) - lease_hw_addr_hash = new_hash (); + lease_hw_addr_hash = new_hash(); /* Make sure that high and low addresses are in same subnet. */ - net = subnet_number (low, subnet -> netmask); - if (!addr_eq (net, subnet_number (high, subnet -> netmask))) { - strlcpy (lowbuf, piaddr (low), sizeof(lowbuf)); - strlcpy (highbuf, piaddr (high), sizeof(highbuf)); - strlcpy (netbuf, piaddr (subnet -> netmask), sizeof(netbuf)); - error ("Address range %s to %s, netmask %s spans %s!", - lowbuf, highbuf, netbuf, "multiple subnets"); + net = subnet_number(low, subnet->netmask); + if (!addr_eq(net, subnet_number(high, subnet->netmask))) { + strlcpy(lowbuf, piaddr(low), sizeof(lowbuf)); + strlcpy(highbuf, piaddr(high), sizeof(highbuf)); + strlcpy(netbuf, piaddr(subnet->netmask), sizeof(netbuf)); + error("Address range %s to %s, netmask %s spans %s!", + lowbuf, highbuf, netbuf, "multiple subnets"); } /* Make sure that the addresses are on the correct subnet. */ - if (!addr_eq (net, subnet -> net)) { - strlcpy (lowbuf, piaddr (low), sizeof(lowbuf)); - strlcpy (highbuf, piaddr (high), sizeof(highbuf)); - strlcpy (netbuf, piaddr (subnet -> netmask), sizeof(netbuf)); - error ("Address range %s to %s not on net %s/%s!", - lowbuf, highbuf, piaddr (subnet -> net), netbuf); + if (!addr_eq(net, subnet->net)) { + strlcpy(lowbuf, piaddr(low), sizeof(lowbuf)); + strlcpy(highbuf, piaddr(high), sizeof(highbuf)); + strlcpy(netbuf, piaddr(subnet->netmask), sizeof(netbuf)); + error("Address range %s to %s not on net %s/%s!", + lowbuf, highbuf, piaddr(subnet->net), netbuf); } /* Get the high and low host addresses... */ - max = host_addr (high, subnet -> netmask); - min = host_addr (low, subnet -> netmask); + max = host_addr(high, subnet->netmask); + min = host_addr(low, subnet->netmask); /* Allow range to be specified high-to-low as well as low-to-high. */ if (min > max) { max = min; - min = host_addr (high, subnet -> netmask); + min = host_addr(high, subnet->netmask); } /* Get a lease structure for each address in the range. */ - address_range = new_leases (max - min + 1, "new_address_range"); + address_range = new_leases(max - min + 1, "new_address_range"); if (!address_range) { - strlcpy (lowbuf, piaddr (low), sizeof(lowbuf)); - strlcpy (highbuf, piaddr (high), sizeof(highbuf)); - error ("No memory for address range %s-%s.", lowbuf, highbuf); + strlcpy(lowbuf, piaddr(low), sizeof(lowbuf)); + strlcpy(highbuf, piaddr(high), sizeof(highbuf)); + error("No memory for address range %s-%s.", lowbuf, highbuf); } - memset (address_range, 0, (sizeof *address_range) * (max - min + 1)); + memset(address_range, 0, (sizeof *address_range) * (max - min + 1)); /* Fill in the last lease if it hasn't been already... */ - if (!share -> last_lease) { - share -> last_lease = &address_range [0]; - } + if (!share->last_lease) + share->last_lease = &address_range[0]; /* Fill out the lease structures with some minimal information. */ for (i = 0; i < max - min + 1; i++) { - address_range [i].ip_addr = - ip_addr (subnet -> net, subnet -> netmask, i + min); - address_range [i].starts = - address_range [i].timestamp = MIN_TIME; - address_range [i].ends = MIN_TIME; - address_range [i].subnet = subnet; - address_range [i].shared_network = share; - address_range [i].flags = dynamic ? DYNAMIC_BOOTP_OK : 0; - - memcpy (&ia, address_range [i].ip_addr.iabuf, 4); - - if (subnet -> group -> get_lease_hostnames) { - h = gethostbyaddr ((char *)&ia, sizeof ia, AF_INET); + address_range[i].ip_addr = ip_addr(subnet->net, + subnet->netmask, i + min); + address_range[i].starts = address_range[i].timestamp = MIN_TIME; + address_range[i].ends = MIN_TIME; + address_range[i].subnet = subnet; + address_range[i].shared_network = share; + address_range[i].flags = dynamic ? DYNAMIC_BOOTP_OK : 0; + + memcpy(&ia, address_range[i].ip_addr.iabuf, 4); + + if (subnet->group->get_lease_hostnames) { + h = gethostbyaddr((char *)&ia, sizeof ia, AF_INET); if (!h) - warn ("No hostname for %s", inet_ntoa (ia)); + warn("No hostname for %s", inet_ntoa(ia)); else { int len = strlen(h->h_name) + 1; - address_range [i].hostname = - malloc (len); - if (!address_range [i].hostname) - error ("no memory for hostname %s.", - h -> h_name); - strlcpy (address_range [i].hostname, - h -> h_name, len); + + address_range[i].hostname = malloc(len); + if (!address_range[i].hostname) + error("no memory for hostname %s.", + h->h_name); + strlcpy(address_range[i].hostname, + h->h_name, len); } } /* Link this entry into the list. */ - address_range [i].next = share -> leases; - address_range [i].prev = (struct lease *)0; - share -> leases = &address_range [i]; - if (address_range [i].next) - address_range [i].next -> prev = share -> leases; - add_hash (lease_ip_addr_hash, - address_range [i].ip_addr.iabuf, - address_range [i].ip_addr.len, - (unsigned char *)&address_range [i]); + address_range[i].next = share->leases; + address_range[i].prev = NULL; + share->leases = &address_range[i]; + if (address_range[i].next) + address_range[i].next->prev = share->leases; + add_hash(lease_ip_addr_hash, address_range[i].ip_addr.iabuf, + address_range[i].ip_addr.len, + (unsigned char *)&address_range[i]); } /* Find out if any dangling leases are in range... */ - plp = (struct lease *)0; - for (lp = dangling_leases; lp; lp = lp -> next) { + plp = NULL; + for (lp = dangling_leases; lp; lp = lp->next) { struct iaddr lnet; int lhost; - lnet = subnet_number (lp -> ip_addr, subnet -> netmask); - lhost = host_addr (lp -> ip_addr, subnet -> netmask); + lnet = subnet_number(lp->ip_addr, subnet->netmask); + lhost = host_addr(lp->ip_addr, subnet->netmask); /* If it's in range, fill in the real lease structure with the dangling lease's values, and remove the lease from the list of dangling leases. */ - if (addr_eq (lnet, subnet -> net) && - lhost >= i && lhost <= max) { + if (addr_eq(lnet, subnet->net) && lhost >= i && lhost <= max) { if (plp) { - plp -> next = lp -> next; + plp->next = lp->next; } else { - dangling_leases = lp -> next; + dangling_leases = lp->next; } - lp -> next = (struct lease *)0; - address_range [lhost - i].hostname = lp -> hostname; - address_range [lhost - i].client_hostname = - lp -> client_hostname; - supersede_lease (&address_range [lhost - i], lp, 0); - free_lease (lp, "new_address_range"); + lp->next = NULL; + address_range[lhost - i].hostname = lp->hostname; + address_range[lhost - i].client_hostname = + lp->client_hostname; + supersede_lease(&address_range[lhost - i], lp, 0); + free_lease(lp, "new_address_range"); } else plp = lp; } } -struct subnet *find_subnet (addr) - struct iaddr addr; +struct subnet * +find_subnet(struct iaddr addr) { struct subnet *rv; - for (rv = subnets; rv; rv = rv -> next_subnet) { - if (addr_eq (subnet_number (addr, rv -> netmask), rv -> net)) + for (rv = subnets; rv; rv = rv->next_subnet) { + if (addr_eq(subnet_number(addr, rv->netmask), rv->net)) return rv; } - return (struct subnet *)0; + return NULL; } -struct subnet *find_grouped_subnet (share, addr) - struct shared_network *share; - struct iaddr addr; +struct subnet * +find_grouped_subnet(struct shared_network *share, struct iaddr addr) { struct subnet *rv; - for (rv = share -> subnets; rv; rv = rv -> next_sibling) { - if (addr_eq (subnet_number (addr, rv -> netmask), rv -> net)) + for (rv = share->subnets; rv; rv = rv->next_sibling) { + if (addr_eq(subnet_number(addr, rv->netmask), rv->net)) return rv; } - return (struct subnet *)0; + return NULL; } -int subnet_inner_than (subnet, scan, warnp) - struct subnet *subnet, *scan; - int warnp; +int +subnet_inner_than(struct subnet *subnet, struct subnet *scan, int warnp) { - if (addr_eq (subnet_number (subnet -> net, scan -> netmask), - scan -> net) || - addr_eq (subnet_number (scan -> net, subnet -> netmask), - subnet -> net)) { - char n1buf [16]; + if (addr_eq(subnet_number(subnet->net, scan->netmask), scan->net) || + addr_eq(subnet_number(scan->net, subnet->netmask), subnet->net)) { + char n1buf[16]; int i, j; + for (i = 0; i < 32; i++) - if (subnet -> netmask.iabuf [3 - (i >> 3)] - & (1 << (i & 7))) + if (subnet->netmask.iabuf[3 - (i >> 3)] & + (1 << (i & 7))) break; for (j = 0; j < 32; j++) - if (scan -> netmask.iabuf [3 - (j >> 3)] & + if (scan->netmask.iabuf[3 - (j >> 3)] & (1 << (j & 7))) break; - strlcpy (n1buf, piaddr (subnet -> net), sizeof(n1buf)); + strlcpy(n1buf, piaddr(subnet->net), sizeof(n1buf)); if (warnp) - warn ("%ssubnet %s/%d conflicts with subnet %s/%d", - "Warning: ", n1buf, 32 - i, - piaddr (scan -> net), 32 - j); + warn("%ssubnet %s/%d conflicts with subnet %s/%d", + "Warning: ", n1buf, 32 - i, + piaddr(scan->net), 32 - j); if (i < j) return 1; } @@ -383,371 +357,338 @@ int subnet_inner_than (subnet, scan, warnp) } /* Enter a new subnet into the subnet list. */ - -void enter_subnet (subnet) - struct subnet *subnet; +void +enter_subnet(struct subnet *subnet) { - struct subnet *scan, *prev = (struct subnet *)0; + struct subnet *scan, *prev = NULL; /* Check for duplicates... */ - for (scan = subnets; scan; scan = scan -> next_subnet) { - /* When we find a conflict, make sure that the - subnet with the narrowest subnet mask comes - first. */ - if (subnet_inner_than (subnet, scan, 1)) { + for (scan = subnets; scan; scan = scan->next_subnet) { + /* + * When we find a conflict, make sure that the + * subnet with the narrowest subnet mask comes + * first. + */ + if (subnet_inner_than(subnet, scan, 1)) { if (prev) { - prev -> next_subnet = subnet; + prev->next_subnet = subnet; } else subnets = subnet; - subnet -> next_subnet = scan; + subnet->next_subnet = scan; return; } prev = scan; } /* XXX use the BSD radix tree code instead of a linked list. */ - subnet -> next_subnet = subnets; + subnet->next_subnet = subnets; subnets = subnet; } /* Enter a new shared network into the shared network list. */ - -void enter_shared_network (share) - struct shared_network *share; +void +enter_shared_network(struct shared_network *share) { /* XXX Sort the nets into a balanced tree to make searching quicker. */ - share -> next = shared_networks; + share->next = shared_networks; shared_networks = share; } -/* Enter a lease into the system. This is called by the parser each - time it reads in a new lease. If the subnet for that lease has - already been read in (usually the case), just update that lease; - otherwise, allocate temporary storage for the lease and keep it around - until we're done reading in the config file. */ - -void enter_lease (lease) - struct lease *lease; +/* + * Enter a lease into the system. This is called by the parser each + * time it reads in a new lease. If the subnet for that lease has + * already been read in (usually the case), just update that lease; + * otherwise, allocate temporary storage for the lease and keep it around + * until we're done reading in the config file. + */ +void +enter_lease(struct lease *lease) { - struct lease *comp = find_lease_by_ip_addr (lease -> ip_addr); + struct lease *comp = find_lease_by_ip_addr(lease->ip_addr); - /* If we don't have a place for this lease yet, save it for - later. */ + /* If we don't have a place for this lease yet, save it for later. */ if (!comp) { - comp = new_lease ("enter_lease"); - if (!comp) { - error ("No memory for lease %s\n", - piaddr (lease -> ip_addr)); - } + comp = new_lease("enter_lease"); + if (!comp) + error("No memory for lease %s\n", + piaddr(lease->ip_addr)); *comp = *lease; - comp -> next = dangling_leases; - comp -> prev = (struct lease *)0; + comp->next = dangling_leases; + comp->prev = NULL; dangling_leases = comp; } else { /* Record the hostname information in the lease. */ - comp -> hostname = lease -> hostname; - comp -> client_hostname = lease -> client_hostname; - supersede_lease (comp, lease, 0); + comp->hostname = lease->hostname; + comp->client_hostname = lease->client_hostname; + supersede_lease(comp, lease, 0); } } -/* 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 - list of leases by expiry time so that we can always find the oldest - lease. */ - -int supersede_lease (comp, lease, commit) - struct lease *comp, *lease; - int commit; +/* + * 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 + * list of leases by expiry time so that we can always find the oldest + * lease. + */ +int +supersede_lease(struct lease *comp, struct lease *lease, int commit) { int enter_uid = 0; int enter_hwaddr = 0; struct lease *lp; /* Static leases are not currently kept in the database... */ - if (lease -> flags & STATIC_LEASE) + if (lease->flags & STATIC_LEASE) return 1; - /* If the existing lease hasn't expired and has a different - unique identifier or, if it doesn't have a unique - identifier, a different hardware address, then the two - leases are in conflict. If the existing lease has a uid - and the new one doesn't, but they both have the same - hardware address, and dynamic bootp is allowed on this - lease, then we allow that, in case a dynamic BOOTP lease is - 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))))) { - warn ("Lease conflict at %s", - piaddr (comp -> ip_addr)); + /* + * If the existing lease hasn't expired and has a different + * unique identifier or, if it doesn't have a unique + * identifier, a different hardware address, then the two + * leases are in conflict. If the existing lease has a uid + * and the new one doesn't, but they both have the same + * hardware address, and dynamic bootp is allowed on this + * lease, then we allow that, in case a dynamic BOOTP lease is + * 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))))) { + warn("Lease conflict at %s", piaddr(comp->ip_addr)); return 0; } else { /* If there's a Unique ID, dissociate it from the hash table and free it if necessary. */ - if (comp -> uid) { - uid_hash_delete (comp); + if (comp->uid) { + uid_hash_delete(comp); enter_uid = 1; - if (comp -> uid != &comp -> uid_buf [0]) { - free (comp -> uid); - comp -> uid_max = 0; - comp -> uid_len = 0; + if (comp->uid != &comp->uid_buf[0]) { + free(comp->uid); + comp->uid_max = 0; + comp->uid_len = 0; } - comp -> uid = (unsigned char *)0; + comp->uid = NULL; } else 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))) { - hw_hash_delete (comp); + 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))) { + hw_hash_delete(comp); enter_hwaddr = 1; - } else if (!comp -> hardware_addr.htype) + } else if (!comp->hardware_addr.htype) enter_hwaddr = 1; /* Copy the data files, but not the linkages. */ - comp -> starts = lease -> starts; - if (lease -> uid) { - if (lease -> uid_len < sizeof (lease -> uid_buf)) { - memcpy (comp -> uid_buf, - lease -> uid, lease -> uid_len); - comp -> uid = &comp -> uid_buf [0]; - comp -> uid_max = sizeof comp -> uid_buf; - } else if (lease -> uid != &lease -> uid_buf [0]) { - comp -> uid = lease -> uid; - comp -> uid_max = lease -> uid_max; - lease -> uid = (unsigned char *)0; - lease -> uid_max = 0; + comp->starts = lease->starts; + if (lease->uid) { + if (lease->uid_len < sizeof (lease->uid_buf)) { + memcpy(comp->uid_buf, lease->uid, lease->uid_len); + comp->uid = &comp->uid_buf[0]; + comp->uid_max = sizeof comp->uid_buf; + } else if (lease->uid != &lease->uid_buf[0]) { + comp->uid = lease->uid; + comp->uid_max = lease->uid_max; + lease->uid = NULL; + lease->uid_max = 0; } else { - error ("corrupt lease uid."); /* XXX */ + error("corrupt lease uid."); /* XXX */ } } else { - comp -> uid = (unsigned char *)0; - comp -> uid_max = 0; + comp->uid = NULL; + comp->uid_max = 0; } - comp -> uid_len = lease -> uid_len; - comp -> host = lease -> host; - comp -> hardware_addr = lease -> hardware_addr; - comp -> flags = ((lease -> flags & ~PERSISTENT_FLAGS) | - (comp -> flags & ~EPHEMERAL_FLAGS)); + comp->uid_len = lease->uid_len; + comp->host = lease->host; + comp->hardware_addr = lease->hardware_addr; + comp->flags = ((lease->flags & ~PERSISTENT_FLAGS) | + (comp->flags & ~EPHEMERAL_FLAGS)); /* Record the lease in the uid hash if necessary. */ - if (enter_uid && lease -> uid) { - uid_hash_add (comp); - } + if (enter_uid && lease->uid) + uid_hash_add(comp); /* Record it in the hardware address hash if necessary. */ - if (enter_hwaddr && lease -> hardware_addr.htype) { - hw_hash_add (comp); - } + if (enter_hwaddr && lease->hardware_addr.htype) + hw_hash_add(comp); /* Remove the lease from its current place in the timeout sequence. */ - if (comp -> prev) { - comp -> prev -> next = comp -> next; - } else { - comp -> shared_network -> leases = comp -> next; - } - if (comp -> next) { - comp -> next -> prev = comp -> prev; - } - if (comp -> shared_network -> last_lease == comp) { - comp -> shared_network -> last_lease = comp -> prev; - } + if (comp->prev) + comp->prev->next = comp->next; + else + comp->shared_network->leases = comp->next; + if (comp->next) + comp->next->prev = comp->prev; + if (comp->shared_network->last_lease == comp) + comp->shared_network->last_lease = comp->prev; /* Find the last insertion point... */ - if (comp == comp -> shared_network -> insertion_point || - !comp -> shared_network -> insertion_point) { - lp = comp -> shared_network -> leases; - } else { - lp = comp -> shared_network -> insertion_point; - } + if (comp == comp->shared_network->insertion_point || + !comp->shared_network->insertion_point) + lp = comp->shared_network->leases; + else + lp = comp->shared_network->insertion_point; if (!lp) { /* Nothing on the list yet? Just make comp the head of the list. */ - comp -> shared_network -> leases = comp; - comp -> shared_network -> last_lease = comp; - } else if (lp -> ends > lease -> ends) { + comp->shared_network->leases = comp; + comp->shared_network->last_lease = comp; + } else if (lp->ends > lease->ends) { /* Skip down the list until we run out of list or find a place for comp. */ - while (lp -> next && lp -> ends > lease -> ends) { - lp = lp -> next; + while (lp->next && lp->ends > lease->ends) { + lp = lp->next; } - if (lp -> ends > lease -> ends) { + if (lp->ends > lease->ends) { /* If we ran out of list, put comp at the end. */ - lp -> next = comp; - comp -> prev = lp; - comp -> next = (struct lease *)0; - comp -> shared_network -> last_lease = comp; + lp->next = comp; + comp->prev = lp; + comp->next = NULL; + comp->shared_network->last_lease = comp; } else { /* If we didn't, put it between lp and the previous item on the list. */ - if ((comp -> prev = lp -> prev)) - comp -> prev -> next = comp; - comp -> next = lp; - lp -> prev = comp; + if ((comp->prev = lp->prev)) + comp->prev->next = comp; + comp->next = lp; + lp->prev = comp; } } else { /* Skip up the list until we run out of list or find a place for comp. */ - while (lp -> prev && lp -> ends < lease -> ends) { - lp = lp -> prev; + while (lp->prev && lp->ends < lease->ends) { + lp = lp->prev; } - if (lp -> ends < lease -> ends) { + if (lp->ends < lease->ends) { /* If we ran out of list, put comp at the beginning. */ - lp -> prev = comp; - comp -> next = lp; - comp -> prev = (struct lease *)0; - comp -> shared_network -> leases = comp; + lp->prev = comp; + comp->next = lp; + comp->prev = NULL; + comp->shared_network->leases = comp; } else { /* If we didn't, put it between lp and the next item on the list. */ - if ((comp -> next = lp -> next)) - comp -> next -> prev = comp; - comp -> prev = lp; - lp -> next = comp; + if ((comp->next = lp->next)) + comp->next->prev = comp; + comp->prev = lp; + lp->next = comp; } } - comp -> shared_network -> insertion_point = comp; - comp -> ends = lease -> ends; + comp->shared_network->insertion_point = comp; + comp->ends = lease->ends; } /* Return zero if we didn't commit the lease to permanent storage; nonzero if we did. */ - return commit && write_lease (comp) && commit_leases (); + return commit && write_lease(comp) && commit_leases(); } /* Release the specified lease and re-hash it as appropriate. */ -void release_lease (lease) - struct lease *lease; +void +release_lease(struct lease *lease) { struct lease lt; lt = *lease; if (lt.ends > cur_time) { lt.ends = cur_time; - supersede_lease (lease, <, 1); - note ("Released lease for IP address %s", - piaddr (lease -> ip_addr)); + supersede_lease(lease, <, 1); + note("Released lease for IP address %s", + piaddr(lease->ip_addr)); } } -/* Abandon the specified lease for the specified time. sets it's - particulars to zero, the end time apropriately and re-hash it as - appropriate. abandons permanently if abtime is 0 */ - -void abandon_lease (lease, message) - struct lease *lease; - char *message; +/* + * Abandon the specified lease for the specified time. sets it's + * particulars to zero, the end time apropriately and re-hash it as + * appropriate. abandons permanently if abtime is 0 + */ +void +abandon_lease(struct lease *lease, char *message) { struct lease lt; time_t abtime; - abtime = lease -> subnet -> group -> default_lease_time; - lease -> flags |= ABANDONED_LEASE; + abtime = lease->subnet->group->default_lease_time; + lease->flags |= ABANDONED_LEASE; lt = *lease; lt.ends = cur_time + abtime; - warn ("Abandoning IP address %s for %d seconds: %s", - piaddr (lease -> ip_addr), abtime, message); + warn("Abandoning IP address %s for %d seconds: %s", + piaddr(lease->ip_addr), abtime, message); lt.hardware_addr.htype = 0; lt.hardware_addr.hlen = 0; - lt.uid = (unsigned char *)0; + lt.uid = NULL; lt.uid_len = 0; - supersede_lease (lease, <, 1); + supersede_lease(lease, <, 1); } /* Locate the lease associated with a given IP address... */ - -struct lease *find_lease_by_ip_addr (addr) - struct iaddr addr; +struct lease * +find_lease_by_ip_addr(struct iaddr addr) { - struct lease *lease = (struct lease *)hash_lookup (lease_ip_addr_hash, - addr.iabuf, - addr.len); - return lease; + return (struct lease *)hash_lookup(lease_ip_addr_hash, + addr.iabuf, addr.len); } -struct lease *find_lease_by_uid (uid, len) - unsigned char *uid; - int len; +struct lease *find_lease_by_uid(unsigned char *uid, int len) { - struct lease *lease = (struct lease *)hash_lookup (lease_uid_hash, - uid, len); - return lease; + return (struct lease *)hash_lookup(lease_uid_hash, uid, len); } -struct lease *find_lease_by_hw_addr (hwaddr, hwlen) - unsigned char *hwaddr; - int hwlen; +struct lease * +find_lease_by_hw_addr(unsigned char *hwaddr, int hwlen) { - struct lease *lease = (struct lease *)hash_lookup (lease_hw_addr_hash, - hwaddr, hwlen); - return lease; + return (struct lease *)hash_lookup(lease_hw_addr_hash, hwaddr, hwlen); } /* Add the specified lease to the uid hash. */ - -void uid_hash_add (lease) - struct lease *lease; +void +uid_hash_add(struct lease *lease) { - struct lease *head = - find_lease_by_uid (lease -> uid, lease -> uid_len); + struct lease *head = find_lease_by_uid(lease->uid, lease->uid_len); struct lease *scan; -#ifdef DEBUG - if (lease -> n_uid) - abort (); -#endif - /* If it's not in the hash, just add it. */ if (!head) - add_hash (lease_uid_hash, lease -> uid, - lease -> uid_len, (unsigned char *)lease); + add_hash(lease_uid_hash, lease->uid, + lease->uid_len, (unsigned char *)lease); else { /* Otherwise, attach it to the end of the list. */ - for (scan = head; scan -> n_uid; scan = scan -> n_uid) -#ifdef DEBUG - if (scan == lease) - abort () -#endif - ; - scan -> n_uid = lease; + for (scan = head; scan->n_uid; scan = scan->n_uid) + ; + scan->n_uid = lease; } } /* Delete the specified lease from the uid hash. */ - -void uid_hash_delete (lease) - struct lease *lease; +void +uid_hash_delete(struct lease *lease) { - struct lease *head = - find_lease_by_uid (lease -> uid, lease -> uid_len); + struct lease *head = find_lease_by_uid(lease->uid, lease->uid_len); struct lease *scan; /* If it's not in the hash, we have no work to do. */ if (!head) { - lease -> n_uid = (struct lease *)0; + lease->n_uid = NULL; return; } @@ -755,64 +696,56 @@ void uid_hash_delete (lease) remove the hash table entry and add a new one with the next lease on the list (if there is one). */ if (head == lease) { - delete_hash_entry (lease_uid_hash, - lease -> uid, lease -> uid_len); - if (lease -> n_uid) - add_hash (lease_uid_hash, - lease -> n_uid -> uid, - lease -> n_uid -> uid_len, - (unsigned char *)(lease -> n_uid)); + delete_hash_entry(lease_uid_hash, lease->uid, lease->uid_len); + if (lease->n_uid) + add_hash(lease_uid_hash, lease->n_uid->uid, + lease->n_uid->uid_len, + (unsigned char *)(lease->n_uid)); } else { /* Otherwise, look for the lease in the list of leases attached to the hash table entry, and remove it if we find it. */ - for (scan = head; scan -> n_uid; scan = scan -> n_uid) { - if (scan -> n_uid == lease) { - scan -> n_uid = scan -> n_uid -> n_uid; + for (scan = head; scan->n_uid; scan = scan->n_uid) { + if (scan->n_uid == lease) { + scan->n_uid = scan->n_uid->n_uid; break; } } } - lease -> n_uid = (struct lease *)0; + lease->n_uid = NULL; } /* Add the specified lease to the hardware address hash. */ - -void hw_hash_add (lease) - struct lease *lease; +void +hw_hash_add(struct lease *lease) { - struct lease *head = - find_lease_by_hw_addr (lease -> hardware_addr.haddr, - lease -> hardware_addr.hlen); + struct lease *head = find_lease_by_hw_addr(lease->hardware_addr.haddr, + lease->hardware_addr.hlen); struct lease *scan; /* If it's not in the hash, just add it. */ if (!head) - add_hash (lease_hw_addr_hash, - lease -> hardware_addr.haddr, - lease -> hardware_addr.hlen, - (unsigned char *)lease); + add_hash(lease_hw_addr_hash, lease->hardware_addr.haddr, + lease->hardware_addr.hlen, (unsigned char *)lease); else { /* Otherwise, attach it to the end of the list. */ - for (scan = head; scan -> n_hw; scan = scan -> n_hw) + for (scan = head; scan->n_hw; scan = scan->n_hw) ; - scan -> n_hw = lease; + scan->n_hw = lease; } } /* Delete the specified lease from the hardware address hash. */ - -void hw_hash_delete (lease) - struct lease *lease; +void +hw_hash_delete(struct lease *lease) { - struct lease *head = - find_lease_by_hw_addr (lease -> hardware_addr.haddr, - lease -> hardware_addr.hlen); + struct lease *head = find_lease_by_hw_addr(lease->hardware_addr.haddr, + lease->hardware_addr.hlen); struct lease *scan; /* If it's not in the hash, we have no work to do. */ if (!head) { - lease -> n_hw = (struct lease *)0; + lease->n_hw = NULL; return; } @@ -820,123 +753,115 @@ void hw_hash_delete (lease) remove the hash table entry and add a new one with the next lease on the list (if there is one). */ if (head == lease) { - delete_hash_entry (lease_hw_addr_hash, - lease -> hardware_addr.haddr, - lease -> hardware_addr.hlen); - if (lease -> n_hw) - add_hash (lease_hw_addr_hash, - lease -> n_hw -> hardware_addr.haddr, - lease -> n_hw -> hardware_addr.hlen, - (unsigned char *)(lease -> n_hw)); + delete_hash_entry(lease_hw_addr_hash, + lease->hardware_addr.haddr, lease->hardware_addr.hlen); + if (lease->n_hw) + add_hash(lease_hw_addr_hash, + lease->n_hw->hardware_addr.haddr, + lease->n_hw->hardware_addr.hlen, + (unsigned char *)(lease->n_hw)); } else { - /* Otherwise, look for the lease in the list of leases - attached to the hash table entry, and remove it if - we find it. */ - for (scan = head; scan -> n_hw; scan = scan -> n_hw) { - if (scan -> n_hw == lease) { - scan -> n_hw = scan -> n_hw -> n_hw; + /* + * Otherwise, look for the lease in the list of leases + * attached to the hash table entry, and remove it if + * we find it. + */ + for (scan = head; scan->n_hw; scan = scan->n_hw) { + if (scan->n_hw == lease) { + scan->n_hw = scan->n_hw->n_hw; break; } } } - lease -> n_hw = (struct lease *)0; + lease->n_hw = NULL; } -struct class *add_class (type, name) - int type; - char *name; +struct class * +add_class(int type, char *name) { - struct class *class = new_class ("add_class"); - char *tname = (char *)malloc (strlen (name) + 1); + struct class *class = new_class("add_class"); + char *tname = malloc(strlen(name) + 1); if (!vendor_class_hash) - vendor_class_hash = new_hash (); + vendor_class_hash = new_hash(); if (!user_class_hash) - user_class_hash = new_hash (); + user_class_hash = new_hash(); if (!tname || !class || !vendor_class_hash || !user_class_hash) - return (struct class *)0; + return NULL; - memset (class, 0, sizeof *class); - strlcpy (tname, name, strlen(name) + 1); - class -> name = tname; + memset(class, 0, sizeof *class); + strlcpy(tname, name, strlen(name) + 1); + class->name = tname; if (type) - add_hash (user_class_hash, - (unsigned char *)tname, strlen (tname), - (unsigned char *)class); + add_hash(user_class_hash, (unsigned char *)tname, + strlen(tname), (unsigned char *)class); else - add_hash (vendor_class_hash, - (unsigned char *)tname, strlen (tname), - (unsigned char *)class); + add_hash(vendor_class_hash, (unsigned char *)tname, + strlen(tname), (unsigned char *)class); return class; } -struct class *find_class (type, name, len) - int type; - unsigned char *name; - int len; +struct class * +find_class(int type, unsigned char *name, int len) { - struct class *class = - (struct class *)hash_lookup (type - ? user_class_hash - : vendor_class_hash, name, len); - return class; + return (struct class *)hash_lookup(type ? user_class_hash : + vendor_class_hash, name, len); } -struct group *clone_group (group, caller) - struct group *group; - char *caller; +struct group * +clone_group(struct group *group, char *caller) { - struct group *g = new_group (caller); + struct group *g = new_group(caller); if (!g) - error ("%s: can't allocate new group", caller); + error("%s: can't allocate new group", caller); *g = *group; return g; } /* Write all interesting leases to permanent storage. */ -void write_leases () +void +write_leases(void) { struct lease *l; struct shared_network *s; - for (s = shared_networks; s; s = s -> next) { - for (l = s -> leases; l; l = l -> next) { - if (l -> hardware_addr.hlen || - l -> uid_len || - (l -> flags & ABANDONED_LEASE)) - if (!write_lease (l)) - error ("Can't rewrite lease database"); + for (s = shared_networks; s; s = s->next) { + for (l = s->leases; l; l = l->next) { + if (l->hardware_addr.hlen || l->uid_len || + (l->flags & ABANDONED_LEASE)) + if (!write_lease(l)) + error("Can't rewrite lease database"); } } - if (!commit_leases ()) - error ("Can't commit leases to new database: %m"); + if (!commit_leases()) + error("Can't commit leases to new database: %m"); } -void dump_subnets () +void +dump_subnets(void) { struct lease *l; struct shared_network *s; struct subnet *n; note ("Subnets:"); - for (n = subnets; n; n = n -> next_subnet) { - debug (" Subnet %s", piaddr (n -> net)); - debug (" netmask %s", - piaddr (n -> netmask)); + for (n = subnets; n; n = n->next_subnet) { + debug(" Subnet %s", piaddr(n->net)); + debug(" netmask %s", piaddr(n->netmask)); } - note ("Shared networks:"); - for (s = shared_networks; s; s = s -> next) { - note (" %s", s -> name); - for (l = s -> leases; l; l = l -> next) { - print_lease (l); + note("Shared networks:"); + for (s = shared_networks; s; s = s->next) { + note(" %s", s->name); + for (l = s->leases; l; l = l->next) { + print_lease(l); } - if (s -> last_lease) { - debug (" Last Lease:"); - print_lease (s -> last_lease); + if (s->last_lease) { + debug(" Last Lease:"); + print_lease(s->last_lease); } } } diff --git a/usr.sbin/dhcpd/print.c b/usr.sbin/dhcpd/print.c index 9ac209e2280..226a59ac91e 100644 --- a/usr.sbin/dhcpd/print.c +++ b/usr.sbin/dhcpd/print.c @@ -1,4 +1,4 @@ -/* $Id: print.c,v 1.3 2004/04/15 22:22:21 hshoexer Exp $ */ +/* $Id: print.c,v 1.4 2004/04/16 04:30:09 deraadt Exp $ */ /* Turn data structures into printable text. */ @@ -45,9 +45,9 @@ char * print_hw_addr(int htype, int hlen, unsigned char *data) { - static char habuf[49]; - char *s; - int i; + static char habuf[49]; + char *s; + int i; if (htype == 0 || hlen == 0) { goto bad; @@ -76,8 +76,8 @@ bad: void print_lease(struct lease *lease) { - struct tm *t; - char tbuf[32]; + struct tm *t; + char tbuf[32]; debug(" Lease %s", piaddr(lease->ip_addr)); @@ -123,7 +123,7 @@ dump_packet(struct packet *tp) debug("filename = %s", tdp->file); debug("server_name = %s", tdp->sname); if (tp->options_valid) { - int i; + int i; for (i = 0; i < 256; i++) { if (tp->options[i].data) @@ -138,10 +138,8 @@ dump_packet(struct packet *tp) void dump_raw(unsigned char *buf, int len) { - int i, j; - char lbuf[80]; - int llen = sizeof(lbuf); - int lbix = 0; + char lbuf[80]; + int i, j, llen = sizeof(lbuf), lbix = 0; lbuf[0] = 0; @@ -170,8 +168,8 @@ dump_raw(unsigned char *buf, int len) void hash_dump(struct hash_table *table) { - int i; struct hash_bucket *bp; + int i; if (!table) return; diff --git a/usr.sbin/dhcpd/tree.c b/usr.sbin/dhcpd/tree.c index d19b7981fe3..fe8bce94c86 100644 --- a/usr.sbin/dhcpd/tree.c +++ b/usr.sbin/dhcpd/tree.c @@ -1,4 +1,4 @@ -/* $Id: tree.c,v 1.5 2004/04/15 21:47:13 hshoexer Exp $ */ +/* $Id: tree.c,v 1.6 2004/04/16 04:30:09 deraadt Exp $ */ /* Routines for manipulating parse trees... */ @@ -53,7 +53,7 @@ cons(caddr_t car, pair cdr) { pair foo = (pair)dmalloc(sizeof *foo, "cons"); if (!foo) - error ("no memory for cons."); + error("no memory for cons."); foo->car = car; foo->cdr = cdr; return foo; @@ -138,6 +138,7 @@ tree_concat(struct tree *left, struct tree *right) if (left->op == TREE_CONST && right->op == TREE_CONST) { unsigned char *buf = dmalloc(left->data.const_val.len + right->data.const_val.len, "tree_concat"); + if (!buf) error("No memory to concatenate constants."); memcpy(buf, left->data.const_val.data, |