Annotation of libwww/Library/src/HTDNS.c, revision 2.32.2.2

2.1       frystyk     1: /*                                                                     HTDNS.c
                      2: **     DOMAIN NAME SERVICE MANAGER
                      3: **
2.32.2.1  duerst      4: **     (c) COPYRIGHT MIT 1995-2003.
2.1       frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
2.32.2.2! duerst      6: **     @(#) $Id: HTDNS.c,v 2.32.2.1 2003/03/01 22:51:00 duerst Exp $
2.1       frystyk     7: **
                      8: **     This object manages a cache of hosts we have looked up vis DNS.
                      9: **     The object contains the necessary parts from hostent. For Internet host
                     10: **     hostent->h_addr_list is not an array of char pointers but an array of 
                     11: **     pointers of type in_addr.
                     12: **
                     13: **     13 Sep 95  HFN  Spawned from HTTCP.c and rewritten
2.32.2.1  duerst     14: **      23 Feb 03  MJD  Started working on IDN implementation
2.1       frystyk    15: */
                     16: 
                     17: /* Library include files */
2.26      frystyk    18: #include "wwwsys.h"
2.21      frystyk    19: #include "WWWUtil.h"
2.1       frystyk    20: #include "HTParse.h"
                     21: #include "HTAlert.h"
                     22: #include "HTError.h"
2.21      frystyk    23: #include "HTTrans.h"
2.23      frystyk    24: #include "HTHstMan.h"
2.1       frystyk    25: #include "HTDNS.h"                                      /* Implemented here */
                     26: 
2.32.2.2! duerst     27: #include "idn/api.h"
        !            28: 
2.25      frystyk    29: #define DNS_TIMEOUT            1800L        /* Default DNS timeout is 30 mn */
2.1       frystyk    30: 
                     31: /* Type definitions and global variables etc. local to this module */
                     32: struct _HTdns {
2.21      frystyk    33:     char *             hostname;                            /* name of host */
2.1       frystyk    34:     time_t             ntime;                              /* Creation time */
                     35:     int                        addrlength;            /* Length of address in bytes */
                     36:     int                        homes;         /* Number of IP addresses on the host */
                     37:     char **            addrlist;      /* List of addresses from name server */
                     38:     double *           weight;                    /* Weight on each address */
                     39: };
                     40: 
2.2       frystyk    41: PRIVATE HTList **CacheTable = NULL;
                     42: PRIVATE time_t DNSTimeout = DNS_TIMEOUT;          /* Timeout on DNS entries */
2.1       frystyk    43: 
                     44: /* ------------------------------------------------------------------------- */
                     45: 
2.20      frystyk    46: PRIVATE void free_object (HTdns * me)
2.1       frystyk    47: {
                     48:     if (me) {
2.17      frystyk    49:        HT_FREE(me->hostname);
2.1       frystyk    50:        if (*me->addrlist)
2.17      frystyk    51:            HT_FREE(*me->addrlist);
                     52:        HT_FREE(me->addrlist);
                     53:        HT_FREE(me->weight);
                     54:        HT_FREE(me);
2.1       frystyk    55:     }
                     56: }
                     57: 
2.21      frystyk    58: PRIVATE BOOL delete_object (HTList * list, HTdns * me)
2.1       frystyk    59: {
2.30      frystyk    60:     HTTRACE(PROT_TRACE, "DNS Delete.. object %p from list %p\n" _ me _ list);
2.1       frystyk    61:     HTList_removeObject(list, (void *) me);
2.20      frystyk    62:     free_object(me);
2.1       frystyk    63:     return YES;
                     64: }
                     65: 
                     66: /*     HTDNS_setTimeout
                     67: **     ----------------
                     68: **     Set the cache timeout for DNS entries. Default is DNS_TIMEOUT
                     69: */
                     70: PUBLIC void HTDNS_setTimeout (time_t timeout)
                     71: {
                     72:     DNSTimeout = timeout;
                     73: }
                     74: 
                     75: /*     HTDNS_timeout
                     76: **     -------------
                     77: **     Get the cache timeout 
                     78: */
                     79: PUBLIC time_t HTDNS_timeout (time_t timeout)
                     80: {
                     81:     return DNSTimeout;
                     82: }
                     83: 
                     84: /*     HTDNS_add
                     85: **     ---------
                     86: **     Add an element to the cache of visited hosts. Note that this function
                     87: **     requires the system implemented structure hostent and not our own
2.21      frystyk    88: **     host_info. The homes variable indicates the number of IP addresses 
                     89: **     found. A host name must NOT contain a port number.
                     90: **     Returns address of new HTdns object
2.1       frystyk    91: */
2.22      frystyk    92: PUBLIC HTdns * HTDNS_add (HTList * list, struct hostent * element,
                     93:                          char *host, int *homes)
2.1       frystyk    94: {
                     95:     HTdns *me;
2.21      frystyk    96:     char *addr = NULL;
2.1       frystyk    97:     char **index = element->h_addr_list;
                     98:     int cnt = 1;
                     99: 
                    100:     while(*index++) cnt++;
2.17      frystyk   101:     if ((me = (HTdns *) HT_CALLOC(1, sizeof(HTdns))) == NULL ||
                    102:        (me->addrlist = (char **) HT_CALLOC(1, cnt*sizeof(char*))) == NULL ||
                    103:        (addr = (char *) HT_CALLOC(1, cnt*element->h_length)) == NULL)
                    104:        HT_OUTOFMEM("HTDNS_add");
2.1       frystyk   105:     StrAllocCopy(me->hostname, host);
                    106:     me->ntime = time(NULL);
                    107:     index = element->h_addr_list;
                    108:     cnt = 0;
                    109:     while (*index) {
                    110:        *(me->addrlist+cnt) = addr+cnt*element->h_length;
                    111:        memcpy((void *) *(me->addrlist+cnt++), *index++, element->h_length);
                    112:     }
                    113:     me->homes = cnt;
                    114:     *homes = cnt;
2.17      frystyk   115:     if ((me->weight = (double *) HT_CALLOC(me->homes, sizeof(double))) == NULL)
                    116:         HT_OUTOFMEM("HTDNS_add");
2.1       frystyk   117:     me->addrlength = element->h_length;
2.30      frystyk   118:     HTTRACE(PROT_TRACE, "DNS Add..... `%s\' with %d home(s) to %p\n" _ 
                    119:                host _ *homes _ list);
2.1       frystyk   120:     HTList_addObject(list, (void *) me);
                    121:     return me;
                    122: }
                    123: 
                    124: 
                    125: /*     HTDNS_updateWeights
                    126: **     -------------------
                    127: **     This function calculates the weights of the different IP addresses
                    128: **     on a multi homed host. Each weight is calculated as
                    129: **
                    130: **             w(n+1) = w(n)*a + (1-a) * deltatime
                    131: **             a = exp(-1/Neff)
                    132: **             Neff is the effective number of samples used
                    133: **             deltatime is time spend on making a connection
                    134: **
                    135: **     A short window (low Neff) gives a high sensibility, but this is
                    136: **     required as we can't expect a lot of data to test on.
2.21      frystyk   137: **     "current" is the index returned by HTGetHostByName()
2.1       frystyk   138: */
2.24      frystyk   139: PUBLIC BOOL HTDNS_updateWeigths(HTdns *dns, int current, ms_t deltatime)
2.1       frystyk   140: {
                    141:     if (dns) {
                    142:        int cnt;
2.19      frystyk   143:        const double passive = 0.9;       /* Factor for all passive IP_addrs */
2.1       frystyk   144: #if 0
2.19      frystyk   145:        const int Neff = 3;
                    146:        const double alpha = exp(-1.0/Neff);
2.1       frystyk   147: #else
2.19      frystyk   148:        const double alpha = 0.716531310574;    /* Doesn't need the math lib */
2.1       frystyk   149: #endif
                    150:        for (cnt=0; cnt<dns->homes; cnt++) {
                    151:            if (cnt == current) {
                    152:                *(dns->weight+current) = *(dns->weight+current)*alpha + (1.0-alpha)*deltatime;
2.16      frystyk   153:                if (*(dns->weight+current) < 0.0) *(dns->weight+current) = 0.0;
2.1       frystyk   154:            } else {
                    155:                *(dns->weight+cnt) = *(dns->weight+cnt) * passive;
                    156:            }
2.30      frystyk   157:            HTTRACE(PROT_TRACE, "DNS weight.. Home %d has weight %4.2f\n" _ cnt _ 
2.1       frystyk   158:                        *(dns->weight+cnt));
                    159:        }
                    160:        return YES;
                    161:     }
2.30      frystyk   162:     HTTRACE(PROT_TRACE, "DNS weight.. Object %p not found'\n" _ dns);
2.1       frystyk   163:     return NO;
                    164: }
                    165: 
                    166: /*     HTDNS_delete
                    167: **     ------------
                    168: **     Remove an element from the cache
                    169: */
2.19      frystyk   170: PUBLIC BOOL HTDNS_delete (const char * host)
2.1       frystyk   171: {
                    172:     HTList *list;
                    173:     int hash = 0;
2.19      frystyk   174:     const char *ptr;
2.1       frystyk   175:     if (!host || !CacheTable) return NO;
                    176:     for(ptr=host; *ptr; ptr++)
2.29      frystyk   177:        hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HT_M_HASH_SIZE);
2.1       frystyk   178:     if ((list = CacheTable[hash])) {    /* We have the list, find the entry */
                    179:        HTdns *pres;
                    180:        while ((pres = (HTdns *) HTList_nextObject(list))) {
                    181:            if (!strcmp(pres->hostname, host)) {
                    182:                delete_object(CacheTable[hash], pres);
                    183:                break;
                    184:            }
                    185:        }
                    186:     }
                    187:     return YES;
                    188: }
                    189: 
                    190: /*     HTDNS_deleteAll
                    191: **     ---------------
                    192: **     Destroys the cache completely
                    193: */
                    194: PUBLIC BOOL HTDNS_deleteAll (void)
                    195: {
                    196:     int cnt;
                    197:     HTList *cur;
                    198:     if (!CacheTable) return NO;
2.29      frystyk   199:     for (cnt=0; cnt<HT_M_HASH_SIZE; cnt++) {
2.1       frystyk   200:        if ((cur = CacheTable[cnt])) { 
                    201:            HTdns *pres;
                    202:            while ((pres = (HTdns *) HTList_nextObject(cur)) != NULL)
2.20      frystyk   203:                free_object(pres);
2.1       frystyk   204:        }
                    205:        HTList_delete(CacheTable[cnt]);
                    206:        CacheTable[cnt] = NULL;
2.2       frystyk   207:     }
2.21      frystyk   208:     HT_FREE(CacheTable);
2.2       frystyk   209:     return YES;
                    210: }
2.1       frystyk   211: 
2.32.2.1  duerst    212: /*     Decode one hex character
                    213: */
                    214: /* copied from HTSRC.c, where it is not used */
                    215: PRIVATE long from_hex (char c)
                    216: {
                    217:     return               (c>='0')&&(c<='9') ? c-'0'
                    218:                        : (c>='A')&&(c<='F') ? c-'A'+10
                    219:                        : (c>='a')&&(c<='f') ? c-'a'+10
                    220:                        :                      0;
                    221: }
                    222: 
2.1       frystyk   223: /*     HTGetHostByName
                    224: **     ---------------
                    225: **     Resolve the host name using internal DNS cache. As we want to refer   
                    226: **     a specific host when timing the connection the weight function must
                    227: **     use the 'current' value as returned.
                    228: **      Returns:
                    229: **             >0      Number of homes
                    230: **             -1      Error
                    231: */
2.23      frystyk   232: PUBLIC int HTGetHostByName (HTHost * host, char *hostname, HTRequest* request)
2.1       frystyk   233: {
2.23      frystyk   234:     SockA *sin = HTHost_getSockAddr(host);
2.1       frystyk   235:     int homes = -1;
                    236:     HTList *list;                                  /* Current list in cache */
                    237:     HTdns *pres = NULL;
2.23      frystyk   238:     if (!host || !hostname) {
2.30      frystyk   239:        HTTRACE(PROT_TRACE, "HostByName.. Bad argument\n");
2.1       frystyk   240:        return -1;
                    241:     }
2.23      frystyk   242:     HTHost_setHome(host, 0);
2.1       frystyk   243:     
2.32.2.1  duerst    244:     /*
                    245:     **
                    246:     ** Code to deal with International Domain Names
                    247:     **
                    248:     */
                    249:     /* unescape %-escapes from URI */
                    250:     {
                    251:        char *ptr, *ptr2;
                    252:        for(ptr=ptr2=hostname; *ptr; ) {
                    253:                if (*ptr=='%' && isxdigit(*(ptr+1)) && isxdigit(*(ptr+2))) {
                    254:                        /* isxdigit() includes check for end of string */
                    255:                    unsigned char unesc = (unsigned char) (from_hex(*(ptr+1))*16 + from_hex(*(ptr+2)));
                    256:                    if (unesc=='.')  *ptr2++ = *ptr++;   /* just copy, because reserved, for security */
                    257:                    else {
                    258:                        *ptr2++ = (char) unesc;
                    259:                        ptr += 3;
                    260:                    }
                    261:                } else *ptr2++ = *ptr++;   /* just copy */
                    262:        }
                    263:        *ptr2=*ptr;
                    264:     }
                    265:     /* still to do: check for valid UTF-8 */
                    266:     /* still to do: call stringprep/nameprep */
2.32.2.2! duerst    267:     {
        !           268:          idn_result_t result;
        !           269:     }
2.32.2.1  duerst    270:     /*
                    271:     ** End of code to deal with International Domain Names
                    272:     */
                    273: 
2.1       frystyk   274:     /* Find a hash for this host */
                    275:     {
                    276:        int hash = 0;
                    277:        char *ptr;
2.23      frystyk   278:        for(ptr=hostname; *ptr; ptr++)
2.29      frystyk   279:            hash = (int) ((hash * 3 + (*(unsigned char *) ptr)) % HT_M_HASH_SIZE);
2.1       frystyk   280:        if (!CacheTable) {
2.29      frystyk   281:            if ((CacheTable = (HTList* *) HT_CALLOC(HT_M_HASH_SIZE, sizeof(HTList *))) == NULL)
2.17      frystyk   282:                HT_OUTOFMEM("HTDNS_init");
2.1       frystyk   283:        }
                    284:        if (!CacheTable[hash]) CacheTable[hash] = HTList_new();
                    285:        list = CacheTable[hash];
                    286:     }
                    287: 
                    288:     /* Search the cache */
                    289:     {
                    290:        HTList *cur = list;
                    291:        while ((pres = (HTdns *) HTList_nextObject(cur))) {
2.23      frystyk   292:            if (!strcmp(pres->hostname, hostname)) {
2.1       frystyk   293:                if (time(NULL) > pres->ntime + DNSTimeout) {
2.30      frystyk   294:                    HTTRACE(PROT_TRACE, "HostByName.. Refreshing cache\n");
2.1       frystyk   295:                    delete_object(list, pres);
                    296:                    pres = NULL;
                    297:                }
                    298:                break;
                    299:            }
                    300:        }
                    301:     }
                    302:     if (pres) {
                    303:        /*
                    304:        ** Find the best home. We still want to do this as we use it as a
                    305:        ** fall back for persistent connections
                    306:        */
                    307:        homes = pres->homes;
                    308:        if (pres->homes > 1) {
                    309:            int cnt = 0;
2.28      frystyk   310:            double best_weight = 1e30;                        /* Pretty bad */
2.1       frystyk   311:            while (cnt < pres->homes) {
                    312:                if (*(pres->weight+cnt) < best_weight) {
                    313:                    best_weight = *(pres->weight+cnt);
2.23      frystyk   314:                    HTHost_setHome(host, cnt);
2.1       frystyk   315:                }
                    316:                cnt++;
                    317:            }
                    318:        }
2.23      frystyk   319:        host->dns = pres;
                    320:        memcpy((void *) &sin->sin_addr, *(pres->addrlist+HTHost_home(host)),
2.14      frystyk   321:               pres->addrlength);
2.1       frystyk   322:     } else {
                    323:        struct hostent *hostelement;                          /* see netdb.h */
2.10      frystyk   324:        HTAlertCallback *cbf = HTAlert_find(HT_PROG_DNS);
2.1       frystyk   325: #ifdef HT_REENTRANT
                    326:        int thd_errno;
                    327:        char buffer[HOSTENT_MAX];
                    328:        struct hostent result;                        /* For gethostbyname_r */
2.31      kahan     329: #endif
2.32      kahan     330: #ifdef HAVE_GETHOSTBYNAME_R_3
2.31      kahan     331:         struct hostent_data hdata;
                    332: #endif
                    333: 
2.23      frystyk   334:        if (cbf) (*cbf)(request, HT_PROG_DNS, HT_MSG_NULL,NULL,hostname,NULL);
2.32      kahan     335: #ifdef HAVE_GETHOSTBYNAME_R_5
2.23      frystyk   336:        hostelement = gethostbyname_r(hostname, &result, buffer,
2.1       frystyk   337:                                      HOSTENT_MAX, &thd_errno);
2.32      kahan     338: #elif defined(HAVE_GETHOSTBYNAME_R_6)
2.31      kahan     339:        gethostbyname_r(hostname, &result, buffer,
                    340:                        HOSTENT_MAX, &hostelement, &thd_errno);
                    341: 
2.32      kahan     342: #elif defined(HAVE_GETHOSTBYNAME_R_3)
2.31      kahan     343:         if (gethostbyname_r(hostname, &result, &hdata) == 0) {
                    344:            hostelement = &result;
                    345:        }
                    346:        else {
                    347:            hostelement = NULL;
                    348:        }
2.1       frystyk   349: #else
2.23      frystyk   350:        if (cbf) (*cbf)(request, HT_PROG_DNS, HT_MSG_NULL,NULL,hostname,NULL);
                    351:        hostelement = gethostbyname(hostname);
2.1       frystyk   352: #endif
                    353:        if (!hostelement) {
2.27      frystyk   354:             HTRequest_addSystemError(request, ERR_FATAL, socerrno, NO,
                    355:                                     "gethostbyname");
2.1       frystyk   356:            return -1;
                    357:        }       
2.23      frystyk   358:        host->dns = HTDNS_add(list, hostelement, hostname, &homes);
2.14      frystyk   359:        memcpy((void *) &sin->sin_addr, *hostelement->h_addr_list,
                    360:               hostelement->h_length);
2.1       frystyk   361:     }
                    362:     return homes;
                    363: }
                    364: 
                    365: 
                    366: /*
                    367: **     Get host name of the machine on the other end of a socket.
                    368: **
                    369: */
2.9       frystyk   370: PUBLIC char * HTGetHostBySock (int soc)
2.1       frystyk   371: {
                    372:     struct sockaddr addr;
                    373:     int len = sizeof(struct sockaddr);
                    374:     struct in_addr *iaddr;
                    375:     char *name = NULL;
                    376:     struct hostent * phost;            /* Pointer to host -- See netdb.h */
                    377: #ifdef HT_REENTRANT
                    378:     int thd_errno;
                    379:     char buffer[HOSTENT_MAX];
                    380:     struct hostent result;                           /* For gethostbyaddr_r */
                    381: #endif
2.31      kahan     382: #ifdef HAVE_GETHOSTBYADDR_R_5
                    383:     struct hostent_data hdata;
                    384: #endif
2.1       frystyk   385: 
                    386: #ifdef DECNET  /* Decnet ain't got no damn name server 8#OO */
                    387:     return NULL;
                    388: #else
                    389:     if (getpeername(soc, &addr, &len) < 0)
                    390:        return NULL;
                    391:     iaddr = &(((struct sockaddr_in *)&addr)->sin_addr);
                    392: 
2.31      kahan     393: #ifdef HAVE_GETHOSTBYADDR_R_7
2.1       frystyk   394:     phost = gethostbyaddr_r((char *) iaddr, sizeof(struct in_addr), AF_INET,
                    395:                            &result, buffer, HOSTENT_MAX, &thd_errno);
2.31      kahan     396: #elif defined(HAVE_GETHOSTBYADDR_R_8)
                    397:     gethostbyaddr_r((char *) iaddr, sizeof(struct in_addr), AF_INET,
                    398:                    &result, buffer, HOSTENT_MAX, &phost, &thd_errno);
                    399: #elif defined(HAVE_GETHOSTBYADDR_R_5)
                    400:     if(gethostbyaddr_r((char *) iaddr, sizeof(struct in_addr), AF_INET,
                    401:                       &result, &hdata)==0) {
                    402:        phost=&result;
                    403:     }
                    404:     else {
                    405:        phost = NULL;
                    406:     }
2.1       frystyk   407: #else
                    408:     phost = gethostbyaddr((char *) iaddr, sizeof(struct in_addr), AF_INET);
                    409: #endif
                    410:     if (!phost) {
2.30      frystyk   411:        HTTRACE(PROT_TRACE, "TCP......... Can't find internet node name for peer!!\n");
2.1       frystyk   412:        return NULL;
                    413:     }
                    414:     StrAllocCopy(name, phost->h_name);
2.30      frystyk   415:     HTTRACE(PROT_TRACE, "TCP......... Peer name is `%s'\n" _ name);
2.1       frystyk   416:     return name;
                    417: 
                    418: #endif /* not DECNET */
                    419: }

Webmaster