]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-dhcp: introduce sd_dhcp_lease_get_prefix()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 24 Jul 2023 16:57:10 +0000 (01:57 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 27 Jul 2023 20:15:11 +0000 (05:15 +0900)
src/libsystemd-network/sd-dhcp-lease.c
src/systemd/sd-dhcp-lease.h

index 03fd80064b7188d0e5d0f63b0e7ebd8bd5545398..5e8869a3b7168ebc3af128c2bb7c0e6a68e8f761 100644 (file)
@@ -201,6 +201,34 @@ int sd_dhcp_lease_get_netmask(sd_dhcp_lease *lease, struct in_addr *addr) {
         return 0;
 }
 
+int sd_dhcp_lease_get_prefix(sd_dhcp_lease *lease, struct in_addr *ret_prefix, uint8_t *ret_prefixlen) {
+        struct in_addr address, netmask;
+        uint8_t prefixlen;
+        int r;
+
+        assert_return(lease, -EINVAL);
+
+        r = sd_dhcp_lease_get_address(lease, &address);
+        if (r < 0)
+                return r;
+
+        r = sd_dhcp_lease_get_netmask(lease, &netmask);
+        if (r < 0)
+                return r;
+
+        prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
+
+        r = in4_addr_mask(&address, prefixlen);
+        if (r < 0)
+                return r;
+
+        if (ret_prefix)
+                *ret_prefix = address;
+        if (ret_prefixlen)
+                *ret_prefixlen = prefixlen;
+        return 0;
+}
+
 int sd_dhcp_lease_get_server_identifier(sd_dhcp_lease *lease, struct in_addr *addr) {
         assert_return(lease, -EINVAL);
         assert_return(addr, -EINVAL);
index a0e09e52aca80e2aff7c72dff29a939c56e4292a..8a270595722f1f893eee3efc91d2dcc9c2ee5bc5 100644 (file)
@@ -52,6 +52,7 @@ int sd_dhcp_lease_get_t1(sd_dhcp_lease *lease, uint32_t *t1);
 int sd_dhcp_lease_get_t2(sd_dhcp_lease *lease, uint32_t *t2);
 int sd_dhcp_lease_get_broadcast(sd_dhcp_lease *lease, struct in_addr *addr);
 int sd_dhcp_lease_get_netmask(sd_dhcp_lease *lease, struct in_addr *addr);
+int sd_dhcp_lease_get_prefix(sd_dhcp_lease *lease, struct in_addr *ret_prefix, uint8_t *ret_prefixlen);
 int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, const struct in_addr **addr);
 int sd_dhcp_lease_get_next_server(sd_dhcp_lease *lease, struct in_addr *addr);
 int sd_dhcp_lease_get_server_identifier(sd_dhcp_lease *lease, struct in_addr *addr);