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
28 #include <avahi-common/domain.h>
29 #include <avahi-common/timeval.h>
30 #include <avahi-common/malloc.h>
31 #include <avahi-common/error.h>
36 #define TIMEOUT_MSEC 5000
38 struct AvahiSHostNameResolver {
42 AvahiSRecordBrowser *record_browser_a;
43 AvahiSRecordBrowser *record_browser_aaaa;
45 AvahiSHostNameResolverCallback callback;
48 AvahiRecord *address_record;
49 AvahiIfIndex interface;
50 AvahiProtocol protocol;
51 AvahiLookupResultFlags flags;
53 AvahiTimeEvent *time_event;
55 AVAHI_LLIST_FIELDS(AvahiSHostNameResolver, resolver);
58 static void finish(AvahiSHostNameResolver *r, AvahiResolverEvent event) {
62 avahi_time_event_free(r->time_event);
67 case AVAHI_RESOLVER_FOUND: {
70 assert(r->address_record);
72 switch (r->address_record->key->type) {
73 case AVAHI_DNS_TYPE_A:
74 a.proto = AVAHI_PROTO_INET;
75 a.data.ipv4 = r->address_record->data.a.address;
78 case AVAHI_DNS_TYPE_AAAA:
79 a.proto = AVAHI_PROTO_INET6;
80 a.data.ipv6 = r->address_record->data.aaaa.address;
87 r->callback(r, r->interface, r->protocol, AVAHI_RESOLVER_FOUND, r->address_record->key->name, &a, r->flags, r->userdata);
92 case AVAHI_RESOLVER_FAILURE:
94 r->callback(r, r->interface, r->protocol, event, r->host_name, NULL, r->flags, r->userdata);
99 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
100 AvahiSHostNameResolver *r = userdata;
105 avahi_server_set_errno(r->server, AVAHI_ERR_TIMEOUT);
106 finish(r, AVAHI_RESOLVER_FAILURE);
109 static void start_timeout(AvahiSHostNameResolver *r) {
116 avahi_elapse_time(&tv, TIMEOUT_MSEC, 0);
118 r->time_event = avahi_time_event_new(r->server->time_event_queue, &tv, time_event_callback, r);
121 static void record_browser_callback(
122 AvahiSRecordBrowser*rr,
123 AvahiIfIndex interface,
124 AvahiProtocol protocol,
125 AvahiBrowserEvent event,
127 AvahiLookupResultFlags flags,
130 AvahiSHostNameResolver *r = userdata;
137 case AVAHI_BROWSER_NEW:
139 assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
141 if (r->interface > 0 && interface != r->interface)
144 if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
147 if (r->interface <= 0)
148 r->interface = interface;
150 if (r->protocol == AVAHI_PROTO_UNSPEC)
151 r->protocol = protocol;
153 if (!r->address_record) {
154 r->address_record = avahi_record_ref(record);
157 finish(r, AVAHI_RESOLVER_FOUND);
162 case AVAHI_BROWSER_REMOVE:
164 assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
166 if (r->address_record && avahi_record_equal_no_ttl(record, r->address_record)) {
167 avahi_record_unref(r->address_record);
168 r->address_record = NULL;
173 /** Look for a replacement */
174 if (r->record_browser_aaaa)
175 avahi_s_record_browser_restart(r->record_browser_aaaa);
176 if (r->record_browser_a)
177 avahi_s_record_browser_restart(r->record_browser_a);
184 case AVAHI_BROWSER_CACHE_EXHAUSTED:
185 case AVAHI_BROWSER_ALL_FOR_NOW:
189 case AVAHI_BROWSER_FAILURE:
193 if (r->record_browser_aaaa)
194 avahi_s_record_browser_free(r->record_browser_aaaa);
195 if (r->record_browser_a)
196 avahi_s_record_browser_free(r->record_browser_a);
198 r->record_browser_a = r->record_browser_aaaa = NULL;
201 finish(r, AVAHI_RESOLVER_FAILURE);
206 AvahiSHostNameResolver *avahi_s_host_name_resolver_new(
208 AvahiIfIndex interface,
209 AvahiProtocol protocol,
210 const char *host_name,
211 AvahiProtocol aprotocol,
212 AvahiLookupFlags flags,
213 AvahiSHostNameResolverCallback callback,
216 AvahiSHostNameResolver *r;
223 assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
225 if (!AVAHI_IF_VALID(interface)) {
226 avahi_server_set_errno(server, AVAHI_ERR_INVALID_INTERFACE);
230 if (!avahi_is_valid_domain_name(host_name)) {
231 avahi_server_set_errno(server, AVAHI_ERR_INVALID_HOST_NAME);
235 if (!AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST)) {
236 avahi_server_set_errno(server, AVAHI_ERR_INVALID_FLAGS);
240 if (!(r = avahi_new(AvahiSHostNameResolver, 1))) {
241 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
246 r->host_name = avahi_normalize_name_strdup(host_name);
247 r->callback = callback;
248 r->userdata = userdata;
249 r->address_record = NULL;
250 r->interface = interface;
251 r->protocol = protocol;
254 r->record_browser_a = r->record_browser_aaaa = NULL;
256 r->time_event = NULL;
258 AVAHI_LLIST_PREPEND(AvahiSHostNameResolver, resolver, server->host_name_resolvers, r);
260 r->record_browser_aaaa = r->record_browser_a = NULL;
263 if (aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_UNSPEC) {
264 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
265 r->record_browser_a = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, r);
268 if (!r->record_browser_a)
272 if (aprotocol == AVAHI_PROTO_INET6 || aprotocol == AVAHI_PROTO_UNSPEC) {
273 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
274 r->record_browser_aaaa = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, r);
277 if (!r->record_browser_aaaa)
281 assert(r->record_browser_aaaa || r->record_browser_a);
288 avahi_s_host_name_resolver_free(r);
292 void avahi_s_host_name_resolver_free(AvahiSHostNameResolver *r) {
295 AVAHI_LLIST_REMOVE(AvahiSHostNameResolver, resolver, r->server->host_name_resolvers, r);
297 if (r->record_browser_a)
298 avahi_s_record_browser_free(r->record_browser_a);
300 if (r->record_browser_aaaa)
301 avahi_s_record_browser_free(r->record_browser_aaaa);
304 avahi_time_event_free(r->time_event);
306 if (r->address_record)
307 avahi_record_unref(r->address_record);
309 avahi_free(r->host_name);