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