|
|
1.1 ! root 1: #ifndef lint ! 2: static char sccsid[] = "@(#)gettable.c 4.3 (Berkeley) 10/5/83"; ! 3: #endif ! 4: ! 5: #include <sys/types.h> ! 6: #include <sys/socket.h> ! 7: ! 8: #include <netinet/in.h> ! 9: ! 10: #include <stdio.h> ! 11: #include <netdb.h> ! 12: ! 13: #define OUTFILE "hosts.txt" /* default output file */ ! 14: #define QUERY "ALL\r\n" /* query to hostname server */ ! 15: ! 16: #define equaln(s1, s2, n) (!strncmp(s1, s2, n)) ! 17: ! 18: struct sockaddr_in sin; ! 19: char buf[BUFSIZ]; ! 20: char *outfile = OUTFILE; ! 21: ! 22: main(argc, argv) ! 23: int argc; ! 24: char *argv[]; ! 25: { ! 26: int s; ! 27: register len; ! 28: register FILE *sfi, *sfo, *hf; ! 29: register char *p; ! 30: char *host; ! 31: register struct hostent *hp; ! 32: struct servent *sp; ! 33: ! 34: argv++, argc--; ! 35: if (argc < 1 || argc > 2) { ! 36: fprintf(stderr, "usage: gettable host [ file ]\n"); ! 37: exit(1); ! 38: } ! 39: sp = getservbyname("hostnames", "tcp"); ! 40: if (sp == NULL) { ! 41: fprintf(stderr, "gettable: hostnames/tcp: unknown service\n"); ! 42: exit(3); ! 43: } ! 44: host = *argv; ! 45: argv++, argc--; ! 46: hp = gethostbyname(host); ! 47: if (hp == NULL) { ! 48: fprintf(stderr, "gettable: %s: host unknown\n", host); ! 49: exit(2); ! 50: } ! 51: host = hp->h_name; ! 52: if (argc > 0) ! 53: outfile = *argv; ! 54: sin.sin_family = hp->h_addrtype; ! 55: s = socket(hp->h_addrtype, SOCK_STREAM, 0, 0); ! 56: if (s < 0) { ! 57: perror("gettable: socket"); ! 58: exit(4); ! 59: } ! 60: if (bind(s, &sin, sizeof (sin), 0) < 0) { ! 61: perror("gettable: bind"); ! 62: exit(5); ! 63: } ! 64: bcopy(hp->h_addr, &sin.sin_addr, hp->h_length); ! 65: sin.sin_port = sp->s_port; ! 66: if (connect(s, &sin, sizeof (sin), 0) < 0) { ! 67: perror("gettable: connect"); ! 68: exit(6); ! 69: } ! 70: fprintf(stderr, "Connection to %s opened.\n", host); ! 71: sfi = fdopen(s, "r"); ! 72: sfo = fdopen(s, "w"); ! 73: if (sfi == NULL || sfo == NULL) { ! 74: perror("gettable: fdopen"); ! 75: close(s); ! 76: exit(1); ! 77: } ! 78: hf = fopen(outfile, "w"); ! 79: if (hf == NULL) { ! 80: fprintf(stderr, "gettable: "); perror(outfile); ! 81: close(s); ! 82: exit(1); ! 83: } ! 84: fprintf(sfo, QUERY); ! 85: fflush(sfo); ! 86: while (fgets(buf, sizeof(buf), sfi) != NULL) { ! 87: len = strlen(buf); ! 88: buf[len-2] = '\0'; ! 89: if (equaln(buf, "BEGIN", 5) || equaln(buf, "END", 3)) { ! 90: continue; ! 91: } ! 92: if (equaln(buf, "ERR", 3)) { ! 93: fprintf(stderr, "gettable: hostnames error: %s", buf); ! 94: continue; ! 95: } ! 96: fprintf(hf, "%s\n", buf); ! 97: } ! 98: fclose(hf); ! 99: fprintf(stderr, "Host table received.\n"); ! 100: close(s); ! 101: fprintf(stderr, "Connection to %s closed\n", host); ! 102: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.