]> git.meshlink.io Git - catta/blob - avahi-core/resolve-address.c
* add address resolver
[catta] / avahi-core / resolve-address.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 "resolve.h"
27 #include "util.h"
28
29 struct AvahiAddressResolver {
30     AvahiServer *server;
31     AvahiAddress address;
32     
33     AvahiRecordResolver *record_resolver;
34
35     AvahiAddressResolverCallback callback;
36     gpointer userdata;
37
38     AVAHI_LLIST_FIELDS(AvahiAddressResolver, resolver);
39 };
40
41 static void record_resolver_callback(AvahiRecordResolver*rr, gint interface, guchar protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
42     AvahiAddressResolver *r = userdata;
43
44     g_assert(rr);
45     g_assert(record);
46     g_assert(r);
47
48     r->callback(r, interface, protocol, event, &r->address, record->data.ptr.name, r->userdata);
49 }
50
51 AvahiAddressResolver *avahi_address_resolver_new(AvahiServer *server, gint interface, guchar protocol, const AvahiAddress *address, AvahiAddressResolverCallback callback, gpointer userdata) {
52     AvahiAddressResolver *r;
53     AvahiKey *k;
54     gchar *n;
55
56     g_assert(server);
57     g_assert(address);
58     g_assert(callback);
59
60     g_assert(address->family == AF_INET || address->family == AF_INET6);
61
62     r = g_new(AvahiAddressResolver, 1);
63     r->server = server;
64     r->address = *address;
65     r->callback = callback;
66     r->userdata = userdata;
67
68     if (address->family == AF_INET)
69         n = avahi_reverse_lookup_name_ipv4(&address->data.ipv4);
70     else 
71         n = avahi_reverse_lookup_name_ipv6_arpa(&address->data.ipv6);
72     
73     k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
74     g_free(n);
75     
76     r->record_resolver = avahi_record_resolver_new(server, interface, protocol, k, record_resolver_callback, r);
77     avahi_key_unref(k);
78
79     AVAHI_LLIST_PREPEND(AvahiAddressResolver, resolver, server->address_resolvers, r);
80     
81     return r;
82 }
83
84 void avahi_address_resolver_free(AvahiAddressResolver *r) {
85     g_assert(r);
86
87     AVAHI_LLIST_REMOVE(AvahiAddressResolver, resolver, r->server->address_resolvers, r);
88     avahi_record_resolver_free(r->record_resolver);
89     g_free(r);
90 }