]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-network-bus.c
Merge pull request #7388 from keszybz/doc-tweak
[thirdparty/systemd.git] / src / network / networkd-network-bus.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2015 Tom Gundersen
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "alloc-util.h"
21 #include "networkd-manager.h"
22 #include "string-util.h"
23 #include "strv.h"
24
25 static int property_get_ether_addrs(
26 sd_bus *bus,
27 const char *path,
28 const char *interface,
29 const char *property,
30 sd_bus_message *reply,
31 void *userdata,
32 sd_bus_error *error) {
33
34 Network *n = userdata;
35 const char *ether = NULL;
36 int r;
37
38 assert(bus);
39 assert(reply);
40 assert(n);
41
42 if (n->match_mac)
43 ether = ether_ntoa(n->match_mac);
44
45 r = sd_bus_message_open_container(reply, 'a', "s");
46 if (r < 0)
47 return r;
48
49 if (ether) {
50 r = sd_bus_message_append(reply, "s", strempty(ether));
51 if (r < 0)
52 return r;
53 }
54
55 return sd_bus_message_close_container(reply);
56 }
57
58 const sd_bus_vtable network_vtable[] = {
59 SD_BUS_VTABLE_START(0),
60
61 SD_BUS_PROPERTY("Description", "s", NULL, offsetof(Network, description), SD_BUS_VTABLE_PROPERTY_CONST),
62 SD_BUS_PROPERTY("SourcePath", "s", NULL, offsetof(Network, filename), SD_BUS_VTABLE_PROPERTY_CONST),
63 SD_BUS_PROPERTY("MatchMAC", "as", property_get_ether_addrs, 0, SD_BUS_VTABLE_PROPERTY_CONST),
64 SD_BUS_PROPERTY("MatchPath", "as", NULL, offsetof(Network, match_path), SD_BUS_VTABLE_PROPERTY_CONST),
65 SD_BUS_PROPERTY("MatchDriver", "as", NULL, offsetof(Network, match_driver), SD_BUS_VTABLE_PROPERTY_CONST),
66 SD_BUS_PROPERTY("MatchType", "as", NULL, offsetof(Network, match_type), SD_BUS_VTABLE_PROPERTY_CONST),
67 SD_BUS_PROPERTY("MatchName", "as", NULL, offsetof(Network, match_name), SD_BUS_VTABLE_PROPERTY_CONST),
68
69 SD_BUS_VTABLE_END
70 };
71
72 static char *network_bus_path(Network *network) {
73 _cleanup_free_ char *name = NULL;
74 char *networkname, *d, *path;
75 int r;
76
77 assert(network);
78 assert(network->filename);
79
80 name = strdup(network->filename);
81 if (!name)
82 return NULL;
83
84 networkname = basename(name);
85
86 d = strrchr(networkname, '.');
87 if (!d)
88 return NULL;
89
90 assert(streq(d, ".network"));
91
92 *d = '\0';
93
94 r = sd_bus_path_encode("/org/freedesktop/network1/network", networkname, &path);
95 if (r < 0)
96 return NULL;
97
98 return path;
99 }
100
101 int network_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
102 _cleanup_strv_free_ char **l = NULL;
103 Manager *m = userdata;
104 Network *network;
105 int r;
106
107 assert(bus);
108 assert(path);
109 assert(m);
110 assert(nodes);
111
112 LIST_FOREACH(networks, network, m->networks) {
113 char *p;
114
115 p = network_bus_path(network);
116 if (!p)
117 return -ENOMEM;
118
119 r = strv_consume(&l, p);
120 if (r < 0)
121 return r;
122 }
123
124 *nodes = l;
125 l = NULL;
126
127 return 1;
128 }
129
130 int network_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
131 Manager *m = userdata;
132 Network *network;
133 _cleanup_free_ char *name = NULL;
134 int r;
135
136 assert(bus);
137 assert(path);
138 assert(interface);
139 assert(m);
140 assert(found);
141
142 r = sd_bus_path_decode(path, "/org/freedesktop/network1/network", &name);
143 if (r < 0)
144 return 0;
145
146 r = network_get_by_name(m, name, &network);
147 if (r < 0)
148 return 0;
149
150 *found = network;
151
152 return 1;
153 }