Skip to content

Commit

Permalink
Convert DHCP4 fsm state to enum to catch all states
Browse files Browse the repository at this point in the history
Signed-off-by: Olaf Hering <olaf@aepfle.de>
  • Loading branch information
olafhering committed Sep 2, 2014
1 parent 15e30cd commit 74dd52e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
5 changes: 2 additions & 3 deletions dhcp4/dhcp.h
Expand Up @@ -16,7 +16,7 @@
#include "netinfo_priv.h"
#include "buffer.h"

enum {
enum fsm_state {
NI_DHCP4_STATE_INIT,
NI_DHCP4_STATE_SELECTING,
NI_DHCP4_STATE_REQUESTING,
Expand All @@ -25,7 +25,6 @@ enum {
NI_DHCP4_STATE_RENEWING,
NI_DHCP4_STATE_REBINDING,
NI_DHCP4_STATE_REBOOT,
NI_DHCP4_STATE_RELEASED,

__NI_DHCP4_STATE_MAX,
};
Expand All @@ -42,7 +41,7 @@ typedef struct ni_dhcp4_device {
ni_linkinfo_t link;

struct {
int state;
enum fsm_state state;
const ni_timer_t * timer;
} fsm;

Expand Down
40 changes: 28 additions & 12 deletions dhcp4/fsm.c
Expand Up @@ -31,7 +31,7 @@
#define NAK_BACKOFF_MAX 60 /* seconds */

static int ni_dhcp4_fsm_arp_validate(ni_dhcp4_device_t *);
static const char * ni_dhcp4_fsm_state_name(int);
static const char * ni_dhcp4_fsm_state_name(enum fsm_state);

static int ni_dhcp4_process_offer(ni_dhcp4_device_t *, ni_addrconf_lease_t *);
static int ni_dhcp4_process_ack(ni_dhcp4_device_t *, ni_addrconf_lease_t *);
Expand Down Expand Up @@ -519,9 +519,8 @@ ni_dhcp4_fsm_timeout(ni_dhcp4_device_t *dev)
ni_dhcp4_fsm_commit_lease(dev, NULL);
ni_dhcp4_fsm_set_timeout(dev, 10);
break;

default:
;
case __NI_DHCP4_STATE_MAX:
break;
}
}

Expand Down Expand Up @@ -570,8 +569,13 @@ ni_dhcp4_fsm_link_up(ni_dhcp4_device_t *dev)
else
ni_dhcp4_fsm_discover(dev);
break;

default:
case NI_DHCP4_STATE_SELECTING:
case NI_DHCP4_STATE_REQUESTING:
case NI_DHCP4_STATE_VALIDATING:
case NI_DHCP4_STATE_RENEWING:
case NI_DHCP4_STATE_REBINDING:
break;
case __NI_DHCP4_STATE_MAX:
break;
}
}
Expand All @@ -591,8 +595,13 @@ ni_dhcp4_fsm_link_down(ni_dhcp4_device_t *dev)
ni_dhcp4_device_drop_lease(dev);
ni_dhcp4_fsm_restart(dev);
break;

default: ;
case NI_DHCP4_STATE_BOUND:
case NI_DHCP4_STATE_REBOOT:
case NI_DHCP4_STATE_RENEWING:
case NI_DHCP4_STATE_REBINDING:
break;
case __NI_DHCP4_STATE_MAX:
break;
}
}

Expand Down Expand Up @@ -978,10 +987,18 @@ ni_dhcp4_process_nak(ni_dhcp4_device_t *dev)
/* RFC says discard NAKs received in state BOUND */
return 0;

default:
case NI_DHCP4_STATE_INIT:
case NI_DHCP4_STATE_SELECTING:
case NI_DHCP4_STATE_REQUESTING:
case NI_DHCP4_STATE_VALIDATING:
case NI_DHCP4_STATE_RENEWING:
case NI_DHCP4_STATE_REBINDING:
case NI_DHCP4_STATE_REBOOT:
/* FIXME: how do we handle a NAK response to an INFORM? */
ni_dhcp4_device_drop_lease(dev);
break;
case __NI_DHCP4_STATE_MAX:
break;
}

/* Move back to state INIT */
Expand Down Expand Up @@ -1028,15 +1045,14 @@ static const char *__dhcp4_state_name[__NI_DHCP4_STATE_MAX] = {
[NI_DHCP4_STATE_RENEWING] = "RENEWING",
[NI_DHCP4_STATE_REBINDING] = "REBINDING",
[NI_DHCP4_STATE_REBOOT] = "REBOOT",
[NI_DHCP4_STATE_RELEASED] = "RELEASED",
};

const char *
ni_dhcp4_fsm_state_name(int state)
ni_dhcp4_fsm_state_name(enum fsm_state state)
{
const char *name = NULL;

if (0 <= state && state < __NI_DHCP4_STATE_MAX)
if (state < __NI_DHCP4_STATE_MAX)
name = __dhcp4_state_name[state];
return name? name : "UNKNOWN STATE";
}

0 comments on commit 74dd52e

Please sign in to comment.