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