]> git.meshlink.io Git - catta/blob - avahi-core/resolve-host-name.c
* update avahi_address_parse() to work with AF_UNSPEC address family
[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, gint interface, guchar 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     avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
60     r->time_event = NULL;
61
62     if (record) {
63         switch (record->key->type) {
64             case AVAHI_DNS_TYPE_A:
65                 a.family = AF_INET;
66                 a.data.ipv4 = record->data.a.address;
67                 break;
68                 
69             case AVAHI_DNS_TYPE_AAAA:
70                 a.family = AF_INET6;
71                 a.data.ipv6 = record->data.aaaa.address;
72                 break;
73                 
74             default:
75                 g_assert(FALSE);
76         }
77     }
78
79     r->callback(r, interface, protocol, event, record ? record->key->name : r->host_name, record ? &a : NULL, r->userdata);
80 }
81
82 static void record_browser_callback(AvahiRecordBrowser*rr, gint interface, guchar protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
83     AvahiHostNameResolver *r = userdata;
84
85     g_assert(rr);
86     g_assert(record);
87     g_assert(r);
88
89     if (!(event == AVAHI_BROWSER_NEW))
90         return;
91     
92     g_assert(record->key->type == AVAHI_DNS_TYPE_A || record->key->type == AVAHI_DNS_TYPE_AAAA);
93     finish(r, interface, protocol, AVAHI_RESOLVER_FOUND, record);
94 }
95
96 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
97     AvahiHostNameResolver *r = userdata;
98     
99     g_assert(e);
100     g_assert(r);
101
102     finish(r, -1, AF_UNSPEC, AVAHI_RESOLVER_TIMEOUT, NULL);
103 }
104
105 AvahiHostNameResolver *avahi_host_name_resolver_new(AvahiServer *server, gint interface, guchar protocol, const gchar *host_name, guchar aprotocol, AvahiHostNameResolverCallback callback, gpointer userdata) {
106     AvahiHostNameResolver *r;
107     AvahiKey *k;
108     GTimeVal tv;
109     
110     g_assert(server);
111     g_assert(host_name);
112     g_assert(callback);
113
114     g_assert(aprotocol == AF_UNSPEC || aprotocol == AF_INET || aprotocol == AF_INET6);
115
116     r = g_new(AvahiHostNameResolver, 1);
117     r->server = server;
118     r->host_name = avahi_normalize_name(host_name);
119     r->callback = callback;
120     r->userdata = userdata;
121
122     r->record_browser_a = r->record_browser_aaaa = NULL;
123         
124     avahi_elapse_time(&tv, 1000, 0);
125     r->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, time_event_callback, r);
126
127     AVAHI_LLIST_PREPEND(AvahiHostNameResolver, resolver, server->host_name_resolvers, r);
128     
129     if (aprotocol == AF_INET || aprotocol == AF_UNSPEC) {
130         k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
131         r->record_browser_a = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
132         avahi_key_unref(k);
133     } 
134
135     if (aprotocol == AF_INET6 || aprotocol == AF_UNSPEC) {
136         k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
137         r->record_browser_aaaa = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
138         avahi_key_unref(k);
139     }
140     
141     return r;
142 }
143
144 void avahi_host_name_resolver_free(AvahiHostNameResolver *r) {
145     g_assert(r);
146
147     AVAHI_LLIST_REMOVE(AvahiHostNameResolver, resolver, r->server->host_name_resolvers, r);
148
149     if (r->record_browser_a)
150         avahi_record_browser_free(r->record_browser_a);
151     if (r->record_browser_aaaa)
152         avahi_record_browser_free(r->record_browser_aaaa);
153     
154     g_free(r->host_name);
155     g_free(r);
156 }