]>
git.saurik.com Git - apple/libinfo.git/blob - dns.subproj/res_query.c
   2  * Copyright (c) 1999, 2012-2018 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@ 
  25 #include "libinfo_common.h" 
  30 #include <arpa/nameser_compat.h> 
  33 #include "si_module.h" 
  37 // Storage for the global struct __res_9_state. 
  38 // The BIND9 libresolv.dylib shares the same storage for this structure as the 
  39 // legacy BIND8 libsystem_info.dylib. This implementation does not require the 
  40 // _res structure but libresolv.dylib does and many 3rd-party applications 
  41 // access this global symbol directly so we preserve it here. 
  43 #define RES_9_STATE_SIZE 552 
  45 #define RES_9_STATE_SIZE 512 
  48 char _res
[RES_9_STATE_SIZE
] = {0}; 
  54         // For compatibility only. 
  58 // Perform a DNS query. Returned DNS response is placed in the answer buffer. 
  59 // A preliminary check of the answer is performed and success is returned only 
  60 // if no error is indicated in the answer and the answer count is nonzero. 
  61 // Returns the size of the response on success, or -1 with h_errno set. 
  63 _mdns_query(int call
, const char *name
, int class, int type
, u_char 
*answer
, int anslen
) 
  70         si_mod_t 
*dns 
= si_module_with_name("mdns"); 
  72                 h_errno 
= NO_RECOVERY
; 
  76         item 
= dns
->vtable
->sim_item_call(dns
, call
, name
, NULL
, NULL
, class, type
, &err
); 
  81                 p 
= (si_dnspacket_t 
*)((uintptr_t)item 
+ sizeof(si_item_t
)); 
  83                 res 
= p
->dns_packet_len
; 
  85                 if (res 
>= 0 && anslen 
>= 0) { 
  86                         // Truncate destination buffer size. 
  87                         memcpy(answer
, p
->dns_packet
, (cpylen 
= MIN(res
, anslen
))); 
  90                         h_errno 
= NO_RECOVERY
; 
  94                 si_item_release(item
); 
  96                 h_errno 
= HOST_NOT_FOUND
; 
 100         if (cpylen 
>= sizeof(HEADER
)) { 
 101                 HEADER 
*hp 
= (HEADER 
*)answer
; 
 104                                 h_errno 
= HOST_NOT_FOUND
; 
 112                                 if (ntohs(hp
->ancount
) == 0) { 
 121                                 h_errno 
= NO_RECOVERY
; 
 127         si_module_release(dns
); 
 133 res_query(const char *name
, int class, int type
, u_char 
*answer
, int anslen
) 
 135         return _mdns_query(SI_CALL_DNS_QUERY
, name
, class, type
, answer
, anslen
); 
 140 res_search(const char *name
, int class, int type
, u_char 
*answer
, int anslen
) 
 142         return _mdns_query(SI_CALL_DNS_SEARCH
, name
, class, type
, answer
, anslen
);