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 static void pending_call_callback(DBusPendingCall *pending, void *userdata) {
42 AvahiServiceResolver *r = userdata;
43 DBusMessage *message = NULL;
44 AvahiStringList *strlst = NULL;
50 dbus_error_init(&error);
52 if (!(message = dbus_pending_call_steal_reply(pending)))
55 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
57 int32_t interface, protocol, aprotocol;
58 char *name, *type, *domain, *host, *address;
60 DBusMessageIter iter, sub;
63 if (!dbus_message_get_args(
65 DBUS_TYPE_INT32, &interface,
66 DBUS_TYPE_INT32, &protocol,
67 DBUS_TYPE_STRING, &name,
68 DBUS_TYPE_STRING, &type,
69 DBUS_TYPE_STRING, &domain,
70 DBUS_TYPE_STRING, &host,
71 DBUS_TYPE_INT32, &aprotocol,
72 DBUS_TYPE_STRING, &address,
73 DBUS_TYPE_UINT16, &port,
75 dbus_error_is_set (&error)) {
76 fprintf(stderr, "Failed to parse resolver event.\n");
80 dbus_message_iter_init(message, &iter);
82 for (j = 0; j < 9; j++)
83 dbus_message_iter_next(&iter);
85 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
86 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) {
87 fprintf(stderr, "Error parsing service resolving message");
92 dbus_message_iter_recurse(&iter, &sub);
99 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
102 assert(at == DBUS_TYPE_ARRAY);
104 if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
105 fprintf(stderr, "Error parsing service resolving message");
109 dbus_message_iter_recurse(&sub, &sub2);
110 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
111 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
113 dbus_message_iter_next(&sub);
117 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
118 fprintf(stderr, "Failed to parse address\n");
122 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, &a, port, strlst, r->userdata);
126 assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
128 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
130 r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, NULL, NULL, NULL, 0, NULL, r->userdata);
136 dbus_message_unref(message);
138 avahi_string_list_free(strlst);
140 dbus_error_free (&error);
143 AvahiServiceResolver * avahi_service_resolver_new(
145 AvahiIfIndex interface,
146 AvahiProtocol protocol,
150 AvahiProtocol aprotocol,
151 AvahiServiceResolverCallback callback,
155 AvahiServiceResolver *r;
156 DBusMessage *message;
157 int32_t i_interface, i_protocol, i_aprotocol;
166 dbus_error_init (&error);
168 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
169 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
173 if (!(r = avahi_new(AvahiServiceResolver, 1))) {
174 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
179 r->callback = callback;
180 r->userdata = userdata;
183 AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
185 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService"))) {
186 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
190 i_interface = interface;
191 i_protocol = protocol;
192 i_aprotocol = aprotocol;
194 if (!(dbus_message_append_args(
196 DBUS_TYPE_INT32, &i_interface,
197 DBUS_TYPE_INT32, &i_protocol,
198 DBUS_TYPE_STRING, &name,
199 DBUS_TYPE_STRING, &type,
200 DBUS_TYPE_STRING, &domain,
201 DBUS_TYPE_INT32, &i_aprotocol,
202 DBUS_TYPE_INVALID))) {
203 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
207 if (!dbus_connection_send_with_reply(client->bus, message, &r->call, -1) ||
208 !dbus_pending_call_set_notify(r->call, pending_call_callback, r, NULL)) {
209 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
213 dbus_message_unref(message);
219 if (dbus_error_is_set(&error)) {
220 avahi_client_set_dbus_error(client, &error);
221 dbus_error_free(&error);
225 avahi_service_resolver_free(r);
228 dbus_message_unref(message);
234 int avahi_service_resolver_free(AvahiServiceResolver *r) {
241 dbus_pending_call_cancel(r->call);
242 dbus_pending_call_unref(r->call);
245 AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
252 int avahi_service_resolver_block(AvahiServiceResolver *r) {
259 dbus_pending_call_block(r->call);