chiark / gitweb /
6e7cbc5a457d2f0011aa94324f28cd511f474557
[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 *name = NULL;
44         char *networkname, *d, *path;
45         int r;
46
47         assert(network);
48         assert(network->filename);
49
50         name = strdup(network->filename);
51         if (!name)
52                 return NULL;
53
54         networkname = basename(name);
55
56         d = strrchr(networkname, '.');
57         if (!d)
58                 return NULL;
59
60         assert(streq(d, ".network"));
61
62         *d = '\0';
63
64         r = sd_bus_path_encode("/org/freedesktop/network1/network", networkname, &path);
65         if (r < 0)
66                 return NULL;
67
68         return path;
69 }
70
71 int network_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
72         _cleanup_strv_free_ char **l = NULL;
73         Manager *m = userdata;
74         Network *network;
75         int r;
76
77         assert(bus);
78         assert(path);
79         assert(m);
80         assert(nodes);
81
82         LIST_FOREACH(networks, network, m->networks) {
83                 char *p;
84
85                 p = network_bus_path(network);
86                 if (!p)
87                         return -ENOMEM;
88
89                 r = strv_consume(&l, p);
90                 if (r < 0)
91                         return r;
92         }
93
94         *nodes = l;
95         l = NULL;
96
97         return 1;
98 }
99
100 int network_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
101         Manager *m = userdata;
102         Network *network;
103         _cleanup_free_ char *name = NULL;
104         int r;
105
106         assert(bus);
107         assert(path);
108         assert(interface);
109         assert(m);
110         assert(found);
111
112         r = sd_bus_path_decode(path, "/org/freedesktop/network1/network", &name);
113         if (r < 0)
114                 return 0;
115
116         r = network_get_by_name(m, name, &network);
117         if (r < 0)
118                 return 0;
119
120         *found = network;
121
122         return 1;
123 }