2 * Copyright (c) 2003 - 2008 Apple, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <mach/mach.h>
30 #include <netdb_async.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
36 #include <si_module.h>
38 extern void *LI_ils_create(char *fmt
, ...);
41 typedef void (*dns_async_callback)(int32_t status, char *buf, uint32_t len, struct sockaddr *from, int fromlen, void *context);
52 _dns_callback(si_item_t
*item
, uint32_t status
, void *ctx
)
54 _dns_context_t
*my_ctx
;
57 struct sockaddr
*from
;
61 if (ctx
== NULL
) return;
63 my_ctx
= (_dns_context_t
*)ctx
;
64 if (my_ctx
->orig_callback
== NULL
)
66 si_item_release(item
);
67 si_module_release(my_ctx
->dns
);
72 if (status
>= SI_STATUS_INTERNAL
) status
= NO_RECOVERY
;
80 if (item
!= NULL
) res
= (si_dnspacket_t
*)((char *)item
+ sizeof(si_item_t
));
82 if ((res
!= NULL
) && (res
->dns_packet_len
> 0))
84 packet
= malloc(res
->dns_packet_len
);
85 if (packet
== NULL
) status
= NO_RECOVERY
;
88 pl
= res
->dns_packet_len
;
89 memcpy(packet
, res
->dns_packet
, res
->dns_packet_len
);
91 if (res
->dns_server_len
> 0)
93 from
= malloc(res
->dns_server_len
);
103 fl
= res
->dns_server_len
;
104 memcpy(from
, res
->dns_server
, res
->dns_server_len
);
110 si_item_release(item
);
112 ((dns_async_callback
)(my_ctx
->orig_callback
))(status
, packet
, pl
, from
, fl
, my_ctx
->orig_context
);
114 si_module_release(my_ctx
->dns
);
119 dns_async_start(mach_port_t
*p
, const char *name
, uint16_t dnsclass
, uint16_t dnstype
, uint32_t do_search
, dns_async_callback callback
, void *context
)
124 _dns_context_t
*my_ctx
;
128 if (name
== NULL
) return NO_RECOVERY
;
130 dns
= si_module_with_name("mdns");
131 if (dns
== NULL
) return NO_RECOVERY
;
133 my_ctx
= (_dns_context_t
*)calloc(1, sizeof(_dns_context_t
));
136 si_module_release(dns
);
140 my_ctx
->orig_callback
= callback
;
141 my_ctx
->orig_context
= context
;
144 call
= SI_CALL_DNS_QUERY
;
145 if (do_search
!= 0) call
= SI_CALL_DNS_SEARCH
;
150 *p
= si_async_call(dns
, call
, name
, NULL
, NULL
, c
, t
, 0, 0, (void *)_dns_callback
, (void *)my_ctx
);
151 if (*p
== MACH_PORT_NULL
)
154 si_module_release(dns
);
162 dns_async_cancel(mach_port_t p
)
168 dns_async_send(mach_port_t
*p
, const char *name
, uint16_t dnsclass
, uint16_t dnstype
, uint32_t do_search
)
170 return dns_async_start(p
, name
, dnsclass
, dnstype
, do_search
, NULL
, NULL
);
175 dns_async_receive(mach_port_t p
, char **buf
, uint32_t *len
, struct sockaddr
**from
, uint32_t *fromlen
)
181 dns_async_handle_reply(void *msg
)
183 si_async_handle_reply(msg
);