]> git.meshlink.io Git - catta/blob - avahi-core/resolve-host-name.c
* implement hashmap
[catta] / avahi-core / resolve-host-name.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <avahi-common/domain.h>
27 #include <avahi-common/timeval.h>
28 #include "browse.h"
29
30 struct AvahiHostNameResolver {
31     AvahiServer *server;
32     gchar *host_name;
33     
34     AvahiRecordBrowser *record_browser_a;
35     AvahiRecordBrowser *record_browser_aaaa;
36
37     AvahiHostNameResolverCallback callback;
38     gpointer userdata;
39
40     AvahiTimeEvent *time_event;
41
42     AVAHI_LLIST_FIELDS(AvahiHostNameResolver, resolver);
43 };
44
45 static void finish(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, AvahiRecord *record) {
46     AvahiAddress a;
47     
48     g_assert(r);
49
50     if (r->record_browser_a) {
51         avahi_record_browser_free(r->record_browser_a);
52         r->record_browser_a = NULL;
53     }
54
55     if (r->record_browser_aaaa) {
56         avahi_record_browser_free(r->record_browser_aaaa);
57         r->record_browser_aaaa = NULL;
58     }
59
60     if (r->time_event) {
61         avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
62         r->time_event = NULL;
63     }
64
65     if (record) {
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;
70                 break;
71                 
72             case AVAHI_DNS_TYPE_AAAA:
73                 a.family = AVAHI_PROTO_INET6;
74                 a.data.ipv6 = record->data.aaaa.address;
75                 break;
76                 
77             default:
78                 g_assert(FALSE);
79         }
80     }
81
82     r->callback(r, interface, protocol, event, record ? record->key->name : r->host_name, record ? &a : NULL, r->userdata);
83 }
84
85 static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
86     AvahiHostNameResolver *r = userdata;
87
88     g_assert(rr);
89     g_assert(record);
90     g_assert(r);
91
92     if (!(event == AVAHI_BROWSER_NEW))
93         return;
94     
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);
97 }
98
99 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
100     AvahiHostNameResolver *r = userdata;
101     
102     g_assert(e);
103     g_assert(r);
104
105     finish(r, -1, AVAHI_PROTO_UNSPEC, AVAHI_RESOLVER_TIMEOUT, NULL);
106 }
107
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;
110     AvahiKey *k;
111     struct timeval tv;
112     
113     g_assert(server);
114     g_assert(host_name);
115     g_assert(callback);
116
117     g_assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
118
119     if (!avahi_is_valid_domain_name(host_name)) {
120         avahi_server_set_errno(server, AVAHI_ERR_INVALID_HOST_NAME);
121         return NULL;
122     }
123     
124     r = g_new(AvahiHostNameResolver, 1);
125     r->server = server;
126     r->host_name = avahi_normalize_name(host_name);
127     r->callback = callback;
128     r->userdata = userdata;
129
130     r->record_browser_a = r->record_browser_aaaa = NULL;
131         
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);
134
135     AVAHI_LLIST_PREPEND(AvahiHostNameResolver, resolver, server->host_name_resolvers, r);
136
137     r->record_browser_aaaa = r->record_browser_a = NULL;
138     
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);
142         avahi_key_unref(k);
143
144         if (!r->record_browser_a)
145             goto fail;
146     } 
147
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);
151         avahi_key_unref(k);
152
153         if (!r->record_browser_aaaa)
154             goto fail;
155     }
156
157     g_assert(r->record_browser_aaaa || r->record_browser_a);
158
159     return r;
160
161 fail:
162     avahi_host_name_resolver_free(r);
163     return NULL;
164 }
165
166 void avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
167     g_assert(r);
168
169     AVAHI_LLIST_REMOVE(AvahiHostNameResolver, resolver, r->server->host_name_resolvers, r);
170
171     if (r->record_browser_a)
172         avahi_record_browser_free(r->record_browser_a);
173
174     if (r->record_browser_aaaa)
175         avahi_record_browser_free(r->record_browser_aaaa);
176
177     if (r->time_event)
178         avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
179     
180     g_free(r->host_name);
181     g_free(r);
182 }