]> git.meshlink.io Git - catta/blob - avahi-daemon/dbus-protocol.c
* strip glib from avahi-core
[catta] / avahi-daemon / dbus-protocol.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 #include <sys/ioctl.h>
28 #include <net/if.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <glib.h>
32
33 #define DBUS_API_SUBJECT_TO_CHANGE
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
36
37 #include <avahi-common/llist.h>
38 #include <avahi-core/log.h>
39 #include <avahi-core/core.h>
40 #include <avahi-common/dbus.h>
41
42 #include "dbus-protocol.h"
43 #include "main.h"
44
45 typedef struct Server Server;
46 typedef struct Client Client;
47 typedef struct EntryGroupInfo EntryGroupInfo;
48 typedef struct HostNameResolverInfo HostNameResolverInfo;
49 typedef struct AddressResolverInfo AddressResolverInfo;
50 typedef struct DomainBrowserInfo DomainBrowserInfo;
51 typedef struct ServiceTypeBrowserInfo ServiceTypeBrowserInfo;
52 typedef struct ServiceBrowserInfo ServiceBrowserInfo;
53 typedef struct ServiceResolverInfo ServiceResolverInfo;
54
55 #define MAX_CLIENTS 20
56 #define MAX_OBJECTS_PER_CLIENT 50
57 #define MAX_ENTRIES_PER_ENTRY_GROUP 20
58
59 /* #define VALGRIND_WORKAROUND */
60
61 struct EntryGroupInfo {
62     guint id;
63     Client *client;
64     AvahiEntryGroup *entry_group;
65     gchar *path;
66
67     gint n_entries;
68     
69     AVAHI_LLIST_FIELDS(EntryGroupInfo, entry_groups);
70 };
71
72 struct HostNameResolverInfo {
73     Client *client;
74     AvahiHostNameResolver *host_name_resolver;
75     DBusMessage *message;
76
77     AVAHI_LLIST_FIELDS(HostNameResolverInfo, host_name_resolvers);
78 };
79
80 struct AddressResolverInfo {
81     Client *client;
82     AvahiAddressResolver *address_resolver;
83     DBusMessage *message;
84
85     AVAHI_LLIST_FIELDS(AddressResolverInfo, address_resolvers);
86 };
87
88 struct DomainBrowserInfo {
89     guint id;
90     Client *client;
91     AvahiDomainBrowser *domain_browser;
92     gchar *path;
93
94     AVAHI_LLIST_FIELDS(DomainBrowserInfo, domain_browsers);
95 };
96
97 struct ServiceTypeBrowserInfo {
98     guint id;
99     Client *client;
100     AvahiServiceTypeBrowser *service_type_browser;
101     gchar *path;
102
103     AVAHI_LLIST_FIELDS(ServiceTypeBrowserInfo, service_type_browsers);
104 };
105
106 struct ServiceBrowserInfo {
107     guint id;
108     Client *client;
109     AvahiServiceBrowser *service_browser;
110     gchar *path;
111
112     AVAHI_LLIST_FIELDS(ServiceBrowserInfo, service_browsers);
113 };
114
115 struct ServiceResolverInfo {
116     Client *client;
117     AvahiServiceResolver *service_resolver;
118     DBusMessage *message;
119
120     AVAHI_LLIST_FIELDS(ServiceResolverInfo, service_resolvers);
121 };
122
123 struct Client {
124     guint id;
125     gchar *name;
126     guint current_id;
127     gint n_objects;
128     
129     AVAHI_LLIST_FIELDS(Client, clients);
130     AVAHI_LLIST_HEAD(EntryGroupInfo, entry_groups);
131     AVAHI_LLIST_HEAD(HostNameResolverInfo, host_name_resolvers);
132     AVAHI_LLIST_HEAD(AddressResolverInfo, address_resolvers);
133     AVAHI_LLIST_HEAD(DomainBrowserInfo, domain_browsers);
134     AVAHI_LLIST_HEAD(ServiceTypeBrowserInfo, service_type_browsers);
135     AVAHI_LLIST_HEAD(ServiceBrowserInfo, service_browsers);
136     AVAHI_LLIST_HEAD(ServiceResolverInfo, service_resolvers);
137 };
138
139 struct Server {
140     DBusConnection *bus;
141     AVAHI_LLIST_HEAD(Client, clients);
142     gint n_clients;
143     guint current_id;
144 };
145
146 static Server *server = NULL;
147
148 static void entry_group_free(EntryGroupInfo *i) {
149     g_assert(i);
150
151     if (i->entry_group)
152         avahi_entry_group_free(i->entry_group);
153     dbus_connection_unregister_object_path(server->bus, i->path);
154     g_free(i->path);
155     AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
156
157     i->client->n_objects--;
158     g_assert(i->client->n_objects >= 0);
159     
160     g_free(i);
161  }
162
163 static void host_name_resolver_free(HostNameResolverInfo *i) {
164     g_assert(i);
165
166     if (i->host_name_resolver)
167         avahi_host_name_resolver_free(i->host_name_resolver);
168     dbus_message_unref(i->message);
169     AVAHI_LLIST_REMOVE(HostNameResolverInfo, host_name_resolvers, i->client->host_name_resolvers, i);
170
171     i->client->n_objects--;
172     g_assert(i->client->n_objects >= 0);
173
174     g_free(i);
175 }
176
177 static void address_resolver_free(AddressResolverInfo *i) {
178     g_assert(i);
179
180     if (i->address_resolver)
181         avahi_address_resolver_free(i->address_resolver);
182     dbus_message_unref(i->message);
183     AVAHI_LLIST_REMOVE(AddressResolverInfo, address_resolvers, i->client->address_resolvers, i);
184
185     i->client->n_objects--;
186     g_assert(i->client->n_objects >= 0);
187
188     g_free(i);
189 }
190
191 static void domain_browser_free(DomainBrowserInfo *i) {
192     g_assert(i);
193
194     if (i->domain_browser)
195         avahi_domain_browser_free(i->domain_browser);
196     dbus_connection_unregister_object_path(server->bus, i->path);
197     g_free(i->path);
198     AVAHI_LLIST_REMOVE(DomainBrowserInfo, domain_browsers, i->client->domain_browsers, i);
199
200     i->client->n_objects--;
201     g_assert(i->client->n_objects >= 0);
202
203     g_free(i);
204 }
205
206 static void service_type_browser_free(ServiceTypeBrowserInfo *i) {
207     g_assert(i);
208
209     if (i->service_type_browser)
210         avahi_service_type_browser_free(i->service_type_browser);
211     dbus_connection_unregister_object_path(server->bus, i->path);
212     g_free(i->path);
213     AVAHI_LLIST_REMOVE(ServiceTypeBrowserInfo, service_type_browsers, i->client->service_type_browsers, i);
214
215     i->client->n_objects--;
216     g_assert(i->client->n_objects >= 0);
217
218     g_free(i);
219 }
220
221 static void service_browser_free(ServiceBrowserInfo *i) {
222     g_assert(i);
223
224     if (i->service_browser)
225         avahi_service_browser_free(i->service_browser);
226     dbus_connection_unregister_object_path(server->bus, i->path);
227     g_free(i->path);
228     AVAHI_LLIST_REMOVE(ServiceBrowserInfo, service_browsers, i->client->service_browsers, i);
229
230     i->client->n_objects--;
231     g_assert(i->client->n_objects >= 0);
232
233     g_free(i);
234 }
235
236 static void service_resolver_free(ServiceResolverInfo *i) {
237     g_assert(i);
238
239     if (i->service_resolver)
240         avahi_service_resolver_free(i->service_resolver);
241     dbus_message_unref(i->message);
242     AVAHI_LLIST_REMOVE(ServiceResolverInfo, service_resolvers, i->client->service_resolvers, i);
243
244     i->client->n_objects--;
245     g_assert(i->client->n_objects >= 0);
246
247     g_free(i);
248 }
249
250 static void client_free(Client *c) {
251     
252     g_assert(server);
253     g_assert(c);
254
255     while (c->entry_groups)
256         entry_group_free(c->entry_groups);
257
258     while (c->host_name_resolvers)
259         host_name_resolver_free(c->host_name_resolvers);
260
261     while (c->address_resolvers)
262         address_resolver_free(c->address_resolvers);
263
264     while (c->domain_browsers)
265         domain_browser_free(c->domain_browsers);
266
267     while (c->service_type_browsers)
268         service_type_browser_free(c->service_type_browsers);
269
270     while (c->service_browsers)
271         service_browser_free(c->service_browsers);
272
273     while (c->service_resolvers)
274         service_resolver_free(c->service_resolvers);
275
276     g_assert(c->n_objects == 0);
277     
278     g_free(c->name);
279     AVAHI_LLIST_REMOVE(Client, clients, server->clients, c);
280     g_free(c);
281
282     server->n_clients --;
283     g_assert(server->n_clients >= 0);
284 }
285
286 static Client *client_get(const gchar *name, gboolean create) {
287     Client *client;
288
289     g_assert(server);
290     g_assert(name);
291
292     for (client = server->clients; client; client = client->clients_next)
293         if (!strcmp(name, client->name))
294             return client;
295
296     if (!create)
297         return NULL;
298
299     if (server->n_clients >= MAX_CLIENTS)
300         return NULL;
301     
302     /* If not existant yet, create a new entry */
303     client = g_new(Client, 1);
304     client->id = server->current_id++;
305     client->name = g_strdup(name);
306     client->current_id = 0;
307     client->n_objects = 0;
308     AVAHI_LLIST_HEAD_INIT(EntryGroupInfo, client->entry_groups);
309     AVAHI_LLIST_HEAD_INIT(HostNameResolverInfo, client->host_name_resolvers);
310     AVAHI_LLIST_HEAD_INIT(AddressResolverInfo, client->address_resolvers);
311     AVAHI_LLIST_HEAD_INIT(DomainBrowserInfo, client->domain_browsers);
312     AVAHI_LLIST_HEAD_INIT(ServiceTypeBrowserInfo, client->service_type_browsers);
313     AVAHI_LLIST_HEAD_INIT(ServiceBrowserInfo, client->service_browsers);
314     AVAHI_LLIST_HEAD_INIT(ServiceResolverInfo, client->service_resolvers);
315
316     AVAHI_LLIST_PREPEND(Client, clients, server->clients, client);
317
318     server->n_clients++;
319     g_assert(server->n_clients > 0);
320     
321     return client;
322 }
323
324 static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, gint error, const gchar *text) {
325     DBusMessage *reply;
326
327     const gchar * const table[- AVAHI_ERR_MAX] = {
328         NULL, /* OK */
329         AVAHI_DBUS_ERR_FAILURE,
330         AVAHI_DBUS_ERR_BAD_STATE,
331         AVAHI_DBUS_ERR_INVALID_HOST_NAME,
332         AVAHI_DBUS_ERR_INVALID_DOMAIN_NAME,
333         AVAHI_DBUS_ERR_NO_NETWORK,
334         AVAHI_DBUS_ERR_INVALID_TTL,
335         AVAHI_DBUS_ERR_IS_PATTERN,
336         AVAHI_DBUS_ERR_LOCAL_COLLISION,
337         AVAHI_DBUS_ERR_INVALID_RECORD,
338         AVAHI_DBUS_ERR_INVALID_SERVICE_NAME,
339         AVAHI_DBUS_ERR_INVALID_SERVICE_TYPE,
340         AVAHI_DBUS_ERR_INVALID_PORT,
341         AVAHI_DBUS_ERR_INVALID_KEY,
342         AVAHI_DBUS_ERR_INVALID_ADDRESS,
343         AVAHI_DBUS_ERR_TIMEOUT,
344         AVAHI_DBUS_ERR_TOO_MANY_CLIENTS,
345         AVAHI_DBUS_ERR_TOO_MANY_OBJECTS,
346         AVAHI_DBUS_ERR_TOO_MANY_ENTRIES,
347         AVAHI_DBUS_ERR_OS,
348         AVAHI_DBUS_ERR_ACCESS_DENIED,
349         AVAHI_DBUS_ERR_INVALID_OPERATION
350     };
351
352     g_assert(-error > -AVAHI_OK);
353     g_assert(-error < -AVAHI_ERR_MAX);
354     
355     reply = dbus_message_new_error(m, table[-error], text ? text : avahi_strerror(error));
356     dbus_connection_send(c, reply, NULL);
357     dbus_message_unref(reply);
358     
359     return DBUS_HANDLER_RESULT_HANDLED;
360 }
361
362 static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const gchar *text) {
363     DBusMessage *reply;
364
365     reply = dbus_message_new_method_return(m);
366     dbus_message_append_args(reply, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
367     dbus_connection_send(c, reply, NULL);
368     dbus_message_unref(reply);
369     
370     return DBUS_HANDLER_RESULT_HANDLED;
371 }
372
373 static DBusHandlerResult respond_int32(DBusConnection *c, DBusMessage *m, gint32 i) {
374     DBusMessage *reply;
375
376     reply = dbus_message_new_method_return(m);
377     dbus_message_append_args(reply, DBUS_TYPE_INT32, &i, DBUS_TYPE_INVALID);
378     dbus_connection_send(c, reply, NULL);
379     dbus_message_unref(reply);
380     
381     return DBUS_HANDLER_RESULT_HANDLED;
382 }
383
384 static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) {
385     DBusMessage *reply;
386
387     reply = dbus_message_new_method_return(m);
388     dbus_connection_send(c, reply, NULL);
389     dbus_message_unref(reply);
390     
391     return DBUS_HANDLER_RESULT_HANDLED;
392 }
393
394 static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const gchar *path) {
395     DBusMessage *reply;
396
397     reply = dbus_message_new_method_return(m);
398     dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
399     dbus_connection_send(c, reply, NULL);
400     dbus_message_unref(reply);
401     
402     return DBUS_HANDLER_RESULT_HANDLED;
403 }
404
405 static DBusHandlerResult handle_introspect(DBusConnection *c, DBusMessage *m, const gchar *fname) {
406     gchar *path = NULL;
407     gchar *contents;
408     GError *gerror = NULL;
409     DBusError error;
410     
411     g_assert(c);
412     g_assert(m);
413     g_assert(fname);
414
415     dbus_error_init(&error);
416
417     if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
418         avahi_log_warn("Error parsing Introspect message: %s", error.message);
419         goto fail;
420     }
421     
422     path = g_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname);
423
424     if (!(g_file_get_contents(path, &contents, NULL, &gerror))) {
425         avahi_log_warn("Failed to load introspection data: %s", gerror->message);
426         g_error_free(gerror);
427         g_free(path);
428         goto fail;
429     }
430
431     g_free(path);
432     
433     respond_string(c, m, contents);
434     g_free(contents);
435     
436     return DBUS_HANDLER_RESULT_HANDLED;
437
438 fail:
439     if (dbus_error_is_set(&error))
440         dbus_error_free(&error);
441     
442     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
443
444 }
445
446 static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
447     GMainLoop *loop = userdata;
448     DBusError error;
449
450     dbus_error_init(&error);
451
452 /*     avahi_log_debug("dbus: interface=%s, path=%s, member=%s", */
453 /*                     dbus_message_get_interface(m), */
454 /*                     dbus_message_get_path(m), */
455 /*                     dbus_message_get_member(m)); */
456
457     if (dbus_message_is_signal(m, DBUS_INTERFACE_LOCAL, "Disconnected")) {
458         /* No, we shouldn't quit, but until we get somewhere
459          * usefull such that we can restore our state, we will */
460         avahi_log_warn("Disconnnected from d-bus, terminating...");
461         g_main_loop_quit (loop);
462         return DBUS_HANDLER_RESULT_HANDLED;
463         
464     } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameAcquired")) {
465         gchar *name;
466
467         if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
468             avahi_log_warn("Error parsing NameAcquired message");
469             goto fail;
470         }
471
472 /*         avahi_log_info("dbus: name acquired (%s)", name); */
473         return DBUS_HANDLER_RESULT_HANDLED;
474         
475     } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
476         gchar *name, *old, *new;
477
478         if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID)) {
479             avahi_log_warn("Error parsing NameOwnerChanged message");
480             goto fail;
481         }
482
483         if (!*new) {
484             Client *client;
485
486             if ((client = client_get(name, FALSE))) {
487 /*                 avahi_log_info("dbus: client %s vanished", name); */
488                 client_free(client);
489             }
490         }
491     }
492
493 fail:
494     if (dbus_error_is_set(&error))
495         dbus_error_free(&error);
496     
497     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
498 }
499
500 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) {
501     EntryGroupInfo *i = userdata;
502     DBusMessage *m;
503     gint32 t;
504     
505     g_assert(s);
506     g_assert(g);
507     g_assert(i);
508
509     m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
510     t = (gint32) state;
511     dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
512     dbus_message_set_destination(m, i->client->name);  
513     dbus_connection_send(server->bus, m, NULL);
514     dbus_message_unref(m);
515 }
516
517 static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
518     DBusError error;
519     EntryGroupInfo *i = userdata;
520
521     g_assert(c);
522     g_assert(m);
523     g_assert(i);
524     
525     dbus_error_init(&error);
526
527     avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
528                     dbus_message_get_interface(m),
529                     dbus_message_get_path(m),
530                     dbus_message_get_member(m));
531
532     /* Introspection */
533     if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
534         return handle_introspect(c, m, "EntryGroup.introspect");
535     
536     /* Access control */
537     if (strcmp(dbus_message_get_sender(m), i->client->name)) 
538         return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
539     
540     if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) {
541
542         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
543             avahi_log_warn("Error parsing EntryGroup::Free message");
544             goto fail;
545         }
546
547         entry_group_free(i);
548         return respond_ok(c, m);
549         
550     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit")) {
551
552         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
553             avahi_log_warn("Error parsing EntryGroup::Commit message");
554             goto fail;
555         }
556
557         avahi_entry_group_commit(i->entry_group);
558         return respond_ok(c, m);
559         
560         
561     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset")) {
562         
563         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
564             avahi_log_warn("Error parsing EntryGroup::Reset message");
565             goto fail;
566         }
567
568         avahi_entry_group_reset(i->entry_group);
569         return respond_ok(c, m);
570         
571     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) {
572         DBusMessage *reply;
573         gboolean b;
574         
575         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
576             avahi_log_warn("Error parsing EntryGroup::IsEmpty message");
577             goto fail;
578         }
579
580         b = !!avahi_entry_group_is_empty(i->entry_group);
581
582         reply = dbus_message_new_method_return(m);
583         dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &b, DBUS_TYPE_INVALID);
584         dbus_connection_send(c, reply, NULL);
585         dbus_message_unref(reply);
586         
587         return DBUS_HANDLER_RESULT_HANDLED;
588         
589     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) {
590         AvahiEntryGroupState state;
591         
592         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
593             avahi_log_warn("Error parsing EntryGroup::GetState message");
594             goto fail;
595         }
596
597         state = avahi_entry_group_get_state(i->entry_group);
598         return respond_int32(c, m, (gint32) state);
599         
600     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
601         gint32 interface, protocol;
602         gchar *type, *name, *domain, *host;
603         guint16 port;
604         AvahiStringList *strlst;
605         DBusMessageIter iter, sub;
606         int j;
607         
608         if (!dbus_message_get_args(
609                 m, &error,
610                 DBUS_TYPE_INT32, &interface,
611                 DBUS_TYPE_INT32, &protocol,
612                 DBUS_TYPE_STRING, &name,
613                 DBUS_TYPE_STRING, &type,
614                 DBUS_TYPE_STRING, &domain,
615                 DBUS_TYPE_STRING, &host,
616                 DBUS_TYPE_UINT16, &port, 
617                 DBUS_TYPE_INVALID) || !type || !name) {
618             avahi_log_warn("Error parsing EntryGroup::AddService message");
619             goto fail;
620         }
621
622         dbus_message_iter_init(m, &iter);
623
624         for (j = 0; j < 7; j++)
625             dbus_message_iter_next(&iter);
626         
627         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
628             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) {
629             avahi_log_warn("Error parsing EntryGroup::AddService message 2");
630             goto fail;
631         }
632
633         strlst = NULL;
634         dbus_message_iter_recurse(&iter, &sub);
635         
636         for (;;) {
637             DBusMessageIter sub2;
638             int at, n;
639             guint8 *k;
640
641             if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
642                 break;
643
644             g_assert(at == DBUS_TYPE_ARRAY);
645             
646             if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
647                 avahi_log_warn("Error parsing EntryGroup::AddService message");
648                 goto fail;
649             }
650
651             dbus_message_iter_recurse(&sub, &sub2);
652             dbus_message_iter_get_fixed_array(&sub2, &k, &n);
653             strlst = avahi_string_list_add_arbitrary(strlst, k, n);
654             
655             dbus_message_iter_next(&sub);
656         }
657
658         if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
659             avahi_string_list_free(strlst);
660             avahi_log_warn("Too many entries per entry group, client request failed.");
661             return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
662         }
663
664         if (domain && !*domain)
665             domain = NULL;
666
667         if (host && !*host)
668             host = NULL;
669
670         if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, host, port, strlst) < 0) {
671             avahi_string_list_free(strlst);
672             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
673         } else
674             i->n_entries ++;
675         
676         avahi_string_list_free(strlst);
677         
678         return respond_ok(c, m);
679         
680     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
681         gint32 interface, protocol;
682         gchar *name, *address;
683         AvahiAddress a;
684         
685         if (!dbus_message_get_args(
686                 m, &error,
687                 DBUS_TYPE_INT32, &interface,
688                 DBUS_TYPE_INT32, &protocol,
689                 DBUS_TYPE_STRING, &name,
690                 DBUS_TYPE_STRING, &address,
691                 DBUS_TYPE_INVALID) || !name || !address) {
692             avahi_log_warn("Error parsing EntryGroup::AddAddress message");
693             goto fail;
694         }
695
696         if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
697             avahi_log_warn("Too many entries per entry group, client request failed.");
698             return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
699         }
700         
701         if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))) {
702             return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
703         }
704
705         if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, 0, name, &a) < 0)
706             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
707         else
708             i->n_entries ++;
709         
710         return respond_ok(c, m);
711     }
712     
713     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
714
715 fail:
716     if (dbus_error_is_set(&error))
717         dbus_error_free(&error);
718     
719     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
720 }
721
722 static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *host_name, const AvahiAddress *a, gpointer userdata) {
723     HostNameResolverInfo *i = userdata;
724     
725     g_assert(r);
726     g_assert(host_name);
727     g_assert(i);
728
729     if (event == AVAHI_RESOLVER_FOUND) {
730         char t[256], *pt = t;
731         gint32 i_interface, i_protocol, i_aprotocol;
732         DBusMessage *reply;
733
734         g_assert(a);
735         avahi_address_snprint(t, sizeof(t), a);
736
737         i_interface = (gint32) interface;
738         i_protocol = (gint32) protocol;
739         i_aprotocol = (gint32) a->family;
740         
741         reply = dbus_message_new_method_return(i->message);
742         dbus_message_append_args(
743             reply,
744             DBUS_TYPE_INT32, &i_interface,
745             DBUS_TYPE_INT32, &i_protocol,
746             DBUS_TYPE_STRING, &host_name,
747             DBUS_TYPE_INT32, &i_aprotocol,
748             DBUS_TYPE_STRING, &pt,
749             DBUS_TYPE_INVALID);
750
751         dbus_connection_send(server->bus, reply, NULL);
752         dbus_message_unref(reply);
753     } else {
754         g_assert(event == AVAHI_RESOLVER_TIMEOUT);
755
756         respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
757     }
758
759     host_name_resolver_free(i);
760 }
761
762 static void address_resolver_callback(AvahiAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const gchar *host_name, gpointer userdata) {
763     AddressResolverInfo *i = userdata;
764     
765     g_assert(r);
766     g_assert(address);
767     g_assert(i);
768
769     if (event == AVAHI_RESOLVER_FOUND) {
770         char t[256], *pt = t;
771         gint32 i_interface, i_protocol, i_aprotocol;
772         DBusMessage *reply;
773
774         g_assert(host_name);
775         avahi_address_snprint(t, sizeof(t), address);
776
777         i_interface = (gint32) interface;
778         i_protocol = (gint32) protocol;
779         i_aprotocol = (gint32) address->family;
780         
781         reply = dbus_message_new_method_return(i->message);
782         dbus_message_append_args(
783             reply,
784             DBUS_TYPE_INT32, &i_interface,
785             DBUS_TYPE_INT32, &i_protocol,
786             DBUS_TYPE_INT32, &i_aprotocol,
787             DBUS_TYPE_STRING, &pt,
788             DBUS_TYPE_STRING, &host_name,
789             DBUS_TYPE_INVALID);
790
791         dbus_connection_send(server->bus, reply, NULL);
792         dbus_message_unref(reply);
793     } else {
794         g_assert(event == AVAHI_RESOLVER_TIMEOUT);
795         respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
796     }
797
798     address_resolver_free(i);
799 }
800
801 static DBusHandlerResult msg_domain_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
802     DBusError error;
803     DomainBrowserInfo *i = userdata;
804
805     g_assert(c);
806     g_assert(m);
807     g_assert(i);
808     
809     dbus_error_init(&error);
810
811     avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
812                     dbus_message_get_interface(m),
813                     dbus_message_get_path(m),
814                     dbus_message_get_member(m));
815
816     /* Introspection */
817     if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
818         return handle_introspect(c, m, "DomainBrowser.introspect");
819     
820     /* Access control */
821     if (strcmp(dbus_message_get_sender(m), i->client->name)) 
822         return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
823     
824     if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free")) {
825
826         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
827             avahi_log_warn("Error parsing DomainBrowser::Free message");
828             goto fail;
829         }
830
831         domain_browser_free(i);
832         return respond_ok(c, m);
833         
834     }
835     
836     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
837
838 fail:
839     if (dbus_error_is_set(&error))
840         dbus_error_free(&error);
841     
842     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
843 }
844
845 static void domain_browser_callback(AvahiDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *domain, gpointer userdata) {
846     DomainBrowserInfo *i = userdata;
847     DBusMessage *m;
848     gint32 i_interface, i_protocol;
849     
850     g_assert(b);
851     g_assert(domain);
852     g_assert(i);
853
854     i_interface = (gint32) interface;
855     i_protocol = (gint32) protocol;
856
857     m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
858     dbus_message_append_args(
859         m,
860         DBUS_TYPE_INT32, &i_interface,
861         DBUS_TYPE_INT32, &i_protocol,
862         DBUS_TYPE_STRING, &domain,
863         DBUS_TYPE_INVALID);
864     dbus_message_set_destination(m, i->client->name);   
865     dbus_connection_send(server->bus, m, NULL);
866     dbus_message_unref(m);
867 }
868
869 static DBusHandlerResult msg_service_type_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
870     DBusError error;
871     ServiceTypeBrowserInfo *i = userdata;
872
873     g_assert(c);
874     g_assert(m);
875     g_assert(i);
876     
877     dbus_error_init(&error);
878
879     avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
880                     dbus_message_get_interface(m),
881                     dbus_message_get_path(m),
882                     dbus_message_get_member(m));
883
884     /* Introspection */
885     if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
886         return handle_introspect(c, m, "ServiceTypeBrowser.introspect");
887     
888     /* Access control */
889     if (strcmp(dbus_message_get_sender(m), i->client->name)) 
890         return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
891     
892     if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free")) {
893
894         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
895             avahi_log_warn("Error parsing ServiceTypeBrowser::Free message");
896             goto fail;
897         }
898
899         service_type_browser_free(i);
900         return respond_ok(c, m);
901         
902     }
903     
904     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
905
906 fail:
907     if (dbus_error_is_set(&error))
908         dbus_error_free(&error);
909     
910     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
911 }
912
913 static void service_type_browser_callback(AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *type, const gchar *domain, gpointer userdata) {
914     ServiceTypeBrowserInfo *i = userdata;
915     DBusMessage *m;
916     gint32 i_interface, i_protocol;
917     
918     g_assert(b);
919     g_assert(type);
920     g_assert(domain);
921     g_assert(i);
922
923     i_interface = (gint32) interface;
924     i_protocol = (gint32) protocol;
925
926     m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
927     dbus_message_append_args(
928         m,
929         DBUS_TYPE_INT32, &i_interface,
930         DBUS_TYPE_INT32, &i_protocol,
931         DBUS_TYPE_STRING, &type,
932         DBUS_TYPE_STRING, &domain,
933         DBUS_TYPE_INVALID);
934     dbus_message_set_destination(m, i->client->name);   
935     dbus_connection_send(server->bus, m, NULL);
936     dbus_message_unref(m);
937 }
938
939 static DBusHandlerResult msg_service_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
940     DBusError error;
941     ServiceBrowserInfo *i = userdata;
942
943     g_assert(c);
944     g_assert(m);
945     g_assert(i);
946     
947     dbus_error_init(&error);
948
949     avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
950                     dbus_message_get_interface(m),
951                     dbus_message_get_path(m),
952                     dbus_message_get_member(m));
953
954     /* Introspection */
955     if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
956         return handle_introspect(c, m, "ServiceBrowser.Introspect");
957     
958     /* Access control */
959     if (strcmp(dbus_message_get_sender(m), i->client->name)) 
960         return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
961     
962     if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free")) {
963
964         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
965             avahi_log_warn("Error parsing ServiceBrowser::Free message");
966             goto fail;
967         }
968
969         service_browser_free(i);
970         return respond_ok(c, m);
971         
972     }
973     
974     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
975
976 fail:
977     if (dbus_error_is_set(&error))
978         dbus_error_free(&error);
979     
980     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
981 }
982
983 static void service_browser_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *name, const gchar *type, const gchar *domain, gpointer userdata) {
984     ServiceBrowserInfo *i = userdata;
985     DBusMessage *m;
986     gint32 i_interface, i_protocol;
987     
988     g_assert(b);
989     g_assert(name);
990     g_assert(type);
991     g_assert(domain);
992     g_assert(i);
993
994     i_interface = (gint32) interface;
995     i_protocol = (gint32) protocol;
996
997     m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
998     dbus_message_append_args(
999         m,
1000         DBUS_TYPE_INT32, &i_interface,
1001         DBUS_TYPE_INT32, &i_protocol,
1002         DBUS_TYPE_STRING, &name,
1003         DBUS_TYPE_STRING, &type,
1004         DBUS_TYPE_STRING, &domain,
1005         DBUS_TYPE_INVALID);
1006     dbus_message_set_destination(m, i->client->name);   
1007     dbus_connection_send(server->bus, m, NULL);
1008     dbus_message_unref(m);
1009 }
1010
1011 static void service_resolver_callback(
1012     AvahiServiceResolver *r,
1013     AvahiIfIndex interface,
1014     AvahiProtocol protocol,
1015     AvahiResolverEvent event,
1016     const gchar *name,
1017     const gchar *type,
1018     const gchar *domain,
1019     const gchar *host_name,
1020     const AvahiAddress *a,
1021     guint16 port,
1022     AvahiStringList *txt,
1023     gpointer userdata) {
1024     
1025     ServiceResolverInfo *i = userdata;
1026     
1027     g_assert(r);
1028     g_assert(i);
1029
1030     if (event == AVAHI_RESOLVER_FOUND) {
1031         char t[256], *pt = t;
1032         gint32 i_interface, i_protocol, i_aprotocol;
1033         guint n, j;
1034         AvahiStringList *p;
1035         DBusMessage *reply;
1036         DBusMessageIter iter, sub;
1037
1038         g_assert(host_name);
1039         
1040         g_assert(a);
1041         avahi_address_snprint(t, sizeof(t), a);
1042
1043         i_interface = (gint32) interface;
1044         i_protocol = (gint32) protocol;
1045         i_aprotocol = (gint32) a->family;
1046
1047         reply = dbus_message_new_method_return(i->message);
1048         dbus_message_append_args(
1049             reply,
1050             DBUS_TYPE_INT32, &i_interface,
1051             DBUS_TYPE_INT32, &i_protocol,
1052             DBUS_TYPE_STRING, &name,
1053             DBUS_TYPE_STRING, &type,
1054             DBUS_TYPE_STRING, &domain,
1055             DBUS_TYPE_STRING, &host_name,
1056             DBUS_TYPE_INT32, &i_aprotocol,
1057             DBUS_TYPE_STRING, &pt,
1058             DBUS_TYPE_UINT16, &port,
1059             DBUS_TYPE_INVALID);
1060
1061         dbus_message_iter_init_append(reply, &iter);
1062         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub);
1063
1064         for (p = txt, j = n-1; p; p = p->next, j--) {
1065             DBusMessageIter sub2;
1066             const guint8 *data = p->text;
1067             
1068             dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
1069             dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size); 
1070             dbus_message_iter_close_container(&sub, &sub2);
1071
1072         }
1073         dbus_message_iter_close_container(&iter, &sub);
1074                 
1075         dbus_connection_send(server->bus, reply, NULL);
1076         dbus_message_unref(reply);
1077     } else {
1078         g_assert(event == AVAHI_RESOLVER_TIMEOUT);
1079
1080         respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
1081     }
1082
1083     service_resolver_free(i);
1084 }
1085
1086 static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
1087     DBusError error;
1088
1089     dbus_error_init(&error);
1090
1091     avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
1092                     dbus_message_get_interface(m),
1093                     dbus_message_get_path(m),
1094                     dbus_message_get_member(m));
1095
1096     if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
1097         return handle_introspect(c, m, "Server.introspect");
1098         
1099     else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostName")) {
1100
1101         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1102             avahi_log_warn("Error parsing Server::GetHostName message");
1103             goto fail;
1104         }
1105
1106         return respond_string(c, m, avahi_server_get_host_name(avahi_server));
1107         
1108     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) {
1109
1110         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1111             avahi_log_warn("Error parsing Server::GetDomainName message");
1112             goto fail;
1113         }
1114
1115         return respond_string(c, m, avahi_server_get_domain_name(avahi_server));
1116
1117     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) {
1118
1119         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1120             avahi_log_warn("Error parsing Server::GetHostNameFqdn message");
1121             goto fail;
1122         }
1123     
1124         return respond_string(c, m, avahi_server_get_host_name_fqdn(avahi_server));
1125         
1126     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")) {
1127
1128         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1129             avahi_log_warn("Error parsing Server::GetVersionString message");
1130             goto fail;
1131         }
1132     
1133         return respond_string(c, m, PACKAGE_STRING);
1134
1135     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetState")) {
1136         AvahiServerState state;
1137         
1138         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1139             avahi_log_warn("Error parsing Server::GetState message");
1140             goto fail;
1141         }
1142         
1143         state = avahi_server_get_state(avahi_server);
1144         return respond_int32(c, m, (gint32) state);
1145
1146     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceNameByIndex")) {
1147         gint32 idx;
1148         int fd;
1149         struct ifreq ifr;
1150         
1151         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INT32, &idx, DBUS_TYPE_INVALID))) {
1152             avahi_log_warn("Error parsing Server::GetNetworkInterfaceNameByIndex message");
1153             goto fail;
1154         }
1155
1156 #ifdef VALGRIND_WORKAROUND
1157         return respond_string(c, m, "blah");
1158 #else
1159         
1160         if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1161             gchar txt[256];
1162             g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1163             return respond_error(c, m, AVAHI_ERR_OS, txt);
1164         }
1165
1166         memset(&ifr, 0, sizeof(ifr));
1167         ifr.ifr_ifindex = idx;
1168
1169         if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
1170             gchar txt[256];
1171             g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1172             close(fd);
1173             return respond_error(c, m, AVAHI_ERR_OS, txt);
1174         }
1175
1176         close(fd);
1177         
1178         return respond_string(c, m, ifr.ifr_name);
1179 #endif
1180         
1181     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceIndexByName")) {
1182         gchar *n;
1183         int fd;
1184         struct ifreq ifr;
1185         
1186         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1187             avahi_log_warn("Error parsing Server::GetNetworkInterfaceIndexByName message");
1188             goto fail;
1189         }
1190
1191 #ifdef VALGRIND_WORKAROUND
1192         return respond_int32(c, m, 1);
1193 #else
1194         if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1195             gchar txt[256];
1196             g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1197             return respond_error(c, m, AVAHI_ERR_OS, txt);
1198         }
1199
1200         memset(&ifr, 0, sizeof(ifr));
1201         g_snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", n);
1202
1203         if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
1204             gchar txt[256];
1205             g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1206             close(fd);
1207             return respond_error(c, m, AVAHI_ERR_OS, txt);
1208         }
1209
1210         close(fd);
1211         
1212         return respond_int32(c, m, ifr.ifr_ifindex);
1213 #endif
1214
1215     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) {
1216         gchar *n, * t;
1217         
1218         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1219             avahi_log_warn("Error parsing Server::GetAlternativeHostName message");
1220             goto fail;
1221         }
1222
1223         t = avahi_alternative_host_name(n);
1224         respond_string(c, m, t);
1225         g_free(t);
1226
1227         return DBUS_HANDLER_RESULT_HANDLED;
1228
1229     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeServiceName")) {
1230         gchar *n, *t;
1231         
1232         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1233             avahi_log_warn("Error parsing Server::GetAlternativeServiceName message");
1234             goto fail;
1235         }
1236
1237         t = avahi_alternative_service_name(n);
1238         respond_string(c, m, t);
1239         g_free(t);
1240
1241         return DBUS_HANDLER_RESULT_HANDLED;
1242         
1243     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew")) {
1244         Client *client;
1245         EntryGroupInfo *i;
1246         static const DBusObjectPathVTable vtable = {
1247             NULL,
1248             msg_entry_group_impl,
1249             NULL,
1250             NULL,
1251             NULL,
1252             NULL
1253         };
1254
1255         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1256             avahi_log_warn("Error parsing Server::EntryGroupNew message");
1257             goto fail;
1258         }
1259
1260         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1261             avahi_log_warn("Too many clients, client request failed.");
1262             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1263         }
1264
1265         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1266             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1267             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1268         }
1269
1270         i = g_new(EntryGroupInfo, 1);
1271         i->id = ++client->current_id;
1272         i->client = client;
1273         i->path = g_strdup_printf("/Client%u/EntryGroup%u", client->id, i->id);
1274         i->n_entries = 0;
1275         AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i);
1276         client->n_objects++;
1277         
1278         if (!(i->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, i))) {
1279             entry_group_free(i);
1280             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1281         }
1282
1283         dbus_connection_register_object_path(c, i->path, &vtable, i);
1284         return respond_path(c, m, i->path);
1285         
1286     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName")) {
1287         Client *client;
1288         gint32 interface, protocol, aprotocol;
1289         gchar *name;
1290         HostNameResolverInfo *i;
1291             
1292         if (!dbus_message_get_args(
1293                 m, &error,
1294                 DBUS_TYPE_INT32, &interface,
1295                 DBUS_TYPE_INT32, &protocol,
1296                 DBUS_TYPE_STRING, &name,
1297                 DBUS_TYPE_INT32, &aprotocol,
1298                 DBUS_TYPE_INVALID) || !name) {
1299             avahi_log_warn("Error parsing Server::ResolveHostName message");
1300             goto fail;
1301         }
1302
1303         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1304             avahi_log_warn("Too many clients, client request failed.");
1305             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1306         }
1307
1308         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1309             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1310             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1311         }
1312
1313         i = g_new(HostNameResolverInfo, 1);
1314         i->client = client;
1315         i->message = dbus_message_ref(m);
1316         AVAHI_LLIST_PREPEND(HostNameResolverInfo, host_name_resolvers, client->host_name_resolvers, i);
1317         client->n_objects++;
1318
1319         if (!(i->host_name_resolver = avahi_host_name_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, (AvahiProtocol) aprotocol, host_name_resolver_callback, i))) {
1320             host_name_resolver_free(i);
1321             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1322         }
1323         
1324         return DBUS_HANDLER_RESULT_HANDLED;
1325         
1326     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress")) {
1327         Client *client;
1328         gint32 interface, protocol;
1329         gchar *address;
1330         AddressResolverInfo *i;
1331         AvahiAddress a;
1332             
1333         if (!dbus_message_get_args(
1334                 m, &error,
1335                 DBUS_TYPE_INT32, &interface,
1336                 DBUS_TYPE_INT32, &protocol,
1337                 DBUS_TYPE_STRING, &address,
1338                 DBUS_TYPE_INVALID) || !address) {
1339             avahi_log_warn("Error parsing Server::ResolveAddress message");
1340             goto fail;
1341         }
1342
1343         if (!avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))
1344             return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
1345
1346         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1347             avahi_log_warn("Too many clients, client request failed.");
1348             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1349         }
1350
1351         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1352             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1353             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1354         }
1355
1356         i = g_new(AddressResolverInfo, 1);
1357         i->client = client;
1358         i->message = dbus_message_ref(m);
1359         AVAHI_LLIST_PREPEND(AddressResolverInfo, address_resolvers, client->address_resolvers, i);
1360         client->n_objects++;
1361
1362         if (!(i->address_resolver = avahi_address_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, &a, address_resolver_callback, i))) {
1363             address_resolver_free(i);
1364             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1365         }
1366         
1367         return DBUS_HANDLER_RESULT_HANDLED;
1368         
1369     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew")) {
1370         Client *client;
1371         DomainBrowserInfo *i;
1372         static const DBusObjectPathVTable vtable = {
1373             NULL,
1374             msg_domain_browser_impl,
1375             NULL,
1376             NULL,
1377             NULL,
1378             NULL
1379         };
1380         gint32 interface, protocol, type;
1381         gchar *domain;
1382         
1383
1384         if (!dbus_message_get_args(
1385                 m, &error,
1386                 DBUS_TYPE_INT32, &interface,
1387                 DBUS_TYPE_INT32, &protocol,
1388                 DBUS_TYPE_STRING, &domain,
1389                 DBUS_TYPE_INT32, &type,
1390                 DBUS_TYPE_INVALID) || type < 0 || type >= AVAHI_DOMAIN_BROWSER_MAX) {
1391             avahi_log_warn("Error parsing Server::DomainBrowserNew message");
1392             goto fail;
1393         }
1394
1395         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1396             avahi_log_warn("Too many clients, client request failed.");
1397             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1398         }
1399
1400         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1401             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1402             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1403         }
1404
1405         if (!*domain)
1406             domain = NULL;
1407
1408         i = g_new(DomainBrowserInfo, 1);
1409         i->id = ++client->current_id;
1410         i->client = client;
1411         i->path = g_strdup_printf("/Client%u/DomainBrowser%u", client->id, i->id);
1412         AVAHI_LLIST_PREPEND(DomainBrowserInfo, domain_browsers, client->domain_browsers, i);
1413         client->n_objects++;
1414
1415         if (!(i->domain_browser = avahi_domain_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, (AvahiDomainBrowserType) type, domain_browser_callback, i))) {
1416             domain_browser_free(i);
1417             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1418         }
1419         
1420         dbus_connection_register_object_path(c, i->path, &vtable, i);
1421         return respond_path(c, m, i->path);
1422
1423     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceTypeBrowserNew")) {
1424         Client *client;
1425         ServiceTypeBrowserInfo *i;
1426         static const DBusObjectPathVTable vtable = {
1427             NULL,
1428             msg_service_type_browser_impl,
1429             NULL,
1430             NULL,
1431             NULL,
1432             NULL
1433         };
1434         gint32 interface, protocol;
1435         gchar *domain;
1436         
1437         if (!dbus_message_get_args(
1438                 m, &error,
1439                 DBUS_TYPE_INT32, &interface,
1440                 DBUS_TYPE_INT32, &protocol,
1441                 DBUS_TYPE_STRING, &domain,
1442                 DBUS_TYPE_INVALID)) {
1443             avahi_log_warn("Error parsing Server::ServiceTypeBrowserNew message");
1444             goto fail;
1445         }
1446
1447         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1448             avahi_log_warn("Too many clients, client request failed.");
1449             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1450         }
1451
1452
1453         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1454             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1455             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1456         }
1457
1458         if (!*domain)
1459             domain = NULL;
1460
1461         i = g_new(ServiceTypeBrowserInfo, 1);
1462         i->id = ++client->current_id;
1463         i->client = client;
1464         i->path = g_strdup_printf("/Client%u/ServiceTypeBrowser%u", client->id, i->id);
1465         AVAHI_LLIST_PREPEND(ServiceTypeBrowserInfo, service_type_browsers, client->service_type_browsers, i);
1466         client->n_objects++;
1467
1468         if (!(i->service_type_browser = avahi_service_type_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, service_type_browser_callback, i))) {
1469             service_type_browser_free(i);
1470             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1471         }
1472         
1473         dbus_connection_register_object_path(c, i->path, &vtable, i);
1474         return respond_path(c, m, i->path);
1475         
1476      } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew")) {
1477         Client *client;
1478         ServiceBrowserInfo *i;
1479         static const DBusObjectPathVTable vtable = {
1480             NULL,
1481             msg_service_browser_impl,
1482             NULL,
1483             NULL,
1484             NULL,
1485             NULL
1486         };
1487         gint32 interface, protocol;
1488         gchar *domain, *type;
1489         
1490         if (!dbus_message_get_args(
1491                 m, &error,
1492                 DBUS_TYPE_INT32, &interface,
1493                 DBUS_TYPE_INT32, &protocol,
1494                 DBUS_TYPE_STRING, &type,
1495                 DBUS_TYPE_STRING, &domain,
1496                 DBUS_TYPE_INVALID) || !type) {
1497             avahi_log_warn("Error parsing Server::ServiceBrowserNew message");
1498             goto fail;
1499         }
1500
1501         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1502             avahi_log_warn("Too many clients, client request failed.");
1503             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1504         }
1505
1506
1507         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1508             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1509             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1510         }
1511
1512         if (!*domain)
1513             domain = NULL;
1514
1515         i = g_new(ServiceBrowserInfo, 1);
1516         i->id = ++client->current_id;
1517         i->client = client;
1518         i->path = g_strdup_printf("/Client%u/ServiceBrowser%u", client->id, i->id);
1519         AVAHI_LLIST_PREPEND(ServiceBrowserInfo, service_browsers, client->service_browsers, i);
1520         client->n_objects++;
1521
1522         if (!(i->service_browser = avahi_service_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, type, domain, service_browser_callback, i))) {
1523             service_browser_free(i);
1524             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1525         }
1526         
1527         dbus_connection_register_object_path(c, i->path, &vtable, i);
1528         return respond_path(c, m, i->path);
1529         
1530     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService")) {
1531         Client *client;
1532         gint32 interface, protocol, aprotocol;
1533         gchar *name, *type, *domain;
1534         ServiceResolverInfo *i;
1535             
1536         if (!dbus_message_get_args(
1537                 m, &error,
1538                 DBUS_TYPE_INT32, &interface,
1539                 DBUS_TYPE_INT32, &protocol,
1540                 DBUS_TYPE_STRING, &name,
1541                 DBUS_TYPE_STRING, &type,
1542                 DBUS_TYPE_STRING, &domain,
1543                 DBUS_TYPE_INT32, &aprotocol,
1544                 DBUS_TYPE_INVALID) || !name || !type) {
1545             avahi_log_warn("Error parsing Server::ResolveService message");
1546             goto fail;
1547         }
1548
1549         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1550             avahi_log_warn("Too many clients, client request failed.");
1551             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1552         }
1553         
1554         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1555             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1556             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1557         }
1558
1559         if (!*domain)
1560             domain = NULL;
1561         
1562         i = g_new(ServiceResolverInfo, 1);
1563         i->client = client;
1564         i->message = dbus_message_ref(m);
1565         AVAHI_LLIST_PREPEND(ServiceResolverInfo, service_resolvers, client->service_resolvers, i);
1566         client->n_objects++;
1567
1568         if (!(i->service_resolver = avahi_service_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, (AvahiProtocol) aprotocol, service_resolver_callback, i))) {
1569             service_resolver_free(i);
1570             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1571         }
1572         
1573         return DBUS_HANDLER_RESULT_HANDLED;
1574      }
1575
1576     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
1577
1578 fail:
1579     if (dbus_error_is_set(&error))
1580         dbus_error_free(&error);
1581     
1582     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1583 }
1584
1585 void dbus_protocol_server_state_changed(AvahiServerState state) {
1586     DBusMessage *m;
1587     gint32 t;
1588     
1589     if (!server)
1590         return;
1591
1592     m = dbus_message_new_signal(AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged");
1593     t = (gint32) state;
1594     dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
1595     dbus_connection_send(server->bus, m, NULL);
1596     dbus_message_unref(m);
1597 }
1598
1599 int dbus_protocol_setup(GMainLoop *loop) {
1600     DBusError error;
1601
1602     static const DBusObjectPathVTable server_vtable = {
1603         NULL,
1604         msg_server_impl,
1605         NULL,
1606         NULL,
1607         NULL,
1608         NULL
1609     };
1610
1611     dbus_error_init(&error);
1612
1613     server = g_malloc(sizeof(Server));
1614     server->clients = NULL;
1615     server->current_id = 0;
1616     server->n_clients = 0;
1617
1618     server->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
1619     if (dbus_error_is_set(&error)) {
1620         avahi_log_warn("dbus_bus_get(): %s", error.message);
1621         goto fail;
1622     }
1623
1624     dbus_connection_setup_with_g_main(server->bus, NULL);
1625     dbus_connection_set_exit_on_disconnect(server->bus, TRUE);
1626
1627     dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, 0, &error);
1628     if (dbus_error_is_set(&error)) {
1629         avahi_log_warn("dbus_bus_request_name(): %s", error.message);
1630         goto fail;
1631     }
1632
1633     dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS  "'", &error);
1634
1635     dbus_connection_add_filter(server->bus, msg_signal_filter_impl, loop, NULL);
1636     dbus_connection_register_object_path(server->bus, AVAHI_DBUS_PATH_SERVER, &server_vtable, NULL);
1637
1638     return 0;
1639
1640 fail:
1641     if (server->bus) {
1642         dbus_connection_disconnect(server->bus);
1643         dbus_connection_unref(server->bus);
1644     }
1645     
1646     dbus_error_free (&error);
1647     g_free(server);
1648     server = NULL;
1649     return -1;
1650 }
1651
1652 void dbus_protocol_shutdown(void) {
1653
1654     if (server) {
1655     
1656         while (server->clients)
1657             client_free(server->clients);
1658
1659         g_assert(server->n_clients == 0);
1660
1661         if (server->bus) {
1662             dbus_connection_disconnect(server->bus);
1663             dbus_connection_unref(server->bus);
1664         }
1665
1666         g_free(server);
1667         server = NULL;
1668     }
1669 }