#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;
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}