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