Home | History | Annotate | Line # | Download | only in resolv
res_data.c revision 1.4
      1  1.4  christos /*	$NetBSD: res_data.c,v 1.4 2004/05/20 19:43:39 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  * Copyright (c) 1995-1999 by Internet Software Consortium.
      6  1.1  christos  *
      7  1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8  1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9  1.1  christos  * copyright notice and this permission notice appear in all copies.
     10  1.1  christos  *
     11  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12  1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14  1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17  1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  1.1  christos  */
     19  1.1  christos 
     20  1.4  christos #include <sys/cdefs.h>
     21  1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     22  1.4  christos #ifdef notdef
     23  1.1  christos static const char rcsid[] = "Id: res_data.c,v 1.1.206.2 2004/03/16 12:34:18 marka Exp";
     24  1.4  christos #else
     25  1.4  christos __RCSID("$NetBSD: res_data.c,v 1.4 2004/05/20 19:43:39 christos Exp $");
     26  1.4  christos #endif
     27  1.1  christos #endif /* LIBC_SCCS and not lint */
     28  1.1  christos 
     29  1.1  christos #include "port_before.h"
     30  1.1  christos 
     31  1.1  christos #include <sys/types.h>
     32  1.1  christos #include <sys/param.h>
     33  1.1  christos #include <sys/socket.h>
     34  1.1  christos #include <sys/time.h>
     35  1.1  christos 
     36  1.1  christos #include <netinet/in.h>
     37  1.1  christos #include <arpa/inet.h>
     38  1.1  christos #include <arpa/nameser.h>
     39  1.1  christos 
     40  1.1  christos #include <ctype.h>
     41  1.1  christos #include <netdb.h>
     42  1.1  christos #include <resolv.h>
     43  1.1  christos #include <res_update.h>
     44  1.1  christos #include <stdio.h>
     45  1.1  christos #include <stdlib.h>
     46  1.1  christos #include <string.h>
     47  1.1  christos #include <unistd.h>
     48  1.1  christos 
     49  1.1  christos #include "port_after.h"
     50  1.1  christos #undef _res
     51  1.1  christos 
     52  1.1  christos const char *_res_opcodes[] = {
     53  1.1  christos 	"QUERY",
     54  1.1  christos 	"IQUERY",
     55  1.1  christos 	"CQUERYM",
     56  1.1  christos 	"CQUERYU",	/* experimental */
     57  1.1  christos 	"NOTIFY",	/* experimental */
     58  1.1  christos 	"UPDATE",
     59  1.1  christos 	"6",
     60  1.1  christos 	"7",
     61  1.1  christos 	"8",
     62  1.1  christos 	"9",
     63  1.1  christos 	"10",
     64  1.1  christos 	"11",
     65  1.1  christos 	"12",
     66  1.1  christos 	"13",
     67  1.1  christos 	"ZONEINIT",
     68  1.1  christos 	"ZONEREF",
     69  1.1  christos };
     70  1.1  christos 
     71  1.1  christos #ifdef BIND_UPDATE
     72  1.1  christos const char *_res_sectioncodes[] = {
     73  1.1  christos 	"ZONE",
     74  1.1  christos 	"PREREQUISITES",
     75  1.1  christos 	"UPDATE",
     76  1.1  christos 	"ADDITIONAL",
     77  1.1  christos };
     78  1.1  christos #endif
     79  1.1  christos 
     80  1.1  christos #ifndef __BIND_NOSTATIC
     81  1.1  christos struct __res_state _res
     82  1.1  christos # if defined(__BIND_RES_TEXT)
     83  1.1  christos 	= { RES_TIMEOUT, }	/* Motorola, et al. */
     84  1.1  christos # endif
     85  1.1  christos         ;
     86  1.1  christos 
     87  1.1  christos /* Proto. */
     88  1.1  christos 
     89  1.2  christos int  res_ourserver_p(const res_state, const struct sockaddr *);
     90  1.1  christos 
     91  1.1  christos int
     92  1.1  christos res_init(void) {
     93  1.1  christos 	extern int __res_vinit(res_state, int);
     94  1.1  christos 
     95  1.1  christos 	/*
     96  1.1  christos 	 * These three fields used to be statically initialized.  This made
     97  1.1  christos 	 * it hard to use this code in a shared library.  It is necessary,
     98  1.1  christos 	 * now that we're doing dynamic initialization here, that we preserve
     99  1.1  christos 	 * the old semantics: if an application modifies one of these three
    100  1.1  christos 	 * fields of _res before res_init() is called, res_init() will not
    101  1.1  christos 	 * alter them.  Of course, if an application is setting them to
    102  1.1  christos 	 * _zero_ before calling res_init(), hoping to override what used
    103  1.1  christos 	 * to be the static default, we can't detect it and unexpected results
    104  1.1  christos 	 * will follow.  Zero for any of these fields would make no sense,
    105  1.1  christos 	 * so one can safely assume that the applications were already getting
    106  1.1  christos 	 * unexpected results.
    107  1.1  christos 	 *
    108  1.1  christos 	 * _res.options is tricky since some apps were known to diddle the bits
    109  1.1  christos 	 * before res_init() was first called. We can't replicate that semantic
    110  1.1  christos 	 * with dynamic initialization (they may have turned bits off that are
    111  1.1  christos 	 * set in RES_DEFAULT).  Our solution is to declare such applications
    112  1.1  christos 	 * "broken".  They could fool us by setting RES_INIT but none do (yet).
    113  1.1  christos 	 */
    114  1.1  christos 	if (!_res.retrans)
    115  1.1  christos 		_res.retrans = RES_TIMEOUT;
    116  1.1  christos 	if (!_res.retry)
    117  1.1  christos 		_res.retry = 4;
    118  1.1  christos 	if (!(_res.options & RES_INIT))
    119  1.1  christos 		_res.options = RES_DEFAULT;
    120  1.1  christos 
    121  1.1  christos 	/*
    122  1.1  christos 	 * This one used to initialize implicitly to zero, so unless the app
    123  1.1  christos 	 * has set it to something in particular, we can randomize it now.
    124  1.1  christos 	 */
    125  1.1  christos 	if (!_res.id)
    126  1.1  christos 		_res.id = res_randomid();
    127  1.1  christos 
    128  1.1  christos 	return (__res_vinit(&_res, 1));
    129  1.1  christos }
    130  1.1  christos 
    131  1.1  christos void
    132  1.1  christos p_query(const u_char *msg) {
    133  1.1  christos 	fp_query(msg, stdout);
    134  1.1  christos }
    135  1.1  christos 
    136  1.1  christos void
    137  1.1  christos fp_query(const u_char *msg, FILE *file) {
    138  1.1  christos 	fp_nquery(msg, PACKETSZ, file);
    139  1.1  christos }
    140  1.1  christos 
    141  1.1  christos void
    142  1.1  christos fp_nquery(const u_char *msg, int len, FILE *file) {
    143  1.1  christos 	if ((_res.options & RES_INIT) == 0U && res_init() == -1)
    144  1.1  christos 		return;
    145  1.1  christos 
    146  1.1  christos 	res_pquery(&_res, msg, len, file);
    147  1.1  christos }
    148  1.1  christos 
    149  1.1  christos int
    150  1.1  christos res_mkquery(int op,			/* opcode of query */
    151  1.1  christos 	    const char *dname,		/* domain name */
    152  1.1  christos 	    int class, int type,	/* class and type of query */
    153  1.1  christos 	    const u_char *data,		/* resource record data */
    154  1.1  christos 	    int datalen,		/* length of data */
    155  1.1  christos 	    const u_char *newrr_in,	/* new rr for modify or append */
    156  1.1  christos 	    u_char *buf,		/* buffer to put query */
    157  1.1  christos 	    int buflen)			/* size of buffer */
    158  1.1  christos {
    159  1.1  christos 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    160  1.1  christos 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    161  1.1  christos 		return (-1);
    162  1.1  christos 	}
    163  1.1  christos 	return (res_nmkquery(&_res, op, dname, class, type,
    164  1.1  christos 			     data, datalen,
    165  1.1  christos 			     newrr_in, buf, buflen));
    166  1.1  christos }
    167  1.1  christos 
    168  1.2  christos #ifdef _LIBRESOLV
    169  1.1  christos int
    170  1.1  christos res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
    171  1.1  christos 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    172  1.1  christos 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    173  1.1  christos 		return (-1);
    174  1.1  christos 	}
    175  1.1  christos 
    176  1.1  christos 	return (res_nmkupdate(&_res, rrecp_in, buf, buflen));
    177  1.1  christos }
    178  1.2  christos #endif
    179  1.1  christos 
    180  1.1  christos int
    181  1.1  christos res_query(const char *name,	/* domain name */
    182  1.1  christos 	  int class, int type,	/* class and type of query */
    183  1.1  christos 	  u_char *answer,	/* buffer to put answer */
    184  1.1  christos 	  int anslen)		/* size of answer buffer */
    185  1.1  christos {
    186  1.1  christos 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    187  1.1  christos 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    188  1.1  christos 		return (-1);
    189  1.1  christos 	}
    190  1.1  christos 	return (res_nquery(&_res, name, class, type, answer, anslen));
    191  1.1  christos }
    192  1.1  christos 
    193  1.1  christos void
    194  1.1  christos res_send_setqhook(res_send_qhook hook) {
    195  1.1  christos 	_res.qhook = hook;
    196  1.1  christos }
    197  1.1  christos 
    198  1.1  christos void
    199  1.1  christos res_send_setrhook(res_send_rhook hook) {
    200  1.1  christos 	_res.rhook = hook;
    201  1.1  christos }
    202  1.1  christos 
    203  1.1  christos int
    204  1.1  christos res_isourserver(const struct sockaddr_in *inp) {
    205  1.2  christos 	return (res_ourserver_p(&_res, (const struct sockaddr *)(const void *)inp));
    206  1.1  christos }
    207  1.1  christos 
    208  1.1  christos int
    209  1.1  christos res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
    210  1.1  christos 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    211  1.1  christos 		/* errno should have been set by res_init() in this case. */
    212  1.1  christos 		return (-1);
    213  1.1  christos 	}
    214  1.1  christos 
    215  1.1  christos 	return (res_nsend(&_res, buf, buflen, ans, anssiz));
    216  1.1  christos }
    217  1.1  christos 
    218  1.2  christos #ifdef _LIBRESOLV
    219  1.1  christos int
    220  1.1  christos res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
    221  1.1  christos 	       u_char *ans, int anssiz)
    222  1.1  christos {
    223  1.1  christos 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    224  1.1  christos 		/* errno should have been set by res_init() in this case. */
    225  1.1  christos 		return (-1);
    226  1.1  christos 	}
    227  1.1  christos 
    228  1.1  christos 	return (res_nsendsigned(&_res, buf, buflen, key, ans, anssiz));
    229  1.1  christos }
    230  1.2  christos #endif
    231  1.1  christos 
    232  1.1  christos void
    233  1.1  christos res_close(void) {
    234  1.1  christos 	res_nclose(&_res);
    235  1.1  christos }
    236  1.1  christos 
    237  1.2  christos #ifdef _LIBRESOLV
    238  1.1  christos int
    239  1.1  christos res_update(ns_updrec *rrecp_in) {
    240  1.1  christos 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    241  1.1  christos 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    242  1.1  christos 		return (-1);
    243  1.1  christos 	}
    244  1.1  christos 
    245  1.1  christos 	return (res_nupdate(&_res, rrecp_in, NULL));
    246  1.1  christos }
    247  1.2  christos #endif
    248  1.1  christos 
    249  1.1  christos int
    250  1.1  christos res_search(const char *name,	/* domain name */
    251  1.1  christos 	   int class, int type,	/* class and type of query */
    252  1.1  christos 	   u_char *answer,	/* buffer to put answer */
    253  1.1  christos 	   int anslen)		/* size of answer */
    254  1.1  christos {
    255  1.1  christos 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    256  1.1  christos 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    257  1.1  christos 		return (-1);
    258  1.1  christos 	}
    259  1.1  christos 
    260  1.1  christos 	return (res_nsearch(&_res, name, class, type, answer, anslen));
    261  1.1  christos }
    262  1.1  christos 
    263  1.1  christos int
    264  1.1  christos res_querydomain(const char *name,
    265  1.1  christos 		const char *domain,
    266  1.1  christos 		int class, int type,	/* class and type of query */
    267  1.1  christos 		u_char *answer,		/* buffer to put answer */
    268  1.1  christos 		int anslen)		/* size of answer */
    269  1.1  christos {
    270  1.1  christos 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    271  1.1  christos 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    272  1.1  christos 		return (-1);
    273  1.1  christos 	}
    274  1.1  christos 
    275  1.1  christos 	return (res_nquerydomain(&_res, name, domain,
    276  1.1  christos 				 class, type,
    277  1.1  christos 				 answer, anslen));
    278  1.1  christos }
    279  1.1  christos 
    280  1.2  christos int
    281  1.2  christos res_opt(int a, u_char *b, int c, int d)
    282  1.2  christos {
    283  1.3  christos 	return res_nopt(&_res, a, b, c, d);
    284  1.2  christos }
    285  1.2  christos 
    286  1.1  christos const char *
    287  1.1  christos hostalias(const char *name) {
    288  1.1  christos 	static char abuf[MAXDNAME];
    289  1.1  christos 
    290  1.1  christos 	return (res_hostalias(&_res, name, abuf, sizeof abuf));
    291  1.1  christos }
    292  1.1  christos 
    293  1.1  christos #ifdef ultrix
    294  1.1  christos int
    295  1.1  christos local_hostname_length(const char *hostname) {
    296  1.1  christos 	int len_host, len_domain;
    297  1.1  christos 
    298  1.1  christos 	if (!*_res.defdname)
    299  1.1  christos 		res_init();
    300  1.1  christos 	len_host = strlen(hostname);
    301  1.1  christos 	len_domain = strlen(_res.defdname);
    302  1.1  christos 	if (len_host > len_domain &&
    303  1.1  christos 	    !strcasecmp(hostname + len_host - len_domain, _res.defdname) &&
    304  1.1  christos 	    hostname[len_host - len_domain - 1] == '.')
    305  1.1  christos 		return (len_host - len_domain - 1);
    306  1.1  christos 	return (0);
    307  1.1  christos }
    308  1.1  christos #endif /*ultrix*/
    309  1.1  christos 
    310  1.1  christos #endif
    311