Home | History | Annotate | Line # | Download | only in resolv
res_data.c revision 1.1
      1 /*	$NetBSD: res_data.c,v 1.1 2004/05/20 17:18:55 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_in *);
     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 int
    164 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
    165 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    166 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    167 		return (-1);
    168 	}
    169 
    170 	return (res_nmkupdate(&_res, rrecp_in, buf, buflen));
    171 }
    172 
    173 int
    174 res_query(const char *name,	/* domain name */
    175 	  int class, int type,	/* class and type of query */
    176 	  u_char *answer,	/* buffer to put answer */
    177 	  int anslen)		/* size of answer buffer */
    178 {
    179 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    180 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    181 		return (-1);
    182 	}
    183 	return (res_nquery(&_res, name, class, type, answer, anslen));
    184 }
    185 
    186 void
    187 res_send_setqhook(res_send_qhook hook) {
    188 	_res.qhook = hook;
    189 }
    190 
    191 void
    192 res_send_setrhook(res_send_rhook hook) {
    193 	_res.rhook = hook;
    194 }
    195 
    196 int
    197 res_isourserver(const struct sockaddr_in *inp) {
    198 	return (res_ourserver_p(&_res, inp));
    199 }
    200 
    201 int
    202 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
    203 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    204 		/* errno should have been set by res_init() in this case. */
    205 		return (-1);
    206 	}
    207 
    208 	return (res_nsend(&_res, buf, buflen, ans, anssiz));
    209 }
    210 
    211 int
    212 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
    213 	       u_char *ans, int anssiz)
    214 {
    215 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    216 		/* errno should have been set by res_init() in this case. */
    217 		return (-1);
    218 	}
    219 
    220 	return (res_nsendsigned(&_res, buf, buflen, key, ans, anssiz));
    221 }
    222 
    223 void
    224 res_close(void) {
    225 	res_nclose(&_res);
    226 }
    227 
    228 int
    229 res_update(ns_updrec *rrecp_in) {
    230 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    231 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    232 		return (-1);
    233 	}
    234 
    235 	return (res_nupdate(&_res, rrecp_in, NULL));
    236 }
    237 
    238 int
    239 res_search(const char *name,	/* domain name */
    240 	   int class, int type,	/* class and type of query */
    241 	   u_char *answer,	/* buffer to put answer */
    242 	   int anslen)		/* size of answer */
    243 {
    244 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    245 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    246 		return (-1);
    247 	}
    248 
    249 	return (res_nsearch(&_res, name, class, type, answer, anslen));
    250 }
    251 
    252 int
    253 res_querydomain(const char *name,
    254 		const char *domain,
    255 		int class, int type,	/* class and type of query */
    256 		u_char *answer,		/* buffer to put answer */
    257 		int anslen)		/* size of answer */
    258 {
    259 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
    260 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
    261 		return (-1);
    262 	}
    263 
    264 	return (res_nquerydomain(&_res, name, domain,
    265 				 class, type,
    266 				 answer, anslen));
    267 }
    268 
    269 const char *
    270 hostalias(const char *name) {
    271 	static char abuf[MAXDNAME];
    272 
    273 	return (res_hostalias(&_res, name, abuf, sizeof abuf));
    274 }
    275 
    276 #ifdef ultrix
    277 int
    278 local_hostname_length(const char *hostname) {
    279 	int len_host, len_domain;
    280 
    281 	if (!*_res.defdname)
    282 		res_init();
    283 	len_host = strlen(hostname);
    284 	len_domain = strlen(_res.defdname);
    285 	if (len_host > len_domain &&
    286 	    !strcasecmp(hostname + len_host - len_domain, _res.defdname) &&
    287 	    hostname[len_host - len_domain - 1] == '.')
    288 		return (len_host - len_domain - 1);
    289 	return (0);
    290 }
    291 #endif /*ultrix*/
    292 
    293 #endif
    294