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) {
58 int32_t interface, protocol, aprotocol;
59 char *name, *type, *domain, *host, *address;
61 DBusMessageIter iter, sub;
64 if (!dbus_message_get_args(
66 DBUS_TYPE_INT32, &interface,
67 DBUS_TYPE_INT32, &protocol,
68 DBUS_TYPE_STRING, &name,
69 DBUS_TYPE_STRING, &type,
70 DBUS_TYPE_STRING, &domain,
71 DBUS_TYPE_STRING, &host,
72 DBUS_TYPE_INT32, &aprotocol,
73 DBUS_TYPE_STRING, &address,
74 DBUS_TYPE_UINT16, &port,
76 dbus_error_is_set (&error)) {
77 fprintf(stderr, "Failed to parse resolver event.\n");
81 dbus_message_iter_init(message, &iter);
83 for (j = 0; j < 9; j++)
84 dbus_message_iter_next(&iter);
86 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
87 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY) {
88 fprintf(stderr, "Error parsing service resolving message");
93 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);
111 if (dbus_message_iter_get_array_len(&sub2) > 0) {
115 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
116 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
119 dbus_message_iter_next(&sub);
123 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
124 fprintf(stderr, "Failed to parse address\n");
128 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, &a, port, strlst, r->userdata);
132 assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
134 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
136 r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, NULL, NULL, NULL, 0, NULL, r->userdata);
142 dbus_message_unref(message);
144 avahi_string_list_free(strlst);
146 dbus_error_free (&error);
150 hostname_pending_call_callback(DBusPendingCall *pending, void *userdata) {
151 AvahiHostNameResolver *r = userdata;
152 DBusMessage *message = NULL;
158 dbus_error_init(&error);
160 if (!(message = dbus_pending_call_steal_reply(pending)))
163 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
164 int32_t interface, protocol, aprotocol;
165 char *name, *address;
168 if (!dbus_message_get_args(
170 DBUS_TYPE_INT32, &interface,
171 DBUS_TYPE_INT32, &protocol,
172 DBUS_TYPE_STRING, &name,
173 DBUS_TYPE_INT32, &aprotocol,
174 DBUS_TYPE_STRING, &address,
175 DBUS_TYPE_INVALID) ||
176 dbus_error_is_set (&error)) {
177 fprintf(stderr, "Failed to parse resolver event.\n");
182 if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
183 fprintf(stderr, "Failed to parse address\n");
187 r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, &a, r->userdata);
191 assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
193 avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
195 r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, r->userdata);
201 dbus_message_unref(message);
203 dbus_error_free (&error);
206 AvahiServiceResolver * avahi_service_resolver_new(
208 AvahiIfIndex interface,
209 AvahiProtocol protocol,
213 AvahiProtocol aprotocol,
214 AvahiServiceResolverCallback callback,
218 AvahiServiceResolver *r;
219 DBusMessage *message;
220 int32_t i_interface, i_protocol, i_aprotocol;
229 dbus_error_init (&error);
231 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
232 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
236 if (!(r = avahi_new(AvahiServiceResolver, 1))) {
237 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
242 r->callback = callback;
243 r->userdata = userdata;
246 AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
248 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService"))) {
249 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
253 i_interface = interface;
254 i_protocol = protocol;
255 i_aprotocol = aprotocol;
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_INVALID))) {
266 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
270 if (!dbus_connection_send_with_reply(client->bus, message, &r->call, -1) ||
271 !dbus_pending_call_set_notify(r->call, service_pending_call_callback, r, NULL)) {
272 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
276 dbus_message_unref(message);
282 if (dbus_error_is_set(&error)) {
283 avahi_client_set_dbus_error(client, &error);
284 dbus_error_free(&error);
288 avahi_service_resolver_free(r);
291 dbus_message_unref(message);
297 AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) {
305 int avahi_service_resolver_free(AvahiServiceResolver *r) {
312 dbus_pending_call_cancel(r->call);
313 dbus_pending_call_unref(r->call);
316 AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
323 int avahi_service_resolver_block(AvahiServiceResolver *r) {
330 dbus_pending_call_block(r->call);
335 AvahiHostNameResolver * avahi_host_name_resolver_new(
337 AvahiIfIndex interface,
338 AvahiProtocol protocol,
340 AvahiProtocol aprotocol,
341 AvahiHostNameResolverCallback callback,
345 AvahiHostNameResolver *r;
346 DBusMessage *message;
347 int32_t i_interface, i_protocol, i_aprotocol;
352 dbus_error_init (&error);
354 if (client->state == AVAHI_CLIENT_DISCONNECTED) {
355 avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
359 if (!(r = avahi_new(AvahiHostNameResolver, 1))) {
360 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
365 r->callback = callback;
366 r->userdata = userdata;
369 AVAHI_LLIST_PREPEND(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
371 if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName"))) {
372 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
376 i_interface = interface;
377 i_protocol = protocol;
378 i_aprotocol = aprotocol;
380 if (!(dbus_message_append_args(
382 DBUS_TYPE_INT32, &i_interface,
383 DBUS_TYPE_INT32, &i_protocol,
384 DBUS_TYPE_STRING, &name,
385 DBUS_TYPE_INT32, &i_aprotocol,
386 DBUS_TYPE_INVALID))) {
387 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
391 if (!dbus_connection_send_with_reply(client->bus, message, &r->call, -1) ||
392 !dbus_pending_call_set_notify(r->call, hostname_pending_call_callback, r, NULL)) {
393 avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
397 dbus_message_unref(message);
403 if (dbus_error_is_set(&error)) {
404 avahi_client_set_dbus_error(client, &error);
405 dbus_error_free(&error);
409 avahi_host_name_resolver_free(r);
412 dbus_message_unref(message);
418 int avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
425 dbus_pending_call_cancel(r->call);
426 dbus_pending_call_unref(r->call);
429 AVAHI_LLIST_REMOVE(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
436 AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) {
444 int avahi_host_name_resolver_block(AvahiHostNameResolver *r) {
451 dbus_pending_call_block(r->call);