]> git.meshlink.io Git - catta/blob - avahi-core/resolve-service.c
* Fix service resolving when a service is updated
[catta] / avahi-core / resolve-service.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <avahi-common/domain.h>
31 #include <avahi-common/timeval.h>
32 #include <avahi-common/malloc.h>
33 #include <avahi-common/error.h>
34
35 #include "browse.h"
36
37 #define TIMEOUT_MSEC 1000
38
39 struct AvahiSServiceResolver {
40     AvahiServer *server;
41     char *service_name;
42     char *service_type;
43     char *domain_name;
44     AvahiProtocol address_protocol;
45
46     AvahiIfIndex interface;
47     AvahiProtocol protocol;
48
49     AvahiSRecordBrowser *record_browser_srv;
50     AvahiSRecordBrowser *record_browser_txt;
51     AvahiSRecordBrowser *record_browser_a;
52     AvahiSRecordBrowser *record_browser_aaaa;
53
54     AvahiRecord *srv_record, *txt_record, *address_record;
55     
56     AvahiSServiceResolverCallback callback;
57     void* userdata;
58
59     AvahiTimeEvent *time_event;
60
61     AVAHI_LLIST_FIELDS(AvahiSServiceResolver, resolver);
62 };
63
64 static void finish(AvahiSServiceResolver *r, AvahiResolverEvent event) {
65     assert(r);
66
67     if (r->time_event) {
68         avahi_time_event_free(r->time_event);
69         r->time_event = NULL;
70     }
71
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);
74     else {
75         AvahiAddress a;
76         char sn[256], st[256];
77         size_t i;
78
79         assert(event == AVAHI_RESOLVER_FOUND);
80         
81         assert(r->srv_record);
82         assert(r->txt_record);
83         assert(r->address_record);
84         
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;
89                 break;
90                 
91             case AVAHI_DNS_TYPE_AAAA:
92                 a.family = AVAHI_PROTO_INET6;
93                 a.data.ipv6 = r->address_record->data.aaaa.address;
94                 break;
95                 
96             default:
97                 assert(0);
98         }
99
100         snprintf(sn, sizeof(sn), r->service_name);
101         snprintf(st, sizeof(st), r->service_type);
102
103         if ((i = strlen(sn)) > 0 && sn[i-1] == '.')
104             sn[i-1] = 0;
105
106         if ((i = strlen(st)) > 0 && st[i-1] == '.')
107             st[i-1] = 0;
108
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);
110     }
111 }
112
113 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
114     AvahiSServiceResolver *r = userdata;
115     
116     assert(e);
117     assert(r);
118
119     finish(r, AVAHI_RESOLVER_TIMEOUT);
120 }
121
122 static void start_timeout(AvahiSServiceResolver *r) {
123     struct timeval tv;
124     assert(r);
125
126     if (r->time_event)
127         return;
128     
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);
131 }
132
133 static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
134     AvahiSServiceResolver *r = userdata;
135
136     assert(rr);
137     assert(record);
138     assert(r);
139
140     if (event == AVAHI_BROWSER_NEW) {
141         int changed = 0;
142
143         if (r->interface > 0 && interface != r->interface)
144             return;
145         
146         if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
147             return;
148         
149         if (r->interface <= 0)
150             r->interface = interface;
151         
152         if (r->protocol == AVAHI_PROTO_UNSPEC)
153             r->protocol = protocol;
154         
155         switch (record->key->type) {
156             case AVAHI_DNS_TYPE_SRV:
157                 if (!r->srv_record) {
158                     r->srv_record = avahi_record_ref(record);
159                     changed = 1;
160
161                     if (r->record_browser_a) {
162                         avahi_s_record_browser_free(r->record_browser_a);
163                         r->record_browser_a = NULL;
164                     }
165
166                     if (r->record_browser_aaaa) {
167                         avahi_s_record_browser_free(r->record_browser_aaaa);
168                         r->record_browser_aaaa = NULL;
169                     }
170                     
171                     if (r->address_protocol == AVAHI_PROTO_INET || r->address_protocol == AVAHI_PROTO_UNSPEC) {
172                         AvahiKey *k = avahi_key_new(r->srv_record->data.srv.name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
173                         r->record_browser_a = avahi_s_record_browser_new(r->server, r->interface, r->protocol, k, record_browser_callback, r);
174                         avahi_key_unref(k);
175                     } 
176                     
177                     if (r->address_protocol == AVAHI_PROTO_INET6 || r->address_protocol == AVAHI_PROTO_UNSPEC) {
178                         AvahiKey *k = avahi_key_new(r->srv_record->data.srv.name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
179                         r->record_browser_aaaa = avahi_s_record_browser_new(r->server, r->interface, r->protocol, k, record_browser_callback, r);
180                         avahi_key_unref(k);
181                     }
182                 }
183                 break;
184                 
185             case AVAHI_DNS_TYPE_TXT:
186                 if (!r->txt_record) {
187                     r->txt_record = avahi_record_ref(record);
188                     changed = 1;
189                 }
190                 break;
191                 
192             case AVAHI_DNS_TYPE_A:
193             case AVAHI_DNS_TYPE_AAAA:
194                 if (!r->address_record) {
195                     r->address_record = avahi_record_ref(record);
196                     changed = 1;
197                 }
198                 break;
199                 
200             default:
201                 abort();
202         }
203
204
205         if (changed && r->txt_record && r->srv_record && r->address_record)
206             finish(r, AVAHI_RESOLVER_FOUND);
207
208     } else {
209         assert(event == AVAHI_BROWSER_REMOVE);
210
211         
212         switch (record->key->type) {
213             case AVAHI_DNS_TYPE_SRV:
214
215                 if (r->srv_record && avahi_record_equal_no_ttl(record, r->srv_record)) {
216                     avahi_record_unref(r->srv_record);
217                     r->srv_record = NULL;
218
219                     /** Look for a replacement */
220                     avahi_s_record_browser_restart(r->record_browser_srv);
221                     start_timeout(r);
222                 }
223                 
224                 break;
225
226             case AVAHI_DNS_TYPE_TXT:
227
228                 if (r->txt_record && avahi_record_equal_no_ttl(record, r->txt_record)) {
229                     avahi_record_unref(r->txt_record);
230                     r->txt_record = NULL;
231
232                     /** Look for a replacement */
233                     avahi_s_record_browser_restart(r->record_browser_txt);
234                     start_timeout(r);
235                 }
236                 break;
237
238             case AVAHI_DNS_TYPE_A:
239             case AVAHI_DNS_TYPE_AAAA:
240
241                 if (r->address_record && avahi_record_equal_no_ttl(record, r->address_record)) {
242                     avahi_record_unref(r->address_record);
243                     r->address_record = NULL;
244
245                     /** Look for a replacement */
246                     if (r->record_browser_aaaa)
247                         avahi_s_record_browser_restart(r->record_browser_aaaa);
248                     if (r->record_browser_a)
249                         avahi_s_record_browser_restart(r->record_browser_a);
250                     start_timeout(r);
251                 }
252                 break;
253
254             default:
255                 abort();
256         }
257     }
258 }
259
260 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) {
261     AvahiSServiceResolver *r;
262     AvahiKey *k;
263     char t[256], *n;
264     size_t l;
265     
266     assert(server);
267     assert(name);
268     assert(type);
269     assert(callback);
270
271     assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
272
273     if (!avahi_is_valid_service_name(name)) {
274         avahi_server_set_errno(server, AVAHI_ERR_INVALID_SERVICE_NAME);
275         return NULL;
276     }
277
278     if (!avahi_is_valid_service_type(type)) {
279         avahi_server_set_errno(server, AVAHI_ERR_INVALID_SERVICE_TYPE);
280         return NULL;
281     }
282
283     if (!avahi_is_valid_domain_name(domain)) {
284         avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME);
285         return NULL;
286     }
287     
288     if (!(r = avahi_new(AvahiSServiceResolver, 1))) {
289         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
290         return NULL;
291     }
292     
293     r->server = server;
294     r->service_name = avahi_strdup(name);
295     r->service_type = avahi_normalize_name(type);
296     r->domain_name = avahi_normalize_name(domain);
297     r->callback = callback;
298     r->userdata = userdata;
299     r->address_protocol = aprotocol;
300     r->srv_record = r->txt_record = r->address_record = NULL;
301     r->interface = interface;
302     r->protocol = protocol;
303     
304     n = t;
305     l = sizeof(t);
306     avahi_escape_label((const uint8_t*) name, strlen(name), &n, &l);
307     snprintf(n, l, ".%s.%s", r->service_type, r->domain_name);
308
309     r->time_event = NULL;
310     start_timeout(r);
311     
312     AVAHI_LLIST_PREPEND(AvahiSServiceResolver, resolver, server->service_resolvers, r);
313
314     r->record_browser_a = r->record_browser_aaaa = r->record_browser_srv = r->record_browser_txt = NULL;
315     
316     k = avahi_key_new(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
317     r->record_browser_srv = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
318     avahi_key_unref(k);
319
320     if (!r->record_browser_srv) {
321         avahi_s_service_resolver_free(r);
322         return NULL;
323     }
324     
325     k = avahi_key_new(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT);
326     r->record_browser_txt = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
327     avahi_key_unref(k);
328
329     if (!r->record_browser_txt) {
330         avahi_s_service_resolver_free(r);
331         return NULL;
332     }
333
334     return r;
335 }
336
337 void avahi_s_service_resolver_free(AvahiSServiceResolver *r) {
338     assert(r);
339
340     AVAHI_LLIST_REMOVE(AvahiSServiceResolver, resolver, r->server->service_resolvers, r);
341
342     if (r->time_event)
343         avahi_time_event_free(r->time_event);
344     
345     if (r->record_browser_srv)
346         avahi_s_record_browser_free(r->record_browser_srv);
347     if (r->record_browser_txt)
348         avahi_s_record_browser_free(r->record_browser_txt);
349     if (r->record_browser_a)
350         avahi_s_record_browser_free(r->record_browser_a);
351     if (r->record_browser_aaaa)
352         avahi_s_record_browser_free(r->record_browser_aaaa);
353
354     if (r->srv_record)
355         avahi_record_unref(r->srv_record);
356     if (r->txt_record)
357         avahi_record_unref(r->txt_record);
358     if (r->address_record)
359         avahi_record_unref(r->address_record);
360     
361     avahi_free(r->service_name);
362     avahi_free(r->service_type);
363     avahi_free(r->domain_name);
364     avahi_free(r);
365 }