1 --- /usr/tmp/clean/avahi-0.6.28/avahi-core/resolve-host-name.c 2010-08-26 01:51:38.991153000 +0100
2 +++ avahi-0.6.28/avahi-core/resolve-host-name.c 2011-01-20 15:06:17.971781264 +0000
9 #include <avahi-common/domain.h>
10 #include <avahi-common/timeval.h>
13 AvahiTimeEvent *time_event;
16 + AvahiTimeEvent *defer_time_event;
17 + AvahiWatch *watch_a;
18 + AvahiWatch *watch_a6;
19 + AvahiLookupFlags lookup_flags;
20 + DNSServiceRef client_a;
21 + DNSServiceRef client_a6;
24 AVAHI_LLIST_FIELDS(AvahiSHostNameResolver, resolver);
33 case AVAHI_RESOLVER_FAILURE:
35 r->callback(r, r->interface, r->protocol, event, r->host_name, NULL, r->flags, r->userdata);
41 +static void resolve_socket_event(AvahiWatch *w, int fd, AvahiWatchEvent events,
43 + AvahiSHostNameResolver *r = userdata;
44 + DNSServiceErrorType ret;
45 + DNSServiceRef client;
49 + assert(events & AVAHI_WATCH_IN);
51 + if (fd == DNSServiceRefSockFD(r->client_a))
52 + client = r->client_a;
53 + else if (fd == DNSServiceRefSockFD(r->client_a6))
54 + client = r->client_a6;
58 + ret = DNSServiceProcessResult(client);
59 + if (ret != kDNSServiceErr_NoError) {
60 + if (client == r->client_a) {
62 + r->server->poll_api->watch_free(r->watch_a);
65 + DNSServiceRefDeallocate(r->client_a);
67 + } else if (client == r->client_a6) {
69 + r->server->poll_api->watch_free(r->watch_a6);
72 + DNSServiceRefDeallocate(r->client_a6);
73 + r->client_a6 = NULL;
75 + avahi_server_set_errno(r->server, AVAHI_ERR_DISCONNECTED);
76 + finish(r, AVAHI_RESOLVER_FAILURE);
80 +static void resolve_reply(DNSServiceRef client, DNSServiceFlags flags, uint32_t IfIndex, DNSServiceErrorType errorCode, const char *fullname, uint16_t rrtype, uint16_t rrclass, uint16_t rdlen, const void* rdata, uint32_t ttl, void *context) {
81 + AvahiSHostNameResolver *r = context;
82 + const unsigned char *rd = rdata;
85 + assert(rrtype == kDNSServiceType_A || rrtype == kDNSServiceType_AAAA);
86 + if (!flags & kDNSServiceFlagsAdd)
87 + /* this is a remove event so ignore*/
90 + if (r->interface > 0 && IfIndex != r->interface)
92 + if (r->interface <= 0)
93 + r->interface = IfIndex;
96 + * Using Bonjour we cannot determine whether result was obtained from
97 + * multicast ot unicast query
102 + case kDNSServiceType_A:
103 + if (r->protocol == AVAHI_PROTO_UNSPEC)
104 + r->protocol = AVAHI_PROTO_INET;
105 + else if (r->protocol != AVAHI_PROTO_INET)
108 + if (!r->address_record) {
109 + if (!(r->address_record = avahi_record_new_full(r->host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, ttl))) {
110 + avahi_server_set_errno(r->server, AVAHI_ERR_NO_MEMORY);
111 + finish(r, AVAHI_RESOLVER_FAILURE);
114 + if (avahi_rdata_parse(r->address_record, rdata, rdlen) != 0) {
115 + avahi_server_set_errno(r->server, AVAHI_ERR_INVALID_PACKET);
116 + finish(r, AVAHI_RESOLVER_FAILURE);
120 + case kDNSServiceType_AAAA:
121 + if (r->protocol == AVAHI_PROTO_UNSPEC)
122 + r->protocol = AVAHI_PROTO_INET6;
123 + else if (r->protocol != AVAHI_PROTO_INET6)
125 + if (!r->address_record) {
126 + if (!(r->address_record = avahi_record_new_full(r->host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, ttl))) {
127 + avahi_server_set_errno(r->server, AVAHI_ERR_NO_MEMORY);
128 + finish(r, AVAHI_RESOLVER_FAILURE);
131 + if (avahi_rdata_parse(r->address_record, rdata, rdlen) != 0) {
132 + avahi_server_set_errno(r->server, AVAHI_ERR_INVALID_PACKET);
133 + finish(r, AVAHI_RESOLVER_FAILURE);
141 + finish(r, AVAHI_RESOLVER_FOUND);
144 +static void resolve_error_callback(AvahiTimeEvent *e, void *userdata) {
145 + AvahiSHostNameResolver *r = userdata;
147 + if (r->defer_time_event) {
148 + avahi_time_event_free(r->defer_time_event);
149 + r->defer_time_event = NULL;
151 + avahi_server_set_errno(r->server, AVAHI_ERR_FAILURE);
152 + finish(r, AVAHI_RESOLVER_FAILURE);
155 +static void avahi_resolve_host_name_start(AvahiSHostNameResolver *r, AvahiProtocol aprotocol)
157 + DNSServiceErrorType ret;
158 + DNSServiceFlags flags;
160 + if (r->flags == AVAHI_LOOKUP_USE_MULTICAST)
161 + flags = kDNSServiceFlagsForceMulticast;
163 + if (aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_UNSPEC) {
164 + ret = DNSServiceQueryRecord(&r->client_a,
166 + r->interface == AVAHI_IF_UNSPEC ?
167 + kDNSServiceInterfaceIndexAny :
171 + kDNSServiceClass_IN,
174 + if (ret != kDNSServiceErr_NoError || !r->client_a) {
175 + r->defer_time_event = avahi_time_event_new(r->server->time_event_queue, NULL, resolve_error_callback, r);
178 + r->watch_a = r->server->poll_api->watch_new(r->server->poll_api, DNSServiceRefSockFD(r->client_a), AVAHI_WATCH_IN, resolve_socket_event, r);
181 + if (aprotocol == AVAHI_PROTO_INET6 || aprotocol == AVAHI_PROTO_UNSPEC) {
182 + ret = DNSServiceQueryRecord(&r->client_a6,
184 + r->interface == AVAHI_IF_UNSPEC ?
185 + kDNSServiceInterfaceIndexAny :
188 + kDNSServiceType_AAAA,
189 + kDNSServiceClass_IN,
192 + if (ret != kDNSServiceErr_NoError || !r->client_a6) {
193 + r->defer_time_event = avahi_time_event_new(r->server->time_event_queue, NULL, resolve_error_callback, r);
196 + r->watch_a6 = r->server->poll_api->watch_new(r->server->poll_api, DNSServiceRefSockFD(r->client_a6), AVAHI_WATCH_IN, resolve_socket_event, r);
202 static void time_event_callback(AvahiTimeEvent *e, void *userdata) {
203 AvahiSHostNameResolver *r = userdata;
206 r->time_event = avahi_time_event_new(r->server->time_event_queue, &tv, time_event_callback, r);
209 +#ifndef HAVE_BONJOUR
210 static void record_browser_callback(
211 AvahiSRecordBrowser*rr,
212 AvahiIfIndex interface,
219 AvahiSHostNameResolver *avahi_s_host_name_resolver_new(
223 r->record_browser_aaaa = r->record_browser_a = NULL;
226 + r->defer_time_event = NULL;
228 + r->watch_a6 = NULL;
229 + r->client_a = NULL;
230 + r->client_a6 = NULL;
231 + r->lookup_flags = flags;
232 + avahi_resolve_host_name_start (r, aprotocol);
234 if (aprotocol == AVAHI_PROTO_INET || aprotocol == AVAHI_PROTO_UNSPEC) {
235 k = avahi_key_new(host_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
236 r->record_browser_a = avahi_s_record_browser_new(server, interface, protocol, k, flags, record_browser_callback, r);
240 assert(r->record_browser_aaaa || r->record_browser_a);
247 if (r->address_record)
248 avahi_record_unref(r->address_record);
250 + if (r->defer_time_event) {
251 + avahi_time_event_free(r->defer_time_event);
252 + r->defer_time_event = NULL;
256 + r->server->poll_api->watch_free(r->watch_a);
258 + r->server->poll_api->watch_free(r->watch_a6);
261 + DNSServiceRefDeallocate(r->client_a);
263 + DNSServiceRefDeallocate(r->client_a6);
266 avahi_free(r->host_name);