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