]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-dhcp-server-bus.c
Merge pull request #15697 from OhNoMoreGit/fix-path-units
[thirdparty/systemd.git] / src / network / networkd-dhcp-server-bus.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "sd-dhcp-server.h"
4
5 #include "alloc-util.h"
6 #include "bus-common-errors.h"
7 #include "bus-util.h"
8 #include "dhcp-server-internal.h"
9 #include "networkd-dhcp-server-bus.h"
10 #include "networkd-link-bus.h"
11 #include "networkd-manager.h"
12 #include "strv.h"
13
14 static int property_get_leases(
15 sd_bus *bus,
16 const char *path,
17 const char *interface,
18 const char *property,
19 sd_bus_message *reply,
20 void *userdata,
21 sd_bus_error *error) {
22 Link *l = userdata;
23 sd_dhcp_server *s;
24 DHCPLease *lease;
25 Iterator i;
26 int r;
27
28 assert(reply);
29 assert(l);
30
31 s = l->dhcp_server;
32 if (!s)
33 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Link %s has no DHCP server.", l->ifname);
34
35 r = sd_bus_message_open_container(reply, 'a', "(uayayayayt)");
36 if (r < 0)
37 return r;
38
39 HASHMAP_FOREACH(lease, s->leases_by_client_id, i) {
40 r = sd_bus_message_open_container(reply, 'r', "uayayayayt");
41 if (r < 0)
42 return r;
43
44 r = sd_bus_message_append(reply, "u", (uint32_t)AF_INET);
45 if (r < 0)
46 return r;
47
48 r = sd_bus_message_append_array(reply, 'y', lease->client_id.data, lease->client_id.length);
49 if (r < 0)
50 return r;
51
52 r = sd_bus_message_append_array(reply, 'y', &lease->address, sizeof(lease->address));
53 if (r < 0)
54 return r;
55
56 r = sd_bus_message_append_array(reply, 'y', &lease->gateway, sizeof(lease->gateway));
57 if (r < 0)
58 return r;
59
60 r = sd_bus_message_append_array(reply, 'y', &lease->chaddr, sizeof(lease->chaddr));
61 if (r < 0)
62 return r;
63
64 r = sd_bus_message_append_basic(reply, 't', &lease->expiration);
65 if (r < 0)
66 return r;
67
68 r = sd_bus_message_close_container(reply);
69 if (r < 0)
70 return r;
71 }
72
73 return sd_bus_message_close_container(reply);
74 }
75
76 static int dhcp_server_emit_changed(Link *link, const char *property, ...) {
77 _cleanup_free_ char *path = NULL;
78 char **l;
79
80 assert(link);
81
82 path = link_bus_path(link);
83 if (!path)
84 return log_oom();
85
86 l = strv_from_stdarg_alloca(property);
87
88 return sd_bus_emit_properties_changed_strv(
89 link->manager->bus,
90 path,
91 "org.freedesktop.network1.DHCPServer",
92 l);
93 }
94
95 void dhcp_server_callback(sd_dhcp_server *s, uint64_t event, void *data) {
96 Link *l = data;
97
98 assert(l);
99
100 if (event & SD_DHCP_SERVER_EVENT_LEASE_CHANGED)
101 (void) dhcp_server_emit_changed(l, "Leases", NULL);
102 }
103
104
105 const sd_bus_vtable dhcp_server_vtable[] = {
106 SD_BUS_VTABLE_START(0),
107
108 SD_BUS_PROPERTY("Leases", "a(uayayayayt)", property_get_leases, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
109
110 SD_BUS_VTABLE_END
111 };