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) {
67 AvahiProtocol 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)) {
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");
102 dbus_message_iter_recurse(&iter, &sub);
105 DBusMessageIter sub2;
108 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
111 assert(at == DBUS_TYPE_ARRAY);
113 if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
114 fprintf(stderr, "Error parsing service resolving message");
118 dbus_message_iter_recurse(&sub, &sub2);
120 if (dbus_message_iter_get_array_len(&sub2) > 0) {
124 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
125 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
128 dbus_message_iter_next(&sub);
132 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
133 fprintf(stderr, "Failed to parse address\n");
137 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, &a, port, strlst, r->userdata);
139 avahi_string_list_free(strlst);
142 assert(event == AVAHI_RESOLVER_TIMEOUT);
144 r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, NULL, NULL, NULL, 0, NULL, r->userdata);
147 return DBUS_HANDLER_RESULT_HANDLED;
151 dbus_error_free (&error);
152 avahi_string_list_free(strlst);
153 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
157 AvahiServiceResolver * avahi_service_resolver_new(
159 AvahiIfIndex interface,
160 AvahiProtocol protocol,
164 AvahiProtocol aprotocol,
165 AvahiServiceResolverCallback callback,
169 AvahiServiceResolver *r;
170 DBusMessage *message = NULL, *reply = NULL;
171 int32_t i_interface, i_protocol, i_aprotocol;
181 dbus_error_init (&error);
183 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
184 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
188 if (!(r = avahi_new(AvahiServiceResolver, 1))) {
189 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
194 r->callback = callback;
195 r->userdata = userdata;
198 AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
200 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ServiceResolverNew"))) {
201 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
205 i_interface = interface;
206 i_protocol = protocol;
207 i_aprotocol = aprotocol;
209 if (!(dbus_message_append_args(
211 DBUS_TYPE_INT32, &i_interface,
212 DBUS_TYPE_INT32, &i_protocol,
213 DBUS_TYPE_STRING, &name,
214 DBUS_TYPE_STRING, &type,
215 DBUS_TYPE_STRING, &domain,
216 DBUS_TYPE_INT32, &i_aprotocol,
217 DBUS_TYPE_INVALID))) {
218 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
222 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
223 dbus_error_is_set(&error)) {
224 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
228 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
229 dbus_error_is_set(&error) ||
231 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
235 if (!(r->path = avahi_strdup(path))) {
237 /* FIXME: We don't remove the object on the server side */
239 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
244 dbus_message_unref(message);
245 dbus_message_unref(reply);
251 if (dbus_error_is_set(&error)) {
252 avahi_client_set_dbus_error(client, &error);
253 dbus_error_free(&error);
257 avahi_service_resolver_free(r);
260 dbus_message_unref(message);
263 dbus_message_unref(reply);
269 AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) {
275 int avahi_service_resolver_free(AvahiServiceResolver *r) {
282 if (r->path && client->state != AVAHI_CLIENT_DISCONNECTED)
283 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_SERVICE_RESOLVER, "Free");
285 AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
293 /* AvahiHostNameResolver implementation */
295 DBusHandlerResult avahi_host_name_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
296 AvahiHostNameResolver *r = NULL;
303 dbus_error_init (&error);
305 if (!(path = dbus_message_get_path(message)))
308 for (r = client->host_name_resolvers; r; r = r->host_name_resolvers_next)
309 if (strcmp (r->path, path) == 0)
315 if (event == AVAHI_RESOLVER_FOUND) {
317 AvahiProtocol protocol, aprotocol;
318 char *name, *address;
321 if (!dbus_message_get_args(
323 DBUS_TYPE_INT32, &interface,
324 DBUS_TYPE_INT32, &protocol,
325 DBUS_TYPE_STRING, &name,
326 DBUS_TYPE_INT32, &aprotocol,
327 DBUS_TYPE_STRING, &address,
328 DBUS_TYPE_INVALID) ||
329 dbus_error_is_set (&error)) {
330 fprintf(stderr, "Failed to parse resolver event.\n");
335 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
336 fprintf(stderr, "Failed to parse address\n");
340 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, &a, r->userdata);
344 assert(event == AVAHI_RESOLVER_TIMEOUT);
346 r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, r->userdata);
349 return DBUS_HANDLER_RESULT_HANDLED;
352 dbus_error_free (&error);
353 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
357 AvahiHostNameResolver * avahi_host_name_resolver_new(
359 AvahiIfIndex interface,
360 AvahiProtocol protocol,
362 AvahiProtocol aprotocol,
363 AvahiHostNameResolverCallback callback,
367 AvahiHostNameResolver *r;
368 DBusMessage *message = NULL, *reply = NULL;
369 int32_t i_interface, i_protocol, i_aprotocol;
375 dbus_error_init (&error);
377 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
378 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
382 if (!(r = avahi_new(AvahiHostNameResolver, 1))) {
383 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
388 r->callback = callback;
389 r->userdata = userdata;
392 AVAHI_LLIST_PREPEND(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
394 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "HostNameResolverNew"))) {
395 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
399 i_interface = interface;
400 i_protocol = protocol;
401 i_aprotocol = aprotocol;
403 if (!(dbus_message_append_args(
405 DBUS_TYPE_INT32, &i_interface,
406 DBUS_TYPE_INT32, &i_protocol,
407 DBUS_TYPE_STRING, &name,
408 DBUS_TYPE_INT32, &i_aprotocol,
409 DBUS_TYPE_INVALID))) {
410 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
414 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
415 dbus_error_is_set(&error)) {
416 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
420 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
421 dbus_error_is_set(&error) ||
423 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
427 if (!(r->path = avahi_strdup(path))) {
429 /* FIXME: We don't remove the object on the server side */
431 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
435 dbus_message_unref(message);
436 dbus_message_unref(reply);
442 if (dbus_error_is_set(&error)) {
443 avahi_client_set_dbus_error(client, &error);
444 dbus_error_free(&error);
448 avahi_host_name_resolver_free(r);
451 dbus_message_unref(message);
454 dbus_message_unref(reply);
460 int avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
467 if (r->path && client->state != AVAHI_CLIENT_DISCONNECTED)
468 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_HOST_NAME_RESOLVER, "Free");
470 AVAHI_LLIST_REMOVE(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
478 AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) {
484 /* AvahiAddressResolver implementation */
486 DBusHandlerResult avahi_address_resolver_event (AvahiClient *client, AvahiResolverEvent event, DBusMessage *message) {
487 AvahiAddressResolver *r = NULL;
494 dbus_error_init (&error);
496 if (!(path = dbus_message_get_path(message)))
499 for (r = client->address_resolvers; r; r = r->address_resolvers_next)
500 if (strcmp (r->path, path) == 0)
506 if (event == AVAHI_RESOLVER_FOUND) {
508 AvahiProtocol protocol, aprotocol;
509 char *name, *address;
512 if (!dbus_message_get_args(
514 DBUS_TYPE_INT32, &interface,
515 DBUS_TYPE_INT32, &protocol,
516 DBUS_TYPE_INT32, &aprotocol,
517 DBUS_TYPE_STRING, &address,
518 DBUS_TYPE_STRING, &name,
519 DBUS_TYPE_INVALID) ||
520 dbus_error_is_set (&error)) {
521 fprintf(stderr, "Failed to parse resolver event.\n");
526 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
527 fprintf(stderr, "Failed to parse address\n");
531 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, (AvahiProtocol) aprotocol, &a, name, r->userdata);
534 assert(event == AVAHI_RESOLVER_TIMEOUT);
536 r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, (AvahiProtocol) 0, NULL, NULL, r->userdata);
539 return DBUS_HANDLER_RESULT_HANDLED;
542 dbus_error_free (&error);
543 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
546 AvahiAddressResolver * avahi_address_resolver_new_a(
548 AvahiIfIndex interface,
549 AvahiProtocol protocol,
550 const AvahiAddress *a,
551 AvahiAddressResolverCallback callback,
558 if (!avahi_address_snprint (addr, sizeof (addr), a)) {
559 avahi_client_set_errno(client, AVAHI_ERR_INVALID_ADDRESS);
563 return avahi_address_resolver_new (
564 client, interface, protocol,
569 AvahiAddressResolver * avahi_address_resolver_new(
571 AvahiIfIndex interface,
572 AvahiProtocol protocol,
574 AvahiAddressResolverCallback callback,
578 AvahiAddressResolver *r;
579 DBusMessage *message = NULL, *reply = NULL;
581 AvahiProtocol i_protocol;
586 dbus_error_init (&error);
588 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
589 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
593 if (!(r = avahi_new(AvahiAddressResolver, 1))) {
594 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
599 r->callback = callback;
600 r->userdata = userdata;
603 AVAHI_LLIST_PREPEND(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);
605 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "AddressResolverNew"))) {
606 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
610 i_interface = interface;
611 i_protocol = protocol;
613 if (!(dbus_message_append_args(
615 DBUS_TYPE_INT32, &i_interface,
616 DBUS_TYPE_INT32, &i_protocol,
617 DBUS_TYPE_STRING, &address,
618 DBUS_TYPE_INVALID))) {
619 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
623 if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
624 dbus_error_is_set(&error)) {
625 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
629 if (!dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID) ||
630 dbus_error_is_set(&error) ||
632 avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
636 if (!(r->path = avahi_strdup(path))) {
638 /* FIXME: We don't remove the object on the server side */
640 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
644 dbus_message_unref(message);
645 dbus_message_unref(reply);
651 if (dbus_error_is_set(&error)) {
652 avahi_client_set_dbus_error(client, &error);
653 dbus_error_free(&error);
657 avahi_address_resolver_free(r);
660 dbus_message_unref(message);
663 dbus_message_unref(reply);
669 AvahiClient* avahi_address_resolver_get_client (AvahiAddressResolver *r) {
675 int avahi_address_resolver_free(AvahiAddressResolver *r) {
682 if (r->path && client->state != AVAHI_CLIENT_DISCONNECTED)
683 ret = avahi_client_simple_method_call(client, r->path, AVAHI_DBUS_INTERFACE_ADDRESS_RESOLVER, "Free");
685 AVAHI_LLIST_REMOVE(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);