]> git.meshlink.io Git - catta/blob - avahi-core/resolve-host-name.c
Make AVAHI_PROTO_xxx well defined constants
[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 <stdlib.h>
27
28 #include <avahi-common/domain.h>
29 #include <avahi-common/timeval.h>
30 #include <avahi-common/malloc.h>
31 #include <avahi-common/error.h>
32
33 #include "browse.h"
34 #include "log.h"
35
36 #define TIMEOUT_MSEC 1000
37
38 struct AvahiSHostNameResolver {
39     AvahiServer *server;
40     char *host_name;
41     
42     AvahiSRecordBrowser *record_browser_a;
43     AvahiSRecordBrowser *record_browser_aaaa;
44
45     AvahiSHostNameResolverCallback callback;
46     void* userdata;
47
48     AvahiRecord *address_record;
49     AvahiIfIndex interface;
50     AvahiProtocol protocol;
51
52     AvahiTimeEvent *time_event;
53
54     AVAHI_LLIST_FIELDS(AvahiSHostNameResolver, resolver);
55 };
56
57 static void finish(AvahiSHostNameResolver *r, AvahiResolverEvent event) {
58     assert(r);
59
60     if (r->time_event) {
61         avahi_time_event_free(r->time_event);
62         r->time_event = NULL;
63     }
64
65     if (event == AVAHI_RESOLVER_TIMEOUT)
66         r->callback(r, r->interface, r->protocol, AVAHI_RESOLVER_TIMEOUT, r->host_name, NULL, r->userdata);
67     else {
68         AvahiAddress a;
69     
70         assert(event == AVAHI_RESOLVER_FOUND);
71         assert(r->address_record);
72     
73         switch (r->address_record->key->type) {
74             case AVAHI_DNS_TYPE_A:
75                 a.proto = AVAHI_PROTO_INET;
76                 a.data.ipv4 = r->address_record->data.a.address;
77                 break;
78                 
79             case AVAHI_DNS_TYPE_AAAA:
80                 a.proto = AVAHI_PROTO_INET6;
81                 a.data.ipv6 = r->address_record->data.aaaa.address;
82                 break;
83                 
84             default:
85                 abort();
86         }
87
88         r->callback(r, r->interface, r->protocol, AVAHI_RESOLVER_FOUND, r->address_record->key->name, &a, r->userdata);
89     }
90 }
91
92 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
93     AvahiSHostNameResolver *r = userdata;
94     
95     assert(e);
96     assert(r);
97
98     finish(r, AVAHI_RESOLVER_TIMEOUT);
99 }
100
101 static void start_timeout(AvahiSHostNameResolver *r) {
102     struct timeval tv;
103     assert(r);
104
105     if (r->time_event)
106         return;
107
108     avahi_elapse_time(&tv, TIMEOUT_MSEC, 0);
109     r->time_event = avahi_time_event_new(r->server->time_event_queue, &tv, time_event_callback, r);
110 }
111
112 static void record_browser_callback(AvahiSRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
113     AvahiSHostNameResolver *r = userdata;
114
115     assert(rr);
116     assert(record);
117     assert(r);
118
119     assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
120     
121     if (event == AVAHI_BROWSER_NEW) {
122
123         if (r->interface > 0 && interface != r->interface)
124             return;
125         
126         if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
127             return;
128         
129         if (r->interface <= 0)
130             r->interface = interface;
131         
132         if (r->protocol == AVAHI_PROTO_UNSPEC)
133             r->protocol = protocol;
134         
135         if (!r->address_record) {
136             r->address_record = avahi_record_ref(record);
137             
138             finish(r, AVAHI_RESOLVER_FOUND);
139         }
140     } else {
141
142         assert(event == AVAHI_BROWSER_REMOVE);
143
144         if (r->address_record && avahi_record_equal_no_ttl(record, r->address_record)) {
145             avahi_record_unref(r->address_record);
146             r->address_record = NULL;
147
148             /** Look for a replacement */
149             if (r->record_browser_aaaa)
150                 avahi_s_record_browser_restart(r->record_browser_aaaa);
151             if (r->record_browser_a)
152                 avahi_s_record_browser_restart(r->record_browser_a);
153
154             start_timeout(r);
155         }
156     }
157 }
158
159 AvahiSHostNameResolver *avahi_s_host_name_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *host_name, AvahiProtocol aprotocol, AvahiSHostNameResolverCallback callback, void* userdata) {
160     AvahiSHostNameResolver *r;
161     AvahiKey *k;
162     
163     assert(server);
164     assert(host_name);
165     assert(callback);
166
167     assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
168
169     if (!avahi_is_valid_domain_name(host_name)) {
170         avahi_server_set_errno(server, AVAHI_ERR_INVALID_HOST_NAME);
171         return NULL;
172     }
173     
174     if (!(r = avahi_new(AvahiSHostNameResolver, 1))) {
175         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
176         return NULL;
177     }
178     
179     r->server = server;
180     r->host_name = avahi_normalize_name(host_name);
181     r->callback = callback;
182     r->userdata = userdata;
183     r->address_record = NULL;
184     r->interface = interface;
185     r->protocol = protocol;
186
187     r->record_browser_a = r->record_browser_aaaa = NULL;
188
189     r->time_event = NULL;
190     start_timeout(r);
191
192     AVAHI_LLIST_PREPEND(AvahiSHostNameResolver, resolver, server->host_name_resolvers, r);
193
194     r->record_browser_aaaa = r->record_browser_a = NULL;
195     
196     if (aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_UNSPEC) {
197         k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
198         r->record_browser_a = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
199         avahi_key_unref(k);
200
201         if (!r->record_browser_a)
202             goto fail;
203     } 
204
205     if (aprotocol == AVAHI_PROTO_INET6 || aprotocol == AVAHI_PROTO_UNSPEC) {
206         k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
207         r->record_browser_aaaa = avahi_s_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
208         avahi_key_unref(k);
209
210         if (!r->record_browser_aaaa)
211             goto fail;
212     }
213
214     assert(r->record_browser_aaaa || r->record_browser_a);
215
216     return r;
217
218 fail:
219     avahi_s_host_name_resolver_free(r);
220     return NULL;
221 }
222
223 void avahi_s_host_name_resolver_free(AvahiSHostNameResolver *r) {
224     assert(r);
225
226     AVAHI_LLIST_REMOVE(AvahiSHostNameResolver, resolver, r->server->host_name_resolvers, r);
227
228     if (r->record_browser_a)
229         avahi_s_record_browser_free(r->record_browser_a);
230
231     if (r->record_browser_aaaa)
232         avahi_s_record_browser_free(r->record_browser_aaaa);
233
234     if (r->time_event)
235         avahi_time_event_free(r->time_event);
236
237     if (r->address_record)
238         avahi_record_unref(r->address_record);
239     
240     avahi_free(r->host_name);
241     avahi_free(r);
242 }