home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
-
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <sys/time.h>
- #include <netinet/in.h>
-
- #include <netdb.h>
-
- #define HISPORT 919 /* 'cos that's what tektronix use */
- #define MAXHOSTS 200
- #define SLEEPTIME 300 /* 5 mins wait at end of loop */
-
- #include "hostup.h"
-
- extern int errno;
-
- struct hostent *gethostent(), *host;
- int inetcomp();
- int s; /* socket number */
- FILE *f; /* output file */
- struct timezone timezone; /* somewhere to ignore it */
-
- struct perhost thishost[MAXHOSTS];
- int nohosts = 0;
-
- main()
- {
- if ((f = fopen(FILENAME, "w")) == NULL) {
- perror(FILENAME);
- exit(1);
- }
- readhosts(); /* find a table of hosts to poll */
- #ifndef DEBUG
- if (fork())
- exit(0);
- { int s;
- for (s = 0; s < 10; s++)
- if (s != fileno(f))
- (void) close(s);
- (void) open("/", 0);
- (void) dup2(0, 1);
- (void) dup2(0, 2);
- s = open("/dev/tty", 2);
- if (s >= 0) {
- ioctl(s, TIOCNOTTY, 0);
- (void) close(s);
- }
- }
- #endif
- while(1) {
- fseek(f, 0L, 0); /* keep writing from start */
- pollhosts();
- sleep(SLEEPTIME);
- }
- }
-
- readhosts()
- {
- while ((host = gethostent()) != NULL) {
- strncpy(thishost[nohosts].ph_name, host->h_name, MAXHOSTCHARS);
- thishost[nohosts].ph_name[MAXHOSTCHARS-1] = '\0';
- bzero((char *)&thishost[nohosts].ph_to, sizeof(thishost[nohosts].ph_to));
- bcopy(host->h_addr, (char *)&thishost[nohosts].ph_to.sin_addr, host->h_length);
- thishost[nohosts].ph_to.sin_family = host->h_addrtype;
- thishost[nohosts].ph_to.sin_port = HISPORT;
- nohosts++;
- if (nohosts == MAXHOSTS)
- break;
- }
- endhostent();
- qsort(thishost, nohosts, sizeof(thishost[0]), inetcomp);
- }
-
- inetcomp(a, b)
- struct perhost *a, *b;
- {
- return(ntohl(a->ph_to.sin_addr.s_addr) - ntohl(b->ph_to.sin_addr.s_addr));
- }
-
- pollhosts()
- {
- int i;
- for (i = 0; i < nohosts; i++) {
- s = socket(AF_INET, SOCK_STREAM, 0);
- if (s < 0) {
- #ifdef DEBUG
- perror("socket:");
- #endif
- exit(1);
- }
- if (connect(s, (char *)&thishost[i].ph_to,
- sizeof(thishost[i].ph_to)) < 0) {
- thishost[i].ph_result = errno;
- } else {
- thishost[i].ph_result = 0;
- }
- gettimeofday(&thishost[i].ph_time, &timezone);
- fwrite(&thishost[i], sizeof(thishost[i]), 1, f);
- fflush(f);
- (void) close(s); /* ! */
- }
- }
-