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
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 AvahiSServiceResolver {
43 AvahiProtocol address_protocol;
45 AvahiIfIndex interface;
46 AvahiProtocol protocol;
48 AvahiSRecordBrowser *record_browser_srv;
49 AvahiSRecordBrowser *record_browser_txt;
50 AvahiSRecordBrowser *record_browser_a;
51 AvahiSRecordBrowser *record_browser_aaaa;
53 AvahiRecord *srv_record, *txt_record, *address_record;
54 AvahiLookupResultFlags srv_flags, txt_flags, address_flags;
56 AvahiSServiceResolverCallback callback;
58 AvahiLookupFlags user_flags;
60 AvahiTimeEvent *time_event;
62 AVAHI_LLIST_FIELDS(AvahiSServiceResolver, resolver);
65 static void finish(AvahiSServiceResolver *r, AvahiResolverEvent event) {
66 AvahiLookupResultFlags flags;
71 avahi_time_event_free(r->time_event);
81 case AVAHI_RESOLVER_FAILURE:
100 case AVAHI_RESOLVER_FOUND: {
103 assert(event == AVAHI_RESOLVER_FOUND);
105 assert(r->srv_record);
107 if (r->address_record) {
108 switch (r->address_record->key->type) {
109 case AVAHI_DNS_TYPE_A:
110 a.proto = AVAHI_PROTO_INET;
111 a.data.ipv4 = r->address_record->data.a.address;
114 case AVAHI_DNS_TYPE_AAAA:
115 a.proto = AVAHI_PROTO_INET6;
116 a.data.ipv6 = r->address_record->data.aaaa.address;
132 r->srv_record->data.srv.name,
133 r->address_record ? &a : NULL,
134 r->srv_record->data.srv.port,
135 r->txt_record ? r->txt_record->data.txt.string_list : NULL,
144 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
145 AvahiSServiceResolver *r = userdata;
150 avahi_server_set_errno(r->server, AVAHI_ERR_TIMEOUT);
151 finish(r, AVAHI_RESOLVER_FAILURE);
154 static void start_timeout(AvahiSServiceResolver *r) {
161 avahi_elapse_time(&tv, TIMEOUT_MSEC, 0);
163 r->time_event = avahi_time_event_new(r->server->time_event_queue, &tv, time_event_callback, r);
166 static void record_browser_callback(
167 AvahiSRecordBrowser*rr,
168 AvahiIfIndex interface,
169 AvahiProtocol protocol,
170 AvahiBrowserEvent event,
172 AvahiLookupResultFlags flags,
175 AvahiSServiceResolver *r = userdata;
180 if (rr == r->record_browser_aaaa || rr == r->record_browser_a)
181 r->address_flags = flags;
182 else if (rr == r->record_browser_srv)
183 r->srv_flags = flags;
184 else if (rr == r->record_browser_txt)
185 r->txt_flags = flags;
189 case AVAHI_BROWSER_NEW: {
193 if (r->interface > 0 && interface > 0 && interface != r->interface)
196 if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
199 if (r->interface <= 0)
200 r->interface = interface;
202 if (r->protocol == AVAHI_PROTO_UNSPEC)
203 r->protocol = protocol;
205 switch (record->key->type) {
206 case AVAHI_DNS_TYPE_SRV:
207 if (!r->srv_record) {
208 r->srv_record = avahi_record_ref(record);
211 if (r->record_browser_a) {
212 avahi_s_record_browser_free(r->record_browser_a);
213 r->record_browser_a = NULL;
216 if (r->record_browser_aaaa) {
217 avahi_s_record_browser_free(r->record_browser_aaaa);
218 r->record_browser_aaaa = NULL;
221 if (!(r->user_flags & AVAHI_LOOKUP_NO_ADDRESS)) {
223 if (r->address_protocol == AVAHI_PROTO_INET || r->address_protocol == AVAHI_PROTO_UNSPEC) {
224 AvahiKey *k = avahi_key_new(r->srv_record->data.srv.name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
225 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);
229 if (r->address_protocol == AVAHI_PROTO_INET6 || r->address_protocol == AVAHI_PROTO_UNSPEC) {
230 AvahiKey *k = avahi_key_new(r->srv_record->data.srv.name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
231 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);
238 case AVAHI_DNS_TYPE_TXT:
240 assert(!(r->user_flags & AVAHI_LOOKUP_NO_TXT));
242 if (!r->txt_record) {
243 r->txt_record = avahi_record_ref(record);
248 case AVAHI_DNS_TYPE_A:
249 case AVAHI_DNS_TYPE_AAAA:
251 assert(!(r->user_flags & AVAHI_LOOKUP_NO_ADDRESS));
253 if (!r->address_record) {
254 r->address_record = avahi_record_ref(record);
266 (r->txt_record || (r->user_flags & AVAHI_LOOKUP_NO_TXT)) &&
267 (r->address_record || (r->user_flags & AVAHI_LOOKUP_NO_ADDRESS)))
268 finish(r, AVAHI_RESOLVER_FOUND);
274 case AVAHI_BROWSER_REMOVE:
278 switch (record->key->type) {
279 case AVAHI_DNS_TYPE_SRV:
281 if (r->srv_record && avahi_record_equal_no_ttl(record, r->srv_record)) {
282 avahi_record_unref(r->srv_record);
283 r->srv_record = NULL;
285 if (r->record_browser_a) {
286 avahi_s_record_browser_free(r->record_browser_a);
287 r->record_browser_a = NULL;
290 if (r->record_browser_aaaa) {
291 avahi_s_record_browser_free(r->record_browser_aaaa);
292 r->record_browser_aaaa = NULL;
295 /** Look for a replacement */
296 avahi_s_record_browser_restart(r->record_browser_srv);
302 case AVAHI_DNS_TYPE_TXT:
304 assert(!(r->user_flags & AVAHI_LOOKUP_NO_TXT));
306 if (r->txt_record && avahi_record_equal_no_ttl(record, r->txt_record)) {
307 avahi_record_unref(r->txt_record);
308 r->txt_record = NULL;
310 /** Look for a replacement */
311 avahi_s_record_browser_restart(r->record_browser_txt);
316 case AVAHI_DNS_TYPE_A:
317 case AVAHI_DNS_TYPE_AAAA:
319 assert(!(r->user_flags & AVAHI_LOOKUP_NO_ADDRESS));
321 if (r->address_record && avahi_record_equal_no_ttl(record, r->address_record)) {
322 avahi_record_unref(r->address_record);
323 r->address_record = NULL;
325 /** Look for a replacement */
326 if (r->record_browser_aaaa)
327 avahi_s_record_browser_restart(r->record_browser_aaaa);
328 if (r->record_browser_a)
329 avahi_s_record_browser_restart(r->record_browser_a);
340 case AVAHI_BROWSER_CACHE_EXHAUSTED:
341 case AVAHI_BROWSER_ALL_FOR_NOW:
344 case AVAHI_BROWSER_FAILURE:
346 if (rr == r->record_browser_a && r->record_browser_aaaa) {
347 /* We were looking for both AAAA and A, and the other query is still living, so we'll not die */
348 avahi_s_record_browser_free(r->record_browser_a);
349 r->record_browser_a = NULL;
353 if (rr == r->record_browser_aaaa && r->record_browser_a) {
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_aaaa);
356 r->record_browser_aaaa = NULL;
360 /* Hmm, everything's lost, tell the user */
362 if (r->record_browser_srv)
363 avahi_s_record_browser_free(r->record_browser_srv);
364 if (r->record_browser_txt)
365 avahi_s_record_browser_free(r->record_browser_txt);
366 if (r->record_browser_a)
367 avahi_s_record_browser_free(r->record_browser_a);
368 if (r->record_browser_aaaa)
369 avahi_s_record_browser_free(r->record_browser_aaaa);
371 r->record_browser_srv = r->record_browser_txt = r->record_browser_a = r->record_browser_aaaa = NULL;
373 finish(r, AVAHI_RESOLVER_FAILURE);
378 AvahiSServiceResolver *avahi_s_service_resolver_new(
380 AvahiIfIndex interface,
381 AvahiProtocol protocol,
385 AvahiProtocol aprotocol,
386 AvahiLookupFlags flags,
387 AvahiSServiceResolverCallback callback,
390 AvahiSServiceResolver *r;
392 char n[AVAHI_DOMAIN_NAME_MAX];
399 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_IF_VALID(interface), AVAHI_ERR_INVALID_INTERFACE);
400 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(protocol), AVAHI_ERR_INVALID_PROTOCOL);
401 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, AVAHI_PROTO_VALID(aprotocol), AVAHI_ERR_INVALID_PROTOCOL);
402 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !domain || avahi_is_valid_domain_name(domain), AVAHI_ERR_INVALID_DOMAIN_NAME);
403 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, !name || avahi_is_valid_service_name(name), AVAHI_ERR_INVALID_SERVICE_NAME);
404 AVAHI_CHECK_VALIDITY_RETURN_NULL(server, avahi_is_valid_service_type_strict(type), AVAHI_ERR_INVALID_SERVICE_TYPE);
405 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);
408 domain = server->domain_name;
410 if ((ret = avahi_service_name_join(n, sizeof(n), name, type, domain)) < 0) {
411 avahi_server_set_errno(server, ret);
415 if (!(r = avahi_new(AvahiSServiceResolver, 1))) {
416 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
421 r->service_name = avahi_strdup(name);
422 r->service_type = avahi_normalize_name_strdup(type);
423 r->domain_name = avahi_normalize_name_strdup(domain);
424 r->callback = callback;
425 r->userdata = userdata;
426 r->address_protocol = aprotocol;
427 r->srv_record = r->txt_record = r->address_record = NULL;
428 r->srv_flags = r->txt_flags = r->address_flags = 0;
429 r->interface = interface;
430 r->protocol = protocol;
431 r->user_flags = flags;
432 r->record_browser_a = r->record_browser_aaaa = r->record_browser_srv = r->record_browser_txt = NULL;
433 r->time_event = NULL;
434 AVAHI_LLIST_PREPEND(AvahiSServiceResolver, resolver, server->service_resolvers, r);
436 k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
437 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);
440 if (!r->record_browser_srv) {
441 avahi_s_service_resolver_free(r);
445 if (!(flags & AVAHI_LOOKUP_NO_TXT)) {
446 k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT);
447 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);
450 if (!r->record_browser_txt) {
451 avahi_s_service_resolver_free(r);
461 void avahi_s_service_resolver_free(AvahiSServiceResolver *r) {
464 AVAHI_LLIST_REMOVE(AvahiSServiceResolver, resolver, r->server->service_resolvers, r);
467 avahi_time_event_free(r->time_event);
469 if (r->record_browser_srv)
470 avahi_s_record_browser_free(r->record_browser_srv);
471 if (r->record_browser_txt)
472 avahi_s_record_browser_free(r->record_browser_txt);
473 if (r->record_browser_a)
474 avahi_s_record_browser_free(r->record_browser_a);
475 if (r->record_browser_aaaa)
476 avahi_s_record_browser_free(r->record_browser_aaaa);
479 avahi_record_unref(r->srv_record);
481 avahi_record_unref(r->txt_record);
482 if (r->address_record)
483 avahi_record_unref(r->address_record);
485 avahi_free(r->service_name);
486 avahi_free(r->service_type);
487 avahi_free(r->domain_name);