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>
31 #include <dbus/dbus-glib-lowlevel.h>
33 #include <avahi-client/client.h>
34 #include <avahi-common/dbus.h>
35 #include <avahi-common/llist.h>
36 #include <avahi-common/error.h>
37 #include <avahi-common/malloc.h>
42 /* AvahiDomainBrowser */
44 AvahiDomainBrowser* avahi_domain_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiDomainBrowserType btype, AvahiDomainBrowserCallback callback, void *user_data)
46 AvahiDomainBrowser *tmp = NULL;
47 DBusMessage *message = NULL, *reply;
54 dbus_error_init (&error);
56 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
57 AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew");
59 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))
62 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
64 if (dbus_error_is_set (&error) || reply == NULL)
67 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
70 if (dbus_error_is_set (&error) || path == NULL)
73 tmp = avahi_new (AvahiDomainBrowser, 1);
75 tmp->callback = callback;
76 tmp->user_data = user_data;
77 tmp->path = strdup (path);
79 AVAHI_LLIST_PREPEND(AvahiDomainBrowser, domain_browsers, client->domain_browsers, tmp);
84 dbus_error_free (&error);
85 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
91 avahi_domain_browser_free (AvahiDomainBrowser *b)
93 AvahiClient *client = b->client;
94 DBusMessage *message = NULL;
96 if (b == NULL || b->path == NULL)
97 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
99 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
101 AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free");
104 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
106 dbus_connection_send (client->bus, message, NULL);
108 AVAHI_LLIST_REMOVE(AvahiDomainBrowser, domain_browsers, client->domain_browsers, b);
112 return avahi_client_set_errno (client, AVAHI_OK);
116 avahi_domain_browser_path (AvahiDomainBrowser *b)
122 avahi_domain_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
124 AvahiDomainBrowser *n, *db = NULL;
128 int interface, protocol;
130 dbus_error_init (&error);
132 path = dbus_message_get_path (message);
137 for (n = client->domain_browsers; n != NULL; n = n->domain_browsers_next)
139 if (strcmp (n->path, path) == 0) {
148 dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &interface,
149 DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID);
151 if (dbus_error_is_set (&error))
154 db->callback (db, interface, protocol, event, domain, db->user_data);
156 return DBUS_HANDLER_RESULT_HANDLED;
159 dbus_error_free (&error);
160 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
163 /* AvahiServiceTypeBrowser */
164 AvahiServiceTypeBrowser* avahi_service_type_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *domain, AvahiServiceTypeBrowserCallback callback, void *user_data)
166 AvahiServiceTypeBrowser *tmp = NULL;
167 DBusMessage *message = NULL, *reply;
174 dbus_error_init (&error);
176 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
177 AVAHI_DBUS_PATH_SERVER,
178 AVAHI_DBUS_INTERFACE_SERVER,
179 "ServiceTypeBrowserNew");
181 if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol, DBUS_TYPE_STRING, &domain, DBUS_TYPE_INVALID))
184 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
186 if (dbus_error_is_set (&error) || reply == NULL)
189 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
192 if (dbus_error_is_set (&error) || path == NULL)
195 tmp = avahi_new(AvahiServiceTypeBrowser, 1);
196 tmp->client = client;
197 tmp->callback = callback;
198 tmp->user_data = user_data;
199 tmp->path = strdup (path);
201 AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, service_type_browsers, client->service_type_browsers, tmp);
206 dbus_error_free (&error);
207 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
213 avahi_service_type_browser_free (AvahiServiceTypeBrowser *b)
215 AvahiClient *client = b->client;
216 DBusMessage *message = NULL;
218 if (b == NULL || b->path == NULL)
219 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
221 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
223 AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free");
226 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
228 dbus_connection_send (b->client->bus, message, NULL);
230 AVAHI_LLIST_REMOVE(AvahiServiceTypeBrowser, service_type_browsers, b->client->service_type_browsers, b);
234 return avahi_client_set_errno (client, AVAHI_OK);
238 avahi_service_type_browser_path (AvahiServiceTypeBrowser *b)
244 avahi_service_type_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
246 AvahiServiceTypeBrowser *n, *db = NULL;
250 int interface, protocol;
252 dbus_error_init (&error);
254 path = dbus_message_get_path (message);
259 for (n = client->service_type_browsers; n != NULL; n = n->service_type_browsers_next)
261 if (strcmp (n->path, path) == 0) {
270 dbus_message_get_args (message, &error,
271 DBUS_TYPE_INT32, &interface,
272 DBUS_TYPE_INT32, &protocol,
273 DBUS_TYPE_STRING, &type,
274 DBUS_TYPE_STRING, &domain,
277 if (dbus_error_is_set (&error))
280 db->callback (db, interface, protocol, event, type, domain, db->user_data);
282 return DBUS_HANDLER_RESULT_HANDLED;
285 dbus_error_free (&error);
286 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
289 /* AvahiServiceBrowser */
291 AvahiServiceBrowser* avahi_service_browser_new (AvahiClient *client, AvahiIfIndex interface, AvahiProtocol protocol, const char *type, const char *domain, AvahiServiceBrowserCallback callback, void *user_data)
293 AvahiServiceBrowser *tmp = NULL;
294 DBusMessage *message = NULL, *reply;
301 dbus_error_init (&error);
303 message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
304 AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew");
306 if (!dbus_message_append_args (message,
307 DBUS_TYPE_INT32, &interface,
308 DBUS_TYPE_INT32, &protocol,
309 DBUS_TYPE_STRING, &type,
310 DBUS_TYPE_STRING, &domain,
314 reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
316 if (dbus_error_is_set (&error) || reply == NULL)
319 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID))
322 if (dbus_error_is_set (&error) || path == NULL)
325 tmp = avahi_new(AvahiServiceBrowser, 1);
326 tmp->client = client;
327 tmp->callback = callback;
328 tmp->user_data = user_data;
329 tmp->path = strdup (path);
331 AVAHI_LLIST_PREPEND(AvahiServiceBrowser, service_browsers, client->service_browsers, tmp);
336 dbus_error_free (&error);
337 avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
343 avahi_service_browser_free (AvahiServiceBrowser *b)
345 AvahiClient *client = b->client;
346 DBusMessage *message = NULL;
348 if (b == NULL || b->path == NULL)
349 return avahi_client_set_errno (client, AVAHI_ERR_INVALID_OBJECT);
351 message = dbus_message_new_method_call (AVAHI_DBUS_NAME,
353 AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free");
356 return avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
358 dbus_connection_send (b->client->bus, message, NULL);
360 AVAHI_LLIST_REMOVE(AvahiServiceBrowser, service_browsers, b->client->service_browsers, b);
364 return avahi_client_set_errno (client, AVAHI_OK);
368 avahi_service_browser_path (AvahiServiceBrowser *b)
374 avahi_service_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message)
376 AvahiServiceBrowser *n, *db = NULL;
379 char *name, *type, *domain;
380 int interface, protocol;
382 dbus_error_init (&error);
384 path = dbus_message_get_path (message);
389 for (n = client->service_browsers; n != NULL; n = n->service_browsers_next)
391 if (strcmp (n->path, path) == 0) {
400 dbus_message_get_args (message, &error,
401 DBUS_TYPE_INT32, &interface,
402 DBUS_TYPE_INT32, &protocol,
403 DBUS_TYPE_STRING, &name,
404 DBUS_TYPE_STRING, &type,
405 DBUS_TYPE_STRING, &domain,
408 if (dbus_error_is_set (&error))
411 db->callback (db, interface, protocol, event, name, type, domain, db->user_data);
413 return DBUS_HANDLER_RESULT_HANDLED;
416 dbus_error_free (&error);
417 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;