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