#include <stdio.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
+#include <netinet/ether.h>
#include <sys/param.h>
#include <sys/ioctl.h>
assert_return(client, -EINVAL);
assert_return (IN_SET(client->state, DHCP_STATE_INIT,
DHCP_STATE_STOPPED), -EBUSY);
- assert_return(interface_index >= -1, -EINVAL);
+ assert_return(interface_index > 0, -EINVAL);
client->index = interface_index;
static sd_dhcp_client *client_stop(sd_dhcp_client *client, int error) {
assert_return(client, NULL);
- log_dhcp_client(client, "STOPPED %d", error);
+ if (error < 0)
+ log_dhcp_client(client, "STOPPED: %s", strerror(-error));
+ else {
+ switch(error) {
+ case DHCP_EVENT_STOP:
+ log_dhcp_client(client, "STOPPED: Requested by user");
+ break;
+ case DHCP_EVENT_NO_LEASE:
+ log_dhcp_client(client, "STOPPED: No lease");
+ break;
+ default:
+ log_dhcp_client(client, "STOPPED: Unknown reason");
+ break;
+ }
+ }
client = client_notify(client, error);
if (r < 0)
return r;
- log_dhcp_client(client, "REQUEST");
+ switch (client->state) {
+ case DHCP_STATE_REQUESTING:
+ log_dhcp_client(client, "REQUEST (requesting)");
+ break;
+ case DHCP_STATE_INIT_REBOOT:
+ log_dhcp_client(client, "REQUEST (init-reboot)");
+ break;
+ case DHCP_STATE_RENEWING:
+ log_dhcp_client(client, "REQUEST (renewing)");
+ break;
+ case DHCP_STATE_REBINDING:
+ log_dhcp_client(client, "REQUEST (rebinding)");
+ break;
+ default:
+ log_dhcp_client(client, "REQUEST (invalid)");
+ break;
+ }
return 0;
}
+static int client_start(sd_dhcp_client *client);
+
static int client_timeout_resend(sd_event_source *s, uint64_t usec,
void *userdata) {
sd_dhcp_client *client = userdata;
case DHCP_STATE_REBOOTING:
/* start over as we did not receive a timely ack or nak */
- client->state = DHCP_STATE_INIT;
- client->attempt = 1;
+ r = client_initialize(client);
+ if (r < 0)
+ goto error;
- client->fd = safe_close(client->fd);
- client->xid = random_u32();
- r = dhcp_network_bind_raw_socket(client->index, &client->link, client->xid);
+ r = client_start(client);
if (r < 0)
goto error;
- client->fd = r;
+ else {
+ log_dhcp_client(client, "REBOOTED");
+ return 0;
+ }
- /* fall through */
case DHCP_STATE_INIT:
case DHCP_STATE_INIT_REBOOT:
case DHCP_STATE_SELECTING:
client->xid = random_u32();
r = dhcp_network_bind_raw_socket(client->index, &client->link, client->xid);
-
if (r < 0) {
client_stop(client, r);
return r;
client->secs = 0;
}
- log_dhcp_client(client, "STARTED");
-
return client_initialize_events(client, client_receive_message_raw);
}
client_stop(client, r);
return 0;
}
-
client->fd = r;
- log_dhcp_client(client, "TIMEOUT T2");
-
return client_initialize_events(client, client_receive_message_raw);
}
client->fd = r;
- log_dhcp_client(client, "TIMEOUT T1");
-
return client_initialize_events(client, client_receive_message_udp);
}
}
}
+ sd_dhcp_lease_unref(client->lease);
client->lease = lease;
lease = NULL;
r = client_start(client);
if (r < 0)
goto error;
+
+ log_dhcp_client(client, "REBOOTED");
}
goto error;
if (client->last_addr)
client->state = DHCP_STATE_INIT_REBOOT;
- return client_start(client);
+ r = client_start(client);
+ if (r >= 0)
+ log_dhcp_client(client, "STARTED on ifindex %u with address %s",
+ client->index,
+ ether_ntoa(&client->client_id.mac_addr));
+
+ return r;
}
int sd_dhcp_client_stop(sd_dhcp_client *client) {
sd_dhcp_client_detach_event(client);
+ sd_dhcp_lease_unref(client->lease);
+
free(client->req_opts);
free(client);