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);
143 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
144 fprintf(stderr, "Failed to parse address\n");
151 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, &a, port, strlst, (AvahiLookupResultFlags) flags, r->userdata);
153 avahi_string_list_free(strlst);
157 case AVAHI_RESOLVER_FAILURE: {
160 if (!dbus_message_get_args(
162 DBUS_TYPE_STRING, &etxt,
163 DBUS_TYPE_INVALID) ||
164 dbus_error_is_set (&error)) {
165 fprintf(stderr, "Failed to parse resolver event.\n");
169 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt));
170 r->callback(r, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, r->userdata);
175 return DBUS_HANDLER_RESULT_HANDLED;
179 dbus_error_free (&error);
180 avahi_string_list_free(strlst);
181 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
184 AvahiServiceResolver * avahi_service_resolver_new(
186 AvahiIfIndex interface,
187 AvahiProtocol protocol,
191 AvahiProtocol aprotocol,
192 AvahiLookupFlags flags,
193 AvahiServiceResolverCallback callback,
197 AvahiServiceResolver *r = NULL;
198 DBusMessage *message = NULL, *reply = NULL;
199 int32_t i_interface, i_protocol, i_aprotocol;
212 dbus_error_init (&error);
214 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
215 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
219 if (!(r = avahi_new(AvahiServiceResolver, 1))) {
220 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
225 r->callback = callback;
226 r->userdata = userdata;
229 AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
231 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ServiceResolverNew"))) {
232 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
236 i_interface = (int32_t) interface;
237 i_protocol = (int32_t) protocol;
238 i_aprotocol = (int32_t) aprotocol;
239 u_flags = (uint32_t) flags;
241 if (!(dbus_message_append_args(
243 DBUS_TYPE_INT32, &i_interface,
244 DBUS_TYPE_INT32, &i_protocol,
245 DBUS_TYPE_STRING, &name,
246 DBUS_TYPE_STRING, &type,
247 DBUS_TYPE_STRING, &domain,
248 DBUS_TYPE_INT32, &i_aprotocol,
249 DBUS_TYPE_UINT32, &u_flags,
250 DBUS_TYPE_INVALID))) {
251 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
255 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
256 dbus_error_is_set(&error)) {
257 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
261 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
262 dbus_error_is_set(&error) ||
264 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
268 if (!(r->path = avahi_strdup(path))) {
270 /* FIXME: We don't remove the object on the server side */
272 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
277 dbus_message_unref(message);
278 dbus_message_unref(reply);
284 if (dbus_error_is_set(&error)) {
285 avahi_client_set_dbus_error(client, &error);
286 dbus_error_free(&error);
290 avahi_service_resolver_free(r);
293 dbus_message_unref(message);
296 dbus_message_unref(reply);
302 AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) {
308 int avahi_service_resolver_free(AvahiServiceResolver *r) {
315 if (r->path && client->state != AVAHI_CLIENT_DISCONNECTED)
316 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Free");
318 AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
326 /* AvahiHostNameResolver implementation */
328 DBusHandlerResult avahi_host_name_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
329 AvahiHostNameResolver *r = NULL;
336 dbus_error_init (&error);
338 if (!(path = dbus_message_get_path(message)))
341 for (r = client->host_name_resolvers; r; r = r->host_name_resolvers_next)
342 if (strcmp (r->path, path) == 0)
349 case AVAHI_RESOLVER_FOUND: {
350 int32_t interface, protocol, aprotocol;
352 char *name, *address;
355 if (!dbus_message_get_args(
357 DBUS_TYPE_INT32, &interface,
358 DBUS_TYPE_INT32, &protocol,
359 DBUS_TYPE_STRING, &name,
360 DBUS_TYPE_INT32, &aprotocol,
361 DBUS_TYPE_STRING, &address,
362 DBUS_TYPE_UINT32, &flags,
363 DBUS_TYPE_INVALID) ||
364 dbus_error_is_set (&error)) {
365 fprintf(stderr, "Failed to parse resolver event.\n");
370 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
371 fprintf(stderr, "Failed to parse address\n");
375 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, &a, (AvahiLookupResultFlags) flags, r->userdata);
379 case AVAHI_RESOLVER_FAILURE: {
382 if (!dbus_message_get_args(
384 DBUS_TYPE_STRING, &etxt,
385 DBUS_TYPE_INVALID) ||
386 dbus_error_is_set (&error)) {
387 fprintf(stderr, "Failed to parse resolver event.\n");
391 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt));
392 r->callback(r, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, NULL, 0, r->userdata);
397 return DBUS_HANDLER_RESULT_HANDLED;
400 dbus_error_free (&error);
401 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
405 AvahiHostNameResolver * avahi_host_name_resolver_new(
407 AvahiIfIndex interface,
408 AvahiProtocol protocol,
410 AvahiProtocol aprotocol,
411 AvahiLookupFlags flags,
412 AvahiHostNameResolverCallback callback,
416 AvahiHostNameResolver *r = NULL;
417 DBusMessage *message = NULL, *reply = NULL;
418 int32_t i_interface, i_protocol, i_aprotocol;
425 dbus_error_init (&error);
427 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
428 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
432 if (!(r = avahi_new(AvahiHostNameResolver, 1))) {
433 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
438 r->callback = callback;
439 r->userdata = userdata;
442 AVAHI_LLIST_PREPEND(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
444 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "HostNameResolverNew"))) {
445 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
449 i_interface = (int32_t) interface;
450 i_protocol = (int32_t) protocol;
451 i_aprotocol = (int32_t) aprotocol;
452 u_flags = (uint32_t) flags;
454 if (!(dbus_message_append_args(
456 DBUS_TYPE_INT32, &i_interface,
457 DBUS_TYPE_INT32, &i_protocol,
458 DBUS_TYPE_STRING, &name,
459 DBUS_TYPE_INT32, &i_aprotocol,
460 DBUS_TYPE_UINT32, &u_flags,
461 DBUS_TYPE_INVALID))) {
462 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
466 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
467 dbus_error_is_set(&error)) {
468 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
472 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
473 dbus_error_is_set(&error) ||
475 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
479 if (!(r->path = avahi_strdup(path))) {
481 /* FIXME: We don't remove the object on the server side */
483 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
487 dbus_message_unref(message);
488 dbus_message_unref(reply);
494 if (dbus_error_is_set(&error)) {
495 avahi_client_set_dbus_error(client, &error);
496 dbus_error_free(&error);
500 avahi_host_name_resolver_free(r);
503 dbus_message_unref(message);
506 dbus_message_unref(reply);
512 int avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
519 if (r->path && client->state != AVAHI_CLIENT_DISCONNECTED)
520 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Free");
522 AVAHI_LLIST_REMOVE(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
530 AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) {
536 /* AvahiAddressResolver implementation */
538 DBusHandlerResult avahi_address_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
539 AvahiAddressResolver *r = NULL;
546 dbus_error_init (&error);
548 if (!(path = dbus_message_get_path(message)))
551 for (r = client->address_resolvers; r; r = r->address_resolvers_next)
552 if (strcmp (r->path, path) == 0)
559 case AVAHI_RESOLVER_FOUND: {
560 int32_t interface, protocol, aprotocol;
562 char *name, *address;
565 if (!dbus_message_get_args(
567 DBUS_TYPE_INT32, &interface,
568 DBUS_TYPE_INT32, &protocol,
569 DBUS_TYPE_INT32, &aprotocol,
570 DBUS_TYPE_STRING, &address,
571 DBUS_TYPE_STRING, &name,
572 DBUS_TYPE_UINT32, &flags,
573 DBUS_TYPE_INVALID) ||
574 dbus_error_is_set (&error)) {
575 fprintf(stderr, "Failed to parse resolver event.\n");
580 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
581 fprintf(stderr, "Failed to parse address\n");
585 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, (AvahiProtocol) aprotocol, &a, name, (AvahiLookupResultFlags) flags, r->userdata);
589 case AVAHI_RESOLVER_FAILURE: {
592 if (!dbus_message_get_args(
594 DBUS_TYPE_STRING, &etxt,
595 DBUS_TYPE_INVALID) ||
596 dbus_error_is_set (&error)) {
597 fprintf(stderr, "Failed to parse resolver event.\n");
601 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt));
602 r->callback(r, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, AVAHI_PROTO_UNSPEC, NULL, NULL, 0, r->userdata);
607 return DBUS_HANDLER_RESULT_HANDLED;
610 dbus_error_free (&error);
611 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
614 AvahiAddressResolver * avahi_address_resolver_new_a(
616 AvahiIfIndex interface,
617 AvahiProtocol protocol,
618 const AvahiAddress *a,
619 AvahiLookupFlags flags,
620 AvahiAddressResolverCallback callback,
627 if (!avahi_address_snprint (addr, sizeof (addr), a)) {
628 avahi_client_set_errno(client, AVAHI_ERR_INVALID_ADDRESS);
632 return avahi_address_resolver_new(
633 client, interface, protocol,
638 AvahiAddressResolver * avahi_address_resolver_new(
640 AvahiIfIndex interface,
641 AvahiProtocol protocol,
643 AvahiLookupFlags flags,
644 AvahiAddressResolverCallback callback,
648 AvahiAddressResolver *r = NULL;
649 DBusMessage *message = NULL, *reply = NULL;
650 int32_t i_interface, i_protocol;
656 dbus_error_init (&error);
658 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
659 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
663 if (!(r = avahi_new(AvahiAddressResolver, 1))) {
664 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
669 r->callback = callback;
670 r->userdata = userdata;
673 AVAHI_LLIST_PREPEND(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);
675 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "AddressResolverNew"))) {
676 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
680 i_interface = (int32_t) interface;
681 i_protocol = (int32_t) protocol;
682 u_flags = (uint32_t) flags;
684 if (!(dbus_message_append_args(
686 DBUS_TYPE_INT32, &i_interface,
687 DBUS_TYPE_INT32, &i_protocol,
688 DBUS_TYPE_STRING, &address,
689 DBUS_TYPE_UINT32, &u_flags,
690 DBUS_TYPE_INVALID))) {
691 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
695 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
696 dbus_error_is_set(&error)) {
697 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
701 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
702 dbus_error_is_set(&error) ||
704 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
708 if (!(r->path = avahi_strdup(path))) {
710 /* FIXME: We don't remove the object on the server side */
712 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
716 dbus_message_unref(message);
717 dbus_message_unref(reply);
723 if (dbus_error_is_set(&error)) {
724 avahi_client_set_dbus_error(client, &error);
725 dbus_error_free(&error);
729 avahi_address_resolver_free(r);
732 dbus_message_unref(message);
735 dbus_message_unref(reply);
741 AvahiClient* avahi_address_resolver_get_client (AvahiAddressResolver *r) {
747 int avahi_address_resolver_free(AvahiAddressResolver *r) {
754 if (r->path && client->state != AVAHI_CLIENT_DISCONNECTED)
755 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Free");
757 AVAHI_LLIST_REMOVE(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);