6c6 < ** @(#) $Id: HTDNS.c,v 1.1.1.1 2000/11/07 22:44:38 tube Exp $ --- > ** @(#) $Id: HTDNS.c,v 2.31 2000/01/06 10:48:50 kahan Exp $ 98,102c98,102 < if ((me = (HTdns *) HT_CALLOC(1, sizeof(HTdns))) == NULL || < (me->addrlist = (char **) HT_CALLOC(1, cnt*sizeof(char*))) == NULL || < (addr = (char *) HT_CALLOC(1, cnt*element->h_length)) == NULL) < HT_OUTOFMEM("HTDNS_add"); < StrAllocCopy(me->hostname, host); --- > > if ((me = (HTdns *) HT_CALLOC(1, sizeof(HTdns))) == NULL || (me->addrlist = (char **) HT_CALLOC(1, cnt*sizeof(char*))) == NULL || (addr = (char *) HT_CALLOC(1, cnt*element->h_length)) == NULL) > HT_OUTOFMEM("HTDNS_add"); > > StrAllocCopy(me->hostname, host); 104,105c104,106 < index = element->h_addr_list; < cnt = 0; --- > index = element->h_addr_list; > cnt = 0; > 107,108c108,109 < *(me->addrlist+cnt) = addr+cnt*element->h_length; < memcpy((void *) *(me->addrlist+cnt++), *index++, element->h_length); --- > *(me->addrlist+cnt) = addr+cnt*element->h_length; > memcpy((void *) *(me->addrlist+cnt++), *index++, element->h_length); 111a113 > 113a116 > 115,116c118,120 < HTTRACE(PROT_TRACE, "DNS Add..... `%s\' with %d home(s) to %p\n" _ < host _ *homes _ list); --- > > HTTRACE(PROT_TRACE, "DNS Add..... `%s\' with %d home(s) to %p\n" _ host _ *homes _ list); > 117a122,178 > > return me; > } > > > > > /* HTDNS_update > ** --------- > */ > PUBLIC HTdns * HTDNS_update (HTdns* me, struct hostent * element, char *host, int *homes) > { > > char *addr = NULL; > char **index = element->h_addr_list; > int cnt = 1; > > // free old structure > if (me) { > > HT_FREE(me->hostname); > > if (*me->addrlist) > HT_FREE(*me->addrlist); > HT_FREE(me->addrlist); > HT_FREE(me->weight); > > } else { > return me; > } > > // clear structure > memset(me, 0, sizeof(HTdns)); > > > while(*index++) cnt++; > > if ((me->addrlist = (char **) HT_CALLOC(1, cnt*sizeof(char*))) == NULL || (addr = (char *) HT_CALLOC(1, cnt*element->h_length)) == NULL) > HT_OUTOFMEM("HTDNS_add"); > > StrAllocCopy(me->hostname, host); > me->ntime = time(NULL); > index = element->h_addr_list; > cnt = 0; > > while (*index) { > *(me->addrlist+cnt) = addr+cnt*element->h_length; > memcpy((void *) *(me->addrlist+cnt++), *index++, element->h_length); > } > me->homes = cnt; > *homes = cnt; > > if ((me->weight = (double *) HT_CALLOC(me->homes, sizeof(double))) == NULL) > HT_OUTOFMEM("HTDNS_add"); > > me->addrlength = element->h_length; > 121a183,185 > > > 243a308,310 > > > 246,252c313,362 < HTList *cur = list; < while ((pres = (HTdns *) HTList_nextObject(cur))) { < if (!strcmp(pres->hostname, hostname)) { < if (time(NULL) > pres->ntime + DNSTimeout) { < HTTRACE(PROT_TRACE, "HostByName.. Refreshing cache\n"); < delete_object(list, pres); < pres = NULL; --- > > HTList *cur = list; > while ((pres = (HTdns *) HTList_nextObject(cur))) { > > if (!strcmp(pres->hostname, hostname)) { > > if (time(NULL) > pres->ntime + DNSTimeout) { > HTTRACE(PROT_TRACE, "HostByName.. Refreshing cache\n"); > > > > { > struct hostent *hostelement; /* see netdb.h */ > HTAlertCallback *cbf = HTAlert_find(HT_PROG_DNS); > #ifdef HT_REENTRANT > int thd_errno; > char buffer[HOSTENT_MAX]; > struct hostent result; /* For gethostbyname_r */ > #endif > #ifdef GETHOSTBYNAME_R_3 > struct hostent_data hdata; > #endif > > if (cbf) (*cbf)(request, HT_PROG_DNS, HT_MSG_NULL,NULL,hostname,NULL); > #ifdef GETHOSTBYNAME_R_5 > hostelement = gethostbyname_r(hostname, &result, buffer,HOSTENT_MAX, &thd_errno); > #elif defined(GETHOSTBYNAME_R_6) > gethostbyname_r(hostname, &result, buffer, HOSTENT_MAX, &hostelement, &thd_errno); > #elif defined(GETHOSTBYNAME_R_3) > if (gethostbyname_r(hostname, &result, &hdata) == 0) { > hostelement = &result; > } else { > hostelement = NULL; > } > #else > if (cbf) (*cbf)(request, HT_PROG_DNS, HT_MSG_NULL,NULL,hostname,NULL); > hostelement = gethostbyname(hostname); > #endif > if (!hostelement) { > HTRequest_addSystemError(request, ERR_FATAL, socerrno, NO,"gethostbyname"); > return -1; > } > host->dns = HTDNS_update(pres, hostelement, hostname, &homes); > memcpy((void *) &sin->sin_addr, *hostelement->h_addr_list, hostelement->h_length); > } > > > } > break; > } 254,256d363 < break; < } < } 257a365,369 > > > > > 263,270c375,385 < homes = pres->homes; < if (pres->homes > 1) { < int cnt = 0; < double best_weight = 1e30; /* Pretty bad */ < while (cnt < pres->homes) { < if (*(pres->weight+cnt) < best_weight) { < best_weight = *(pres->weight+cnt); < HTHost_setHome(host, cnt); --- > homes = pres->homes; > if (pres->homes > 1) { > int cnt = 0; > double best_weight = 1e30; /* Pretty bad */ > while (cnt < pres->homes) { > if (*(pres->weight+cnt) < best_weight) { > best_weight = *(pres->weight+cnt); > HTHost_setHome(host, cnt); > } > cnt++; > } 272,277c387,389 < cnt++; < } < } < host->dns = pres; < memcpy((void *) &sin->sin_addr, *(pres->addrlist+HTHost_home(host)), < pres->addrlength); --- > host->dns = pres; > memcpy((void *) &sin->sin_addr, *(pres->addrlist+HTHost_home(host)),pres->addrlength); > 279,280c391,393 < struct hostent *hostelement; /* see netdb.h */ < HTAlertCallback *cbf = HTAlert_find(HT_PROG_DNS); --- > > struct hostent *hostelement; /* see netdb.h */ > HTAlertCallback *cbf = HTAlert_find(HT_PROG_DNS); 282,284c395,397 < int thd_errno; < char buffer[HOSTENT_MAX]; < struct hostent result; /* For gethostbyname_r */ --- > int thd_errno; > char buffer[HOSTENT_MAX]; > struct hostent result; /* For gethostbyname_r */ 290c403 < if (cbf) (*cbf)(request, HT_PROG_DNS, HT_MSG_NULL,NULL,hostname,NULL); --- > if (cbf) (*cbf)(request, HT_PROG_DNS, HT_MSG_NULL,NULL,hostname,NULL); 292,293c405 < hostelement = gethostbyname_r(hostname, &result, buffer, < HOSTENT_MAX, &thd_errno); --- > hostelement = gethostbyname_r(hostname, &result, buffer,HOSTENT_MAX, &thd_errno); 295,297c407 < gethostbyname_r(hostname, &result, buffer, < HOSTENT_MAX, &hostelement, &thd_errno); < --- > gethostbyname_r(hostname, &result, buffer, HOSTENT_MAX, &hostelement, &thd_errno); 300,304c410,413 < hostelement = &result; < } < else { < hostelement = NULL; < } --- > hostelement = &result; > } else { > hostelement = NULL; > } 306,307c415,416 < if (cbf) (*cbf)(request, HT_PROG_DNS, HT_MSG_NULL,NULL,hostname,NULL); < hostelement = gethostbyname(hostname); --- > if (cbf) (*cbf)(request, HT_PROG_DNS, HT_MSG_NULL,NULL,hostname,NULL); > hostelement = gethostbyname(hostname); 309,318c418,428 < if (!hostelement) { < HTRequest_addSystemError(request, ERR_FATAL, socerrno, NO, < "gethostbyname"); < return -1; < } < host->dns = HTDNS_add(list, hostelement, hostname, &homes); < memcpy((void *) &sin->sin_addr, *hostelement->h_addr_list, < hostelement->h_length); < } < return homes; --- > if (!hostelement) { > HTRequest_addSystemError(request, ERR_FATAL, socerrno, NO,"gethostbyname"); > return -1; > } > host->dns = HTDNS_add(list, hostelement, hostname, &homes); > memcpy((void *) &sin->sin_addr, *hostelement->h_addr_list, hostelement->h_length); > > } > > > return homes;