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
30 #include <avahi-common/domain.h>
31 #include <avahi-common/timeval.h>
32 #include <avahi-common/malloc.h>
33 #include <avahi-common/error.h>
38 #define TIMEOUT_MSEC 5000
40 struct AvahiSServiceResolver {
45 AvahiProtocol address_protocol;
47 AvahiIfIndex interface;
48 AvahiProtocol protocol;
50 AvahiSRecordBrowser *record_browser_srv;
51 AvahiSRecordBrowser *record_browser_txt;
52 AvahiSRecordBrowser *record_browser_a;
53 AvahiSRecordBrowser *record_browser_aaaa;
55 AvahiRecord *srv_record, *txt_record, *address_record;
56 AvahiLookupResultFlags srv_flags, txt_flags, address_flags;
58 AvahiSServiceResolverCallback callback;
60 AvahiLookupFlags user_flags;
62 AvahiTimeEvent *time_event;
64 AVAHI_LLIST_FIELDS(AvahiSServiceResolver, resolver);
67 static void finish(AvahiSServiceResolver *r, AvahiResolverEvent event) {
68 AvahiLookupResultFlags flags;
73 avahi_time_event_free(r->time_event);
83 case AVAHI_RESOLVER_FAILURE:
84 case AVAHI_RESOLVER_NOT_FOUND:
85 case AVAHI_RESOLVER_TIMEOUT:
104 case AVAHI_RESOLVER_FOUND: {
107 assert(event == AVAHI_RESOLVER_FOUND);
109 assert(r->srv_record);
111 if (r->address_record) {
112 switch (r->address_record->key->type) {
113 case AVAHI_DNS_TYPE_A:
114 a.proto = AVAHI_PROTO_INET;
115 a.data.ipv4 = r->address_record->data.a.address;
118 case AVAHI_DNS_TYPE_AAAA:
119 a.proto = AVAHI_PROTO_INET6;
120 a.data.ipv6 = r->address_record->data.aaaa.address;
136 r->address_record ? r->address_record->key->name : r->srv_record->data.srv.name,
137 r->address_record ? &a : NULL,
138 r->srv_record->data.srv.port,
139 r->txt_record ? r->txt_record->data.txt.string_list : NULL,
148 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
149 AvahiSServiceResolver *r = userdata;
154 avahi_log_debug("timeout");
156 finish(r, AVAHI_RESOLVER_TIMEOUT);
159 static void start_timeout(AvahiSServiceResolver *r) {
166 avahi_elapse_time(&tv, TIMEOUT_MSEC, 0);
168 r->time_event = avahi_time_event_new(r->server->time_event_queue, &tv, time_event_callback, r);
171 static void record_browser_callback(
172 AvahiSRecordBrowser*rr,
173 AvahiIfIndex interface,
174 AvahiProtocol protocol,
175 AvahiBrowserEvent event,
177 AvahiLookupResultFlags flags,
180 AvahiSServiceResolver *r = userdata;
185 if (rr == r->record_browser_aaaa || rr == r->record_browser_a)
186 r->address_flags = flags;
187 else if (rr == r->record_browser_srv)
188 r->srv_flags = flags;
189 else if (rr == r->record_browser_txt)
190 r->txt_flags = flags;
194 case AVAHI_BROWSER_NEW: {
198 if (r->interface > 0 && interface > 0 && interface != r->interface)
201 if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
204 if (r->interface <= 0)
205 r->interface = interface;
207 if (r->protocol == AVAHI_PROTO_UNSPEC)
208 r->protocol = protocol;
210 switch (record->key->type) {
211 case AVAHI_DNS_TYPE_SRV:
212 if (!r->srv_record) {
213 r->srv_record = avahi_record_ref(record);
216 if (r->record_browser_a) {
217 avahi_s_record_browser_free(r->record_browser_a);
218 r->record_browser_a = NULL;
221 if (r->record_browser_aaaa) {
222 avahi_s_record_browser_free(r->record_browser_aaaa);
223 r->record_browser_aaaa = NULL;
226 if (!(r->user_flags & AVAHI_LOOKUP_NO_ADDRESS)) {
228 if (r->address_protocol == AVAHI_PROTO_INET || r->address_protocol == AVAHI_PROTO_UNSPEC) {
229 AvahiKey *k = avahi_key_new(r->srv_record->data.srv.name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
230 r->record_browser_a = avahi_s_record_browser_new(r->server, r->interface, r->protocol, k, r->user_flags & ~(AVAHI_LOOKUP_NO_TXT|AVAHI_LOOKUP_NO_ADDRESS), record_browser_callback, r);
234 if (r->address_protocol == AVAHI_PROTO_INET6 || r->address_protocol == AVAHI_PROTO_UNSPEC) {
235 AvahiKey *k = avahi_key_new(r->srv_record->data.srv.name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
236 r->record_browser_aaaa = avahi_s_record_browser_new(r->server, r->interface, r->protocol, k, r->user_flags & ~(AVAHI_LOOKUP_NO_TXT|AVAHI_LOOKUP_NO_ADDRESS), record_browser_callback, r);
243 case AVAHI_DNS_TYPE_TXT:
245 assert(!(r->user_flags & AVAHI_LOOKUP_NO_TXT));
247 if (!r->txt_record) {
248 r->txt_record = avahi_record_ref(record);
253 case AVAHI_DNS_TYPE_A:
254 case AVAHI_DNS_TYPE_AAAA:
256 assert(!(r->user_flags & AVAHI_LOOKUP_NO_ADDRESS));
258 if (!r->address_record) {
259 r->address_record = avahi_record_ref(record);
271 (r->txt_record || (r->user_flags & AVAHI_LOOKUP_NO_TXT)) &&
272 (r->address_record || (r->user_flags & AVAHI_LOOKUP_NO_ADDRESS)))
273 finish(r, AVAHI_RESOLVER_FOUND);
279 case AVAHI_BROWSER_REMOVE:
283 switch (record->key->type) {
284 case AVAHI_DNS_TYPE_SRV:
286 if (r->srv_record && avahi_record_equal_no_ttl(record, r->srv_record)) {
287 avahi_record_unref(r->srv_record);
288 r->srv_record = NULL;
290 if (r->record_browser_a) {
291 avahi_s_record_browser_free(r->record_browser_a);
292 r->record_browser_a = NULL;
295 if (r->record_browser_aaaa) {
296 avahi_s_record_browser_free(r->record_browser_aaaa);
297 r->record_browser_aaaa = NULL;
300 /** Look for a replacement */
301 avahi_s_record_browser_restart(r->record_browser_srv);
307 case AVAHI_DNS_TYPE_TXT:
309 assert(!(r->user_flags & AVAHI_LOOKUP_NO_TXT));
311 if (r->txt_record && avahi_record_equal_no_ttl(record, r->txt_record)) {
312 avahi_record_unref(r->txt_record);
313 r->txt_record = NULL;
315 /** Look for a replacement */
316 avahi_s_record_browser_restart(r->record_browser_txt);
321 case AVAHI_DNS_TYPE_A:
322 case AVAHI_DNS_TYPE_AAAA:
324 assert(!(r->user_flags & AVAHI_LOOKUP_NO_ADDRESS));
326 if (r->address_record && avahi_record_equal_no_ttl(record, r->address_record)) {
327 avahi_record_unref(r->address_record);
328 r->address_record = NULL;
330 /** Look for a replacement */
331 if (r->record_browser_aaaa)
332 avahi_s_record_browser_restart(r->record_browser_aaaa);
333 if (r->record_browser_a)
334 avahi_s_record_browser_restart(r->record_browser_a);
345 case AVAHI_BROWSER_CACHE_EXHAUSTED:
346 case AVAHI_BROWSER_ALL_FOR_NOW:
349 case AVAHI_BROWSER_NOT_FOUND:
350 case AVAHI_BROWSER_FAILURE:
353 if (rr == r->record_browser_a && r->record_browser_aaaa) {
354 /* We were looking for both AAAA and A, and the other query is still living, so we'll not die */
355 avahi_s_record_browser_free(r->record_browser_a);
356 r->record_browser_a = NULL;
360 if (rr == r->record_browser_aaaa && r->record_browser_a) {
361 /* We were looking for both AAAA and A, and the other query is still living, so we'll not die */
362 avahi_s_record_browser_free(r->record_browser_aaaa);
363 r->record_browser_aaaa = NULL;
368 /* Hmm, everything's lost, tell the user */
370 if (r->record_browser_srv)
371 avahi_s_record_browser_free(r->record_browser_srv);
372 if (r->record_browser_txt)
373 avahi_s_record_browser_free(r->record_browser_txt);
374 if (r->record_browser_a)
375 avahi_s_record_browser_free(r->record_browser_a);
376 if (r->record_browser_aaaa)
377 avahi_s_record_browser_free(r->record_browser_aaaa);
379 r->record_browser_srv = r->record_browser_txt = r->record_browser_a = r->record_browser_aaaa = NULL;
381 finish(r, event == AVAHI_BROWSER_FAILURE ? AVAHI_RESOLVER_FAILURE : AVAHI_RESOLVER_NOT_FOUND);
386 AvahiSServiceResolver *avahi_s_service_resolver_new(
388 AvahiIfIndex interface,
389 AvahiProtocol protocol,
393 AvahiProtocol aprotocol,
394 AvahiLookupFlags flags,
395 AvahiSServiceResolverCallback callback,
398 AvahiSServiceResolver *r;
400 char n[AVAHI_DOMAIN_NAME_MAX];
407 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
408 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
409 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(aprotocol), AVAHI_ERR_INVALID_PROTOCOL);
410 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
411 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !name || avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
412 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, avahi_is_valid_service_type(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
413 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST|AVAHI_LOOKUP_NO_TXT|AVAHI_LOOKUP_NO_ADDRESS), AVAHI_ERR_INVALID_FLAGS);
416 domain = server->domain_name;
418 if ((ret = avahi_service_name_join(n, sizeof(n), name, type, domain)) < 0) {
419 avahi_server_set_errno(server, ret);
423 if (!(r = avahi_new(AvahiSServiceResolver, 1))) {
424 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
429 r->service_name = avahi_strdup(name);
430 r->service_type = avahi_normalize_name_strdup(type);
431 r->domain_name = avahi_normalize_name_strdup(domain);
432 r->callback = callback;
433 r->userdata = userdata;
434 r->address_protocol = aprotocol;
435 r->srv_record = r->txt_record = r->address_record = NULL;
436 r->srv_flags = r->txt_flags = r->address_flags = 0;
437 r->interface = interface;
438 r->protocol = protocol;
439 r->user_flags = flags;
440 r->record_browser_a = r->record_browser_aaaa = r->record_browser_srv = r->record_browser_txt = NULL;
441 r->time_event = NULL;
442 AVAHI_LLIST_PREPEND(AvahiSServiceResolver, resolver, server->service_resolvers, r);
444 k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
445 r->record_browser_srv = avahi_s_record_browser_new(server, interface, protocol, k, flags & ~(AVAHI_LOOKUP_NO_TXT|AVAHI_LOOKUP_NO_ADDRESS), record_browser_callback, r);
448 if (!r->record_browser_srv) {
449 avahi_s_service_resolver_free(r);
453 if (!(flags & AVAHI_LOOKUP_NO_TXT)) {
454 k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT);
455 r->record_browser_txt = avahi_s_record_browser_new(server, interface, protocol, k, flags & ~(AVAHI_LOOKUP_NO_TXT|AVAHI_LOOKUP_NO_ADDRESS), record_browser_callback, r);
458 if (!r->record_browser_txt) {
459 avahi_s_service_resolver_free(r);
469 void avahi_s_service_resolver_free(AvahiSServiceResolver *r) {
472 AVAHI_LLIST_REMOVE(AvahiSServiceResolver, resolver, r->server->service_resolvers, r);
475 avahi_time_event_free(r->time_event);
477 if (r->record_browser_srv)
478 avahi_s_record_browser_free(r->record_browser_srv);
479 if (r->record_browser_txt)
480 avahi_s_record_browser_free(r->record_browser_txt);
481 if (r->record_browser_a)
482 avahi_s_record_browser_free(r->record_browser_a);
483 if (r->record_browser_aaaa)
484 avahi_s_record_browser_free(r->record_browser_aaaa);
487 avahi_record_unref(r->srv_record);
489 avahi_record_unref(r->txt_record);
490 if (r->address_record)
491 avahi_record_unref(r->address_record);
493 avahi_free(r->service_name);
494 avahi_free(r->service_type);
495 avahi_free(r->domain_name);