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>
38 #include "dbus-protocol.h"
41 #define AVAHI_DBUS_NAME "org.freedesktop.Avahi"
42 #define AVAHI_DBUS_INTERFACE_SERVER AVAHI_DBUS_NAME".Server"
43 #define AVAHI_DBUS_PATH_SERVER "/org/freedesktop/Avahi/Server"
44 #define AVAHI_DBUS_INTERFACE_ENTRY_GROUP AVAHI_DBUS_NAME".EntryGroup"
47 typedef struct Server Server;
48 typedef struct Client Client;
49 typedef struct EntryGroupInfo EntryGroupInfo;
51 struct EntryGroupInfo {
54 AvahiEntryGroup *entry_group;
57 AVAHI_LLIST_FIELDS(EntryGroupInfo, entry_groups);
65 AVAHI_LLIST_FIELDS(Client, clients);
66 AVAHI_LLIST_HEAD(EntryGroupInfo, entry_groups);
72 AVAHI_LLIST_HEAD(Client, clients);
76 static Server *server = NULL;
78 static void entry_group_free(EntryGroupInfo *i) {
81 avahi_entry_group_free(i->entry_group);
82 dbus_connection_unregister_object_path(server->bus, i->path);
84 AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
88 static void client_free(Client *c) {
93 while (c->entry_groups)
94 entry_group_free(c->entry_groups);
97 AVAHI_LLIST_REMOVE(Client, clients, server->clients, c);
101 static Client *client_get(const gchar *name, gboolean create) {
107 for (client = server->clients; client; client = client->clients_next)
108 if (!strcmp(name, client->name))
114 /* If not existant yet, create a new entry */
115 client = g_new(Client, 1);
116 client->id = server->current_id++;
117 client->name = g_strdup(name);
118 client->current_id = 0;
119 AVAHI_LLIST_HEAD_INIT(Client, client->entry_groups);
121 AVAHI_LLIST_PREPEND(Client, clients, server->clients, client);
125 static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
126 GMainLoop *loop = userdata;
129 dbus_error_init(&error);
131 /* avahi_log_debug("dbus: interface=%s, path=%s, member=%s", */
132 /* dbus_message_get_interface(m), */
133 /* dbus_message_get_path(m), */
134 /* dbus_message_get_member(m)); */
136 if (dbus_message_is_signal(m, DBUS_INTERFACE_LOCAL, "Disconnected")) {
137 /* No, we shouldn't quit, but until we get somewhere
138 * usefull such that we can restore our state, we will */
139 avahi_log_warn("Disconnnected from d-bus, terminating...");
140 g_main_loop_quit (loop);
141 return DBUS_HANDLER_RESULT_HANDLED;
143 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameAcquired")) {
146 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
147 avahi_log_warn("Error parsing NameAcquired message");
151 avahi_log_info("dbus: name acquired (%s)", name);
152 return DBUS_HANDLER_RESULT_HANDLED;
153 } else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
154 gchar *name, *old, *new;
156 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID)) {
157 avahi_log_warn("Error parsing NameOwnerChanged message");
164 if ((client = client_get(name, FALSE))) {
165 avahi_log_info("dbus: client %s vanished", name);
172 if (dbus_error_is_set(&error))
173 dbus_error_free(&error);
175 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
178 static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, const gchar *error, const gchar *text) {
181 reply = dbus_message_new_error(m, error, text);
182 dbus_connection_send(c, reply, NULL);
183 dbus_message_unref(reply);
185 return DBUS_HANDLER_RESULT_HANDLED;
188 static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) {
189 EntryGroupInfo *i = userdata;
197 m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
199 dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
200 dbus_message_set_destination(m, i->client->name);
201 dbus_connection_send(server->bus, m, NULL);
202 dbus_message_unref(m);
205 static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) {
208 reply = dbus_message_new_method_return(m);
209 dbus_connection_send(c, reply, NULL);
210 dbus_message_unref(reply);
212 return DBUS_HANDLER_RESULT_HANDLED;
215 static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
217 EntryGroupInfo *i = userdata;
223 dbus_error_init(&error);
225 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
226 dbus_message_get_interface(m),
227 dbus_message_get_path(m),
228 dbus_message_get_member(m));
231 if (strcmp(dbus_message_get_sender(m), i->client->name))
232 return respond_error(c, m, DBUS_ERROR_ACCESS_DENIED, NULL);
234 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Free")) {
236 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
237 avahi_log_warn("Error parsing EntryGroup::Free message");
242 return respond_ok(c, m);
243 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit")) {
245 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
246 avahi_log_warn("Error parsing EntryGroup::Commit message");
250 avahi_entry_group_commit(i->entry_group);
251 return respond_ok(c, m);
252 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) {
256 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
257 avahi_log_warn("Error parsing EntryGroup::GetState message");
261 t = (gint32) avahi_entry_group_get_state(i->entry_group);
262 reply = dbus_message_new_method_return(m);
263 dbus_message_append_args(reply, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
264 dbus_connection_send(c, reply, NULL);
265 dbus_message_unref(reply);
267 return DBUS_HANDLER_RESULT_HANDLED;
268 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
269 gint32 interface, protocol;
270 gchar *type, *name, *domain, *host;
274 AvahiStringList *strlst;
276 if (!dbus_message_get_args(
278 DBUS_TYPE_INT32, &interface,
279 DBUS_TYPE_INT32, &protocol,
280 DBUS_TYPE_STRING, &type,
281 DBUS_TYPE_STRING, &name,
282 DBUS_TYPE_STRING, &domain,
283 DBUS_TYPE_STRING, &host,
284 DBUS_TYPE_UINT16, &port,
285 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &txt, &txt_len,
286 DBUS_TYPE_INVALID) || !type || !*type || !name || !*name || !port) {
287 avahi_log_warn("Error parsing EntryGroup::AddService message");
291 strlst = avahi_string_list_new_from_array((const gchar**) txt, txt_len);
292 dbus_free_string_array(txt);
294 if (domain && !*domain)
300 if (avahi_server_add_service_strlst(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, type, name, domain, host, port, strlst) < 0) {
301 avahi_log_warn("Failed to add service: %s", name);
302 return respond_error(c, m, "org.freedesktop.Avahi.InvalidServiceError", NULL);
304 avahi_log_info("Successfully added service: %s", name);
306 return respond_ok(c, m);
307 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
308 gint32 interface, protocol;
309 gchar *name, *address;
312 if (!dbus_message_get_args(
314 DBUS_TYPE_INT32, &interface,
315 DBUS_TYPE_INT32, &protocol,
316 DBUS_TYPE_STRING, &name,
317 DBUS_TYPE_STRING, &address,
318 DBUS_TYPE_INVALID) || !name || !*name || !address || !*address) {
319 avahi_log_warn("Error parsing EntryGroup::AddAddress message");
323 if (!(avahi_address_parse(address, AVAHI_PROTO_UNSPEC, &a))) {
324 avahi_log_warn("Error parsing address data");
325 return respond_error(c, m, "org.freedesktop.Avahi.InvalidAddressError", NULL);
328 if (avahi_server_add_address(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, 0, name, &a) < 0) {
329 avahi_log_warn("Failed to add service: %s", name);
330 return respond_error(c, m, "org.freedesktop.Avahi.InvalidAddressError", NULL);
332 avahi_log_info("Successfully added address: %s -> %s", name, address);
334 return respond_ok(c, m);
337 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
340 if (dbus_error_is_set(&error))
341 dbus_error_free(&error);
343 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
346 static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const gchar *text) {
349 reply = dbus_message_new_method_return(m);
350 dbus_message_append_args(reply, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
351 dbus_connection_send(c, reply, NULL);
352 dbus_message_unref(reply);
354 return DBUS_HANDLER_RESULT_HANDLED;
357 static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
360 dbus_error_init(&error);
362 avahi_log_debug("dbus: interface=%s, path=%s, member=%s",
363 dbus_message_get_interface(m),
364 dbus_message_get_path(m),
365 dbus_message_get_member(m));
367 if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostName")) {
369 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
370 avahi_log_warn("Error parsing Server::GetHostName message");
374 return respond_string(c, m, avahi_server_get_host_name(avahi_server));
376 } if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetDomainName")) {
378 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
379 avahi_log_warn("Error parsing Server::GetDomainName message");
383 return respond_string(c, m, avahi_server_get_domain_name(avahi_server));
385 } if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetHostNameFqdn")) {
387 if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) {
388 avahi_log_warn("Error parsing Server::GetHostNameFqdn message");
392 return respond_string(c, m, avahi_server_get_host_name_fqdn(avahi_server));
394 } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew")) {
397 static const DBusObjectPathVTable vtable = {
399 msg_entry_group_impl,
407 if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
408 avahi_log_warn("Error parsing Server::EntryGroupNew message");
412 client = client_get(dbus_message_get_sender(m), TRUE);
414 i = g_new(EntryGroupInfo, 1);
415 i->id = ++client->current_id;
417 i->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, i);
418 i->path = g_strdup_printf("/org/freedesktop/Avahi/Client%u/EntryGroup%u", client->id, i->id);
420 AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i);
422 dbus_connection_register_object_path(c, i->path, &vtable, i);
423 reply = dbus_message_new_method_return(m);
424 dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &i->path, DBUS_TYPE_INVALID);
425 dbus_connection_send(c, reply, NULL);
426 dbus_message_unref(reply);
428 return DBUS_HANDLER_RESULT_HANDLED;
431 avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
435 if (dbus_error_is_set(&error))
436 dbus_error_free(&error);
438 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
441 void dbus_protocol_server_state_changed(AvahiServerState state) {
448 m = dbus_message_new_signal(AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged");
450 dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
451 dbus_connection_send(server->bus, m, NULL);
452 dbus_message_unref(m);
455 int dbus_protocol_setup(GMainLoop *loop) {
458 static const DBusObjectPathVTable server_vtable = {
467 dbus_error_init(&error);
469 server = g_malloc(sizeof(Server));
470 server->clients = NULL;
471 server->current_id = 0;
473 server->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
474 if (dbus_error_is_set(&error)) {
475 avahi_log_warn("dbus_bus_get(): %s", error.message);
479 dbus_connection_setup_with_g_main(server->bus, NULL);
480 dbus_connection_set_exit_on_disconnect(server->bus, FALSE);
482 dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, 0, &error);
483 if (dbus_error_is_set(&error)) {
484 avahi_log_warn("dbus_bus_request_name(): %s", error.message);
488 dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS "'", &error);
490 dbus_connection_add_filter(server->bus, msg_signal_filter_impl, loop, NULL);
491 dbus_connection_register_object_path(server->bus, AVAHI_DBUS_PATH_SERVER, &server_vtable, NULL);
497 dbus_connection_disconnect(server->bus);
498 dbus_connection_unref(server->bus);
501 dbus_error_free (&error);
507 void dbus_protocol_shutdown(void) {
511 while (server->clients)
512 client_free(server->clients);
515 dbus_connection_disconnect(server->bus);
516 dbus_connection_unref(server->bus);