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