chiark / gitweb /
sd-dhcp-client: log positive error number
[elogind.git] / src / libsystemd-network / sd-dhcp-client.c
index 0adcf1328f93284a3bb433fd7bb99a44cff69eb0..2ddb9ad1dca6bcdec44981b4171b0d33f7d03c35 100644 (file)
@@ -23,6 +23,7 @@
 #include <stdio.h>
 #include <net/ethernet.h>
 #include <net/if_arp.h>
+#include <netinet/ether.h>
 #include <sys/param.h>
 #include <sys/ioctl.h>
 
@@ -142,7 +143,7 @@ int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index) {
         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;
 
@@ -230,7 +231,21 @@ static int client_initialize(sd_dhcp_client *client) {
 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);
 
@@ -480,11 +495,29 @@ static int client_send_request(sd_dhcp_client *client) {
         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;
@@ -523,17 +556,18 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
 
         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:
@@ -677,7 +711,6 @@ static int client_start(sd_dhcp_client *client) {
         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;
@@ -689,8 +722,6 @@ static int client_start(sd_dhcp_client *client) {
                 client->secs = 0;
         }
 
-        log_dhcp_client(client, "STARTED");
-
         return client_initialize_events(client, client_receive_message_raw);
 }
 
@@ -726,11 +757,8 @@ static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata)
                 client_stop(client, r);
                 return 0;
         }
-
         client->fd = r;
 
-        log_dhcp_client(client, "TIMEOUT T2");
-
         return client_initialize_events(client, client_receive_message_raw);
 }
 
@@ -752,8 +780,6 @@ static int client_timeout_t1(sd_event_source *s, uint64_t usec,
 
         client->fd = r;
 
-        log_dhcp_client(client, "TIMEOUT T1");
-
         return client_initialize_events(client, client_receive_message_udp);
 }
 
@@ -794,6 +820,7 @@ static int client_handle_offer(sd_dhcp_client *client, DHCPMessage *offer,
                 }
         }
 
+        sd_dhcp_lease_unref(client->lease);
         client->lease = lease;
         lease = NULL;
 
@@ -1094,6 +1121,8 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message,
                                 r = client_start(client);
                                 if (r < 0)
                                         goto error;
+
+                                log_dhcp_client(client, "REBOOTED");
                         }
 
                         goto error;
@@ -1248,7 +1277,13 @@ int sd_dhcp_client_start(sd_dhcp_client *client) {
         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) {
@@ -1313,6 +1348,8 @@ sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client) {
 
                 sd_dhcp_client_detach_event(client);
 
+                sd_dhcp_lease_unref(client->lease);
+
                 free(client->req_opts);
                 free(client);