]> git.meshlink.io Git - catta/blob - avahi-core/resolve-address.c
* drop glib from avahi-common
[catta] / avahi-core / resolve-address.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 AvahiAddressResolver {
30     AvahiServer *server;
31     AvahiAddress address;
32     
33     AvahiRecordBrowser *record_browser;
34
35     AvahiAddressResolverCallback callback;
36     gpointer userdata;
37
38     AvahiTimeEvent *time_event;
39
40     AVAHI_LLIST_FIELDS(AvahiAddressResolver, resolver);
41 };
42
43 static void finish(AvahiAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, AvahiRecord *record) {
44     g_assert(r);
45     
46     avahi_record_browser_free(r->record_browser);
47     r->record_browser = NULL;
48
49     if (r->time_event) {
50         avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
51         r->time_event = NULL;
52     }
53
54     r->callback(r, interface, protocol, event, &r->address, record ? record->data.ptr.name : NULL, r->userdata);
55 }
56
57 static void record_browser_callback(AvahiRecordBrowser*rr, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, AvahiRecord *record, gpointer userdata) {
58     AvahiAddressResolver *r = userdata;
59
60     g_assert(rr);
61     g_assert(record);
62     g_assert(r);
63
64     if (!(event == AVAHI_BROWSER_NEW))
65         return;
66
67     g_assert(record->key->type == AVAHI_DNS_TYPE_PTR);
68
69     finish(r, interface, protocol, AVAHI_RESOLVER_FOUND, record);
70 }
71
72 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
73     AvahiAddressResolver *r = userdata;
74     
75     g_assert(e);
76     g_assert(r);
77
78     finish(r, -1, AVAHI_PROTO_UNSPEC, AVAHI_RESOLVER_TIMEOUT, NULL);
79 }
80
81 AvahiAddressResolver *avahi_address_resolver_new(AvahiServer *server, AvahiIfIndex interface, AvahiProtocol protocol, const AvahiAddress *address, AvahiAddressResolverCallback callback, gpointer userdata) {
82     AvahiAddressResolver *r;
83     AvahiKey *k;
84     gchar *n;
85     struct timeval tv;
86
87     g_assert(server);
88     g_assert(address);
89     g_assert(callback);
90
91     g_assert(address->family == AVAHI_PROTO_INET || address->family == AVAHI_PROTO_INET6);
92
93     r = g_new(AvahiAddressResolver, 1);
94     r->server = server;
95     r->address = *address;
96     r->callback = callback;
97     r->userdata = userdata;
98
99     avahi_elapse_time(&tv, 1000, 0);
100     r->time_event = avahi_time_event_queue_add(server->time_event_queue, &tv, time_event_callback, r);
101
102     AVAHI_LLIST_PREPEND(AvahiAddressResolver, resolver, server->address_resolvers, r);
103     
104     if (address->family == AVAHI_PROTO_INET)
105         n = avahi_reverse_lookup_name_ipv4(&address->data.ipv4);
106     else 
107         n = avahi_reverse_lookup_name_ipv6_arpa(&address->data.ipv6);
108     
109     k = avahi_key_new(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
110     g_free(n);
111     
112     r->record_browser = avahi_record_browser_new(server, interface, protocol, k, record_browser_callback, r);
113     avahi_key_unref(k);
114
115     if (!r->record_browser) {
116         avahi_address_resolver_free(r);
117         return NULL;
118     }
119     
120     return r;
121 }
122
123 void avahi_address_resolver_free(AvahiAddressResolver *r) {
124     g_assert(r);
125
126     AVAHI_LLIST_REMOVE(AvahiAddressResolver, resolver, r->server->address_resolvers, r);
127
128     if (r->record_browser)
129         avahi_record_browser_free(r->record_browser);
130
131     if (r->time_event)
132         avahi_time_event_queue_remove(r->server->time_event_queue, r->time_event);
133     
134     g_free(r);
135 }