diff options
author | Umut Tezduyar Lindskog <umut.tezduyar@axis.com> | 2014-01-20 09:58:41 +0100 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2014-01-20 14:01:14 +0100 |
commit | 8c8f0fb9682bf8b4a6fc598b7243e193a98d1cbd (patch) | |
tree | f0c55b1a0556b064a36319488b539d8929e7b745 /src/libsystemd | |
parent | 67d6621059085963a2a908a3ea99ced3b0ca789e (diff) |
sd-dhcp-client: refactor state machine check
Diffstat (limited to 'src/libsystemd')
-rw-r--r-- | src/libsystemd/sd-dhcp-client.c | 147 |
1 files changed, 36 insertions, 111 deletions
diff --git a/src/libsystemd/sd-dhcp-client.c b/src/libsystemd/sd-dhcp-client.c index a057852e36..3b7b9f4ccd 100644 --- a/src/libsystemd/sd-dhcp-client.c +++ b/src/libsystemd/sd-dhcp-client.c @@ -33,6 +33,15 @@ #define DHCP_CLIENT_MIN_OPTIONS_SIZE 312 +#define client_state_machine_check(s, r) \ + do { \ + if (s != DHCP_STATE_BOUND && \ + s != DHCP_STATE_RENEWING && \ + s != DHCP_STATE_REBINDING) { \ + return (r); \ + } \ + } while (false) + struct DHCPLease { uint32_t t1; uint32_t t2; @@ -166,21 +175,9 @@ int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr) { assert_return(client, -EINVAL); assert_return(addr, -EINVAL); - switch (client->state) { - case DHCP_STATE_INIT: - case DHCP_STATE_SELECTING: - case DHCP_STATE_INIT_REBOOT: - case DHCP_STATE_REBOOTING: - case DHCP_STATE_REQUESTING: - return -EADDRNOTAVAIL; - - case DHCP_STATE_BOUND: - case DHCP_STATE_RENEWING: - case DHCP_STATE_REBINDING: - addr->s_addr = client->lease->address; + client_state_machine_check (client->state, -EADDRNOTAVAIL); - break; - } + addr->s_addr = client->lease->address; return 0; } @@ -189,24 +186,12 @@ int sd_dhcp_client_get_mtu(sd_dhcp_client *client, uint16_t *mtu) { assert_return(client, -EINVAL); assert_return(mtu, -EINVAL); - switch (client->state) { - case DHCP_STATE_INIT: - case DHCP_STATE_SELECTING: - case DHCP_STATE_INIT_REBOOT: - case DHCP_STATE_REBOOTING: - case DHCP_STATE_REQUESTING: - return -EADDRNOTAVAIL; - - case DHCP_STATE_BOUND: - case DHCP_STATE_RENEWING: - case DHCP_STATE_REBINDING: - if (client->lease->mtu) - *mtu = client->lease->mtu; - else - return -ENOENT; + client_state_machine_check (client->state, -EADDRNOTAVAIL); - break; - } + if (client->lease->mtu) + *mtu = client->lease->mtu; + else + return -ENOENT; return 0; } @@ -216,25 +201,13 @@ int sd_dhcp_client_get_dns(sd_dhcp_client *client, struct in_addr **addr, size_t assert_return(addr, -EINVAL); assert_return(addr_size, -EINVAL); - switch (client->state) { - case DHCP_STATE_INIT: - case DHCP_STATE_SELECTING: - case DHCP_STATE_INIT_REBOOT: - case DHCP_STATE_REBOOTING: - case DHCP_STATE_REQUESTING: - return -EADDRNOTAVAIL; - - case DHCP_STATE_BOUND: - case DHCP_STATE_RENEWING: - case DHCP_STATE_REBINDING: - if (client->lease->dns_size) { - *addr_size = client->lease->dns_size; - *addr = client->lease->dns; - } else - return -ENOENT; + client_state_machine_check (client->state, -EADDRNOTAVAIL); - break; - } + if (client->lease->dns_size) { + *addr_size = client->lease->dns_size; + *addr = client->lease->dns; + } else + return -ENOENT; return 0; } @@ -243,24 +216,12 @@ int sd_dhcp_client_get_domainname(sd_dhcp_client *client, const char **domainnam assert_return(client, -EINVAL); assert_return(domainname, -EINVAL); - switch (client->state) { - case DHCP_STATE_INIT: - case DHCP_STATE_SELECTING: - case DHCP_STATE_INIT_REBOOT: - case DHCP_STATE_REBOOTING: - case DHCP_STATE_REQUESTING: - return -EADDRNOTAVAIL; - - case DHCP_STATE_BOUND: - case DHCP_STATE_RENEWING: - case DHCP_STATE_REBINDING: - if (client->lease->domainname) - *domainname = client->lease->domainname; - else - return -ENOENT; + client_state_machine_check (client->state, -EADDRNOTAVAIL); - break; - } + if (client->lease->domainname) + *domainname = client->lease->domainname; + else + return -ENOENT; return 0; } @@ -269,24 +230,12 @@ int sd_dhcp_client_get_hostname(sd_dhcp_client *client, const char **hostname) { assert_return(client, -EINVAL); assert_return(hostname, -EINVAL); - switch (client->state) { - case DHCP_STATE_INIT: - case DHCP_STATE_SELECTING: - case DHCP_STATE_INIT_REBOOT: - case DHCP_STATE_REBOOTING: - case DHCP_STATE_REQUESTING: - return -EADDRNOTAVAIL; - - case DHCP_STATE_BOUND: - case DHCP_STATE_RENEWING: - case DHCP_STATE_REBINDING: - if (client->lease->hostname) - *hostname = client->lease->hostname; - else - return -ENOENT; + client_state_machine_check (client->state, -EADDRNOTAVAIL); - break; - } + if (client->lease->hostname) + *hostname = client->lease->hostname; + else + return -ENOENT; return 0; } @@ -310,21 +259,9 @@ int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr) { assert_return(client, -EINVAL); assert_return(addr, -EINVAL); - switch (client->state) { - case DHCP_STATE_INIT: - case DHCP_STATE_SELECTING: - case DHCP_STATE_INIT_REBOOT: - case DHCP_STATE_REBOOTING: - case DHCP_STATE_REQUESTING: - return -EADDRNOTAVAIL; - - case DHCP_STATE_BOUND: - case DHCP_STATE_RENEWING: - case DHCP_STATE_REBINDING: - addr->s_addr = client->lease->router; + client_state_machine_check (client->state, -EADDRNOTAVAIL); - break; - } + addr->s_addr = client->lease->router; return 0; } @@ -333,21 +270,9 @@ int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr) { assert_return(client, -EINVAL); assert_return(addr, -EINVAL); - switch (client->state) { - case DHCP_STATE_INIT: - case DHCP_STATE_SELECTING: - case DHCP_STATE_INIT_REBOOT: - case DHCP_STATE_REBOOTING: - case DHCP_STATE_REQUESTING: - return -EADDRNOTAVAIL; - - case DHCP_STATE_BOUND: - case DHCP_STATE_RENEWING: - case DHCP_STATE_REBINDING: - addr->s_addr = client->lease->subnet_mask; + client_state_machine_check (client->state, -EADDRNOTAVAIL); - break; - } + addr->s_addr = client->lease->subnet_mask; return 0; } |