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>
34 #define DBUS_API_SUBJECT_TO_CHANGE
35 #include <dbus/dbus.h>
36 #include <dbus/dbus-glib-lowlevel.h>
43 /* AvahiDomainBrowser */
45 AvahiDomainBrowser* avahi_domain_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *domain, AvahiDomainBrowserType btype, AvahiDomainBrowserCallback callback, void *user_data)
47 AvahiDomainBrowser *tmp = NULL;
48 DBusMessage *message = NULL, *reply;
55 dbus_error_init (&error);
57 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
58 AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew");
60 if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INT32, &btype, DBUS_TYPE_INVALID))
63 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
65 if (dbus_error_is_set (&error) || reply == NULL)
68 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
71 if (dbus_error_is_set (&error) || path == NULL)
74 tmp = malloc (sizeof (AvahiDomainBrowser));
76 tmp->callback = callback;
77 tmp->user_data = user_data;
78 tmp->path = strdup (path);
80 AVAHI_LLIST_PREPEND(AvahiDomainBrowser, domain_browsers, client->domain_browsers, tmp);
85 dbus_error_free (&error);
86 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
92 avahi_domain_browser_free (AvahiDomainBrowser *b)
94 AvahiClient *client = b->client;
95 DBusMessage *message = NULL;
97 if (b == NULL || b->path == NULL)
98 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
100 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
102 AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free");
105 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
107 dbus_connection_send (client->bus, message, NULL);
109 AVAHI_LLIST_REMOVE(AvahiDomainBrowser, domain_browsers, client->domain_browsers, b);
113 return avahi_client_set_errno (client, AVAHI_OK);
117 avahi_domain_browser_path (AvahiDomainBrowser *b)
123 avahi_domain_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
125 AvahiDomainBrowser *n, *db = NULL;
129 int interface, protocol;
131 dbus_error_init (&error);
133 path = dbus_message_get_path (message);
138 for (n = client->domain_browsers; n != NULL; n = n->domain_browsers_next)
140 if (strcmp (n->path, path) == 0) {
149 dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &interface,
150 DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID);
152 if (dbus_error_is_set (&error))
155 db->callback (db, interface, protocol, event, domain, db->user_data);
157 return DBUS_HANDLER_RESULT_HANDLED;
160 dbus_error_free (&error);
161 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
164 /* AvahiServiceTypeBrowser */
165 AvahiServiceTypeBrowser* avahi_service_type_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *domain, AvahiServiceTypeBrowserCallback callback, void *user_data)
167 AvahiServiceTypeBrowser *tmp = NULL;
168 DBusMessage *message = NULL, *reply;
175 dbus_error_init (&error);
177 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
178 AVAHI_DBUS_PATH_SERVER,
179 AVAHI_DBUS_INTERFACE_SERVER,
180 "ServiceTypeBrowserNew");
182 if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID))
185 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
187 if (dbus_error_is_set (&error) || reply == NULL)
190 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
193 if (dbus_error_is_set (&error) || path == NULL)
196 tmp = malloc (sizeof (AvahiServiceTypeBrowser));
197 tmp->client = client;
198 tmp->callback = callback;
199 tmp->user_data = user_data;
200 tmp->path = strdup (path);
202 AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, service_type_browsers, client->service_type_browsers, tmp);
207 dbus_error_free (&error);
208 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
214 avahi_service_type_browser_free (AvahiServiceTypeBrowser *b)
216 AvahiClient *client = b->client;
217 DBusMessage *message = NULL;
219 if (b == NULL || b->path == NULL)
220 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
222 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
224 AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free");
227 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
229 dbus_connection_send (b->client->bus, message, NULL);
231 AVAHI_LLIST_REMOVE(AvahiServiceTypeBrowser, service_type_browsers, b->client->service_type_browsers, b);
235 return avahi_client_set_errno (client, AVAHI_OK);
239 avahi_service_type_browser_path (AvahiServiceTypeBrowser *b)
245 avahi_service_type_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
247 AvahiServiceTypeBrowser *n, *db = NULL;
251 int interface, protocol;
253 dbus_error_init (&error);
255 path = dbus_message_get_path (message);
260 for (n = client->service_type_browsers; n != NULL; n = n->service_type_browsers_next)
262 if (strcmp (n->path, path) == 0) {
271 dbus_message_get_args (message, &error,
272 DBUS_TYPE_INT32, &interface,
273 DBUS_TYPE_INT32, &protocol,
274 DBUS_TYPE_STRING, &type,
275 DBUS_TYPE_STRING, &domain,
278 if (dbus_error_is_set (&error))
281 db->callback (db, interface, protocol, event, type, domain, db->user_data);
283 return DBUS_HANDLER_RESULT_HANDLED;
286 dbus_error_free (&error);
287 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
290 /* AvahiServiceBrowser */
292 AvahiServiceBrowser* avahi_service_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *type, char *domain, AvahiServiceBrowserCallback callback, void *user_data)
294 AvahiServiceBrowser *tmp = NULL;
295 DBusMessage *message = NULL, *reply;
302 dbus_error_init (&error);
304 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
305 AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew");
307 if (!dbus_message_append_args (message,
308 DBUS_TYPE_INT32, &interface,
309 DBUS_TYPE_INT32, &protocol,
310 DBUS_TYPE_STRING, &type,
311 DBUS_TYPE_STRING, &domain,
315 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
317 if (dbus_error_is_set (&error) || reply == NULL)
320 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
323 if (dbus_error_is_set (&error) || path == NULL)
326 tmp = malloc (sizeof (AvahiServiceBrowser));
327 tmp->client = client;
328 tmp->callback = callback;
329 tmp->user_data = user_data;
330 tmp->path = strdup (path);
332 AVAHI_LLIST_PREPEND(AvahiServiceBrowser, service_browsers, client->service_browsers, tmp);
337 dbus_error_free (&error);
338 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
344 avahi_service_browser_free (AvahiServiceBrowser *b)
346 AvahiClient *client = b->client;
347 DBusMessage *message = NULL;
349 if (b == NULL || b->path == NULL)
350 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
352 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
354 AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free");
357 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
359 dbus_connection_send (b->client->bus, message, NULL);
361 AVAHI_LLIST_REMOVE(AvahiServiceBrowser, service_browsers, b->client->service_browsers, b);
365 return avahi_client_set_errno (client, AVAHI_OK);
369 avahi_service_browser_path (AvahiServiceBrowser *b)
375 avahi_service_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
377 AvahiServiceBrowser *n, *db = NULL;
380 char *name, *type, *domain;
381 int interface, protocol;
383 dbus_error_init (&error);
385 path = dbus_message_get_path (message);
390 for (n = client->service_browsers; n != NULL; n = n->service_browsers_next)
392 if (strcmp (n->path, path) == 0) {
401 dbus_message_get_args (message, &error,
402 DBUS_TYPE_INT32, &interface,
403 DBUS_TYPE_INT32, &protocol,
404 DBUS_TYPE_STRING, &name,
405 DBUS_TYPE_STRING, &type,
406 DBUS_TYPE_STRING, &domain,
409 if (dbus_error_is_set (&error))
412 db->callback (db, interface, protocol, event, name, type, domain, db->user_data);
414 return DBUS_HANDLER_RESULT_HANDLED;
417 dbus_error_free (&error);
418 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;