diff options
author | Kenneth R Westerback <krw@cvs.openbsd.org> | 2018-01-28 23:12:37 +0000 |
---|---|---|
committer | Kenneth R Westerback <krw@cvs.openbsd.org> | 2018-01-28 23:12:37 +0000 |
commit | 60d57d10ec62f3cb932aa2e8fc86d5b26c3e117d (patch) | |
tree | bf4688d80b006928240b3a2f845a93e8c3938b5f /sbin/dhclient | |
parent | 74a0c8c07ea6d80b33dba9a4063557cc010cd693 (diff) |
Refactor and simplify the logic to select and invoke the
appropriate function to process a packet.
Diffstat (limited to 'sbin/dhclient')
-rw-r--r-- | sbin/dhclient/dhclient.c | 57 | ||||
-rw-r--r-- | sbin/dhclient/dhcpd.h | 11 | ||||
-rw-r--r-- | sbin/dhclient/dispatch.c | 36 |
3 files changed, 58 insertions, 46 deletions
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 <henning@openbsd.org> @@ -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 <henning@openbsd.org> @@ -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 <henning@openbsd.org> @@ -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 = "<unknown>"; - 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); } /* |