2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <dbus/dbus.h>
30 #include <avahi-client/client.h>
31 #include <avahi-common/dbus.h>
32 #include <avahi-common/llist.h>
33 #include <avahi-common/error.h>
34 #include <avahi-common/malloc.h>
39 /* AvahiServiceResolver implementation */
41 DBusHandlerResult avahi_service_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
42 AvahiServiceResolver *r = NULL;
45 AvahiStringList *strlst = NULL;
50 dbus_error_init (&error);
52 if (!(path = dbus_message_get_path(message)))
55 for (r = client->service_resolvers; r; r = r->service_resolvers_next)
56 if (strcmp (r->path, path) == 0)
63 case AVAHI_RESOLVER_FOUND: {
65 int32_t interface, protocol, aprotocol;
67 char *name, *type, *domain, *host, *address;
69 DBusMessageIter iter, sub;
72 if (!dbus_message_get_args(
74 DBUS_TYPE_INT32, &interface,
75 DBUS_TYPE_INT32, &protocol,
76 DBUS_TYPE_STRING, &name,
77 DBUS_TYPE_STRING, &type,
78 DBUS_TYPE_STRING, &domain,
79 DBUS_TYPE_STRING, &host,
80 DBUS_TYPE_INT32, &aprotocol,
81 DBUS_TYPE_STRING, &address,
82 DBUS_TYPE_UINT16, &port,
84 dbus_error_is_set (&error)) {
86 fprintf(stderr, "Failed to parse resolver event.\n");
90 dbus_message_iter_init(message, &iter);
92 for (j = 0; j < 9; j++)
93 dbus_message_iter_next(&iter);
95 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
96 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) {
97 fprintf(stderr, "Error parsing service resolving message\n");
102 dbus_message_iter_recurse(&iter, &sub);
105 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);
123 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
125 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
127 dbus_message_iter_next(&sub);
130 dbus_message_iter_next(&iter);
132 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) {
133 fprintf(stderr, "Failed to parse resolver event.\n");
137 dbus_message_iter_get_basic(&iter, &flags);
144 avahi_address_parse(address, (AvahiProtocol) aprotocol, &a);
146 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, address ? &a : NULL, port, strlst, (AvahiLookupResultFlags) flags, r->userdata);
148 avahi_string_list_free(strlst);
152 case AVAHI_RESOLVER_FAILURE: {
155 if (!dbus_message_get_args(
157 DBUS_TYPE_STRING, &etxt,
158 DBUS_TYPE_INVALID) ||
159 dbus_error_is_set (&error)) {
160 fprintf(stderr, "Failed to parse resolver event.\n");
164 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt));
165 r->callback(r, r->interface, r->protocol, event, r->name, r->type, r->domain, NULL, NULL, 0, NULL, 0, r->userdata);
170 return DBUS_HANDLER_RESULT_HANDLED;
174 dbus_error_free (&error);
175 avahi_string_list_free(strlst);
176 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
179 AvahiServiceResolver * avahi_service_resolver_new(
181 AvahiIfIndex interface,
182 AvahiProtocol protocol,
186 AvahiProtocol aprotocol,
187 AvahiLookupFlags flags,
188 AvahiServiceResolverCallback callback,
192 AvahiServiceResolver *r = NULL;
193 DBusMessage *message = NULL, *reply = NULL;
194 int32_t i_interface, i_protocol, i_aprotocol;
207 dbus_error_init (&error);
209 if (!avahi_client_is_connected(client)) {
210 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
214 if (!(r = avahi_new(AvahiServiceResolver, 1))) {
215 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
220 r->callback = callback;
221 r->userdata = userdata;
223 r->name = r->type = r->domain = NULL;
224 r->interface = interface;
225 r->protocol = protocol;
227 AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
230 if (!(r->name = avahi_strdup(name))) {
231 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
235 if (!(r->type = avahi_strdup(type))) {
236 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
240 if (domain && domain[0])
241 if (!(r->domain = avahi_strdup(domain))) {
242 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
247 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ServiceResolverNew"))) {
248 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
252 i_interface = (int32_t) interface;
253 i_protocol = (int32_t) protocol;
254 i_aprotocol = (int32_t) aprotocol;
255 u_flags = (uint32_t) flags;
257 if (!(dbus_message_append_args(
259 DBUS_TYPE_INT32, &i_interface,
260 DBUS_TYPE_INT32, &i_protocol,
261 DBUS_TYPE_STRING, &name,
262 DBUS_TYPE_STRING, &type,
263 DBUS_TYPE_STRING, &domain,
264 DBUS_TYPE_INT32, &i_aprotocol,
265 DBUS_TYPE_UINT32, &u_flags,
266 DBUS_TYPE_INVALID))) {
267 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
271 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
272 dbus_error_is_set(&error)) {
273 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
277 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
278 dbus_error_is_set(&error) ||
280 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
284 if (!(r->path = avahi_strdup(path))) {
286 /* FIXME: We don't remove the object on the server side */
288 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
293 dbus_message_unref(message);
294 dbus_message_unref(reply);
300 if (dbus_error_is_set(&error)) {
301 avahi_client_set_dbus_error(client, &error);
302 dbus_error_free(&error);
306 avahi_service_resolver_free(r);
309 dbus_message_unref(message);
312 dbus_message_unref(reply);
318 AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) {
324 int avahi_service_resolver_free(AvahiServiceResolver *r) {
331 if (r->path && avahi_client_is_connected(client))
332 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Free");
334 AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
339 avahi_free(r->domain);
345 /* AvahiHostNameResolver implementation */
347 DBusHandlerResult avahi_host_name_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
348 AvahiHostNameResolver *r = NULL;
355 dbus_error_init (&error);
357 if (!(path = dbus_message_get_path(message)))
360 for (r = client->host_name_resolvers; r; r = r->host_name_resolvers_next)
361 if (strcmp (r->path, path) == 0)
368 case AVAHI_RESOLVER_FOUND: {
369 int32_t interface, protocol, aprotocol;
371 char *name, *address;
374 if (!dbus_message_get_args(
376 DBUS_TYPE_INT32, &interface,
377 DBUS_TYPE_INT32, &protocol,
378 DBUS_TYPE_STRING, &name,
379 DBUS_TYPE_INT32, &aprotocol,
380 DBUS_TYPE_STRING, &address,
381 DBUS_TYPE_UINT32, &flags,
382 DBUS_TYPE_INVALID) ||
383 dbus_error_is_set (&error)) {
384 fprintf(stderr, "Failed to parse resolver event.\n");
389 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
390 fprintf(stderr, "Failed to parse address\n");
394 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, &a, (AvahiLookupResultFlags) flags, r->userdata);
398 case AVAHI_RESOLVER_FAILURE: {
401 if (!dbus_message_get_args(
403 DBUS_TYPE_STRING, &etxt,
404 DBUS_TYPE_INVALID) ||
405 dbus_error_is_set (&error)) {
406 fprintf(stderr, "Failed to parse resolver event.\n");
410 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt));
411 r->callback(r, r->interface, r->protocol, event, r->host_name, NULL, 0, r->userdata);
416 return DBUS_HANDLER_RESULT_HANDLED;
419 dbus_error_free (&error);
420 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
424 AvahiHostNameResolver * avahi_host_name_resolver_new(
426 AvahiIfIndex interface,
427 AvahiProtocol protocol,
429 AvahiProtocol aprotocol,
430 AvahiLookupFlags flags,
431 AvahiHostNameResolverCallback callback,
435 AvahiHostNameResolver *r = NULL;
436 DBusMessage *message = NULL, *reply = NULL;
437 int32_t i_interface, i_protocol, i_aprotocol;
444 dbus_error_init (&error);
446 if (!avahi_client_is_connected(client)) {
447 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
451 if (!(r = avahi_new(AvahiHostNameResolver, 1))) {
452 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
457 r->callback = callback;
458 r->userdata = userdata;
460 r->interface = interface;
461 r->protocol = protocol;
464 AVAHI_LLIST_PREPEND(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
466 if (!(r->host_name = avahi_strdup(name))) {
467 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
471 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "HostNameResolverNew"))) {
472 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
476 i_interface = (int32_t) interface;
477 i_protocol = (int32_t) protocol;
478 i_aprotocol = (int32_t) aprotocol;
479 u_flags = (uint32_t) flags;
481 if (!(dbus_message_append_args(
483 DBUS_TYPE_INT32, &i_interface,
484 DBUS_TYPE_INT32, &i_protocol,
485 DBUS_TYPE_STRING, &name,
486 DBUS_TYPE_INT32, &i_aprotocol,
487 DBUS_TYPE_UINT32, &u_flags,
488 DBUS_TYPE_INVALID))) {
489 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
493 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
494 dbus_error_is_set(&error)) {
495 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
499 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
500 dbus_error_is_set(&error) ||
502 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
506 if (!(r->path = avahi_strdup(path))) {
508 /* FIXME: We don't remove the object on the server side */
510 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
514 dbus_message_unref(message);
515 dbus_message_unref(reply);
521 if (dbus_error_is_set(&error)) {
522 avahi_client_set_dbus_error(client, &error);
523 dbus_error_free(&error);
527 avahi_host_name_resolver_free(r);
530 dbus_message_unref(message);
533 dbus_message_unref(reply);
539 int avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
546 if (r->path && avahi_client_is_connected(client))
547 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Free");
549 AVAHI_LLIST_REMOVE(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
552 avahi_free(r->host_name);
558 AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) {
564 /* AvahiAddressResolver implementation */
566 DBusHandlerResult avahi_address_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
567 AvahiAddressResolver *r = NULL;
574 dbus_error_init (&error);
576 if (!(path = dbus_message_get_path(message)))
579 for (r = client->address_resolvers; r; r = r->address_resolvers_next)
580 if (strcmp (r->path, path) == 0)
587 case AVAHI_RESOLVER_FOUND: {
588 int32_t interface, protocol, aprotocol;
590 char *name, *address;
593 if (!dbus_message_get_args(
595 DBUS_TYPE_INT32, &interface,
596 DBUS_TYPE_INT32, &protocol,
597 DBUS_TYPE_INT32, &aprotocol,
598 DBUS_TYPE_STRING, &address,
599 DBUS_TYPE_STRING, &name,
600 DBUS_TYPE_UINT32, &flags,
601 DBUS_TYPE_INVALID) ||
602 dbus_error_is_set (&error)) {
603 fprintf(stderr, "Failed to parse resolver event.\n");
608 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
609 fprintf(stderr, "Failed to parse address\n");
613 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, &a, name, (AvahiLookupResultFlags) flags, r->userdata);
617 case AVAHI_RESOLVER_FAILURE: {
620 if (!dbus_message_get_args(
622 DBUS_TYPE_STRING, &etxt,
623 DBUS_TYPE_INVALID) ||
624 dbus_error_is_set (&error)) {
625 fprintf(stderr, "Failed to parse resolver event.\n");
629 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(etxt));
630 r->callback(r, r->interface, r->protocol, event, &r->address, NULL, 0, r->userdata);
635 return DBUS_HANDLER_RESULT_HANDLED;
638 dbus_error_free (&error);
639 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
642 AvahiAddressResolver * avahi_address_resolver_new(
644 AvahiIfIndex interface,
645 AvahiProtocol protocol,
646 const AvahiAddress *a,
647 AvahiLookupFlags flags,
648 AvahiAddressResolverCallback callback,
652 AvahiAddressResolver *r = NULL;
653 DBusMessage *message = NULL, *reply = NULL;
654 int32_t i_interface, i_protocol;
657 char addr[AVAHI_ADDRESS_STR_MAX], *address = addr;
662 dbus_error_init (&error);
664 if (!avahi_address_snprint (addr, sizeof(addr), a)) {
665 avahi_client_set_errno(client, AVAHI_ERR_INVALID_ADDRESS);
669 if (!avahi_client_is_connected(client)) {
670 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
674 if (!(r = avahi_new(AvahiAddressResolver, 1))) {
675 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
680 r->callback = callback;
681 r->userdata = userdata;
683 r->interface = interface;
684 r->protocol = protocol;
687 AVAHI_LLIST_PREPEND(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);
689 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "AddressResolverNew"))) {
690 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
694 i_interface = (int32_t) interface;
695 i_protocol = (int32_t) protocol;
696 u_flags = (uint32_t) flags;
698 if (!(dbus_message_append_args(
700 DBUS_TYPE_INT32, &i_interface,
701 DBUS_TYPE_INT32, &i_protocol,
702 DBUS_TYPE_STRING, &address,
703 DBUS_TYPE_UINT32, &u_flags,
704 DBUS_TYPE_INVALID))) {
705 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
709 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
710 dbus_error_is_set(&error)) {
711 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
715 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
716 dbus_error_is_set(&error) ||
718 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
722 if (!(r->path = avahi_strdup(path))) {
724 /* FIXME: We don't remove the object on the server side */
726 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
730 dbus_message_unref(message);
731 dbus_message_unref(reply);
737 if (dbus_error_is_set(&error)) {
738 avahi_client_set_dbus_error(client, &error);
739 dbus_error_free(&error);
743 avahi_address_resolver_free(r);
746 dbus_message_unref(message);
749 dbus_message_unref(reply);
755 AvahiClient* avahi_address_resolver_get_client (AvahiAddressResolver *r) {
761 int avahi_address_resolver_free(AvahiAddressResolver *r) {
768 if (r->path && avahi_client_is_connected(client))
769 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Free");
771 AVAHI_LLIST_REMOVE(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);