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>
42 service_pending_call_callback(DBusPendingCall *pending, void *userdata) {
43 AvahiServiceResolver *r = userdata;
44 DBusMessage *message = NULL;
45 AvahiStringList *strlst = NULL;
51 dbus_error_init(&error);
53 if (!(message = dbus_pending_call_steal_reply(pending)))
56 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
59 AvahiProtocol protocol, aprotocol;
60 char *name, *type, *domain, *host, *address;
62 DBusMessageIter iter, sub;
65 if (!dbus_message_get_args(
67 DBUS_TYPE_INT32, &interface,
68 DBUS_TYPE_INT32, &protocol,
69 DBUS_TYPE_STRING, &name,
70 DBUS_TYPE_STRING, &type,
71 DBUS_TYPE_STRING, &domain,
72 DBUS_TYPE_STRING, &host,
73 DBUS_TYPE_INT32, &aprotocol,
74 DBUS_TYPE_STRING, &address,
75 DBUS_TYPE_UINT16, &port,
77 dbus_error_is_set (&error)) {
78 fprintf(stderr, "Failed to parse resolver event.\n");
82 dbus_message_iter_init(message, &iter);
84 for (j = 0; j < 9; j++)
85 dbus_message_iter_next(&iter);
87 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
88 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) {
89 fprintf(stderr, "Error parsing service resolving message");
94 dbus_message_iter_recurse(&iter, &sub);
100 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
103 assert(at == DBUS_TYPE_ARRAY);
105 if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
106 fprintf(stderr, "Error parsing service resolving message");
110 dbus_message_iter_recurse(&sub, &sub2);
112 if (dbus_message_iter_get_array_len(&sub2) > 0) {
116 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
117 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
120 dbus_message_iter_next(&sub);
124 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
125 fprintf(stderr, "Failed to parse address\n");
129 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, &a, port, strlst, r->userdata);
133 assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
135 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
137 r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, NULL, NULL, NULL, 0, NULL, r->userdata);
143 dbus_message_unref(message);
145 avahi_string_list_free(strlst);
147 dbus_error_free (&error);
151 hostname_pending_call_callback(DBusPendingCall *pending, void *userdata) {
152 AvahiHostNameResolver *r = userdata;
153 DBusMessage *message = NULL;
159 dbus_error_init(&error);
161 if (!(message = dbus_pending_call_steal_reply(pending)))
164 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
166 AvahiProtocol protocol, aprotocol;
167 char *name, *address;
170 if (!dbus_message_get_args(
172 DBUS_TYPE_INT32, &interface,
173 DBUS_TYPE_INT32, &protocol,
174 DBUS_TYPE_STRING, &name,
175 DBUS_TYPE_INT32, &aprotocol,
176 DBUS_TYPE_STRING, &address,
177 DBUS_TYPE_INVALID) ||
178 dbus_error_is_set (&error)) {
179 fprintf(stderr, "Failed to parse resolver event.\n");
184 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
185 fprintf(stderr, "Failed to parse address\n");
189 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, &a, r->userdata);
193 assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
195 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
197 r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, r->userdata);
203 dbus_message_unref(message);
205 dbus_error_free (&error);
209 address_pending_call_callback(DBusPendingCall *pending, void *userdata) {
210 AvahiAddressResolver *r = userdata;
211 DBusMessage *message = NULL;
217 dbus_error_init(&error);
219 if (!(message = dbus_pending_call_steal_reply(pending)))
222 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
224 AvahiProtocol protocol, aprotocol;
225 char *name, *address;
228 if (!dbus_message_get_args(
230 DBUS_TYPE_INT32, &interface,
231 DBUS_TYPE_INT32, &protocol,
232 DBUS_TYPE_INT32, &aprotocol,
233 DBUS_TYPE_STRING, &address,
234 DBUS_TYPE_STRING, &name,
235 DBUS_TYPE_INVALID) ||
236 dbus_error_is_set (&error)) {
237 fprintf(stderr, "Failed to parse resolver event.\n");
242 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
243 fprintf(stderr, "Failed to parse address\n");
247 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, (AvahiProtocol) aprotocol, &a, name, r->userdata);
251 assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
253 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
255 r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, (AvahiProtocol) 0, NULL, NULL, r->userdata);
261 dbus_message_unref(message);
263 dbus_error_free (&error);
266 /* AvahiServiceResolver implementation */
267 AvahiServiceResolver * avahi_service_resolver_new(
269 AvahiIfIndex interface,
270 AvahiProtocol protocol,
274 AvahiProtocol aprotocol,
275 AvahiServiceResolverCallback callback,
279 AvahiServiceResolver *r;
280 DBusMessage *message;
281 int32_t i_interface, i_protocol, i_aprotocol;
290 dbus_error_init (&error);
292 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
293 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
297 if (!(r = avahi_new(AvahiServiceResolver, 1))) {
298 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
303 r->callback = callback;
304 r->userdata = userdata;
307 AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
309 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService"))) {
310 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
314 i_interface = interface;
315 i_protocol = protocol;
316 i_aprotocol = aprotocol;
318 if (!(dbus_message_append_args(
320 DBUS_TYPE_INT32, &i_interface,
321 DBUS_TYPE_INT32, &i_protocol,
322 DBUS_TYPE_STRING, &name,
323 DBUS_TYPE_STRING, &type,
324 DBUS_TYPE_STRING, &domain,
325 DBUS_TYPE_INT32, &i_aprotocol,
326 DBUS_TYPE_INVALID))) {
327 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
331 if (!dbus_connection_send_with_reply(client->bus, message, &r->call, -1) ||
332 !dbus_pending_call_set_notify(r->call, service_pending_call_callback, r, NULL)) {
333 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
337 dbus_message_unref(message);
343 if (dbus_error_is_set(&error)) {
344 avahi_client_set_dbus_error(client, &error);
345 dbus_error_free(&error);
349 avahi_service_resolver_free(r);
352 dbus_message_unref(message);
358 AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) {
364 int avahi_service_resolver_free(AvahiServiceResolver *r) {
371 dbus_pending_call_cancel(r->call);
372 dbus_pending_call_unref(r->call);
375 AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
382 int avahi_service_resolver_block(AvahiServiceResolver *r) {
389 dbus_pending_call_block(r->call);
394 /* AvahiHostNameResolver implementation */
396 AvahiHostNameResolver * avahi_host_name_resolver_new(
398 AvahiIfIndex interface,
399 AvahiProtocol protocol,
401 AvahiProtocol aprotocol,
402 AvahiHostNameResolverCallback callback,
406 AvahiHostNameResolver *r;
407 DBusMessage *message;
408 int32_t i_interface, i_protocol, i_aprotocol;
413 dbus_error_init (&error);
415 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
416 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
420 if (!(r = avahi_new(AvahiHostNameResolver, 1))) {
421 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
426 r->callback = callback;
427 r->userdata = userdata;
430 AVAHI_LLIST_PREPEND(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
432 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName"))) {
433 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
437 i_interface = interface;
438 i_protocol = protocol;
439 i_aprotocol = aprotocol;
441 if (!(dbus_message_append_args(
443 DBUS_TYPE_INT32, &i_interface,
444 DBUS_TYPE_INT32, &i_protocol,
445 DBUS_TYPE_STRING, &name,
446 DBUS_TYPE_INT32, &i_aprotocol,
447 DBUS_TYPE_INVALID))) {
448 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
452 if (!dbus_connection_send_with_reply(client->bus, message, &r->call, -1) ||
453 !dbus_pending_call_set_notify(r->call, hostname_pending_call_callback, r, NULL)) {
454 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
458 dbus_message_unref(message);
464 if (dbus_error_is_set(&error)) {
465 avahi_client_set_dbus_error(client, &error);
466 dbus_error_free(&error);
470 avahi_host_name_resolver_free(r);
473 dbus_message_unref(message);
479 int avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
486 dbus_pending_call_cancel(r->call);
487 dbus_pending_call_unref(r->call);
490 AVAHI_LLIST_REMOVE(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
497 AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) {
503 int avahi_host_name_resolver_block(AvahiHostNameResolver *r) {
510 dbus_pending_call_block(r->call);
515 /* AvahiAddressResolver implementation */
517 AvahiAddressResolver * avahi_address_resolver_new_a(
519 AvahiIfIndex interface,
520 const AvahiAddress *a,
521 AvahiAddressResolverCallback callback,
528 avahi_address_snprint (addr, sizeof (addr), a);
530 return avahi_address_resolver_new (client, interface,
535 AvahiAddressResolver * avahi_address_resolver_new(
537 AvahiIfIndex interface,
538 AvahiProtocol protocol,
540 AvahiAddressResolverCallback callback,
544 AvahiAddressResolver *r;
545 DBusMessage *message;
547 AvahiProtocol i_protocol;
551 dbus_error_init (&error);
553 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
554 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
558 if (!(r = avahi_new(AvahiAddressResolver, 1))) {
559 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
564 r->callback = callback;
565 r->userdata = userdata;
568 AVAHI_LLIST_PREPEND(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);
570 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress"))) {
571 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
575 i_interface = interface;
576 i_protocol = protocol;
578 if (!(dbus_message_append_args(
580 DBUS_TYPE_INT32, &i_interface,
581 DBUS_TYPE_INT32, &i_protocol,
582 DBUS_TYPE_STRING, &address,
583 DBUS_TYPE_INVALID))) {
584 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
588 if (!dbus_connection_send_with_reply(client->bus, message, &r->call, -1) ||
589 !dbus_pending_call_set_notify(r->call, address_pending_call_callback, r, NULL)) {
590 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
594 dbus_message_unref(message);
600 if (dbus_error_is_set(&error)) {
601 avahi_client_set_dbus_error(client, &error);
602 dbus_error_free(&error);
606 avahi_address_resolver_free(r);
609 dbus_message_unref(message);
615 AvahiClient* avahi_address_resolver_get_client (AvahiAddressResolver *r) {
621 int avahi_address_resolver_free(AvahiAddressResolver *r) {
628 dbus_pending_call_cancel(r->call);
629 dbus_pending_call_unref(r->call);
632 AVAHI_LLIST_REMOVE(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);
639 int avahi_address_resolver_block(AvahiAddressResolver *r) {
646 dbus_pending_call_block(r->call);