]> git.meshlink.io Git - catta/blob - avahi-daemon/dbus-entry-group.c
get rid of a lot of old svn cruft
[catta] / avahi-daemon / dbus-entry-group.c
1 /***
2   This file is part of avahi.
3
4   avahi is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   avahi is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with avahi; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <string.h>
25
26 #include <avahi-common/malloc.h>
27 #include <avahi-common/dbus.h>
28 #include <avahi-common/error.h>
29 #include <avahi-common/domain.h>
30 #include <avahi-core/log.h>
31
32 #include "dbus-util.h"
33 #include "dbus-internal.h"
34 #include "main.h"
35
36 void avahi_dbus_entry_group_free(EntryGroupInfo *i) {
37     assert(i);
38
39     if (i->entry_group)
40         avahi_s_entry_group_free(i->entry_group);
41
42     if (i->path) {
43         dbus_connection_unregister_object_path(server->bus, i->path);
44         avahi_free(i->path);
45     }
46     AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
47
48     i->client->n_objects--;
49     assert(i->client->n_objects >= 0);
50
51     avahi_free(i);
52 }
53
54 void avahi_dbus_entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void* userdata) {
55     EntryGroupInfo *i = userdata;
56     DBusMessage *m;
57     int32_t t;
58     const char *e;
59
60     assert(s);
61     assert(g);
62     assert(i);
63
64     m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
65
66     t = (int32_t) state;
67     if (state == AVAHI_ENTRY_GROUP_FAILURE)
68         e = avahi_error_number_to_dbus(avahi_server_errno(s));
69     else if (state == AVAHI_ENTRY_GROUP_COLLISION)
70         e = AVAHI_DBUS_ERR_COLLISION;
71     else
72         e = AVAHI_DBUS_ERR_OK;
73
74     dbus_message_append_args(
75         m,
76         DBUS_TYPE_INT32, &t,
77         DBUS_TYPE_STRING, &e,
78         DBUS_TYPE_INVALID);
79     dbus_message_set_destination(m, i->client->name);
80     dbus_connection_send(server->bus, m, NULL);
81     dbus_message_unref(m);
82 }
83
84 DBusHandlerResult avahi_dbus_msg_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
85     DBusError error;
86     EntryGroupInfo *i = userdata;
87
88     assert(c);
89     assert(m);
90     assert(i);
91
92     dbus_error_init(&error);
93
94     avahi_log_debug(__FILE__": interface=%s, path=%s, member=%s",
95                     dbus_message_get_interface(m),
96                     dbus_message_get_path(m),
97                     dbus_message_get_member(m));
98
99     /* Introspection */
100     if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
101         return avahi_dbus_handle_introspect(c, m, "EntryGroup.introspect");
102
103     /* Access control */
104     if (strcmp(dbus_message_get_sender(m), i->client->name))
105         return avahi_dbus_respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
106
107     if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) {
108
109         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
110             avahi_log_warn("Error parsing EntryGroup::Free message");
111             goto fail;
112         }
113
114         avahi_dbus_entry_group_free(i);
115         return avahi_dbus_respond_ok(c, m);
116
117     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit")) {
118
119         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
120             avahi_log_warn("Error parsing EntryGroup::Commit message");
121             goto fail;
122         }
123
124         if (avahi_s_entry_group_commit(i->entry_group) < 0)
125             return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
126
127         return avahi_dbus_respond_ok(c, m);
128
129
130     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset")) {
131
132         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
133             avahi_log_warn("Error parsing EntryGroup::Reset message");
134             goto fail;
135         }
136
137         avahi_s_entry_group_reset(i->entry_group);
138         i->n_entries = 0;
139         return avahi_dbus_respond_ok(c, m);
140
141     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) {
142
143         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
144             avahi_log_warn("Error parsing EntryGroup::IsEmpty message");
145             goto fail;
146         }
147
148         return avahi_dbus_respond_boolean(c, m, !!avahi_s_entry_group_is_empty(i->entry_group));
149
150     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) {
151         AvahiEntryGroupState state;
152
153         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
154             avahi_log_warn("Error parsing EntryGroup::GetState message");
155             goto fail;
156         }
157
158         state = avahi_s_entry_group_get_state(i->entry_group);
159         return avahi_dbus_respond_int32(c, m, (int32_t) state);
160
161     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
162         int32_t interface, protocol;
163         uint32_t flags;
164         char *type, *name, *domain, *host;
165         uint16_t port;
166         AvahiStringList *strlst = NULL;
167
168         if (!dbus_message_get_args(
169                 m, &error,
170                 DBUS_TYPE_INT32, &interface,
171                 DBUS_TYPE_INT32, &protocol,
172                 DBUS_TYPE_UINT32, &flags,
173                 DBUS_TYPE_STRING, &name,
174                 DBUS_TYPE_STRING, &type,
175                 DBUS_TYPE_STRING, &domain,
176                 DBUS_TYPE_STRING, &host,
177                 DBUS_TYPE_UINT16, &port,
178                 DBUS_TYPE_INVALID) ||
179             !type || !name ||
180             avahi_dbus_read_strlst(m, 8, &strlst) < 0) {
181             avahi_log_warn("Error parsing EntryGroup::AddService message");
182             goto fail;
183         }
184
185         if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX) {
186             avahi_string_list_free(strlst);
187             return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
188         }
189
190         if (domain && !*domain)
191             domain = NULL;
192
193         if (host && !*host)
194             host = NULL;
195
196         if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, host, port, strlst) < 0) {
197             avahi_string_list_free(strlst);
198             return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
199         }
200
201         if (!(flags & AVAHI_PUBLISH_UPDATE))
202             i->n_entries ++;
203
204         avahi_string_list_free(strlst);
205
206         return avahi_dbus_respond_ok(c, m);
207
208     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddServiceSubtype")) {
209
210         int32_t interface, protocol;
211         uint32_t flags;
212         char *type, *name, *domain, *subtype;
213
214         if (!dbus_message_get_args(
215                 m, &error,
216                 DBUS_TYPE_INT32, &interface,
217                 DBUS_TYPE_INT32, &protocol,
218                 DBUS_TYPE_UINT32, &flags,
219                 DBUS_TYPE_STRING, &name,
220                 DBUS_TYPE_STRING, &type,
221                 DBUS_TYPE_STRING, &domain,
222                 DBUS_TYPE_STRING, &subtype,
223                 DBUS_TYPE_INVALID) || !type || !name || !subtype) {
224             avahi_log_warn("Error parsing EntryGroup::AddServiceSubtype message");
225             goto fail;
226         }
227
228         if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
229             return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
230
231         if (domain && !*domain)
232             domain = NULL;
233
234         if (avahi_server_add_service_subtype(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, subtype) < 0)
235             return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
236
237         if (!(flags & AVAHI_PUBLISH_UPDATE))
238             i->n_entries ++;
239
240         return avahi_dbus_respond_ok(c, m);
241
242     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "UpdateServiceTxt")) {
243         int32_t interface, protocol;
244         uint32_t flags;
245         char *type, *name, *domain;
246         AvahiStringList *strlst;
247
248         if (!dbus_message_get_args(
249                 m, &error,
250                 DBUS_TYPE_INT32, &interface,
251                 DBUS_TYPE_INT32, &protocol,
252                 DBUS_TYPE_UINT32, &flags,
253                 DBUS_TYPE_STRING, &name,
254                 DBUS_TYPE_STRING, &type,
255                 DBUS_TYPE_STRING, &domain,
256                 DBUS_TYPE_INVALID) ||
257             !type || !name ||
258             avahi_dbus_read_strlst(m, 6, &strlst)) {
259             avahi_log_warn("Error parsing EntryGroup::UpdateServiceTxt message");
260             goto fail;
261         }
262
263         if (domain && !*domain)
264             domain = NULL;
265
266         if (avahi_server_update_service_txt_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, strlst) < 0) {
267             avahi_string_list_free(strlst);
268             return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
269         }
270
271         avahi_string_list_free(strlst);
272
273         return avahi_dbus_respond_ok(c, m);
274
275     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
276         int32_t interface, protocol;
277         uint32_t flags;
278         char *name, *address;
279         AvahiAddress a;
280
281         if (!dbus_message_get_args(
282                 m, &error,
283                 DBUS_TYPE_INT32, &interface,
284                 DBUS_TYPE_INT32, &protocol,
285                 DBUS_TYPE_UINT32, &flags,
286                 DBUS_TYPE_STRING, &name,
287                 DBUS_TYPE_STRING, &address,
288                 DBUS_TYPE_INVALID) || !name || !address) {
289             avahi_log_warn("Error parsing EntryGroup::AddAddress message");
290             goto fail;
291         }
292
293         if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
294             return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
295
296         if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a)))
297             return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
298
299         if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, &a) < 0)
300             return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
301
302         if (!(flags & AVAHI_PUBLISH_UPDATE))
303             i->n_entries ++;
304
305         return avahi_dbus_respond_ok(c, m);
306     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddRecord")) {
307         int32_t interface, protocol;
308         uint32_t flags, ttl, size;
309         uint16_t clazz, type;
310         char *name;
311         void *rdata;
312         AvahiRecord *r;
313
314         if (!dbus_message_get_args(
315                 m, &error,
316                 DBUS_TYPE_INT32, &interface,
317                 DBUS_TYPE_INT32, &protocol,
318                 DBUS_TYPE_UINT32, &flags,
319                 DBUS_TYPE_STRING, &name,
320                 DBUS_TYPE_UINT16, &clazz,
321                 DBUS_TYPE_UINT16, &type,
322                 DBUS_TYPE_UINT32, &ttl,
323                 DBUS_TYPE_INVALID) || !name ||
324             avahi_dbus_read_rdata (m, 7, &rdata, &size)) {
325             avahi_log_warn("Error parsing EntryGroup::AddRecord message");
326             goto fail;
327         }
328
329         if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
330             return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
331
332         if (!avahi_is_valid_domain_name (name))
333             return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_DOMAIN_NAME, NULL);
334
335         if (!(r = avahi_record_new_full (name, clazz, type, ttl)))
336             return avahi_dbus_respond_error(c, m, AVAHI_ERR_NO_MEMORY, NULL);
337
338         if (avahi_rdata_parse (r, rdata, size) < 0) {
339             avahi_record_unref (r);
340             return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_RDATA, NULL);
341         }
342
343         if (avahi_server_add(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, r) < 0) {
344             avahi_record_unref (r);
345             return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
346         }
347
348         if (!(flags & AVAHI_PUBLISH_UPDATE))
349             i->n_entries ++;
350
351         avahi_record_unref (r);
352
353         return avahi_dbus_respond_ok(c, m);
354     }
355
356
357     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
358
359 fail:
360     if (dbus_error_is_set(&error))
361         dbus_error_free(&error);
362
363     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
364 }