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>
37 #define TIMEOUT_MSEC 1000
39 struct AvahiSServiceResolver {
44 AvahiProtocol address_protocol;
46 AvahiIfIndex interface;
47 AvahiProtocol protocol;
49 AvahiSRecordBrowser *record_browser_srv;
50 AvahiSRecordBrowser *record_browser_txt;
51 AvahiSRecordBrowser *record_browser_a;
52 AvahiSRecordBrowser *record_browser_aaaa;
54 AvahiRecord *srv_record, *txt_record, *address_record;
56 AvahiSServiceResolverCallback callback;
59 AvahiTimeEvent *time_event;
61 AVAHI_LLIST_FIELDS(AvahiSServiceResolver, resolver);
64 static void finish(AvahiSServiceResolver *r, AvahiResolverEvent event) {
68 avahi_time_event_free(r->time_event);
72 if (event == AVAHI_RESOLVER_TIMEOUT)
73 r->callback(r, r->interface, r->protocol, event, r->service_name, r->service_type, r->domain_name, NULL, NULL, 0, NULL, r->userdata);
76 char sn[256], st[256];
79 assert(event == AVAHI_RESOLVER_FOUND);
81 assert(r->srv_record);
82 assert(r->txt_record);
83 assert(r->address_record);
85 switch (r->address_record->key->type) {
86 case AVAHI_DNS_TYPE_A:
87 a.family = AVAHI_PROTO_INET;
88 a.data.ipv4 = r->address_record->data.a.address;
91 case AVAHI_DNS_TYPE_AAAA:
92 a.family = AVAHI_PROTO_INET6;
93 a.data.ipv6 = r->address_record->data.aaaa.address;
100 snprintf(sn, sizeof(sn), r->service_name);
101 snprintf(st, sizeof(st), r->service_type);
103 if ((i = strlen(sn)) > 0 && sn[i-1] == '.')
106 if ((i = strlen(st)) > 0 && st[i-1] == '.')
109 r->callback(r, r->interface, r->protocol, event, sn, st, r->domain_name, r->srv_record->data.srv.name, &a, r->srv_record->data.srv.port, r->txt_record->data.txt.string_list, r->userdata);
113 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
114 AvahiSServiceResolver *r = userdata;
119 finish(r, AVAHI_RESOLVER_TIMEOUT);
122 static void start_timeout(AvahiSServiceResolver *r) {
129 avahi_elapse_time(&tv, TIMEOUT_MSEC, 0);
130 r->time_event = avahi_time_event_new(r->server->time_event_queue, &tv, time_event_callback, r);
133 static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
134 AvahiSServiceResolver *r = userdata;
140 if (event == AVAHI_BROWSER_NEW) {
143 if (r->interface > 0 && interface != r->interface)
146 if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
149 if (r->interface <= 0)
150 r->interface = interface;
152 if (r->protocol == AVAHI_PROTO_UNSPEC)
153 r->protocol = protocol;
155 switch (record->key->type) {
156 case AVAHI_DNS_TYPE_SRV:
157 if (!r->srv_record) {
158 r->srv_record = avahi_record_ref(record);
161 assert(!r->record_browser_a && !r->record_browser_aaaa);
163 if (r->address_protocol == AVAHI_PROTO_INET || r->address_protocol == AVAHI_PROTO_UNSPEC) {
164 AvahiKey *k = avahi_key_new(r->srv_record->data.srv.name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
165 r->record_browser_a = avahi_s_record_browser_new(r->server, r->interface, r->protocol, k, record_browser_callback, r);
169 if (r->address_protocol == AVAHI_PROTO_INET6 || r->address_protocol == AVAHI_PROTO_UNSPEC) {
170 AvahiKey *k = avahi_key_new(r->srv_record->data.srv.name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
171 r->record_browser_aaaa = avahi_s_record_browser_new(r->server, r->interface, r->protocol, k, record_browser_callback, r);
177 case AVAHI_DNS_TYPE_TXT:
178 if (!r->txt_record) {
179 r->txt_record = avahi_record_ref(record);
184 case AVAHI_DNS_TYPE_A:
185 case AVAHI_DNS_TYPE_AAAA:
186 if (!r->address_record) {
187 r->address_record = avahi_record_ref(record);
197 if (changed && r->txt_record && r->srv_record && r->address_record)
198 finish(r, AVAHI_RESOLVER_FOUND);
201 assert(event == AVAHI_BROWSER_REMOVE);
204 switch (record->key->type) {
205 case AVAHI_DNS_TYPE_SRV:
207 if (r->srv_record && avahi_record_equal_no_ttl(record, r->srv_record)) {
208 avahi_record_unref(r->srv_record);
209 r->srv_record = NULL;
211 /** Look for a replacement */
212 avahi_s_record_browser_restart(r->record_browser_srv);
218 case AVAHI_DNS_TYPE_TXT:
220 if (r->txt_record && avahi_record_equal_no_ttl(record, r->txt_record)) {
221 avahi_record_unref(r->txt_record);
222 r->txt_record = NULL;
224 /** Look for a replacement */
225 avahi_s_record_browser_restart(r->record_browser_txt);
230 case AVAHI_DNS_TYPE_A:
231 case AVAHI_DNS_TYPE_AAAA:
233 if (r->address_record && avahi_record_equal_no_ttl(record, r->address_record)) {
234 avahi_record_unref(r->address_record);
235 r->address_record = NULL;
237 /** Look for a replacement */
238 if (r->record_browser_aaaa)
239 avahi_s_record_browser_restart(r->record_browser_aaaa);
240 if (r->record_browser_a)
241 avahi_s_record_browser_restart(r->record_browser_a);
252 AvahiSServiceResolver *avahi_s_service_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain, AvahiProtocol aprotocol, AvahiSServiceResolverCallback callback, void* userdata) {
253 AvahiSServiceResolver *r;
263 assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
265 if (!avahi_is_valid_service_name(name)) {
266 avahi_server_set_errno(server, AVAHI_ERR_INVALID_SERVICE_NAME);
270 if (!avahi_is_valid_service_type(type)) {
271 avahi_server_set_errno(server, AVAHI_ERR_INVALID_SERVICE_TYPE);
275 if (!avahi_is_valid_domain_name(domain)) {
276 avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME);
280 if (!(r = avahi_new(AvahiSServiceResolver, 1))) {
281 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
286 r->service_name = avahi_strdup(name);
287 r->service_type = avahi_normalize_name(type);
288 r->domain_name = avahi_normalize_name(domain);
289 r->callback = callback;
290 r->userdata = userdata;
291 r->address_protocol = aprotocol;
292 r->srv_record = r->txt_record = r->address_record = NULL;
293 r->interface = interface;
294 r->protocol = protocol;
298 avahi_escape_label((const uint8_t*) name, strlen(name), &n, &l);
299 snprintf(n, l, ".%s.%s", r->service_type, r->domain_name);
301 r->time_event = NULL;
304 AVAHI_LLIST_PREPEND(AvahiSServiceResolver, resolver, server->service_resolvers, r);
306 r->record_browser_a = r->record_browser_aaaa = r->record_browser_srv = r->record_browser_txt = NULL;
308 k = avahi_key_new(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
309 r->record_browser_srv = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
312 if (!r->record_browser_srv) {
313 avahi_s_service_resolver_free(r);
317 k = avahi_key_new(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT);
318 r->record_browser_txt = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
321 if (!r->record_browser_txt) {
322 avahi_s_service_resolver_free(r);
329 void avahi_s_service_resolver_free(AvahiSServiceResolver *r) {
332 AVAHI_LLIST_REMOVE(AvahiSServiceResolver, resolver, r->server->service_resolvers, r);
335 avahi_time_event_free(r->time_event);
337 if (r->record_browser_srv)
338 avahi_s_record_browser_free(r->record_browser_srv);
339 if (r->record_browser_txt)
340 avahi_s_record_browser_free(r->record_browser_txt);
341 if (r->record_browser_a)
342 avahi_s_record_browser_free(r->record_browser_a);
343 if (r->record_browser_aaaa)
344 avahi_s_record_browser_free(r->record_browser_aaaa);
347 avahi_record_unref(r->srv_record);
349 avahi_record_unref(r->txt_record);
350 if (r->address_record)
351 avahi_record_unref(r->address_record);
353 avahi_free(r->service_name);
354 avahi_free(r->service_type);
355 avahi_free(r->domain_name);