1 /*- 2 * SPDX-License-Identifier: ISC 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.7 2008/12/11 09:59:00 marka Exp $"; 22 #endif /* LIBC_SCCS and not lint */ 23 #include "port_before.h" 24 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 #ifndef __BIND_NOSTATIC 73 74 /* Proto. */ 75 76 int res_ourserver_p(const res_state, const struct sockaddr_in *); 77 78 __noinline int 79 res_init(void) { 80 extern int __res_vinit(res_state, int); 81 res_state statp = &_res; 82 83 /* 84 * These three fields used to be statically initialized. This made 85 * it hard to use this code in a shared library. It is necessary, 86 * now that we're doing dynamic initialization here, that we preserve 87 * the old semantics: if an application modifies one of these three 88 * fields of _res before res_init() is called, res_init() will not 89 * alter them. Of course, if an application is setting them to 90 * _zero_ before calling res_init(), hoping to override what used 91 * to be the static default, we can't detect it and unexpected results 92 * will follow. Zero for any of these fields would make no sense, 93 * so one can safely assume that the applications were already getting 94 * unexpected results. 95 * 96 * _res.options is tricky since some apps were known to diddle the bits 97 * before res_init() was first called. We can't replicate that semantic 98 * with dynamic initialization (they may have turned bits off that are 99 * set in RES_DEFAULT). Our solution is to declare such applications 100 * "broken". They could fool us by setting RES_INIT but none do (yet). 101 */ 102 if (!statp->retrans) 103 statp->retrans = RES_TIMEOUT; 104 if (!statp->retry) 105 statp->retry = RES_DFLRETRY; 106 if (!(statp->options & RES_INIT)) 107 statp->options = RES_DEFAULT; 108 109 return (__res_vinit(statp, 1)); 110 } 111 112 void 113 p_query(const u_char *msg) { 114 fp_query(msg, stdout); 115 } 116 117 void 118 fp_query(const u_char *msg, FILE *file) { 119 fp_nquery(msg, PACKETSZ, file); 120 } 121 122 void 123 fp_nquery(const u_char *msg, int len, FILE *file) { 124 res_state statp = &_res; 125 if ((statp->options & RES_INIT) == 0U && res_init() == -1) 126 return; 127 128 res_pquery(statp, msg, len, file); 129 } 130 131 int 132 res_mkquery(int op, /*!< opcode of query */ 133 const char *dname, /*!< domain name */ 134 int class, int type, /*!< class and type of query */ 135 const u_char *data, /*!< resource record data */ 136 int datalen, /*!< length of data */ 137 const u_char *newrr_in, /*!< new rr for modify or append */ 138 u_char *buf, /*!< buffer to put query */ 139 int buflen) /*!< size of buffer */ 140 { 141 res_state statp = &_res; 142 if ((statp->options & RES_INIT) == 0U && res_init() == -1) { 143 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 144 return (-1); 145 } 146 return (res_nmkquery(statp, op, dname, class, type, 147 data, datalen, 148 newrr_in, buf, buflen)); 149 } 150 151 int 152 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) { 153 res_state statp = &_res; 154 if ((statp->options & RES_INIT) == 0U && res_init() == -1) { 155 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 156 return (-1); 157 } 158 159 return (res_nmkupdate(statp, rrecp_in, buf, buflen)); 160 } 161 162 int 163 res_query(const char *name, /*!< domain name */ 164 int class, int type, /*!< class and type of query */ 165 u_char *answer, /*!< buffer to put answer */ 166 int anslen) /*!< size of answer buffer */ 167 { 168 res_state statp = &_res; 169 if ((statp->options & RES_INIT) == 0U && res_init() == -1) { 170 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 171 return (-1); 172 } 173 return (res_nquery(statp, name, class, type, answer, anslen)); 174 } 175 176 #ifndef _LIBC 177 void 178 res_send_setqhook(res_send_qhook hook) { 179 _res.qhook = hook; 180 } 181 182 void 183 res_send_setrhook(res_send_rhook hook) { 184 _res.rhook = hook; 185 } 186 #endif 187 188 int 189 res_isourserver(const struct sockaddr_in *inp) { 190 return (res_ourserver_p(&_res, inp)); 191 } 192 193 int 194 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) { 195 res_state statp = &_res; 196 if ((statp->options & RES_INIT) == 0U && res_init() == -1) { 197 /* errno should have been set by res_init() in this case. */ 198 return (-1); 199 } 200 201 return (res_nsend(statp, buf, buflen, ans, anssiz)); 202 } 203 204 #ifndef _LIBC 205 int 206 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key, 207 u_char *ans, int anssiz) 208 { 209 res_state statp = &_res; 210 if ((statp->options & RES_INIT) == 0U && res_init() == -1) { 211 /* errno should have been set by res_init() in this case. */ 212 return (-1); 213 } 214 215 return (res_nsendsigned(statp, buf, buflen, key, ans, anssiz)); 216 } 217 #endif 218 219 void 220 res_close(void) { 221 res_nclose(&_res); 222 } 223 224 int 225 res_update(ns_updrec *rrecp_in) { 226 res_state statp = &_res; 227 if ((statp->options & RES_INIT) == 0U && res_init() == -1) { 228 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 229 return (-1); 230 } 231 232 return (res_nupdate(statp, rrecp_in, NULL)); 233 } 234 235 int 236 res_search(const char *name, /*!< domain name */ 237 int class, int type, /*!< class and type of query */ 238 u_char *answer, /*!< buffer to put answer */ 239 int anslen) /*!< size of answer */ 240 { 241 res_state statp = &_res; 242 if ((statp->options & RES_INIT) == 0U && res_init() == -1) { 243 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 244 return (-1); 245 } 246 247 return (res_nsearch(statp, name, class, type, answer, anslen)); 248 } 249 250 int 251 res_querydomain(const char *name, 252 const char *domain, 253 int class, int type, /*!< class and type of query */ 254 u_char *answer, /*!< buffer to put answer */ 255 int anslen) /*!< size of answer */ 256 { 257 res_state statp = &_res; 258 if ((statp->options & RES_INIT) == 0U && res_init() == -1) { 259 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 260 return (-1); 261 } 262 263 return (res_nquerydomain(statp, name, domain, 264 class, type, 265 answer, anslen)); 266 } 267 268 u_int 269 res_randomid(void) { 270 res_state statp = &_res; 271 if ((statp->options & RES_INIT) == 0U && res_init() == -1) { 272 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 273 return (-1); 274 } 275 276 return (res_nrandomid(statp)); 277 } 278 279 int 280 res_opt(int n0, u_char *buf, int buflen, int anslen) 281 { 282 return (res_nopt(&_res, n0, buf, buflen, anslen)); 283 } 284 285 const char * 286 hostalias(const char *name) { 287 static char abuf[MAXDNAME]; 288 289 return (res_hostalias(&_res, name, abuf, sizeof abuf)); 290 } 291 292 #ifdef ultrix 293 int 294 local_hostname_length(const char *hostname) { 295 int len_host, len_domain; 296 res_state statp; 297 298 statp = &_res; 299 if (!*statp->defdname) 300 res_init(); 301 len_host = strlen(hostname); 302 len_domain = strlen(statp->defdname); 303 if (len_host > len_domain && 304 !strcasecmp(hostname + len_host - len_domain, statp->defdname) && 305 hostname[len_host - len_domain - 1] == '.') 306 return (len_host - len_domain - 1); 307 return (0); 308 } 309 #endif /*ultrix*/ 310 311 /* 312 * Weak aliases for applications that use certain private entry points, 313 * and fail to include <resolv.h>. 314 */ 315 #undef res_init 316 __weak_reference(__res_init, res_init); 317 #undef p_query 318 __weak_reference(__p_query, p_query); 319 #undef res_mkquery 320 __weak_reference(__res_mkquery, res_mkquery); 321 #undef res_query 322 __weak_reference(__res_query, res_query); 323 #undef res_send 324 __weak_reference(__res_send, res_send); 325 #undef res_close 326 __weak_reference(__res_close, _res_close); 327 #undef res_search 328 __weak_reference(__res_search, res_search); 329 #undef res_querydomain 330 __weak_reference(__res_querydomain, res_querydomain); 331 332 #endif 333 334 /*! \file */ 335