]> git.meshlink.io Git - catta/blob - avahi-core/resolve-host-name.c
* rename subscribe.[ch] to resolve.[ch]
[catta] / avahi-core / resolve-host-name.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 AvahiHostNameResolver {
30     AvahiServer *server;
31     gchar *host_name;
32     
33     AvahiRecordResolver *record_resolver_a;
34     AvahiRecordResolver *record_resolver_aaaa;
35
36     AvahiHostNameResolverCallback callback;
37     gpointer userdata;
38
39     AVAHI_LLIST_FIELDS(AvahiHostNameResolver, resolver);
40 };
41
42 static void record_resolver_callback(AvahiRecordResolver*rr, gint interface, guchar protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
43     AvahiHostNameResolver *r = userdata;
44     AvahiAddress a;
45
46     g_assert(rr);
47     g_assert(record);
48     g_assert(r);
49
50     switch (record->key->type) {
51         case AVAHI_DNS_TYPE_A:
52             a.family = AF_INET;
53             a.data.ipv4 = record->data.a.address;
54             break;
55
56         case AVAHI_DNS_TYPE_AAAA:
57             a.family = AF_INET6;
58             a.data.ipv6 = record->data.aaaa.address;
59             break;
60
61         default:
62             g_assert(FALSE);
63     }
64
65     r->callback(r, interface, protocol, event, record->key->name, &a, r->userdata);
66 }
67
68 AvahiHostNameResolver *avahi_host_name_resolver_new(AvahiServer *s, gint interface, guchar protocol, const gchar *host_name, AvahiHostNameResolverCallback callback, gpointer userdata) {
69     AvahiHostNameResolver *r;
70     AvahiKey *k;
71
72     g_assert(s);
73     g_assert(host_name);
74     g_assert(callback);
75
76     r = g_new(AvahiHostNameResolver, 1);
77     r->server = s;
78     r->host_name = avahi_normalize_name(host_name);
79     r->callback = callback;
80     r->userdata = userdata;
81
82     k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
83     r->record_resolver_a = avahi_record_resolver_new(s, interface, protocol, k, record_resolver_callback, r);
84     avahi_key_unref(k);
85
86     k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
87     r->record_resolver_aaaa = avahi_record_resolver_new(s, interface, protocol, k, record_resolver_callback, r);
88     avahi_key_unref(k);
89
90     AVAHI_LLIST_PREPEND(AvahiHostNameResolver, resolver, s->host_name_resolvers, r);
91     
92     return r;
93 }
94
95 void avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
96     g_assert(r);
97
98     AVAHI_LLIST_REMOVE(AvahiHostNameResolver, resolver, r->server->host_name_resolvers, r);
99
100     avahi_record_resolver_free(r->record_resolver_a);
101     avahi_record_resolver_free(r->record_resolver_aaaa);
102     g_free(r->host_name);
103     g_free(r);
104 }