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>
29 #include <avahi-common/error.h>
30 #include <avahi-common/malloc.h>
31 #include <avahi-common/dbus-watch-glue.h>
36 #include <dbus/dbus.h>
44 avahi_client_set_errno (AvahiClient *client, int error)
48 client->error = error;
53 static void avahi_client_set_state (AvahiClient *client, AvahiServerState state) {
56 if (client->state == state)
59 client->state = state;
62 client->callback (client, state, client->userdata);
66 avahi_client_state_request_callback (DBusPendingCall *call, void *data)
68 AvahiClient *client = data;
73 dbus_error_init (&error);
75 reply = dbus_pending_call_steal_reply (call);
77 type = dbus_message_get_type (reply);
79 if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
81 dbus_message_get_args (reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
83 if (dbus_error_is_set (&error))
86 avahi_client_set_state (client, state);
87 } else if (type == DBUS_MESSAGE_TYPE_ERROR) {
88 dbus_set_error_from_message (&error, reply);
91 dbus_pending_call_unref (call);
95 avahi_client_schedule_state_request (AvahiClient *client)
98 DBusPendingCall *pcall;
100 /*** Lennart says that this can't happen this way since it will
101 * never be called if no main loop is used. This call has to
102 * happen synchronously */
104 if (client == NULL) return;
106 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState");
108 dbus_connection_send_with_reply (client->bus, message, &pcall, -1);
110 dbus_pending_call_set_notify (pcall, avahi_client_state_request_callback, client, NULL);
113 static DBusHandlerResult
114 filter_func (DBusConnection *bus, DBusMessage *message, void *data)
116 AvahiClient *client = data;
119 printf ("dbus: interface=%s, path=%s, member=%s\n",
120 dbus_message_get_interface (message),
121 dbus_message_get_path (message),
122 dbus_message_get_member (message));
124 dbus_error_init (&error);
126 if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
127 char *name, *old, *new;
128 dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID);
130 if (dbus_error_is_set (&error)) {
131 dbus_error_free (&error);
135 if (strcmp (name, AVAHI_DBUS_NAME) == 0) {
137 if (old == NULL && new != NULL) {
138 avahi_client_set_state (client, AVAHI_CLIENT_RECONNECTED);
139 } else if (old != NULL && new == NULL) {
140 avahi_client_set_state (client, AVAHI_CLIENT_DISCONNECTED);
141 /* XXX: we really need to expire all entry groups */
144 } else if (dbus_message_is_signal (message, AVAHI_DBUS_NAME, "StateChanged")) {
146 printf ("server statechange\n");
147 } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) {
149 AvahiEntryGroup *n, *group = NULL;
150 path = dbus_message_get_path (message);
152 for (n = client->groups; n != NULL; n = n->groups_next)
154 if (strcmp (n->path, path) == 0)
163 dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
164 if (dbus_error_is_set (&error))
167 avahi_entry_group_state_change (group, state);
169 } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemNew")) {
170 return avahi_domain_browser_event (client, AVAHI_BROWSER_NEW, message);
171 } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemRemove")) {
172 return avahi_domain_browser_event (client, AVAHI_BROWSER_REMOVE, message);
173 } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemNew")) {
174 return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message);
175 } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove")) {
176 return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message);
177 } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemNew")) {
178 return avahi_service_browser_event (client, AVAHI_BROWSER_NEW, message);
179 } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "ItemRemove")) {
180 return avahi_service_browser_event (client, AVAHI_BROWSER_REMOVE, message);
183 return DBUS_HANDLER_RESULT_HANDLED;
186 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
189 static int translate_dbus_error(const DBusError *error) {
192 /*** FIXME! Some more eloquent error translation should happen here */
194 return AVAHI_ERR_DBUS_ERROR;
198 avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *ret_error)
200 AvahiClient *client = NULL;
203 dbus_error_init (&error);
205 if (!(client = avahi_new(AvahiClient, 1))) {
207 *ret_error = AVAHI_ERR_NO_MEMORY;
211 client->poll_api = poll_api;
212 client->error = AVAHI_OK;
213 client->callback = callback;
214 client->userdata = userdata;
215 client->state = AVAHI_SERVER_INVALID;
217 AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, client->groups);
218 AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers);
219 AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);
220 AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers);
222 client->bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
223 if (dbus_error_is_set (&error)) {
225 *ret_error = translate_dbus_error(&error);
229 if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
231 *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
235 if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
237 *ret_error = AVAHI_ERR_NO_MEMORY;
244 "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
245 "sender='" AVAHI_DBUS_NAME "', "
246 "path='" AVAHI_DBUS_PATH_SERVER "'",
249 if (dbus_error_is_set (&error)) {
251 *ret_error = translate_dbus_error(&error);
258 "interface='" DBUS_INTERFACE_DBUS "', "
259 "sender='" DBUS_SERVICE_DBUS "', "
260 "path='" DBUS_PATH_DBUS "'",
263 if (dbus_error_is_set (&error)) {
265 *ret_error = translate_dbus_error(&error);
269 if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error))) {
271 if (dbus_error_is_set (&error)) {
273 *ret_error = translate_dbus_error(&error);
278 *ret_error = AVAHI_ERR_NO_DAEMON;
282 /* This can't happen asynchronously, since it is not guaranteed that a main loop is used */
284 /*client_get_server_state (client);*/
293 dbus_connection_disconnect(client->bus);
294 dbus_connection_unref(client->bus);
300 if (dbus_error_is_set(&error))
301 dbus_error_free(&error);
307 avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param)
309 DBusMessage *message;
314 if (client == NULL || method == NULL) return NULL;
316 dbus_error_init (&error);
318 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method);
322 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, ¶m, DBUS_TYPE_INVALID))
324 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
329 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
331 if (dbus_error_is_set (&error))
333 dbus_error_free (&error);
334 dbus_message_unref (message);
336 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
342 dbus_message_unref (message);
344 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
348 dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
350 if (dbus_error_is_set (&error))
352 dbus_error_free (&error);
354 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
358 new = avahi_strdup (ret);
360 avahi_client_set_errno (client, AVAHI_OK);
365 avahi_client_get_version_string (AvahiClient *client)
367 return avahi_client_get_string_reply_and_block (client, "GetVersionString", NULL);
371 avahi_client_get_domain_name (AvahiClient *client)
373 return avahi_client_get_string_reply_and_block (client, "GetDomainName", NULL);
377 avahi_client_get_host_name (AvahiClient *client)
379 return avahi_client_get_string_reply_and_block (client, "GetHostName", NULL);
383 avahi_client_get_host_name_fqdn (AvahiClient *client)
385 return avahi_client_get_string_reply_and_block (client, "GetHostNameFqdn", NULL);