Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include "alloc-util.h"
4 : #include "ether-addr-util.h"
5 : #include "networkd-manager.h"
6 : #include "networkd-network-bus.h"
7 : #include "string-util.h"
8 : #include "strv.h"
9 :
10 0 : static int property_get_ether_addrs(
11 : sd_bus *bus,
12 : const char *path,
13 : const char *interface,
14 : const char *property,
15 : sd_bus_message *reply,
16 : void *userdata,
17 : sd_bus_error *error) {
18 :
19 : char buf[ETHER_ADDR_TO_STRING_MAX];
20 : const struct ether_addr *p;
21 : Iterator i;
22 : Set *s;
23 : int r;
24 :
25 0 : assert(bus);
26 0 : assert(reply);
27 0 : assert(userdata);
28 :
29 0 : s = *(Set **) userdata;
30 :
31 0 : r = sd_bus_message_open_container(reply, 'a', "s");
32 0 : if (r < 0)
33 0 : return r;
34 :
35 0 : SET_FOREACH(p, s, i) {
36 0 : r = sd_bus_message_append(reply, "s", ether_addr_to_string(p, buf));
37 0 : if (r < 0)
38 0 : return r;
39 : }
40 :
41 0 : return sd_bus_message_close_container(reply);
42 : }
43 :
44 : const sd_bus_vtable network_vtable[] = {
45 : SD_BUS_VTABLE_START(0),
46 :
47 : SD_BUS_PROPERTY("Description", "s", NULL, offsetof(Network, description), SD_BUS_VTABLE_PROPERTY_CONST),
48 : SD_BUS_PROPERTY("SourcePath", "s", NULL, offsetof(Network, filename), SD_BUS_VTABLE_PROPERTY_CONST),
49 : SD_BUS_PROPERTY("MatchMAC", "as", property_get_ether_addrs, offsetof(Network, match_mac), SD_BUS_VTABLE_PROPERTY_CONST),
50 : SD_BUS_PROPERTY("MatchPath", "as", NULL, offsetof(Network, match_path), SD_BUS_VTABLE_PROPERTY_CONST),
51 : SD_BUS_PROPERTY("MatchDriver", "as", NULL, offsetof(Network, match_driver), SD_BUS_VTABLE_PROPERTY_CONST),
52 : SD_BUS_PROPERTY("MatchType", "as", NULL, offsetof(Network, match_type), SD_BUS_VTABLE_PROPERTY_CONST),
53 : SD_BUS_PROPERTY("MatchName", "as", NULL, offsetof(Network, match_name), SD_BUS_VTABLE_PROPERTY_CONST),
54 :
55 : SD_BUS_VTABLE_END
56 : };
57 :
58 0 : static char *network_bus_path(Network *network) {
59 0 : _cleanup_free_ char *name = NULL;
60 : char *networkname, *d, *path;
61 : int r;
62 :
63 0 : assert(network);
64 0 : assert(network->filename);
65 :
66 0 : name = strdup(network->filename);
67 0 : if (!name)
68 0 : return NULL;
69 :
70 0 : networkname = basename(name);
71 :
72 0 : d = strrchr(networkname, '.');
73 0 : if (!d)
74 0 : return NULL;
75 :
76 0 : assert(streq(d, ".network"));
77 :
78 0 : *d = '\0';
79 :
80 0 : r = sd_bus_path_encode("/org/freedesktop/network1/network", networkname, &path);
81 0 : if (r < 0)
82 0 : return NULL;
83 :
84 0 : return path;
85 : }
86 :
87 0 : int network_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
88 0 : _cleanup_strv_free_ char **l = NULL;
89 0 : Manager *m = userdata;
90 : Network *network;
91 : Iterator i;
92 : int r;
93 :
94 0 : assert(bus);
95 0 : assert(path);
96 0 : assert(m);
97 0 : assert(nodes);
98 :
99 0 : ORDERED_HASHMAP_FOREACH(network, m->networks, i) {
100 : char *p;
101 :
102 0 : p = network_bus_path(network);
103 0 : if (!p)
104 0 : return -ENOMEM;
105 :
106 0 : r = strv_consume(&l, p);
107 0 : if (r < 0)
108 0 : return r;
109 : }
110 :
111 0 : *nodes = TAKE_PTR(l);
112 :
113 0 : return 1;
114 : }
115 :
116 0 : int network_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
117 0 : Manager *m = userdata;
118 : Network *network;
119 0 : _cleanup_free_ char *name = NULL;
120 : int r;
121 :
122 0 : assert(bus);
123 0 : assert(path);
124 0 : assert(interface);
125 0 : assert(m);
126 0 : assert(found);
127 :
128 0 : r = sd_bus_path_decode(path, "/org/freedesktop/network1/network", &name);
129 0 : if (r < 0)
130 0 : return 0;
131 :
132 0 : r = network_get_by_name(m, name, &network);
133 0 : if (r < 0)
134 0 : return 0;
135 :
136 0 : *found = network;
137 :
138 0 : return 1;
139 : }
|