home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / INTERNET / UPC2S1.ZIP / HOSTATUS.C < prev    next >
C/C++ Source or Header  |  1993-07-06  |  8KB  |  216 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    h o s t a t u s . c                                             */
  3. /*                                                                    */
  4. /*    Load host status information for UUPC/extended                  */
  5. /*                                                                    */
  6. /*    Copyright (c) 1991, Andrew H. Derbyshire                        */
  7. /*--------------------------------------------------------------------*/
  8.  
  9. /*--------------------------------------------------------------------*/
  10. /*    Changes Copyright (c) 1990-1993 by Kendra Electronic            */
  11. /*    Wonderworks.                                                    */
  12. /*                                                                    */
  13. /*    All rights reserved except those explicitly granted by the      */
  14. /*    UUPC/extended license agreement.                                */
  15. /*--------------------------------------------------------------------*/
  16.  
  17. /*--------------------------------------------------------------------*/
  18. /*                          RCS Information                           */
  19. /*--------------------------------------------------------------------*/
  20.  
  21. /*
  22.  *    $Id: hostatus.c 1.4 1993/07/06 10:56:27 ahd Exp $
  23.  *
  24.  *    Revision history:
  25.  *    $Log: hostatus.c $
  26.  *     Revision 1.4  1993/07/06  10:56:27  ahd
  27.  *     Always report bad record failures
  28.  *
  29.  */
  30.  
  31. /*--------------------------------------------------------------------*/
  32. /*                        System include files                        */
  33. /*--------------------------------------------------------------------*/
  34.  
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <time.h>
  38.  
  39. #include <io.h>
  40.  
  41. /*--------------------------------------------------------------------*/
  42. /*                    UUPC/extended include files                     */
  43. /*--------------------------------------------------------------------*/
  44.  
  45. #include "lib.h"
  46. #include "hlib.h"
  47. #include "hostable.h"
  48. #include "hostatus.h"
  49. #include "security.h"
  50. #include "timestmp.h"
  51. #include "stater.h"
  52.  
  53. /*--------------------------------------------------------------------*/
  54. /*        Define current file name for panic() and printerr()         */
  55. /*--------------------------------------------------------------------*/
  56.  
  57. currentfile();
  58.  
  59. /*--------------------------------------------------------------------*/
  60. /*                          Global variables                          */
  61. /*--------------------------------------------------------------------*/
  62.  
  63. time_t start_stats = 0;
  64. time_t hstatus_age = -2;
  65.  
  66. void HostStatus( void )
  67. {
  68.  
  69.    char fname[FILENAME_MAX];
  70.    char buf[BUFSIZ];
  71.    struct HostTable *host;
  72.    FILE *stream;
  73.    unsigned short len1;
  74.    unsigned short len2;
  75.    long size;
  76.  
  77.    time_t age;
  78.  
  79.    mkfilename( fname, E_confdir, DCSTATUS );
  80.  
  81. /*--------------------------------------------------------------------*/
  82. /*    If the file does not exist but does exist in the spool          */
  83. /*    directory, attempt to move it, else use the spool directory.    */
  84. /*    If the file doesn't exist at all, return quietly.               */
  85. /*--------------------------------------------------------------------*/
  86.  
  87.    if ( access( fname, 0 ))
  88.    {
  89.       mkfilename( buf, E_spooldir, DCSTATUS );
  90.       if ( access( buf, 0 ))
  91.          return;
  92.  
  93.       if (rename( buf, fname ))
  94.       {
  95.          printerr( buf );
  96.          strcpy( fname, buf );
  97.       }
  98.    }
  99.  
  100. /*--------------------------------------------------------------------*/
  101. /*              Get the age of the file to avoid reloads              */
  102. /*--------------------------------------------------------------------*/
  103.  
  104.    age = stater( fname , &size );
  105.  
  106.    if ( age == hstatus_age )  /* Unchanged file?                     */
  107.       return;                 /* Yes --> Just return quietly         */
  108.  
  109. /*--------------------------------------------------------------------*/
  110. /*                       Actually open the file                       */
  111. /*--------------------------------------------------------------------*/
  112.  
  113.    if ((stream  = FOPEN(fname , "r", BINARY_MODE)) == NULL)
  114.    {
  115.  
  116.       perror( fname );
  117.       printmsg(1,"HostStatus: Unable to open host status file");
  118.       time(&start_stats);
  119.       return;
  120.  
  121.    } /* if */
  122.  
  123. /*--------------------------------------------------------------------*/
  124. /*                    Read the header information                     */
  125. /*--------------------------------------------------------------------*/
  126.  
  127.    fread( &len1, sizeof len1, 1, stream );
  128.    fread( &len2, sizeof len2, 1, stream );
  129.    fread( buf , 1, len1, stream);
  130.    buf[len1++] = ' ';
  131.    fread( buf + len1 , 1, len2, stream);
  132.    buf[ len1 + len2 ] = '\0';
  133.    fread( &start_stats , sizeof start_stats , 1,  stream);
  134.  
  135.    printmsg(5,"HostStatus: %s generated by %s beginning %s",
  136.          fname,
  137.          buf,
  138.          ctime(&start_stats));
  139.  
  140.    if ( start_stats < 630720000L )  /* Invalid date?              */
  141.       time(&start_stats);           /* Yes --> reset date to today*/
  142.  
  143. /*--------------------------------------------------------------------*/
  144. /*                           Load each host                           */
  145. /*--------------------------------------------------------------------*/
  146.  
  147.    while  (!feof( stream ) && !ferror(stream))
  148.    {
  149.       if (fread( &len1, sizeof len1, 1, stream ) < 1)
  150.          break;
  151.  
  152.       fread( &len2, sizeof len2, 1, stream );
  153.       fread( buf , sizeof host->hostname[0], len1, stream);
  154.       buf[ len1 ] = '\0';
  155.  
  156.       printmsg(5,"HostStatus: Name length %d, status length %d, "
  157.                  "host name \"%s\"",
  158.                  len1, len2, buf );
  159.  
  160.       host = checkreal( buf );
  161.  
  162.       if ( host == BADHOST )
  163.       {
  164.          printmsg(1,"HostStatus: Host \"%s\" not found, purging entry",
  165.                      buf );
  166.          fread( buf , len2, 1,  stream);
  167.       }
  168.       else if ( len2 <= (sizeof *(host->hstats)))
  169.       {
  170.          struct HostStats stats;    /* host status, as defined by hostatus */
  171.  
  172.          fread( &stats , len2, 1,  stream);
  173.  
  174.          printmsg(5,"HostStatus: Loaded status for host %s",
  175.                   host->hostname);
  176.  
  177. /*--------------------------------------------------------------------*/
  178. /*        Update the host status to the best known information        */
  179. /*--------------------------------------------------------------------*/
  180.  
  181.          if ( host->hstats->lconnect > stats.lconnect )
  182.             stats.lconnect = host->hstats->lconnect;
  183.  
  184.          if ( host->hstats->ltime > stats.ltime )
  185.             stats.ltime = host->hstats->ltime;
  186.          else if ( host->hstats->lconnect > stats.ltime )
  187.             ; /* No operation */
  188.          else if (( stats.save_hstatus >= nocall ) &&
  189.                   ( stats.save_hstatus < last_status ))
  190.             host->hstatus = stats.save_hstatus;
  191.          else
  192.             printmsg(0,"HostStatus: Invalid status (%d) ignored for \"%s\"",
  193.                stats.save_hstatus,host->hostname ) ;
  194.  
  195.          memcpy( host->hstats, &stats , sizeof stats );
  196.       }
  197.       else {
  198.          printmsg(0,"HostStatus: Bad record length %d (wanted %d), "
  199.                     "purging status for host %s",
  200.                     len2, (int) (sizeof *(host->hstats)), buf);
  201.          fread( buf , len2, 1,  stream);
  202.       } /* else */
  203.    } /* while */
  204.  
  205.    if (ferror( stream ))
  206.    {
  207.       printerr( fname );
  208.       clearerr( stream );
  209.    }
  210.  
  211.    fclose( stream );
  212.  
  213.    hstatus_age = age;             /* Remember the age of the file     */
  214.  
  215. } /* HostStatus */
  216.