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