4 This file is part of avahi.
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.
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.
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
27 #include <sys/ioctl.h>
33 #define DBUS_API_SUBJECT_TO_CHANGE
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
37 #include <avahi-common/llist.h>
38 #include <avahi-core/log.h>
39 #include <avahi-core/core.h>
40 #include <avahi-common/dbus.h>
42 #include "dbus-protocol.h"
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;
55 #define MAX_CLIENTS 20
56 #define MAX_OBJECTS_PER_CLIENT 50
57 #define MAX_ENTRIES_PER_ENTRY_GROUP 20
59 struct EntryGroupInfo {
62 AvahiEntryGroup *entry_group;
67 AVAHI_LLIST_FIELDS(EntryGroupInfo, entry_groups);
70 struct HostNameResolverInfo {
72 AvahiHostNameResolver *host_name_resolver;
75 AVAHI_LLIST_FIELDS(HostNameResolverInfo, host_name_resolvers);
78 struct AddressResolverInfo {
80 AvahiAddressResolver *address_resolver;
83 AVAHI_LLIST_FIELDS(AddressResolverInfo, address_resolvers);
86 struct DomainBrowserInfo {
89 AvahiDomainBrowser *domain_browser;
92 AVAHI_LLIST_FIELDS(DomainBrowserInfo, domain_browsers);
95 struct ServiceTypeBrowserInfo {
98 AvahiServiceTypeBrowser *service_type_browser;
101 AVAHI_LLIST_FIELDS(ServiceTypeBrowserInfo, service_type_browsers);
104 struct ServiceBrowserInfo {
107 AvahiServiceBrowser *service_browser;
110 AVAHI_LLIST_FIELDS(ServiceBrowserInfo, service_browsers);
113 struct ServiceResolverInfo {
115 AvahiServiceResolver *service_resolver;
116 DBusMessage *message;
118 AVAHI_LLIST_FIELDS(ServiceResolverInfo, service_resolvers);
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);
139 AVAHI_LLIST_HEAD(Client, clients);
144 static Server *server = NULL;
146 static void entry_group_free(EntryGroupInfo *i) {
150 avahi_entry_group_free(i->entry_group);
151 dbus_connection_unregister_object_path(server->bus, i->path);
153 AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
155 i->client->n_objects--;
156 g_assert(i->client->n_objects >= 0);
161 static void host_name_resolver_free(HostNameResolverInfo *i) {
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);
169 i->client->n_objects--;
170 g_assert(i->client->n_objects >= 0);
175 static void address_resolver_free(AddressResolverInfo *i) {
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);
183 i->client->n_objects--;
184 g_assert(i->client->n_objects >= 0);
189 static void domain_browser_free(DomainBrowserInfo *i) {
192 if (i->domain_browser)
193 avahi_domain_browser_free(i->domain_browser);
194 dbus_connection_unregister_object_path(server->bus, i->path);
196 AVAHI_LLIST_REMOVE(DomainBrowserInfo, domain_browsers, i->client->domain_browsers, i);
198 i->client->n_objects--;
199 g_assert(i->client->n_objects >= 0);
204 static void service_type_browser_free(ServiceTypeBrowserInfo *i) {
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);
211 AVAHI_LLIST_REMOVE(ServiceTypeBrowserInfo, service_type_browsers, i->client->service_type_browsers, i);
213 i->client->n_objects--;
214 g_assert(i->client->n_objects >= 0);
219 static void service_browser_free(ServiceBrowserInfo *i) {
222 if (i->service_browser)
223 avahi_service_browser_free(i->service_browser);
224 dbus_connection_unregister_object_path(server->bus, i->path);
226 AVAHI_LLIST_REMOVE(ServiceBrowserInfo, service_browsers, i->client->service_browsers, i);
228 i->client->n_objects--;
229 g_assert(i->client->n_objects >= 0);
234 static void service_resolver_free(ServiceResolverInfo *i) {
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);
242 i->client->n_objects--;
243 g_assert(i->client->n_objects >= 0);
248 static void client_free(Client *c) {
253 while (c->entry_groups)
254 entry_group_free(c->entry_groups);
256 while (c->host_name_resolvers)
257 host_name_resolver_free(c->host_name_resolvers);
259 while (c->address_resolvers)
260 address_resolver_free(c->address_resolvers);
262 while (c->domain_browsers)
263 domain_browser_free(c->domain_browsers);
265 while (c->service_type_browsers)
266 service_type_browser_free(c->service_type_browsers);
268 while (c->service_browsers)
269 service_browser_free(c->service_browsers);
271 while (c->service_resolvers)
272 service_resolver_free(c->service_resolvers);
274 g_assert(c->n_objects == 0);
277 AVAHI_LLIST_REMOVE(Client, clients, server->clients, c);
280 server->n_clients --;
281 g_assert(server->n_clients >= 0);
284 static Client *client_get(const gchar *name, gboolean create) {
290 for (client = server->clients; client; client = client->clients_next)
291 if (!strcmp(name, client->name))
297 if (server->n_clients >= MAX_CLIENTS)
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);
314 AVAHI_LLIST_PREPEND(Client, clients, server->clients, client);
317 g_assert(server->n_clients > 0);
322 static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, gint error, const gchar *text) {
325 const gchar * const table[- AVAHI_ERR_MAX] = {
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,
346 AVAHI_DBUS_ERR_ACCESS_DENIED,
347 AVAHI_DBUS_ERR_INVALID_OPERATION
350 g_assert(-error > -AVAHI_OK);
351 g_assert(-error < -AVAHI_ERR_MAX);
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);
357 return DBUS_HANDLER_RESULT_HANDLED;
360 static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const gchar *text) {
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);
368 return DBUS_HANDLER_RESULT_HANDLED;
371 static DBusHandlerResult respond_int32(DBusConnection *c, DBusMessage *m, gint32 i) {
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);
379 return DBUS_HANDLER_RESULT_HANDLED;
382 static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) {
385 reply = dbus_message_new_method_return(m);
386 dbus_connection_send(c, reply, NULL);
387 dbus_message_unref(reply);
389 return DBUS_HANDLER_RESULT_HANDLED;
392 static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const gchar *path) {
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);
400 return DBUS_HANDLER_RESULT_HANDLED;
403 static DBusHandlerResult handle_introspect(DBusConnection *c, DBusMessage *m, const gchar *fname) {
406 GError *gerror = NULL;
413 dbus_error_init(&error);
415 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
416 avahi_log_warn("Error parsing Introspect message: %s", error.message);
420 path = g_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname);
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);
431 respond_string(c, m, contents);
434 return DBUS_HANDLER_RESULT_HANDLED;
437 if (dbus_error_is_set(&error))
438 dbus_error_free(&error);
440 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
444 static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
445 GMainLoop *loop = userdata;
448 dbus_error_init(&error);
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)); */
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;
462 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameAcquired")) {
465 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
466 avahi_log_warn("Error parsing NameAcquired message");
470 /* avahi_log_info("dbus: name acquired (%s)", name); */
471 return DBUS_HANDLER_RESULT_HANDLED;
473 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
474 gchar *name, *old, *new;
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");
484 if ((client = client_get(name, FALSE))) {
485 /* avahi_log_info("dbus: client %s vanished", name); */
492 if (dbus_error_is_set(&error))
493 dbus_error_free(&error);
495 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
498 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) {
499 EntryGroupInfo *i = userdata;
507 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
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);
515 static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
517 EntryGroupInfo *i = userdata;
523 dbus_error_init(&error);
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));
531 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
532 return handle_introspect(c, m, "EntryGroup.introspect");
535 if (strcmp(dbus_message_get_sender(m), i->client->name))
536 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
538 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) {
540 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
541 avahi_log_warn("Error parsing EntryGroup::Free message");
546 return respond_ok(c, m);
548 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit")) {
550 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
551 avahi_log_warn("Error parsing EntryGroup::Commit message");
555 avahi_entry_group_commit(i->entry_group);
556 return respond_ok(c, m);
559 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset")) {
561 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
562 avahi_log_warn("Error parsing EntryGroup::Reset message");
566 avahi_entry_group_reset(i->entry_group);
567 return respond_ok(c, m);
569 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) {
573 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
574 avahi_log_warn("Error parsing EntryGroup::IsEmpty message");
578 b = !!avahi_entry_group_is_empty(i->entry_group);
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);
585 return DBUS_HANDLER_RESULT_HANDLED;
587 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) {
589 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
590 avahi_log_warn("Error parsing EntryGroup::GetState message");
594 return respond_int32(c, m, (gint32) avahi_entry_group_get_state(i->entry_group));
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;
602 AvahiStringList *strlst;
604 if (!dbus_message_get_args(
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");
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);
625 strlst = avahi_string_list_new_from_array((const gchar**) txt, txt_len);
626 dbus_free_string_array(txt);
628 if (domain && !*domain)
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);
640 avahi_string_list_free(strlst);
642 return respond_ok(c, m);
644 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
645 gint32 interface, protocol;
646 gchar *name, *address;
649 if (!dbus_message_get_args(
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");
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);
665 if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))) {
666 return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
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);
674 return respond_ok(c, m);
677 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
680 if (dbus_error_is_set(&error))
681 dbus_error_free(&error);
683 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
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;
693 if (event == AVAHI_RESOLVER_FOUND) {
694 char t[256], *pt = t;
695 gint32 i_interface, i_protocol, i_aprotocol;
699 avahi_address_snprint(t, sizeof(t), a);
701 i_interface = (gint32) interface;
702 i_protocol = (gint32) protocol;
703 i_aprotocol = (gint32) a->family;
705 reply = dbus_message_new_method_return(i->message);
706 dbus_message_append_args(
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,
715 dbus_connection_send(server->bus, reply, NULL);
716 dbus_message_unref(reply);
718 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
720 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
723 host_name_resolver_free(i);
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;
733 if (event == AVAHI_RESOLVER_FOUND) {
734 char t[256], *pt = t;
735 gint32 i_interface, i_protocol, i_aprotocol;
739 avahi_address_snprint(t, sizeof(t), address);
741 i_interface = (gint32) interface;
742 i_protocol = (gint32) protocol;
743 i_aprotocol = (gint32) address->family;
745 reply = dbus_message_new_method_return(i->message);
746 dbus_message_append_args(
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,
755 dbus_connection_send(server->bus, reply, NULL);
756 dbus_message_unref(reply);
758 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
759 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
762 address_resolver_free(i);
765 static DBusHandlerResult msg_domain_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
767 DomainBrowserInfo *i = userdata;
773 dbus_error_init(&error);
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));
781 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
782 return handle_introspect(c, m, "DomainBrowser.introspect");
785 if (strcmp(dbus_message_get_sender(m), i->client->name))
786 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
788 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free")) {
790 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
791 avahi_log_warn("Error parsing DomainBrowser::Free message");
795 domain_browser_free(i);
796 return respond_ok(c, m);
800 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
803 if (dbus_error_is_set(&error))
804 dbus_error_free(&error);
806 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
809 static void domain_browser_callback(AvahiDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *domain, gpointer userdata) {
810 DomainBrowserInfo *i = userdata;
812 gint32 i_interface, i_protocol;
818 i_interface = (gint32) interface;
819 i_protocol = (gint32) protocol;
821 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
822 dbus_message_append_args(
824 DBUS_TYPE_INT32, &i_interface,
825 DBUS_TYPE_INT32, &i_protocol,
826 DBUS_TYPE_STRING, &domain,
828 dbus_message_set_destination(m, i->client->name);
829 dbus_connection_send(server->bus, m, NULL);
830 dbus_message_unref(m);
833 static DBusHandlerResult msg_service_type_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
835 ServiceTypeBrowserInfo *i = userdata;
841 dbus_error_init(&error);
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));
849 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
850 return handle_introspect(c, m, "ServiceTypeBrowser.introspect");
853 if (strcmp(dbus_message_get_sender(m), i->client->name))
854 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
856 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free")) {
858 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
859 avahi_log_warn("Error parsing ServiceTypeBrowser::Free message");
863 service_type_browser_free(i);
864 return respond_ok(c, m);
868 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
871 if (dbus_error_is_set(&error))
872 dbus_error_free(&error);
874 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
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;
880 gint32 i_interface, i_protocol;
887 i_interface = (gint32) interface;
888 i_protocol = (gint32) protocol;
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(
893 DBUS_TYPE_INT32, &i_interface,
894 DBUS_TYPE_INT32, &i_protocol,
895 DBUS_TYPE_STRING, &type,
896 DBUS_TYPE_STRING, &domain,
898 dbus_message_set_destination(m, i->client->name);
899 dbus_connection_send(server->bus, m, NULL);
900 dbus_message_unref(m);
903 static DBusHandlerResult msg_service_browser_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
905 ServiceBrowserInfo *i = userdata;
911 dbus_error_init(&error);
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));
919 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
920 return handle_introspect(c, m, "ServiceBrowser.Introspect");
923 if (strcmp(dbus_message_get_sender(m), i->client->name))
924 return respond_error(c, m, AVAHI_ERR_ACCESS_DENIED, NULL);
926 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free")) {
928 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
929 avahi_log_warn("Error parsing ServiceBrowser::Free message");
933 service_browser_free(i);
934 return respond_ok(c, m);
938 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
941 if (dbus_error_is_set(&error))
942 dbus_error_free(&error);
944 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
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;
950 gint32 i_interface, i_protocol;
958 i_interface = (gint32) interface;
959 i_protocol = (gint32) protocol;
961 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
962 dbus_message_append_args(
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,
970 dbus_message_set_destination(m, i->client->name);
971 dbus_connection_send(server->bus, m, NULL);
972 dbus_message_unref(m);
975 static void service_resolver_callback(
976 AvahiServiceResolver *r,
977 AvahiIfIndex interface,
978 AvahiProtocol protocol,
979 AvahiResolverEvent event,
983 const gchar *host_name,
984 const AvahiAddress *a,
986 AvahiStringList *txt,
989 ServiceResolverInfo *i = userdata;
994 if (event == AVAHI_RESOLVER_FOUND) {
995 char t[256], *pt = t;
996 gint32 i_interface, i_protocol, i_aprotocol;
1002 g_assert(host_name);
1005 avahi_address_snprint(t, sizeof(t), a);
1007 i_interface = (gint32) interface;
1008 i_protocol = (gint32) protocol;
1009 i_aprotocol = (gint32) a->family;
1011 array = g_new(gchar*, (n = avahi_string_list_length(txt)));
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);
1017 reply = dbus_message_new_method_return(i->message);
1018 dbus_message_append_args(
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,
1032 for (j = 0; j < n; j++)
1035 dbus_connection_send(server->bus, reply, NULL);
1036 dbus_message_unref(reply);
1038 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
1040 respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
1043 service_resolver_free(i);
1046 static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
1049 dbus_error_init(&error);
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));
1056 if (dbus_message_is_method_call(m, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
1057 return handle_introspect(c, m, "Server.introspect");
1059 else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostName")) {
1061 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1062 avahi_log_warn("Error parsing Server::GetHostName message");
1066 return respond_string(c, m, avahi_server_get_host_name(avahi_server));
1068 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) {
1070 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1071 avahi_log_warn("Error parsing Server::GetDomainName message");
1075 return respond_string(c, m, avahi_server_get_domain_name(avahi_server));
1077 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) {
1079 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1080 avahi_log_warn("Error parsing Server::GetHostNameFqdn message");
1084 return respond_string(c, m, avahi_server_get_host_name_fqdn(avahi_server));
1086 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetVersionString")) {
1088 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1089 avahi_log_warn("Error parsing Server::GetVersionString message");
1093 return respond_string(c, m, PACKAGE_STRING);
1095 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetState")) {
1097 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
1098 avahi_log_warn("Error parsing Server::GetState message");
1102 return respond_int32(c, m, (gint32) avahi_server_get_state(avahi_server));
1104 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceNameByIndex")) {
1109 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INT32, &idx, DBUS_TYPE_INVALID))) {
1110 avahi_log_warn("Error parsing Server::GetNetworkInterfaceNameByIndex message");
1114 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1116 g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1117 return respond_error(c, m, AVAHI_ERR_OS, txt);
1120 memset(&ifr, 0, sizeof(ifr));
1121 ifr.ifr_ifindex = idx;
1123 if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
1125 g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1127 return respond_error(c, m, AVAHI_ERR_OS, txt);
1132 return respond_string(c, m, ifr.ifr_name);
1134 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceIndexByName")) {
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");
1144 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1146 g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1147 return respond_error(c, m, AVAHI_ERR_OS, txt);
1150 memset(&ifr, 0, sizeof(ifr));
1151 g_snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", n);
1153 if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
1155 g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
1157 return respond_error(c, m, AVAHI_ERR_OS, txt);
1162 return respond_int32(c, m, ifr.ifr_ifindex);
1164 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) {
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");
1172 t = avahi_alternative_host_name(n);
1173 respond_string(c, m, t);
1176 return DBUS_HANDLER_RESULT_HANDLED;
1178 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeServiceName")) {
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");
1186 t = avahi_alternative_service_name(n);
1187 respond_string(c, m, t);
1190 return DBUS_HANDLER_RESULT_HANDLED;
1192 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew")) {
1195 static const DBusObjectPathVTable vtable = {
1197 msg_entry_group_impl,
1204 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
1205 avahi_log_warn("Error parsing Server::EntryGroupNew message");
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);
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);
1219 i = g_new(EntryGroupInfo, 1);
1220 i->id = ++client->current_id;
1222 i->path = g_strdup_printf("/Client%u/EntryGroup%u", client->id, i->id);
1224 AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i);
1225 client->n_objects++;
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);
1232 dbus_connection_register_object_path(c, i->path, &vtable, i);
1233 return respond_path(c, m, i->path);
1235 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName")) {
1237 gint32 interface, protocol, aprotocol;
1239 HostNameResolverInfo *i;
1241 if (!dbus_message_get_args(
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");
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);
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);
1262 i = g_new(HostNameResolverInfo, 1);
1264 i->message = dbus_message_ref(m);
1265 AVAHI_LLIST_PREPEND(HostNameResolverInfo, host_name_resolvers, client->host_name_resolvers, i);
1266 client->n_objects++;
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);
1273 return DBUS_HANDLER_RESULT_HANDLED;
1275 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress")) {
1277 gint32 interface, protocol;
1279 AddressResolverInfo *i;
1282 if (!dbus_message_get_args(
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");
1292 if (!avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))
1293 return respond_error(c, m, AVAHI_ERR_INVALID_ADDRESS, NULL);
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);
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);
1305 i = g_new(AddressResolverInfo, 1);
1307 i->message = dbus_message_ref(m);
1308 AVAHI_LLIST_PREPEND(AddressResolverInfo, address_resolvers, client->address_resolvers, i);
1309 client->n_objects++;
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);
1316 return DBUS_HANDLER_RESULT_HANDLED;
1318 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew")) {
1320 DomainBrowserInfo *i;
1321 static const DBusObjectPathVTable vtable = {
1323 msg_domain_browser_impl,
1329 gint32 interface, protocol, type;
1333 if (!dbus_message_get_args(
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");
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);
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);
1357 i = g_new(DomainBrowserInfo, 1);
1358 i->id = ++client->current_id;
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++;
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);
1369 dbus_connection_register_object_path(c, i->path, &vtable, i);
1370 return respond_path(c, m, i->path);
1372 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceTypeBrowserNew")) {
1374 ServiceTypeBrowserInfo *i;
1375 static const DBusObjectPathVTable vtable = {
1377 msg_service_type_browser_impl,
1383 gint32 interface, protocol;
1386 if (!dbus_message_get_args(
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");
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);
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);
1410 i = g_new(ServiceTypeBrowserInfo, 1);
1411 i->id = ++client->current_id;
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++;
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);
1422 dbus_connection_register_object_path(c, i->path, &vtable, i);
1423 return respond_path(c, m, i->path);
1425 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew")) {
1427 ServiceBrowserInfo *i;
1428 static const DBusObjectPathVTable vtable = {
1430 msg_service_browser_impl,
1436 gint32 interface, protocol;
1437 gchar *domain, *type;
1439 if (!dbus_message_get_args(
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");
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);
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);
1464 i = g_new(ServiceBrowserInfo, 1);
1465 i->id = ++client->current_id;
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++;
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);
1476 dbus_connection_register_object_path(c, i->path, &vtable, i);
1477 return respond_path(c, m, i->path);
1479 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService")) {
1481 gint32 interface, protocol, aprotocol;
1482 gchar *name, *type, *domain;
1483 ServiceResolverInfo *i;
1485 if (!dbus_message_get_args(
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");
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);
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);
1511 i = g_new(ServiceResolverInfo, 1);
1513 i->message = dbus_message_ref(m);
1514 AVAHI_LLIST_PREPEND(ServiceResolverInfo, service_resolvers, client->service_resolvers, i);
1515 client->n_objects++;
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);
1522 return DBUS_HANDLER_RESULT_HANDLED;
1525 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
1528 if (dbus_error_is_set(&error))
1529 dbus_error_free(&error);
1531 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1534 void dbus_protocol_server_state_changed(AvahiServerState state) {
1541 m = dbus_message_new_signal(AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged");
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);
1548 int dbus_protocol_setup(GMainLoop *loop) {
1551 static const DBusObjectPathVTable server_vtable = {
1560 dbus_error_init(&error);
1562 server = g_malloc(sizeof(Server));
1563 server->clients = NULL;
1564 server->current_id = 0;
1565 server->n_clients = 0;
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);
1573 dbus_connection_setup_with_g_main(server->bus, NULL);
1574 dbus_connection_set_exit_on_disconnect(server->bus, FALSE);
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);
1582 dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS "'", &error);
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);
1591 dbus_connection_disconnect(server->bus);
1592 dbus_connection_unref(server->bus);
1595 dbus_error_free (&error);
1601 void dbus_protocol_shutdown(void) {
1605 while (server->clients)
1606 client_free(server->clients);
1608 g_assert(server->n_clients == 0);
1611 dbus_connection_disconnect(server->bus);
1612 dbus_connection_unref(server->bus);