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>
30 struct AvahiHostNameResolver {
34 AvahiRecordBrowser *record_browser_a;
35 AvahiRecordBrowser *record_browser_aaaa;
37 AvahiHostNameResolverCallback callback;
40 AvahiTimeEvent *time_event;
42 AVAHI_LLIST_FIELDS(AvahiHostNameResolver, resolver);
45 static void finish(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, AvahiRecord *record) {
50 if (r->record_browser_a) {
51 avahi_record_browser_free(r->record_browser_a);
52 r->record_browser_a = NULL;
55 if (r->record_browser_aaaa) {
56 avahi_record_browser_free(r->record_browser_aaaa);
57 r->record_browser_aaaa = NULL;
61 avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
66 switch (record->key->type) {
67 case AVAHI_DNS_TYPE_A:
68 a.family = AVAHI_PROTO_INET;
69 a.data.ipv4 = record->data.a.address;
72 case AVAHI_DNS_TYPE_AAAA:
73 a.family = AVAHI_PROTO_INET6;
74 a.data.ipv6 = record->data.aaaa.address;
82 r->callback(r, interface, protocol, event, record ? record->key->name : r->host_name, record ? &a : NULL, r->userdata);
85 static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
86 AvahiHostNameResolver *r = userdata;
92 if (!(event == AVAHI_BROWSER_NEW))
95 g_assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
96 finish(r, interface, protocol, AVAHI_RESOLVER_FOUND, record);
99 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
100 AvahiHostNameResolver *r = userdata;
105 finish(r, -1, AVAHI_PROTO_UNSPEC, AVAHI_RESOLVER_TIMEOUT, NULL);
108 AvahiHostNameResolver *avahi_host_name_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const gchar *host_name, guchar aprotocol, AvahiHostNameResolverCallback callback, gpointer userdata) {
109 AvahiHostNameResolver *r;
117 g_assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
119 if (!avahi_valid_domain_name(host_name)) {
120 avahi_server_set_errno(server, AVAHI_ERR_INVALID_HOST_NAME);
124 r = g_new(AvahiHostNameResolver, 1);
126 r->host_name = avahi_normalize_name(host_name);
127 r->callback = callback;
128 r->userdata = userdata;
130 r->record_browser_a = r->record_browser_aaaa = NULL;
132 avahi_elapse_time(&tv, 1000, 0);
133 r->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, time_event_callback, r);
135 AVAHI_LLIST_PREPEND(AvahiHostNameResolver, resolver, server->host_name_resolvers, r);
137 r->record_browser_aaaa = r->record_browser_a = NULL;
139 if (aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_UNSPEC) {
140 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
141 r->record_browser_a = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
144 if (!r->record_browser_a)
148 if (aprotocol == AVAHI_PROTO_INET6 || aprotocol == AVAHI_PROTO_UNSPEC) {
149 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
150 r->record_browser_aaaa = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
153 if (!r->record_browser_aaaa)
157 g_assert(r->record_browser_aaaa || r->record_browser_a);
162 avahi_host_name_resolver_free(r);
166 void avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
169 AVAHI_LLIST_REMOVE(AvahiHostNameResolver, resolver, r->server->host_name_resolvers, r);
171 if (r->record_browser_a)
172 avahi_record_browser_free(r->record_browser_a);
174 if (r->record_browser_aaaa)
175 avahi_record_browser_free(r->record_browser_aaaa);
178 avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
180 g_free(r->host_name);