Home | History | Annotate | Line # | Download | only in resolv
res_data.c revision 1.5
      1 /*	$NetBSD: res_data.c,v 1.5 2004/05/21 15:35:05 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.5 2004/05/21 15:35:05 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_send_setqhook,__res_send_setqhook)
     56 __weak_alias(res_send_setrhook,__res_send_setrhook)
     57 __weak_alias(res_isourserver,__res_isourserver)
     58 __weak_alias(res_send,__res_send)
     59 __weak_alias(res_close,__res_close)
     60 __weak_alias(res_search,_res_search)
     61 __weak_alias(res_querydomain,__res_querydomain)
     62 __weak_alias(res_opt,_res_opt)
     63 __weak_alias(hostalias,__hostalias)
     64 #endif
     65 
     66 #undef _res
     67 
     68 const char *_res_opcodes[] = {
     69 	"QUERY",
     70 	"IQUERY",
     71 	"CQUERYM",
     72 	"CQUERYU",	/* experimental */
     73 	"NOTIFY",	/* experimental */
     74 	"UPDATE",
     75 	"6",
     76 	"7",
     77 	"8",
     78 	"9",
     79 	"10",
     80 	"11",
     81 	"12",
     82 	"13",
     83 	"ZONEINIT",
     84 	"ZONEREF",
     85 };
     86 
     87 #ifdef BIND_UPDATE
     88 const char *_res_sectioncodes[] = {
     89 	"ZONE",
     90 	"PREREQUISITES",
     91 	"UPDATE",
     92 	"ADDITIONAL",
     93 };
     94 #endif
     95 
     96 #ifndef __BIND_NOSTATIC
     97 struct __res_state _res
     98 # if defined(__BIND_RES_TEXT)
     99 	= { RES_TIMEOUT, }	/* Motorola, et al. */
    100 # endif
    101         ;
    102 
    103 /* Proto. */
    104 
    105 int  res_ourserver_p(const res_state, const struct sockaddr *);
    106 
    107 int
    108 res_init(void) {
    109 	extern int __res_vinit(res_state, int);
    110 
    111 	/*
    112 	 * These three fields used to be statically initialized.  This made
    113 	 * it hard to use this code in a shared library.  It is necessary,
    114 	 * now that we're doing dynamic initialization here, that we preserve
    115 	 * the old semantics: if an application modifies one of these three
    116 	 * fields of _res before res_init() is called, res_init() will not
    117 	 * alter them.  Of course, if an application is setting them to
    118 	 * _zero_ before calling res_init(), hoping to override what used
    119 	 * to be the static default, we can't detect it and unexpected results
    120 	 * will follow.  Zero for any of these fields would make no sense,
    121 	 * so one can safely assume that the applications were already getting
    122 	 * unexpected results.
    123 	 *
    124 	 * _res.options is tricky since some apps were known to diddle the bits
    125 	 * before res_init() was first called. We can't replicate that semantic
    126 	 * with dynamic initialization (they may have turned bits off that are
    127 	 * set in RES_DEFAULT).  Our solution is to declare such applications
    128 	 * "broken".  They could fool us by setting RES_INIT but none do (yet).
    129 	 */
    130 	if (!_res.retrans)
    131 		_res.retrans = RES_TIMEOUT;
    132 	if (!_res.retry)
    133 		_res.retry = 4;
    134 	if (!(_res.options & RES_INIT))
    135 		_res.options = RES_DEFAULT;
    136 
    137 	/*
    138 	 * This one used to initialize implicitly to zero, so unless the app
    139 	 * has set it to something in particular, we can randomize it now.
    140 	 */
    141 	if (!_res.id)
    142 		_res.id = res_randomid();
    143 
    144 	return (__res_vinit(&_res, 1));
    145 }
    146 
    147 void
    148 p_query(const u_char *msg) {
    149 	fp_query(msg, stdout);
    150 }
    151 
    152 void
    153 fp_query(const u_char *msg, FILE *file) {
    154 	fp_nquery(msg, PACKETSZ, file);
    155 }
    156 
    157 void
    158 fp_nquery(const u_char *msg, int len, FILE *file) {
    159 	if ((_res.options & RES_INIT) == 0U && res_init() == -1)
    160 		return;
    161 
    162 	res_pquery(&_res, msg, len, file);
    163 }
    164 
    165 int
    166 res_mkquery(int op,			/* opcode of query */
    167 	    const char *dname,		/* domain name */
    168 	    int class, int type,	/* class and type of query */
    169 	    const u_char *data,		/* resource record data */
    170 	    int datalen,		/* length of data */
    171 	    const u_char *newrr_in,	/* new rr for modify or append */
    172 	    u_char *buf,		/* buffer to put query */
    173 	    int buflen)			/* size of buffer */
    174 {
    175 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    176 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    177 		return (-1);
    178 	}
    179 	return (res_nmkquery(&_res, op, dname, class, type,
    180 			     data, datalen,
    181 			     newrr_in, buf, buflen));
    182 }
    183 
    184 #ifdef _LIBRESOLV
    185 int
    186 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
    187 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    188 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    189 		return (-1);
    190 	}
    191 
    192 	return (res_nmkupdate(&_res, rrecp_in, buf, buflen));
    193 }
    194 #endif
    195 
    196 int
    197 res_query(const char *name,	/* domain name */
    198 	  int class, int type,	/* class and type of query */
    199 	  u_char *answer,	/* buffer to put answer */
    200 	  int anslen)		/* size of answer buffer */
    201 {
    202 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    203 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    204 		return (-1);
    205 	}
    206 	return (res_nquery(&_res, name, class, type, answer, anslen));
    207 }
    208 
    209 void
    210 res_send_setqhook(res_send_qhook hook) {
    211 	_res.qhook = hook;
    212 }
    213 
    214 void
    215 res_send_setrhook(res_send_rhook hook) {
    216 	_res.rhook = hook;
    217 }
    218 
    219 int
    220 res_isourserver(const struct sockaddr_in *inp) {
    221 	return (res_ourserver_p(&_res, (const struct sockaddr *)(const void *)inp));
    222 }
    223 
    224 int
    225 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
    226 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    227 		/* errno should have been set by res_init() in this case. */
    228 		return (-1);
    229 	}
    230 
    231 	return (res_nsend(&_res, buf, buflen, ans, anssiz));
    232 }
    233 
    234 #ifdef _LIBRESOLV
    235 int
    236 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
    237 	       u_char *ans, int anssiz)
    238 {
    239 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    240 		/* errno should have been set by res_init() in this case. */
    241 		return (-1);
    242 	}
    243 
    244 	return (res_nsendsigned(&_res, buf, buflen, key, ans, anssiz));
    245 }
    246 #endif
    247 
    248 void
    249 res_close(void) {
    250 	res_nclose(&_res);
    251 }
    252 
    253 #ifdef _LIBRESOLV
    254 int
    255 res_update(ns_updrec *rrecp_in) {
    256 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    257 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    258 		return (-1);
    259 	}
    260 
    261 	return (res_nupdate(&_res, rrecp_in, NULL));
    262 }
    263 #endif
    264 
    265 int
    266 res_search(const char *name,	/* domain name */
    267 	   int class, int type,	/* class and type of query */
    268 	   u_char *answer,	/* buffer to put answer */
    269 	   int anslen)		/* size of answer */
    270 {
    271 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    272 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    273 		return (-1);
    274 	}
    275 
    276 	return (res_nsearch(&_res, name, class, type, answer, anslen));
    277 }
    278 
    279 int
    280 res_querydomain(const char *name,
    281 		const char *domain,
    282 		int class, int type,	/* class and type of query */
    283 		u_char *answer,		/* buffer to put answer */
    284 		int anslen)		/* size of answer */
    285 {
    286 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    287 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    288 		return (-1);
    289 	}
    290 
    291 	return (res_nquerydomain(&_res, name, domain,
    292 				 class, type,
    293 				 answer, anslen));
    294 }
    295 
    296 int
    297 res_opt(int a, u_char *b, int c, int d)
    298 {
    299 	return res_nopt(&_res, a, b, c, d);
    300 }
    301 
    302 const char *
    303 hostalias(const char *name) {
    304 	static char abuf[MAXDNAME];
    305 
    306 	return (res_hostalias(&_res, name, abuf, sizeof abuf));
    307 }
    308 
    309 #ifdef ultrix
    310 int
    311 local_hostname_length(const char *hostname) {
    312 	int len_host, len_domain;
    313 
    314 	if (!*_res.defdname)
    315 		res_init();
    316 	len_host = strlen(hostname);
    317 	len_domain = strlen(_res.defdname);
    318 	if (len_host > len_domain &&
    319 	    !strcasecmp(hostname + len_host - len_domain, _res.defdname) &&
    320 	    hostname[len_host - len_domain - 1] == '.')
    321 		return (len_host - len_domain - 1);
    322 	return (0);
    323 }
    324 #endif /*ultrix*/
    325 
    326 #endif
    327