]> git.meshlink.io Git - catta/blob - avahi-client/resolver.c
2e443512aa277706aabffdd2ded7bc42cf85e61a
[catta] / avahi-client / resolver.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include <dbus/dbus.h>
31
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>
37
38 #include "client.h"
39 #include "internal.h"
40
41 static void
42 service_pending_call_callback(DBusPendingCall *pending, void *userdata) {
43     AvahiServiceResolver *r =  userdata;
44     DBusMessage *message = NULL;
45     AvahiStringList *strlst = NULL;
46     DBusError error;
47     
48     assert(pending);
49     assert(r);
50
51     dbus_error_init(&error);
52
53     if (!(message = dbus_pending_call_steal_reply(pending)))
54         goto fail;
55
56     if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
57         int j;
58         int32_t interface, protocol, aprotocol;
59         char *name, *type, *domain, *host, *address;
60         uint16_t port;
61         DBusMessageIter iter, sub;
62         AvahiAddress a;
63         
64         if (!dbus_message_get_args(
65                 message, &error,
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,
75                 DBUS_TYPE_INVALID) ||
76             dbus_error_is_set (&error)) {
77             fprintf(stderr, "Failed to parse resolver event.\n");
78             goto fail;
79         }
80         
81         dbus_message_iter_init(message, &iter);
82         
83         for (j = 0; j < 9; j++)
84             dbus_message_iter_next(&iter);
85         
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");
89             goto fail;
90         }
91         
92         strlst = NULL;
93         dbus_message_iter_recurse(&iter, &sub);
94         
95         for (;;) {
96             DBusMessageIter sub2;
97             int at;
98             
99             if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
100                 break;
101             
102             assert(at == DBUS_TYPE_ARRAY);
103             
104             if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
105                 fprintf(stderr, "Error parsing service resolving message");
106                 goto fail;
107             }
108             
109             dbus_message_iter_recurse(&sub, &sub2);
110
111             if (dbus_message_iter_get_array_len(&sub2) > 0) {
112                 uint8_t *k;
113                 int n;
114                 
115                 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
116                 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
117             }
118             
119             dbus_message_iter_next(&sub);
120         }
121
122         assert(address);
123         if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
124             fprintf(stderr, "Failed to parse address\n");
125             goto fail;
126         }
127     
128         r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, &a, port, strlst, r->userdata);
129
130     } else {
131
132         assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
133
134         avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
135
136         r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, NULL, NULL, NULL, 0, NULL, r->userdata);
137     }
138
139 fail:
140     
141     if (message)
142         dbus_message_unref(message);
143     
144     avahi_string_list_free(strlst);
145     
146     dbus_error_free (&error);
147 }
148
149 static void
150 hostname_pending_call_callback(DBusPendingCall *pending, void *userdata) {
151     AvahiHostNameResolver *r =  userdata;
152     DBusMessage *message = NULL;
153     DBusError error;
154     
155     assert(pending);
156     assert(r);
157
158     dbus_error_init(&error);
159
160     if (!(message = dbus_pending_call_steal_reply(pending)))
161         goto fail;
162
163     if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
164         int32_t interface, protocol, aprotocol;
165         char *name, *address;
166         AvahiAddress a;
167         
168         if (!dbus_message_get_args(
169                 message, &error,
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");
178             goto fail;
179         }
180         
181         assert(address);
182         if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
183             fprintf(stderr, "Failed to parse address\n");
184             goto fail;
185         }
186     
187         r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, &a, r->userdata);
188
189     } else {
190
191         assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
192
193         avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
194
195         r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, r->userdata);
196     }
197
198 fail:
199     
200     if (message)
201         dbus_message_unref(message);
202     
203     dbus_error_free (&error);
204 }
205
206 AvahiServiceResolver * avahi_service_resolver_new(
207     AvahiClient *client,
208     AvahiIfIndex interface,
209     AvahiProtocol protocol,
210     const char *name,
211     const char *type,
212     const char *domain,
213     AvahiProtocol aprotocol,
214     AvahiServiceResolverCallback callback,
215     void *userdata) {
216
217     DBusError error;
218     AvahiServiceResolver *r;
219     DBusMessage *message;
220     int32_t i_interface, i_protocol, i_aprotocol;
221     
222     assert(client);
223     assert(name);
224     assert(type);
225
226     if (!domain)
227         domain = "";
228     
229     dbus_error_init (&error);
230
231     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
232         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
233         goto fail;
234     }
235
236     if (!(r = avahi_new(AvahiServiceResolver, 1))) {
237         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
238         goto fail;
239     }
240
241     r->client = client;
242     r->callback = callback;
243     r->userdata = userdata;
244     r->call = NULL;
245     
246     AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
247
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);
250         goto fail;
251     }
252
253     i_interface = interface;
254     i_protocol = protocol;
255     i_aprotocol = aprotocol;
256
257     if (!(dbus_message_append_args(
258               message,
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);
267         goto fail;
268     }
269
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);
273         goto fail;
274     }
275
276     dbus_message_unref(message);
277
278     return r;
279     
280 fail:
281
282     if (dbus_error_is_set(&error)) {
283         avahi_client_set_dbus_error(client, &error);
284         dbus_error_free(&error);
285     }
286
287     if (r)
288         avahi_service_resolver_free(r);
289     
290     if (message)
291         dbus_message_unref(message);
292
293     return NULL;
294
295 }
296
297 AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) {
298     AvahiClient *client;
299
300     assert (r);
301
302     return r->client;
303 }
304
305 int avahi_service_resolver_free(AvahiServiceResolver *r) {
306     AvahiClient *client;
307
308     assert(r);
309     client = r->client;
310
311     if (r->call) {
312         dbus_pending_call_cancel(r->call);
313         dbus_pending_call_unref(r->call);
314     }
315
316     AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
317
318     avahi_free(r);
319
320     return AVAHI_OK;
321 }
322
323 int avahi_service_resolver_block(AvahiServiceResolver *r) {
324     AvahiClient *client;
325
326     assert(r);
327     client = r->client;
328
329     if (r->call)
330         dbus_pending_call_block(r->call);
331
332     return AVAHI_OK;
333 }
334
335 AvahiHostNameResolver * avahi_host_name_resolver_new(
336     AvahiClient *client,
337     AvahiIfIndex interface,
338     AvahiProtocol protocol,
339     const char *name,
340     AvahiProtocol aprotocol,
341     AvahiHostNameResolverCallback callback,
342     void *userdata) {
343
344     DBusError error;
345     AvahiHostNameResolver *r;
346     DBusMessage *message;
347     int32_t i_interface, i_protocol, i_aprotocol;
348     
349     assert(client);
350     assert(name);
351
352     dbus_error_init (&error);
353
354     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
355         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
356         goto fail;
357     }
358
359     if (!(r = avahi_new(AvahiHostNameResolver, 1))) {
360         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
361         goto fail;
362     }
363
364     r->client = client;
365     r->callback = callback;
366     r->userdata = userdata;
367     r->call = NULL;
368     
369     AVAHI_LLIST_PREPEND(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
370
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);
373         goto fail;
374     }
375
376     i_interface = interface;
377     i_protocol = protocol;
378     i_aprotocol = aprotocol;
379
380     if (!(dbus_message_append_args(
381               message,
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);
388         goto fail;
389     }
390
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);
394         goto fail;
395     }
396
397     dbus_message_unref(message);
398
399     return r;
400     
401 fail:
402
403     if (dbus_error_is_set(&error)) {
404         avahi_client_set_dbus_error(client, &error);
405         dbus_error_free(&error);
406     }
407
408     if (r)
409         avahi_host_name_resolver_free(r);
410     
411     if (message)
412         dbus_message_unref(message);
413
414     return NULL;
415
416 }
417
418 int avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
419     AvahiClient *client;
420
421     assert(r);
422     client = r->client;
423
424     if (r->call) {
425         dbus_pending_call_cancel(r->call);
426         dbus_pending_call_unref(r->call);
427     }
428
429     AVAHI_LLIST_REMOVE(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
430
431     avahi_free(r);
432
433     return AVAHI_OK;
434 }
435
436 AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) {
437     AvahiClient *client;
438
439     assert (r);
440
441     return r->client;
442 }
443
444 int avahi_host_name_resolver_block(AvahiHostNameResolver *r) {
445     AvahiClient *client;
446
447     assert(r);
448     client = r->client;
449
450     if (r->call)
451         dbus_pending_call_block(r->call);
452
453     return AVAHI_OK;
454 }