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
29 struct AvahiHostNameResolver {
33 AvahiRecordBrowser *record_browser_a;
34 AvahiRecordBrowser *record_browser_aaaa;
36 AvahiHostNameResolverCallback callback;
39 AvahiTimeEvent *time_event;
41 AVAHI_LLIST_FIELDS(AvahiHostNameResolver, resolver);
44 static void finish(AvahiHostNameResolver *r, gint interface, guchar protocol, AvahiResolverEvent event, AvahiRecord *record) {
49 if (r->record_browser_a) {
50 avahi_record_browser_free(r->record_browser_a);
51 r->record_browser_a = NULL;
54 if (r->record_browser_aaaa) {
55 avahi_record_browser_free(r->record_browser_aaaa);
56 r->record_browser_aaaa = NULL;
59 avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
63 switch (record->key->type) {
64 case AVAHI_DNS_TYPE_A:
66 a.data.ipv4 = record->data.a.address;
69 case AVAHI_DNS_TYPE_AAAA:
71 a.data.ipv6 = record->data.aaaa.address;
79 r->callback(r, interface, protocol, event, record ? record->key->name : r->host_name, record ? &a : NULL, r->userdata);
82 static void record_browser_callback(AvahiRecordBrowser*rr, gint interface, guchar protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
83 AvahiHostNameResolver *r = userdata;
89 if (!(event == AVAHI_BROWSER_NEW))
92 g_assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
93 finish(r, interface, protocol, AVAHI_RESOLVER_FOUND, record);
96 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
97 AvahiHostNameResolver *r = userdata;
102 finish(r, -1, AF_UNSPEC, AVAHI_RESOLVER_TIMEOUT, NULL);
105 AvahiHostNameResolver *avahi_host_name_resolver_new(AvahiServer *server, gint interface, guchar protocol, const gchar *host_name, guchar aprotocol, AvahiHostNameResolverCallback callback, gpointer userdata) {
106 AvahiHostNameResolver *r;
114 g_assert(aprotocol == AF_UNSPEC || aprotocol == AF_INET || aprotocol == AF_INET6);
116 r = g_new(AvahiHostNameResolver, 1);
118 r->host_name = avahi_normalize_name(host_name);
119 r->callback = callback;
120 r->userdata = userdata;
122 r->record_browser_a = r->record_browser_aaaa = NULL;
124 avahi_elapse_time(&tv, 1000, 0);
125 r->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, time_event_callback, r);
127 AVAHI_LLIST_PREPEND(AvahiHostNameResolver, resolver, server->host_name_resolvers, r);
129 if (aprotocol == AF_INET || aprotocol == AF_UNSPEC) {
130 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
131 r->record_browser_a = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
135 if (aprotocol == AF_INET6 || aprotocol == AF_UNSPEC) {
136 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
137 r->record_browser_aaaa = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
144 void avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
147 AVAHI_LLIST_REMOVE(AvahiHostNameResolver, resolver, r->server->host_name_resolvers, r);
149 if (r->record_browser_a)
150 avahi_record_browser_free(r->record_browser_a);
151 if (r->record_browser_aaaa)
152 avahi_record_browser_free(r->record_browser_aaaa);
154 g_free(r->host_name);