]> git.meshlink.io Git - catta/blob - avahi-core/resolve-host-name.c
* replace guchar and gint by AvahiProtocol, AvahiIfIndex at many places where it...
[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 "browse.h"
27 #include "util.h"
28
29 struct AvahiHostNameResolver {
30     AvahiServer *server;
31     gchar *host_name;
32     
33     AvahiRecordBrowser *record_browser_a;
34     AvahiRecordBrowser *record_browser_aaaa;
35
36     AvahiHostNameResolverCallback callback;
37     gpointer userdata;
38
39     AvahiTimeEvent *time_event;
40
41     AVAHI_LLIST_FIELDS(AvahiHostNameResolver, resolver);
42 };
43
44 static void finish(AvahiHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, AvahiRecord *record) {
45     AvahiAddress a;
46     
47     g_assert(r);
48
49     if (r->record_browser_a) {
50         avahi_record_browser_free(r->record_browser_a);
51         r->record_browser_a = NULL;
52     }
53
54     if (r->record_browser_aaaa) {
55         avahi_record_browser_free(r->record_browser_aaaa);
56         r->record_browser_aaaa = NULL;
57     }
58
59     if (r->time_event) {
60         avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
61         r->time_event = NULL;
62     }
63
64     if (record) {
65         switch (record->key->type) {
66             case AVAHI_DNS_TYPE_A:
67                 a.family = AVAHI_PROTO_INET;
68                 a.data.ipv4 = record->data.a.address;
69                 break;
70                 
71             case AVAHI_DNS_TYPE_AAAA:
72                 a.family = AVAHI_PROTO_INET6;
73                 a.data.ipv6 = record->data.aaaa.address;
74                 break;
75                 
76             default:
77                 g_assert(FALSE);
78         }
79     }
80
81     r->callback(r, interface, protocol, event, record ? record->key->name : r->host_name, record ? &a : NULL, r->userdata);
82 }
83
84 static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
85     AvahiHostNameResolver *r = userdata;
86
87     g_assert(rr);
88     g_assert(record);
89     g_assert(r);
90
91     if (!(event == AVAHI_BROWSER_NEW))
92         return;
93     
94     g_assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
95     finish(r, interface, protocol, AVAHI_RESOLVER_FOUND, record);
96 }
97
98 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
99     AvahiHostNameResolver *r = userdata;
100     
101     g_assert(e);
102     g_assert(r);
103
104     finish(r, -1, AVAHI_PROTO_UNSPEC, AVAHI_RESOLVER_TIMEOUT, NULL);
105 }
106
107 AvahiHostNameResolver *avahi_host_name_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const gchar *host_name, guchar aprotocol, AvahiHostNameResolverCallback callback, gpointer userdata) {
108     AvahiHostNameResolver *r;
109     AvahiKey *k;
110     GTimeVal tv;
111     
112     g_assert(server);
113     g_assert(host_name);
114     g_assert(callback);
115
116     g_assert(aprotocol == AVAHI_PROTO_UNSPEC || aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_INET6);
117
118     r = g_new(AvahiHostNameResolver, 1);
119     r->server = server;
120     r->host_name = avahi_normalize_name(host_name);
121     r->callback = callback;
122     r->userdata = userdata;
123
124     r->record_browser_a = r->record_browser_aaaa = NULL;
125         
126     avahi_elapse_time(&tv, 1000, 0);
127     r->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, time_event_callback, r);
128
129     AVAHI_LLIST_PREPEND(AvahiHostNameResolver, resolver, server->host_name_resolvers, r);
130     
131     if (aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_UNSPEC) {
132         k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
133         r->record_browser_a = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
134         avahi_key_unref(k);
135     } 
136
137     if (aprotocol == AVAHI_PROTO_INET6 || aprotocol == AVAHI_PROTO_UNSPEC) {
138         k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
139         r->record_browser_aaaa = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
140         avahi_key_unref(k);
141     }
142     
143     return r;
144 }
145
146 void avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
147     g_assert(r);
148
149     AVAHI_LLIST_REMOVE(AvahiHostNameResolver, resolver, r->server->host_name_resolvers, r);
150
151     if (r->record_browser_a)
152         avahi_record_browser_free(r->record_browser_a);
153
154     if (r->record_browser_aaaa)
155         avahi_record_browser_free(r->record_browser_aaaa);
156
157     if (r->time_event)
158         avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
159     
160     g_free(r->host_name);
161     g_free(r);
162 }