home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume34 / fsp / part03 / server_host.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-18  |  4.0 KB  |  130 lines

  1.     /*********************************************************************\
  2.     *  Copyright (c) 1991 by Wen-King Su (wen-king@vlsi.cs.caltech.edu)   *
  3.     *                                                                     *
  4.     *  You may copy or modify this file in any manner you wish, provided  *
  5.     *  that this notice is always included, and that you hold the author  *
  6.     *  harmless for any loss or damage resulting from the installation or *
  7.     *  use of this software.                                              *
  8.     \*********************************************************************/
  9.  
  10. #include "server_def.h"
  11.  
  12. /****************************************************************************
  13. * This file contains routines to maintain client database.
  14. ****************************************************************************/
  15.  
  16. extern char *realloc(), *malloc(), *ctime();
  17.  
  18. static HTAB     *htab;        /* client data base.            */
  19. static unsigned  hcnt;        /* number of clients.            */
  20. static unsigned  htot = 0;    /* available entries in the data base.    */
  21. static HTAB     hzero;
  22.  
  23. #define HALLOC_SIZE 30
  24.  
  25. /****************************************************************************
  26. * Returns an entry from the database corresponding to to the inet number.
  27. * A new entry is created is it is not found.
  28. * The database is a linear array of sorted structures.
  29. * Entries are searched using binary search on the array.
  30. ****************************************************************************/
  31.  
  32. HTAB *find_host(inet_num)
  33.     unsigned long inet_num;
  34. {
  35.     unsigned      l, h, m, i;
  36.     unsigned long inum;
  37.     HTAB      *hs, *hd;
  38.  
  39.     for(l = 0, h = hcnt-1; (m = (l + h) >> 1) != l; )    /* binary search */
  40.     {
  41.     inum = htab[m].inet_num;
  42.     if(inum > inet_num) h = m; else
  43.     if(inum < inet_num) l = m; else { htab[m].acc_cnt++; return(htab+m); }
  44.     }
  45.  
  46.     if(htab[m].inet_num < inet_num) m++;  /* locate first entry that is > */
  47.  
  48.     if((hcnt+1) > htot)            /* need more space */
  49.     {
  50.     htot += HALLOC_SIZE;        /* add HALLOC_SIZE entries at a time */
  51.  
  52.     if(!(htab = (HTAB *) realloc(htab,sizeof(HTAB)*htot)))
  53.                     { perror("grow_htab realloc"); exit(1); }
  54.     }
  55.  
  56.     for(i = hcnt-m, hs = htab+hcnt, hd=htab+hcnt+1; i--; *--hd = *--hs);
  57.  
  58.     htab[m]=hzero;
  59.     htab[m].inet_num = inet_num;
  60.     htab[m].last_key = get_next_key()  ;
  61.     htab[m].next_key = get_next_key()+1;
  62.     hcnt++;
  63.     return(htab+m);
  64. }
  65.  
  66. /****************************************************************************
  67. * Write out the client table in the .HTAB_DUMP file.
  68. ****************************************************************************/
  69.  
  70. dump_htab()
  71. {
  72.     int i;
  73.     FILE *fp;
  74.     HTAB *hp;
  75.  
  76.     if(!(fp = fopen(".HTAB_DUMP","w"))) return;
  77.  
  78.     for(i = hcnt-2, hp = htab+1; i--; hp++)
  79.     {
  80.     fprintf(fp,"%d.%d.%d.%d\t%5d %c %s",
  81.                 ((unsigned char *)(&hp->inet_num))[0],
  82.                 ((unsigned char *)(&hp->inet_num))[1],
  83.                 ((unsigned char *)(&hp->inet_num))[2],
  84.                 ((unsigned char *)(&hp->inet_num))[3],
  85.                 hp->acc_cnt,
  86.                 (hp->inhibit) ? '*' : ((hp->active) ? '+' : ' '),
  87.                 ctime((time_t *) &(hp->last_acc)));
  88.     }
  89.  
  90.     fclose(fp);
  91. }
  92.  
  93. /****************************************************************************
  94. * Client database initialization routine.  Reads in .ROGUE_HOSTS.
  95. ****************************************************************************/
  96.  
  97. init_htab()        /* always have 2 entries -- 0, MAXINT */
  98. {
  99.     FILE *fp;
  100.     HTAB    *hp;
  101.     char  buf[1024];
  102.     unsigned int i1,i2,i3,i4;
  103.     unsigned long hnum;
  104.  
  105.     if(!(htab = (HTAB *) malloc(sizeof(HTAB)*HALLOC_SIZE)))
  106.                 { perror("grow_htab malloc"); exit(1); }
  107.     htab[0] = hzero;
  108.     htab[1] = hzero;
  109.     htab[1].inet_num = ~0;
  110.     hcnt = 2;
  111.     htot = HALLOC_SIZE;
  112.  
  113.     if(fp = fopen(".ROGUE_HOSTS","r"))
  114.     {
  115.     while(fgets(buf,sizeof(buf),fp))
  116.     {
  117.         if(*buf < '0' || *buf > '9') continue;
  118.  
  119.         sscanf(buf,"%d.%d.%d.%d",&i1,&i2,&i3,&i4);
  120.         ((unsigned char *) (&hnum))[0] = i1;
  121.         ((unsigned char *) (&hnum))[1] = i2;
  122.         ((unsigned char *) (&hnum))[2] = i3;
  123.         ((unsigned char *) (&hnum))[3] = i4;
  124.         hp = find_host(hnum);
  125.         hp->inhibit = 1;
  126.     }
  127.     fclose(fp);
  128.     }
  129. }
  130.