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
26 #include <avahi-client/client.h>
27 #include <avahi-common/dbus.h>
28 #include <avahi-common/llist.h>
33 #define DBUS_API_SUBJECT_TO_CHANGE
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
42 AVAHI_LLIST_HEAD(AvahiEntryGroup, groups);
45 struct _AvahiEntryGroup {
48 AVAHI_LLIST_FIELDS(AvahiEntryGroup, groups);
51 static DBusHandlerResult
52 filter_func (DBusConnection *bus, DBusMessage *message, void *data)
56 g_assert (bus != NULL);
57 g_assert (message != NULL);
59 printf ("dbus: interface=%s, path=%s, member=%s\n",
60 dbus_message_get_interface (message),
61 dbus_message_get_path (message),
62 dbus_message_get_member (message));
64 dbus_error_init (&error);
66 if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
67 gchar *name, *old, *new;
68 dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID);
70 if (dbus_error_is_set (&error)) {
71 fprintf(stderr, "Failed to parse NameOwnerChanged message: %s", error.message);
72 dbus_error_free (&error);
76 if (strcmp (name, AVAHI_DBUS_NAME) == 0) {
77 if (old == NULL && new != NULL) {
78 fprintf(stderr, "Avahi Daemon connected\n");
79 } else if (old != NULL && new == NULL) {
80 fprintf(stderr, "Avahi Daemon disconnected\n");
84 return DBUS_HANDLER_RESULT_HANDLED;
88 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
97 tmp = g_new (AvahiClient, 1);
102 AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, tmp->groups);
104 dbus_error_init (&error);
106 tmp->bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
108 dbus_connection_setup_with_g_main (tmp->bus, NULL);
110 if (dbus_error_is_set (&error)) {
111 fprintf(stderr, "Error getting system d-bus: %s\n", error.message);
112 dbus_error_free (&error);
116 dbus_connection_set_exit_on_disconnect (tmp->bus, FALSE);
118 if (!dbus_connection_add_filter (tmp->bus, filter_func, tmp, NULL))
120 fprintf (stderr, "Failed to add d-bus filter\n");
124 dbus_bus_add_match (tmp->bus,
126 "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
127 "sender='" AVAHI_DBUS_NAME "', "
128 "path='" AVAHI_DBUS_PATH_SERVER "'",
131 if (dbus_error_is_set (&error))
133 fprintf (stderr, "Error adding filter match: %s\n", error.message);
134 dbus_error_free (&error);
139 dbus_bus_add_match (tmp->bus,
141 "interface='" DBUS_INTERFACE_DBUS "', "
142 "sender='" DBUS_SERVICE_DBUS "', "
143 "path='" DBUS_PATH_DBUS "'",
146 if (dbus_error_is_set (&error))
148 fprintf (stderr, "Error adding filter match: %s\n", error.message);
149 dbus_error_free (&error);
162 avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char *param)
164 DBusMessage *message;
169 g_assert (client != NULL);
170 g_assert (method != NULL);
172 dbus_error_init (&error);
174 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method);
178 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, ¶m, DBUS_TYPE_INVALID))
180 fprintf (stderr, "Failed to append string argument to %s message\n", method);
185 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
187 if (dbus_error_is_set (&error))
189 fprintf (stderr, "Error sending %s message: %s\n", method, error.message);
190 dbus_error_free (&error);
191 dbus_message_unref (message);
197 dbus_message_unref (message);
198 fprintf (stderr, "Could not connect to Avahi daemon\n");
202 dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
204 if (dbus_error_is_set (&error))
206 fprintf (stderr, "Failed to parse %s reply: %s\n", method, error.message);
207 dbus_error_free (&error);
217 avahi_client_get_version_string (AvahiClient *client)
219 return avahi_client_get_string_reply_and_block (client, "GetVersionString", NULL);
223 avahi_client_get_domain_name (AvahiClient *client)
225 return avahi_client_get_string_reply_and_block (client, "GetDomainName", NULL);
229 avahi_client_get_host_name (AvahiClient *client)
231 return avahi_client_get_string_reply_and_block (client, "GetHostName", NULL);
235 avahi_client_get_host_name_fqdn (AvahiClient *client)
237 return avahi_client_get_string_reply_and_block (client, "GetHostNameFqdn", NULL);
241 avahi_entry_group_new (AvahiClient *client)
243 AvahiEntryGroup *tmp;
245 tmp = malloc (sizeof (AvahiEntryGroup));
247 tmp->parent = client;
249 AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, tmp);