home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / tcpipsrc / DNS / c / resolve_0 < prev    next >
Encoding:
Text File  |  1995-02-22  |  17.1 KB  |  724 lines

  1. /*
  2.  * Copyright (c) 1985 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted provided
  6.  * that: (1) source distributions retain this entire copyright notice and
  7.  * comment, and (2) distributions including binaries display the following
  8.  * acknowledgement:  ``This product includes software developed by the
  9.  * University of California, Berkeley and its contributors'' in the
  10.  * documentation or other materials provided with the distribution and in
  11.  * all advertising materials mentioning features or use of this software.
  12.  * Neither the name of the University nor the names of its contributors may
  13.  * be used to endorse or promote products derived from this software without
  14.  * specific prior written permission.
  15.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  16.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  17.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)res_comp.c    6.18 (Berkeley) 6/27/90";
  22. #endif /* LIBC_SCCS and not lint */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <time.h>
  28.  
  29. #include "netdb.h"
  30. #include "resolve.h"
  31. #include "nameser.h"
  32. #include "in.h"
  33. #include "socket.h"
  34.  
  35. static int dn_find(u_char *exp_dn, u_char *msg,
  36.            u_char **dnptrs, u_char **lastdnptr);
  37.  
  38. /*
  39.  * Expand compressed domain name 'comp_dn' to full domain name.
  40.  * 'msg' is a pointer to the begining of the message,
  41.  * 'eomorig' points to the first location after the message,
  42.  * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  43.  * Return size of compressed name or -1 if there was an error.
  44.  */
  45. int
  46. dn_expand(msg, eomorig, comp_dn, exp_dn, length)
  47.     u_char *msg, *eomorig, *comp_dn, *exp_dn;
  48.     int length;
  49. {
  50.     register u_char *cp, *dn;
  51.     register int n, c;
  52.     u_char *eom;
  53.     int len = -1, checked = 0;
  54.  
  55.     dn = exp_dn;
  56.     cp = comp_dn;
  57.     eom = exp_dn + length;
  58.     /*
  59.      * fetch next label in domain name
  60.      */
  61.     while (n = *cp++) {
  62.         /*
  63.          * Check for indirection
  64.          */
  65.         switch (n & INDIR_MASK) {
  66.         case 0:
  67.             if (dn != exp_dn) {
  68.                 if (dn >= eom)
  69.                     return (-1);
  70.                 *dn++ = '.';
  71.             }
  72.             if (dn+n >= eom)
  73.                 return (-1);
  74.             checked += n + 1;
  75.             while (--n >= 0) {
  76.                 if ((c = *cp++) == '.') {
  77.                     if (dn + n + 2 >= eom)
  78.                         return (-1);
  79.                     *dn++ = '\\';
  80.                 }
  81.                 *dn++ = c;
  82.                 if (cp >= eomorig)    /* out of range */
  83.                     return(-1);
  84.             }
  85.             break;
  86.  
  87.         case INDIR_MASK:
  88.             if (len < 0)
  89.                 len = cp - comp_dn + 1;
  90.             cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
  91.             if (cp < msg || cp >= eomorig)    /* out of range */
  92.                 return(-1);
  93.             checked += 2;
  94.             /*
  95.              * Check for loops in the compressed name;
  96.              * if we've looked at the whole message,
  97.              * there must be a loop.
  98.              */
  99.             if (checked >= eomorig - msg)
  100.                 return (-1);
  101.             break;
  102.  
  103.         default:
  104.             return (-1);            /* flag error */
  105.         }
  106.     }
  107.     *dn = '\0';
  108.     if (len < 0)
  109.         len = cp - comp_dn;
  110.     return (len);
  111. }
  112.  
  113. /*
  114.  * Compress domain name 'exp_dn' into 'comp_dn'.
  115.  * Return the size of the compressed name or -1.
  116.  * 'length' is the size of the array pointed to by 'comp_dn'.
  117.  * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
  118.  * is a pointer to the beginning of the message. The list ends with NULL.
  119.  * 'lastdnptr' is a pointer to the end of the arrary pointed to
  120.  * by 'dnptrs'. Side effect is to update the list of pointers for
  121.  * labels inserted into the message as we compress the name.
  122.  * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
  123.  * is NULL, we don't update the list.
  124.  */
  125. int
  126. dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr)
  127.     u_char *exp_dn, *comp_dn;
  128.     int length;
  129.     u_char **dnptrs, **lastdnptr;
  130. {
  131.     register u_char *cp, *dn;
  132.     register int c, l;
  133.     u_char **cpp, **lpp, *sp, *eob;
  134.     u_char *msg;
  135.  
  136.     dn = exp_dn;
  137.     cp = comp_dn;
  138.     eob = cp + length;
  139.     if (dnptrs != NULL) {
  140.         if ((msg = *dnptrs++) != NULL) {
  141.             for (cpp = dnptrs; *cpp != NULL; cpp++)
  142.                 ;
  143.             lpp = cpp;    /* end of list to search */
  144.         }
  145.     } else
  146.         msg = NULL;
  147.     for (c = *dn++; c != '\0'; ) {
  148.         /* look to see if we can use pointers */
  149.         if (msg != NULL) {
  150.             if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
  151.                 if (cp+1 >= eob)
  152.                     return (-1);
  153.                 *cp++ = (l >> 8) | INDIR_MASK;
  154.                 *cp++ = l % 256;
  155.                 return (cp - comp_dn);
  156.             }
  157.             /* not found, save it */
  158.             if (lastdnptr != NULL && cpp < lastdnptr-1) {
  159.                 *cpp++ = cp;
  160.                 *cpp = NULL;
  161.             }
  162.         }
  163.         sp = cp++;    /* save ptr to length byte */
  164.         do {
  165.             if (c == '.') {
  166.                 c = *dn++;
  167.                 break;
  168.             }
  169.             if (c == '\\') {
  170.                 if ((c = *dn++) == '\0')
  171.                     break;
  172.             }
  173.             if (cp >= eob) {
  174.                 if (msg != NULL)
  175.                     *lpp = NULL;
  176.                 return (-1);
  177.             }
  178.             *cp++ = c;
  179.         } while ((c = *dn++) != '\0');
  180.         /* catch trailing '.'s but not '..' */
  181.         if ((l = cp - sp - 1) == 0 && c == '\0') {
  182.             cp--;
  183.             break;
  184.         }
  185.         if (l <= 0 || l > MAXLABEL) {
  186.             if (msg != NULL)
  187.                 *lpp = NULL;
  188.             return (-1);
  189.         }
  190.         *sp = l;
  191.     }
  192.     if (cp >= eob) {
  193.         if (msg != NULL)
  194.             *lpp = NULL;
  195.         return (-1);
  196.     }
  197.     *cp++ = '\0';
  198.     return (cp - comp_dn);
  199. }
  200.  
  201. /*
  202.  * Skip over a compressed domain name. Return the size or -1.
  203.  */
  204. int
  205. dn_skipname(comp_dn, eom)
  206.     u_char *comp_dn, *eom;
  207. {
  208.     register u_char *cp;
  209.     register int n;
  210.  
  211.     cp = comp_dn;
  212.     while (cp < eom && (n = *cp++)) {
  213.         /*
  214.          * check for indirection
  215.          */
  216.         switch (n & INDIR_MASK) {
  217.         case 0:        /* normal case, n == len */
  218.             cp += n;
  219.             continue;
  220.         default:    /* illegal type */
  221.             return (-1);
  222.         case INDIR_MASK:    /* indirection */
  223.             cp++;
  224.         }
  225.         break;
  226.     }
  227.     return (cp - comp_dn);
  228. }
  229.  
  230. /*
  231.  * Search for expanded name from a list of previously compressed names.
  232.  * Return the offset from msg if found or -1.
  233.  * dnptrs is the pointer to the first name on the list,
  234.  * not the pointer to the start of the message.
  235.  */
  236. static int
  237. dn_find(exp_dn, msg, dnptrs, lastdnptr)
  238.     u_char *exp_dn, *msg;
  239.     u_char **dnptrs, **lastdnptr;
  240. {
  241.     register u_char *dn, *cp, **cpp;
  242.     register int n;
  243.     u_char *sp;
  244.  
  245.     for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
  246.         dn = exp_dn;
  247.         sp = cp = *cpp;
  248.         while (n = *cp++) {
  249.             /*
  250.              * check for indirection
  251.              */
  252.             switch (n & INDIR_MASK) {
  253.             case 0:        /* normal case, n == len */
  254.                 while (--n >= 0) {
  255.                     if (*dn == '.')
  256.                         goto next;
  257.                     if (*dn == '\\')
  258.                         dn++;
  259.                     if (*dn++ != *cp++)
  260.                         goto next;
  261.                 }
  262.                 if ((n = *dn++) == '\0' && *cp == '\0')
  263.                     return (sp - msg);
  264.                 if (n == '.')
  265.                     continue;
  266.                 goto next;
  267.  
  268.             default:    /* illegal type */
  269.                 return (-1);
  270.  
  271.             case INDIR_MASK:    /* indirection */
  272.                 cp = msg + (((n & 0x3f) << 8) | *cp);
  273.             }
  274.         }
  275.         if (*dn == '\0')
  276.             return (sp - msg);
  277.     next:    ;
  278.     }
  279.     return (-1);
  280. }
  281.  
  282. /*
  283.  * Routines to insert/extract short/long's. Must account for byte
  284.  * order and non-alignment problems. This code at least has the
  285.  * advantage of being portable.
  286.  *
  287.  * used by sendmail.
  288.  */
  289.  
  290. u_short
  291. _getshort(msgp)
  292.     u_char *msgp;
  293. {
  294.     register u_char *p = (u_char *) msgp;
  295. #ifdef vax
  296.     /*
  297.      * vax compiler doesn't put shorts in registers
  298.      */
  299.     register u_long u;
  300. #else
  301.     register u_short u;
  302. #endif
  303.  
  304.     u = *p++ << 8;
  305.     return ((u_short)(u | *p));
  306. }
  307.  
  308. u_long
  309. _getlong(msgp)
  310.     u_char *msgp;
  311. {
  312.     register u_char *p = (u_char *) msgp;
  313.     register u_long u;
  314.  
  315.     u = *p++; u <<= 8;
  316.     u |= *p++; u <<= 8;
  317.     u |= *p++; u <<= 8;
  318.     return (u | *p);
  319. }
  320.  
  321.  
  322. putshort(s, msgp)
  323.     register u_short s;
  324.     register u_char *msgp;
  325. {
  326.  
  327.     msgp[1] = s;
  328.     msgp[0] = s >> 8;
  329. }
  330.  
  331. putlong(l, msgp)
  332.     register u_long l;
  333.     register u_char *msgp;
  334. {
  335.  
  336.     msgp[3] = l;
  337.     msgp[2] = (l >>= 8);
  338.     msgp[1] = (l >>= 8);
  339.     msgp[0] = l >> 8;
  340. }
  341.  
  342.  
  343.  
  344. static struct cache_s {
  345.   void *key;
  346.   int type;
  347.   int len;
  348.   struct hostent *cont;
  349.   time_t expires;
  350.   time_t accessed;
  351.   } *cache = 0;
  352.  
  353. unsigned int cachesize = 0;
  354.  
  355.  
  356. static void kill_cache( struct cache_s *entry )
  357. {
  358.   if( ! entry->key ) return;
  359.   free( entry->key );
  360.   entry->cont = NULL;
  361.   entry->key = NULL;
  362. }
  363.  
  364.  
  365. static int he_size( struct hostent *he )
  366. {
  367.   int size;
  368.   char **cp;
  369.   struct mx_data **mx;
  370.  
  371.   if( !he ) return 0;
  372.   size = sizeof( struct hostent ) + 2*sizeof(char *) + ((strlen(he->h_name)+4)&~3) + sizeof(struct mx_data *);
  373.   for( cp=he->h_aliases; *cp; cp++ ) size += sizeof(char *) + strlen(*cp)+1;
  374.   for( cp=he->h_addr_list; *cp; cp++ ) size += sizeof(char *) + he->h_length;
  375.   for( mx=he->h_mx; *mx; mx++ ) size += sizeof(struct mx_data) + strlen((*mx)->host) + sizeof(struct mx_data *) + 1;
  376.   return size;
  377. }
  378.  
  379.  
  380. static struct hostent *he_copy( char *mem, struct hostent *he )
  381. {
  382.   struct hostent *new;
  383.   int i;
  384.   char **cp;
  385.   char **ds;
  386.   struct mx_data **mx,**xd;
  387.  
  388.   if( !he ) return NULL;
  389.   new = (struct hostent *)mem;
  390.   memcpy( new, he, sizeof(*new) );
  391.   mem += sizeof(*new);
  392.   for( cp=he->h_aliases,i=1; *cp; cp++,i++ );
  393.   new->h_aliases = (char **)mem;
  394.   mem += i*sizeof(char *);
  395.   for( cp=he->h_addr_list,i=1; *cp; cp++,i++ );
  396.   new->h_addr_list = (char **)mem;
  397.   mem += i*sizeof(char *);
  398.   for( mx=he->h_mx,i=1; *mx; mx++,i++ );
  399.   new->h_mx = (struct mx_data **)mem;
  400.   mem += i*sizeof(struct mx_data *);
  401.   for( mx=new->h_mx; i>1; i--,mx++,mem+=sizeof(struct mx_data) ) *mx = (struct mx_data *)mem;
  402.   *mx = NULL;
  403.  
  404.   strcpy( mem, he->h_name );
  405.   new->h_name = mem;
  406.   mem += ((strlen( he->h_name ) + 4)&~3);
  407.  
  408.   for( ds=new->h_addr_list,cp=he->h_addr_list; *cp; cp++,ds++ ) {
  409.     *ds = mem;
  410.     memcpy( mem, *cp, he->h_length );
  411.     mem += he->h_length;
  412.     }
  413.   *ds = NULL;
  414.  
  415.   for( ds=new->h_aliases,cp=he->h_aliases; *cp; cp++,ds++ ) {
  416.     *ds = mem;
  417.     i = strlen( *cp ) +1;
  418.     memcpy( mem, *cp, i );
  419.     mem += i;
  420.     }
  421.   *ds = NULL;
  422.  
  423.   for( xd=new->h_mx,mx=he->h_mx; *mx; mx++,xd++ ) {
  424.     (*xd)->preference = (*mx)->preference;
  425.     (*xd)->host = (char *)mem;
  426.     i = strlen( (*mx)->host) + 1;
  427.     memcpy( mem, (*mx)->host, i );
  428.     mem += i;
  429.     }
  430.   *xd = NULL;
  431.  
  432.   return new;
  433. }
  434.  
  435.  
  436. extern int cache_req( void *, int, int , struct hostent ** );
  437. int cache_req( void *data, int type, int len, struct hostent **rval )
  438. {
  439.   struct cache_s *find;
  440.   int i;
  441.   time_t t;
  442.  
  443.   for( find=cache,i=cachesize; i; i--,find++ ) {
  444.    if( (find->key) && (find->type==type) && (find->len==len) && ! memcmp( find->key, data, len ) ) {
  445.     time( &t );
  446.     if( difftime( t, find->expires ) >= 0 ) kill_cache( find );
  447.      else {
  448.        *rval = find->cont;
  449.        find->accessed = time(NULL);
  450.        return 0;
  451.        }
  452.     }
  453.   }
  454.  
  455.   return -1;
  456. }
  457.  
  458.  
  459. void cache_it( void *data, int type, int len, struct hostent *result, long ttl )
  460. {
  461.   struct cache_s *overw=NULL, *clear=NULL;
  462.   int i;
  463.   time_t min;
  464.   char *mem;
  465.  
  466.   if (ttl == 0)
  467.     return;
  468.  
  469.   time(&min);
  470.   for( overw=cache,i=cachesize; i; i--,overw++ ) if( overw->key && difftime(min,overw->expires)<0 ) {
  471.     if( overw->type==type && overw->len==len && ! memcmp( overw->key, data, len ) ) break;
  472.     }
  473.    else clear = overw;
  474.   if( !i ) overw = clear;
  475.   if( ! overw ) for( overw=cache,clear=&cache[1],i=cachesize-1,min=cache->accessed; i; i--,clear++ )
  476.    if( difftime( clear->accessed, min ) < 0 ) {
  477.     overw = clear;
  478.     min = clear->accessed;
  479.     }
  480.  
  481.   mem = malloc( len+he_size(result)+3 );
  482.   if( ! mem ) return;
  483.   kill_cache( overw );
  484.   memcpy( mem, data, len );
  485.   overw->key = mem;
  486.   overw->type = type;
  487.   overw->len = len;
  488.   overw->accessed = time( NULL );
  489.   overw->expires = overw->accessed + ttl;
  490.   len = (len+3)&~3;
  491.   overw->cont = he_copy( mem+len, result );
  492. }
  493.  
  494.  
  495. void cache_save( void )
  496. {
  497.   FILE *f;
  498.   int i;
  499.   int n;
  500.   struct cache_s *cs;
  501.   char **cp;
  502.   struct mx_data **mx;
  503.   unsigned char *addr;
  504.  
  505.   f = fopen( "<TCPIP$dir>.resolve.cache", "w" );
  506.   if( ! f ) return;
  507.   for( n=cachesize,cs=cache; n; n--,cs++ ) if( cs->key ) {
  508.     switch( cs->type ) {
  509.       case CACHE_MX:
  510.         fprintf( f, "MX " );
  511.         goto keysave;
  512.       case CACHE_NAME:
  513.         fprintf( f, "NAME " );
  514.     keysave:
  515.         fwrite( cs->key, cs->len, 1, f );
  516.         fprintf( f, "\n" );
  517.         break;
  518.       case CACHE_ADDR:
  519.         if (cs->len != 4)
  520.           break;
  521.         addr = (unsigned char *)cs->key;
  522.         fprintf( f, "ADDR %d.%d.%d.%d\n", addr[3], addr[2], addr[1], addr[0] );
  523.         break;
  524.       }
  525.     fprintf( f, "%d %d\n", cs->expires, cs->accessed );
  526.     if( cs->cont ) {
  527.       for( mx=cs->cont->h_mx; *mx; mx++ ) {
  528.         fprintf( f, "%s", (*mx)->host );
  529.         if( mx[1] ) fputc( ' ', f );
  530.         }
  531.       fputc( '\n', f );
  532.       fprintf( f, "%d\n%s\n%d\n", cs->cont->h_length, cs->cont->h_name, cs->cont->h_addrtype );
  533.       for( cp=cs->cont->h_aliases; *cp; cp++ ) fprintf( f, "%s ", *cp );
  534.       fputc( '\n', f );
  535.       for( cp=cs->cont->h_addr_list; *cp; cp++ ) {
  536.         for( i=cs->cont->h_length; i; i-- ) fprintf( f, "%d.", (*(unsigned char **)cp)[i-1] );
  537.         fputc( ' ', f );
  538.         }
  539.       fputc( '\n', f );
  540.       }
  541.      else fprintf( f, "\n%d\n",
  542.      0 );
  543.     if( n>1 ) fputc( '\n', f );
  544.     }
  545.   fclose( f );
  546. }
  547.  
  548.  
  549. void cache_restore( void )
  550. {
  551.   unsigned int addr[4];
  552.   char buffer[500];
  553.   char hostname[256];
  554.   char addrs[100];
  555.   char aliases[500];
  556.   char mxhosts[500];
  557.   struct mx_data mxdata[30];
  558.   struct mx_data *mxptr[30];
  559.   struct mx_data **mxpp;
  560.   unsigned char key[256];
  561.   struct hostent he;
  562.   char *p_aliases[30];
  563.   char *p_addrs[35];
  564.   char *next;
  565.   char **at;
  566.   char *nextabyte;
  567.   FILE *f;
  568.   struct cache_s *c;
  569.   int i,j;
  570.   int n;
  571.   struct hostent *ohe;
  572.  
  573.   f = fopen( "<TCPIP$dir>.resolve.cache", "r" );
  574.   if( ! f ) return;
  575.   cache_purge();
  576.   he.h_aliases = p_aliases;
  577.   he.h_addr_list = p_addrs;
  578.   he.h_mx = mxptr;
  579.   for( i=cachesize,c=cache; i &&  ! feof(f) && ! ferror(f); i--,c++ ) {
  580.  
  581.     c->cont = NULL;
  582.     fgets( buffer, 255, f );
  583.     if( ! memcmp( buffer, "NAME ", 5 ) ) {
  584.       c->type = CACHE_NAME;
  585.       memcpy( key, &buffer[5], strlen(buffer)-4 );
  586.       c->len = strlen( (char *)key );
  587.       if( key[c->len-1] == '\n' ) c->len--;
  588.       }
  589.      else if( ! memcmp( buffer, "MX ", 3 ) ) {
  590.       c->type = CACHE_MX;
  591.       memcpy( key, &buffer[3], strlen(buffer)-2 );
  592.       c->len = strlen( (char *)key );
  593.       if( key[c->len-1] == '\n' ) c->len--;
  594.       }
  595.      else if( ! memcmp( buffer, "ADDR ", 5 ) ) {
  596.       c->type = CACHE_ADDR;
  597.       c->len = sscanf( buffer, "ADDR %u.%u.%u.%u", &addr[3], &addr[2], &addr[1], &addr[0]);
  598.       *(unsigned int *)(key) = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
  599.       }
  600.      else break;
  601.  
  602.     fgets( buffer, 255, f );
  603.     if( sscanf( buffer, "%d %d", &c->expires, &c->accessed ) < 2 ) break;
  604.  
  605.     fgets( mxhosts, 499, f );
  606.     mxpp = mxptr;
  607.     for( j=0,next=mxhosts; *next != '\n' && *next; j++, mxpp++ ) {
  608.       while( *next == ' ' || *next == '\t' ) next++;
  609.       if( ! *next || *next == '\n' ) break;
  610.       *mxpp = &mxdata[j];
  611.       (*mxpp)->preference = j;
  612.       (*mxpp)->host = next;
  613.       while( *next != ' ' && *next && *next != '\t' && *next != '\n' ) next++;
  614.       if( *next == '\n' ) *next = 0;
  615.        else *(next++) = 0;
  616.       }
  617.     *mxpp = NULL;
  618.  
  619.     fgets( buffer, 255, f );
  620.     if( sscanf( buffer, "%d", &he.h_length ) < 1 ) break;
  621.     if( he.h_length ) ohe = &he; else ohe = NULL;
  622.  
  623.     if( ohe ) {
  624.       fgets( hostname, 255, f );
  625.       if( hostname[strlen(hostname)-1] == '\n' ) hostname[strlen(hostname)-1] = 0;
  626.       he.h_name = hostname;
  627.  
  628.       fgets( buffer, 255, f );
  629.       if( sscanf( buffer, "%d", &he.h_addrtype ) < 1 ) break;
  630.  
  631.       fgets( aliases, 499, f );
  632.       next = aliases;
  633.       for( n=29,at=he.h_aliases; n; at++,n-- ) {
  634.         while( *next==' ' || *next=='\t' ) next++;
  635.         if( *next=='\n' || *next==0 ) break;
  636.         *at = next;
  637.         while( *next != ' ' && *next && *next != '\t' && *next != '\n' ) next++;
  638.         *(next++) = 0;
  639.         }
  640.       *at = NULL;
  641.  
  642.       fgets( buffer, 499, f );
  643.       for( nextabyte=addrs,next=buffer,n=100,at=he.h_addr_list; n; n--,at++,nextabyte+=he.h_length ) {
  644.         while( *next==' ' || *next=='\t' ) next++;
  645.         if( *next=='\n' || *next==0 ) break;
  646.         *at = nextabyte;
  647.         for( j=he.h_length; j; j-- ) {
  648.           nextabyte[j-1] = atoi(next);
  649.           while( *next!='.' && *next!=' ' && *next && *next!='\t' ) next++;
  650.           if( *next ) next++;
  651.           }
  652.         if( *next=='.' ) next++;
  653.         }
  654.       *at = NULL;
  655.       }
  656.  
  657.     next = malloc( c->len + he_size( ohe ) + 3 );
  658.     memcpy( next, key, c->len );
  659.     c->key = next;
  660.     c->cont = he_copy( next + ((c->len+3)&~3), ohe );
  661.     fgets( buffer, 255, f );
  662.     }
  663.   fclose(f);
  664. }
  665.  
  666.  
  667. void cache_print( void )
  668. {
  669.   struct cache_s *c;
  670.   int i, j;
  671.   char buf[255];
  672.   time_t t;
  673.   unsigned char *addr;
  674.  
  675.   time( &t );
  676.   for( c=cache,i=cachesize; i; i--,c++ ) if( c->key )
  677.   {
  678.     cwprintf(NULL, "type=" );
  679.     switch( c->type )
  680.     {
  681.       case CACHE_NAME:
  682.       case CACHE_MX:
  683.         memcpy( buf, c->key, c->len );
  684.         buf[c->len] = 0;
  685.         cwprintf(NULL, "%s, name=%s", c->type==CACHE_NAME?"NAME":"MX", buf );
  686.         break;
  687.       case CACHE_ADDR:
  688.         addr = (unsigned char *)c->key;
  689.         cwprintf(NULL, "ADDR, address=%d", addr[c->len-1]);
  690.         for (j = c->len-2; j >= 0; j--)
  691.           cwprintf(NULL, ".%d", addr[j]);
  692.         break;
  693.       default:
  694.         cwprintf(NULL, "unknown" );
  695.       }
  696.     if( difftime( t, c->expires ) >= 0 )
  697.       cwprintf(NULL, ",  expired since" );
  698.     else
  699.       cwprintf(NULL, ",  expires at" );
  700.     strftime( buf, 255, "%H:%M:%S on %a, %d %b %Y", localtime( &c->expires ) );
  701.     cwprintf( NULL, " %s\n", buf );
  702.     }
  703. }
  704.  
  705.  
  706. void cache_purge( void )
  707. {
  708.   int i;
  709.  
  710.   for( i=0; i<cachesize; i++ )
  711.     kill_cache( &cache[i] );
  712. }
  713.  
  714. void cache_init( unsigned int size )
  715. {
  716.   cachesize = size;
  717.   cache = malloc(cachesize * sizeof(struct cache_s));
  718.  
  719.   memset(cache, 0, cachesize * sizeof(struct cache_s));
  720.  
  721.   cache_restore();
  722.   atexit(cache_save);
  723. }
  724.