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 /* AvahiServiceResolver implementation */
43 DBusHandlerResult avahi_service_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
44 AvahiServiceResolver *r = NULL;
47 AvahiStringList *strlst = NULL;
52 dbus_error_init (&error);
54 if (!(path = dbus_message_get_path(message)))
57 for (r = client->service_resolvers; r; r = r->service_resolvers_next)
58 if (strcmp (r->path, path) == 0)
65 case AVAHI_RESOLVER_FOUND: {
67 int32_t interface, protocol, aprotocol;
69 char *name, *type, *domain, *host, *address;
71 DBusMessageIter iter, sub;
74 if (!dbus_message_get_args(
76 DBUS_TYPE_INT32, &interface,
77 DBUS_TYPE_INT32, &protocol,
78 DBUS_TYPE_STRING, &name,
79 DBUS_TYPE_STRING, &type,
80 DBUS_TYPE_STRING, &domain,
81 DBUS_TYPE_STRING, &host,
82 DBUS_TYPE_INT32, &aprotocol,
83 DBUS_TYPE_STRING, &address,
84 DBUS_TYPE_UINT16, &port,
86 dbus_error_is_set (&error)) {
88 fprintf(stderr, "Failed to parse resolver event.\n");
92 dbus_message_iter_init(message, &iter);
94 for (j = 0; j < 9; j++)
95 dbus_message_iter_next(&iter);
97 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
98 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) {
99 fprintf(stderr, "Error parsing service resolving message\n");
104 dbus_message_iter_recurse(&iter, &sub);
107 DBusMessageIter sub2;
110 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
113 assert(at == DBUS_TYPE_ARRAY);
115 if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
116 fprintf(stderr, "Error parsing service resolving message\n");
120 dbus_message_iter_recurse(&sub, &sub2);
122 if (dbus_message_iter_get_array_len(&sub2) > 0) {
126 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
127 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
130 dbus_message_iter_next(&sub);
133 dbus_message_iter_next(&iter);
135 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) {
136 fprintf(stderr, "Failed to parse resolver event.\n");
140 dbus_message_iter_get_basic(&iter, &flags);
147 avahi_address_parse(address, (AvahiProtocol) aprotocol, &a);
149 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, address ? &a : NULL, port, strlst, (AvahiLookupResultFlags) flags, r->userdata);
151 avahi_string_list_free(strlst);
155 case AVAHI_RESOLVER_FAILURE: {
158 if (!dbus_message_get_args(
160 DBUS_TYPE_STRING, &etxt,
161 DBUS_TYPE_INVALID) ||
162 dbus_error_is_set (&error)) {
163 fprintf(stderr, "Failed to parse resolver event.\n");
167 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt));
168 r->callback(r, r->interface, r->protocol, event, r->name, r->type, r->domain, NULL, NULL, 0, NULL, 0, r->userdata);
173 return DBUS_HANDLER_RESULT_HANDLED;
177 dbus_error_free (&error);
178 avahi_string_list_free(strlst);
179 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
182 AvahiServiceResolver * avahi_service_resolver_new(
184 AvahiIfIndex interface,
185 AvahiProtocol protocol,
189 AvahiProtocol aprotocol,
190 AvahiLookupFlags flags,
191 AvahiServiceResolverCallback callback,
195 AvahiServiceResolver *r = NULL;
196 DBusMessage *message = NULL, *reply = NULL;
197 int32_t i_interface, i_protocol, i_aprotocol;
210 dbus_error_init (&error);
212 if (!avahi_client_is_connected(client)) {
213 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
217 if (!(r = avahi_new(AvahiServiceResolver, 1))) {
218 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
223 r->callback = callback;
224 r->userdata = userdata;
226 r->name = r->type = r->domain = NULL;
227 r->interface = interface;
228 r->protocol = protocol;
230 AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
233 if (!(r->name = avahi_strdup(name))) {
234 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
238 if (!(r->type = avahi_strdup(type))) {
239 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
243 if (domain && domain[0])
244 if (!(r->domain = avahi_strdup(domain))) {
245 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
250 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ServiceResolverNew"))) {
251 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
255 i_interface = (int32_t) interface;
256 i_protocol = (int32_t) protocol;
257 i_aprotocol = (int32_t) aprotocol;
258 u_flags = (uint32_t) flags;
260 if (!(dbus_message_append_args(
262 DBUS_TYPE_INT32, &i_interface,
263 DBUS_TYPE_INT32, &i_protocol,
264 DBUS_TYPE_STRING, &name,
265 DBUS_TYPE_STRING, &type,
266 DBUS_TYPE_STRING, &domain,
267 DBUS_TYPE_INT32, &i_aprotocol,
268 DBUS_TYPE_UINT32, &u_flags,
269 DBUS_TYPE_INVALID))) {
270 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
274 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
275 dbus_error_is_set(&error)) {
276 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
280 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
281 dbus_error_is_set(&error) ||
283 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
287 if (!(r->path = avahi_strdup(path))) {
289 /* FIXME: We don't remove the object on the server side */
291 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
296 dbus_message_unref(message);
297 dbus_message_unref(reply);
303 if (dbus_error_is_set(&error)) {
304 avahi_client_set_dbus_error(client, &error);
305 dbus_error_free(&error);
309 avahi_service_resolver_free(r);
312 dbus_message_unref(message);
315 dbus_message_unref(reply);
321 AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) {
327 int avahi_service_resolver_free(AvahiServiceResolver *r) {
334 if (r->path && avahi_client_is_connected(client))
335 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Free");
337 AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
342 avahi_free(r->domain);
348 /* AvahiHostNameResolver implementation */
350 DBusHandlerResult avahi_host_name_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
351 AvahiHostNameResolver *r = NULL;
358 dbus_error_init (&error);
360 if (!(path = dbus_message_get_path(message)))
363 for (r = client->host_name_resolvers; r; r = r->host_name_resolvers_next)
364 if (strcmp (r->path, path) == 0)
371 case AVAHI_RESOLVER_FOUND: {
372 int32_t interface, protocol, aprotocol;
374 char *name, *address;
377 if (!dbus_message_get_args(
379 DBUS_TYPE_INT32, &interface,
380 DBUS_TYPE_INT32, &protocol,
381 DBUS_TYPE_STRING, &name,
382 DBUS_TYPE_INT32, &aprotocol,
383 DBUS_TYPE_STRING, &address,
384 DBUS_TYPE_UINT32, &flags,
385 DBUS_TYPE_INVALID) ||
386 dbus_error_is_set (&error)) {
387 fprintf(stderr, "Failed to parse resolver event.\n");
392 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
393 fprintf(stderr, "Failed to parse address\n");
397 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, &a, (AvahiLookupResultFlags) flags, r->userdata);
401 case AVAHI_RESOLVER_FAILURE: {
404 if (!dbus_message_get_args(
406 DBUS_TYPE_STRING, &etxt,
407 DBUS_TYPE_INVALID) ||
408 dbus_error_is_set (&error)) {
409 fprintf(stderr, "Failed to parse resolver event.\n");
413 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt));
414 r->callback(r, r->interface, r->protocol, event, r->host_name, NULL, 0, r->userdata);
419 return DBUS_HANDLER_RESULT_HANDLED;
422 dbus_error_free (&error);
423 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
427 AvahiHostNameResolver * avahi_host_name_resolver_new(
429 AvahiIfIndex interface,
430 AvahiProtocol protocol,
432 AvahiProtocol aprotocol,
433 AvahiLookupFlags flags,
434 AvahiHostNameResolverCallback callback,
438 AvahiHostNameResolver *r = NULL;
439 DBusMessage *message = NULL, *reply = NULL;
440 int32_t i_interface, i_protocol, i_aprotocol;
447 dbus_error_init (&error);
449 if (!avahi_client_is_connected(client)) {
450 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
454 if (!(r = avahi_new(AvahiHostNameResolver, 1))) {
455 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
460 r->callback = callback;
461 r->userdata = userdata;
463 r->interface = interface;
464 r->protocol = protocol;
467 AVAHI_LLIST_PREPEND(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
469 if (!(r->host_name = avahi_strdup(name))) {
470 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
474 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "HostNameResolverNew"))) {
475 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
479 i_interface = (int32_t) interface;
480 i_protocol = (int32_t) protocol;
481 i_aprotocol = (int32_t) aprotocol;
482 u_flags = (uint32_t) flags;
484 if (!(dbus_message_append_args(
486 DBUS_TYPE_INT32, &i_interface,
487 DBUS_TYPE_INT32, &i_protocol,
488 DBUS_TYPE_STRING, &name,
489 DBUS_TYPE_INT32, &i_aprotocol,
490 DBUS_TYPE_UINT32, &u_flags,
491 DBUS_TYPE_INVALID))) {
492 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
496 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
497 dbus_error_is_set(&error)) {
498 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
502 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
503 dbus_error_is_set(&error) ||
505 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
509 if (!(r->path = avahi_strdup(path))) {
511 /* FIXME: We don't remove the object on the server side */
513 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
517 dbus_message_unref(message);
518 dbus_message_unref(reply);
524 if (dbus_error_is_set(&error)) {
525 avahi_client_set_dbus_error(client, &error);
526 dbus_error_free(&error);
530 avahi_host_name_resolver_free(r);
533 dbus_message_unref(message);
536 dbus_message_unref(reply);
542 int avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
549 if (r->path && avahi_client_is_connected(client))
550 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Free");
552 AVAHI_LLIST_REMOVE(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
555 avahi_free(r->host_name);
561 AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) {
567 /* AvahiAddressResolver implementation */
569 DBusHandlerResult avahi_address_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
570 AvahiAddressResolver *r = NULL;
577 dbus_error_init (&error);
579 if (!(path = dbus_message_get_path(message)))
582 for (r = client->address_resolvers; r; r = r->address_resolvers_next)
583 if (strcmp (r->path, path) == 0)
590 case AVAHI_RESOLVER_FOUND: {
591 int32_t interface, protocol, aprotocol;
593 char *name, *address;
596 if (!dbus_message_get_args(
598 DBUS_TYPE_INT32, &interface,
599 DBUS_TYPE_INT32, &protocol,
600 DBUS_TYPE_INT32, &aprotocol,
601 DBUS_TYPE_STRING, &address,
602 DBUS_TYPE_STRING, &name,
603 DBUS_TYPE_UINT32, &flags,
604 DBUS_TYPE_INVALID) ||
605 dbus_error_is_set (&error)) {
606 fprintf(stderr, "Failed to parse resolver event.\n");
611 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
612 fprintf(stderr, "Failed to parse address\n");
616 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, &a, name, (AvahiLookupResultFlags) flags, r->userdata);
620 case AVAHI_RESOLVER_FAILURE: {
623 if (!dbus_message_get_args(
625 DBUS_TYPE_STRING, &etxt,
626 DBUS_TYPE_INVALID) ||
627 dbus_error_is_set (&error)) {
628 fprintf(stderr, "Failed to parse resolver event.\n");
632 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt));
633 r->callback(r, r->interface, r->protocol, event, &r->address, NULL, 0, r->userdata);
638 return DBUS_HANDLER_RESULT_HANDLED;
641 dbus_error_free (&error);
642 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
645 AvahiAddressResolver * avahi_address_resolver_new(
647 AvahiIfIndex interface,
648 AvahiProtocol protocol,
649 const AvahiAddress *a,
650 AvahiLookupFlags flags,
651 AvahiAddressResolverCallback callback,
655 AvahiAddressResolver *r = NULL;
656 DBusMessage *message = NULL, *reply = NULL;
657 int32_t i_interface, i_protocol;
660 char addr[AVAHI_ADDRESS_STR_MAX], *address = addr;
665 dbus_error_init (&error);
667 if (!avahi_address_snprint (addr, sizeof(addr), a)) {
668 avahi_client_set_errno(client, AVAHI_ERR_INVALID_ADDRESS);
672 if (!avahi_client_is_connected(client)) {
673 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
677 if (!(r = avahi_new(AvahiAddressResolver, 1))) {
678 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
683 r->callback = callback;
684 r->userdata = userdata;
686 r->interface = interface;
687 r->protocol = protocol;
690 AVAHI_LLIST_PREPEND(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);
692 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "AddressResolverNew"))) {
693 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
697 i_interface = (int32_t) interface;
698 i_protocol = (int32_t) protocol;
699 u_flags = (uint32_t) flags;
701 if (!(dbus_message_append_args(
703 DBUS_TYPE_INT32, &i_interface,
704 DBUS_TYPE_INT32, &i_protocol,
705 DBUS_TYPE_STRING, &address,
706 DBUS_TYPE_UINT32, &u_flags,
707 DBUS_TYPE_INVALID))) {
708 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
712 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
713 dbus_error_is_set(&error)) {
714 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
718 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
719 dbus_error_is_set(&error) ||
721 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
725 if (!(r->path = avahi_strdup(path))) {
727 /* FIXME: We don't remove the object on the server side */
729 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
733 dbus_message_unref(message);
734 dbus_message_unref(reply);
740 if (dbus_error_is_set(&error)) {
741 avahi_client_set_dbus_error(client, &error);
742 dbus_error_free(&error);
746 avahi_address_resolver_free(r);
749 dbus_message_unref(message);
752 dbus_message_unref(reply);
758 AvahiClient* avahi_address_resolver_get_client (AvahiAddressResolver *r) {
764 int avahi_address_resolver_free(AvahiAddressResolver *r) {
771 if (r->path && avahi_client_is_connected(client))
772 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Free");
774 AVAHI_LLIST_REMOVE(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);