• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

ink_res_init.cc

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1985, 1989, 1993
00003  *    The Regents of the University of California.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
00032  *
00033  * Permission to use, copy, modify, and distribute this software for any
00034  * purpose with or without fee is hereby granted, provided that the above
00035  * copyright notice and this permission notice appear in all copies, and that
00036  * the name of Digital Equipment Corporation not be used in advertising or
00037  * publicity pertaining to distribution of the document or software without
00038  * specific, written prior permission.
00039  *
00040  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
00041  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
00042  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
00043  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00044  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00045  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00046  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00047  * SOFTWARE.
00048  */
00049 
00050 /*
00051  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
00052  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
00053  *
00054  * Permission to use, copy, modify, and distribute this software for any
00055  * purpose with or without fee is hereby granted, provided that the above
00056  * copyright notice and this permission notice appear in all copies.
00057  *
00058  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
00059  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00060  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
00061  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00062  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00063  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
00064  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00065  */
00066 
00067 
00068 #include "ink_platform.h"
00069 #include "ink_defs.h"
00070 
00071 #include <sys/types.h>
00072 #include <sys/param.h>
00073 #include <sys/socket.h>
00074 #include <sys/time.h>
00075 #include <netinet/in.h>
00076 #include <arpa/inet.h>
00077 #include <arpa/nameser.h>
00078 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
00079 #include <arpa/nameser_compat.h>
00080 #endif
00081 #include <stdio.h>
00082 #include <ctype.h>
00083 #include <resolv.h>
00084 #include <unistd.h>
00085 #include <stdlib.h>
00086 #include <string.h>
00087 
00088 #include "ink_string.h"
00089 #include "ink_resolver.h"
00090 #include "ink_inet.h"
00091 #include "Tokenizer.h"
00092 
00093 #if !defined(isascii)           /* XXX - could be a function */
00094 # define isascii(c) (!(c & 0200))
00095 #endif
00096 
00097 HostResPreferenceOrder const HOST_RES_DEFAULT_PREFERENCE_ORDER = {
00098   HOST_RES_PREFER_IPV4,
00099   HOST_RES_PREFER_IPV6,
00100   HOST_RES_PREFER_NONE
00101 };
00102 
00103 HostResPreferenceOrder host_res_default_preference_order;
00104 
00105 char const* const HOST_RES_PREFERENCE_STRING[N_HOST_RES_PREFERENCE] = {
00106     "only", "client", "ipv4", "ipv6"
00107 };
00108 
00109 char const* const HOST_RES_STYLE_STRING[] = {
00110   "invalid", "IPv4", "IPv4 only", "IPv6", "IPv6 only"
00111 };
00112 
00113 /*%
00114  * This routine is for closing the socket if a virtual circuit is used and
00115  * the program wants to close it.  This provides support for endhostent()
00116  * which expects to close the socket.
00117  *
00118  * This routine is not expected to be user visible.
00119  */
00120 static void
00121 ink_res_nclose(ink_res_state statp) {
00122   if (statp->_vcsock >= 0) {
00123     (void) close(statp->_vcsock);
00124     statp->_vcsock = -1;
00125     statp->_flags &= ~(INK_RES_F_VC | INK_RES_F_CONN);
00126   }
00127 }
00128 
00129 static void
00130 ink_res_setservers(ink_res_state statp, IpEndpoint const* set, int cnt) {
00131   /* close open servers */
00132   ink_res_nclose(statp);
00133 
00134   /* cause rtt times to be forgotten */
00135   statp->nscount = 0;
00136 
00137   /* The goal here seems to be to compress the source list (@a set) by
00138      squeezing out invalid addresses. We handle the special case where
00139      the destination and sourcea are the same.
00140   */
00141   int nserv = 0;
00142   for ( IpEndpoint const* limit = set + cnt ; nserv < INK_MAXNS && set < limit ; ++set ) {
00143     IpEndpoint* dst = &statp->nsaddr_list[nserv];
00144 
00145     if (dst == set) {
00146       if (ats_is_ip(&set->sa))
00147         ++nserv;
00148     } else if (ats_ip_copy(&dst->sa, &set->sa)) {
00149       ++nserv;
00150     }
00151   }
00152   statp->nscount = nserv;
00153 }
00154 
00155 int
00156 ink_res_getservers(ink_res_state statp, sockaddr *set, int cnt) {
00157   int zret = 0; // return count.
00158   IpEndpoint const* src = statp->nsaddr_list;
00159 
00160   for (int i = 0; i < statp->nscount && i < cnt; ++i, ++src) {
00161     if (ats_ip_copy(set, &src->sa)) {
00162       ++set;
00163       ++zret;
00164     }
00165   }
00166   return zret;
00167 }
00168 
00169 static void
00170 ink_res_setoptions(ink_res_state statp, const char *options, const char *source ATS_UNUSED)
00171 {
00172   const char *cp = options;
00173   int i;
00174 
00175 #ifdef DEBUG
00176   if (statp->options & INK_RES_DEBUG)
00177     printf(";; res_setoptions(\"%s\", \"%s\")...\n", options, source);
00178 #endif
00179   while (*cp) {
00180     /* skip leading and inner runs of spaces */
00181     while (*cp == ' ' || *cp == '\t')
00182       cp++;
00183     /* search for and process individual options */
00184     if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) {
00185       i = atoi(cp + sizeof("ndots:") - 1);
00186       if (i <= INK_RES_MAXNDOTS)
00187         statp->ndots = i;
00188       else
00189         statp->ndots = INK_RES_MAXNDOTS;
00190 #ifdef DEBUG
00191       if (statp->options & INK_RES_DEBUG)
00192         printf(";;\tndots=%d\n", statp->ndots);
00193 #endif
00194     } else if (!strncmp(cp, "timeout:", sizeof("timeout:") - 1)) {
00195       i = atoi(cp + sizeof("timeout:") - 1);
00196       if (i <= INK_RES_MAXRETRANS)
00197         statp->retrans = i;
00198       else
00199         statp->retrans = INK_RES_MAXRETRANS;
00200 #ifdef DEBUG
00201       if (statp->options & INK_RES_DEBUG)
00202         printf(";;\ttimeout=%d\n", statp->retrans);
00203 #endif
00204 #ifdef  SOLARIS2
00205     } else if (!strncmp(cp, "retrans:", sizeof("retrans:") - 1)) {
00206       /*
00207        * For backward compatibility, 'retrans' is
00208        * supported as an alias for 'timeout', though
00209        * without an imposed maximum.
00210        */
00211       statp->retrans = atoi(cp + sizeof("retrans:") - 1);
00212     } else if (!strncmp(cp, "retry:", sizeof("retry:") - 1)){
00213       /*
00214        * For backward compatibility, 'retry' is
00215        * supported as an alias for 'attempts', though
00216        * without an imposed maximum.
00217        */
00218       statp->retry = atoi(cp + sizeof("retry:") - 1);
00219 #endif  /* SOLARIS2 */
00220     } else if (!strncmp(cp, "attempts:", sizeof("attempts:") - 1)){
00221       i = atoi(cp + sizeof("attempts:") - 1);
00222       if (i <= INK_RES_MAXRETRY)
00223         statp->retry = i;
00224       else
00225         statp->retry = INK_RES_MAXRETRY;
00226 #ifdef DEBUG
00227       if (statp->options & INK_RES_DEBUG)
00228         printf(";;\tattempts=%d\n", statp->retry);
00229 #endif
00230     } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) {
00231 #ifdef DEBUG
00232       if (!(statp->options & INK_RES_DEBUG)) {
00233         printf(";; res_setoptions(\"%s\", \"%s\")..\n", options, source);
00234         statp->options |= INK_RES_DEBUG;
00235       }
00236       printf(";;\tdebug\n");
00237 #endif
00238     } else if (!strncmp(cp, "no_tld_query", sizeof("no_tld_query") - 1) ||
00239                !strncmp(cp, "no-tld-query", sizeof("no-tld-query") - 1)) {
00240       statp->options |= INK_RES_NOTLDQUERY;
00241     } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) {
00242       statp->options |= INK_RES_USE_INET6;
00243     } else if (!strncmp(cp, "rotate", sizeof("rotate") - 1)) {
00244       statp->options |= INK_RES_ROTATE;
00245     } else if (!strncmp(cp, "no-check-names", sizeof("no-check-names") - 1)) {
00246       statp->options |= INK_RES_NOCHECKNAME;
00247     }
00248 #ifdef INK_RES_USE_EDNS0
00249     else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) {
00250       statp->options |= INK_RES_USE_EDNS0;
00251     }
00252 #endif
00253     else if (!strncmp(cp, "dname", sizeof("dname") - 1)) {
00254       statp->options |= INK_RES_USE_DNAME;
00255     }
00256     else {
00257       /* XXX - print a warning here? */
00258     }
00259     /* skip to next run of spaces */
00260     while (*cp && *cp != ' ' && *cp != '\t')
00261       cp++;
00262   }
00263 }
00264 
00265 static u_int
00266 ink_res_randomid(void) {
00267   struct timeval now;
00268 
00269   gettimeofday(&now, NULL);
00270   return (0xffff & (now.tv_sec ^ now.tv_usec ^ getpid()));
00271 }
00272 
00273 /*%
00274  * Set up default settings.  If the configuration file exist, the values
00275  * there will have precedence.  Otherwise, the server address is set to
00276  * INADDR_ANY and the default domain name comes from the gethostname().
00277  *
00278  * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1
00279  * rather than INADDR_ANY ("0.0.0.0") as the default name server address
00280  * since it was noted that INADDR_ANY actually meant ``the first interface
00281  * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface,
00282  * it had to be "up" in order for you to reach your own name server.  It
00283  * was later decided that since the recommended practice is to always
00284  * install local static routes through 127.0.0.1 for all your network
00285  * interfaces, that we could solve this problem without a code change.
00286  *
00287  * The configuration file should always be used, since it is the only way
00288  * to specify a default domain.  If you are running a server on your local
00289  * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1"
00290  * in the configuration file.
00291  *
00292  * Return 0 if completes successfully, -1 on error
00293  *
00294  * @internal This function has to be reachable by res_data.c but not publically.
00295  */
00296 int
00297 ink_res_init(
00298   ink_res_state statp, ///< State object to update.
00299   IpEndpoint const* pHostList, ///< Additional servers.
00300   size_t pHostListSize, ///< # of entries in @a pHostList.
00301   const char *pDefDomain, ///< Default domain (may be NULL).
00302   const char *pSearchList, ///< Unknown
00303   const char *pResolvConf ///< Path to configuration file.
00304 ) {
00305   FILE *fp;
00306   char *cp, **pp;
00307   int n;
00308   char buf[BUFSIZ];
00309   size_t nserv = 0;
00310   int haveenv = 0;
00311   int havesearch = 0;
00312   int dots;
00313   size_t maxns = INK_MAXNS;
00314 
00315   // INK_RES_SET_H_ERRNO(statp, 0);
00316   statp->res_h_errno = 0;
00317 
00318   statp->retrans = INK_RES_TIMEOUT;
00319   statp->retry = INK_RES_DFLRETRY;
00320   statp->options = INK_RES_DEFAULT;
00321   statp->id = ink_res_randomid();
00322 
00323   statp->nscount = 0;
00324   statp->ndots = 1;
00325   statp->pfcode = 0;
00326   statp->_vcsock = -1;
00327   statp->_flags = 0;
00328   statp->qhook = NULL;
00329   statp->rhook = NULL;
00330 
00331 #ifdef  SOLARIS2
00332   /*
00333    * The old libresolv derived the defaultdomain from NIS/NIS+.
00334    * We want to keep this behaviour
00335    */
00336   {
00337     char buf[sizeof(statp->defdname)], *cp;
00338     int ret;
00339 
00340     if ((ret = sysinfo(SI_SRPC_DOMAIN, buf, sizeof(buf))) > 0 &&
00341         (unsigned int)ret <= sizeof(buf)) {
00342       if (buf[0] == '+')
00343         buf[0] = '.';
00344       cp = strchr(buf, '.');
00345       cp = (cp == NULL) ? buf : (cp + 1);
00346       ink_strlcpy(statp->defdname, cp, sizeof(statp->defdname));
00347     }
00348   }
00349 #endif  /* SOLARIS2 */
00350 
00351         /* Allow user to override the local domain definition */
00352   if ((cp = getenv("LOCALDOMAIN")) != NULL) {
00353     (void)ink_strlcpy(statp->defdname, cp, sizeof(statp->defdname));
00354     haveenv++;
00355 
00356     /*
00357      * Set search list to be blank-separated strings
00358      * from rest of env value.  Permits users of LOCALDOMAIN
00359      * to still have a search list, and anyone to set the
00360      * one that they want to use as an individual (even more
00361      * important now that the rfc1535 stuff restricts searches)
00362      */
00363     cp = statp->defdname;
00364     pp = statp->dnsrch;
00365     *pp++ = cp;
00366     for (n = 0; *cp && pp < statp->dnsrch + INK_MAXDNSRCH; cp++) {
00367       if (*cp == '\n')  /*%< silly backwards compat */
00368         break;
00369       else if (*cp == ' ' || *cp == '\t') {
00370         *cp = 0;
00371         n = 1;
00372       } else if (n) {
00373         *pp++ = cp;
00374         n = 0;
00375         havesearch = 1;
00376       }
00377     }
00378     /* null terminate last domain if there are excess */
00379     while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n')
00380       cp++;
00381     *cp = '\0';
00382     *pp++ = 0;
00383   }
00384 
00385   /* ---------------------------------------------
00386      Default domain name and doamin Search list:
00387 
00388      if we are supplied a default domain name,
00389      and/or search list we will use it. Otherwise,
00390      we will skip to using  what is present in the
00391      conf file
00392      ---------------------------------------------- */
00393 
00394   if (pDefDomain && '\0' != *pDefDomain && '\n' != *pDefDomain) {
00395     ink_strlcpy(statp->defdname, pDefDomain, sizeof(statp->defdname));
00396     if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
00397       *cp = '\0';
00398   }
00399   if (pSearchList && '\0' != *pSearchList && '\n' != *pSearchList) {
00400     ink_strlcpy(statp->defdname, pSearchList, sizeof(statp->defdname));
00401     if ((cp = strchr(statp->defdname, '\n')) != NULL)
00402       *cp = '\0';
00403     /*
00404      * Set search list to be blank-separated strings
00405      * on rest of line.
00406      */
00407     cp = statp->defdname;
00408     pp = statp->dnsrch;
00409     *pp++ = cp;
00410     for (n = 0; *cp && pp < statp->dnsrch + INK_MAXDNSRCH; cp++) {
00411       if (*cp == ' ' || *cp == '\t') {
00412         *cp = 0;
00413         n = 1;
00414       } else if (n) {
00415         *pp++ = cp;
00416         n = 0;
00417       }
00418     }
00419     /* null terminate last domain if there are excess */
00420     while (*cp != '\0' && *cp != ' ' && *cp != '\t')
00421       cp++;
00422     *cp = '\0';
00423     *pp++ = 0;
00424     havesearch = 1;
00425   }
00426 
00427   /* -------------------------------------------
00428      we must be provided with atleast a named!
00429      ------------------------------------------- */
00430   if (pHostList) {
00431     if (pHostListSize > INK_MAXNS) pHostListSize = INK_MAXNS;
00432     for (
00433         ; nserv < pHostListSize
00434           && ats_is_ip(&pHostList[nserv].sa) 
00435         ; ++nserv
00436     ) {
00437       ats_ip_copy(&statp->nsaddr_list[nserv].sa, &pHostList[nserv].sa);
00438     }
00439   }
00440 
00441 #define MATCH(line, name)                       \
00442   (!strncmp(line, name, sizeof(name) - 1) &&    \
00443    (line[sizeof(name) - 1] == ' ' ||            \
00444     line[sizeof(name) - 1] == '\t'))
00445 
00446   if ((fp = fopen(pResolvConf, "r")) != NULL) {
00447     /* read the config file */
00448     while (fgets(buf, sizeof(buf), fp) != NULL) {
00449       /* skip comments */
00450       if (*buf == ';' || *buf == '#')
00451         continue;
00452       /* read default domain name */
00453       if (MATCH(buf, "domain")) {
00454         if (haveenv)    /*%< skip if have from environ */
00455           continue;
00456         cp = buf + sizeof("domain") - 1;
00457         while (*cp == ' ' || *cp == '\t')
00458           cp++;
00459         if ((*cp == '\0') || (*cp == '\n'))
00460           continue;
00461         ink_strlcpy(statp->defdname, cp, sizeof(statp->defdname));
00462         if ((cp = strpbrk(statp->defdname, " \t\n")) != NULL)
00463           *cp = '\0';
00464         havesearch = 0;
00465         continue;
00466       }
00467       /* set search list */
00468       if (MATCH(buf, "search")) {
00469         if (haveenv)    /*%< skip if have from environ */
00470           continue;
00471         cp = buf + sizeof("search") - 1;
00472         while (*cp == ' ' || *cp == '\t')
00473           cp++;
00474         if ((*cp == '\0') || (*cp == '\n'))
00475           continue;
00476         ink_strlcpy(statp->defdname, cp, sizeof(statp->defdname));
00477         if ((cp = strchr(statp->defdname, '\n')) != NULL)
00478           *cp = '\0';
00479         /*
00480          * Set search list to be blank-separated strings
00481          * on rest of line.
00482          */
00483         cp = statp->defdname;
00484         pp = statp->dnsrch;
00485         *pp++ = cp;
00486         for (n = 0; *cp && pp < statp->dnsrch + INK_MAXDNSRCH; cp++) {
00487           if (*cp == ' ' || *cp == '\t') {
00488             *cp = 0;
00489             n = 1;
00490           } else if (n) {
00491             *pp++ = cp;
00492             n = 0;
00493           }
00494         }
00495         /* null terminate last domain if there are excess */
00496         while (*cp != '\0' && *cp != ' ' && *cp != '\t')
00497           cp++;
00498         *cp = '\0';
00499         *pp++ = 0;
00500         havesearch = 1;
00501         continue;
00502       }
00503       /* read nameservers to query */
00504       if (MATCH(buf, "nameserver") && nserv < maxns) {
00505         struct addrinfo hints, *ai;
00506         char sbuf[NI_MAXSERV];
00507 
00508         cp = buf + sizeof("nameserver") - 1;
00509         while (*cp == ' ' || *cp == '\t')
00510           cp++;
00511         cp[strcspn(cp, ";# \t\n")] = '\0';
00512         if ((*cp != '\0') && (*cp != '\n')) {
00513           memset(&hints, 0, sizeof(hints));
00514           hints.ai_family = PF_UNSPEC;
00515           hints.ai_socktype = SOCK_DGRAM;       /*dummy*/
00516           hints.ai_flags = AI_NUMERICHOST;
00517           sprintf(sbuf, "%d", NAMESERVER_PORT);
00518           if (getaddrinfo(cp, sbuf, &hints, &ai) == 0) {
00519             if (ats_ip_copy(
00520                 &statp->nsaddr_list[nserv].sa,
00521                 ai->ai_addr
00522             ))
00523               ++nserv;
00524             freeaddrinfo(ai);
00525           }
00526         }
00527         continue;
00528       }
00529       if (MATCH(buf, "options")) {
00530         ink_res_setoptions(statp, buf + sizeof("options") - 1, "conf");
00531         continue;
00532       }
00533     }
00534     (void) fclose(fp);
00535   }
00536 
00537   if (nserv > 0)
00538     statp->nscount = nserv;
00539 
00540   if (statp->defdname[0] == 0 && gethostname(buf, sizeof(statp->defdname) - 1) == 0 && (cp = strchr(buf, '.')) != NULL)
00541     ink_strlcpy(statp->defdname, cp + 1, sizeof(statp->defdname));
00542 
00543   /* find components of local domain that might be searched */
00544   if (havesearch == 0) {
00545     pp = statp->dnsrch;
00546     *pp++ = statp->defdname;
00547     *pp = NULL;
00548 
00549     dots = 0;
00550     for (cp = statp->defdname; *cp; cp++)
00551       dots += (*cp == '.');
00552 
00553     cp = statp->defdname;
00554     while (pp < statp->dnsrch + INK_MAXDFLSRCH) {
00555       if (dots < INK_LOCALDOMAINPARTS)
00556         break;
00557       cp = strchr(cp, '.') + 1;    /*%< we know there is one */
00558       *pp++ = cp;
00559       dots--;
00560     }
00561     *pp = NULL;
00562 #ifdef DEBUG
00563     if (statp->options & INK_RES_DEBUG) {
00564       printf(";; res_init()... default dnsrch list:\n");
00565       for (pp = statp->dnsrch; *pp; pp++)
00566         printf(";;\t%s\n", *pp);
00567       printf(";;\t..END..\n");
00568     }
00569 #endif
00570   }
00571 
00572   /* export all ns servers to DNSprocessor. */
00573   ink_res_setservers(statp, &statp->nsaddr_list[0], statp->nscount);
00574 
00575   if ((cp = getenv("RES_OPTIONS")) != NULL)
00576     ink_res_setoptions(statp, cp, "env");
00577   statp->options |= INK_RES_INIT;
00578   return (statp->res_h_errno);
00579 }
00580 
00581 void
00582 parse_host_res_preference(char const* value, HostResPreferenceOrder order) {
00583   Tokenizer tokens(";/|");
00584   // preference from the config string.
00585   int np = 0; // index in to @a m_host_res_preference
00586   bool found[N_HOST_RES_PREFERENCE];  // redundancy check array
00587   int n; // # of tokens
00588   int i; // index
00589 
00590   n = tokens.Initialize(value);
00591 
00592   for ( i = 0 ; i < N_HOST_RES_PREFERENCE ; ++i )
00593     found[i] = false;
00594 
00595   for ( i = 0 ; i < n && np < N_HOST_RES_PREFERENCE_ORDER ; ++i ) {
00596     char const* elt = tokens[i];
00597     // special case none/only because that terminates the sequence.
00598     if (0 == strcasecmp(elt, HOST_RES_PREFERENCE_STRING[HOST_RES_PREFER_NONE])) {
00599       found[HOST_RES_PREFER_NONE] = true;
00600       order[np] = HOST_RES_PREFER_NONE;
00601       break;
00602     } else {
00603       // scan the other types
00604       HostResPreference ep = HOST_RES_PREFER_NONE;
00605       for ( int ip = HOST_RES_PREFER_NONE + 1 ; ip < N_HOST_RES_PREFERENCE ; ++ip ) {
00606         if (0 == strcasecmp(elt, HOST_RES_PREFERENCE_STRING[ip])) {
00607           ep = static_cast<HostResPreference>(ip);
00608           break;
00609         }
00610       }
00611       if (HOST_RES_PREFER_NONE != ep && !found[ep]) { // ignore duplicates
00612         found[ep] = true;
00613         order[np++] = ep;
00614       }
00615     }
00616   }
00617 
00618   if (!found[HOST_RES_PREFER_NONE]) {
00619     // If 'only' wasn't explicit, fill in the rest by default.
00620     if (!found[HOST_RES_PREFER_IPV4])
00621       order[np++] = HOST_RES_PREFER_IPV4;
00622     if (!found[HOST_RES_PREFER_IPV6])
00623       order[np++] = HOST_RES_PREFER_IPV6;
00624     if (np < N_HOST_RES_PREFERENCE_ORDER) // was N_HOST_RES_PREFERENCE)
00625       order[np] = HOST_RES_PREFER_NONE;
00626   }
00627 }      
00628 
00629 int
00630 ts_host_res_order_to_string(HostResPreferenceOrder const& order, char* out, int size)
00631 {
00632   int zret = 0;
00633   bool first = true;
00634   for ( int i = 0 ; i < N_HOST_RES_PREFERENCE_ORDER ; ++i ) {
00635     /* Note we use a semi-colon here because this must be compatible
00636      * with the -httpport command line option which uses comma to
00637      * separate port descriptors so we cannot use that to separate
00638      * resolution key words.
00639      */
00640     zret += snprintf(out+zret, size-zret, "%s%s", !first ? ";" : "", HOST_RES_PREFERENCE_STRING[order[i]]);
00641     if (HOST_RES_PREFER_NONE == order[i])
00642       break;
00643     first = false;
00644   }
00645   return zret;
00646 }

Generated by  doxygen 1.7.1