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;
112 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
115 assert(at == DBUS_TYPE_ARRAY);
117 if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
118 fprintf(stderr, "Error parsing service resolving message\n");
122 dbus_message_iter_recurse(&sub, &sub2);
125 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
127 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
129 dbus_message_iter_next(&sub);
132 dbus_message_iter_next(&iter);
134 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) {
135 fprintf(stderr, "Failed to parse resolver event.\n");
139 dbus_message_iter_get_basic(&iter, &flags);
146 avahi_address_parse(address, (AvahiProtocol) aprotocol, &a);
148 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, address ? &a : NULL, port, strlst, (AvahiLookupResultFlags) flags, r->userdata);
150 avahi_string_list_free(strlst);
154 case AVAHI_RESOLVER_FAILURE: {
157 if (!dbus_message_get_args(
159 DBUS_TYPE_STRING, &etxt,
160 DBUS_TYPE_INVALID) ||
161 dbus_error_is_set (&error)) {
162 fprintf(stderr, "Failed to parse resolver event.\n");
166 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt));
167 r->callback(r, r->interface, r->protocol, event, r->name, r->type, r->domain, NULL, NULL, 0, NULL, 0, r->userdata);
172 return DBUS_HANDLER_RESULT_HANDLED;
176 dbus_error_free (&error);
177 avahi_string_list_free(strlst);
178 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
181 AvahiServiceResolver * avahi_service_resolver_new(
183 AvahiIfIndex interface,
184 AvahiProtocol protocol,
188 AvahiProtocol aprotocol,
189 AvahiLookupFlags flags,
190 AvahiServiceResolverCallback callback,
194 AvahiServiceResolver *r = NULL;
195 DBusMessage *message = NULL, *reply = NULL;
196 int32_t i_interface, i_protocol, i_aprotocol;
209 dbus_error_init (&error);
211 if (!avahi_client_is_connected(client)) {
212 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
216 if (!(r = avahi_new(AvahiServiceResolver, 1))) {
217 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
222 r->callback = callback;
223 r->userdata = userdata;
225 r->name = r->type = r->domain = NULL;
226 r->interface = interface;
227 r->protocol = protocol;
229 AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
232 if (!(r->name = avahi_strdup(name))) {
233 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
237 if (!(r->type = avahi_strdup(type))) {
238 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
242 if (domain && domain[0])
243 if (!(r->domain = avahi_strdup(domain))) {
244 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
249 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ServiceResolverNew"))) {
250 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
254 i_interface = (int32_t) interface;
255 i_protocol = (int32_t) protocol;
256 i_aprotocol = (int32_t) aprotocol;
257 u_flags = (uint32_t) flags;
259 if (!(dbus_message_append_args(
261 DBUS_TYPE_INT32, &i_interface,
262 DBUS_TYPE_INT32, &i_protocol,
263 DBUS_TYPE_STRING, &name,
264 DBUS_TYPE_STRING, &type,
265 DBUS_TYPE_STRING, &domain,
266 DBUS_TYPE_INT32, &i_aprotocol,
267 DBUS_TYPE_UINT32, &u_flags,
268 DBUS_TYPE_INVALID))) {
269 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
273 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
274 dbus_error_is_set(&error)) {
275 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
279 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
280 dbus_error_is_set(&error) ||
282 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
286 if (!(r->path = avahi_strdup(path))) {
288 /* FIXME: We don't remove the object on the server side */
290 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
295 dbus_message_unref(message);
296 dbus_message_unref(reply);
302 if (dbus_error_is_set(&error)) {
303 avahi_client_set_dbus_error(client, &error);
304 dbus_error_free(&error);
308 avahi_service_resolver_free(r);
311 dbus_message_unref(message);
314 dbus_message_unref(reply);
320 AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) {
326 int avahi_service_resolver_free(AvahiServiceResolver *r) {
333 if (r->path && avahi_client_is_connected(client))
334 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Free");
336 AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
341 avahi_free(r->domain);
347 /* AvahiHostNameResolver implementation */
349 DBusHandlerResult avahi_host_name_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
350 AvahiHostNameResolver *r = NULL;
357 dbus_error_init (&error);
359 if (!(path = dbus_message_get_path(message)))
362 for (r = client->host_name_resolvers; r; r = r->host_name_resolvers_next)
363 if (strcmp (r->path, path) == 0)
370 case AVAHI_RESOLVER_FOUND: {
371 int32_t interface, protocol, aprotocol;
373 char *name, *address;
376 if (!dbus_message_get_args(
378 DBUS_TYPE_INT32, &interface,
379 DBUS_TYPE_INT32, &protocol,
380 DBUS_TYPE_STRING, &name,
381 DBUS_TYPE_INT32, &aprotocol,
382 DBUS_TYPE_STRING, &address,
383 DBUS_TYPE_UINT32, &flags,
384 DBUS_TYPE_INVALID) ||
385 dbus_error_is_set (&error)) {
386 fprintf(stderr, "Failed to parse resolver event.\n");
391 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
392 fprintf(stderr, "Failed to parse address\n");
396 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, &a, (AvahiLookupResultFlags) flags, r->userdata);
400 case AVAHI_RESOLVER_FAILURE: {
403 if (!dbus_message_get_args(
405 DBUS_TYPE_STRING, &etxt,
406 DBUS_TYPE_INVALID) ||
407 dbus_error_is_set (&error)) {
408 fprintf(stderr, "Failed to parse resolver event.\n");
412 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt));
413 r->callback(r, r->interface, r->protocol, event, r->host_name, NULL, 0, r->userdata);
418 return DBUS_HANDLER_RESULT_HANDLED;
421 dbus_error_free (&error);
422 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
426 AvahiHostNameResolver * avahi_host_name_resolver_new(
428 AvahiIfIndex interface,
429 AvahiProtocol protocol,
431 AvahiProtocol aprotocol,
432 AvahiLookupFlags flags,
433 AvahiHostNameResolverCallback callback,
437 AvahiHostNameResolver *r = NULL;
438 DBusMessage *message = NULL, *reply = NULL;
439 int32_t i_interface, i_protocol, i_aprotocol;
446 dbus_error_init (&error);
448 if (!avahi_client_is_connected(client)) {
449 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
453 if (!(r = avahi_new(AvahiHostNameResolver, 1))) {
454 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
459 r->callback = callback;
460 r->userdata = userdata;
462 r->interface = interface;
463 r->protocol = protocol;
466 AVAHI_LLIST_PREPEND(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
468 if (!(r->host_name = avahi_strdup(name))) {
469 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
473 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "HostNameResolverNew"))) {
474 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
478 i_interface = (int32_t) interface;
479 i_protocol = (int32_t) protocol;
480 i_aprotocol = (int32_t) aprotocol;
481 u_flags = (uint32_t) flags;
483 if (!(dbus_message_append_args(
485 DBUS_TYPE_INT32, &i_interface,
486 DBUS_TYPE_INT32, &i_protocol,
487 DBUS_TYPE_STRING, &name,
488 DBUS_TYPE_INT32, &i_aprotocol,
489 DBUS_TYPE_UINT32, &u_flags,
490 DBUS_TYPE_INVALID))) {
491 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
495 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
496 dbus_error_is_set(&error)) {
497 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
501 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
502 dbus_error_is_set(&error) ||
504 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
508 if (!(r->path = avahi_strdup(path))) {
510 /* FIXME: We don't remove the object on the server side */
512 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
516 dbus_message_unref(message);
517 dbus_message_unref(reply);
523 if (dbus_error_is_set(&error)) {
524 avahi_client_set_dbus_error(client, &error);
525 dbus_error_free(&error);
529 avahi_host_name_resolver_free(r);
532 dbus_message_unref(message);
535 dbus_message_unref(reply);
541 int avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
548 if (r->path && avahi_client_is_connected(client))
549 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Free");
551 AVAHI_LLIST_REMOVE(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
554 avahi_free(r->host_name);
560 AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) {
566 /* AvahiAddressResolver implementation */
568 DBusHandlerResult avahi_address_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
569 AvahiAddressResolver *r = NULL;
576 dbus_error_init (&error);
578 if (!(path = dbus_message_get_path(message)))
581 for (r = client->address_resolvers; r; r = r->address_resolvers_next)
582 if (strcmp (r->path, path) == 0)
589 case AVAHI_RESOLVER_FOUND: {
590 int32_t interface, protocol, aprotocol;
592 char *name, *address;
595 if (!dbus_message_get_args(
597 DBUS_TYPE_INT32, &interface,
598 DBUS_TYPE_INT32, &protocol,
599 DBUS_TYPE_INT32, &aprotocol,
600 DBUS_TYPE_STRING, &address,
601 DBUS_TYPE_STRING, &name,
602 DBUS_TYPE_UINT32, &flags,
603 DBUS_TYPE_INVALID) ||
604 dbus_error_is_set (&error)) {
605 fprintf(stderr, "Failed to parse resolver event.\n");
610 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
611 fprintf(stderr, "Failed to parse address\n");
615 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, &a, name, (AvahiLookupResultFlags) flags, r->userdata);
619 case AVAHI_RESOLVER_FAILURE: {
622 if (!dbus_message_get_args(
624 DBUS_TYPE_STRING, &etxt,
625 DBUS_TYPE_INVALID) ||
626 dbus_error_is_set (&error)) {
627 fprintf(stderr, "Failed to parse resolver event.\n");
631 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt));
632 r->callback(r, r->interface, r->protocol, event, &r->address, NULL, 0, r->userdata);
637 return DBUS_HANDLER_RESULT_HANDLED;
640 dbus_error_free (&error);
641 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
644 AvahiAddressResolver * avahi_address_resolver_new(
646 AvahiIfIndex interface,
647 AvahiProtocol protocol,
648 const AvahiAddress *a,
649 AvahiLookupFlags flags,
650 AvahiAddressResolverCallback callback,
654 AvahiAddressResolver *r = NULL;
655 DBusMessage *message = NULL, *reply = NULL;
656 int32_t i_interface, i_protocol;
659 char addr[AVAHI_ADDRESS_STR_MAX], *address = addr;
664 dbus_error_init (&error);
666 if (!avahi_address_snprint (addr, sizeof(addr), a)) {
667 avahi_client_set_errno(client, AVAHI_ERR_INVALID_ADDRESS);
671 if (!avahi_client_is_connected(client)) {
672 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
676 if (!(r = avahi_new(AvahiAddressResolver, 1))) {
677 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
682 r->callback = callback;
683 r->userdata = userdata;
685 r->interface = interface;
686 r->protocol = protocol;
689 AVAHI_LLIST_PREPEND(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);
691 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "AddressResolverNew"))) {
692 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
696 i_interface = (int32_t) interface;
697 i_protocol = (int32_t) protocol;
698 u_flags = (uint32_t) flags;
700 if (!(dbus_message_append_args(
702 DBUS_TYPE_INT32, &i_interface,
703 DBUS_TYPE_INT32, &i_protocol,
704 DBUS_TYPE_STRING, &address,
705 DBUS_TYPE_UINT32, &u_flags,
706 DBUS_TYPE_INVALID))) {
707 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
711 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
712 dbus_error_is_set(&error)) {
713 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
717 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
718 dbus_error_is_set(&error) ||
720 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
724 if (!(r->path = avahi_strdup(path))) {
726 /* FIXME: We don't remove the object on the server side */
728 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
732 dbus_message_unref(message);
733 dbus_message_unref(reply);
739 if (dbus_error_is_set(&error)) {
740 avahi_client_set_dbus_error(client, &error);
741 dbus_error_free(&error);
745 avahi_address_resolver_free(r);
748 dbus_message_unref(message);
751 dbus_message_unref(reply);
757 AvahiClient* avahi_address_resolver_get_client (AvahiAddressResolver *r) {
763 int avahi_address_resolver_free(AvahiAddressResolver *r) {
770 if (r->path && avahi_client_is_connected(client))
771 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Free");
773 AVAHI_LLIST_REMOVE(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);