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>
29 #include <avahi-common/error.h>
34 struct AvahiSHostNameResolver {
38 AvahiSRecordBrowser *record_browser_a;
39 AvahiSRecordBrowser *record_browser_aaaa;
41 AvahiSHostNameResolverCallback callback;
44 AvahiTimeEvent *time_event;
46 AVAHI_LLIST_FIELDS(AvahiSHostNameResolver, resolver);
49 static void finish(AvahiSHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, AvahiRecord *record) {
54 if (r->record_browser_a) {
55 avahi_s_record_browser_free(r->record_browser_a);
56 r->record_browser_a = NULL;
59 if (r->record_browser_aaaa) {
60 avahi_s_record_browser_free(r->record_browser_aaaa);
61 r->record_browser_aaaa = NULL;
65 avahi_time_event_free(r->time_event);
70 switch (record->key->type) {
71 case AVAHI_DNS_TYPE_A:
72 a.family = AVAHI_PROTO_INET;
73 a.data.ipv4 = record->data.a.address;
76 case AVAHI_DNS_TYPE_AAAA:
77 a.family = AVAHI_PROTO_INET6;
78 a.data.ipv6 = record->data.aaaa.address;
86 r->callback(r, interface, protocol, event, record ? record->key->name : r->host_name, record ? &a : NULL, r->userdata);
89 static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
90 AvahiSHostNameResolver *r = userdata;
96 if (!(event == AVAHI_BROWSER_NEW))
99 assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
100 finish(r, interface, protocol, AVAHI_RESOLVER_FOUND, record);
103 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
104 AvahiSHostNameResolver *r = userdata;
109 finish(r, -1, AVAHI_PROTO_UNSPEC, AVAHI_RESOLVER_TIMEOUT, NULL);
112 AvahiSHostNameResolver *avahi_s_host_name_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *host_name, AvahiProtocol aprotocol, AvahiSHostNameResolverCallback callback, void* userdata) {
113 AvahiSHostNameResolver *r;
121 assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
123 if (!avahi_is_valid_domain_name(host_name)) {
124 avahi_server_set_errno(server, AVAHI_ERR_INVALID_HOST_NAME);
128 if (!(r = avahi_new(AvahiSHostNameResolver, 1))) {
129 avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
134 r->host_name = avahi_normalize_name(host_name);
135 r->callback = callback;
136 r->userdata = userdata;
138 r->record_browser_a = r->record_browser_aaaa = NULL;
140 avahi_elapse_time(&tv, 1000, 0);
141 r->time_event = avahi_time_event_new(server->time_event_queue, &tv, time_event_callback, r);
143 AVAHI_LLIST_PREPEND(AvahiSHostNameResolver, resolver, server->host_name_resolvers, r);
145 r->record_browser_aaaa = r->record_browser_a = NULL;
147 if (aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_UNSPEC) {
148 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
149 r->record_browser_a = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
152 if (!r->record_browser_a)
156 if (aprotocol == AVAHI_PROTO_INET6 || aprotocol == AVAHI_PROTO_UNSPEC) {
157 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
158 r->record_browser_aaaa = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
161 if (!r->record_browser_aaaa)
165 assert(r->record_browser_aaaa || r->record_browser_a);
170 avahi_s_host_name_resolver_free(r);
174 void avahi_s_host_name_resolver_free(AvahiSHostNameResolver *r) {
177 AVAHI_LLIST_REMOVE(AvahiSHostNameResolver, resolver, r->server->host_name_resolvers, r);
179 if (r->record_browser_a)
180 avahi_s_record_browser_free(r->record_browser_a);
182 if (r->record_browser_aaaa)
183 avahi_s_record_browser_free(r->record_browser_aaaa);
186 avahi_time_event_free(r->time_event);
188 avahi_free(r->host_name);