2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <avahi-common/domain.h>
27 #include <avahi-common/timeval.h>
28 #include <avahi-common/malloc.h>
29 #include <avahi-common/error.h>
34 #define TIMEOUT_MSEC 5000
36 struct AvahiSHostNameResolver {
40 AvahiSRecordBrowser *record_browser_a;
41 AvahiSRecordBrowser *record_browser_aaaa;
43 AvahiSHostNameResolverCallback callback;
46 AvahiRecord *address_record;
47 AvahiIfIndex interface;
48 AvahiProtocol protocol;
49 AvahiLookupResultFlags flags;
51 AvahiTimeEvent *time_event;
53 AVAHI_LLIST_FIELDS(AvahiSHostNameResolver, resolver);
56 static void finish(AvahiSHostNameResolver *r, AvahiResolverEvent event) {
60 avahi_time_event_free(r->time_event);
65 case AVAHI_RESOLVER_FOUND: {
68 assert(r->address_record);
70 switch (r->address_record->key->type) {
71 case AVAHI_DNS_TYPE_A:
72 a.proto = AVAHI_PROTO_INET;
73 a.data.ipv4 = r->address_record->data.a.address;
76 case AVAHI_DNS_TYPE_AAAA:
77 a.proto = AVAHI_PROTO_INET6;
78 a.data.ipv6 = r->address_record->data.aaaa.address;
85 r->callback(r, r->interface, r->protocol, AVAHI_RESOLVER_FOUND, r->address_record->key->name, &a, r->flags, r->userdata);
90 case AVAHI_RESOLVER_FAILURE:
92 r->callback(r, r->interface, r->protocol, event, r->host_name, NULL, r->flags, r->userdata);
97 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
98 AvahiSHostNameResolver *r = userdata;
103 avahi_server_set_errno(r->server, AVAHI_ERR_TIMEOUT);
104 finish(r, AVAHI_RESOLVER_FAILURE);
107 static void start_timeout(AvahiSHostNameResolver *r) {
114 avahi_elapse_time(&tv, TIMEOUT_MSEC, 0);
116 r->time_event = avahi_time_event_new(r->server->time_event_queue, &tv, time_event_callback, r);
119 static void record_browser_callback(
120 AvahiSRecordBrowser*rr,
121 AvahiIfIndex interface,
122 AvahiProtocol protocol,
123 AvahiBrowserEvent event,
125 AvahiLookupResultFlags flags,
128 AvahiSHostNameResolver *r = userdata;
135 case AVAHI_BROWSER_NEW:
137 assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
139 if (r->interface > 0 && interface != r->interface)
142 if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
145 if (r->interface <= 0)
146 r->interface = interface;
148 if (r->protocol == AVAHI_PROTO_UNSPEC)
149 r->protocol = protocol;
151 if (!r->address_record) {
152 r->address_record = avahi_record_ref(record);
155 finish(r, AVAHI_RESOLVER_FOUND);
160 case AVAHI_BROWSER_REMOVE:
162 assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
164 if (r->address_record && avahi_record_equal_no_ttl(record, r->address_record)) {
165 avahi_record_unref(r->address_record);
166 r->address_record = NULL;
171 /** Look for a replacement */
172 if (r->record_browser_aaaa)
173 avahi_s_record_browser_restart(r->record_browser_aaaa);
174 if (r->record_browser_a)
175 avahi_s_record_browser_restart(r->record_browser_a);
182 case AVAHI_BROWSER_CACHE_EXHAUSTED:
183 case AVAHI_BROWSER_ALL_FOR_NOW:
187 case AVAHI_BROWSER_FAILURE:
191 if (r->record_browser_aaaa)
192 avahi_s_record_browser_free(r->record_browser_aaaa);
193 if (r->record_browser_a)
194 avahi_s_record_browser_free(r->record_browser_a);
196 r->record_browser_a = r->record_browser_aaaa = NULL;
199 finish(r, AVAHI_RESOLVER_FAILURE);
204 AvahiSHostNameResolver *avahi_s_host_name_resolver_new(
206 AvahiIfIndex interface,
207 AvahiProtocol protocol,
208 const char *host_name,
209 AvahiProtocol aprotocol,
210 AvahiLookupFlags flags,
211 AvahiSHostNameResolverCallback callback,
214 AvahiSHostNameResolver *r;
221 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
222 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
223 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, avahi_is_valid_fqdn(host_name), AVAHI_ERR_INVALID_HOST_NAME);
224 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(aprotocol), AVAHI_ERR_INVALID_PROTOCOL);
225 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST), AVAHI_ERR_INVALID_FLAGS);
227 if (!(r = avahi_new(AvahiSHostNameResolver, 1))) {
228 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
233 r->host_name = avahi_normalize_name_strdup(host_name);
234 r->callback = callback;
235 r->userdata = userdata;
236 r->address_record = NULL;
237 r->interface = interface;
238 r->protocol = protocol;
241 r->record_browser_a = r->record_browser_aaaa = NULL;
243 r->time_event = NULL;
245 AVAHI_LLIST_PREPEND(AvahiSHostNameResolver, resolver, server->host_name_resolvers, r);
247 r->record_browser_aaaa = r->record_browser_a = NULL;
249 if (aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_UNSPEC) {
250 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
251 r->record_browser_a = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, r);
254 if (!r->record_browser_a)
258 if (aprotocol == AVAHI_PROTO_INET6 || aprotocol == AVAHI_PROTO_UNSPEC) {
259 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
260 r->record_browser_aaaa = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, r);
263 if (!r->record_browser_aaaa)
267 assert(r->record_browser_aaaa || r->record_browser_a);
274 avahi_s_host_name_resolver_free(r);
278 void avahi_s_host_name_resolver_free(AvahiSHostNameResolver *r) {
281 AVAHI_LLIST_REMOVE(AvahiSHostNameResolver, resolver, r->server->host_name_resolvers, r);
283 if (r->record_browser_a)
284 avahi_s_record_browser_free(r->record_browser_a);
286 if (r->record_browser_aaaa)
287 avahi_s_record_browser_free(r->record_browser_aaaa);
290 avahi_time_event_free(r->time_event);
292 if (r->address_record)
293 avahi_record_unref(r->address_record);
295 avahi_free(r->host_name);