]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
38ee31a6 MAL |
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; | |
38ee31a6 MAL |
25 | int r; |
26 | ||
27 | assert(reply); | |
28 | assert(l); | |
29 | ||
30 | s = l->dhcp_server; | |
31 | if (!s) | |
32 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Link %s has no DHCP server.", l->ifname); | |
33 | ||
c95df587 YA |
34 | if (sd_dhcp_server_is_in_relay_mode(s)) |
35 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Link %s has DHCP relay agent active.", l->ifname); | |
36 | ||
38ee31a6 MAL |
37 | r = sd_bus_message_open_container(reply, 'a', "(uayayayayt)"); |
38 | if (r < 0) | |
39 | return r; | |
40 | ||
90e74a66 | 41 | HASHMAP_FOREACH(lease, s->leases_by_client_id) { |
38ee31a6 MAL |
42 | r = sd_bus_message_open_container(reply, 'r', "uayayayayt"); |
43 | if (r < 0) | |
44 | return r; | |
45 | ||
46 | r = sd_bus_message_append(reply, "u", (uint32_t)AF_INET); | |
47 | if (r < 0) | |
48 | return r; | |
49 | ||
50 | r = sd_bus_message_append_array(reply, 'y', lease->client_id.data, lease->client_id.length); | |
51 | if (r < 0) | |
52 | return r; | |
53 | ||
54 | r = sd_bus_message_append_array(reply, 'y', &lease->address, sizeof(lease->address)); | |
55 | if (r < 0) | |
56 | return r; | |
57 | ||
58 | r = sd_bus_message_append_array(reply, 'y', &lease->gateway, sizeof(lease->gateway)); | |
59 | if (r < 0) | |
60 | return r; | |
61 | ||
62 | r = sd_bus_message_append_array(reply, 'y', &lease->chaddr, sizeof(lease->chaddr)); | |
63 | if (r < 0) | |
64 | return r; | |
65 | ||
66 | r = sd_bus_message_append_basic(reply, 't', &lease->expiration); | |
67 | if (r < 0) | |
68 | return r; | |
69 | ||
70 | r = sd_bus_message_close_container(reply); | |
71 | if (r < 0) | |
72 | return r; | |
73 | } | |
74 | ||
75 | return sd_bus_message_close_container(reply); | |
76 | } | |
77 | ||
78 | static int dhcp_server_emit_changed(Link *link, const char *property, ...) { | |
79 | _cleanup_free_ char *path = NULL; | |
80 | char **l; | |
81 | ||
82 | assert(link); | |
83 | ||
706875f1 YW |
84 | if (sd_bus_is_ready(link->manager->bus) <= 0) |
85 | return 0; | |
86 | ||
38ee31a6 MAL |
87 | path = link_bus_path(link); |
88 | if (!path) | |
89 | return log_oom(); | |
90 | ||
91 | l = strv_from_stdarg_alloca(property); | |
92 | ||
93 | return sd_bus_emit_properties_changed_strv( | |
94 | link->manager->bus, | |
95 | path, | |
96 | "org.freedesktop.network1.DHCPServer", | |
97 | l); | |
98 | } | |
99 | ||
100 | void dhcp_server_callback(sd_dhcp_server *s, uint64_t event, void *data) { | |
101 | Link *l = data; | |
102 | ||
103 | assert(l); | |
104 | ||
105 | if (event & SD_DHCP_SERVER_EVENT_LEASE_CHANGED) | |
106 | (void) dhcp_server_emit_changed(l, "Leases", NULL); | |
107 | } | |
108 | ||
109 | ||
110 | const sd_bus_vtable dhcp_server_vtable[] = { | |
111 | SD_BUS_VTABLE_START(0), | |
112 | ||
113 | SD_BUS_PROPERTY("Leases", "a(uayayayayt)", property_get_leases, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
114 | ||
115 | SD_BUS_VTABLE_END | |
116 | }; |