1ebd1706eSgtb /*
2ae5b046dSsemery  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3ebd1706eSgtb  * Use is subject to license terms.
4ebd1706eSgtb  */
6505d05c7Sgtb /*
7505d05c7Sgtb  * lib/krb5/os/dnsglue.c
8505d05c7Sgtb  *
9505d05c7Sgtb  * Copyright 2004 by the Massachusetts Institute of Technology.
10505d05c7Sgtb  * All Rights Reserved.
11505d05c7Sgtb  *
12505d05c7Sgtb  * Export of this software from the United States of America may
13505d05c7Sgtb  *   require a specific license from the United States Government.
14505d05c7Sgtb  *   It is the responsibility of any person or organization contemplating
15505d05c7Sgtb  *   export to obtain such a license before exporting.
16505d05c7Sgtb  *
17505d05c7Sgtb  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
18505d05c7Sgtb  * distribute this software and its documentation for any purpose and
19505d05c7Sgtb  * without fee is hereby granted, provided that the above copyright
20505d05c7Sgtb  * notice appear in all copies and that both that copyright notice and
21505d05c7Sgtb  * this permission notice appear in supporting documentation, and that
22505d05c7Sgtb  * the name of M.I.T. not be used in advertising or publicity pertaining
23505d05c7Sgtb  * to distribution of the software without specific, written prior
24505d05c7Sgtb  * permission.  Furthermore if you modify this software you must label
25505d05c7Sgtb  * your software as modified software and not distribute it in such a
26505d05c7Sgtb  * fashion that it might be confused with the original M.I.T. software.
27505d05c7Sgtb  * M.I.T. makes no representations about the suitability of
28505d05c7Sgtb  * this software for any purpose.  It is provided "as is" without express
29505d05c7Sgtb  * or implied warranty.
30505d05c7Sgtb  *
31505d05c7Sgtb  */
32*159d09a2SMark Phalan #include "autoconf.h"
33505d05c7Sgtb #ifdef KRB5_DNS_LOOKUP
35505d05c7Sgtb #include "dnsglue.h"
37*159d09a2SMark Phalan /*
38*159d09a2SMark Phalan  * Only use res_ninit() if there's also a res_ndestroy(), to avoid
39*159d09a2SMark Phalan  * memory leaks (Linux & Solaris) and outright corruption (AIX 4.x,
40*159d09a2SMark Phalan  * 5.x).  While we're at it, make sure res_nsearch() is there too.
41*159d09a2SMark Phalan  *
42*159d09a2SMark Phalan  * In any case, it is probable that platforms having broken
43*159d09a2SMark Phalan  * res_ninit() will have thread safety hacks for res_init() and _res.
44*159d09a2SMark Phalan  */
46*159d09a2SMark Phalan #define USE_RES_NINIT 1
47*159d09a2SMark Phalan #endif
48*159d09a2SMark Phalan 
49505d05c7Sgtb /*
50505d05c7Sgtb  * Opaque handle
51505d05c7Sgtb  */
52505d05c7Sgtb struct krb5int_dns_state {
53505d05c7Sgtb     int nclass;
54505d05c7Sgtb     int ntype;
55505d05c7Sgtb     void *ansp;
56505d05c7Sgtb     int anslen;
57505d05c7Sgtb     int ansmax;
58505d05c7Sgtb #if HAVE_NS_INITPARSE
59505d05c7Sgtb     int cur_ans;
60505d05c7Sgtb     ns_msg msg;
61505d05c7Sgtb #else
62505d05c7Sgtb     unsigned char *ptr;
63505d05c7Sgtb     unsigned short nanswers;
64505d05c7Sgtb #endif
65505d05c7Sgtb };
67505d05c7Sgtb #if !HAVE_NS_INITPARSE
68505d05c7Sgtb static int initparse(struct krb5int_dns_state *);
69505d05c7Sgtb #endif
71505d05c7Sgtb /*
72505d05c7Sgtb  * krb5int_dns_init()
73505d05c7Sgtb  *
74*159d09a2SMark Phalan  * Initialize an opaque handle.  Do name lookup and initial parsing of
75505d05c7Sgtb  * reply, skipping question section.  Prepare to iterate over answer
76505d05c7Sgtb  * section.  Returns -1 on error, 0 on success.
77505d05c7Sgtb  */
78505d05c7Sgtb int
krb5int_dns_init(struct krb5int_dns_state ** dsp,char * host,int nclass,int ntype)79505d05c7Sgtb krb5int_dns_init(struct krb5int_dns_state **dsp,
80505d05c7Sgtb 		 char *host, int nclass, int ntype)
81505d05c7Sgtb {
82*159d09a2SMark Phalan #if USE_RES_NINIT