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)
64 if (event == AVAHI_RESOLVER_FOUND) {
66 int32_t interface, protocol, aprotocol;
68 char *name, *type, *domain, *host, *address;
70 DBusMessageIter iter, sub;
73 if (!dbus_message_get_args(
75 DBUS_TYPE_INT32, &interface,
76 DBUS_TYPE_INT32, &protocol,
77 DBUS_TYPE_STRING, &name,
78 DBUS_TYPE_STRING, &type,
79 DBUS_TYPE_STRING, &domain,
80 DBUS_TYPE_STRING, &host,
81 DBUS_TYPE_INT32, &aprotocol,
82 DBUS_TYPE_STRING, &address,
83 DBUS_TYPE_UINT16, &port,
85 dbus_error_is_set (&error)) {
87 fprintf(stderr, "Failed to parse resolver event.\n");
91 dbus_message_iter_init(message, &iter);
93 for (j = 0; j < 9; j++)
94 dbus_message_iter_next(&iter);
96 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
97 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) {
98 fprintf(stderr, "Error parsing service resolving message\n");
103 dbus_message_iter_recurse(&iter, &sub);
106 DBusMessageIter sub2;
109 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
112 assert(at == DBUS_TYPE_ARRAY);
114 if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
115 fprintf(stderr, "Error parsing service resolving message\n");
119 dbus_message_iter_recurse(&sub, &sub2);
121 if (dbus_message_iter_get_array_len(&sub2) > 0) {
125 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
126 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);
142 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
143 fprintf(stderr, "Failed to parse address\n");
147 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, &a, port, strlst, (AvahiLookupResultFlags) flags, r->userdata);
149 avahi_string_list_free(strlst);
152 r->callback(r, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, r->userdata);
154 return DBUS_HANDLER_RESULT_HANDLED;
158 dbus_error_free (&error);
159 avahi_string_list_free(strlst);
160 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
164 AvahiServiceResolver * avahi_service_resolver_new(
166 AvahiIfIndex interface,
167 AvahiProtocol protocol,
171 AvahiProtocol aprotocol,
172 AvahiLookupFlags flags,
173 AvahiServiceResolverCallback callback,
177 AvahiServiceResolver *r;
178 DBusMessage *message = NULL, *reply = NULL;
179 int32_t i_interface, i_protocol, i_aprotocol;
192 dbus_error_init (&error);
194 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
195 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
199 if (!(r = avahi_new(AvahiServiceResolver, 1))) {
200 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
205 r->callback = callback;
206 r->userdata = userdata;
209 AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
211 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ServiceResolverNew"))) {
212 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
216 i_interface = (int32_t) interface;
217 i_protocol = (int32_t) protocol;
218 i_aprotocol = (int32_t) aprotocol;
219 u_flags = (uint32_t) flags;
221 if (!(dbus_message_append_args(
223 DBUS_TYPE_INT32, &i_interface,
224 DBUS_TYPE_INT32, &i_protocol,
225 DBUS_TYPE_STRING, &name,
226 DBUS_TYPE_STRING, &type,
227 DBUS_TYPE_STRING, &domain,
228 DBUS_TYPE_INT32, &i_aprotocol,
229 DBUS_TYPE_UINT32, &u_flags,
230 DBUS_TYPE_INVALID))) {
231 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
235 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
236 dbus_error_is_set(&error)) {
237 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
241 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
242 dbus_error_is_set(&error) ||
244 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
248 if (!(r->path = avahi_strdup(path))) {
250 /* FIXME: We don't remove the object on the server side */
252 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
257 dbus_message_unref(message);
258 dbus_message_unref(reply);
264 if (dbus_error_is_set(&error)) {
265 avahi_client_set_dbus_error(client, &error);
266 dbus_error_free(&error);
270 avahi_service_resolver_free(r);
273 dbus_message_unref(message);
276 dbus_message_unref(reply);
282 AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) {
288 int avahi_service_resolver_free(AvahiServiceResolver *r) {
295 if (r->path && client->state != AVAHI_CLIENT_DISCONNECTED)
296 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Free");
298 AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
306 /* AvahiHostNameResolver implementation */
308 DBusHandlerResult avahi_host_name_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
309 AvahiHostNameResolver *r = NULL;
316 dbus_error_init (&error);
318 if (!(path = dbus_message_get_path(message)))
321 for (r = client->host_name_resolvers; r; r = r->host_name_resolvers_next)
322 if (strcmp (r->path, path) == 0)
328 if (event == AVAHI_RESOLVER_FOUND) {
329 int32_t interface, protocol, aprotocol;
331 char *name, *address;
334 if (!dbus_message_get_args(
336 DBUS_TYPE_INT32, &interface,
337 DBUS_TYPE_INT32, &protocol,
338 DBUS_TYPE_STRING, &name,
339 DBUS_TYPE_INT32, &aprotocol,
340 DBUS_TYPE_STRING, &address,
341 DBUS_TYPE_UINT32, &flags,
342 DBUS_TYPE_INVALID) ||
343 dbus_error_is_set (&error)) {
344 fprintf(stderr, "Failed to parse resolver event.\n");
349 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
350 fprintf(stderr, "Failed to parse address\n");
354 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, &a, (AvahiLookupResultFlags) flags, r->userdata);
357 r->callback(r, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, NULL, NULL, 0, r->userdata);
359 return DBUS_HANDLER_RESULT_HANDLED;
362 dbus_error_free (&error);
363 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
367 AvahiHostNameResolver * avahi_host_name_resolver_new(
369 AvahiIfIndex interface,
370 AvahiProtocol protocol,
372 AvahiProtocol aprotocol,
373 AvahiLookupFlags flags,
374 AvahiHostNameResolverCallback callback,
378 AvahiHostNameResolver *r;
379 DBusMessage *message = NULL, *reply = NULL;
380 int32_t i_interface, i_protocol, i_aprotocol;
387 dbus_error_init (&error);
389 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
390 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
394 if (!(r = avahi_new(AvahiHostNameResolver, 1))) {
395 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
400 r->callback = callback;
401 r->userdata = userdata;
404 AVAHI_LLIST_PREPEND(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
406 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "HostNameResolverNew"))) {
407 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
411 i_interface = (int32_t) interface;
412 i_protocol = (int32_t) protocol;
413 i_aprotocol = (int32_t) aprotocol;
414 u_flags = (uint32_t) flags;
416 if (!(dbus_message_append_args(
418 DBUS_TYPE_INT32, &i_interface,
419 DBUS_TYPE_INT32, &i_protocol,
420 DBUS_TYPE_STRING, &name,
421 DBUS_TYPE_INT32, &i_aprotocol,
422 DBUS_TYPE_UINT32, &u_flags,
423 DBUS_TYPE_INVALID))) {
424 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
428 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
429 dbus_error_is_set(&error)) {
430 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
434 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
435 dbus_error_is_set(&error) ||
437 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
441 if (!(r->path = avahi_strdup(path))) {
443 /* FIXME: We don't remove the object on the server side */
445 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
449 dbus_message_unref(message);
450 dbus_message_unref(reply);
456 if (dbus_error_is_set(&error)) {
457 avahi_client_set_dbus_error(client, &error);
458 dbus_error_free(&error);
462 avahi_host_name_resolver_free(r);
465 dbus_message_unref(message);
468 dbus_message_unref(reply);
474 int avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
481 if (r->path && client->state != AVAHI_CLIENT_DISCONNECTED)
482 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Free");
484 AVAHI_LLIST_REMOVE(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
492 AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) {
498 /* AvahiAddressResolver implementation */
500 DBusHandlerResult avahi_address_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
501 AvahiAddressResolver *r = NULL;
508 dbus_error_init (&error);
510 if (!(path = dbus_message_get_path(message)))
513 for (r = client->address_resolvers; r; r = r->address_resolvers_next)
514 if (strcmp (r->path, path) == 0)
520 if (event == AVAHI_RESOLVER_FOUND) {
521 int32_t interface, protocol, aprotocol;
523 char *name, *address;
526 if (!dbus_message_get_args(
528 DBUS_TYPE_INT32, &interface,
529 DBUS_TYPE_INT32, &protocol,
530 DBUS_TYPE_INT32, &aprotocol,
531 DBUS_TYPE_STRING, &address,
532 DBUS_TYPE_STRING, &name,
533 DBUS_TYPE_UINT32, &flags,
534 DBUS_TYPE_INVALID) ||
535 dbus_error_is_set (&error)) {
536 fprintf(stderr, "Failed to parse resolver event.\n");
541 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
542 fprintf(stderr, "Failed to parse address\n");
546 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, (AvahiProtocol) aprotocol, &a, name, (AvahiLookupResultFlags) flags, r->userdata);
548 r->callback(r, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, event, AVAHI_PROTO_UNSPEC, NULL, NULL, 0, r->userdata);
550 return DBUS_HANDLER_RESULT_HANDLED;
553 dbus_error_free (&error);
554 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
557 AvahiAddressResolver * avahi_address_resolver_new_a(
559 AvahiIfIndex interface,
560 AvahiProtocol protocol,
561 const AvahiAddress *a,
562 AvahiLookupFlags flags,
563 AvahiAddressResolverCallback callback,
570 if (!avahi_address_snprint (addr, sizeof (addr), a)) {
571 avahi_client_set_errno(client, AVAHI_ERR_INVALID_ADDRESS);
575 return avahi_address_resolver_new(
576 client, interface, protocol,
581 AvahiAddressResolver * avahi_address_resolver_new(
583 AvahiIfIndex interface,
584 AvahiProtocol protocol,
586 AvahiLookupFlags flags,
587 AvahiAddressResolverCallback callback,
591 AvahiAddressResolver *r;
592 DBusMessage *message = NULL, *reply = NULL;
593 int32_t i_interface, i_protocol;
599 dbus_error_init (&error);
601 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
602 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
606 if (!(r = avahi_new(AvahiAddressResolver, 1))) {
607 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
612 r->callback = callback;
613 r->userdata = userdata;
616 AVAHI_LLIST_PREPEND(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);
618 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "AddressResolverNew"))) {
619 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
623 i_interface = (int32_t) interface;
624 i_protocol = (int32_t) protocol;
625 u_flags = (uint32_t) flags;
627 if (!(dbus_message_append_args(
629 DBUS_TYPE_INT32, &i_interface,
630 DBUS_TYPE_INT32, &i_protocol,
631 DBUS_TYPE_STRING, &address,
632 DBUS_TYPE_UINT32, &u_flags,
633 DBUS_TYPE_INVALID))) {
634 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
638 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
639 dbus_error_is_set(&error)) {
640 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
644 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
645 dbus_error_is_set(&error) ||
647 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
651 if (!(r->path = avahi_strdup(path))) {
653 /* FIXME: We don't remove the object on the server side */
655 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
659 dbus_message_unref(message);
660 dbus_message_unref(reply);
666 if (dbus_error_is_set(&error)) {
667 avahi_client_set_dbus_error(client, &error);
668 dbus_error_free(&error);
672 avahi_address_resolver_free(r);
675 dbus_message_unref(message);
678 dbus_message_unref(reply);
684 AvahiClient* avahi_address_resolver_get_client (AvahiAddressResolver *r) {
690 int avahi_address_resolver_free(AvahiAddressResolver *r) {
697 if (r->path && client->state != AVAHI_CLIENT_DISCONNECTED)
698 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Free");
700 AVAHI_LLIST_REMOVE(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);