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
29 #define DBUS_API_SUBJECT_TO_CHANGE
30 #include <dbus/dbus.h>
31 #include <dbus/dbus-glib-lowlevel.h>
33 #include <avahi-core/llist.h>
34 #include <avahi-core/log.h>
35 #include <avahi-core/core.h>
37 #include "dbus-protocol.h"
40 #define AVAHI_DBUS_NAME "org.freedesktop.Avahi"
41 #define AVAHI_DBUS_INTERFACE_SERVER AVAHI_DBUS_NAME".Server"
42 #define AVAHI_DBUS_PATH_SERVER "/org/freedesktop/Avahi/Server"
43 #define AVAHI_DBUS_INTERFACE_ENTRY_GROUP AVAHI_DBUS_NAME".EntryGroup"
46 - AvahiServiceResolver
48 - AvahiServiceTypeBrowser
49 - AvahiServiceBrowser */
51 typedef struct Server Server;
52 typedef struct Client Client;
53 typedef struct EntryGroupInfo EntryGroupInfo;
54 typedef struct HostNameResolverInfo HostNameResolverInfo;
55 typedef struct AddressResolverInfo AddressResolverInfo;
57 struct EntryGroupInfo {
60 AvahiEntryGroup *entry_group;
63 AVAHI_LLIST_FIELDS(EntryGroupInfo, entry_groups);
66 struct HostNameResolverInfo {
68 AvahiHostNameResolver *host_name_resolver;
71 AVAHI_LLIST_FIELDS(HostNameResolverInfo, host_name_resolvers);
74 struct AddressResolverInfo {
76 AvahiAddressResolver *address_resolver;
79 AVAHI_LLIST_FIELDS(AddressResolverInfo, address_resolvers);
87 AVAHI_LLIST_FIELDS(Client, clients);
88 AVAHI_LLIST_HEAD(EntryGroupInfo, entry_groups);
89 AVAHI_LLIST_HEAD(HostNameResolverInfo, host_name_resolvers);
90 AVAHI_LLIST_HEAD(AddressResolverInfo, address_resolvers);
95 AVAHI_LLIST_HEAD(Client, clients);
99 static Server *server = NULL;
101 static void entry_group_free(EntryGroupInfo *i) {
104 avahi_entry_group_free(i->entry_group);
105 dbus_connection_unregister_object_path(server->bus, i->path);
107 AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
111 static void host_name_resolver_free(HostNameResolverInfo *i) {
114 avahi_host_name_resolver_free(i->host_name_resolver);
115 dbus_message_unref(i->message);
116 AVAHI_LLIST_REMOVE(HostNameResolverInfo, host_name_resolvers, i->client->host_name_resolvers, i);
120 static void address_resolver_free(AddressResolverInfo *i) {
123 avahi_address_resolver_free(i->address_resolver);
124 dbus_message_unref(i->message);
125 AVAHI_LLIST_REMOVE(AddressResolverInfo, address_resolvers, i->client->address_resolvers, i);
129 static void client_free(Client *c) {
134 while (c->entry_groups)
135 entry_group_free(c->entry_groups);
137 while (c->host_name_resolvers)
138 host_name_resolver_free(c->host_name_resolvers);
140 while (c->address_resolvers)
141 address_resolver_free(c->address_resolvers);
144 AVAHI_LLIST_REMOVE(Client, clients, server->clients, c);
148 static Client *client_get(const gchar *name, gboolean create) {
154 for (client = server->clients; client; client = client->clients_next)
155 if (!strcmp(name, client->name))
161 /* If not existant yet, create a new entry */
162 client = g_new(Client, 1);
163 client->id = server->current_id++;
164 client->name = g_strdup(name);
165 client->current_id = 0;
166 AVAHI_LLIST_HEAD_INIT(EntryGroupInfo, client->entry_groups);
167 AVAHI_LLIST_HEAD_INIT(HostNameResolverInfo, client->host_name_resolvers);
168 AVAHI_LLIST_HEAD_INIT(AddressResolverInfo, client->address_resolvers);
170 AVAHI_LLIST_PREPEND(Client, clients, server->clients, client);
174 static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
175 GMainLoop *loop = userdata;
178 dbus_error_init(&error);
180 /* avahi_log_debug("dbus: interface=%s, path=%s, member=%s", */
181 /* dbus_message_get_interface(m), */
182 /* dbus_message_get_path(m), */
183 /* dbus_message_get_member(m)); */
185 if (dbus_message_is_signal(m, DBUS_INTERFACE_LOCAL, "Disconnected")) {
186 /* No, we shouldn't quit, but until we get somewhere
187 * usefull such that we can restore our state, we will */
188 avahi_log_warn("Disconnnected from d-bus, terminating...");
189 g_main_loop_quit (loop);
190 return DBUS_HANDLER_RESULT_HANDLED;
192 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameAcquired")) {
195 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
196 avahi_log_warn("Error parsing NameAcquired message");
200 avahi_log_info("dbus: name acquired (%s)", name);
201 return DBUS_HANDLER_RESULT_HANDLED;
202 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
203 gchar *name, *old, *new;
205 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID)) {
206 avahi_log_warn("Error parsing NameOwnerChanged message");
213 if ((client = client_get(name, FALSE))) {
214 avahi_log_info("dbus: client %s vanished", name);
221 if (dbus_error_is_set(&error))
222 dbus_error_free(&error);
224 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
227 static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, const gchar *error, const gchar *text) {
230 reply = dbus_message_new_error(m, error, text);
231 dbus_connection_send(c, reply, NULL);
232 dbus_message_unref(reply);
234 return DBUS_HANDLER_RESULT_HANDLED;
237 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) {
238 EntryGroupInfo *i = userdata;
246 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
248 dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
249 dbus_message_set_destination(m, i->client->name);
250 dbus_connection_send(server->bus, m, NULL);
251 dbus_message_unref(m);
254 static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) {
257 reply = dbus_message_new_method_return(m);
258 dbus_connection_send(c, reply, NULL);
259 dbus_message_unref(reply);
261 return DBUS_HANDLER_RESULT_HANDLED;
264 static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
266 EntryGroupInfo *i = userdata;
272 dbus_error_init(&error);
274 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
275 dbus_message_get_interface(m),
276 dbus_message_get_path(m),
277 dbus_message_get_member(m));
280 if (strcmp(dbus_message_get_sender(m), i->client->name))
281 return respond_error(c, m, DBUS_ERROR_ACCESS_DENIED, NULL);
283 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) {
285 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
286 avahi_log_warn("Error parsing EntryGroup::Free message");
291 return respond_ok(c, m);
292 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit")) {
294 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
295 avahi_log_warn("Error parsing EntryGroup::Commit message");
299 avahi_entry_group_commit(i->entry_group);
300 return respond_ok(c, m);
301 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) {
305 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
306 avahi_log_warn("Error parsing EntryGroup::GetState message");
310 t = (gint32) avahi_entry_group_get_state(i->entry_group);
311 reply = dbus_message_new_method_return(m);
312 dbus_message_append_args(reply, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
313 dbus_connection_send(c, reply, NULL);
314 dbus_message_unref(reply);
316 return DBUS_HANDLER_RESULT_HANDLED;
317 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
318 gint32 interface, protocol;
319 gchar *type, *name, *domain, *host;
323 AvahiStringList *strlst;
325 if (!dbus_message_get_args(
327 DBUS_TYPE_INT32, &interface,
328 DBUS_TYPE_INT32, &protocol,
329 DBUS_TYPE_STRING, &name,
330 DBUS_TYPE_STRING, &type,
331 DBUS_TYPE_STRING, &domain,
332 DBUS_TYPE_STRING, &host,
333 DBUS_TYPE_UINT16, &port,
334 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &txt, &txt_len,
335 DBUS_TYPE_INVALID) || !type || !*type || !name || !*name || !port) {
336 avahi_log_warn("Error parsing EntryGroup::AddService message");
340 strlst = avahi_string_list_new_from_array((const gchar**) txt, txt_len);
341 dbus_free_string_array(txt);
343 if (domain && !*domain)
349 if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, type, domain, host, port, strlst) < 0) {
350 avahi_log_warn("Failed to add service: %s", name);
351 return respond_error(c, m, "org.freedesktop.Avahi.InvalidServiceError", NULL);
353 avahi_log_info("Successfully added service: %s", name);
355 return respond_ok(c, m);
356 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
357 gint32 interface, protocol;
358 gchar *name, *address;
361 if (!dbus_message_get_args(
363 DBUS_TYPE_INT32, &interface,
364 DBUS_TYPE_INT32, &protocol,
365 DBUS_TYPE_STRING, &name,
366 DBUS_TYPE_STRING, &address,
367 DBUS_TYPE_INVALID) || !name || !*name || !address || !*address) {
368 avahi_log_warn("Error parsing EntryGroup::AddAddress message");
372 if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))) {
373 avahi_log_warn("Error parsing address data");
374 return respond_error(c, m, "org.freedesktop.Avahi.InvalidAddressError", NULL);
377 if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, 0, name, &a) < 0) {
378 avahi_log_warn("Failed to add service: %s", name);
379 return respond_error(c, m, "org.freedesktop.Avahi.InvalidAddressError", NULL);
381 avahi_log_info("Successfully added address: %s -> %s", name, address);
383 return respond_ok(c, m);
386 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
389 if (dbus_error_is_set(&error))
390 dbus_error_free(&error);
392 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
395 static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *host_name, const AvahiAddress *a, gpointer userdata) {
396 HostNameResolverInfo *i = userdata;
403 if (event == AVAHI_RESOLVER_FOUND) {
404 char t[256], *pt = t;
405 gint32 i_interface, i_protocol, i_aprotocol;
408 avahi_address_snprint(t, sizeof(t), a);
410 i_interface = (gint32) interface;
411 i_protocol = (gint32) protocol;
412 i_aprotocol = (gint32) a->family;
414 reply = dbus_message_new_method_return(i->message);
415 dbus_message_append_args(
417 DBUS_TYPE_INT32, &i_interface,
418 DBUS_TYPE_INT32, &i_protocol,
419 DBUS_TYPE_STRING, &host_name,
420 DBUS_TYPE_INT32, &i_aprotocol,
421 DBUS_TYPE_STRING, &pt,
425 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
426 reply = dbus_message_new_error(i->message, "org.freedesktop.Avahi.TimeoutError", NULL);
429 dbus_connection_send(server->bus, reply, NULL);
430 dbus_message_unref(reply);
432 host_name_resolver_free(i);
435 static void address_resolver_callback(AvahiAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const gchar *host_name, gpointer userdata) {
436 AddressResolverInfo *i = userdata;
443 if (event == AVAHI_RESOLVER_FOUND) {
444 char t[256], *pt = t;
445 gint32 i_interface, i_protocol, i_aprotocol;
448 avahi_address_snprint(t, sizeof(t), address);
450 i_interface = (gint32) interface;
451 i_protocol = (gint32) protocol;
452 i_aprotocol = (gint32) address->family;
454 reply = dbus_message_new_method_return(i->message);
455 dbus_message_append_args(
457 DBUS_TYPE_INT32, &i_interface,
458 DBUS_TYPE_INT32, &i_protocol,
459 DBUS_TYPE_INT32, &i_aprotocol,
460 DBUS_TYPE_STRING, &pt,
461 DBUS_TYPE_STRING, &host_name,
465 g_assert(event == AVAHI_RESOLVER_TIMEOUT);
466 reply = dbus_message_new_error(i->message, "org.freedesktop.Avahi.TimeoutError", NULL);
469 dbus_connection_send(server->bus, reply, NULL);
470 dbus_message_unref(reply);
472 address_resolver_free(i);
475 static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const gchar *text) {
478 reply = dbus_message_new_method_return(m);
479 dbus_message_append_args(reply, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
480 dbus_connection_send(c, reply, NULL);
481 dbus_message_unref(reply);
483 return DBUS_HANDLER_RESULT_HANDLED;
486 static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
489 dbus_error_init(&error);
491 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
492 dbus_message_get_interface(m),
493 dbus_message_get_path(m),
494 dbus_message_get_member(m));
496 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostName")) {
498 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
499 avahi_log_warn("Error parsing Server::GetHostName message");
503 return respond_string(c, m, avahi_server_get_host_name(avahi_server));
505 } if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) {
507 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
508 avahi_log_warn("Error parsing Server::GetDomainName message");
512 return respond_string(c, m, avahi_server_get_domain_name(avahi_server));
514 } if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) {
516 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
517 avahi_log_warn("Error parsing Server::GetHostNameFqdn message");
521 return respond_string(c, m, avahi_server_get_host_name_fqdn(avahi_server));
523 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew")) {
526 static const DBusObjectPathVTable vtable = {
528 msg_entry_group_impl,
536 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
537 avahi_log_warn("Error parsing Server::EntryGroupNew message");
541 client = client_get(dbus_message_get_sender(m), TRUE);
543 i = g_new(EntryGroupInfo, 1);
544 i->id = ++client->current_id;
546 i->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, i);
547 i->path = g_strdup_printf("/org/freedesktop/Avahi/Client%u/EntryGroup%u", client->id, i->id);
549 AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i);
551 dbus_connection_register_object_path(c, i->path, &vtable, i);
552 reply = dbus_message_new_method_return(m);
553 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &i->path, DBUS_TYPE_INVALID);
554 dbus_connection_send(c, reply, NULL);
555 dbus_message_unref(reply);
557 return DBUS_HANDLER_RESULT_HANDLED;
559 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName")) {
561 gint32 interface, protocol, aprotocol;
563 HostNameResolverInfo *i;
565 if (!dbus_message_get_args(
567 DBUS_TYPE_INT32, &interface,
568 DBUS_TYPE_INT32, &protocol,
569 DBUS_TYPE_STRING, &name,
570 DBUS_TYPE_INT32, &aprotocol,
571 DBUS_TYPE_INVALID) || !name || !*name) {
572 avahi_log_warn("Error parsing EntryGroup::ResolveHostName message");
576 client = client_get(dbus_message_get_sender(m), TRUE);
578 i = g_new(HostNameResolverInfo, 1);
580 i->message = dbus_message_ref(m);
581 i->host_name_resolver = avahi_host_name_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, name, (AvahiProtocol) aprotocol, host_name_resolver_callback, i);
583 AVAHI_LLIST_PREPEND(HostNameResolverInfo, host_name_resolvers, client->host_name_resolvers, i);
585 return DBUS_HANDLER_RESULT_HANDLED;
587 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress")) {
589 gint32 interface, protocol;
591 AddressResolverInfo *i;
594 if (!dbus_message_get_args(
596 DBUS_TYPE_INT32, &interface,
597 DBUS_TYPE_INT32, &protocol,
598 DBUS_TYPE_STRING, &address,
599 DBUS_TYPE_INVALID) || !address || !*address) {
600 avahi_log_warn("Error parsing EntryGroup::ResolveAddress message");
604 if (!avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a)) {
605 avahi_log_warn("Error parsing address data");
606 return respond_error(c, m, "org.freedesktop.Avahi.InvalidAddressError", NULL);
609 client = client_get(dbus_message_get_sender(m), TRUE);
611 i = g_new(AddressResolverInfo, 1);
613 i->message = dbus_message_ref(m);
614 i->address_resolver = avahi_address_resolver_new(avahi_server, (AvahiIfIndex) interface, (AvahiProtocol) protocol, &a, address_resolver_callback, i);
616 AVAHI_LLIST_PREPEND(AddressResolverInfo, address_resolvers, client->address_resolvers, i);
618 return DBUS_HANDLER_RESULT_HANDLED;
621 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
625 if (dbus_error_is_set(&error))
626 dbus_error_free(&error);
628 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
631 void dbus_protocol_server_state_changed(AvahiServerState state) {
638 m = dbus_message_new_signal(AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged");
640 dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
641 dbus_connection_send(server->bus, m, NULL);
642 dbus_message_unref(m);
645 int dbus_protocol_setup(GMainLoop *loop) {
648 static const DBusObjectPathVTable server_vtable = {
657 dbus_error_init(&error);
659 server = g_malloc(sizeof(Server));
660 server->clients = NULL;
661 server->current_id = 0;
663 server->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
664 if (dbus_error_is_set(&error)) {
665 avahi_log_warn("dbus_bus_get(): %s", error.message);
669 dbus_connection_setup_with_g_main(server->bus, NULL);
670 dbus_connection_set_exit_on_disconnect(server->bus, FALSE);
672 dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, 0, &error);
673 if (dbus_error_is_set(&error)) {
674 avahi_log_warn("dbus_bus_request_name(): %s", error.message);
678 dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS "'", &error);
680 dbus_connection_add_filter(server->bus, msg_signal_filter_impl, loop, NULL);
681 dbus_connection_register_object_path(server->bus, AVAHI_DBUS_PATH_SERVER, &server_vtable, NULL);
687 dbus_connection_disconnect(server->bus);
688 dbus_connection_unref(server->bus);
691 dbus_error_free (&error);
697 void dbus_protocol_shutdown(void) {
701 while (server->clients)
702 client_free(server->clients);
705 dbus_connection_disconnect(server->bus);
706 dbus_connection_unref(server->bus);