1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 3*0Sstevel@tonic-gate * Use is subject to license terms. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate /* 7*0Sstevel@tonic-gate * Copyright (c) 1985, 1988, 1993 8*0Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 9*0Sstevel@tonic-gate * 10*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 11*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 12*0Sstevel@tonic-gate * are met: 13*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 14*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 15*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 16*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 17*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 18*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 19*0Sstevel@tonic-gate * must display the following acknowledgement: 20*0Sstevel@tonic-gate * This product includes software developed by the University of 21*0Sstevel@tonic-gate * California, Berkeley and its contributors. 22*0Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 23*0Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 24*0Sstevel@tonic-gate * without specific prior written permission. 25*0Sstevel@tonic-gate * 26*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36*0Sstevel@tonic-gate * SUCH DAMAGE. 37*0Sstevel@tonic-gate */ 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate /* 40*0Sstevel@tonic-gate * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 41*0Sstevel@tonic-gate * 42*0Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 43*0Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 44*0Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 45*0Sstevel@tonic-gate * 46*0Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 47*0Sstevel@tonic-gate * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 48*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 49*0Sstevel@tonic-gate * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 50*0Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 51*0Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 52*0Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 53*0Sstevel@tonic-gate * SOFTWARE. 54*0Sstevel@tonic-gate */ 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate /* from gethostnamadr.c 8.1 (Berkeley) 6/4/93 */ 59*0Sstevel@tonic-gate /* BIND Id: gethnamaddr.c,v 8.15 1996/05/22 04:56:30 vixie Exp $ */ 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 62*0Sstevel@tonic-gate static const char rcsid[] = "$Id: dns_ho.c,v 1.43 2003/05/27 23:36:52 marka Exp $"; 63*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate /* Imports. */ 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate #include "port_before.h" 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate #include <sys/types.h> 70*0Sstevel@tonic-gate #include <sys/param.h> 71*0Sstevel@tonic-gate #include <sys/socket.h> 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate #include <netinet/in.h> 74*0Sstevel@tonic-gate #include <arpa/inet.h> 75*0Sstevel@tonic-gate #include <arpa/nameser.h> 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate #include <ctype.h> 78*0Sstevel@tonic-gate #include <errno.h> 79*0Sstevel@tonic-gate #include <stdlib.h> 80*0Sstevel@tonic-gate #include <netdb.h> 81*0Sstevel@tonic-gate #include <resolv.h> 82*0Sstevel@tonic-gate #include <stdio.h> 83*0Sstevel@tonic-gate #include <string.h> 84*0Sstevel@tonic-gate #include <syslog.h> 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate #include <isc/memcluster.h> 87*0Sstevel@tonic-gate #include <irs.h> 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate #include "port_after.h" 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate #include "irs_p.h" 92*0Sstevel@tonic-gate #include "dns_p.h" 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate #ifdef SPRINTF_CHAR 95*0Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x) 96*0Sstevel@tonic-gate #else 97*0Sstevel@tonic-gate # define SPRINTF(x) sprintf x 98*0Sstevel@tonic-gate #endif 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate /* Definitions. */ 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate #define MAXALIASES 35 103*0Sstevel@tonic-gate #define MAXADDRS 35 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate #define MAXPACKET (65535) /* Maximum TCP message size */ 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate #define BOUNDS_CHECK(ptr, count) \ 108*0Sstevel@tonic-gate if ((ptr) + (count) > eom) { \ 109*0Sstevel@tonic-gate had_error++; \ 110*0Sstevel@tonic-gate continue; \ 111*0Sstevel@tonic-gate } else (void)0 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate typedef union { 114*0Sstevel@tonic-gate HEADER hdr; 115*0Sstevel@tonic-gate u_char buf[MAXPACKET]; 116*0Sstevel@tonic-gate } querybuf; 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate struct dns_res_target { 119*0Sstevel@tonic-gate struct dns_res_target *next; 120*0Sstevel@tonic-gate querybuf qbuf; /* query buffer */ 121*0Sstevel@tonic-gate u_char *answer; /* buffer to put answer */ 122*0Sstevel@tonic-gate int anslen; /* size of answer buffer */ 123*0Sstevel@tonic-gate int qclass, qtype; /* class and type of query */ 124*0Sstevel@tonic-gate int action; /* condition whether query is really issued */ 125*0Sstevel@tonic-gate char qname[MAXDNAME +1]; /* domain name */ 126*0Sstevel@tonic-gate #if 0 127*0Sstevel@tonic-gate int n; /* result length */ 128*0Sstevel@tonic-gate #endif 129*0Sstevel@tonic-gate }; 130*0Sstevel@tonic-gate enum {RESTGT_DOALWAYS, RESTGT_AFTERFAILURE, RESTGT_IGNORE}; 131*0Sstevel@tonic-gate enum {RESQRY_SUCCESS, RESQRY_FAIL}; 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate struct pvt { 134*0Sstevel@tonic-gate struct hostent host; 135*0Sstevel@tonic-gate char * h_addr_ptrs[MAXADDRS + 1]; 136*0Sstevel@tonic-gate char * host_aliases[MAXALIASES]; 137*0Sstevel@tonic-gate char hostbuf[8*1024]; 138*0Sstevel@tonic-gate u_char host_addr[16]; /* IPv4 or IPv6 */ 139*0Sstevel@tonic-gate struct __res_state *res; 140*0Sstevel@tonic-gate void (*free_res)(void *); 141*0Sstevel@tonic-gate }; 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate typedef union { 144*0Sstevel@tonic-gate int32_t al; 145*0Sstevel@tonic-gate char ac; 146*0Sstevel@tonic-gate } align; 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff }; 149*0Sstevel@tonic-gate static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 }; 150*0Sstevel@tonic-gate /* Note: the IPv6 loopback address is in the "tunnel" space */ 151*0Sstevel@tonic-gate static const u_char v6local[] = { 0,0, 0,1 }; /* last 4 bytes of IPv6 addr */ 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate /* Forwards. */ 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate static void ho_close(struct irs_ho *this); 156*0Sstevel@tonic-gate static struct hostent * ho_byname(struct irs_ho *this, const char *name); 157*0Sstevel@tonic-gate static struct hostent * ho_byname2(struct irs_ho *this, const char *name, 158*0Sstevel@tonic-gate int af); 159*0Sstevel@tonic-gate static struct hostent * ho_byaddr(struct irs_ho *this, const void *addr, 160*0Sstevel@tonic-gate int len, int af); 161*0Sstevel@tonic-gate static struct hostent * ho_next(struct irs_ho *this); 162*0Sstevel@tonic-gate static void ho_rewind(struct irs_ho *this); 163*0Sstevel@tonic-gate static void ho_minimize(struct irs_ho *this); 164*0Sstevel@tonic-gate static struct __res_state * ho_res_get(struct irs_ho *this); 165*0Sstevel@tonic-gate static void ho_res_set(struct irs_ho *this, 166*0Sstevel@tonic-gate struct __res_state *res, 167*0Sstevel@tonic-gate void (*free_res)(void *)); 168*0Sstevel@tonic-gate static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name, 169*0Sstevel@tonic-gate const struct addrinfo *pai); 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate static void map_v4v6_hostent(struct hostent *hp, char **bp, 172*0Sstevel@tonic-gate char *ep); 173*0Sstevel@tonic-gate static void addrsort(res_state, char **, int); 174*0Sstevel@tonic-gate static struct hostent * gethostans(struct irs_ho *this, 175*0Sstevel@tonic-gate const u_char *ansbuf, int anslen, 176*0Sstevel@tonic-gate const char *qname, int qtype, 177*0Sstevel@tonic-gate int af, int size, 178*0Sstevel@tonic-gate struct addrinfo **ret_aip, 179*0Sstevel@tonic-gate const struct addrinfo *pai); 180*0Sstevel@tonic-gate static int add_hostent(struct pvt *pvt, char *bp, char **hap, 181*0Sstevel@tonic-gate struct addrinfo *ai); 182*0Sstevel@tonic-gate static int init(struct irs_ho *this); 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate /* Exports. */ 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate struct irs_ho * 187*0Sstevel@tonic-gate irs_dns_ho(struct irs_acc *this) { 188*0Sstevel@tonic-gate struct irs_ho *ho; 189*0Sstevel@tonic-gate struct pvt *pvt; 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate UNUSED(this); 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) { 194*0Sstevel@tonic-gate errno = ENOMEM; 195*0Sstevel@tonic-gate return (NULL); 196*0Sstevel@tonic-gate } 197*0Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt); 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate if (!(ho = memget(sizeof *ho))) { 200*0Sstevel@tonic-gate memput(pvt, sizeof *pvt); 201*0Sstevel@tonic-gate errno = ENOMEM; 202*0Sstevel@tonic-gate return (NULL); 203*0Sstevel@tonic-gate } 204*0Sstevel@tonic-gate memset(ho, 0x5e, sizeof *ho); 205*0Sstevel@tonic-gate ho->private = pvt; 206*0Sstevel@tonic-gate ho->close = ho_close; 207*0Sstevel@tonic-gate ho->byname = ho_byname; 208*0Sstevel@tonic-gate ho->byname2 = ho_byname2; 209*0Sstevel@tonic-gate ho->byaddr = ho_byaddr; 210*0Sstevel@tonic-gate ho->next = ho_next; 211*0Sstevel@tonic-gate ho->rewind = ho_rewind; 212*0Sstevel@tonic-gate ho->minimize = ho_minimize; 213*0Sstevel@tonic-gate ho->res_get = ho_res_get; 214*0Sstevel@tonic-gate ho->res_set = ho_res_set; 215*0Sstevel@tonic-gate ho->addrinfo = ho_addrinfo; 216*0Sstevel@tonic-gate return (ho); 217*0Sstevel@tonic-gate } 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gate /* Methods. */ 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate static void 222*0Sstevel@tonic-gate ho_close(struct irs_ho *this) { 223*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate ho_minimize(this); 226*0Sstevel@tonic-gate if (pvt->res && pvt->free_res) 227*0Sstevel@tonic-gate (*pvt->free_res)(pvt->res); 228*0Sstevel@tonic-gate if (pvt) 229*0Sstevel@tonic-gate memput(pvt, sizeof *pvt); 230*0Sstevel@tonic-gate memput(this, sizeof *this); 231*0Sstevel@tonic-gate } 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate static struct hostent * 234*0Sstevel@tonic-gate ho_byname(struct irs_ho *this, const char *name) { 235*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 236*0Sstevel@tonic-gate struct hostent *hp; 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate if (init(this) == -1) 239*0Sstevel@tonic-gate return (NULL); 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate if (pvt->res->options & RES_USE_INET6) { 242*0Sstevel@tonic-gate hp = ho_byname2(this, name, AF_INET6); 243*0Sstevel@tonic-gate if (hp) 244*0Sstevel@tonic-gate return (hp); 245*0Sstevel@tonic-gate } 246*0Sstevel@tonic-gate return (ho_byname2(this, name, AF_INET)); 247*0Sstevel@tonic-gate } 248*0Sstevel@tonic-gate 249*0Sstevel@tonic-gate static struct hostent * 250*0Sstevel@tonic-gate ho_byname2(struct irs_ho *this, const char *name, int af) 251*0Sstevel@tonic-gate { 252*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 253*0Sstevel@tonic-gate struct hostent *hp = NULL; 254*0Sstevel@tonic-gate int n, size; 255*0Sstevel@tonic-gate char tmp[NS_MAXDNAME]; 256*0Sstevel@tonic-gate const char *cp; 257*0Sstevel@tonic-gate struct addrinfo ai; 258*0Sstevel@tonic-gate struct dns_res_target *q, *p; 259*0Sstevel@tonic-gate int querystate = RESQRY_FAIL; 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gate if (init(this) == -1) 262*0Sstevel@tonic-gate return (NULL); 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gate q = memget(sizeof(*q)); 265*0Sstevel@tonic-gate if (q == NULL) { 266*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 267*0Sstevel@tonic-gate errno = ENOMEM; 268*0Sstevel@tonic-gate goto cleanup; 269*0Sstevel@tonic-gate } 270*0Sstevel@tonic-gate memset(q, 0, sizeof(q)); 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate switch (af) { 273*0Sstevel@tonic-gate case AF_INET: 274*0Sstevel@tonic-gate size = INADDRSZ; 275*0Sstevel@tonic-gate q->qclass = C_IN; 276*0Sstevel@tonic-gate q->qtype = T_A; 277*0Sstevel@tonic-gate q->answer = q->qbuf.buf; 278*0Sstevel@tonic-gate q->anslen = sizeof(q->qbuf); 279*0Sstevel@tonic-gate q->action = RESTGT_DOALWAYS; 280*0Sstevel@tonic-gate break; 281*0Sstevel@tonic-gate case AF_INET6: 282*0Sstevel@tonic-gate size = IN6ADDRSZ; 283*0Sstevel@tonic-gate q->qclass = C_IN; 284*0Sstevel@tonic-gate q->qtype = T_AAAA; 285*0Sstevel@tonic-gate q->answer = q->qbuf.buf; 286*0Sstevel@tonic-gate q->anslen = sizeof(q->qbuf); 287*0Sstevel@tonic-gate q->action = RESTGT_DOALWAYS; 288*0Sstevel@tonic-gate break; 289*0Sstevel@tonic-gate default: 290*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 291*0Sstevel@tonic-gate errno = EAFNOSUPPORT; 292*0Sstevel@tonic-gate hp = NULL; 293*0Sstevel@tonic-gate goto cleanup; 294*0Sstevel@tonic-gate } 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate /* 297*0Sstevel@tonic-gate * if there aren't any dots, it could be a user-level alias. 298*0Sstevel@tonic-gate * this is also done in res_nquery() since we are not the only 299*0Sstevel@tonic-gate * function that looks up host names. 300*0Sstevel@tonic-gate */ 301*0Sstevel@tonic-gate if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name, 302*0Sstevel@tonic-gate tmp, sizeof tmp))) 303*0Sstevel@tonic-gate name = cp; 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate for (p = q; p; p = p->next) { 306*0Sstevel@tonic-gate switch(p->action) { 307*0Sstevel@tonic-gate case RESTGT_DOALWAYS: 308*0Sstevel@tonic-gate break; 309*0Sstevel@tonic-gate case RESTGT_AFTERFAILURE: 310*0Sstevel@tonic-gate if (querystate == RESQRY_SUCCESS) 311*0Sstevel@tonic-gate continue; 312*0Sstevel@tonic-gate break; 313*0Sstevel@tonic-gate case RESTGT_IGNORE: 314*0Sstevel@tonic-gate continue; 315*0Sstevel@tonic-gate } 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype, 318*0Sstevel@tonic-gate p->answer, p->anslen)) < 0) { 319*0Sstevel@tonic-gate querystate = RESQRY_FAIL; 320*0Sstevel@tonic-gate continue; 321*0Sstevel@tonic-gate } 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate memset(&ai, 0, sizeof(ai)); 324*0Sstevel@tonic-gate ai.ai_family = af; 325*0Sstevel@tonic-gate if ((hp = gethostans(this, p->answer, n, name, p->qtype, 326*0Sstevel@tonic-gate af, size, NULL, 327*0Sstevel@tonic-gate (const struct addrinfo *)&ai)) != NULL) 328*0Sstevel@tonic-gate goto cleanup; /* no more loop is necessary */ 329*0Sstevel@tonic-gate 330*0Sstevel@tonic-gate querystate = RESQRY_FAIL; 331*0Sstevel@tonic-gate continue; 332*0Sstevel@tonic-gate } 333*0Sstevel@tonic-gate 334*0Sstevel@tonic-gate cleanup: 335*0Sstevel@tonic-gate if (q != NULL) 336*0Sstevel@tonic-gate memput(q, sizeof(*q)); 337*0Sstevel@tonic-gate return(hp); 338*0Sstevel@tonic-gate } 339*0Sstevel@tonic-gate 340*0Sstevel@tonic-gate static struct hostent * 341*0Sstevel@tonic-gate ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) 342*0Sstevel@tonic-gate { 343*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 344*0Sstevel@tonic-gate const u_char *uaddr = addr; 345*0Sstevel@tonic-gate char *qp; 346*0Sstevel@tonic-gate struct hostent *hp = NULL; 347*0Sstevel@tonic-gate struct addrinfo ai; 348*0Sstevel@tonic-gate struct dns_res_target *q, *q2, *p; 349*0Sstevel@tonic-gate int n, size, i; 350*0Sstevel@tonic-gate int querystate = RESQRY_FAIL; 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gate if (init(this) == -1) 353*0Sstevel@tonic-gate return (NULL); 354*0Sstevel@tonic-gate 355*0Sstevel@tonic-gate q = memget(sizeof(*q)); 356*0Sstevel@tonic-gate q2 = memget(sizeof(*q2)); 357*0Sstevel@tonic-gate if (q == NULL || q2 == NULL) { 358*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 359*0Sstevel@tonic-gate errno = ENOMEM; 360*0Sstevel@tonic-gate goto cleanup; 361*0Sstevel@tonic-gate } 362*0Sstevel@tonic-gate memset(q, 0, sizeof(q)); 363*0Sstevel@tonic-gate memset(q2, 0, sizeof(q2)); 364*0Sstevel@tonic-gate 365*0Sstevel@tonic-gate if (af == AF_INET6 && len == IN6ADDRSZ && 366*0Sstevel@tonic-gate (!memcmp(uaddr, mapped, sizeof mapped) || 367*0Sstevel@tonic-gate (!memcmp(uaddr, tunnelled, sizeof tunnelled) && 368*0Sstevel@tonic-gate memcmp(&uaddr[sizeof tunnelled], v6local, sizeof(v6local))))) { 369*0Sstevel@tonic-gate /* Unmap. */ 370*0Sstevel@tonic-gate addr = (const char *)addr + sizeof mapped; 371*0Sstevel@tonic-gate uaddr += sizeof mapped; 372*0Sstevel@tonic-gate af = AF_INET; 373*0Sstevel@tonic-gate len = INADDRSZ; 374*0Sstevel@tonic-gate } 375*0Sstevel@tonic-gate switch (af) { 376*0Sstevel@tonic-gate case AF_INET: 377*0Sstevel@tonic-gate size = INADDRSZ; 378*0Sstevel@tonic-gate q->qclass = C_IN; 379*0Sstevel@tonic-gate q->qtype = T_PTR; 380*0Sstevel@tonic-gate q->answer = q->qbuf.buf; 381*0Sstevel@tonic-gate q->anslen = sizeof(q->qbuf); 382*0Sstevel@tonic-gate q->action = RESTGT_DOALWAYS; 383*0Sstevel@tonic-gate break; 384*0Sstevel@tonic-gate case AF_INET6: 385*0Sstevel@tonic-gate size = IN6ADDRSZ; 386*0Sstevel@tonic-gate q->qclass = C_IN; 387*0Sstevel@tonic-gate q->qtype = T_PTR; 388*0Sstevel@tonic-gate q->answer = q->qbuf.buf; 389*0Sstevel@tonic-gate q->anslen = sizeof(q->qbuf); 390*0Sstevel@tonic-gate q->next = q2; 391*0Sstevel@tonic-gate q->action = RESTGT_DOALWAYS; 392*0Sstevel@tonic-gate q2->qclass = C_IN; 393*0Sstevel@tonic-gate q2->qtype = T_PTR; 394*0Sstevel@tonic-gate q2->answer = q2->qbuf.buf; 395*0Sstevel@tonic-gate q2->anslen = sizeof(q2->qbuf); 396*0Sstevel@tonic-gate if ((pvt->res->options & RES_NO_NIBBLE2) != 0) 397*0Sstevel@tonic-gate q2->action = RESTGT_IGNORE; 398*0Sstevel@tonic-gate else 399*0Sstevel@tonic-gate q2->action = RESTGT_AFTERFAILURE; 400*0Sstevel@tonic-gate break; 401*0Sstevel@tonic-gate default: 402*0Sstevel@tonic-gate errno = EAFNOSUPPORT; 403*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 404*0Sstevel@tonic-gate hp = NULL; 405*0Sstevel@tonic-gate goto cleanup; 406*0Sstevel@tonic-gate } 407*0Sstevel@tonic-gate if (size > len) { 408*0Sstevel@tonic-gate errno = EINVAL; 409*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 410*0Sstevel@tonic-gate hp = NULL; 411*0Sstevel@tonic-gate goto cleanup; 412*0Sstevel@tonic-gate } 413*0Sstevel@tonic-gate switch (af) { 414*0Sstevel@tonic-gate case AF_INET: 415*0Sstevel@tonic-gate qp = q->qname; 416*0Sstevel@tonic-gate (void) sprintf(qp, "%u.%u.%u.%u.in-addr.arpa", 417*0Sstevel@tonic-gate (uaddr[3] & 0xff), 418*0Sstevel@tonic-gate (uaddr[2] & 0xff), 419*0Sstevel@tonic-gate (uaddr[1] & 0xff), 420*0Sstevel@tonic-gate (uaddr[0] & 0xff)); 421*0Sstevel@tonic-gate break; 422*0Sstevel@tonic-gate case AF_INET6: 423*0Sstevel@tonic-gate if (q->action != RESTGT_IGNORE) { 424*0Sstevel@tonic-gate qp = q->qname; 425*0Sstevel@tonic-gate for (n = IN6ADDRSZ - 1; n >= 0; n--) { 426*0Sstevel@tonic-gate i = SPRINTF((qp, "%x.%x.", 427*0Sstevel@tonic-gate uaddr[n] & 0xf, 428*0Sstevel@tonic-gate (uaddr[n] >> 4) & 0xf)); 429*0Sstevel@tonic-gate if (i < 0) 430*0Sstevel@tonic-gate abort(); 431*0Sstevel@tonic-gate qp += i; 432*0Sstevel@tonic-gate } 433*0Sstevel@tonic-gate #ifdef HAVE_STRLCAT 434*0Sstevel@tonic-gate strlcat(q->qname, res_get_nibblesuffix(pvt->res), 435*0Sstevel@tonic-gate sizeof(q->qname)); 436*0Sstevel@tonic-gate #else 437*0Sstevel@tonic-gate strcpy(qp, res_get_nibblesuffix(pvt->res)); 438*0Sstevel@tonic-gate #endif 439*0Sstevel@tonic-gate } 440*0Sstevel@tonic-gate if (q2->action != RESTGT_IGNORE) { 441*0Sstevel@tonic-gate qp = q2->qname; 442*0Sstevel@tonic-gate for (n = IN6ADDRSZ - 1; n >= 0; n--) { 443*0Sstevel@tonic-gate i = SPRINTF((qp, "%x.%x.", 444*0Sstevel@tonic-gate uaddr[n] & 0xf, 445*0Sstevel@tonic-gate (uaddr[n] >> 4) & 0xf)); 446*0Sstevel@tonic-gate if (i < 0) 447*0Sstevel@tonic-gate abort(); 448*0Sstevel@tonic-gate qp += i; 449*0Sstevel@tonic-gate } 450*0Sstevel@tonic-gate #ifdef HAVE_STRLCAT 451*0Sstevel@tonic-gate strlcat(q->qname, res_get_nibblesuffix2(pvt->res), 452*0Sstevel@tonic-gate sizeof(q->qname)); 453*0Sstevel@tonic-gate #else 454*0Sstevel@tonic-gate strcpy(qp, res_get_nibblesuffix2(pvt->res)); 455*0Sstevel@tonic-gate #endif 456*0Sstevel@tonic-gate } 457*0Sstevel@tonic-gate break; 458*0Sstevel@tonic-gate default: 459*0Sstevel@tonic-gate abort(); 460*0Sstevel@tonic-gate } 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gate for (p = q; p; p = p->next) { 463*0Sstevel@tonic-gate switch(p->action) { 464*0Sstevel@tonic-gate case RESTGT_DOALWAYS: 465*0Sstevel@tonic-gate break; 466*0Sstevel@tonic-gate case RESTGT_AFTERFAILURE: 467*0Sstevel@tonic-gate if (querystate == RESQRY_SUCCESS) 468*0Sstevel@tonic-gate continue; 469*0Sstevel@tonic-gate break; 470*0Sstevel@tonic-gate case RESTGT_IGNORE: 471*0Sstevel@tonic-gate continue; 472*0Sstevel@tonic-gate } 473*0Sstevel@tonic-gate 474*0Sstevel@tonic-gate if ((n = res_nquery(pvt->res, p->qname, p->qclass, p->qtype, 475*0Sstevel@tonic-gate p->answer, p->anslen)) < 0) { 476*0Sstevel@tonic-gate querystate = RESQRY_FAIL; 477*0Sstevel@tonic-gate continue; 478*0Sstevel@tonic-gate } 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gate memset(&ai, 0, sizeof(ai)); 481*0Sstevel@tonic-gate ai.ai_family = af; 482*0Sstevel@tonic-gate hp = gethostans(this, p->answer, n, p->qname, T_PTR, af, size, 483*0Sstevel@tonic-gate NULL, (const struct addrinfo *)&ai); 484*0Sstevel@tonic-gate if (!hp) { 485*0Sstevel@tonic-gate querystate = RESQRY_FAIL; 486*0Sstevel@tonic-gate continue; 487*0Sstevel@tonic-gate } 488*0Sstevel@tonic-gate 489*0Sstevel@tonic-gate memcpy(pvt->host_addr, addr, len); 490*0Sstevel@tonic-gate pvt->h_addr_ptrs[0] = (char *)pvt->host_addr; 491*0Sstevel@tonic-gate pvt->h_addr_ptrs[1] = NULL; 492*0Sstevel@tonic-gate if (af == AF_INET && (pvt->res->options & RES_USE_INET6)) { 493*0Sstevel@tonic-gate map_v4v6_address((char*)pvt->host_addr, 494*0Sstevel@tonic-gate (char*)pvt->host_addr); 495*0Sstevel@tonic-gate pvt->host.h_addrtype = AF_INET6; 496*0Sstevel@tonic-gate pvt->host.h_length = IN6ADDRSZ; 497*0Sstevel@tonic-gate } 498*0Sstevel@tonic-gate 499*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS); 500*0Sstevel@tonic-gate goto cleanup; /* no more loop is necessary. */ 501*0Sstevel@tonic-gate } 502*0Sstevel@tonic-gate hp = NULL; /* H_ERRNO was set by subroutines */ 503*0Sstevel@tonic-gate 504*0Sstevel@tonic-gate cleanup: 505*0Sstevel@tonic-gate if (q != NULL) 506*0Sstevel@tonic-gate memput(q, sizeof(*q)); 507*0Sstevel@tonic-gate if (q2 != NULL) 508*0Sstevel@tonic-gate memput(q2, sizeof(*q2)); 509*0Sstevel@tonic-gate return(hp); 510*0Sstevel@tonic-gate } 511*0Sstevel@tonic-gate 512*0Sstevel@tonic-gate static struct hostent * 513*0Sstevel@tonic-gate ho_next(struct irs_ho *this) { 514*0Sstevel@tonic-gate 515*0Sstevel@tonic-gate UNUSED(this); 516*0Sstevel@tonic-gate 517*0Sstevel@tonic-gate return (NULL); 518*0Sstevel@tonic-gate } 519*0Sstevel@tonic-gate 520*0Sstevel@tonic-gate static void 521*0Sstevel@tonic-gate ho_rewind(struct irs_ho *this) { 522*0Sstevel@tonic-gate 523*0Sstevel@tonic-gate UNUSED(this); 524*0Sstevel@tonic-gate 525*0Sstevel@tonic-gate /* NOOP */ 526*0Sstevel@tonic-gate } 527*0Sstevel@tonic-gate 528*0Sstevel@tonic-gate static void 529*0Sstevel@tonic-gate ho_minimize(struct irs_ho *this) { 530*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 531*0Sstevel@tonic-gate 532*0Sstevel@tonic-gate if (pvt->res) 533*0Sstevel@tonic-gate res_nclose(pvt->res); 534*0Sstevel@tonic-gate } 535*0Sstevel@tonic-gate 536*0Sstevel@tonic-gate static struct __res_state * 537*0Sstevel@tonic-gate ho_res_get(struct irs_ho *this) { 538*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 539*0Sstevel@tonic-gate 540*0Sstevel@tonic-gate if (!pvt->res) { 541*0Sstevel@tonic-gate struct __res_state *res; 542*0Sstevel@tonic-gate res = (struct __res_state *)malloc(sizeof *res); 543*0Sstevel@tonic-gate if (!res) { 544*0Sstevel@tonic-gate errno = ENOMEM; 545*0Sstevel@tonic-gate return (NULL); 546*0Sstevel@tonic-gate } 547*0Sstevel@tonic-gate memset(res, 0, sizeof *res); 548*0Sstevel@tonic-gate ho_res_set(this, res, free); 549*0Sstevel@tonic-gate } 550*0Sstevel@tonic-gate 551*0Sstevel@tonic-gate return (pvt->res); 552*0Sstevel@tonic-gate } 553*0Sstevel@tonic-gate 554*0Sstevel@tonic-gate /* XXX */ 555*0Sstevel@tonic-gate extern struct addrinfo *addr2addrinfo __P((const struct addrinfo *, 556*0Sstevel@tonic-gate const char *)); 557*0Sstevel@tonic-gate 558*0Sstevel@tonic-gate static struct addrinfo * 559*0Sstevel@tonic-gate ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai) 560*0Sstevel@tonic-gate { 561*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 562*0Sstevel@tonic-gate int n; 563*0Sstevel@tonic-gate char tmp[NS_MAXDNAME]; 564*0Sstevel@tonic-gate const char *cp; 565*0Sstevel@tonic-gate struct dns_res_target *q, *q2, *p; 566*0Sstevel@tonic-gate struct addrinfo sentinel, *cur; 567*0Sstevel@tonic-gate int querystate = RESQRY_FAIL; 568*0Sstevel@tonic-gate 569*0Sstevel@tonic-gate if (init(this) == -1) 570*0Sstevel@tonic-gate return (NULL); 571*0Sstevel@tonic-gate 572*0Sstevel@tonic-gate memset(&sentinel, 0, sizeof(sentinel)); 573*0Sstevel@tonic-gate cur = &sentinel; 574*0Sstevel@tonic-gate 575*0Sstevel@tonic-gate q = memget(sizeof(*q)); 576*0Sstevel@tonic-gate q2 = memget(sizeof(*q2)); 577*0Sstevel@tonic-gate if (q == NULL || q2 == NULL) { 578*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); 579*0Sstevel@tonic-gate errno = ENOMEM; 580*0Sstevel@tonic-gate goto cleanup; 581*0Sstevel@tonic-gate } 582*0Sstevel@tonic-gate memset(q, 0, sizeof(q2)); 583*0Sstevel@tonic-gate memset(q2, 0, sizeof(q2)); 584*0Sstevel@tonic-gate 585*0Sstevel@tonic-gate switch (pai->ai_family) { 586*0Sstevel@tonic-gate case AF_UNSPEC: 587*0Sstevel@tonic-gate /* prefer IPv6 */ 588*0Sstevel@tonic-gate q->qclass = C_IN; 589*0Sstevel@tonic-gate q->qtype = T_AAAA; 590*0Sstevel@tonic-gate q->answer = q->qbuf.buf; 591*0Sstevel@tonic-gate q->anslen = sizeof(q->qbuf); 592*0Sstevel@tonic-gate q->next = q2; 593*0Sstevel@tonic-gate q->action = RESTGT_DOALWAYS; 594*0Sstevel@tonic-gate q2->qclass = C_IN; 595*0Sstevel@tonic-gate q2->qtype = T_A; 596*0Sstevel@tonic-gate q2->answer = q2->qbuf.buf; 597*0Sstevel@tonic-gate q2->anslen = sizeof(q2->qbuf); 598*0Sstevel@tonic-gate q2->action = RESTGT_DOALWAYS; 599*0Sstevel@tonic-gate break; 600*0Sstevel@tonic-gate case AF_INET: 601*0Sstevel@tonic-gate q->qclass = C_IN; 602*0Sstevel@tonic-gate q->qtype = T_A; 603*0Sstevel@tonic-gate q->answer = q->qbuf.buf; 604*0Sstevel@tonic-gate q->anslen = sizeof(q->qbuf); 605*0Sstevel@tonic-gate q->action = RESTGT_DOALWAYS; 606*0Sstevel@tonic-gate break; 607*0Sstevel@tonic-gate case AF_INET6: 608*0Sstevel@tonic-gate q->qclass = C_IN; 609*0Sstevel@tonic-gate q->qtype = T_AAAA; 610*0Sstevel@tonic-gate q->answer = q->qbuf.buf; 611*0Sstevel@tonic-gate q->anslen = sizeof(q->qbuf); 612*0Sstevel@tonic-gate q->action = RESTGT_DOALWAYS; 613*0Sstevel@tonic-gate break; 614*0Sstevel@tonic-gate default: 615*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); /* better error? */ 616*0Sstevel@tonic-gate goto cleanup; 617*0Sstevel@tonic-gate } 618*0Sstevel@tonic-gate 619*0Sstevel@tonic-gate /* 620*0Sstevel@tonic-gate * if there aren't any dots, it could be a user-level alias. 621*0Sstevel@tonic-gate * this is also done in res_nquery() since we are not the only 622*0Sstevel@tonic-gate * function that looks up host names. 623*0Sstevel@tonic-gate */ 624*0Sstevel@tonic-gate if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name, 625*0Sstevel@tonic-gate tmp, sizeof tmp))) 626*0Sstevel@tonic-gate name = cp; 627*0Sstevel@tonic-gate 628*0Sstevel@tonic-gate for (p = q; p; p = p->next) { 629*0Sstevel@tonic-gate struct addrinfo *ai; 630*0Sstevel@tonic-gate 631*0Sstevel@tonic-gate switch(p->action) { 632*0Sstevel@tonic-gate case RESTGT_DOALWAYS: 633*0Sstevel@tonic-gate break; 634*0Sstevel@tonic-gate case RESTGT_AFTERFAILURE: 635*0Sstevel@tonic-gate if (querystate == RESQRY_SUCCESS) 636*0Sstevel@tonic-gate continue; 637*0Sstevel@tonic-gate break; 638*0Sstevel@tonic-gate case RESTGT_IGNORE: 639*0Sstevel@tonic-gate continue; 640*0Sstevel@tonic-gate } 641*0Sstevel@tonic-gate 642*0Sstevel@tonic-gate if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype, 643*0Sstevel@tonic-gate p->answer, p->anslen)) < 0) { 644*0Sstevel@tonic-gate querystate = RESQRY_FAIL; 645*0Sstevel@tonic-gate continue; 646*0Sstevel@tonic-gate } 647*0Sstevel@tonic-gate (void)gethostans(this, p->answer, n, name, p->qtype, 648*0Sstevel@tonic-gate pai->ai_family, /* XXX: meaningless */ 649*0Sstevel@tonic-gate 0, &ai, pai); 650*0Sstevel@tonic-gate if (ai) { 651*0Sstevel@tonic-gate querystate = RESQRY_SUCCESS; 652*0Sstevel@tonic-gate cur->ai_next = ai; 653*0Sstevel@tonic-gate while (cur && cur->ai_next) 654*0Sstevel@tonic-gate cur = cur->ai_next; 655*0Sstevel@tonic-gate } 656*0Sstevel@tonic-gate else 657*0Sstevel@tonic-gate querystate = RESQRY_FAIL; 658*0Sstevel@tonic-gate } 659*0Sstevel@tonic-gate 660*0Sstevel@tonic-gate cleanup: 661*0Sstevel@tonic-gate if (q != NULL) 662*0Sstevel@tonic-gate memput(q, sizeof(*q)); 663*0Sstevel@tonic-gate if (q2 != NULL) 664*0Sstevel@tonic-gate memput(q2, sizeof(*q2)); 665*0Sstevel@tonic-gate return(sentinel.ai_next); 666*0Sstevel@tonic-gate } 667*0Sstevel@tonic-gate 668*0Sstevel@tonic-gate static void 669*0Sstevel@tonic-gate ho_res_set(struct irs_ho *this, struct __res_state *res, 670*0Sstevel@tonic-gate void (*free_res)(void *)) { 671*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 672*0Sstevel@tonic-gate 673*0Sstevel@tonic-gate if (pvt->res && pvt->free_res) { 674*0Sstevel@tonic-gate res_nclose(pvt->res); 675*0Sstevel@tonic-gate (*pvt->free_res)(pvt->res); 676*0Sstevel@tonic-gate } 677*0Sstevel@tonic-gate 678*0Sstevel@tonic-gate pvt->res = res; 679*0Sstevel@tonic-gate pvt->free_res = free_res; 680*0Sstevel@tonic-gate } 681*0Sstevel@tonic-gate 682*0Sstevel@tonic-gate /* Private. */ 683*0Sstevel@tonic-gate 684*0Sstevel@tonic-gate static struct hostent * 685*0Sstevel@tonic-gate gethostans(struct irs_ho *this, 686*0Sstevel@tonic-gate const u_char *ansbuf, int anslen, const char *qname, int qtype, 687*0Sstevel@tonic-gate int af, int size, /* meaningless for addrinfo cases */ 688*0Sstevel@tonic-gate struct addrinfo **ret_aip, const struct addrinfo *pai) 689*0Sstevel@tonic-gate { 690*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 691*0Sstevel@tonic-gate int type, class, ancount, qdcount, n, haveanswer, had_error; 692*0Sstevel@tonic-gate int error = NETDB_SUCCESS, arcount; 693*0Sstevel@tonic-gate int (*name_ok)(const char *); 694*0Sstevel@tonic-gate const HEADER *hp; 695*0Sstevel@tonic-gate const u_char *eom; 696*0Sstevel@tonic-gate const u_char *eor; 697*0Sstevel@tonic-gate const u_char *cp; 698*0Sstevel@tonic-gate const char *tname; 699*0Sstevel@tonic-gate const char *hname; 700*0Sstevel@tonic-gate char *bp, *ep, **ap, **hap; 701*0Sstevel@tonic-gate char tbuf[MAXDNAME+1]; 702*0Sstevel@tonic-gate struct addrinfo sentinel, *cur, ai; 703*0Sstevel@tonic-gate 704*0Sstevel@tonic-gate if (pai == NULL) abort(); 705*0Sstevel@tonic-gate if (ret_aip != NULL) 706*0Sstevel@tonic-gate *ret_aip = NULL; 707*0Sstevel@tonic-gate memset(&sentinel, 0, sizeof(sentinel)); 708*0Sstevel@tonic-gate cur = &sentinel; 709*0Sstevel@tonic-gate 710*0Sstevel@tonic-gate tname = qname; 711*0Sstevel@tonic-gate eom = ansbuf + anslen; 712*0Sstevel@tonic-gate switch (qtype) { 713*0Sstevel@tonic-gate case T_A: 714*0Sstevel@tonic-gate case T_AAAA: 715*0Sstevel@tonic-gate case T_ANY: /* use T_ANY only for T_A/T_AAAA lookup */ 716*0Sstevel@tonic-gate name_ok = res_hnok; 717*0Sstevel@tonic-gate break; 718*0Sstevel@tonic-gate case T_PTR: 719*0Sstevel@tonic-gate name_ok = res_dnok; 720*0Sstevel@tonic-gate break; 721*0Sstevel@tonic-gate default: 722*0Sstevel@tonic-gate abort(); 723*0Sstevel@tonic-gate } 724*0Sstevel@tonic-gate 725*0Sstevel@tonic-gate pvt->host.h_addrtype = af; 726*0Sstevel@tonic-gate pvt->host.h_length = size; 727*0Sstevel@tonic-gate hname = pvt->host.h_name = NULL; 728*0Sstevel@tonic-gate 729*0Sstevel@tonic-gate /* 730*0Sstevel@tonic-gate * Find first satisfactory answer. 731*0Sstevel@tonic-gate */ 732*0Sstevel@tonic-gate if (ansbuf + HFIXEDSZ > eom) { 733*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); 734*0Sstevel@tonic-gate return (NULL); 735*0Sstevel@tonic-gate } 736*0Sstevel@tonic-gate hp = (const HEADER *)ansbuf; 737*0Sstevel@tonic-gate ancount = ntohs(hp->ancount); 738*0Sstevel@tonic-gate qdcount = ntohs(hp->qdcount); 739*0Sstevel@tonic-gate arcount = ntohs(hp->arcount); 740*0Sstevel@tonic-gate bp = pvt->hostbuf; 741*0Sstevel@tonic-gate ep = pvt->hostbuf + sizeof(pvt->hostbuf); 742*0Sstevel@tonic-gate cp = ansbuf + HFIXEDSZ; 743*0Sstevel@tonic-gate if (qdcount != 1) { 744*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); 745*0Sstevel@tonic-gate return (NULL); 746*0Sstevel@tonic-gate } 747*0Sstevel@tonic-gate n = dn_expand(ansbuf, eom, cp, bp, ep - bp); 748*0Sstevel@tonic-gate if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) { 749*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); 750*0Sstevel@tonic-gate return (NULL); 751*0Sstevel@tonic-gate } 752*0Sstevel@tonic-gate cp += n + QFIXEDSZ; 753*0Sstevel@tonic-gate if (cp > eom) { 754*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); 755*0Sstevel@tonic-gate return (NULL); 756*0Sstevel@tonic-gate } 757*0Sstevel@tonic-gate if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { 758*0Sstevel@tonic-gate /* res_nsend() has already verified that the query name is the 759*0Sstevel@tonic-gate * same as the one we sent; this just gets the expanded name 760*0Sstevel@tonic-gate * (i.e., with the succeeding search-domain tacked on). 761*0Sstevel@tonic-gate */ 762*0Sstevel@tonic-gate n = strlen(bp) + 1; /* for the \0 */ 763*0Sstevel@tonic-gate if (n > MAXHOSTNAMELEN) { 764*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); 765*0Sstevel@tonic-gate return (NULL); 766*0Sstevel@tonic-gate } 767*0Sstevel@tonic-gate pvt->host.h_name = bp; 768*0Sstevel@tonic-gate hname = bp; 769*0Sstevel@tonic-gate bp += n; 770*0Sstevel@tonic-gate /* The qname can be abbreviated, but hname is now absolute. */ 771*0Sstevel@tonic-gate qname = pvt->host.h_name; 772*0Sstevel@tonic-gate } 773*0Sstevel@tonic-gate ap = pvt->host_aliases; 774*0Sstevel@tonic-gate *ap = NULL; 775*0Sstevel@tonic-gate pvt->host.h_aliases = pvt->host_aliases; 776*0Sstevel@tonic-gate hap = pvt->h_addr_ptrs; 777*0Sstevel@tonic-gate *hap = NULL; 778*0Sstevel@tonic-gate pvt->host.h_addr_list = pvt->h_addr_ptrs; 779*0Sstevel@tonic-gate haveanswer = 0; 780*0Sstevel@tonic-gate had_error = 0; 781*0Sstevel@tonic-gate while (ancount-- > 0 && cp < eom && !had_error) { 782*0Sstevel@tonic-gate n = dn_expand(ansbuf, eom, cp, bp, ep - bp); 783*0Sstevel@tonic-gate if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) { 784*0Sstevel@tonic-gate had_error++; 785*0Sstevel@tonic-gate continue; 786*0Sstevel@tonic-gate } 787*0Sstevel@tonic-gate cp += n; /* name */ 788*0Sstevel@tonic-gate BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); 789*0Sstevel@tonic-gate type = ns_get16(cp); 790*0Sstevel@tonic-gate cp += INT16SZ; /* type */ 791*0Sstevel@tonic-gate class = ns_get16(cp); 792*0Sstevel@tonic-gate cp += INT16SZ + INT32SZ; /* class, TTL */ 793*0Sstevel@tonic-gate n = ns_get16(cp); 794*0Sstevel@tonic-gate cp += INT16SZ; /* len */ 795*0Sstevel@tonic-gate BOUNDS_CHECK(cp, n); 796*0Sstevel@tonic-gate if (class != C_IN) { 797*0Sstevel@tonic-gate cp += n; 798*0Sstevel@tonic-gate continue; 799*0Sstevel@tonic-gate } 800*0Sstevel@tonic-gate eor = cp + n; 801*0Sstevel@tonic-gate if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && 802*0Sstevel@tonic-gate type == T_CNAME) { 803*0Sstevel@tonic-gate if (haveanswer) { 804*0Sstevel@tonic-gate int level = LOG_CRIT; 805*0Sstevel@tonic-gate #ifdef LOG_SECURITY 806*0Sstevel@tonic-gate level |= LOG_SECURITY; 807*0Sstevel@tonic-gate #endif 808*0Sstevel@tonic-gate syslog(level, 809*0Sstevel@tonic-gate "gethostans: possible attempt to exploit buffer overflow while looking up %s", 810*0Sstevel@tonic-gate *qname ? qname : "."); 811*0Sstevel@tonic-gate } 812*0Sstevel@tonic-gate n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf); 813*0Sstevel@tonic-gate if (n < 0 || !maybe_ok(pvt->res, tbuf, name_ok)) { 814*0Sstevel@tonic-gate had_error++; 815*0Sstevel@tonic-gate continue; 816*0Sstevel@tonic-gate } 817*0Sstevel@tonic-gate cp += n; 818*0Sstevel@tonic-gate /* Store alias. */ 819*0Sstevel@tonic-gate if (ap >= &pvt->host_aliases[MAXALIASES-1]) 820*0Sstevel@tonic-gate continue; 821*0Sstevel@tonic-gate *ap++ = bp; 822*0Sstevel@tonic-gate n = strlen(bp) + 1; /* for the \0 */ 823*0Sstevel@tonic-gate bp += n; 824*0Sstevel@tonic-gate /* Get canonical name. */ 825*0Sstevel@tonic-gate n = strlen(tbuf) + 1; /* for the \0 */ 826*0Sstevel@tonic-gate if (n > (ep - bp) || n > MAXHOSTNAMELEN) { 827*0Sstevel@tonic-gate had_error++; 828*0Sstevel@tonic-gate continue; 829*0Sstevel@tonic-gate } 830*0Sstevel@tonic-gate #ifdef HAVE_STRLCPY 831*0Sstevel@tonic-gate strlcpy(bp, tbuf, ep - bp); 832*0Sstevel@tonic-gate #else 833*0Sstevel@tonic-gate strcpy(bp, tbuf); 834*0Sstevel@tonic-gate #endif 835*0Sstevel@tonic-gate pvt->host.h_name = bp; 836*0Sstevel@tonic-gate hname = bp; 837*0Sstevel@tonic-gate bp += n; 838*0Sstevel@tonic-gate continue; 839*0Sstevel@tonic-gate } 840*0Sstevel@tonic-gate if (qtype == T_PTR && type == T_CNAME) { 841*0Sstevel@tonic-gate n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf); 842*0Sstevel@tonic-gate if (n < 0 || !maybe_dnok(pvt->res, tbuf)) { 843*0Sstevel@tonic-gate had_error++; 844*0Sstevel@tonic-gate continue; 845*0Sstevel@tonic-gate } 846*0Sstevel@tonic-gate cp += n; 847*0Sstevel@tonic-gate #ifdef RES_USE_DNAME 848*0Sstevel@tonic-gate if ((pvt->res->options & RES_USE_DNAME) != 0) 849*0Sstevel@tonic-gate #endif 850*0Sstevel@tonic-gate { 851*0Sstevel@tonic-gate /* 852*0Sstevel@tonic-gate * We may be able to check this regardless 853*0Sstevel@tonic-gate * of the USE_DNAME bit, but we add the check 854*0Sstevel@tonic-gate * for now since the DNAME support is 855*0Sstevel@tonic-gate * experimental. 856*0Sstevel@tonic-gate */ 857*0Sstevel@tonic-gate if (ns_samename(tname, bp) != 1) 858*0Sstevel@tonic-gate continue; 859*0Sstevel@tonic-gate } 860*0Sstevel@tonic-gate /* Get canonical name. */ 861*0Sstevel@tonic-gate n = strlen(tbuf) + 1; /* for the \0 */ 862*0Sstevel@tonic-gate if (n > (ep - bp)) { 863*0Sstevel@tonic-gate had_error++; 864*0Sstevel@tonic-gate continue; 865*0Sstevel@tonic-gate } 866*0Sstevel@tonic-gate #ifdef HAVE_STRLCPY 867*0Sstevel@tonic-gate strlcpy(bp, tbuf, ep - bp); 868*0Sstevel@tonic-gate #else 869*0Sstevel@tonic-gate strcpy(bp, tbuf); 870*0Sstevel@tonic-gate #endif 871*0Sstevel@tonic-gate tname = bp; 872*0Sstevel@tonic-gate bp += n; 873*0Sstevel@tonic-gate continue; 874*0Sstevel@tonic-gate } 875*0Sstevel@tonic-gate if (qtype == T_ANY) { 876*0Sstevel@tonic-gate if (!(type == T_A || type == T_AAAA)) { 877*0Sstevel@tonic-gate cp += n; 878*0Sstevel@tonic-gate continue; 879*0Sstevel@tonic-gate } 880*0Sstevel@tonic-gate } else if (type != qtype) { 881*0Sstevel@tonic-gate cp += n; 882*0Sstevel@tonic-gate continue; 883*0Sstevel@tonic-gate } 884*0Sstevel@tonic-gate switch (type) { 885*0Sstevel@tonic-gate case T_PTR: 886*0Sstevel@tonic-gate if (ret_aip != NULL) { 887*0Sstevel@tonic-gate /* addrinfo never needs T_PTR */ 888*0Sstevel@tonic-gate cp += n; 889*0Sstevel@tonic-gate continue; 890*0Sstevel@tonic-gate } 891*0Sstevel@tonic-gate if (ns_samename(tname, bp) != 1) { 892*0Sstevel@tonic-gate cp += n; 893*0Sstevel@tonic-gate continue; 894*0Sstevel@tonic-gate } 895*0Sstevel@tonic-gate n = dn_expand(ansbuf, eor, cp, bp, ep - bp); 896*0Sstevel@tonic-gate if (n < 0 || !maybe_hnok(pvt->res, bp) || 897*0Sstevel@tonic-gate n >= MAXHOSTNAMELEN) { 898*0Sstevel@tonic-gate had_error++; 899*0Sstevel@tonic-gate break; 900*0Sstevel@tonic-gate } 901*0Sstevel@tonic-gate cp += n; 902*0Sstevel@tonic-gate if (!haveanswer) { 903*0Sstevel@tonic-gate pvt->host.h_name = bp; 904*0Sstevel@tonic-gate hname = bp; 905*0Sstevel@tonic-gate } 906*0Sstevel@tonic-gate else if (ap < &pvt->host_aliases[MAXALIASES-1]) 907*0Sstevel@tonic-gate *ap++ = bp; 908*0Sstevel@tonic-gate else 909*0Sstevel@tonic-gate n = -1; 910*0Sstevel@tonic-gate if (n != -1) { 911*0Sstevel@tonic-gate n = strlen(bp) + 1; /* for the \0 */ 912*0Sstevel@tonic-gate bp += n; 913*0Sstevel@tonic-gate } 914*0Sstevel@tonic-gate break; 915*0Sstevel@tonic-gate case T_A: 916*0Sstevel@tonic-gate case T_AAAA: 917*0Sstevel@tonic-gate if (ns_samename(hname, bp) != 1) { 918*0Sstevel@tonic-gate cp += n; 919*0Sstevel@tonic-gate continue; 920*0Sstevel@tonic-gate } 921*0Sstevel@tonic-gate if (type == T_A && n != INADDRSZ) { 922*0Sstevel@tonic-gate cp += n; 923*0Sstevel@tonic-gate continue; 924*0Sstevel@tonic-gate } 925*0Sstevel@tonic-gate if (type == T_AAAA && n != IN6ADDRSZ) { 926*0Sstevel@tonic-gate cp += n; 927*0Sstevel@tonic-gate continue; 928*0Sstevel@tonic-gate } 929*0Sstevel@tonic-gate 930*0Sstevel@tonic-gate /* make addrinfo. don't overwrite constant PAI */ 931*0Sstevel@tonic-gate ai = *pai; 932*0Sstevel@tonic-gate ai.ai_family = (type == T_AAAA) ? AF_INET6 : AF_INET; 933*0Sstevel@tonic-gate cur->ai_next = addr2addrinfo( 934*0Sstevel@tonic-gate (const struct addrinfo *)&ai, 935*0Sstevel@tonic-gate (const char *)cp); 936*0Sstevel@tonic-gate if (cur->ai_next == NULL) 937*0Sstevel@tonic-gate had_error++; 938*0Sstevel@tonic-gate 939*0Sstevel@tonic-gate if (!haveanswer) { 940*0Sstevel@tonic-gate int nn; 941*0Sstevel@tonic-gate 942*0Sstevel@tonic-gate nn = strlen(bp) + 1; /* for the \0 */ 943*0Sstevel@tonic-gate if (nn >= MAXHOSTNAMELEN) { 944*0Sstevel@tonic-gate cp += n; 945*0Sstevel@tonic-gate had_error++; 946*0Sstevel@tonic-gate continue; 947*0Sstevel@tonic-gate } 948*0Sstevel@tonic-gate pvt->host.h_name = bp; 949*0Sstevel@tonic-gate hname = bp; 950*0Sstevel@tonic-gate bp += nn; 951*0Sstevel@tonic-gate } 952*0Sstevel@tonic-gate /* Ensure alignment. */ 953*0Sstevel@tonic-gate bp = (char *)(((u_long)bp + (sizeof(align) - 1)) & 954*0Sstevel@tonic-gate ~(sizeof(align) - 1)); 955*0Sstevel@tonic-gate /* Avoid overflows. */ 956*0Sstevel@tonic-gate if (bp + n >= &pvt->hostbuf[sizeof pvt->hostbuf]) { 957*0Sstevel@tonic-gate had_error++; 958*0Sstevel@tonic-gate continue; 959*0Sstevel@tonic-gate } 960*0Sstevel@tonic-gate if (ret_aip) { /* need addrinfo. keep it. */ 961*0Sstevel@tonic-gate while (cur && cur->ai_next) 962*0Sstevel@tonic-gate cur = cur->ai_next; 963*0Sstevel@tonic-gate } else if (cur->ai_next) { /* need hostent */ 964*0Sstevel@tonic-gate struct addrinfo *aip = cur->ai_next; 965*0Sstevel@tonic-gate 966*0Sstevel@tonic-gate for (aip = cur->ai_next; aip; 967*0Sstevel@tonic-gate aip = aip->ai_next) { 968*0Sstevel@tonic-gate int m; 969*0Sstevel@tonic-gate 970*0Sstevel@tonic-gate m = add_hostent(pvt, bp, hap, aip); 971*0Sstevel@tonic-gate if (m < 0) { 972*0Sstevel@tonic-gate had_error++; 973*0Sstevel@tonic-gate break; 974*0Sstevel@tonic-gate } 975*0Sstevel@tonic-gate if (m == 0) 976*0Sstevel@tonic-gate continue; 977*0Sstevel@tonic-gate if (hap < &pvt->h_addr_ptrs[MAXADDRS-1]) 978*0Sstevel@tonic-gate hap++; 979*0Sstevel@tonic-gate *hap = NULL; 980*0Sstevel@tonic-gate bp += m; 981*0Sstevel@tonic-gate } 982*0Sstevel@tonic-gate 983*0Sstevel@tonic-gate freeaddrinfo(cur->ai_next); 984*0Sstevel@tonic-gate cur->ai_next = NULL; 985*0Sstevel@tonic-gate } 986*0Sstevel@tonic-gate cp += n; 987*0Sstevel@tonic-gate break; 988*0Sstevel@tonic-gate default: 989*0Sstevel@tonic-gate abort(); 990*0Sstevel@tonic-gate } 991*0Sstevel@tonic-gate if (!had_error) 992*0Sstevel@tonic-gate haveanswer++; 993*0Sstevel@tonic-gate } 994*0Sstevel@tonic-gate if (haveanswer) { 995*0Sstevel@tonic-gate if (ret_aip == NULL) { 996*0Sstevel@tonic-gate *ap = NULL; 997*0Sstevel@tonic-gate *hap = NULL; 998*0Sstevel@tonic-gate 999*0Sstevel@tonic-gate if (pvt->res->nsort && haveanswer > 1 && qtype == T_A) 1000*0Sstevel@tonic-gate addrsort(pvt->res, pvt->h_addr_ptrs, 1001*0Sstevel@tonic-gate haveanswer); 1002*0Sstevel@tonic-gate if (pvt->host.h_name == NULL) { 1003*0Sstevel@tonic-gate n = strlen(qname) + 1; /* for the \0 */ 1004*0Sstevel@tonic-gate if (n > (ep - bp) || n >= MAXHOSTNAMELEN) 1005*0Sstevel@tonic-gate goto no_recovery; 1006*0Sstevel@tonic-gate #ifdef HAVE_STRLCPY 1007*0Sstevel@tonic-gate strlcpy(bp, qname, ep - bp); 1008*0Sstevel@tonic-gate #else 1009*0Sstevel@tonic-gate strcpy(bp, qname); 1010*0Sstevel@tonic-gate #endif 1011*0Sstevel@tonic-gate pvt->host.h_name = bp; 1012*0Sstevel@tonic-gate bp += n; 1013*0Sstevel@tonic-gate } 1014*0Sstevel@tonic-gate if (pvt->res->options & RES_USE_INET6) 1015*0Sstevel@tonic-gate map_v4v6_hostent(&pvt->host, &bp, ep); 1016*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS); 1017*0Sstevel@tonic-gate return (&pvt->host); 1018*0Sstevel@tonic-gate } else { 1019*0Sstevel@tonic-gate if ((pai->ai_flags & AI_CANONNAME) != 0) { 1020*0Sstevel@tonic-gate if (pvt->host.h_name == NULL) { 1021*0Sstevel@tonic-gate sentinel.ai_next->ai_canonname = 1022*0Sstevel@tonic-gate strdup(qname); 1023*0Sstevel@tonic-gate } 1024*0Sstevel@tonic-gate else { 1025*0Sstevel@tonic-gate sentinel.ai_next->ai_canonname = 1026*0Sstevel@tonic-gate strdup(pvt->host.h_name); 1027*0Sstevel@tonic-gate } 1028*0Sstevel@tonic-gate } 1029*0Sstevel@tonic-gate *ret_aip = sentinel.ai_next; 1030*0Sstevel@tonic-gate return(NULL); 1031*0Sstevel@tonic-gate } 1032*0Sstevel@tonic-gate } 1033*0Sstevel@tonic-gate no_recovery: 1034*0Sstevel@tonic-gate if (sentinel.ai_next) { 1035*0Sstevel@tonic-gate /* this should be impossible, but check it for safety */ 1036*0Sstevel@tonic-gate freeaddrinfo(sentinel.ai_next); 1037*0Sstevel@tonic-gate } 1038*0Sstevel@tonic-gate if (error == NETDB_SUCCESS) 1039*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); 1040*0Sstevel@tonic-gate else 1041*0Sstevel@tonic-gate RES_SET_H_ERRNO(pvt->res, error); 1042*0Sstevel@tonic-gate return(NULL); 1043*0Sstevel@tonic-gate } 1044*0Sstevel@tonic-gate 1045*0Sstevel@tonic-gate static int 1046*0Sstevel@tonic-gate add_hostent(struct pvt *pvt, char *bp, char **hap, struct addrinfo *ai) 1047*0Sstevel@tonic-gate { 1048*0Sstevel@tonic-gate int addrlen; 1049*0Sstevel@tonic-gate char *addrp; 1050*0Sstevel@tonic-gate const char **tap; 1051*0Sstevel@tonic-gate char *obp = bp; 1052*0Sstevel@tonic-gate 1053*0Sstevel@tonic-gate switch(ai->ai_addr->sa_family) { 1054*0Sstevel@tonic-gate case AF_INET6: 1055*0Sstevel@tonic-gate addrlen = IN6ADDRSZ; 1056*0Sstevel@tonic-gate addrp = (char *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr; 1057*0Sstevel@tonic-gate break; 1058*0Sstevel@tonic-gate case AF_INET: 1059*0Sstevel@tonic-gate addrlen = INADDRSZ; 1060*0Sstevel@tonic-gate addrp = (char *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr; 1061*0Sstevel@tonic-gate break; 1062*0Sstevel@tonic-gate default: 1063*0Sstevel@tonic-gate return(-1); /* abort? */ 1064*0Sstevel@tonic-gate } 1065*0Sstevel@tonic-gate 1066*0Sstevel@tonic-gate /* Ensure alignment. */ 1067*0Sstevel@tonic-gate bp = (char *)(((u_long)bp + (sizeof(align) - 1)) & 1068*0Sstevel@tonic-gate ~(sizeof(align) - 1)); 1069*0Sstevel@tonic-gate /* Avoid overflows. */ 1070*0Sstevel@tonic-gate if (bp + addrlen >= &pvt->hostbuf[sizeof pvt->hostbuf]) 1071*0Sstevel@tonic-gate return(-1); 1072*0Sstevel@tonic-gate if (hap >= &pvt->h_addr_ptrs[MAXADDRS-1]) 1073*0Sstevel@tonic-gate return(0); /* fail, but not treat it as an error. */ 1074*0Sstevel@tonic-gate 1075*0Sstevel@tonic-gate /* Suppress duplicates. */ 1076*0Sstevel@tonic-gate for (tap = (const char **)pvt->h_addr_ptrs; 1077*0Sstevel@tonic-gate *tap != NULL; 1078*0Sstevel@tonic-gate tap++) 1079*0Sstevel@tonic-gate if (memcmp(*tap, addrp, addrlen) == 0) 1080*0Sstevel@tonic-gate break; 1081*0Sstevel@tonic-gate if (*tap != NULL) 1082*0Sstevel@tonic-gate return (0); 1083*0Sstevel@tonic-gate 1084*0Sstevel@tonic-gate memcpy(*hap = bp, addrp, addrlen); 1085*0Sstevel@tonic-gate return((bp + addrlen) - obp); 1086*0Sstevel@tonic-gate } 1087*0Sstevel@tonic-gate 1088*0Sstevel@tonic-gate static void 1089*0Sstevel@tonic-gate map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) { 1090*0Sstevel@tonic-gate char **ap; 1091*0Sstevel@tonic-gate 1092*0Sstevel@tonic-gate if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ) 1093*0Sstevel@tonic-gate return; 1094*0Sstevel@tonic-gate hp->h_addrtype = AF_INET6; 1095*0Sstevel@tonic-gate hp->h_length = IN6ADDRSZ; 1096*0Sstevel@tonic-gate for (ap = hp->h_addr_list; *ap; ap++) { 1097*0Sstevel@tonic-gate int i = (u_long)*bpp % sizeof(align); 1098*0Sstevel@tonic-gate 1099*0Sstevel@tonic-gate if (i != 0) 1100*0Sstevel@tonic-gate i = sizeof(align) - i; 1101*0Sstevel@tonic-gate 1102*0Sstevel@tonic-gate if ((ep - *bpp) < (i + IN6ADDRSZ)) { 1103*0Sstevel@tonic-gate /* Out of memory. Truncate address list here. */ 1104*0Sstevel@tonic-gate *ap = NULL; 1105*0Sstevel@tonic-gate return; 1106*0Sstevel@tonic-gate } 1107*0Sstevel@tonic-gate *bpp += i; 1108*0Sstevel@tonic-gate map_v4v6_address(*ap, *bpp); 1109*0Sstevel@tonic-gate *ap = *bpp; 1110*0Sstevel@tonic-gate *bpp += IN6ADDRSZ; 1111*0Sstevel@tonic-gate } 1112*0Sstevel@tonic-gate } 1113*0Sstevel@tonic-gate 1114*0Sstevel@tonic-gate static void 1115*0Sstevel@tonic-gate addrsort(res_state statp, char **ap, int num) { 1116*0Sstevel@tonic-gate int i, j, needsort = 0, aval[MAXADDRS]; 1117*0Sstevel@tonic-gate char **p; 1118*0Sstevel@tonic-gate 1119*0Sstevel@tonic-gate p = ap; 1120*0Sstevel@tonic-gate for (i = 0; i < num; i++, p++) { 1121*0Sstevel@tonic-gate for (j = 0 ; (unsigned)j < statp->nsort; j++) 1122*0Sstevel@tonic-gate if (statp->sort_list[j].addr.s_addr == 1123*0Sstevel@tonic-gate (((struct in_addr *)(*p))->s_addr & 1124*0Sstevel@tonic-gate statp->sort_list[j].mask)) 1125*0Sstevel@tonic-gate break; 1126*0Sstevel@tonic-gate aval[i] = j; 1127*0Sstevel@tonic-gate if (needsort == 0 && i > 0 && j < aval[i-1]) 1128*0Sstevel@tonic-gate needsort = i; 1129*0Sstevel@tonic-gate } 1130*0Sstevel@tonic-gate if (!needsort) 1131*0Sstevel@tonic-gate return; 1132*0Sstevel@tonic-gate 1133*0Sstevel@tonic-gate while (needsort < num) { 1134*0Sstevel@tonic-gate for (j = needsort - 1; j >= 0; j--) { 1135*0Sstevel@tonic-gate if (aval[j] > aval[j+1]) { 1136*0Sstevel@tonic-gate char *hp; 1137*0Sstevel@tonic-gate 1138*0Sstevel@tonic-gate i = aval[j]; 1139*0Sstevel@tonic-gate aval[j] = aval[j+1]; 1140*0Sstevel@tonic-gate aval[j+1] = i; 1141*0Sstevel@tonic-gate 1142*0Sstevel@tonic-gate hp = ap[j]; 1143*0Sstevel@tonic-gate ap[j] = ap[j+1]; 1144*0Sstevel@tonic-gate ap[j+1] = hp; 1145*0Sstevel@tonic-gate 1146*0Sstevel@tonic-gate } else 1147*0Sstevel@tonic-gate break; 1148*0Sstevel@tonic-gate } 1149*0Sstevel@tonic-gate needsort++; 1150*0Sstevel@tonic-gate } 1151*0Sstevel@tonic-gate } 1152*0Sstevel@tonic-gate 1153*0Sstevel@tonic-gate static int 1154*0Sstevel@tonic-gate init(struct irs_ho *this) { 1155*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 1156*0Sstevel@tonic-gate 1157*0Sstevel@tonic-gate if (!pvt->res && !ho_res_get(this)) 1158*0Sstevel@tonic-gate return (-1); 1159*0Sstevel@tonic-gate if (((pvt->res->options & RES_INIT) == 0) && 1160*0Sstevel@tonic-gate res_ninit(pvt->res) == -1) 1161*0Sstevel@tonic-gate return (-1); 1162*0Sstevel@tonic-gate return (0); 1163*0Sstevel@tonic-gate } 1164