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