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
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 1000
38 struct AvahiSHostNameResolver {
42 AvahiSRecordBrowser *record_browser_a;
43 AvahiSRecordBrowser *record_browser_aaaa;
45 AvahiSHostNameResolverCallback callback;
48 AvahiRecord *address_record;
49 AvahiIfIndex interface;
50 AvahiProtocol protocol;
52 AvahiTimeEvent *time_event;
54 AVAHI_LLIST_FIELDS(AvahiSHostNameResolver, resolver);
57 static void finish(AvahiSHostNameResolver *r, AvahiResolverEvent event) {
61 avahi_time_event_free(r->time_event);
65 if (event == AVAHI_RESOLVER_TIMEOUT)
66 r->callback(r, r->interface, r->protocol, AVAHI_RESOLVER_TIMEOUT, r->host_name, NULL, r->userdata);
70 assert(event == AVAHI_RESOLVER_FOUND);
71 assert(r->address_record);
73 switch (r->address_record->key->type) {
74 case AVAHI_DNS_TYPE_A:
75 a.family = AVAHI_PROTO_INET;
76 a.data.ipv4 = r->address_record->data.a.address;
79 case AVAHI_DNS_TYPE_AAAA:
80 a.family = AVAHI_PROTO_INET6;
81 a.data.ipv6 = r->address_record->data.aaaa.address;
88 r->callback(r, r->interface, r->protocol, AVAHI_RESOLVER_FOUND, r->address_record->key->name, &a, r->userdata);
92 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
93 AvahiSHostNameResolver *r = userdata;
98 finish(r, AVAHI_RESOLVER_TIMEOUT);
101 static void start_timeout(AvahiSHostNameResolver *r) {
108 avahi_elapse_time(&tv, TIMEOUT_MSEC, 0);
109 r->time_event = avahi_time_event_new(r->server->time_event_queue, &tv, time_event_callback, r);
112 static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
113 AvahiSHostNameResolver *r = userdata;
119 assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
121 if (event == AVAHI_BROWSER_NEW) {
123 if (r->interface > 0 && interface != r->interface)
126 if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
129 if (r->interface <= 0)
130 r->interface = interface;
132 if (r->protocol == AVAHI_PROTO_UNSPEC)
133 r->protocol = protocol;
135 if (!r->address_record) {
136 r->address_record = avahi_record_ref(record);
138 finish(r, AVAHI_RESOLVER_FOUND);
142 assert(event == AVAHI_BROWSER_REMOVE);
144 if (r->address_record && avahi_record_equal_no_ttl(record, r->address_record)) {
145 avahi_record_unref(r->address_record);
146 r->address_record = NULL;
148 /** Look for a replacement */
149 if (r->record_browser_aaaa)
150 avahi_s_record_browser_restart(r->record_browser_aaaa);
151 if (r->record_browser_a)
152 avahi_s_record_browser_restart(r->record_browser_a);
159 AvahiSHostNameResolver *avahi_s_host_name_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *host_name, AvahiProtocol aprotocol, AvahiSHostNameResolverCallback callback, void* userdata) {
160 AvahiSHostNameResolver *r;
167 assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
169 if (!avahi_is_valid_domain_name(host_name)) {
170 avahi_server_set_errno(server, AVAHI_ERR_INVALID_HOST_NAME);
174 if (!(r = avahi_new(AvahiSHostNameResolver, 1))) {
175 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
180 r->host_name = avahi_normalize_name(host_name);
181 r->callback = callback;
182 r->userdata = userdata;
183 r->address_record = NULL;
184 r->interface = interface;
185 r->protocol = protocol;
187 r->record_browser_a = r->record_browser_aaaa = NULL;
189 r->time_event = NULL;
192 AVAHI_LLIST_PREPEND(AvahiSHostNameResolver, resolver, server->host_name_resolvers, r);
194 r->record_browser_aaaa = r->record_browser_a = NULL;
196 if (aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_UNSPEC) {
197 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
198 r->record_browser_a = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
201 if (!r->record_browser_a)
205 if (aprotocol == AVAHI_PROTO_INET6 || aprotocol == AVAHI_PROTO_UNSPEC) {
206 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
207 r->record_browser_aaaa = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
210 if (!r->record_browser_aaaa)
214 assert(r->record_browser_aaaa || r->record_browser_a);
219 avahi_s_host_name_resolver_free(r);
223 void avahi_s_host_name_resolver_free(AvahiSHostNameResolver *r) {
226 AVAHI_LLIST_REMOVE(AvahiSHostNameResolver, resolver, r->server->host_name_resolvers, r);
228 if (r->record_browser_a)
229 avahi_s_record_browser_free(r->record_browser_a);
231 if (r->record_browser_aaaa)
232 avahi_s_record_browser_free(r->record_browser_aaaa);
235 avahi_time_event_free(r->time_event);
237 if (r->address_record)
238 avahi_record_unref(r->address_record);
240 avahi_free(r->host_name);