]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-network-bus.c
util-lib: split out allocation calls into alloc-util.[ch]
[thirdparty/systemd.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 "alloc-util.h"
23 #include "networkd.h"
24 #include "string-util.h"
25 #include "strv.h"
26
27 static int property_get_ether_addrs(
28 sd_bus *bus,
29 const char *path,
30 const char *interface,
31 const char *property,
32 sd_bus_message *reply,
33 void *userdata,
34 sd_bus_error *error) {
35
36 Network *n = userdata;
37 const char *ether = NULL;
38 int r;
39
40 assert(bus);
41 assert(reply);
42 assert(n);
43
44 if (n->match_mac)
45 ether = ether_ntoa(n->match_mac);
46
47 r = sd_bus_message_open_container(reply, 'a', "s");
48 if (r < 0)
49 return r;
50
51 if (ether) {
52 r = sd_bus_message_append(reply, "s", strempty(ether));
53 if (r < 0)
54 return r;
55 }
56
57 return sd_bus_message_close_container(reply);
58 }
59
60 const sd_bus_vtable network_vtable[] = {
61 SD_BUS_VTABLE_START(0),
62
63 SD_BUS_PROPERTY("Description", "s", NULL, offsetof(Network, description), SD_BUS_VTABLE_PROPERTY_CONST),
64 SD_BUS_PROPERTY("SourcePath", "s", NULL, offsetof(Network, filename), SD_BUS_VTABLE_PROPERTY_CONST),
65 SD_BUS_PROPERTY("MatchMAC", "as", property_get_ether_addrs, offsetof(Network, match_mac), SD_BUS_VTABLE_PROPERTY_CONST),
66 SD_BUS_PROPERTY("MatchPath", "as", NULL, offsetof(Network, match_path), SD_BUS_VTABLE_PROPERTY_CONST),
67 SD_BUS_PROPERTY("MatchDriver", "as", NULL, offsetof(Network, match_driver), SD_BUS_VTABLE_PROPERTY_CONST),
68 SD_BUS_PROPERTY("MatchType", "as", NULL, offsetof(Network, match_type), SD_BUS_VTABLE_PROPERTY_CONST),
69 SD_BUS_PROPERTY("MatchName", "as", NULL, offsetof(Network, match_name), SD_BUS_VTABLE_PROPERTY_CONST),
70
71 SD_BUS_VTABLE_END
72 };
73
74 static char *network_bus_path(Network *network) {
75 _cleanup_free_ char *name = NULL;
76 char *networkname, *d, *path;
77 int r;
78
79 assert(network);
80 assert(network->filename);
81
82 name = strdup(network->filename);
83 if (!name)
84 return NULL;
85
86 networkname = basename(name);
87
88 d = strrchr(networkname, '.');
89 if (!d)
90 return NULL;
91
92 assert(streq(d, ".network"));
93
94 *d = '\0';
95
96 r = sd_bus_path_encode("/org/freedesktop/network1/network", networkname, &path);
97 if (r < 0)
98 return NULL;
99
100 return path;
101 }
102
103 int network_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
104 _cleanup_strv_free_ char **l = NULL;
105 Manager *m = userdata;
106 Network *network;
107 int r;
108
109 assert(bus);
110 assert(path);
111 assert(m);
112 assert(nodes);
113
114 LIST_FOREACH(networks, network, m->networks) {
115 char *p;
116
117 p = network_bus_path(network);
118 if (!p)
119 return -ENOMEM;
120
121 r = strv_consume(&l, p);
122 if (r < 0)
123 return r;
124 }
125
126 *nodes = l;
127 l = NULL;
128
129 return 1;
130 }
131
132 int network_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
133 Manager *m = userdata;
134 Network *network;
135 _cleanup_free_ char *name = NULL;
136 int r;
137
138 assert(bus);
139 assert(path);
140 assert(interface);
141 assert(m);
142 assert(found);
143
144 r = sd_bus_path_decode(path, "/org/freedesktop/network1/network", &name);
145 if (r < 0)
146 return 0;
147
148 r = network_get_by_name(m, name, &network);
149 if (r < 0)
150 return 0;
151
152 *found = network;
153
154 return 1;
155 }