]> git.meshlink.io Git - catta/blob - avahi-daemon/dbus-protocol.c
41cef18f2b3f680d2e4da55c71e724d076332cec
[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
591         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
592             avahi_log_warn("Error parsing EntryGroup::GetState message");
593             goto fail;
594         }
595
596         return respond_int32(c, m, (gint32) avahi_entry_group_get_state(i->entry_group));
597         
598     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
599         gint32 interface, protocol;
600         gchar *type, *name, *domain, *host;
601         guint16 port;
602         AvahiStringList *strlst;
603         DBusMessageIter iter, sub;
604         int j;
605         
606         if (!dbus_message_get_args(
607                 m, &error,
608                 DBUS_TYPE_INT32, &interface,
609                 DBUS_TYPE_INT32, &protocol,
610                 DBUS_TYPE_STRING, &name,
611                 DBUS_TYPE_STRING, &type,
612                 DBUS_TYPE_STRING, &domain,
613                 DBUS_TYPE_STRING, &host,
614                 DBUS_TYPE_UINT16, &port, 
615                 DBUS_TYPE_INVALID) || !type || !name) {
616             avahi_log_warn("Error parsing EntryGroup::AddService message");
617             goto fail;
618         }
619
620         dbus_message_iter_init(m, &iter);
621
622         for (j = 0; j < 7; j++)
623             dbus_message_iter_next(&iter);
624         
625         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
626             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) {
627             avahi_log_warn("Error parsing EntryGroup::AddService message 2");
628             goto fail;
629         }
630
631         strlst = NULL;
632         dbus_message_iter_recurse(&iter, &sub);
633         
634         for (;;) {
635             DBusMessageIter sub2;
636             int at, n;
637             guint8 *k;
638
639             if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
640                 break;
641
642             g_assert(at == DBUS_TYPE_ARRAY);
643             
644             if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
645                 avahi_log_warn("Error parsing EntryGroup::AddService message");
646                 goto fail;
647             }
648
649             dbus_message_iter_recurse(&sub, &sub2);
650             dbus_message_iter_get_fixed_array(&sub2, &k, &n);
651             strlst = avahi_string_list_add_arbitrary(strlst, k, n);
652             
653             dbus_message_iter_next(&sub);
654         }
655
656         if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
657             avahi_string_list_free(strlst);
658             avahi_log_warn("Too many entries per entry group, client request failed.");
659             return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
660         }
661
662         if (domain && !*domain)
663             domain = NULL;
664
665         if (host && !*host)
666             host = NULL;
667
668         if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, host, port, strlst) < 0) {
669             avahi_string_list_free(strlst);
670             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
671         } else
672             i->n_entries ++;
673         
674         avahi_string_list_free(strlst);
675         
676         return respond_ok(c, m);
677         
678     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
679         gint32 interface, protocol;
680         gchar *name, *address;
681         AvahiAddress a;
682         
683         if (!dbus_message_get_args(
684                 m, &error,
685                 DBUS_TYPE_INT32, &interface,
686                 DBUS_TYPE_INT32, &protocol,
687                 DBUS_TYPE_STRING, &name,
688                 DBUS_TYPE_STRING, &address,
689                 DBUS_TYPE_INVALID) || !name || !address) {
690             avahi_log_warn("Error parsing EntryGroup::AddAddress message");
691             goto fail;
692         }
693
694         if (i->n_entries >= MAX_ENTRIES_PER_ENTRY_GROUP) {
695             avahi_log_warn("Too many entries per entry group, client request failed.");
696             return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
697         }
698         
699         if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))) {
700             return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
701         }
702
703         if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, 0, name, &a) < 0)
704             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
705         else
706             i->n_entries ++;
707         
708         return respond_ok(c, m);
709     }
710     
711     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
712
713 fail:
714     if (dbus_error_is_set(&error))
715         dbus_error_free(&error);
716     
717     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
718 }
719
720 static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *host_name, const AvahiAddress *a, gpointer userdata) {
721     HostNameResolverInfo *i = userdata;
722     
723     g_assert(r);
724     g_assert(host_name);
725     g_assert(i);
726
727     if (event == AVAHI_RESOLVER_FOUND) {
728         char t[256], *pt = t;
729         gint32 i_interface, i_protocol, i_aprotocol;
730         DBusMessage *reply;
731
732         g_assert(a);
733         avahi_address_snprint(t, sizeof(t), a);
734
735         i_interface = (gint32) interface;
736         i_protocol = (gint32) protocol;
737         i_aprotocol = (gint32) a->family;
738         
739         reply = dbus_message_new_method_return(i->message);
740         dbus_message_append_args(
741             reply,
742             DBUS_TYPE_INT32, &i_interface,
743             DBUS_TYPE_INT32, &i_protocol,
744             DBUS_TYPE_STRING, &host_name,
745             DBUS_TYPE_INT32, &i_aprotocol,
746             DBUS_TYPE_STRING, &pt,
747             DBUS_TYPE_INVALID);
748
749         dbus_connection_send(server->bus, reply, NULL);
750         dbus_message_unref(reply);
751     } else {
752         g_assert(event == AVAHI_RESOLVER_TIMEOUT);
753
754         respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
755     }
756
757     host_name_resolver_free(i);
758 }
759
760 static void address_resolver_callback(AvahiAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const gchar *host_name, gpointer userdata) {
761     AddressResolverInfo *i = userdata;
762     
763     g_assert(r);
764     g_assert(address);
765     g_assert(i);
766
767     if (event == AVAHI_RESOLVER_FOUND) {
768         char t[256], *pt = t;
769         gint32 i_interface, i_protocol, i_aprotocol;
770         DBusMessage *reply;
771
772         g_assert(host_name);
773         avahi_address_snprint(t, sizeof(t), address);
774
775         i_interface = (gint32) interface;
776         i_protocol = (gint32) protocol;
777         i_aprotocol = (gint32) address->family;
778         
779         reply = dbus_message_new_method_return(i->message);
780         dbus_message_append_args(
781             reply,
782             DBUS_TYPE_INT32, &i_interface,
783             DBUS_TYPE_INT32, &i_protocol,
784             DBUS_TYPE_INT32, &i_aprotocol,
785             DBUS_TYPE_STRING, &pt,
786             DBUS_TYPE_STRING, &host_name,
787             DBUS_TYPE_INVALID);
788
789         dbus_connection_send(server->bus, reply, NULL);
790         dbus_message_unref(reply);
791     } else {
792         g_assert(event == AVAHI_RESOLVER_TIMEOUT);
793         respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
794     }
795
796     address_resolver_free(i);
797 }
798
799 static DBusHandlerResult msg_domain_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
800     DBusError error;
801     DomainBrowserInfo *i = userdata;
802
803     g_assert(c);
804     g_assert(m);
805     g_assert(i);
806     
807     dbus_error_init(&error);
808
809     avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
810                     dbus_message_get_interface(m),
811                     dbus_message_get_path(m),
812                     dbus_message_get_member(m));
813
814     /* Introspection */
815     if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
816         return handle_introspect(c, m, "DomainBrowser.introspect");
817     
818     /* Access control */
819     if (strcmp(dbus_message_get_sender(m), i->client->name)) 
820         return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
821     
822     if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free")) {
823
824         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
825             avahi_log_warn("Error parsing DomainBrowser::Free message");
826             goto fail;
827         }
828
829         domain_browser_free(i);
830         return respond_ok(c, m);
831         
832     }
833     
834     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
835
836 fail:
837     if (dbus_error_is_set(&error))
838         dbus_error_free(&error);
839     
840     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
841 }
842
843 static void domain_browser_callback(AvahiDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *domain, gpointer userdata) {
844     DomainBrowserInfo *i = userdata;
845     DBusMessage *m;
846     gint32 i_interface, i_protocol;
847     
848     g_assert(b);
849     g_assert(domain);
850     g_assert(i);
851
852     i_interface = (gint32) interface;
853     i_protocol = (gint32) protocol;
854
855     m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
856     dbus_message_append_args(
857         m,
858         DBUS_TYPE_INT32, &i_interface,
859         DBUS_TYPE_INT32, &i_protocol,
860         DBUS_TYPE_STRING, &domain,
861         DBUS_TYPE_INVALID);
862     dbus_message_set_destination(m, i->client->name);   
863     dbus_connection_send(server->bus, m, NULL);
864     dbus_message_unref(m);
865 }
866
867 static DBusHandlerResult msg_service_type_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
868     DBusError error;
869     ServiceTypeBrowserInfo *i = userdata;
870
871     g_assert(c);
872     g_assert(m);
873     g_assert(i);
874     
875     dbus_error_init(&error);
876
877     avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
878                     dbus_message_get_interface(m),
879                     dbus_message_get_path(m),
880                     dbus_message_get_member(m));
881
882     /* Introspection */
883     if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
884         return handle_introspect(c, m, "ServiceTypeBrowser.introspect");
885     
886     /* Access control */
887     if (strcmp(dbus_message_get_sender(m), i->client->name)) 
888         return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
889     
890     if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free")) {
891
892         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
893             avahi_log_warn("Error parsing ServiceTypeBrowser::Free message");
894             goto fail;
895         }
896
897         service_type_browser_free(i);
898         return respond_ok(c, m);
899         
900     }
901     
902     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
903
904 fail:
905     if (dbus_error_is_set(&error))
906         dbus_error_free(&error);
907     
908     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
909 }
910
911 static void service_type_browser_callback(AvahiServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *type, const gchar *domain, gpointer userdata) {
912     ServiceTypeBrowserInfo *i = userdata;
913     DBusMessage *m;
914     gint32 i_interface, i_protocol;
915     
916     g_assert(b);
917     g_assert(type);
918     g_assert(domain);
919     g_assert(i);
920
921     i_interface = (gint32) interface;
922     i_protocol = (gint32) protocol;
923
924     m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
925     dbus_message_append_args(
926         m,
927         DBUS_TYPE_INT32, &i_interface,
928         DBUS_TYPE_INT32, &i_protocol,
929         DBUS_TYPE_STRING, &type,
930         DBUS_TYPE_STRING, &domain,
931         DBUS_TYPE_INVALID);
932     dbus_message_set_destination(m, i->client->name);   
933     dbus_connection_send(server->bus, m, NULL);
934     dbus_message_unref(m);
935 }
936
937 static DBusHandlerResult msg_service_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
938     DBusError error;
939     ServiceBrowserInfo *i = userdata;
940
941     g_assert(c);
942     g_assert(m);
943     g_assert(i);
944     
945     dbus_error_init(&error);
946
947     avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
948                     dbus_message_get_interface(m),
949                     dbus_message_get_path(m),
950                     dbus_message_get_member(m));
951
952     /* Introspection */
953     if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
954         return handle_introspect(c, m, "ServiceBrowser.Introspect");
955     
956     /* Access control */
957     if (strcmp(dbus_message_get_sender(m), i->client->name)) 
958         return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
959     
960     if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free")) {
961
962         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
963             avahi_log_warn("Error parsing ServiceBrowser::Free message");
964             goto fail;
965         }
966
967         service_browser_free(i);
968         return respond_ok(c, m);
969         
970     }
971     
972     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
973
974 fail:
975     if (dbus_error_is_set(&error))
976         dbus_error_free(&error);
977     
978     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
979 }
980
981 static void service_browser_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *name, const gchar *type, const gchar *domain, gpointer userdata) {
982     ServiceBrowserInfo *i = userdata;
983     DBusMessage *m;
984     gint32 i_interface, i_protocol;
985     
986     g_assert(b);
987     g_assert(name);
988     g_assert(type);
989     g_assert(domain);
990     g_assert(i);
991
992     i_interface = (gint32) interface;
993     i_protocol = (gint32) protocol;
994
995     m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
996     dbus_message_append_args(
997         m,
998         DBUS_TYPE_INT32, &i_interface,
999         DBUS_TYPE_INT32, &i_protocol,
1000         DBUS_TYPE_STRING, &name,
1001         DBUS_TYPE_STRING, &type,
1002         DBUS_TYPE_STRING, &domain,
1003         DBUS_TYPE_INVALID);
1004     dbus_message_set_destination(m, i->client->name);   
1005     dbus_connection_send(server->bus, m, NULL);
1006     dbus_message_unref(m);
1007 }
1008
1009 static void service_resolver_callback(
1010     AvahiServiceResolver *r,
1011     AvahiIfIndex interface,
1012     AvahiProtocol protocol,
1013     AvahiResolverEvent event,
1014     const gchar *name,
1015     const gchar *type,
1016     const gchar *domain,
1017     const gchar *host_name,
1018     const AvahiAddress *a,
1019     guint16 port,
1020     AvahiStringList *txt,
1021     gpointer userdata) {
1022     
1023     ServiceResolverInfo *i = userdata;
1024     
1025     g_assert(r);
1026     g_assert(i);
1027
1028     if (event == AVAHI_RESOLVER_FOUND) {
1029         char t[256], *pt = t;
1030         gint32 i_interface, i_protocol, i_aprotocol;
1031         guint n, j;
1032         AvahiStringList *p;
1033         DBusMessage *reply;
1034         DBusMessageIter iter, sub;
1035
1036         g_assert(host_name);
1037         
1038         g_assert(a);
1039         avahi_address_snprint(t, sizeof(t), a);
1040
1041         i_interface = (gint32) interface;
1042         i_protocol = (gint32) protocol;
1043         i_aprotocol = (gint32) a->family;
1044
1045         reply = dbus_message_new_method_return(i->message);
1046         dbus_message_append_args(
1047             reply,
1048             DBUS_TYPE_INT32, &i_interface,
1049             DBUS_TYPE_INT32, &i_protocol,
1050             DBUS_TYPE_STRING, &name,
1051             DBUS_TYPE_STRING, &type,
1052             DBUS_TYPE_STRING, &domain,
1053             DBUS_TYPE_STRING, &host_name,
1054             DBUS_TYPE_INT32, &i_aprotocol,
1055             DBUS_TYPE_STRING, &pt,
1056             DBUS_TYPE_UINT16, &port,
1057             DBUS_TYPE_INVALID);
1058
1059         dbus_message_iter_init_append(reply, &iter);
1060         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "ay", &sub);
1061
1062         for (p = txt, j = n-1; p; p = p->next, j--) {
1063             DBusMessageIter sub2;
1064             const guint8 *data = p->text;
1065             
1066             dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
1067             dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size); 
1068             dbus_message_iter_close_container(&sub, &sub2);
1069
1070         }
1071         dbus_message_iter_close_container(&iter, &sub);
1072                 
1073         dbus_connection_send(server->bus, reply, NULL);
1074         dbus_message_unref(reply);
1075     } else {
1076         g_assert(event == AVAHI_RESOLVER_TIMEOUT);
1077
1078         respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
1079     }
1080
1081     service_resolver_free(i);
1082 }
1083
1084 static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
1085     DBusError error;
1086
1087     dbus_error_init(&error);
1088
1089     avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
1090                     dbus_message_get_interface(m),
1091                     dbus_message_get_path(m),
1092                     dbus_message_get_member(m));
1093
1094     if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
1095         return handle_introspect(c, m, "Server.introspect");
1096         
1097     else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostName")) {
1098
1099         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1100             avahi_log_warn("Error parsing Server::GetHostName message");
1101             goto fail;
1102         }
1103
1104         return respond_string(c, m, avahi_server_get_host_name(avahi_server));
1105         
1106     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) {
1107
1108         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1109             avahi_log_warn("Error parsing Server::GetDomainName message");
1110             goto fail;
1111         }
1112
1113         return respond_string(c, m, avahi_server_get_domain_name(avahi_server));
1114
1115     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) {
1116
1117         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1118             avahi_log_warn("Error parsing Server::GetHostNameFqdn message");
1119             goto fail;
1120         }
1121     
1122         return respond_string(c, m, avahi_server_get_host_name_fqdn(avahi_server));
1123         
1124     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")) {
1125
1126         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1127             avahi_log_warn("Error parsing Server::GetVersionString message");
1128             goto fail;
1129         }
1130     
1131         return respond_string(c, m, PACKAGE_STRING);
1132
1133     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetState")) {
1134
1135         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1136             avahi_log_warn("Error parsing Server::GetState message");
1137             goto fail;
1138         }
1139
1140         return respond_int32(c, m, (gint32) avahi_server_get_state(avahi_server));
1141
1142     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceNameByIndex")) {
1143         gint32 idx;
1144         int fd;
1145         struct ifreq ifr;
1146         
1147         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INT32, &idx, DBUS_TYPE_INVALID))) {
1148             avahi_log_warn("Error parsing Server::GetNetworkInterfaceNameByIndex message");
1149             goto fail;
1150         }
1151
1152 #ifdef VALGRIND_WORKAROUND
1153         return respond_string(c, m, "blah");
1154 #else
1155         
1156         if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1157             gchar txt[256];
1158             g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1159             return respond_error(c, m, AVAHI_ERR_OS, txt);
1160         }
1161
1162         memset(&ifr, 0, sizeof(ifr));
1163         ifr.ifr_ifindex = idx;
1164
1165         if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
1166             gchar txt[256];
1167             g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1168             close(fd);
1169             return respond_error(c, m, AVAHI_ERR_OS, txt);
1170         }
1171
1172         close(fd);
1173         
1174         return respond_string(c, m, ifr.ifr_name);
1175 #endif
1176         
1177     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceIndexByName")) {
1178         gchar *n;
1179         int fd;
1180         struct ifreq ifr;
1181         
1182         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1183             avahi_log_warn("Error parsing Server::GetNetworkInterfaceIndexByName message");
1184             goto fail;
1185         }
1186
1187 #ifdef VALGRIND_WORKAROUND
1188         return respond_int32(c, m, 1);
1189 #else
1190         if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1191             gchar txt[256];
1192             g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1193             return respond_error(c, m, AVAHI_ERR_OS, txt);
1194         }
1195
1196         memset(&ifr, 0, sizeof(ifr));
1197         g_snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", n);
1198
1199         if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
1200             gchar txt[256];
1201             g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1202             close(fd);
1203             return respond_error(c, m, AVAHI_ERR_OS, txt);
1204         }
1205
1206         close(fd);
1207         
1208         return respond_int32(c, m, ifr.ifr_ifindex);
1209 #endif
1210
1211     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) {
1212         gchar *n, * t;
1213         
1214         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1215             avahi_log_warn("Error parsing Server::GetAlternativeHostName message");
1216             goto fail;
1217         }
1218
1219         t = avahi_alternative_host_name(n);
1220         respond_string(c, m, t);
1221         g_free(t);
1222
1223         return DBUS_HANDLER_RESULT_HANDLED;
1224
1225     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeServiceName")) {
1226         gchar *n, *t;
1227         
1228         if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
1229             avahi_log_warn("Error parsing Server::GetAlternativeServiceName message");
1230             goto fail;
1231         }
1232
1233         t = avahi_alternative_service_name(n);
1234         respond_string(c, m, t);
1235         g_free(t);
1236
1237         return DBUS_HANDLER_RESULT_HANDLED;
1238         
1239     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew")) {
1240         Client *client;
1241         EntryGroupInfo *i;
1242         static const DBusObjectPathVTable vtable = {
1243             NULL,
1244             msg_entry_group_impl,
1245             NULL,
1246             NULL,
1247             NULL,
1248             NULL
1249         };
1250
1251         if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1252             avahi_log_warn("Error parsing Server::EntryGroupNew message");
1253             goto fail;
1254         }
1255
1256         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1257             avahi_log_warn("Too many clients, client request failed.");
1258             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1259         }
1260
1261         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1262             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1263             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1264         }
1265
1266         i = g_new(EntryGroupInfo, 1);
1267         i->id = ++client->current_id;
1268         i->client = client;
1269         i->path = g_strdup_printf("/Client%u/EntryGroup%u", client->id, i->id);
1270         i->n_entries = 0;
1271         AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i);
1272         client->n_objects++;
1273         
1274         if (!(i->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, i))) {
1275             entry_group_free(i);
1276             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1277         }
1278
1279         dbus_connection_register_object_path(c, i->path, &vtable, i);
1280         return respond_path(c, m, i->path);
1281         
1282     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName")) {
1283         Client *client;
1284         gint32 interface, protocol, aprotocol;
1285         gchar *name;
1286         HostNameResolverInfo *i;
1287             
1288         if (!dbus_message_get_args(
1289                 m, &error,
1290                 DBUS_TYPE_INT32, &interface,
1291                 DBUS_TYPE_INT32, &protocol,
1292                 DBUS_TYPE_STRING, &name,
1293                 DBUS_TYPE_INT32, &aprotocol,
1294                 DBUS_TYPE_INVALID) || !name) {
1295             avahi_log_warn("Error parsing Server::ResolveHostName message");
1296             goto fail;
1297         }
1298
1299         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1300             avahi_log_warn("Too many clients, client request failed.");
1301             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1302         }
1303
1304         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1305             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1306             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1307         }
1308
1309         i = g_new(HostNameResolverInfo, 1);
1310         i->client = client;
1311         i->message = dbus_message_ref(m);
1312         AVAHI_LLIST_PREPEND(HostNameResolverInfo, host_name_resolvers, client->host_name_resolvers, i);
1313         client->n_objects++;
1314
1315         if (!(i->host_name_resolver = avahi_host_name_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, (AvahiProtocol) aprotocol, host_name_resolver_callback, i))) {
1316             host_name_resolver_free(i);
1317             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1318         }
1319         
1320         return DBUS_HANDLER_RESULT_HANDLED;
1321         
1322     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress")) {
1323         Client *client;
1324         gint32 interface, protocol;
1325         gchar *address;
1326         AddressResolverInfo *i;
1327         AvahiAddress a;
1328             
1329         if (!dbus_message_get_args(
1330                 m, &error,
1331                 DBUS_TYPE_INT32, &interface,
1332                 DBUS_TYPE_INT32, &protocol,
1333                 DBUS_TYPE_STRING, &address,
1334                 DBUS_TYPE_INVALID) || !address) {
1335             avahi_log_warn("Error parsing Server::ResolveAddress message");
1336             goto fail;
1337         }
1338
1339         if (!avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))
1340             return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
1341
1342         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1343             avahi_log_warn("Too many clients, client request failed.");
1344             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1345         }
1346
1347         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1348             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1349             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1350         }
1351
1352         i = g_new(AddressResolverInfo, 1);
1353         i->client = client;
1354         i->message = dbus_message_ref(m);
1355         AVAHI_LLIST_PREPEND(AddressResolverInfo, address_resolvers, client->address_resolvers, i);
1356         client->n_objects++;
1357
1358         if (!(i->address_resolver = avahi_address_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, &a, address_resolver_callback, i))) {
1359             address_resolver_free(i);
1360             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1361         }
1362         
1363         return DBUS_HANDLER_RESULT_HANDLED;
1364         
1365     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew")) {
1366         Client *client;
1367         DomainBrowserInfo *i;
1368         static const DBusObjectPathVTable vtable = {
1369             NULL,
1370             msg_domain_browser_impl,
1371             NULL,
1372             NULL,
1373             NULL,
1374             NULL
1375         };
1376         gint32 interface, protocol, type;
1377         gchar *domain;
1378         
1379
1380         if (!dbus_message_get_args(
1381                 m, &error,
1382                 DBUS_TYPE_INT32, &interface,
1383                 DBUS_TYPE_INT32, &protocol,
1384                 DBUS_TYPE_STRING, &domain,
1385                 DBUS_TYPE_INT32, &type,
1386                 DBUS_TYPE_INVALID) || type < 0 || type >= AVAHI_DOMAIN_BROWSER_MAX) {
1387             avahi_log_warn("Error parsing Server::DomainBrowserNew message");
1388             goto fail;
1389         }
1390
1391         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1392             avahi_log_warn("Too many clients, client request failed.");
1393             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1394         }
1395
1396         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1397             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1398             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1399         }
1400
1401         if (!*domain)
1402             domain = NULL;
1403
1404         i = g_new(DomainBrowserInfo, 1);
1405         i->id = ++client->current_id;
1406         i->client = client;
1407         i->path = g_strdup_printf("/Client%u/DomainBrowser%u", client->id, i->id);
1408         AVAHI_LLIST_PREPEND(DomainBrowserInfo, domain_browsers, client->domain_browsers, i);
1409         client->n_objects++;
1410
1411         if (!(i->domain_browser = avahi_domain_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, (AvahiDomainBrowserType) type, domain_browser_callback, i))) {
1412             domain_browser_free(i);
1413             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1414         }
1415         
1416         dbus_connection_register_object_path(c, i->path, &vtable, i);
1417         return respond_path(c, m, i->path);
1418
1419     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceTypeBrowserNew")) {
1420         Client *client;
1421         ServiceTypeBrowserInfo *i;
1422         static const DBusObjectPathVTable vtable = {
1423             NULL,
1424             msg_service_type_browser_impl,
1425             NULL,
1426             NULL,
1427             NULL,
1428             NULL
1429         };
1430         gint32 interface, protocol;
1431         gchar *domain;
1432         
1433         if (!dbus_message_get_args(
1434                 m, &error,
1435                 DBUS_TYPE_INT32, &interface,
1436                 DBUS_TYPE_INT32, &protocol,
1437                 DBUS_TYPE_STRING, &domain,
1438                 DBUS_TYPE_INVALID)) {
1439             avahi_log_warn("Error parsing Server::ServiceTypeBrowserNew message");
1440             goto fail;
1441         }
1442
1443         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1444             avahi_log_warn("Too many clients, client request failed.");
1445             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1446         }
1447
1448
1449         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1450             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1451             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1452         }
1453
1454         if (!*domain)
1455             domain = NULL;
1456
1457         i = g_new(ServiceTypeBrowserInfo, 1);
1458         i->id = ++client->current_id;
1459         i->client = client;
1460         i->path = g_strdup_printf("/Client%u/ServiceTypeBrowser%u", client->id, i->id);
1461         AVAHI_LLIST_PREPEND(ServiceTypeBrowserInfo, service_type_browsers, client->service_type_browsers, i);
1462         client->n_objects++;
1463
1464         if (!(i->service_type_browser = avahi_service_type_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, domain, service_type_browser_callback, i))) {
1465             service_type_browser_free(i);
1466             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1467         }
1468         
1469         dbus_connection_register_object_path(c, i->path, &vtable, i);
1470         return respond_path(c, m, i->path);
1471         
1472      } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew")) {
1473         Client *client;
1474         ServiceBrowserInfo *i;
1475         static const DBusObjectPathVTable vtable = {
1476             NULL,
1477             msg_service_browser_impl,
1478             NULL,
1479             NULL,
1480             NULL,
1481             NULL
1482         };
1483         gint32 interface, protocol;
1484         gchar *domain, *type;
1485         
1486         if (!dbus_message_get_args(
1487                 m, &error,
1488                 DBUS_TYPE_INT32, &interface,
1489                 DBUS_TYPE_INT32, &protocol,
1490                 DBUS_TYPE_STRING, &type,
1491                 DBUS_TYPE_STRING, &domain,
1492                 DBUS_TYPE_INVALID) || !type) {
1493             avahi_log_warn("Error parsing Server::ServiceBrowserNew message");
1494             goto fail;
1495         }
1496
1497         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1498             avahi_log_warn("Too many clients, client request failed.");
1499             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1500         }
1501
1502
1503         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1504             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1505             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1506         }
1507
1508         if (!*domain)
1509             domain = NULL;
1510
1511         i = g_new(ServiceBrowserInfo, 1);
1512         i->id = ++client->current_id;
1513         i->client = client;
1514         i->path = g_strdup_printf("/Client%u/ServiceBrowser%u", client->id, i->id);
1515         AVAHI_LLIST_PREPEND(ServiceBrowserInfo, service_browsers, client->service_browsers, i);
1516         client->n_objects++;
1517
1518         if (!(i->service_browser = avahi_service_browser_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, type, domain, service_browser_callback, i))) {
1519             service_browser_free(i);
1520             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1521         }
1522         
1523         dbus_connection_register_object_path(c, i->path, &vtable, i);
1524         return respond_path(c, m, i->path);
1525         
1526     } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService")) {
1527         Client *client;
1528         gint32 interface, protocol, aprotocol;
1529         gchar *name, *type, *domain;
1530         ServiceResolverInfo *i;
1531             
1532         if (!dbus_message_get_args(
1533                 m, &error,
1534                 DBUS_TYPE_INT32, &interface,
1535                 DBUS_TYPE_INT32, &protocol,
1536                 DBUS_TYPE_STRING, &name,
1537                 DBUS_TYPE_STRING, &type,
1538                 DBUS_TYPE_STRING, &domain,
1539                 DBUS_TYPE_INT32, &aprotocol,
1540                 DBUS_TYPE_INVALID) || !name || !type) {
1541             avahi_log_warn("Error parsing Server::ResolveService message");
1542             goto fail;
1543         }
1544
1545         if (!(client = client_get(dbus_message_get_sender(m), TRUE))) {
1546             avahi_log_warn("Too many clients, client request failed.");
1547             return respond_error(c, m, AVAHI_ERR_TOO_MANY_CLIENTS, NULL);
1548         }
1549         
1550         if (client->n_objects >= MAX_OBJECTS_PER_CLIENT) {
1551             avahi_log_warn("Too many objects for client '%s', client request failed.", client->name);
1552             return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
1553         }
1554
1555         if (!*domain)
1556             domain = NULL;
1557         
1558         i = g_new(ServiceResolverInfo, 1);
1559         i->client = client;
1560         i->message = dbus_message_ref(m);
1561         AVAHI_LLIST_PREPEND(ServiceResolverInfo, service_resolvers, client->service_resolvers, i);
1562         client->n_objects++;
1563
1564         if (!(i->service_resolver = avahi_service_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, (AvahiProtocol) aprotocol, service_resolver_callback, i))) {
1565             service_resolver_free(i);
1566             return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
1567         }
1568         
1569         return DBUS_HANDLER_RESULT_HANDLED;
1570      }
1571
1572     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
1573
1574 fail:
1575     if (dbus_error_is_set(&error))
1576         dbus_error_free(&error);
1577     
1578     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1579 }
1580
1581 void dbus_protocol_server_state_changed(AvahiServerState state) {
1582     DBusMessage *m;
1583     gint32 t;
1584     
1585     if (!server)
1586         return;
1587
1588     m = dbus_message_new_signal(AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged");
1589     t = (gint32) state;
1590     dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
1591     dbus_connection_send(server->bus, m, NULL);
1592     dbus_message_unref(m);
1593 }
1594
1595 int dbus_protocol_setup(GMainLoop *loop) {
1596     DBusError error;
1597
1598     static const DBusObjectPathVTable server_vtable = {
1599         NULL,
1600         msg_server_impl,
1601         NULL,
1602         NULL,
1603         NULL,
1604         NULL
1605     };
1606
1607     dbus_error_init(&error);
1608
1609     server = g_malloc(sizeof(Server));
1610     server->clients = NULL;
1611     server->current_id = 0;
1612     server->n_clients = 0;
1613
1614     server->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
1615     if (dbus_error_is_set(&error)) {
1616         avahi_log_warn("dbus_bus_get(): %s", error.message);
1617         goto fail;
1618     }
1619
1620     dbus_connection_setup_with_g_main(server->bus, NULL);
1621     dbus_connection_set_exit_on_disconnect(server->bus, FALSE);
1622
1623     dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, 0, &error);
1624     if (dbus_error_is_set(&error)) {
1625         avahi_log_warn("dbus_bus_request_name(): %s", error.message);
1626         goto fail;
1627     }
1628
1629     dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS  "'", &error);
1630
1631     dbus_connection_add_filter(server->bus, msg_signal_filter_impl, loop, NULL);
1632     dbus_connection_register_object_path(server->bus, AVAHI_DBUS_PATH_SERVER, &server_vtable, NULL);
1633
1634     return 0;
1635
1636 fail:
1637     if (server->bus) {
1638         dbus_connection_disconnect(server->bus);
1639         dbus_connection_unref(server->bus);
1640     }
1641     
1642     dbus_error_free (&error);
1643     g_free(server);
1644     server = NULL;
1645     return -1;
1646 }
1647
1648 void dbus_protocol_shutdown(void) {
1649
1650     if (server) {
1651     
1652         while (server->clients)
1653             client_free(server->clients);
1654
1655         g_assert(server->n_clients == 0);
1656
1657         if (server->bus) {
1658             dbus_connection_disconnect(server->bus);
1659             dbus_connection_unref(server->bus);
1660         }
1661
1662         g_free(server);
1663         server = NULL;
1664     }
1665 }