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/domain.h>
27 #include <avahi-common/timeval.h>
28 #include <avahi-common/malloc.h>
33 struct AvahiSHostNameResolver {
37 AvahiSRecordBrowser *record_browser_a;
38 AvahiSRecordBrowser *record_browser_aaaa;
40 AvahiSHostNameResolverCallback callback;
43 AvahiTimeEvent *time_event;
45 AVAHI_LLIST_FIELDS(AvahiSHostNameResolver, resolver);
48 static void finish(AvahiSHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, AvahiRecord *record) {
53 if (r->record_browser_a) {
54 avahi_s_record_browser_free(r->record_browser_a);
55 r->record_browser_a = NULL;
58 if (r->record_browser_aaaa) {
59 avahi_s_record_browser_free(r->record_browser_aaaa);
60 r->record_browser_aaaa = NULL;
64 avahi_time_event_free(r->time_event);
69 switch (record->key->type) {
70 case AVAHI_DNS_TYPE_A:
71 a.family = AVAHI_PROTO_INET;
72 a.data.ipv4 = record->data.a.address;
75 case AVAHI_DNS_TYPE_AAAA:
76 a.family = AVAHI_PROTO_INET6;
77 a.data.ipv6 = record->data.aaaa.address;
85 r->callback(r, interface, protocol, event, record ? record->key->name : r->host_name, record ? &a : NULL, r->userdata);
88 static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
89 AvahiSHostNameResolver *r = userdata;
95 if (!(event == AVAHI_BROWSER_NEW))
98 assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
99 finish(r, interface, protocol, AVAHI_RESOLVER_FOUND, record);
102 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
103 AvahiSHostNameResolver *r = userdata;
108 finish(r, -1, AVAHI_PROTO_UNSPEC, AVAHI_RESOLVER_TIMEOUT, NULL);
111 AvahiSHostNameResolver *avahi_s_host_name_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *host_name, AvahiProtocol aprotocol, AvahiSHostNameResolverCallback callback, void* userdata) {
112 AvahiSHostNameResolver *r;
120 assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
122 if (!avahi_is_valid_domain_name(host_name)) {
123 avahi_server_set_errno(server, AVAHI_ERR_INVALID_HOST_NAME);
127 if (!(r = avahi_new(AvahiSHostNameResolver, 1))) {
128 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
133 r->host_name = avahi_normalize_name(host_name);
134 r->callback = callback;
135 r->userdata = userdata;
137 r->record_browser_a = r->record_browser_aaaa = NULL;
139 avahi_elapse_time(&tv, 1000, 0);
140 r->time_event = avahi_time_event_new(server->time_event_queue, &tv, time_event_callback, r);
142 AVAHI_LLIST_PREPEND(AvahiSHostNameResolver, resolver, server->host_name_resolvers, r);
144 r->record_browser_aaaa = r->record_browser_a = NULL;
146 if (aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_UNSPEC) {
147 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
148 r->record_browser_a = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
151 if (!r->record_browser_a)
155 if (aprotocol == AVAHI_PROTO_INET6 || aprotocol == AVAHI_PROTO_UNSPEC) {
156 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
157 r->record_browser_aaaa = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
160 if (!r->record_browser_aaaa)
164 assert(r->record_browser_aaaa || r->record_browser_a);
169 avahi_s_host_name_resolver_free(r);
173 void avahi_s_host_name_resolver_free(AvahiSHostNameResolver *r) {
176 AVAHI_LLIST_REMOVE(AvahiSHostNameResolver, resolver, r->server->host_name_resolvers, r);
178 if (r->record_browser_a)
179 avahi_s_record_browser_free(r->record_browser_a);
181 if (r->record_browser_aaaa)
182 avahi_s_record_browser_free(r->record_browser_aaaa);
185 avahi_time_event_free(r->time_event);
187 avahi_free(r->host_name);