]> git.meshlink.io Git - catta/blob - avahi-client/resolver.c
* Implement AddressResolver in the avahi-client C api
[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;
59         AvahiProtocol protocol, aprotocol;
60         char *name, *type, *domain, *host, *address;
61         uint16_t port;
62         DBusMessageIter iter, sub;
63         AvahiAddress a;
64         
65         if (!dbus_message_get_args(
66                 message, &error,
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,
76                 DBUS_TYPE_INVALID) ||
77             dbus_error_is_set (&error)) {
78             fprintf(stderr, "Failed to parse resolver event.\n");
79             goto fail;
80         }
81         
82         dbus_message_iter_init(message, &iter);
83         
84         for (j = 0; j < 9; j++)
85             dbus_message_iter_next(&iter);
86         
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");
90             goto fail;
91         }
92         
93         strlst = NULL;
94         dbus_message_iter_recurse(&iter, &sub);
95         
96         for (;;) {
97             DBusMessageIter sub2;
98             int at;
99             
100             if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
101                 break;
102             
103             assert(at == DBUS_TYPE_ARRAY);
104             
105             if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
106                 fprintf(stderr, "Error parsing service resolving message");
107                 goto fail;
108             }
109             
110             dbus_message_iter_recurse(&sub, &sub2);
111
112             if (dbus_message_iter_get_array_len(&sub2) > 0) {
113                 uint8_t *k;
114                 int n;
115                 
116                 dbus_message_iter_get_fixed_array(&sub2, &k, &n);
117                 strlst = avahi_string_list_add_arbitrary(strlst, k, n);
118             }
119             
120             dbus_message_iter_next(&sub);
121         }
122
123         assert(address);
124         if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
125             fprintf(stderr, "Failed to parse address\n");
126             goto fail;
127         }
128     
129         r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, type, domain, host, &a, port, strlst, r->userdata);
130
131     } else {
132
133         assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
134
135         avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
136
137         r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, NULL, NULL, NULL, 0, NULL, r->userdata);
138     }
139
140 fail:
141     
142     if (message)
143         dbus_message_unref(message);
144     
145     avahi_string_list_free(strlst);
146     
147     dbus_error_free (&error);
148 }
149
150 static void
151 hostname_pending_call_callback(DBusPendingCall *pending, void *userdata) {
152     AvahiHostNameResolver *r =  userdata;
153     DBusMessage *message = NULL;
154     DBusError error;
155     
156     assert(pending);
157     assert(r);
158
159     dbus_error_init(&error);
160
161     if (!(message = dbus_pending_call_steal_reply(pending)))
162         goto fail;
163
164     if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
165         int32_t interface;
166         AvahiProtocol protocol, aprotocol;
167         char *name, *address;
168         AvahiAddress a;
169         
170         if (!dbus_message_get_args(
171                 message, &error,
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");
180             goto fail;
181         }
182         
183         assert(address);
184         if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
185             fprintf(stderr, "Failed to parse address\n");
186             goto fail;
187         }
188     
189         r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, name, &a, r->userdata);
190
191     } else {
192
193         assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
194
195         avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
196
197         r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, NULL, NULL, r->userdata);
198     }
199
200 fail:
201     
202     if (message)
203         dbus_message_unref(message);
204     
205     dbus_error_free (&error);
206 }
207
208 static void
209 address_pending_call_callback(DBusPendingCall *pending, void *userdata) {
210     AvahiAddressResolver *r =  userdata;
211     DBusMessage *message = NULL;
212     DBusError error;
213     
214     assert(pending);
215     assert(r);
216
217     dbus_error_init(&error);
218
219     if (!(message = dbus_pending_call_steal_reply(pending)))
220         goto fail;
221
222     if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
223         int32_t interface;
224         AvahiProtocol protocol, aprotocol;
225         char *name, *address;
226         AvahiAddress a;
227         
228         if (!dbus_message_get_args(
229                 message, &error,
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");
238             goto fail;
239         }
240         
241         assert(address);
242         if (!avahi_address_parse(address, (AvahiProtocol) aprotocol, &a)) {
243             fprintf(stderr, "Failed to parse address\n");
244             goto fail;
245         }
246     
247         r->callback(r, (AvahiIfIndex) interface, (AvahiProtocol) protocol, AVAHI_RESOLVER_FOUND, (AvahiProtocol) aprotocol, &a, name, r->userdata);
248
249     } else {
250
251         assert(dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_ERROR);
252
253         avahi_client_set_errno(r->client, avahi_error_dbus_to_number(dbus_message_get_error_name(message)));
254
255         r->callback(r, (AvahiIfIndex) 0, (AvahiProtocol) 0, AVAHI_RESOLVER_TIMEOUT, (AvahiProtocol) 0, NULL, NULL, r->userdata);
256     }
257
258 fail:
259     
260     if (message)
261         dbus_message_unref(message);
262     
263     dbus_error_free (&error);
264 }
265
266 /* AvahiServiceResolver implementation */
267 AvahiServiceResolver * avahi_service_resolver_new(
268     AvahiClient *client,
269     AvahiIfIndex interface,
270     AvahiProtocol protocol,
271     const char *name,
272     const char *type,
273     const char *domain,
274     AvahiProtocol aprotocol,
275     AvahiServiceResolverCallback callback,
276     void *userdata) {
277
278     DBusError error;
279     AvahiServiceResolver *r;
280     DBusMessage *message;
281     int32_t i_interface, i_protocol, i_aprotocol;
282     
283     assert(client);
284     assert(name);
285     assert(type);
286
287     if (!domain)
288         domain = "";
289     
290     dbus_error_init (&error);
291
292     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
293         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
294         goto fail;
295     }
296
297     if (!(r = avahi_new(AvahiServiceResolver, 1))) {
298         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
299         goto fail;
300     }
301
302     r->client = client;
303     r->callback = callback;
304     r->userdata = userdata;
305     r->call = NULL;
306     
307     AVAHI_LLIST_PREPEND(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
308
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);
311         goto fail;
312     }
313
314     i_interface = interface;
315     i_protocol = protocol;
316     i_aprotocol = aprotocol;
317
318     if (!(dbus_message_append_args(
319               message,
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);
328         goto fail;
329     }
330
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);
334         goto fail;
335     }
336
337     dbus_message_unref(message);
338
339     return r;
340     
341 fail:
342
343     if (dbus_error_is_set(&error)) {
344         avahi_client_set_dbus_error(client, &error);
345         dbus_error_free(&error);
346     }
347
348     if (r)
349         avahi_service_resolver_free(r);
350     
351     if (message)
352         dbus_message_unref(message);
353
354     return NULL;
355
356 }
357
358 AvahiClient* avahi_service_resolver_get_client (AvahiServiceResolver *r) {
359     assert (r);
360
361     return r->client;
362 }
363
364 int avahi_service_resolver_free(AvahiServiceResolver *r) {
365     AvahiClient *client;
366
367     assert(r);
368     client = r->client;
369
370     if (r->call) {
371         dbus_pending_call_cancel(r->call);
372         dbus_pending_call_unref(r->call);
373     }
374
375     AVAHI_LLIST_REMOVE(AvahiServiceResolver, service_resolvers, client->service_resolvers, r);
376
377     avahi_free(r);
378
379     return AVAHI_OK;
380 }
381
382 int avahi_service_resolver_block(AvahiServiceResolver *r) {
383     AvahiClient *client;
384
385     assert(r);
386     client = r->client;
387
388     if (r->call)
389         dbus_pending_call_block(r->call);
390
391     return AVAHI_OK;
392 }
393
394 /* AvahiHostNameResolver implementation */
395
396 AvahiHostNameResolver * avahi_host_name_resolver_new(
397     AvahiClient *client,
398     AvahiIfIndex interface,
399     AvahiProtocol protocol,
400     const char *name,
401     AvahiProtocol aprotocol,
402     AvahiHostNameResolverCallback callback,
403     void *userdata) {
404
405     DBusError error;
406     AvahiHostNameResolver *r;
407     DBusMessage *message;
408     int32_t i_interface, i_protocol, i_aprotocol;
409     
410     assert(client);
411     assert(name);
412
413     dbus_error_init (&error);
414
415     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
416         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
417         goto fail;
418     }
419
420     if (!(r = avahi_new(AvahiHostNameResolver, 1))) {
421         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
422         goto fail;
423     }
424
425     r->client = client;
426     r->callback = callback;
427     r->userdata = userdata;
428     r->call = NULL;
429     
430     AVAHI_LLIST_PREPEND(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
431
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);
434         goto fail;
435     }
436
437     i_interface = interface;
438     i_protocol = protocol;
439     i_aprotocol = aprotocol;
440
441     if (!(dbus_message_append_args(
442               message,
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);
449         goto fail;
450     }
451
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);
455         goto fail;
456     }
457
458     dbus_message_unref(message);
459
460     return r;
461     
462 fail:
463
464     if (dbus_error_is_set(&error)) {
465         avahi_client_set_dbus_error(client, &error);
466         dbus_error_free(&error);
467     }
468
469     if (r)
470         avahi_host_name_resolver_free(r);
471     
472     if (message)
473         dbus_message_unref(message);
474
475     return NULL;
476
477 }
478
479 int avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
480     AvahiClient *client;
481
482     assert(r);
483     client = r->client;
484
485     if (r->call) {
486         dbus_pending_call_cancel(r->call);
487         dbus_pending_call_unref(r->call);
488     }
489
490     AVAHI_LLIST_REMOVE(AvahiHostNameResolver, host_name_resolvers, client->host_name_resolvers, r);
491
492     avahi_free(r);
493
494     return AVAHI_OK;
495 }
496
497 AvahiClient* avahi_host_name_resolver_get_client (AvahiHostNameResolver *r) {
498     assert (r);
499
500     return r->client;
501 }
502
503 int avahi_host_name_resolver_block(AvahiHostNameResolver *r) {
504     AvahiClient *client;
505
506     assert(r);
507     client = r->client;
508
509     if (r->call)
510         dbus_pending_call_block(r->call);
511
512     return AVAHI_OK;
513 }
514
515 /* AvahiAddressResolver implementation */
516
517 AvahiAddressResolver * avahi_address_resolver_new_a(
518     AvahiClient *client,
519     AvahiIfIndex interface,
520     const AvahiAddress *a,
521     AvahiAddressResolverCallback callback,
522     void *userdata) {
523
524     char addr[64];
525
526     assert (a);
527
528     avahi_address_snprint (addr, sizeof (addr), a);
529
530     return avahi_address_resolver_new (client, interface,
531             a->family, addr,
532             callback, userdata);
533 }
534
535 AvahiAddressResolver * avahi_address_resolver_new(
536     AvahiClient *client,
537     AvahiIfIndex interface,
538     AvahiProtocol protocol,
539     const char *address,
540     AvahiAddressResolverCallback callback,
541     void *userdata) {
542
543     DBusError error;
544     AvahiAddressResolver *r;
545     DBusMessage *message;
546     int32_t i_interface;
547     AvahiProtocol i_protocol;
548     
549     assert(client);
550
551     dbus_error_init (&error);
552
553     if (client->state == AVAHI_CLIENT_DISCONNECTED) {
554         avahi_client_set_errno(client, AVAHI_ERR_BAD_STATE);
555         goto fail;
556     }
557
558     if (!(r = avahi_new(AvahiAddressResolver, 1))) {
559         avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
560         goto fail;
561     }
562
563     r->client = client;
564     r->callback = callback;
565     r->userdata = userdata;
566     r->call = NULL;
567     
568     AVAHI_LLIST_PREPEND(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);
569
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);
572         goto fail;
573     }
574
575     i_interface = interface;
576     i_protocol = protocol;
577
578     if (!(dbus_message_append_args(
579               message,
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);
585         goto fail;
586     }
587
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);
591         goto fail;
592     }
593
594     dbus_message_unref(message);
595
596     return r;
597     
598 fail:
599
600     if (dbus_error_is_set(&error)) {
601         avahi_client_set_dbus_error(client, &error);
602         dbus_error_free(&error);
603     }
604
605     if (r)
606         avahi_address_resolver_free(r);
607     
608     if (message)
609         dbus_message_unref(message);
610
611     return NULL;
612
613 }
614
615 AvahiClient* avahi_address_resolver_get_client (AvahiAddressResolver *r) {
616     assert (r);
617
618     return r->client;
619 }
620
621 int avahi_address_resolver_free(AvahiAddressResolver *r) {
622     AvahiClient *client;
623
624     assert(r);
625     client = r->client;
626
627     if (r->call) {
628         dbus_pending_call_cancel(r->call);
629         dbus_pending_call_unref(r->call);
630     }
631
632     AVAHI_LLIST_REMOVE(AvahiAddressResolver, address_resolvers, client->address_resolvers, r);
633
634     avahi_free(r);
635
636     return AVAHI_OK;
637 }
638
639 int avahi_address_resolver_block(AvahiAddressResolver *r) {
640     AvahiClient *client;
641
642     assert(r);
643     client = r->client;
644
645     if (r->call)
646         dbus_pending_call_block(r->call);
647
648     return AVAHI_OK;
649 }