]> git.meshlink.io Git - catta/blob - avahi-daemon/dbus-entry-group.c
* split dbus-protocol.c in multiple sources files
[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         avahi_s_entry_group_commit(i->entry_group);
124         return avahi_dbus_respond_ok(c, m);
125         
126         
127     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset")) {
128         
129         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
130             avahi_log_warn("Error parsing EntryGroup::Reset message");
131             goto fail;
132         }
133
134         avahi_s_entry_group_reset(i->entry_group);
135         i->n_entries = 0;
136         return avahi_dbus_respond_ok(c, m);
137         
138     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) {
139
140         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
141             avahi_log_warn("Error parsing EntryGroup::IsEmpty message");
142             goto fail;
143         }
144
145         return avahi_dbus_respond_boolean(c, m, !!avahi_s_entry_group_is_empty(i->entry_group));
146         
147     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) {
148         AvahiEntryGroupState state;
149         
150         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
151             avahi_log_warn("Error parsing EntryGroup::GetState message");
152             goto fail;
153         }
154
155         state = avahi_s_entry_group_get_state(i->entry_group);
156         return avahi_dbus_respond_int32(c, m, (int32_t) state);
157         
158     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
159         int32_t interface, protocol;
160         uint32_t flags;
161         char *type, *name, *domain, *host;
162         uint16_t port;
163         AvahiStringList *strlst = NULL;
164         
165         if (!dbus_message_get_args(
166                 m, &error,
167                 DBUS_TYPE_INT32, &interface,
168                 DBUS_TYPE_INT32, &protocol,
169                 DBUS_TYPE_UINT32, &flags,
170                 DBUS_TYPE_STRING, &name,
171                 DBUS_TYPE_STRING, &type,
172                 DBUS_TYPE_STRING, &domain,
173                 DBUS_TYPE_STRING, &host,
174                 DBUS_TYPE_UINT16, &port, 
175                 DBUS_TYPE_INVALID) ||
176             !type || !name ||
177             avahi_dbus_read_strlst(m, 8, &strlst) < 0) {
178             avahi_log_warn("Error parsing EntryGroup::AddService message");
179             goto fail;
180         }
181
182         if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX) {
183             avahi_string_list_free(strlst);
184             return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
185         }
186
187         if (domain && !*domain)
188             domain = NULL;
189
190         if (host && !*host)
191             host = NULL;
192
193         if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, host, port, strlst) < 0) {
194             avahi_string_list_free(strlst);
195             return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
196         }
197
198         if (!(flags & AVAHI_PUBLISH_UPDATE))
199             i->n_entries ++;
200             
201         avahi_string_list_free(strlst);
202         
203         return avahi_dbus_respond_ok(c, m);
204         
205     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddServiceSubtype")) {
206
207         int32_t interface, protocol;
208         uint32_t flags;
209         char *type, *name, *domain, *subtype;
210         
211         if (!dbus_message_get_args(
212                 m, &error,
213                 DBUS_TYPE_INT32, &interface,
214                 DBUS_TYPE_INT32, &protocol,
215                 DBUS_TYPE_UINT32, &flags,
216                 DBUS_TYPE_STRING, &name,
217                 DBUS_TYPE_STRING, &type,
218                 DBUS_TYPE_STRING, &domain,
219                 DBUS_TYPE_STRING, &subtype,
220                 DBUS_TYPE_INVALID) || !type || !name || !subtype) {
221             avahi_log_warn("Error parsing EntryGroup::AddServiceSubtype message");
222             goto fail;
223         }
224
225         if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
226             return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
227
228         if (domain && !*domain)
229             domain = NULL;
230
231         if (avahi_server_add_service_subtype(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, subtype) < 0) 
232             return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
233
234         if (!(flags & AVAHI_PUBLISH_UPDATE))
235             i->n_entries ++;
236         
237         return avahi_dbus_respond_ok(c, m);
238
239     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "UpdateServiceTxt")) {
240         int32_t interface, protocol;
241         uint32_t flags;
242         char *type, *name, *domain;
243         AvahiStringList *strlst;
244         
245         if (!dbus_message_get_args(
246                 m, &error,
247                 DBUS_TYPE_INT32, &interface,
248                 DBUS_TYPE_INT32, &protocol,
249                 DBUS_TYPE_UINT32, &flags,
250                 DBUS_TYPE_STRING, &name,
251                 DBUS_TYPE_STRING, &type,
252                 DBUS_TYPE_STRING, &domain,
253                 DBUS_TYPE_INVALID) ||
254             !type || !name ||
255             avahi_dbus_read_strlst(m, 6, &strlst)) {
256             avahi_log_warn("Error parsing EntryGroup::UpdateServiceTxt message");
257             goto fail;
258         }
259
260         if (domain && !*domain)
261             domain = NULL;
262
263         if (avahi_server_update_service_txt_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, type, domain, strlst) < 0) {
264             avahi_string_list_free(strlst);
265             return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
266         }
267
268         avahi_string_list_free(strlst);
269         
270         return avahi_dbus_respond_ok(c, m);
271         
272     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
273         int32_t interface, protocol;
274         uint32_t flags;
275         char *name, *address;
276         AvahiAddress a;
277         
278         if (!dbus_message_get_args(
279                 m, &error,
280                 DBUS_TYPE_INT32, &interface,
281                 DBUS_TYPE_INT32, &protocol,
282                 DBUS_TYPE_UINT32, &flags,
283                 DBUS_TYPE_STRING, &name,
284                 DBUS_TYPE_STRING, &address,
285                 DBUS_TYPE_INVALID) || !name || !address) {
286             avahi_log_warn("Error parsing EntryGroup::AddAddress message");
287             goto fail;
288         }
289
290         if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
291             return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
292         
293         if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a)))
294             return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
295
296         if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, name, &a) < 0)
297             return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
298
299         if (!(flags & AVAHI_PUBLISH_UPDATE))
300             i->n_entries ++;
301         
302         return avahi_dbus_respond_ok(c, m);
303     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddRecord")) {
304         int32_t interface, protocol;
305         uint32_t flags, ttl, size;
306         uint16_t clazz, type;
307         char *name;
308         void *rdata;
309         AvahiRecord *r;
310         
311         if (!dbus_message_get_args(
312                 m, &error,
313                 DBUS_TYPE_INT32, &interface,
314                 DBUS_TYPE_INT32, &protocol,
315                 DBUS_TYPE_UINT32, &flags,
316                 DBUS_TYPE_STRING, &name,
317                 DBUS_TYPE_UINT16, &clazz,
318                 DBUS_TYPE_UINT16, &type,
319                 DBUS_TYPE_UINT32, &ttl,
320                 DBUS_TYPE_INVALID) || !name ||
321             avahi_dbus_read_rdata (m, 7, &rdata, &size)) {
322             avahi_log_warn("Error parsing EntryGroup::AddRecord message");
323             goto fail;
324         }
325
326         if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
327             return avahi_dbus_respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
328
329         if (!avahi_is_valid_domain_name (name))
330             return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_DOMAIN_NAME, NULL);
331
332         if (!(r = avahi_record_new_full (name, clazz, type, ttl)))
333             return avahi_dbus_respond_error(c, m, AVAHI_ERR_NO_MEMORY, NULL);
334
335         if (avahi_rdata_parse (r, rdata, size) < 0) {
336             avahi_record_unref (r);
337             return avahi_dbus_respond_error(c, m, AVAHI_ERR_INVALID_RDATA, NULL);
338         }
339         
340         if (avahi_server_add(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, r) < 0) {
341             avahi_record_unref (r);
342             return avahi_dbus_respond_error(c, m, avahi_server_errno(avahi_server), NULL);
343         }
344
345         if (!(flags & AVAHI_PUBLISH_UPDATE))
346             i->n_entries ++;
347        
348         avahi_record_unref (r); 
349
350         return avahi_dbus_respond_ok(c, m);
351     } 
352  
353     
354     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
355
356 fail:
357     if (dbus_error_is_set(&error))
358         dbus_error_free(&error);
359     
360     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
361 }