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