summaryrefslogtreecommitdiffstats
path: root/src/libsystemd-network
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2023-09-04 19:52:48 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2023-09-05 19:02:21 +0200
commita93538e89238f7a6df0236c3939a04aa46fa312a (patch)
tree77247b3474c2ad3b841e097b7775c6add9d75197 /src/libsystemd-network
parenttime-util: introduce triple_timestamp_from_boottime() (diff)
downloadsystemd-a93538e89238f7a6df0236c3939a04aa46fa312a.tar.xz
systemd-a93538e89238f7a6df0236c3939a04aa46fa312a.zip
dhcp: introduce sd_dhcp_lease_get_timestamp()
And drop sd_dhcp_client_get_lease_timestamp(). Also, this introduce sd_dhcp_lease_get_lifetime_timestamp() and friends, which provides timestamp of the lifetime and so on, while sd_dhcp_lease_get_lifetime() provides timestamp.
Diffstat (limited to 'src/libsystemd-network')
-rw-r--r--src/libsystemd-network/dhcp-lease-internal.h2
-rw-r--r--src/libsystemd-network/sd-dhcp-client.c14
-rw-r--r--src/libsystemd-network/sd-dhcp-lease.c41
3 files changed, 45 insertions, 12 deletions
diff --git a/src/libsystemd-network/dhcp-lease-internal.h b/src/libsystemd-network/dhcp-lease-internal.h
index 4f0e96fc23..04a9893876 100644
--- a/src/libsystemd-network/dhcp-lease-internal.h
+++ b/src/libsystemd-network/dhcp-lease-internal.h
@@ -10,6 +10,7 @@
#include "dhcp-internal.h"
#include "dhcp-protocol.h"
#include "list.h"
+#include "time-util.h"
struct sd_dhcp_route {
struct in_addr dst_addr;
@@ -32,6 +33,7 @@ struct sd_dhcp_lease {
usec_t t1;
usec_t t2;
usec_t lifetime;
+ triple_timestamp timestamp;
/* each 0 if unset */
be32_t address;
diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c
index b7de3e843a..854eca5a58 100644
--- a/src/libsystemd-network/sd-dhcp-client.c
+++ b/src/libsystemd-network/sd-dhcp-client.c
@@ -716,18 +716,6 @@ int sd_dhcp_client_get_lease(sd_dhcp_client *client, sd_dhcp_lease **ret) {
return 0;
}
-int sd_dhcp_client_get_lease_timestamp(sd_dhcp_client *client, uint64_t *timestamp) {
- assert_return(client, -EINVAL);
-
- if (!IN_SET(client->state, DHCP_STATE_BOUND, DHCP_STATE_RENEWING, DHCP_STATE_REBINDING))
- return -ENODATA;
-
- if(timestamp)
- *timestamp = client->request_sent;
-
- return 0;
-}
-
int sd_dhcp_client_set_service_type(sd_dhcp_client *client, int type) {
assert_return(client, -EINVAL);
assert_return(!sd_dhcp_client_is_running(client), -EBUSY);
@@ -1727,6 +1715,8 @@ static int client_set_lease_timeouts(sd_dhcp_client *client) {
assert(client->lease);
assert(client->lease->lifetime > 0);
+ triple_timestamp_from_boottime(&client->lease->timestamp, client->request_sent);
+
/* don't set timers for infinite leases */
if (client->lease->lifetime == USEC_INFINITY) {
(void) event_source_disable(client->timeout_t1);
diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c
index 229d5656d9..97985c74c7 100644
--- a/src/libsystemd-network/sd-dhcp-lease.c
+++ b/src/libsystemd-network/sd-dhcp-lease.c
@@ -29,9 +29,23 @@
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
+#include "time-util.h"
#include "tmpfile-util.h"
#include "unaligned.h"
+int sd_dhcp_lease_get_timestamp(sd_dhcp_lease *lease, clockid_t clock, uint64_t *ret) {
+ assert_return(lease, -EINVAL);
+ assert_return(TRIPLE_TIMESTAMP_HAS_CLOCK(clock), -EOPNOTSUPP);
+ assert_return(clock_supported(clock), -EOPNOTSUPP);
+ assert_return(ret, -EINVAL);
+
+ if (!triple_timestamp_is_set(&lease->timestamp))
+ return -ENODATA;
+
+ *ret = triple_timestamp_by_clock(&lease->timestamp, clock);
+ return 0;
+}
+
int sd_dhcp_lease_get_address(sd_dhcp_lease *lease, struct in_addr *addr) {
assert_return(lease, -EINVAL);
assert_return(addr, -EINVAL);
@@ -87,6 +101,33 @@ int sd_dhcp_lease_get_t2(sd_dhcp_lease *lease, uint64_t *ret) {
return 0;
}
+#define DEFINE_GET_TIMESTAMP(name) \
+ int sd_dhcp_lease_get_##name##_timestamp( \
+ sd_dhcp_lease *lease, \
+ clockid_t clock, \
+ uint64_t *ret) { \
+ \
+ usec_t t, timestamp; \
+ int r; \
+ \
+ assert_return(ret, -EINVAL); \
+ \
+ r = sd_dhcp_lease_get_##name(lease, &t); \
+ if (r < 0) \
+ return r; \
+ \
+ r = sd_dhcp_lease_get_timestamp(lease, clock, &timestamp); \
+ if (r < 0) \
+ return r; \
+ \
+ *ret = usec_add(t, timestamp); \
+ return 0; \
+ }
+
+DEFINE_GET_TIMESTAMP(lifetime);
+DEFINE_GET_TIMESTAMP(t1);
+DEFINE_GET_TIMESTAMP(t2);
+
int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu) {
assert_return(lease, -EINVAL);
assert_return(mtu, -EINVAL);