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
29 #include <avahi-common/domain.h>
30 #include <avahi-common/timeval.h>
31 #include <avahi-common/malloc.h>
32 #include <avahi-common/error.h>
36 struct AvahiSServiceResolver {
41 AvahiProtocol address_protocol;
43 AvahiIfIndex interface;
44 AvahiProtocol protocol;
46 AvahiSRecordBrowser *record_browser_srv;
47 AvahiSRecordBrowser *record_browser_txt;
48 AvahiSRecordBrowser *record_browser_a;
49 AvahiSRecordBrowser *record_browser_aaaa;
51 AvahiRecord *srv_record, *txt_record, *address_record;
53 AvahiSServiceResolverCallback callback;
56 AvahiTimeEvent *time_event;
58 AVAHI_LLIST_FIELDS(AvahiSServiceResolver, resolver);
61 static void finish(AvahiSServiceResolver *r, AvahiResolverEvent event) {
64 if (r->record_browser_a) {
65 avahi_s_record_browser_free(r->record_browser_a);
66 r->record_browser_a = NULL;
69 if (r->record_browser_aaaa) {
70 avahi_s_record_browser_free(r->record_browser_aaaa);
71 r->record_browser_aaaa = NULL;
74 if (r->record_browser_srv) {
75 avahi_s_record_browser_free(r->record_browser_srv);
76 r->record_browser_srv = NULL;
79 if (r->record_browser_txt) {
80 avahi_s_record_browser_free(r->record_browser_txt);
81 r->record_browser_txt = NULL;
85 avahi_time_event_free(r->time_event);
89 if (event == AVAHI_RESOLVER_TIMEOUT)
90 r->callback(r, r->interface, r->protocol, event, r->service_name, r->service_type, r->domain_name, NULL, NULL, 0, NULL, r->userdata);
93 char sn[256], st[256];
96 assert(r->srv_record);
97 assert(r->txt_record);
98 assert(r->address_record);
100 switch (r->address_record->key->type) {
101 case AVAHI_DNS_TYPE_A:
102 a.family = AVAHI_PROTO_INET;
103 a.data.ipv4 = r->address_record->data.a.address;
106 case AVAHI_DNS_TYPE_AAAA:
107 a.family = AVAHI_PROTO_INET6;
108 a.data.ipv6 = r->address_record->data.aaaa.address;
115 snprintf(sn, sizeof(sn), r->service_name);
116 snprintf(st, sizeof(st), r->service_type);
118 if ((i = strlen(sn)) > 0 && sn[i-1] == '.')
121 if ((i = strlen(st)) > 0 && st[i-1] == '.')
124 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);
128 static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
129 AvahiSServiceResolver *r = userdata;
135 if (!(event == AVAHI_BROWSER_NEW))
138 if (r->interface > 0 && interface != r->interface)
141 if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
144 if (r->interface <= 0)
145 r->interface = interface;
147 if (r->protocol == AVAHI_PROTO_UNSPEC)
148 r->protocol = protocol;
150 switch (record->key->type) {
151 case AVAHI_DNS_TYPE_SRV:
152 if (!r->srv_record) {
153 r->srv_record = avahi_record_ref(record);
155 assert(!r->record_browser_a && !r->record_browser_aaaa);
157 if (r->address_protocol == AVAHI_PROTO_INET || r->address_protocol == AVAHI_PROTO_UNSPEC) {
158 AvahiKey *k = avahi_key_new(r->srv_record->data.srv.name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
159 r->record_browser_a = avahi_s_record_browser_new(r->server, r->interface, r->protocol, k, record_browser_callback, r);
163 if (r->address_protocol == AVAHI_PROTO_INET6 || 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_AAAA);
165 r->record_browser_aaaa = avahi_s_record_browser_new(r->server, r->interface, r->protocol, k, record_browser_callback, r);
171 case AVAHI_DNS_TYPE_TXT:
173 r->txt_record = avahi_record_ref(record);
176 case AVAHI_DNS_TYPE_A:
177 case AVAHI_DNS_TYPE_AAAA:
178 if (!r->address_record)
179 r->address_record = avahi_record_ref(record);
186 if (r->txt_record && r->srv_record && r->address_record)
187 finish(r, AVAHI_RESOLVER_FOUND);
190 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
191 AvahiSServiceResolver *r = userdata;
196 finish(r, AVAHI_RESOLVER_TIMEOUT);
199 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) {
200 AvahiSServiceResolver *r;
211 assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
213 if (!avahi_is_valid_service_name(name)) {
214 avahi_server_set_errno(server, AVAHI_ERR_INVALID_SERVICE_NAME);
218 if (!avahi_is_valid_service_type(type)) {
219 avahi_server_set_errno(server, AVAHI_ERR_INVALID_SERVICE_TYPE);
223 if (!avahi_is_valid_domain_name(domain)) {
224 avahi_server_set_errno(server, AVAHI_ERR_INVALID_DOMAIN_NAME);
228 if (!(r = avahi_new(AvahiSServiceResolver, 1))) {
229 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
234 r->service_name = avahi_strdup(name);
235 r->service_type = avahi_normalize_name(type);
236 r->domain_name = avahi_normalize_name(domain);
237 r->callback = callback;
238 r->userdata = userdata;
239 r->address_protocol = aprotocol;
240 r->srv_record = r->txt_record = r->address_record = NULL;
241 r->interface = interface;
242 r->protocol = protocol;
246 avahi_escape_label((const uint8_t*) name, strlen(name), &n, &l);
247 snprintf(n, l, ".%s.%s", r->service_type, r->domain_name);
249 avahi_elapse_time(&tv, 1000, 0);
250 r->time_event = avahi_time_event_new(server->time_event_queue, &tv, time_event_callback, r);
252 AVAHI_LLIST_PREPEND(AvahiSServiceResolver, resolver, server->service_resolvers, r);
254 r->record_browser_a = r->record_browser_aaaa = r->record_browser_srv = r->record_browser_txt = NULL;
256 k = avahi_key_new(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
257 r->record_browser_srv = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
260 if (!r->record_browser_srv) {
261 avahi_s_service_resolver_free(r);
265 k = avahi_key_new(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT);
266 r->record_browser_txt = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
269 if (!r->record_browser_txt) {
270 avahi_s_service_resolver_free(r);
277 void avahi_s_service_resolver_free(AvahiSServiceResolver *r) {
280 AVAHI_LLIST_REMOVE(AvahiSServiceResolver, resolver, r->server->service_resolvers, r);
283 avahi_time_event_free(r->time_event);
285 if (r->record_browser_srv)
286 avahi_s_record_browser_free(r->record_browser_srv);
287 if (r->record_browser_txt)
288 avahi_s_record_browser_free(r->record_browser_txt);
289 if (r->record_browser_a)
290 avahi_s_record_browser_free(r->record_browser_a);
291 if (r->record_browser_aaaa)
292 avahi_s_record_browser_free(r->record_browser_aaaa);
295 avahi_record_unref(r->srv_record);
297 avahi_record_unref(r->txt_record);
298 if (r->address_record)
299 avahi_record_unref(r->address_record);
301 avahi_free(r->service_name);
302 avahi_free(r->service_type);
303 avahi_free(r->domain_name);