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
26 #include <avahi-common/timeval.h>
27 #include <avahi-common/malloc.h>
28 #include <avahi-common/error.h>
32 #define TIMEOUT_MSEC 5000
34 struct AvahiSAddressResolver {
38 AvahiSRecordBrowser *record_browser;
40 AvahiSAddressResolverCallback callback;
43 AvahiRecord *ptr_record;
44 AvahiIfIndex interface;
45 AvahiProtocol protocol;
46 AvahiLookupResultFlags flags;
48 int retry_with_multicast;
51 AvahiTimeEvent *time_event;
53 AVAHI_LLIST_FIELDS(AvahiSAddressResolver, resolver);
56 static void finish(AvahiSAddressResolver *r, AvahiResolverEvent event) {
60 avahi_time_event_free(r->time_event);
65 case AVAHI_RESOLVER_FAILURE:
66 r->callback(r, r->interface, r->protocol, event, &r->address, NULL, r->flags, r->userdata);
69 case AVAHI_RESOLVER_FOUND:
70 assert(r->ptr_record);
71 r->callback(r, r->interface, r->protocol, event, &r->address, r->ptr_record->data.ptr.name, r->flags, r->userdata);
76 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
77 AvahiSAddressResolver *r = userdata;
82 avahi_server_set_errno(r->server, AVAHI_ERR_TIMEOUT);
83 finish(r, AVAHI_RESOLVER_FAILURE);
86 static void start_timeout(AvahiSAddressResolver *r) {
93 avahi_elapse_time(&tv, TIMEOUT_MSEC, 0);
94 r->time_event = avahi_time_event_new(r->server->time_event_queue, &tv, time_event_callback, r);
97 static void record_browser_callback(
98 AvahiSRecordBrowser*rr,
99 AvahiIfIndex interface,
100 AvahiProtocol protocol,
101 AvahiBrowserEvent event,
103 AvahiLookupResultFlags flags,
106 AvahiSAddressResolver *r = userdata;
113 case AVAHI_BROWSER_NEW:
115 assert(record->key->type == AVAHI_DNS_TYPE_PTR);
117 if (r->interface > 0 && interface != r->interface)
120 if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
123 if (r->interface <= 0)
124 r->interface = interface;
126 if (r->protocol == AVAHI_PROTO_UNSPEC)
127 r->protocol = protocol;
129 if (!r->ptr_record) {
130 r->ptr_record = avahi_record_ref(record);
133 finish(r, AVAHI_RESOLVER_FOUND);
137 case AVAHI_BROWSER_REMOVE:
139 assert(record->key->type == AVAHI_DNS_TYPE_PTR);
141 if (r->ptr_record && avahi_record_equal_no_ttl(record, r->ptr_record)) {
142 avahi_record_unref(r->ptr_record);
143 r->ptr_record = NULL;
146 /** Look for a replacement */
147 avahi_s_record_browser_restart(r->record_browser);
153 case AVAHI_BROWSER_CACHE_EXHAUSTED:
154 case AVAHI_BROWSER_ALL_FOR_NOW:
157 case AVAHI_BROWSER_FAILURE:
159 if (r->retry_with_multicast) {
160 r->retry_with_multicast = 0;
162 avahi_s_record_browser_free(r->record_browser);
163 r->record_browser = avahi_s_record_browser_new(r->server, r->interface, r->protocol, r->key, AVAHI_LOOKUP_USE_MULTICAST, record_browser_callback, r);
165 if (r->record_browser) {
172 finish(r, AVAHI_RESOLVER_FAILURE);
177 AvahiSAddressResolver *avahi_s_address_resolver_new(
179 AvahiIfIndex interface,
180 AvahiProtocol protocol,
181 const AvahiAddress *address,
182 AvahiLookupFlags flags,
183 AvahiSAddressResolverCallback callback,
186 AvahiSAddressResolver *r;
194 assert(address->proto == AVAHI_PROTO_INET || address->proto == AVAHI_PROTO_INET6);
196 if (!AVAHI_IF_VALID(interface)) {
197 avahi_server_set_errno(server, AVAHI_ERR_INVALID_INTERFACE);
201 if (!AVAHI_FLAGS_VALID(flags, AVAHI_LOOKUP_USE_WIDE_AREA|AVAHI_LOOKUP_USE_MULTICAST)) {
202 avahi_server_set_errno(server, AVAHI_ERR_INVALID_FLAGS);
206 if (address->proto == AVAHI_PROTO_INET)
207 n = avahi_reverse_lookup_name_ipv4(&address->data.ipv4);
209 n = avahi_reverse_lookup_name_ipv6_arpa(&address->data.ipv6);
212 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
216 k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
220 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
224 if (!(r = avahi_new(AvahiSAddressResolver, 1))) {
225 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
231 r->address = *address;
232 r->callback = callback;
233 r->userdata = userdata;
234 r->ptr_record = NULL;
235 r->interface = interface;
236 r->protocol = protocol;
238 r->retry_with_multicast = 0;
241 r->record_browser = NULL;
242 AVAHI_LLIST_PREPEND(AvahiSAddressResolver, resolver, server->address_resolvers, r);
244 r->time_event = NULL;
246 if (!(flags & (AVAHI_LOOKUP_USE_MULTICAST|AVAHI_LOOKUP_USE_WIDE_AREA))) {
248 if (!server->wide_area_lookup_engine || !avahi_wide_area_has_servers(server->wide_area_lookup_engine))
249 flags |= AVAHI_LOOKUP_USE_MULTICAST;
251 flags |= AVAHI_LOOKUP_USE_WIDE_AREA;
252 r->retry_with_multicast = 1;
256 r->record_browser = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, r);
258 if (!r->record_browser) {
259 avahi_s_address_resolver_free(r);
268 void avahi_s_address_resolver_free(AvahiSAddressResolver *r) {
271 AVAHI_LLIST_REMOVE(AvahiSAddressResolver, resolver, r->server->address_resolvers, r);
273 if (r->record_browser)
274 avahi_s_record_browser_free(r->record_browser);
277 avahi_time_event_free(r->time_event);
280 avahi_record_unref(r->ptr_record);
283 avahi_key_unref(r->key);