From: krw Date: Sun, 28 Jan 2018 23:12:36 +0000 (+0000) Subject: Refactor and simplify the logic to select and invoke the X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=f7f48a103d87a0cfc0a1542d0fc2fbb004523f3d;p=openbsd Refactor and simplify the logic to select and invoke the appropriate function to process a packet. --- diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c index f912b65e883..3b235257de6 100644 --- a/sbin/dhclient/dhclient.c +++ b/sbin/dhclient/dhclient.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dhclient.c,v 1.546 2018/01/28 11:29:30 krw Exp $ */ +/* $OpenBSD: dhclient.c,v 1.547 2018/01/28 23:12:36 krw Exp $ */ /* * Copyright 2004 Henning Brauer @@ -154,6 +154,7 @@ void send_discover(struct interface_info *); void send_request(struct interface_info *); void send_decline(struct interface_info *); +void process_offer(struct interface_info *, struct option_data *); void bind_lease(struct interface_info *); void make_discover(struct interface_info *, struct client_lease *); @@ -831,19 +832,40 @@ state_selecting(struct interface_info *ifi) } void -dhcpoffer(struct interface_info *ifi, struct option_data *options, char *info) +dhcpoffer(struct interface_info *ifi, struct option_data *options, + const char *src) { - struct client_lease *lease; - time_t cur_time, stop_selecting; + if (ifi->state != S_SELECTING) { + DPRINTF("%s: unexpected DHCPOFFER from %s - state #%d", + log_procname, src, ifi->state); + return; + } + + log_info("%s: DHCPOFFER from %s", log_procname, src); + process_offer(ifi, options); +} +void +bootreply(struct interface_info *ifi, struct option_data *options, + const char *src) +{ if (ifi->state != S_SELECTING) { - DPRINTF("%s: unexpected %s - state #%d", log_procname, info, - ifi->state); + DPRINTF("%s: unexpected BOOTREPLY from %s - state #%d", + log_procname, src, ifi->state); return; } + log_info("%s: BOOTREPLY from %s", log_procname, src); + process_offer(ifi, options); +} + +void +process_offer(struct interface_info *ifi, struct option_data *options) +{ + struct client_lease *lease; + time_t cur_time, stop_selecting; + time(&cur_time); - log_info("%s: %s", log_procname, info); lease = packet_to_lease(ifi, options); if (lease != NULL) { @@ -873,7 +895,8 @@ dhcpoffer(struct interface_info *ifi, struct option_data *options, char *info) } void -dhcpack(struct interface_info *ifi, struct option_data *options, char *info) +dhcpack(struct interface_info *ifi, struct option_data *options, + const char *src) { struct client_lease *lease; @@ -881,12 +904,12 @@ dhcpack(struct interface_info *ifi, struct option_data *options, char *info) ifi->state != S_REQUESTING && ifi->state != S_RENEWING && ifi->state != S_REBINDING) { - DPRINTF("%s: unexpected %s - state #%d", log_procname, info, - ifi->state); + DPRINTF("%s: unexpected DHCPACK from %s - state #%d", + log_procname, src, ifi->state); return; } - log_info("%s: %s", log_procname, info); + log_info("%s: DHCPACK from %s", log_procname, src); lease = packet_to_lease(ifi, options); if (lease == NULL) { @@ -906,24 +929,24 @@ dhcpack(struct interface_info *ifi, struct option_data *options, char *info) } void -dhcpnak(struct interface_info *ifi, struct option_data *options, char *info) +dhcpnak(struct interface_info *ifi, const char *src) { if (ifi->state != S_REBOOTING && ifi->state != S_REQUESTING && ifi->state != S_RENEWING && ifi->state != S_REBINDING) { - DPRINTF("%s: unexpected %s - state #%d", log_procname, info, - ifi->state); + DPRINTF("%s: unexpected DHCPNAK from %s - state #%d", + log_procname, src, ifi->state); return; } if (ifi->active == NULL) { - DPRINTF("%s: unexpected %s - no active lease", log_procname, - info); + DPRINTF("%s: unexpected DHCPNAK from %s - no active lease", + log_procname, src); return; } - log_info("%s: %s", log_procname, info); + log_info("%s: DHCPNAK from %s", log_procname, src); delete_address(ifi->active->address); /* XXX Do we really want to remove a NAK'd lease from the database? */ diff --git a/sbin/dhclient/dhcpd.h b/sbin/dhclient/dhcpd.h index dd90342bb8f..99763ef0dd5 100644 --- a/sbin/dhclient/dhcpd.h +++ b/sbin/dhclient/dhcpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dhcpd.h,v 1.246 2018/01/25 15:43:51 krw Exp $ */ +/* $OpenBSD: dhcpd.h,v 1.247 2018/01/28 23:12:36 krw Exp $ */ /* * Copyright (c) 2004 Henning Brauer @@ -219,9 +219,12 @@ extern int cmd_opts; #define OPT_FOREGROUND 4 void dhcpoffer(struct interface_info *, struct option_data *, - char *); -void dhcpack(struct interface_info *, struct option_data *,char *); -void dhcpnak(struct interface_info *, struct option_data *,char *); + const char *); +void dhcpack(struct interface_info *, struct option_data *, + const char *); +void dhcpnak(struct interface_info *, const char *); +void bootreply(struct interface_info *, struct option_data *, + const char *); void free_client_lease(struct client_lease *); void routehandler(struct interface_info *, int); diff --git a/sbin/dhclient/dispatch.c b/sbin/dhclient/dispatch.c index 3a9e6582c74..fed4b49d2aa 100644 --- a/sbin/dhclient/dispatch.c +++ b/sbin/dhclient/dispatch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dispatch.c,v 1.148 2018/01/25 15:43:51 krw Exp $ */ +/* $OpenBSD: dispatch.c,v 1.149 2018/01/28 23:12:36 krw Exp $ */ /* * Copyright 2004 Henning Brauer @@ -181,10 +181,8 @@ packethandler(struct interface_info *ifi) struct dhcp_packet *packet = &ifi->recv_packet; struct reject_elem *ap; struct option_data *options; - char *type, *info; + char *src; ssize_t result; - void (*handler)(struct interface_info *, - struct option_data *, char *); int i, rslt; result = receive_packet(ifi, &from, &hfrom); @@ -242,48 +240,36 @@ packethandler(struct interface_info *ifi) return; } - type = ""; - handler = NULL; + rslt = asprintf(&src, "%s (%s)",inet_ntoa(ifrom), ether_ntoa(&hfrom)); + if (rslt == -1) + fatal("src"); i = DHO_DHCP_MESSAGE_TYPE; if (options[i].data != NULL) { /* Always try a DHCP packet, even if a bad option was seen. */ switch (options[i].data[0]) { case DHCPOFFER: - handler = dhcpoffer; - type = "DHCPOFFER"; + dhcpoffer(ifi, options, src); break; case DHCPNAK: - handler = dhcpnak; - type = "DHCPNACK"; + dhcpnak(ifi, src); break; case DHCPACK: - handler = dhcpack; - type = "DHCPACK"; + dhcpack(ifi, options, src); break; default: DPRINTF("%s: discarding DHCP packet of unknown type " "(%d)", log_procname, options[i].data[0]); - return; + break; } } else if (packet->op == BOOTREPLY) { - handler = dhcpoffer; - type = "BOOTREPLY"; + bootreply(ifi, options, src); } else { DPRINTF("%s: discarding packet which is neither DHCP nor " "BOOTP", log_procname); - return; } - rslt = asprintf(&info, "%s from %s (%s)", type, inet_ntoa(ifrom), - ether_ntoa(&hfrom)); - if (rslt == -1) - fatal("info string"); - - if (handler != NULL) - (*handler)(ifi, options, info); - - free(info); + free(src); } /*