home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / hostup / hostupd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.3 KB  |  105 lines

  1. #include <stdio.h>
  2.  
  3. #include <sys/types.h>
  4. #include <sys/ioctl.h>
  5. #include <sys/socket.h>
  6. #include <sys/time.h>
  7. #include <netinet/in.h>
  8.  
  9. #include <netdb.h>
  10.  
  11. #define HISPORT 919        /* 'cos that's what tektronix use */
  12. #define MAXHOSTS 200
  13. #define SLEEPTIME 300        /* 5 mins wait at end of loop */
  14.  
  15. #include "hostup.h"
  16.  
  17. extern int errno;
  18.  
  19. struct hostent *gethostent(), *host;
  20. int inetcomp();
  21. int s;                /* socket number */
  22. FILE *f;            /* output file */
  23. struct timezone timezone;    /* somewhere to ignore it */
  24.  
  25. struct perhost thishost[MAXHOSTS];
  26. int nohosts = 0;
  27.  
  28. main()
  29. {
  30.   if ((f = fopen(FILENAME, "w")) == NULL) {
  31.     perror(FILENAME);
  32.     exit(1);
  33.   }
  34.   readhosts();            /* find a table of hosts to poll */
  35. #ifndef DEBUG
  36.   if (fork())
  37.     exit(0);
  38.   { int s;
  39.     for (s = 0; s < 10; s++)
  40.       if (s != fileno(f))
  41.     (void) close(s);
  42.     (void) open("/", 0);
  43.     (void) dup2(0, 1);
  44.     (void) dup2(0, 2);
  45.     s = open("/dev/tty", 2);
  46.     if (s >= 0) {
  47.       ioctl(s, TIOCNOTTY, 0);
  48.       (void) close(s);
  49.     }
  50.   }
  51. #endif
  52.   while(1) {
  53.     fseek(f, 0L, 0);        /* keep writing from start */
  54.     pollhosts();
  55.     sleep(SLEEPTIME);
  56.   }
  57. }
  58.  
  59. readhosts()
  60. {
  61.   while ((host = gethostent()) != NULL) {
  62.     strncpy(thishost[nohosts].ph_name, host->h_name, MAXHOSTCHARS);
  63.     thishost[nohosts].ph_name[MAXHOSTCHARS-1] = '\0';
  64.     bzero((char *)&thishost[nohosts].ph_to, sizeof(thishost[nohosts].ph_to));
  65.     bcopy(host->h_addr, (char *)&thishost[nohosts].ph_to.sin_addr, host->h_length);
  66.     thishost[nohosts].ph_to.sin_family = host->h_addrtype;
  67.     thishost[nohosts].ph_to.sin_port = HISPORT;
  68.     nohosts++;
  69.     if (nohosts == MAXHOSTS)
  70.       break;
  71.   }
  72.   endhostent();
  73.   qsort(thishost, nohosts, sizeof(thishost[0]), inetcomp);
  74. }
  75.  
  76. inetcomp(a, b)
  77.      struct perhost *a, *b;
  78. {
  79.   return(ntohl(a->ph_to.sin_addr.s_addr) - ntohl(b->ph_to.sin_addr.s_addr));
  80. }
  81.  
  82. pollhosts()
  83. {
  84.   int i;
  85.   for (i = 0; i < nohosts; i++) {
  86.     s = socket(AF_INET, SOCK_STREAM, 0);
  87.     if (s < 0) {
  88. #ifdef DEBUG
  89.       perror("socket:");
  90. #endif      
  91.       exit(1);
  92.     }
  93.     if (connect(s, (char *)&thishost[i].ph_to,
  94.         sizeof(thishost[i].ph_to)) < 0) {
  95.       thishost[i].ph_result = errno;
  96.     } else {
  97.       thishost[i].ph_result = 0;
  98.     }
  99.     gettimeofday(&thishost[i].ph_time, &timezone);
  100.     fwrite(&thishost[i], sizeof(thishost[i]), 1, f);
  101.     fflush(f);
  102.     (void) close(s);            /* ! */
  103.   }
  104. }
  105.