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* avahi_domain_browser_new(
43 AvahiIfIndex interface,
44 AvahiProtocol protocol,
46 AvahiDomainBrowserType btype,
47 AvahiDomainBrowserCallback callback,
50 AvahiDomainBrowser *db = NULL;
51 DBusMessage *message = NULL, *reply = NULL;
54 int32_t i_interface, i_protocol, bt;
59 dbus_error_init (&error);
61 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
62 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
69 if (!(db = avahi_new (AvahiDomainBrowser, 1))) {
70 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
75 db->callback = callback;
76 db->userdata = userdata;
79 AVAHI_LLIST_PREPEND(AvahiDomainBrowser, domain_browsers, client->domain_browsers, db);
81 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "DomainBrowserNew"))) {
82 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
86 i_interface = interface;
87 i_protocol = protocol;
90 if (!(dbus_message_append_args(
92 DBUS_TYPE_INT32, &i_interface,
93 DBUS_TYPE_INT32, &i_protocol,
94 DBUS_TYPE_STRING, &domain,
96 DBUS_TYPE_INVALID))) {
97 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
101 if (!(reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error)) ||
102 dbus_error_is_set(&error)) {
103 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
107 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
108 dbus_error_is_set(&error) ||
110 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
114 if (!(db->path = avahi_strdup(path))) {
116 /* FIXME: We don't remove the object on the server side */
118 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
122 dbus_message_unref(message);
123 dbus_message_unref(reply);
129 if (dbus_error_is_set(&error)) {
130 avahi_client_set_dbus_error(client, &error);
131 dbus_error_free(&error);
135 avahi_domain_browser_free(db);
138 dbus_message_unref(message);
141 dbus_message_unref(reply);
146 AvahiClient* avahi_domain_browser_get_client (AvahiDomainBrowser *b)
152 int avahi_domain_browser_free (AvahiDomainBrowser *b) {
159 if (b->path && client->state != AVAHI_CLIENT_DISCONNECTED)
160 r = avahi_client_simple_method_call(client, b->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "Free");
162 AVAHI_LLIST_REMOVE(AvahiDomainBrowser, domain_browsers, client->domain_browsers, b);
170 DBusHandlerResult avahi_domain_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message) {
171 AvahiDomainBrowser *db = NULL;
175 int32_t interface, protocol;
180 dbus_error_init (&error);
182 if (!(path = dbus_message_get_path(message)))
185 for (db = client->domain_browsers; db; db = db->domain_browsers_next)
186 if (strcmp (db->path, path) == 0)
192 if (!dbus_message_get_args(
194 DBUS_TYPE_INT32, &interface,
195 DBUS_TYPE_INT32, &protocol,
196 DBUS_TYPE_STRING, &domain,
197 DBUS_TYPE_INVALID) ||
198 dbus_error_is_set (&error)) {
199 fprintf(stderr, "Failed to parse browser event.\n");
203 db->callback(db, (AvahiIfIndex) interface, (AvahiProtocol) protocol, event, domain, db->userdata);
205 return DBUS_HANDLER_RESULT_HANDLED;
208 dbus_error_free (&error);
209 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
212 /* AvahiServiceTypeBrowser */
213 AvahiServiceTypeBrowser* avahi_service_type_browser_new(
215 AvahiIfIndex interface,
216 AvahiProtocol protocol,
218 AvahiServiceTypeBrowserCallback callback,
221 AvahiServiceTypeBrowser *b = NULL;
222 DBusMessage *message = NULL, *reply = NULL;
225 int32_t i_interface, i_protocol;
230 dbus_error_init(&error);
232 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
233 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
240 if (!(b = avahi_new(AvahiServiceTypeBrowser, 1))) {
241 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
246 b->callback = callback;
247 b->userdata = userdata;
250 AVAHI_LLIST_PREPEND(AvahiServiceTypeBrowser, service_type_browsers, client->service_type_browsers, b);
252 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ServiceTypeBrowserNew"))) {
253 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
257 i_interface = interface;
258 i_protocol = protocol;
260 if (!dbus_message_append_args(
262 DBUS_TYPE_INT32, &interface,
263 DBUS_TYPE_INT32, &protocol,
264 DBUS_TYPE_STRING, &domain,
265 DBUS_TYPE_INVALID)) {
266 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
270 if (!(reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error)) ||
271 dbus_error_is_set(&error)) {
272 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
276 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
277 dbus_error_is_set(&error) ||
279 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
283 if (!(b->path = avahi_strdup(path))) {
285 /* FIXME: We don't remove the object on the server side */
287 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
291 dbus_message_unref(message);
292 dbus_message_unref(reply);
298 if (dbus_error_is_set(&error)) {
299 avahi_client_set_dbus_error(client, &error);
300 dbus_error_free(&error);
304 avahi_service_type_browser_free(b);
307 dbus_message_unref(message);
310 dbus_message_unref(reply);
315 AvahiClient* avahi_service_type_browser_get_client (AvahiServiceTypeBrowser *b)
321 int avahi_service_type_browser_free (AvahiServiceTypeBrowser *b) {
328 if (b->path && client->state != AVAHI_CLIENT_DISCONNECTED)
329 r = avahi_client_simple_method_call(client, b->path, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "Free");
331 AVAHI_LLIST_REMOVE(AvahiServiceTypeBrowser, service_type_browsers, b->client->service_type_browsers, b);
338 DBusHandlerResult avahi_service_type_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message) {
339 AvahiServiceTypeBrowser *b = NULL;
343 int32_t interface, protocol;
348 dbus_error_init (&error);
350 if (!(path = dbus_message_get_path(message)))
353 for (b = client->service_type_browsers; b; b = b->service_type_browsers_next)
354 if (strcmp (b->path, path) == 0)
360 if (!dbus_message_get_args(
362 DBUS_TYPE_INT32, &interface,
363 DBUS_TYPE_INT32, &protocol,
364 DBUS_TYPE_STRING, &type,
365 DBUS_TYPE_STRING, &domain,
366 DBUS_TYPE_INVALID) ||
367 dbus_error_is_set(&error)) {
368 fprintf(stderr, "Failed to parse browser event.\n");
372 b->callback(b, (AvahiIfIndex) interface, (AvahiProtocol) protocol, event, type, domain, b->userdata);
374 return DBUS_HANDLER_RESULT_HANDLED;
377 dbus_error_free (&error);
378 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
381 /* AvahiServiceBrowser */
383 AvahiServiceBrowser* avahi_service_browser_new(
385 AvahiIfIndex interface,
386 AvahiProtocol protocol,
389 AvahiServiceBrowserCallback callback,
392 AvahiServiceBrowser *b = NULL;
393 DBusMessage *message = NULL, *reply = NULL;
396 int32_t i_protocol, i_interface;
402 dbus_error_init(&error);
404 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
405 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
412 if (!(b = avahi_new(AvahiServiceBrowser, 1))) {
413 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
418 b->callback = callback;
419 b->userdata = userdata;
422 AVAHI_LLIST_PREPEND(AvahiServiceBrowser, service_browsers, client->service_browsers, b);
424 if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ServiceBrowserNew"))) {
425 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
429 i_interface = interface;
430 i_protocol = protocol;
433 if (!dbus_message_append_args(
435 DBUS_TYPE_INT32, &i_interface,
436 DBUS_TYPE_INT32, &i_protocol,
437 DBUS_TYPE_STRING, &type,
438 DBUS_TYPE_STRING, &domain,
439 DBUS_TYPE_INVALID)) {
440 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
444 if (!(reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error)) ||
445 dbus_error_is_set(&error)) {
446 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
450 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
451 dbus_error_is_set(&error) ||
453 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
457 if (!(b->path = avahi_strdup(path))) {
459 /* FIXME: We don't remove the object on the server side */
461 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
465 dbus_message_unref(message);
466 dbus_message_unref(reply);
471 if (dbus_error_is_set(&error)) {
472 avahi_client_set_dbus_error(client, &error);
473 dbus_error_free(&error);
477 avahi_service_browser_free(b);
480 dbus_message_unref(message);
483 dbus_message_unref(reply);
488 AvahiClient* avahi_service_browser_get_client (AvahiServiceBrowser *b)
494 int avahi_service_browser_free (AvahiServiceBrowser *b) {
501 if (b->path && client->state != AVAHI_CLIENT_DISCONNECTED)
502 r = avahi_client_simple_method_call(client, b->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, "Free");
504 AVAHI_LLIST_REMOVE(AvahiServiceBrowser, service_browsers, b->client->service_browsers, b);
512 DBusHandlerResult avahi_service_browser_event (AvahiClient *client, AvahiBrowserEvent event, DBusMessage *message) {
513 AvahiServiceBrowser *b = NULL;
516 char *name, *type, *domain;
517 int32_t interface, protocol;
519 dbus_error_init (&error);
521 if (!(path = dbus_message_get_path(message)))
524 for (b = client->service_browsers; b; b = b->service_browsers_next)
525 if (strcmp (b->path, path) == 0)
531 if (!dbus_message_get_args (
533 DBUS_TYPE_INT32, &interface,
534 DBUS_TYPE_INT32, &protocol,
535 DBUS_TYPE_STRING, &name,
536 DBUS_TYPE_STRING, &type,
537 DBUS_TYPE_STRING, &domain,
538 DBUS_TYPE_INVALID) ||
539 dbus_error_is_set(&error)) {
540 fprintf(stderr, "Failed to parse browser event.\n");
544 b->callback(b, (AvahiIfIndex) interface, (AvahiProtocol) protocol, event, name, type, domain, b->userdata);
546 return DBUS_HANDLER_RESULT_HANDLED;
549 dbus_error_free (&error);
550 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;