]> git.meshlink.io Git - catta/blob - avahi-core/resolve-host-name.c
* strip glib from avahi-core
[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 <avahi-common/malloc.h>
29
30 #include "browse.h"
31 #include "log.h"
32
33 struct AvahiHostNameResolver {
34     AvahiServer *server;
35     char *host_name;
36     
37     AvahiRecordBrowser *record_browser_a;
38     AvahiRecordBrowser *record_browser_aaaa;
39
40     AvahiHostNameResolverCallback callback;
41     void* userdata;
42
43     AvahiTimeEvent *time_event;
44
45     AVAHI_LLIST_FIELDS(AvahiHostNameResolver, resolver);
46 };
47
48 static void finish(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, AvahiRecord *record) {
49     AvahiAddress a;
50     
51     assert(r);
52
53     if (r->record_browser_a) {
54         avahi_record_browser_free(r->record_browser_a);
55         r->record_browser_a = NULL;
56     }
57
58     if (r->record_browser_aaaa) {
59         avahi_record_browser_free(r->record_browser_aaaa);
60         r->record_browser_aaaa = NULL;
61     }
62
63     if (r->time_event) {
64         avahi_time_event_free(r->time_event);
65         r->time_event = NULL;
66     }
67
68     if (record) {
69         switch (record->key->type) {
70             case AVAHI_DNS_TYPE_A:
71                 a.family = AVAHI_PROTO_INET;
72                 a.data.ipv4 = record->data.a.address;
73                 break;
74                 
75             case AVAHI_DNS_TYPE_AAAA:
76                 a.family = AVAHI_PROTO_INET6;
77                 a.data.ipv6 = record->data.aaaa.address;
78                 break;
79                 
80             default:
81                 assert(0);
82         }
83     }
84
85     r->callback(r, interface, protocol, event, record ? record->key->name : r->host_name, record ? &a : NULL, r->userdata);
86 }
87
88 static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, void* userdata) {
89     AvahiHostNameResolver *r = userdata;
90
91     assert(rr);
92     assert(record);
93     assert(r);
94
95     if (!(event == AVAHI_BROWSER_NEW))
96         return;
97     
98     assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
99     finish(r, interface, protocol, AVAHI_RESOLVER_FOUND, record);
100 }
101
102 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
103     AvahiHostNameResolver *r = userdata;
104     
105     assert(e);
106     assert(r);
107
108     finish(r, -1, AVAHI_PROTO_UNSPEC, AVAHI_RESOLVER_TIMEOUT, NULL);
109 }
110
111 AvahiHostNameResolver *avahi_host_name_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const char *host_name, AvahiProtocol aprotocol, AvahiHostNameResolverCallback callback, void* userdata) {
112     AvahiHostNameResolver *r;
113     AvahiKey *k;
114     struct timeval tv;
115     
116     assert(server);
117     assert(host_name);
118     assert(callback);
119
120     assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
121
122     if (!avahi_is_valid_domain_name(host_name)) {
123         avahi_server_set_errno(server, AVAHI_ERR_INVALID_HOST_NAME);
124         return NULL;
125     }
126     
127     if (!(r = avahi_new(AvahiHostNameResolver, 1))) {
128         avahi_server_set_errno(server, AVAHI_ERR_NO_MEMORY);
129         return NULL;
130     }
131     
132     r->server = server;
133     r->host_name = avahi_normalize_name(host_name);
134     r->callback = callback;
135     r->userdata = userdata;
136
137     r->record_browser_a = r->record_browser_aaaa = NULL;
138         
139     avahi_elapse_time(&tv, 1000, 0);
140     r->time_event = avahi_time_event_new(server->time_event_queue, &tv, time_event_callback, r);
141
142     AVAHI_LLIST_PREPEND(AvahiHostNameResolver, resolver, server->host_name_resolvers, r);
143
144     r->record_browser_aaaa = r->record_browser_a = NULL;
145     
146     if (aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_UNSPEC) {
147         k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
148         r->record_browser_a = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
149         avahi_key_unref(k);
150
151         if (!r->record_browser_a)
152             goto fail;
153     } 
154
155     if (aprotocol == AVAHI_PROTO_INET6 || aprotocol == AVAHI_PROTO_UNSPEC) {
156         k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
157         r->record_browser_aaaa = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
158         avahi_key_unref(k);
159
160         if (!r->record_browser_aaaa)
161             goto fail;
162     }
163
164     assert(r->record_browser_aaaa || r->record_browser_a);
165
166     return r;
167
168 fail:
169     avahi_host_name_resolver_free(r);
170     return NULL;
171 }
172
173 void avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
174     assert(r);
175
176     AVAHI_LLIST_REMOVE(AvahiHostNameResolver, resolver, r->server->host_name_resolvers, r);
177
178     if (r->record_browser_a)
179         avahi_record_browser_free(r->record_browser_a);
180
181     if (r->record_browser_aaaa)
182         avahi_record_browser_free(r->record_browser_aaaa);
183
184     if (r->time_event)
185         avahi_time_event_free(r->time_event);
186     
187     avahi_free(r->host_name);
188     avahi_free(r);
189 }