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 printf ("cmp: %s, %s\n", n->path, path);
141 if (strcmp (n->path, path) == 0) {
150 dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &interface,
151 DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID);
153 if (dbus_error_is_set (&error))
156 db->callback (db, interface, protocol, event, domain, db->user_data);
158 return DBUS_HANDLER_RESULT_HANDLED;
161 dbus_error_free (&error);
162 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
165 /* AvahiServiceTypeBrowser */
166 AvahiServiceTypeBrowser* avahi_service_type_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *domain, AvahiServiceTypeBrowserCallback callback, void *user_data)
168 AvahiServiceTypeBrowser *tmp = NULL;
169 DBusMessage *message = NULL, *reply;
176 dbus_error_init (&error);
178 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
179 AVAHI_DBUS_PATH_SERVER,
180 AVAHI_DBUS_INTERFACE_SERVER,
181 "ServiceTypeBrowserNew");
183 if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID))
186 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
188 if (dbus_error_is_set (&error) || reply == NULL)
191 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
194 if (dbus_error_is_set (&error) || path == NULL)
197 tmp = malloc (sizeof (AvahiServiceTypeBrowser));
198 tmp->client = client;
199 tmp->callback = callback;
200 tmp->user_data = user_data;
201 tmp->path = strdup (path);
203 AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, service_type_browsers, client->service_type_browsers, tmp);
208 dbus_error_free (&error);
209 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
215 avahi_service_type_browser_free (AvahiServiceTypeBrowser *b)
217 AvahiClient *client = b->client;
218 DBusMessage *message = NULL;
220 if (b == NULL || b->path == NULL)
221 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
223 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
225 AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free");
228 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
230 dbus_connection_send (b->client->bus, message, NULL);
232 AVAHI_LLIST_REMOVE(AvahiServiceTypeBrowser, service_type_browsers, b->client->service_type_browsers, b);
236 return avahi_client_set_errno (client, AVAHI_OK);
240 avahi_service_type_browser_path (AvahiServiceTypeBrowser *b)
246 avahi_service_type_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
248 AvahiServiceTypeBrowser *n, *db = NULL;
252 int interface, protocol;
254 dbus_error_init (&error);
256 path = dbus_message_get_path (message);
261 for (n = client->service_type_browsers; n != NULL; n = n->service_type_browsers_next)
263 printf ("cmp: %s, %s\n", n->path, path);
264 if (strcmp (n->path, path) == 0) {
273 dbus_message_get_args (message, &error,
274 DBUS_TYPE_INT32, &interface,
275 DBUS_TYPE_INT32, &protocol,
276 DBUS_TYPE_STRING, &type,
277 DBUS_TYPE_STRING, &domain,
280 if (dbus_error_is_set (&error))
283 db->callback (db, interface, protocol, event, type, domain, db->user_data);
285 return DBUS_HANDLER_RESULT_HANDLED;
288 dbus_error_free (&error);
289 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
292 /* AvahiServiceBrowser */
294 AvahiServiceBrowser* avahi_service_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, char *type, char *domain, AvahiServiceBrowserCallback callback, void *user_data)
296 AvahiServiceBrowser *tmp = NULL;
297 DBusMessage *message = NULL, *reply;
304 dbus_error_init (&error);
306 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
307 AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew");
309 if (!dbus_message_append_args (message,
310 DBUS_TYPE_INT32, &interface,
311 DBUS_TYPE_INT32, &protocol,
312 DBUS_TYPE_STRING, &type,
313 DBUS_TYPE_STRING, &domain,
317 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
319 if (dbus_error_is_set (&error) || reply == NULL)
322 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
325 if (dbus_error_is_set (&error) || path == NULL)
328 tmp = malloc (sizeof (AvahiServiceBrowser));
329 tmp->client = client;
330 tmp->callback = callback;
331 tmp->user_data = user_data;
332 tmp->path = strdup (path);
334 AVAHI_LLIST_PREPEND(AvahiServiceBrowser, service_browsers, client->service_browsers, tmp);
339 dbus_error_free (&error);
340 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
346 avahi_service_browser_free (AvahiServiceBrowser *b)
348 AvahiClient *client = b->client;
349 DBusMessage *message = NULL;
351 if (b == NULL || b->path == NULL)
352 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
354 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
356 AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free");
359 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
361 dbus_connection_send (b->client->bus, message, NULL);
363 AVAHI_LLIST_REMOVE(AvahiServiceBrowser, service_browsers, b->client->service_browsers, b);
367 return avahi_client_set_errno (client, AVAHI_OK);
371 avahi_service_browser_path (AvahiServiceBrowser *b)
377 avahi_service_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
379 AvahiServiceBrowser *n, *db = NULL;
382 char *name, *type, *domain;
383 int interface, protocol;
385 dbus_error_init (&error);
387 path = dbus_message_get_path (message);
392 for (n = client->service_browsers; n != NULL; n = n->service_browsers_next)
394 printf ("cmp: %s, %s\n", n->path, path);
395 if (strcmp (n->path, path) == 0) {
404 dbus_message_get_args (message, &error,
405 DBUS_TYPE_INT32, &interface,
406 DBUS_TYPE_INT32, &protocol,
407 DBUS_TYPE_STRING, &name,
408 DBUS_TYPE_STRING, &type,
409 DBUS_TYPE_STRING, &domain,
412 if (dbus_error_is_set (&error))
415 db->callback (db, interface, protocol, event, name, type, domain, db->user_data);
417 return DBUS_HANDLER_RESULT_HANDLED;
420 dbus_error_free (&error);
421 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;