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
30 #include <dbus/dbus.h>
32 #include <avahi-client/client.h>
33 #include <avahi-common/dbus.h>
34 #include <avahi-common/llist.h>
35 #include <avahi-common/error.h>
36 #include <avahi-common/malloc.h>
41 /* AvahiDomainBrowser */
43 AvahiDomainBrowser* avahi_domain_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiDomainBrowserType btype, AvahiDomainBrowserCallback callback, void *user_data)
45 AvahiDomainBrowser *tmp = NULL;
46 DBusMessage *message = NULL, *reply;
53 dbus_error_init (&error);
55 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
56 AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew");
58 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))
61 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
63 if (dbus_error_is_set (&error) || reply == NULL)
66 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
69 if (dbus_error_is_set (&error) || path == NULL)
72 tmp = avahi_new (AvahiDomainBrowser, 1);
74 tmp->callback = callback;
75 tmp->user_data = user_data;
76 tmp->path = strdup (path);
78 AVAHI_LLIST_PREPEND(AvahiDomainBrowser, domain_browsers, client->domain_browsers, tmp);
83 dbus_error_free (&error);
84 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
90 avahi_domain_browser_free (AvahiDomainBrowser *b)
92 AvahiClient *client = b->client;
93 DBusMessage *message = NULL;
95 if (b == NULL || b->path == NULL)
96 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
98 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
100 AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free");
103 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
105 dbus_connection_send (client->bus, message, NULL);
107 AVAHI_LLIST_REMOVE(AvahiDomainBrowser, domain_browsers, client->domain_browsers, b);
111 return avahi_client_set_errno (client, AVAHI_OK);
115 avahi_domain_browser_path (AvahiDomainBrowser *b)
121 avahi_domain_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
123 AvahiDomainBrowser *n, *db = NULL;
127 int interface, protocol;
129 dbus_error_init (&error);
131 path = dbus_message_get_path (message);
136 for (n = client->domain_browsers; n != NULL; n = n->domain_browsers_next)
138 if (strcmp (n->path, path) == 0) {
147 dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &interface,
148 DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID);
150 if (dbus_error_is_set (&error))
153 db->callback (db, interface, protocol, event, domain, db->user_data);
155 return DBUS_HANDLER_RESULT_HANDLED;
158 dbus_error_free (&error);
159 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
162 /* AvahiServiceTypeBrowser */
163 AvahiServiceTypeBrowser* avahi_service_type_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiServiceTypeBrowserCallback callback, void *user_data)
165 AvahiServiceTypeBrowser *tmp = NULL;
166 DBusMessage *message = NULL, *reply;
173 dbus_error_init (&error);
175 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
176 AVAHI_DBUS_PATH_SERVER,
177 AVAHI_DBUS_INTERFACE_SERVER,
178 "ServiceTypeBrowserNew");
180 if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID))
183 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
185 if (dbus_error_is_set (&error) || reply == NULL)
188 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
191 if (dbus_error_is_set (&error) || path == NULL)
194 tmp = avahi_new(AvahiServiceTypeBrowser, 1);
195 tmp->client = client;
196 tmp->callback = callback;
197 tmp->user_data = user_data;
198 tmp->path = strdup (path);
200 AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, service_type_browsers, client->service_type_browsers, tmp);
205 dbus_error_free (&error);
206 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
212 avahi_service_type_browser_free (AvahiServiceTypeBrowser *b)
214 AvahiClient *client = b->client;
215 DBusMessage *message = NULL;
217 if (b == NULL || b->path == NULL)
218 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
220 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
222 AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free");
225 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
227 dbus_connection_send (b->client->bus, message, NULL);
229 AVAHI_LLIST_REMOVE(AvahiServiceTypeBrowser, service_type_browsers, b->client->service_type_browsers, b);
233 return avahi_client_set_errno (client, AVAHI_OK);
237 avahi_service_type_browser_path (AvahiServiceTypeBrowser *b)
243 avahi_service_type_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
245 AvahiServiceTypeBrowser *n, *db = NULL;
249 int interface, protocol;
251 dbus_error_init (&error);
253 path = dbus_message_get_path (message);
258 for (n = client->service_type_browsers; n != NULL; n = n->service_type_browsers_next)
260 if (strcmp (n->path, path) == 0) {
269 dbus_message_get_args (message, &error,
270 DBUS_TYPE_INT32, &interface,
271 DBUS_TYPE_INT32, &protocol,
272 DBUS_TYPE_STRING, &type,
273 DBUS_TYPE_STRING, &domain,
276 if (dbus_error_is_set (&error))
279 db->callback (db, interface, protocol, event, type, domain, db->user_data);
281 return DBUS_HANDLER_RESULT_HANDLED;
284 dbus_error_free (&error);
285 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
288 /* AvahiServiceBrowser */
290 AvahiServiceBrowser* avahi_service_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *type, const char *domain, AvahiServiceBrowserCallback callback, void *user_data)
292 AvahiServiceBrowser *tmp = NULL;
293 DBusMessage *message = NULL, *reply;
300 dbus_error_init (&error);
302 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
303 AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew");
305 if (!dbus_message_append_args (message,
306 DBUS_TYPE_INT32, &interface,
307 DBUS_TYPE_INT32, &protocol,
308 DBUS_TYPE_STRING, &type,
309 DBUS_TYPE_STRING, &domain,
313 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
315 if (dbus_error_is_set (&error) || reply == NULL)
318 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
321 if (dbus_error_is_set (&error) || path == NULL)
324 tmp = avahi_new(AvahiServiceBrowser, 1);
325 tmp->client = client;
326 tmp->callback = callback;
327 tmp->user_data = user_data;
328 tmp->path = strdup (path);
330 AVAHI_LLIST_PREPEND(AvahiServiceBrowser, service_browsers, client->service_browsers, tmp);
335 dbus_error_free (&error);
336 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
342 avahi_service_browser_free (AvahiServiceBrowser *b)
344 AvahiClient *client = b->client;
345 DBusMessage *message = NULL;
347 if (b == NULL || b->path == NULL)
348 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
350 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
352 AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free");
355 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
357 dbus_connection_send (b->client->bus, message, NULL);
359 AVAHI_LLIST_REMOVE(AvahiServiceBrowser, service_browsers, b->client->service_browsers, b);
363 return avahi_client_set_errno (client, AVAHI_OK);
367 avahi_service_browser_path (AvahiServiceBrowser *b)
373 avahi_service_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
375 AvahiServiceBrowser *n, *db = NULL;
378 char *name, *type, *domain;
379 int interface, protocol;
381 dbus_error_init (&error);
383 path = dbus_message_get_path (message);
388 for (n = client->service_browsers; n != NULL; n = n->service_browsers_next)
390 if (strcmp (n->path, path) == 0) {
399 dbus_message_get_args (message, &error,
400 DBUS_TYPE_INT32, &interface,
401 DBUS_TYPE_INT32, &protocol,
402 DBUS_TYPE_STRING, &name,
403 DBUS_TYPE_STRING, &type,
404 DBUS_TYPE_STRING, &domain,
407 if (dbus_error_is_set (&error))
410 db->callback (db, interface, protocol, event, name, type, domain, db->user_data);
412 return DBUS_HANDLER_RESULT_HANDLED;
415 dbus_error_free (&error);
416 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;