size_t i;
assert_return(client, -EINVAL);
- assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
+ assert_return (IN_SET(client->state, DHCP_STATE_INIT,
+ DHCP_STATE_STOPPED), -EBUSY);
switch(option) {
case DHCP_OPTION_PAD:
int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
const struct in_addr *last_addr) {
assert_return(client, -EINVAL);
- assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
+ assert_return (IN_SET(client->state, DHCP_STATE_INIT,
+ DHCP_STATE_STOPPED), -EBUSY);
if (last_addr)
client->last_addr = last_addr->s_addr;
int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index) {
assert_return(client, -EINVAL);
- assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
+ assert_return (IN_SET(client->state, DHCP_STATE_INIT,
+ DHCP_STATE_STOPPED), -EBUSY);
assert_return(interface_index >= -1, -EINVAL);
client->index = interface_index;
if (memcmp(&client->client_id.mac_addr, addr, ETH_ALEN) == 0)
return 0;
- if (client->state != DHCP_STATE_INIT) {
+ if (!IN_SET(client->state, DHCP_STATE_INIT, DHCP_STATE_STOPPED)) {
log_dhcp_client(client, "Changing MAC address on running DHCP "
"client, restarting");
need_restart = true;
memcpy(&client->client_id.mac_addr, addr, ETH_ALEN);
client->client_id.type = 0x01;
- if (need_restart)
+ if (need_restart && client->state != DHCP_STATE_STOPPED)
sd_dhcp_client_start(client);
return 0;
case DHCP_STATE_REBINDING:
break;
+
+ case DHCP_STATE_STOPPED:
+ return -EINVAL;
}
r = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
next_timeout = time_now + (client->attempt - 1) * USEC_PER_SEC;
break;
+
+ case DHCP_STATE_STOPPED:
+ r = -EINVAL;
+ goto error;
}
next_timeout += (random_u32() & 0x1fffff);
case DHCP_STATE_BOUND:
break;
+
+ case DHCP_STATE_STOPPED:
+ r = -EINVAL;
+ goto error;
}
return 0;
client = client_notify(client, DHCP_EVENT_EXPIRED);
- /* lease was lost, start over if not freed */
- if (client) {
+ /* lease was lost, start over if not freed or stopped in callback */
+ if (client && client->state != DHCP_STATE_STOPPED) {
client_initialize(client);
client_start(client);
}
if (notify_event) {
client = client_notify(client, notify_event);
- if (!client)
+ if (!client ||
+ client->state == DHCP_STATE_STOPPED)
return 0;
}
case DHCP_STATE_BOUND:
break;
+
+ case DHCP_STATE_STOPPED:
+ r = -EINVAL;
+ goto error;
}
error:
int sd_dhcp_client_stop(sd_dhcp_client *client) {
assert_return(client, -EINVAL);
- client_stop(client, DHCP_EVENT_STOP);
+ if (client_stop(client, DHCP_EVENT_STOP))
+ client->state = DHCP_STATE_STOPPED;
return 0;
}