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