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
29 struct AvahiHostNameResolver {
33 AvahiRecordBrowser *record_browser_a;
34 AvahiRecordBrowser *record_browser_aaaa;
36 AvahiHostNameResolverCallback callback;
39 AvahiTimeEvent *time_event;
41 AVAHI_LLIST_FIELDS(AvahiHostNameResolver, resolver);
44 static void finish(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, AvahiRecord *record) {
49 if (r->record_browser_a) {
50 avahi_record_browser_free(r->record_browser_a);
51 r->record_browser_a = NULL;
54 if (r->record_browser_aaaa) {
55 avahi_record_browser_free(r->record_browser_aaaa);
56 r->record_browser_aaaa = NULL;
60 avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
65 switch (record->key->type) {
66 case AVAHI_DNS_TYPE_A:
67 a.family = AVAHI_PROTO_INET;
68 a.data.ipv4 = record->data.a.address;
71 case AVAHI_DNS_TYPE_AAAA:
72 a.family = AVAHI_PROTO_INET6;
73 a.data.ipv6 = record->data.aaaa.address;
81 r->callback(r, interface, protocol, event, record ? record->key->name : r->host_name, record ? &a : NULL, r->userdata);
84 static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
85 AvahiHostNameResolver *r = userdata;
91 if (!(event == AVAHI_BROWSER_NEW))
94 g_assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
95 finish(r, interface, protocol, AVAHI_RESOLVER_FOUND, record);
98 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
99 AvahiHostNameResolver *r = userdata;
104 finish(r, -1, AVAHI_PROTO_UNSPEC, AVAHI_RESOLVER_TIMEOUT, NULL);
107 AvahiHostNameResolver *avahi_host_name_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const gchar *host_name, guchar aprotocol, AvahiHostNameResolverCallback callback, gpointer userdata) {
108 AvahiHostNameResolver *r;
116 g_assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
118 r = g_new(AvahiHostNameResolver, 1);
120 r->host_name = avahi_normalize_name(host_name);
121 r->callback = callback;
122 r->userdata = userdata;
124 r->record_browser_a = r->record_browser_aaaa = NULL;
126 avahi_elapse_time(&tv, 1000, 0);
127 r->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, time_event_callback, r);
129 AVAHI_LLIST_PREPEND(AvahiHostNameResolver, resolver, server->host_name_resolvers, r);
131 if (aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_UNSPEC) {
132 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
133 r->record_browser_a = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
137 if (aprotocol == AVAHI_PROTO_INET6 || aprotocol == AVAHI_PROTO_UNSPEC) {
138 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
139 r->record_browser_aaaa = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
146 void avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
149 AVAHI_LLIST_REMOVE(AvahiHostNameResolver, resolver, r->server->host_name_resolvers, r);
151 if (r->record_browser_a)
152 avahi_record_browser_free(r->record_browser_a);
154 if (r->record_browser_aaaa)
155 avahi_record_browser_free(r->record_browser_aaaa);
158 avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
160 g_free(r->host_name);