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