]>
git.saurik.com Git - apple/libinfo.git/blob - dns.subproj/res_query.c
3b048ef5cabece10b070b93a87b8f5a0458c349c
2 * Copyright (c) 1999, 2012 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
28 #include <arpa/nameser_compat.h>
31 #include "si_module.h"
35 // Storage for the global struct __res_9_state.
36 // The BIND9 libresolv.dylib shares the same storage for this structure as the
37 // legacy BIND8 libsystem_info.dylib. This implementation does not require the
38 // _res structure but libresolv.dylib does and many 3rd-party applications
39 // access this global symbol directly so we preserve it here.
41 #define RES_9_STATE_SIZE 552
43 #define RES_9_STATE_SIZE 512
45 char _res
[RES_9_STATE_SIZE
] = {0};
50 // For compatibility only.
54 // Perform a DNS query. Returned DNS response is placed in the answer buffer.
55 // A preliminary check of the answer is performed and success is returned only
56 // if no error is indicated in the answer and the answer count is nonzero.
57 // Returns the size of the response on success, or -1 with h_errno set.
59 _mdns_query(int call
, const char *name
, int class, int type
, u_char
*answer
, int anslen
)
65 si_mod_t
*dns
= si_module_with_name("mdns");
67 h_errno
= NO_RECOVERY
;
71 item
= dns
->vtable
->sim_item_call(dns
, call
, name
, NULL
, NULL
, class, type
, &err
);
76 p
= (si_dnspacket_t
*)((uintptr_t)item
+ sizeof(si_item_t
));
78 res
= p
->dns_packet_len
;
80 // Truncate to destination buffer size.
81 memcpy(answer
, p
->dns_packet
, MIN(res
, anslen
));
83 si_item_release(item
);
85 h_errno
= HOST_NOT_FOUND
;
89 if (MIN(res
, anslen
) >= sizeof(HEADER
)) {
90 HEADER
*hp
= (HEADER
*)answer
;
93 h_errno
= HOST_NOT_FOUND
;
101 if (ntohs(hp
->ancount
) == 0) {
110 h_errno
= NO_RECOVERY
;
116 si_module_release(dns
);
121 res_query(const char *name
, int class, int type
, u_char
*answer
, int anslen
)
123 return _mdns_query(SI_CALL_DNS_QUERY
, name
, class, type
, answer
, anslen
);
127 res_search(const char *name
, int class, int type
, u_char
*answer
, int anslen
)
129 return _mdns_query(SI_CALL_DNS_SEARCH
, name
, class, type
, answer
, anslen
);