]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Adding dhcp_state_to_string and dhcp client state change logging
authorpelaufer <paul@laufernet.com>
Sat, 2 Sep 2023 20:46:47 +0000 (14:46 -0600)
committerpelaufer <paul@laufernet.com>
Sat, 2 Sep 2023 20:46:47 +0000 (14:46 -0600)
src/libsystemd-network/dhcp-protocol.c [new file with mode: 0644]
src/libsystemd-network/dhcp-protocol.h
src/libsystemd-network/meson.build
src/libsystemd-network/sd-dhcp-client.c

diff --git a/src/libsystemd-network/dhcp-protocol.c b/src/libsystemd-network/dhcp-protocol.c
new file mode 100644 (file)
index 0000000..955d087
--- /dev/null
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "dhcp-protocol.h"
+#include "string-table.h"
+
+static const char* const dhcp_state_table[_DHCP_STATE_MAX] = {
+        [DHCP_STATE_STOPPED]              = "stopped",
+        [DHCP_STATE_INIT]                 = "initialization",
+        [DHCP_STATE_SELECTING]            = "selecting",
+        [DHCP_STATE_INIT_REBOOT]          = "init-reboot",
+        [DHCP_STATE_REBOOTING]            = "rebooting",
+        [DHCP_STATE_REQUESTING]           = "requesting",
+        [DHCP_STATE_BOUND]                = "bound",
+        [DHCP_STATE_RENEWING]             = "renewing",
+        [DHCP_STATE_REBINDING]            = "rebinding",
+};
+
+DEFINE_STRING_TABLE_LOOKUP_TO_STRING(dhcp_state, DHCPState);
index 2dc0660cc7d7b59dbce56e32d6c9e1a532d8d048..dd330ae83938829a5600ac3447ccfe914f449913 100644 (file)
@@ -55,15 +55,17 @@ enum {
 };
 
 enum DHCPState {
-        DHCP_STATE_STOPPED                      = 0,
-        DHCP_STATE_INIT                         = 1,
-        DHCP_STATE_SELECTING                    = 2,
-        DHCP_STATE_INIT_REBOOT                  = 3,
-        DHCP_STATE_REBOOTING                    = 4,
-        DHCP_STATE_REQUESTING                   = 5,
-        DHCP_STATE_BOUND                        = 6,
-        DHCP_STATE_RENEWING                     = 7,
-        DHCP_STATE_REBINDING                    = 8,
+        DHCP_STATE_STOPPED,
+        DHCP_STATE_INIT,
+        DHCP_STATE_SELECTING,
+        DHCP_STATE_INIT_REBOOT,
+        DHCP_STATE_REBOOTING,
+        DHCP_STATE_REQUESTING,
+        DHCP_STATE_BOUND,
+        DHCP_STATE_RENEWING,
+        DHCP_STATE_REBINDING,
+        _DHCP_STATE_MAX,
+        _DHCP_STATE_INVALID                     = -EINVAL,
 };
 
 typedef enum DHCPState DHCPState;
@@ -107,3 +109,5 @@ enum {
         DHCP_FQDN_FLAG_E = (1 << 2),
         DHCP_FQDN_FLAG_N = (1 << 3),
 };
+
+const char *dhcp_state_to_string(DHCPState s) _const_;
index 043d3bc2548b5fbe5725963f633bd589fc441b41..0b35eeec3488a0915f6409bcd6b40bd090e540ea 100644 (file)
@@ -6,6 +6,7 @@ sources = files(
         'dhcp-network.c',
         'dhcp-option.c',
         'dhcp-packet.c',
+        'dhcp-protocol.c',
         'dhcp6-network.c',
         'dhcp6-option.c',
         'dhcp6-protocol.c',
index cbea55dd63d0f348b0b1787633822f542118e263..2e17f3cfd511104a85efc359c8a27cc123f09f4b 100644 (file)
@@ -736,6 +736,9 @@ static void client_set_state(sd_dhcp_client *client, DHCPState state) {
         if (client->state == state)
                 return;
 
+        log_dhcp_client(client, "State changed: %s -> %s",
+                        dhcp_state_to_string(client->state), dhcp_state_to_string(state));
+
         client->state = state;
 }
 
@@ -1192,6 +1195,7 @@ static int client_send_request(sd_dhcp_client *client) {
         case DHCP_STATE_REBOOTING:
         case DHCP_STATE_BOUND:
         case DHCP_STATE_STOPPED:
+        default:
                 return -EINVAL;
         }
 
@@ -1349,6 +1353,7 @@ static int client_timeout_resend(
                 break;
 
         case DHCP_STATE_STOPPED:
+        default:
                 r = -EINVAL;
                 goto error;
         }