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
31 struct AvahiServiceResolver {
36 guchar address_protocol;
41 AvahiRecordBrowser *record_browser_srv;
42 AvahiRecordBrowser *record_browser_txt;
43 AvahiRecordBrowser *record_browser_a;
44 AvahiRecordBrowser *record_browser_aaaa;
46 AvahiRecord *srv_record, *txt_record, *address_record;
48 AvahiServiceResolverCallback callback;
51 AvahiTimeEvent *time_event;
53 AVAHI_LLIST_FIELDS(AvahiServiceResolver, resolver);
56 static void finish(AvahiServiceResolver *r, AvahiResolverEvent event) {
59 if (r->record_browser_a) {
60 avahi_record_browser_free(r->record_browser_a);
61 r->record_browser_a = NULL;
64 if (r->record_browser_aaaa) {
65 avahi_record_browser_free(r->record_browser_aaaa);
66 r->record_browser_aaaa = NULL;
69 if (r->record_browser_srv) {
70 avahi_record_browser_free(r->record_browser_srv);
71 r->record_browser_srv = NULL;
74 if (r->record_browser_txt) {
75 avahi_record_browser_free(r->record_browser_txt);
76 r->record_browser_txt = NULL;
80 avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
84 if (event == AVAHI_RESOLVER_TIMEOUT)
85 r->callback(r, r->interface, r->protocol, event, r->service_name, r->service_type, r->domain_name, NULL, NULL, 0, NULL, r->userdata);
88 gchar sn[256], st[256];
91 g_assert(r->srv_record);
92 g_assert(r->txt_record);
93 g_assert(r->address_record);
95 switch (r->address_record->key->type) {
96 case AVAHI_DNS_TYPE_A:
97 a.family = AVAHI_PROTO_INET;
98 a.data.ipv4 = r->address_record->data.a.address;
101 case AVAHI_DNS_TYPE_AAAA:
102 a.family = AVAHI_PROTO_INET6;
103 a.data.ipv6 = r->address_record->data.aaaa.address;
110 snprintf(sn, sizeof(sn), r->service_name);
111 snprintf(st, sizeof(st), r->service_type);
113 if ((i = strlen(sn)) > 0 && sn[i-1] == '.')
116 if ((i = strlen(st)) > 0 && st[i-1] == '.')
119 r->callback(r, r->interface, r->protocol, event, sn, st, r->domain_name, r->srv_record->data.srv.name, &a, r->srv_record->data.srv.port, r->txt_record->data.txt.string_list, r->userdata);
124 static void record_browser_callback(AvahiRecordBrowser*rr, gint interface, guchar protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
125 AvahiServiceResolver *r = userdata;
131 if (!(event == AVAHI_BROWSER_NEW))
134 if (r->interface > 0 && interface != r->interface)
137 if (r->protocol != AVAHI_PROTO_UNSPEC && protocol != r->protocol)
140 if (r->interface <= 0)
141 r->interface = interface;
143 if (r->protocol == AVAHI_PROTO_UNSPEC)
144 r->protocol = protocol;
146 switch (record->key->type) {
147 case AVAHI_DNS_TYPE_SRV:
148 if (!r->srv_record) {
149 r->srv_record = avahi_record_ref(record);
151 g_assert(!r->record_browser_a && !r->record_browser_aaaa);
153 if (r->address_protocol == AVAHI_PROTO_INET || r->address_protocol == AVAHI_PROTO_UNSPEC) {
154 AvahiKey *k = avahi_key_new(r->srv_record->data.srv.name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
155 r->record_browser_a = avahi_record_browser_new(r->server, r->interface, r->protocol, k, record_browser_callback, r);
159 if (r->address_protocol == AVAHI_PROTO_INET6 || r->address_protocol == AVAHI_PROTO_UNSPEC) {
160 AvahiKey *k = avahi_key_new(r->srv_record->data.srv.name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
161 r->record_browser_aaaa = avahi_record_browser_new(r->server, r->interface, r->protocol, k, record_browser_callback, r);
167 case AVAHI_DNS_TYPE_TXT:
169 r->txt_record = avahi_record_ref(record);
172 case AVAHI_DNS_TYPE_A:
173 case AVAHI_DNS_TYPE_AAAA:
174 if (!r->address_record)
175 r->address_record = avahi_record_ref(record);
182 if (r->txt_record && r->srv_record && r->address_record)
183 finish(r, AVAHI_RESOLVER_FOUND);
186 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
187 AvahiServiceResolver *r = userdata;
192 finish(r, AVAHI_RESOLVER_TIMEOUT);
195 AvahiServiceResolver *avahi_service_resolver_new(AvahiServer *server, gint interface, guchar protocol, const gchar *name, const gchar *type, const gchar *domain, guchar aprotocol, AvahiServiceResolverCallback callback, gpointer userdata) {
196 AvahiServiceResolver *r;
207 g_assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
209 r = g_new(AvahiServiceResolver, 1);
211 r->service_name = avahi_normalize_name(name);
212 r->service_type = avahi_normalize_name(type);
213 r->domain_name = avahi_normalize_name(domain);
214 r->callback = callback;
215 r->userdata = userdata;
216 r->address_protocol = aprotocol;
217 r->srv_record = r->txt_record = r->address_record = NULL;
218 r->interface = interface;
219 r->protocol = protocol;
223 avahi_escape_label((guint8*) name, strlen(name), &n, &l);
224 snprintf(n, l, ".%s%s", r->service_type, r->domain_name);
226 avahi_elapse_time(&tv, 1000, 0);
227 r->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, time_event_callback, r);
229 AVAHI_LLIST_PREPEND(AvahiServiceResolver, resolver, server->service_resolvers, r);
231 r->record_browser_a = r->record_browser_aaaa = r->record_browser_srv = r->record_browser_txt = NULL;
233 k = avahi_key_new(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
234 r->record_browser_srv = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
237 k = avahi_key_new(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT);
238 r->record_browser_txt = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
244 void avahi_service_resolver_free(AvahiServiceResolver *r) {
247 AVAHI_LLIST_REMOVE(AvahiServiceResolver, resolver, r->server->service_resolvers, r);
250 avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
252 if (r->record_browser_srv)
253 avahi_record_browser_free(r->record_browser_srv);
254 if (r->record_browser_txt)
255 avahi_record_browser_free(r->record_browser_txt);
256 if (r->record_browser_a)
257 avahi_record_browser_free(r->record_browser_a);
258 if (r->record_browser_aaaa)
259 avahi_record_browser_free(r->record_browser_aaaa);
262 avahi_record_unref(r->srv_record);
264 avahi_record_unref(r->txt_record);
265 if (r->address_record)
266 avahi_record_unref(r->address_record);
268 g_free(r->service_name);
269 g_free(r->service_type);
270 g_free(r->domain_name);